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_var.h> 82 #include <netinet/ip6.h> 83 #include <netinet6/in6_pcb.h> 84 #include <netinet6/ip6_var.h> 85 #define TCPOUTFLAGS 86 #include <netinet/tcp.h> 87 #include <netinet/tcp_fsm.h> 88 #include <netinet/tcp_seq.h> 89 #include <netinet/tcp_timer.h> 90 #include <netinet/tcp_var.h> 91 #include <netinet/tcpip.h> 92 #include <netinet/tcp_hpts.h> 93 #include <netinet/cc/cc.h> 94 #include <netinet/tcp_log_buf.h> 95 #include <netinet/tcp_ratelimit.h> 96 #include <netinet/tcp_lro.h> 97 #ifdef TCP_OFFLOAD 98 #include <netinet/tcp_offload.h> 99 #endif 100 #ifdef INET6 101 #include <netinet6/tcp6_var.h> 102 #endif 103 #include <netinet/tcp_fastopen.h> 104 105 #include <netipsec/ipsec_support.h> 106 #include <net/if.h> 107 #include <net/if_var.h> 108 #include <net/ethernet.h> 109 110 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 111 #include <netipsec/ipsec.h> 112 #include <netipsec/ipsec6.h> 113 #endif /* IPSEC */ 114 115 #include <netinet/udp.h> 116 #include <netinet/udp_var.h> 117 #include <machine/in_cksum.h> 118 119 #ifdef MAC 120 #include <security/mac/mac_framework.h> 121 #endif 122 123 #include "sack_filter.h" 124 #include "tcp_bbr.h" 125 #include "rack_bbr_common.h" 126 uma_zone_t bbr_zone; 127 uma_zone_t bbr_pcb_zone; 128 129 struct sysctl_ctx_list bbr_sysctl_ctx; 130 struct sysctl_oid *bbr_sysctl_root; 131 132 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \ 133 (tv) = (value); \ 134 if ((u_long)(tv) < (u_long)(tvmin)) \ 135 (tv) = (tvmin); \ 136 if ((u_long)(tv) > (u_long)(tvmax)) \ 137 (tv) = (tvmax); \ 138 } while(0) 139 140 /*#define BBR_INVARIANT 1*/ 141 142 /* 143 * initial window 144 */ 145 static uint32_t bbr_def_init_win = 10; 146 static int32_t bbr_persist_min = 250000; /* 250ms */ 147 static int32_t bbr_persist_max = 1000000; /* 1 Second */ 148 static int32_t bbr_cwnd_may_shrink = 0; 149 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP; 150 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT; 151 static int32_t bbr_hardware_pacing_limit = 8000; 152 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */ 153 static int32_t bbr_no_retran = 0; 154 155 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */ 156 static int32_t bbr_max_net_error_cnt = 10; 157 /* Should the following be dynamic too -- loss wise */ 158 static int32_t bbr_rtt_gain_thresh = 0; 159 /* Measurement controls */ 160 static int32_t bbr_use_google_algo = 1; 161 static int32_t bbr_ts_limiting = 1; 162 static int32_t bbr_ts_can_raise = 0; 163 static int32_t bbr_do_red = 600; 164 static int32_t bbr_red_scale = 20000; 165 static int32_t bbr_red_mul = 1; 166 static int32_t bbr_red_div = 2; 167 static int32_t bbr_red_growth_restrict = 1; 168 static int32_t bbr_target_is_bbunit = 0; 169 static int32_t bbr_drop_limit = 0; 170 /* 171 * How much gain do we need to see to 172 * stay in startup? 173 */ 174 static int32_t bbr_marks_rxt_sack_passed = 0; 175 static int32_t bbr_start_exit = 25; 176 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */ 177 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */ 178 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */ 179 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this 180 * if we go back ever to where the pacer 181 * has priority over timers. 182 */ 183 static int32_t bbr_policer_call_from_rack_to = 0; 184 static int32_t bbr_policer_detection_enabled = 1; 185 static int32_t bbr_min_measurements_req = 1; /* We need at least 2 186 * measurements before we are 187 * "good" note that 2 == 1. 188 * This is because we use a > 189 * comparison. This means if 190 * min_measure was 0, it takes 191 * num-measures > min(0) and 192 * you get 1 measurement and 193 * you are good. Set to 1, you 194 * have to have two 195 * measurements (this is done 196 * to prevent it from being ok 197 * to have no measurements). */ 198 static int32_t bbr_no_pacing_until = 4; 199 200 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */ 201 static int32_t bbr_min_peer_delta = 20; /* 20 units */ 202 static int32_t bbr_delta_percent = 150; /* 15.0 % */ 203 204 static int32_t bbr_target_cwnd_mult_limit = 8; 205 /* 206 * bbr_cwnd_min_val is the number of 207 * segments we hold to in the RTT probe 208 * state typically 4. 209 */ 210 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS; 211 212 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS; 213 214 static int32_t bbr_gain_to_target = 1; 215 static int32_t bbr_gain_gets_extra_too = 1; 216 /* 217 * bbr_high_gain is the 2/ln(2) value we need 218 * to double the sending rate in startup. This 219 * is used for both cwnd and hptsi gain's. 220 */ 221 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1; 222 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1; 223 static int32_t bbr_use_lower_gain_in_startup = 1; 224 225 /* thresholds for reduction on drain in sub-states/drain */ 226 static int32_t bbr_drain_rtt = BBR_SRTT; 227 static int32_t bbr_drain_floor = 88; 228 static int32_t google_allow_early_out = 1; 229 static int32_t google_consider_lost = 1; 230 static int32_t bbr_drain_drop_mul = 4; 231 static int32_t bbr_drain_drop_div = 5; 232 static int32_t bbr_rand_ot = 50; 233 static int32_t bbr_can_force_probertt = 0; 234 static int32_t bbr_can_adjust_probertt = 1; 235 static int32_t bbr_probertt_sets_rtt = 0; 236 static int32_t bbr_can_use_ts_for_rtt = 1; 237 static int32_t bbr_is_ratio = 0; 238 static int32_t bbr_sub_drain_app_limit = 1; 239 static int32_t bbr_prtt_slam_cwnd = 1; 240 static int32_t bbr_sub_drain_slam_cwnd = 1; 241 static int32_t bbr_slam_cwnd_in_main_drain = 1; 242 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter 243 * hold */ 244 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4); 245 /* 246 * bbr_drain_gain is the reverse of the high_gain 247 * designed to drain back out the standing queue 248 * that is formed in startup by causing a larger 249 * hptsi gain and thus drainging the packets 250 * in flight. 251 */ 252 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885; 253 static int32_t bbr_rttprobe_gain = 192; 254 255 /* 256 * The cwnd_gain is the default cwnd gain applied when 257 * calculating a target cwnd. Note that the cwnd is 258 * a secondary factor in the way BBR works (see the 259 * paper and think about it, it will take some time). 260 * Basically the hptsi_gain spreads the packets out 261 * so you never get more than BDP to the peer even 262 * if the cwnd is high. In our implemenation that 263 * means in non-recovery/retransmission scenarios 264 * cwnd will never be reached by the flight-size. 265 */ 266 static int32_t bbr_cwnd_gain = BBR_UNIT * 2; 267 static int32_t bbr_tlp_type_to_use = BBR_SRTT; 268 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */ 269 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */ 270 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */ 271 static int32_t bbr_ignore_data_after_close = 1; 272 static int16_t bbr_hptsi_gain[] = { 273 (BBR_UNIT *5 / 4), 274 (BBR_UNIT * 3 / 4), 275 BBR_UNIT, 276 BBR_UNIT, 277 BBR_UNIT, 278 BBR_UNIT, 279 BBR_UNIT, 280 BBR_UNIT 281 }; 282 int32_t bbr_use_rack_resend_cheat = 1; 283 int32_t bbr_sends_full_iwnd = 1; 284 285 #define BBR_HPTSI_GAIN_MAX 8 286 /* 287 * The BBR module incorporates a number of 288 * TCP ideas that have been put out into the IETF 289 * over the last few years: 290 * - Yuchung Cheng's RACK TCP (for which its named) that 291 * will stop us using the number of dup acks and instead 292 * use time as the gage of when we retransmit. 293 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 294 * of Dukkipati et.al. 295 * - Van Jacobson's et.al BBR. 296 * 297 * RACK depends on SACK, so if an endpoint arrives that 298 * cannot do SACK the state machine below will shuttle the 299 * connection back to using the "default" TCP stack that is 300 * in FreeBSD. 301 * 302 * To implement BBR and RACK the original TCP stack was first decomposed 303 * into a functional state machine with individual states 304 * for each of the possible TCP connection states. The do_segment 305 * functions role in life is to mandate the connection supports SACK 306 * initially and then assure that the RACK state matches the conenction 307 * state before calling the states do_segment function. Data processing 308 * of inbound segments also now happens in the hpts_do_segment in general 309 * with only one exception. This is so we can keep the connection on 310 * a single CPU. 311 * 312 * Each state is simplified due to the fact that the original do_segment 313 * has been decomposed and we *know* what state we are in (no 314 * switches on the state) and all tests for SACK are gone. This 315 * greatly simplifies what each state does. 316 * 317 * TCP output is also over-written with a new version since it 318 * must maintain the new rack scoreboard and has had hptsi 319 * integrated as a requirment. Still todo is to eliminate the 320 * use of the callout_() system and use the hpts for all 321 * timers as well. 322 */ 323 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */ 324 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */ 325 static const int32_t bbr_min_req_free = 2; /* The min we must have on the 326 * free list */ 327 static int32_t bbr_tlp_thresh = 1; 328 static int32_t bbr_reorder_thresh = 2; 329 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def 330 * 60,000,000 - 60 seconds */ 331 static int32_t bbr_pkt_delay = 1000; 332 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */ 333 static int32_t bbr_incr_timers = 1; 334 335 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */ 336 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */ 337 static int32_t bbr_exit_startup_at_loss = 1; 338 339 /* 340 * bbr_lt_bw_ratio is 1/8th 341 * bbr_lt_bw_diff is < 4 Kbit/sec 342 */ 343 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */ 344 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */ 345 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use 346 * the lt_bw for */ 347 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure 348 * lt_bw */ 349 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */ 350 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */ 351 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */ 352 353 static int32_t bbr_verbose_logging = 0; 354 /* 355 * Currently regular tcp has a rto_min of 30ms 356 * the backoff goes 12 times so that ends up 357 * being a total of 122.850 seconds before a 358 * connection is killed. 359 */ 360 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */ 361 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */ 362 363 /****************************************************/ 364 /* DEFAULT TSO SIZING (cpu performance impacting) */ 365 /****************************************************/ 366 /* What amount is our formula using to get TSO size */ 367 static int32_t bbr_hptsi_per_second = 1000; 368 369 /* 370 * For hptsi under bbr_cross_over connections what is delay 371 * target 7ms (in usec) combined with a seg_max of 2 372 * gets us close to identical google behavior in 373 * TSO size selection (possibly more 1MSS sends). 374 */ 375 static int32_t bbr_hptsi_segments_delay_tar = 7000; 376 377 /* Does pacing delay include overhead's in its time calculations? */ 378 static int32_t bbr_include_enet_oh = 0; 379 static int32_t bbr_include_ip_oh = 1; 380 static int32_t bbr_include_tcp_oh = 1; 381 static int32_t bbr_google_discount = 10; 382 383 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */ 384 static int32_t bbr_state_is_pkt_epoch = 0; 385 static int32_t bbr_state_drain_2_tar = 1; 386 /* What is the max the 0 - bbr_cross_over MBPS TSO target 387 * can reach using our delay target. Note that this 388 * value becomes the floor for the cross over 389 * algorithm. 390 */ 391 static int32_t bbr_hptsi_segments_max = 2; 392 static int32_t bbr_hptsi_segments_floor = 1; 393 static int32_t bbr_hptsi_utter_max = 0; 394 395 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */ 396 static int32_t bbr_hptsi_bytes_min = 1460; 397 static int32_t bbr_all_get_min = 0; 398 399 /* Cross over point from algo-a to algo-b */ 400 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS; 401 402 /* Do we deal with our restart state? */ 403 static int32_t bbr_uses_idle_restart = 0; 404 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */ 405 406 /* Do we allow hardware pacing? */ 407 static int32_t bbr_allow_hdwr_pacing = 0; 408 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */ 409 static int32_t bbr_hdwr_pace_floor = 1; 410 static int32_t bbr_hdwr_pacing_delay_cnt = 10; 411 412 /****************************************************/ 413 static int32_t bbr_resends_use_tso = 0; 414 static int32_t bbr_tlp_max_resend = 2; 415 static int32_t bbr_sack_block_limit = 128; 416 417 #define BBR_MAX_STAT 19 418 counter_u64_t bbr_state_time[BBR_MAX_STAT]; 419 counter_u64_t bbr_state_lost[BBR_MAX_STAT]; 420 counter_u64_t bbr_state_resend[BBR_MAX_STAT]; 421 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE]; 422 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE]; 423 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE]; 424 counter_u64_t bbr_flows_whdwr_pacing; 425 counter_u64_t bbr_flows_nohdwr_pacing; 426 427 counter_u64_t bbr_nohdwr_pacing_enobuf; 428 counter_u64_t bbr_hdwr_pacing_enobuf; 429 430 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr); 431 432 /* 433 * Static defintions we need for forward declarations. 434 */ 435 static uint32_t 436 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, 437 uint32_t useconds_time, uint64_t bw); 438 static uint32_t 439 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain); 440 static void 441 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win); 442 static void 443 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses); 444 static void 445 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line, 446 int dolog); 447 static uint32_t 448 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain); 449 static void 450 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, 451 int32_t pkt_epoch, uint32_t losses); 452 static uint32_t 453 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, 454 struct bbr_sendmap *rsm); 455 static uint32_t 456 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp); 457 static uint32_t 458 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 459 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts); 460 static void 461 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 462 int32_t line); 463 static void 464 bbr_set_state_target(struct tcp_bbr *bbr, int line); 465 static void 466 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line); 467 static void 468 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, 469 int event, int line); 470 static void 471 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts); 472 static void 473 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts); 474 static void 475 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 476 uint32_t rtt, uint32_t line, uint8_t is_start, 477 uint16_t set); 478 static struct bbr_sendmap * 479 bbr_find_lowest_rsm(struct tcp_bbr *bbr); 480 static __inline uint32_t 481 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type); 482 static void 483 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, 484 uint8_t which); 485 static void 486 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, 487 uint32_t time_since_sent, uint32_t srtt, 488 uint32_t thresh, uint32_t to); 489 static void 490 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag); 491 static void 492 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, 493 uint32_t del_by, uint32_t cts, uint32_t sloton, 494 uint32_t prev_delay); 495 static void 496 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 497 int32_t line); 498 static void 499 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr); 500 static void 501 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts); 502 static void 503 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts); 504 static void 505 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts); 506 static void 507 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 508 uint32_t cts, uint32_t usecs, uint64_t bw, 509 uint32_t override, int mod); 510 static int bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt); 511 512 static inline uint8_t 513 bbr_state_val(struct tcp_bbr *bbr) 514 { 515 return(bbr->rc_bbr_substate); 516 } 517 518 static inline uint32_t 519 get_min_cwnd(struct tcp_bbr *bbr) 520 { 521 int mss; 522 523 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 524 bbr->r_ctl.rc_pace_max_segs); 525 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED) 526 return (bbr_cwnd_min_val_hs * mss); 527 else 528 return (bbr_cwnd_min_val * mss); 529 } 530 531 static uint32_t 532 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr) 533 { 534 uint64_t srtt, var; 535 uint64_t ret_val; 536 537 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 538 if (tp->t_srtt == 0) { 539 srtt = (uint64_t)BBR_INITIAL_RTO; 540 var = 0; 541 } else { 542 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 543 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT); 544 } 545 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]), 546 bbr_persist_min, bbr_persist_max); 547 return ((uint32_t)ret_val); 548 } 549 550 static uint32_t 551 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 552 { 553 /* 554 * Start the FR timer, we do this based on getting the first one in 555 * the rc_tmap. Note that if its NULL we must stop the timer. in all 556 * events we need to stop the running timer (if its running) before 557 * starting the new one. 558 */ 559 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 560 int32_t idx; 561 int32_t is_tlp_timer = 0; 562 struct bbr_sendmap *rsm; 563 564 if (bbr->rc_all_timers_stopped) { 565 /* All timers have been stopped none are to run */ 566 return (0); 567 } 568 if (bbr->rc_in_persist) { 569 /* We can't start any timer in persists */ 570 return (bbr_get_persists_timer_val(tp, bbr)); 571 } 572 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 573 if ((rsm == NULL) || 574 ((tp->t_flags & TF_SACK_PERMIT) == 0) || 575 (tp->t_state < TCPS_ESTABLISHED)) { 576 /* Nothing on the send map */ 577 activate_rxt: 578 if (SEQ_LT(tp->snd_una, tp->snd_max) || 579 sbavail(&tptosocket(tp)->so_snd)) { 580 uint64_t tov; 581 582 time_since_sent = 0; 583 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 584 if (rsm) { 585 idx = rsm->r_rtr_cnt - 1; 586 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 587 tstmp_touse = rsm->r_tim_lastsent[idx]; 588 else 589 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 590 if (TSTMP_GT(tstmp_touse, cts)) 591 time_since_sent = cts - tstmp_touse; 592 } 593 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 594 if (tp->t_srtt == 0) 595 tov = BBR_INITIAL_RTO; 596 else 597 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) + 598 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT); 599 if (tp->t_rxtshift) 600 tov *= tcp_backoff[tp->t_rxtshift]; 601 if (tov > time_since_sent) 602 tov -= time_since_sent; 603 else 604 tov = bbr->r_ctl.rc_min_to; 605 TCPT_RANGESET_NOSLOP(to, tov, 606 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC), 607 (bbr->rc_max_rto_sec * USECS_IN_SECOND)); 608 bbr_log_timer_var(bbr, 2, cts, 0, bbr_get_rtt(bbr, BBR_SRTT), 0, to); 609 return (to); 610 } 611 return (0); 612 } 613 if (rsm->r_flags & BBR_ACKED) { 614 rsm = bbr_find_lowest_rsm(bbr); 615 if (rsm == NULL) { 616 /* No lowest? */ 617 goto activate_rxt; 618 } 619 } 620 /* Convert from ms to usecs */ 621 if (rsm->r_flags & BBR_SACK_PASSED) { 622 if ((tp->t_flags & TF_SENTFIN) && 623 ((tp->snd_max - tp->snd_una) == 1) && 624 (rsm->r_flags & BBR_HAS_FIN)) { 625 /* 626 * We don't start a bbr rack timer if all we have is 627 * a FIN outstanding. 628 */ 629 goto activate_rxt; 630 } 631 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK); 632 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm); 633 idx = rsm->r_rtr_cnt - 1; 634 exp = rsm->r_tim_lastsent[idx] + thresh; 635 if (SEQ_GEQ(exp, cts)) { 636 to = exp - cts; 637 if (to < bbr->r_ctl.rc_min_to) { 638 to = bbr->r_ctl.rc_min_to; 639 } 640 } else { 641 to = bbr->r_ctl.rc_min_to; 642 } 643 } else { 644 /* Ok we need to do a TLP not RACK */ 645 if (bbr->rc_tlp_in_progress != 0) { 646 /* 647 * The previous send was a TLP. 648 */ 649 goto activate_rxt; 650 } 651 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 652 if (rsm == NULL) { 653 /* We found no rsm to TLP with. */ 654 goto activate_rxt; 655 } 656 if (rsm->r_flags & BBR_HAS_FIN) { 657 /* If its a FIN we don't do TLP */ 658 rsm = NULL; 659 goto activate_rxt; 660 } 661 time_since_sent = 0; 662 idx = rsm->r_rtr_cnt - 1; 663 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 664 tstmp_touse = rsm->r_tim_lastsent[idx]; 665 else 666 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 667 if (TSTMP_GT(tstmp_touse, cts)) 668 time_since_sent = cts - tstmp_touse; 669 is_tlp_timer = 1; 670 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use); 671 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts); 672 if (thresh > time_since_sent) 673 to = thresh - time_since_sent; 674 else 675 to = bbr->r_ctl.rc_min_to; 676 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 677 /* 678 * If the TLP time works out to larger than the max 679 * RTO lets not do TLP.. just RTO. 680 */ 681 goto activate_rxt; 682 } 683 if ((bbr->rc_tlp_rtx_out == 1) && 684 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) { 685 /* 686 * Second retransmit of the same TLP 687 * lets not. 688 */ 689 bbr->rc_tlp_rtx_out = 0; 690 goto activate_rxt; 691 } 692 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) { 693 /* 694 * The tail is no longer the last one I did a probe 695 * on 696 */ 697 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 698 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 699 } 700 } 701 if (is_tlp_timer == 0) { 702 BBR_STAT_INC(bbr_to_arm_rack); 703 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 704 } else { 705 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to); 706 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 707 /* 708 * We have exceeded how many times we can retran the 709 * current TLP timer, switch to the RTO timer. 710 */ 711 goto activate_rxt; 712 } else { 713 BBR_STAT_INC(bbr_to_arm_tlp); 714 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 715 } 716 } 717 return (to); 718 } 719 720 static inline int32_t 721 bbr_minseg(struct tcp_bbr *bbr) 722 { 723 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options); 724 } 725 726 static void 727 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len) 728 { 729 struct inpcb *inp = tptoinpcb(tp); 730 struct hpts_diag diag; 731 uint32_t delayed_ack = 0; 732 uint32_t left = 0; 733 uint32_t hpts_timeout; 734 uint8_t stopped; 735 int32_t delay_calc = 0; 736 uint32_t prev_delay = 0; 737 738 if (tcp_in_hpts(tp)) { 739 /* A previous call is already set up */ 740 return; 741 } 742 if ((tp->t_state == TCPS_CLOSED) || 743 (tp->t_state == TCPS_LISTEN)) { 744 return; 745 } 746 stopped = bbr->rc_tmr_stopped; 747 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 748 left = bbr->r_ctl.rc_timer_exp - cts; 749 } 750 bbr->r_ctl.rc_hpts_flags = 0; 751 bbr->r_ctl.rc_timer_exp = 0; 752 prev_delay = bbr->r_ctl.rc_last_delay_val; 753 if (bbr->r_ctl.rc_last_delay_val && 754 (slot == 0)) { 755 /* 756 * If a previous pacer delay was in place we 757 * are not coming from the output side (where 758 * we calculate a delay, more likely a timer). 759 */ 760 slot = bbr->r_ctl.rc_last_delay_val; 761 if (TSTMP_GT(cts, bbr->rc_pacer_started)) { 762 /* Compensate for time passed */ 763 delay_calc = cts - bbr->rc_pacer_started; 764 if (delay_calc <= slot) 765 slot -= delay_calc; 766 } 767 } 768 /* Do we have early to make up for by pushing out the pacing time? */ 769 if (bbr->r_agg_early_set) { 770 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2); 771 slot += bbr->r_ctl.rc_agg_early; 772 bbr->r_ctl.rc_agg_early = 0; 773 bbr->r_agg_early_set = 0; 774 } 775 /* Are we running a total debt that needs to be compensated for? */ 776 if (bbr->r_ctl.rc_hptsi_agg_delay) { 777 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) { 778 /* We nuke the delay */ 779 slot -= bbr->r_ctl.rc_hptsi_agg_delay; 780 bbr->r_ctl.rc_hptsi_agg_delay = 0; 781 } else { 782 /* We nuke some of the delay, put in a minimal 100usecs */ 783 bbr->r_ctl.rc_hptsi_agg_delay -= slot; 784 bbr->r_ctl.rc_last_delay_val = slot = 100; 785 } 786 } 787 bbr->r_ctl.rc_last_delay_val = slot; 788 hpts_timeout = bbr_timer_start(tp, bbr, cts); 789 if (tp->t_flags & TF_DELACK) { 790 if (bbr->rc_in_persist == 0) { 791 delayed_ack = bbr_delack_time; 792 } else { 793 /* 794 * We are in persists and have 795 * gotten a new data element. 796 */ 797 if (hpts_timeout > bbr_delack_time) { 798 /* 799 * Lets make the persists timer (which acks) 800 * be the smaller of hpts_timeout and bbr_delack_time. 801 */ 802 hpts_timeout = bbr_delack_time; 803 } 804 } 805 } 806 if (delayed_ack && 807 ((hpts_timeout == 0) || 808 (delayed_ack < hpts_timeout))) { 809 /* We need a Delayed ack timer */ 810 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 811 hpts_timeout = delayed_ack; 812 } 813 if (slot) { 814 /* Mark that we have a pacing timer up */ 815 BBR_STAT_INC(bbr_paced_segments); 816 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 817 } 818 /* 819 * If no timers are going to run and we will fall off thfe hptsi 820 * wheel, we resort to a keep-alive timer if its configured. 821 */ 822 if ((hpts_timeout == 0) && 823 (slot == 0)) { 824 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 825 (tp->t_state <= TCPS_CLOSING)) { 826 /* 827 * Ok we have no timer (persists, rack, tlp, rxt or 828 * del-ack), we don't have segments being paced. So 829 * all that is left is the keepalive timer. 830 */ 831 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 832 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 833 } else { 834 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 835 } 836 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 837 } 838 } 839 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 840 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 841 /* 842 * RACK, TLP, persists and RXT timers all are restartable 843 * based on actions input .. i.e we received a packet (ack 844 * or sack) and that changes things (rw, or snd_una etc). 845 * Thus we can restart them with a new value. For 846 * keep-alive, delayed_ack we keep track of what was left 847 * and restart the timer with a smaller value. 848 */ 849 if (left < hpts_timeout) 850 hpts_timeout = left; 851 } 852 if (bbr->r_ctl.rc_incr_tmrs && slot && 853 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 854 /* 855 * If configured to do so, and the timer is either 856 * the TLP or RXT timer, we need to increase the timeout 857 * by the pacing time. Consider the bottleneck at my 858 * machine as an example, we are sending something 859 * to start a TLP on. The last packet won't be emitted 860 * fully until the pacing time (the bottleneck will hold 861 * the data in place). Once the packet is emitted that 862 * is when we want to start waiting for the TLP. This 863 * is most evident with hardware pacing (where the nic 864 * is holding the packet(s) before emitting). But it 865 * can also show up in the network so we do it for all 866 * cases. Technically we would take off one packet from 867 * this extra delay but this is easier and being more 868 * conservative is probably better. 869 */ 870 hpts_timeout += slot; 871 } 872 if (hpts_timeout) { 873 /* 874 * Hack alert for now we can't time-out over 2147 seconds (a 875 * bit more than 35min) 876 */ 877 if (hpts_timeout > 0x7ffffffe) 878 hpts_timeout = 0x7ffffffe; 879 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 880 } else 881 bbr->r_ctl.rc_timer_exp = 0; 882 if ((slot) && 883 (bbr->rc_use_google || 884 bbr->output_error_seen || 885 (slot <= hpts_timeout)) ) { 886 /* 887 * Tell LRO that it can queue packets while 888 * we pace. 889 */ 890 bbr->rc_tp->t_flags2 |= TF2_MBUF_QUEUE_READY; 891 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 892 (bbr->rc_cwnd_limited == 0)) { 893 /* 894 * If we are not cwnd limited and we 895 * are running a rack timer we put on 896 * the do not disturbe even for sack. 897 */ 898 tp->t_flags2 |= TF2_DONT_SACK_QUEUE; 899 } else 900 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 901 bbr->rc_pacer_started = cts; 902 903 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot), 904 __LINE__, &diag); 905 bbr->rc_timer_first = 0; 906 bbr->bbr_timer_src = frm; 907 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1); 908 bbr_log_hpts_diag(bbr, cts, &diag); 909 } else if (hpts_timeout) { 910 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout), 911 __LINE__, &diag); 912 /* 913 * We add the flag here as well if the slot is set, 914 * since hpts will call in to clear the queue first before 915 * calling the output routine (which does our timers). 916 * We don't want to set the flag if its just a timer 917 * else the arrival of data might (that causes us 918 * to send more) might get delayed. Imagine being 919 * on a keep-alive timer and a request comes in for 920 * more data. 921 */ 922 if (slot) 923 bbr->rc_pacer_started = cts; 924 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 925 (bbr->rc_cwnd_limited == 0)) { 926 /* 927 * For a rack timer, don't wake us even 928 * if a sack arrives as long as we are 929 * not cwnd limited. 930 */ 931 tp->t_flags2 |= (TF2_MBUF_QUEUE_READY | 932 TF2_DONT_SACK_QUEUE); 933 } else { 934 /* All other timers wake us up */ 935 tp->t_flags2 &= ~(TF2_MBUF_QUEUE_READY | 936 TF2_DONT_SACK_QUEUE); 937 } 938 bbr->bbr_timer_src = frm; 939 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0); 940 bbr_log_hpts_diag(bbr, cts, &diag); 941 bbr->rc_timer_first = 1; 942 } 943 bbr->rc_tmr_stopped = 0; 944 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay); 945 } 946 947 static void 948 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb) 949 { 950 /* 951 * We received an ack, and then did not call send or were bounced 952 * out due to the hpts was running. Now a timer is up as well, is it 953 * the right timer? 954 */ 955 struct inpcb *inp; 956 struct bbr_sendmap *rsm; 957 uint32_t hpts_timeout; 958 int tmr_up; 959 960 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 961 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 962 return; 963 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 964 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 965 (tmr_up == PACE_TMR_RXT)) { 966 /* Should be an RXT */ 967 return; 968 } 969 inp = bbr->rc_inp; 970 if (rsm == NULL) { 971 /* Nothing outstanding? */ 972 if (tp->t_flags & TF_DELACK) { 973 if (tmr_up == PACE_TMR_DELACK) 974 /* 975 * We are supposed to have delayed ack up 976 * and we do 977 */ 978 return; 979 } else if (((V_tcp_always_keepalive || 980 inp->inp_socket->so_options & SO_KEEPALIVE) && 981 (tp->t_state <= TCPS_CLOSING)) && 982 (tmr_up == PACE_TMR_KEEP) && 983 (tp->snd_max == tp->snd_una)) { 984 /* We should have keep alive up and we do */ 985 return; 986 } 987 } 988 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) { 989 if ((tp->t_flags & TF_SENTFIN) && 990 ((tp->snd_max - tp->snd_una) == 1) && 991 (rsm->r_flags & BBR_HAS_FIN)) { 992 /* needs to be a RXT */ 993 if (tmr_up == PACE_TMR_RXT) 994 return; 995 else 996 goto wrong_timer; 997 } else if (tmr_up == PACE_TMR_RACK) 998 return; 999 else 1000 goto wrong_timer; 1001 } else if (rsm && (tmr_up == PACE_TMR_RACK)) { 1002 /* Rack timer has priority if we have data out */ 1003 return; 1004 } else if (SEQ_GT(tp->snd_max, tp->snd_una) && 1005 ((tmr_up == PACE_TMR_TLP) || 1006 (tmr_up == PACE_TMR_RXT))) { 1007 /* 1008 * Either a TLP or RXT is fine if no sack-passed is in place 1009 * and data is outstanding. 1010 */ 1011 return; 1012 } else if (tmr_up == PACE_TMR_DELACK) { 1013 /* 1014 * If the delayed ack was going to go off before the 1015 * rtx/tlp/rack timer were going to expire, then that would 1016 * be the timer in control. Note we don't check the time 1017 * here trusting the code is correct. 1018 */ 1019 return; 1020 } 1021 if (SEQ_GT(tp->snd_max, tp->snd_una) && 1022 ((tmr_up == PACE_TMR_RXT) || 1023 (tmr_up == PACE_TMR_TLP) || 1024 (tmr_up == PACE_TMR_RACK))) { 1025 /* 1026 * We have outstanding data and 1027 * we *do* have a RACK, TLP or RXT 1028 * timer running. We won't restart 1029 * anything here since thats probably ok we 1030 * will get called with some timer here shortly. 1031 */ 1032 return; 1033 } 1034 /* 1035 * Ok the timer originally started is not what we want now. We will 1036 * force the hpts to be stopped if any, and restart with the slot 1037 * set to what was in the saved slot. 1038 */ 1039 wrong_timer: 1040 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) { 1041 if (tcp_in_hpts(tp)) 1042 tcp_hpts_remove(tp); 1043 bbr_timer_cancel(bbr, __LINE__, cts); 1044 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val, 1045 0); 1046 } else { 1047 /* 1048 * Output is hptsi so we just need to switch the type of 1049 * timer. We don't bother with keep-alive, since when we 1050 * jump through the output, it will start the keep-alive if 1051 * nothing is sent. 1052 * 1053 * We only need a delayed-ack added and or the hpts_timeout. 1054 */ 1055 hpts_timeout = bbr_timer_start(tp, bbr, cts); 1056 if (tp->t_flags & TF_DELACK) { 1057 if (hpts_timeout == 0) { 1058 hpts_timeout = bbr_delack_time; 1059 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1060 } 1061 else if (hpts_timeout > bbr_delack_time) { 1062 hpts_timeout = bbr_delack_time; 1063 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1064 } 1065 } 1066 if (hpts_timeout) { 1067 if (hpts_timeout > 0x7ffffffe) 1068 hpts_timeout = 0x7ffffffe; 1069 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 1070 } 1071 } 1072 } 1073 1074 int32_t bbr_clear_lost = 0; 1075 1076 /* 1077 * Considers the two time values now (cts) and earlier. 1078 * If cts is smaller than earlier, we could have 1079 * had a sequence wrap (our counter wraps every 1080 * 70 min or so) or it could be just clock skew 1081 * getting us two different time values. Clock skew 1082 * will show up within 10ms or so. So in such 1083 * a case (where cts is behind earlier time by 1084 * less than 10ms) we return 0. Otherwise we 1085 * return the true difference between them. 1086 */ 1087 static inline uint32_t 1088 bbr_calc_time(uint32_t cts, uint32_t earlier_time) { 1089 /* 1090 * Given two timestamps, the current time stamp cts, and some other 1091 * time-stamp taken in theory earlier return the difference. The 1092 * trick is here sometimes locking will get the other timestamp 1093 * after the cts. If this occurs we need to return 0. 1094 */ 1095 if (TSTMP_GEQ(cts, earlier_time)) 1096 return (cts - earlier_time); 1097 /* 1098 * cts is behind earlier_time if its less than 10ms consider it 0. 1099 * If its more than 10ms difference then we had a time wrap. Else 1100 * its just the normal locking foo. I wonder if we should not go to 1101 * 64bit TS and get rid of this issue. 1102 */ 1103 if (TSTMP_GEQ((cts + 10000), earlier_time)) 1104 return (0); 1105 /* 1106 * Ok the time must have wrapped. So we need to answer a large 1107 * amount of time, which the normal subtraction should do. 1108 */ 1109 return (cts - earlier_time); 1110 } 1111 1112 static int 1113 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS) 1114 { 1115 uint32_t stat; 1116 int32_t error; 1117 1118 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t)); 1119 if (error || req->newptr == NULL) 1120 return error; 1121 1122 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 1123 if (error) 1124 return (error); 1125 if (stat == 1) { 1126 #ifdef BBR_INVARIANTS 1127 printf("Clearing BBR lost counters\n"); 1128 #endif 1129 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT); 1130 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT); 1131 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT); 1132 } else if (stat == 2) { 1133 #ifdef BBR_INVARIANTS 1134 printf("Clearing BBR option counters\n"); 1135 #endif 1136 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE); 1137 } else if (stat == 3) { 1138 #ifdef BBR_INVARIANTS 1139 printf("Clearing BBR stats counters\n"); 1140 #endif 1141 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE); 1142 } else if (stat == 4) { 1143 #ifdef BBR_INVARIANTS 1144 printf("Clearing BBR out-size counters\n"); 1145 #endif 1146 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE); 1147 } 1148 bbr_clear_lost = 0; 1149 return (0); 1150 } 1151 1152 static void 1153 bbr_init_sysctls(void) 1154 { 1155 struct sysctl_oid *bbr_probertt; 1156 struct sysctl_oid *bbr_hptsi; 1157 struct sysctl_oid *bbr_measure; 1158 struct sysctl_oid *bbr_cwnd; 1159 struct sysctl_oid *bbr_timeout; 1160 struct sysctl_oid *bbr_states; 1161 struct sysctl_oid *bbr_startup; 1162 struct sysctl_oid *bbr_policer; 1163 1164 /* Probe rtt controls */ 1165 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1166 SYSCTL_CHILDREN(bbr_sysctl_root), 1167 OID_AUTO, 1168 "probertt", 1169 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1170 ""); 1171 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1172 SYSCTL_CHILDREN(bbr_probertt), 1173 OID_AUTO, "gain", CTLFLAG_RW, 1174 &bbr_rttprobe_gain, 192, 1175 "What is the filter gain drop in probe_rtt (0=disable)?"); 1176 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1177 SYSCTL_CHILDREN(bbr_probertt), 1178 OID_AUTO, "cwnd", CTLFLAG_RW, 1179 &bbr_rtt_probe_cwndtarg, 4, 1180 "How many mss's are outstanding during probe-rtt"); 1181 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1182 SYSCTL_CHILDREN(bbr_probertt), 1183 OID_AUTO, "int", CTLFLAG_RW, 1184 &bbr_rtt_probe_limit, 4000000, 1185 "If RTT has not shrank in this many micro-seconds enter probe-rtt"); 1186 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1187 SYSCTL_CHILDREN(bbr_probertt), 1188 OID_AUTO, "mintime", CTLFLAG_RW, 1189 &bbr_rtt_probe_time, 200000, 1190 "How many microseconds in probe-rtt"); 1191 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1192 SYSCTL_CHILDREN(bbr_probertt), 1193 OID_AUTO, "filter_len_sec", CTLFLAG_RW, 1194 &bbr_filter_len_sec, 6, 1195 "How long in seconds does the rttProp filter run?"); 1196 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1197 SYSCTL_CHILDREN(bbr_probertt), 1198 OID_AUTO, "drain_rtt", CTLFLAG_RW, 1199 &bbr_drain_rtt, BBR_SRTT, 1200 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?"); 1201 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1202 SYSCTL_CHILDREN(bbr_probertt), 1203 OID_AUTO, "can_force", CTLFLAG_RW, 1204 &bbr_can_force_probertt, 0, 1205 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??"); 1206 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1207 SYSCTL_CHILDREN(bbr_probertt), 1208 OID_AUTO, "enter_sets_force", CTLFLAG_RW, 1209 &bbr_probertt_sets_rtt, 0, 1210 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?"); 1211 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1212 SYSCTL_CHILDREN(bbr_probertt), 1213 OID_AUTO, "can_adjust", CTLFLAG_RW, 1214 &bbr_can_adjust_probertt, 1, 1215 "Can we dynamically adjust the probe-rtt limits and times?"); 1216 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1217 SYSCTL_CHILDREN(bbr_probertt), 1218 OID_AUTO, "is_ratio", CTLFLAG_RW, 1219 &bbr_is_ratio, 0, 1220 "is the limit to filter a ratio?"); 1221 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1222 SYSCTL_CHILDREN(bbr_probertt), 1223 OID_AUTO, "use_cwnd", CTLFLAG_RW, 1224 &bbr_prtt_slam_cwnd, 0, 1225 "Should we set/recover cwnd?"); 1226 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1227 SYSCTL_CHILDREN(bbr_probertt), 1228 OID_AUTO, "can_use_ts", CTLFLAG_RW, 1229 &bbr_can_use_ts_for_rtt, 1, 1230 "Can we use the ms timestamp if available for retransmistted rtt calculations?"); 1231 1232 /* Pacing controls */ 1233 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1234 SYSCTL_CHILDREN(bbr_sysctl_root), 1235 OID_AUTO, 1236 "pacing", 1237 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1238 ""); 1239 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1240 SYSCTL_CHILDREN(bbr_hptsi), 1241 OID_AUTO, "hw_pacing", CTLFLAG_RW, 1242 &bbr_allow_hdwr_pacing, 1, 1243 "Do we allow hardware pacing?"); 1244 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1245 SYSCTL_CHILDREN(bbr_hptsi), 1246 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW, 1247 &bbr_hardware_pacing_limit, 4000, 1248 "Do we have a limited number of connections for pacing chelsio (0=no limit)?"); 1249 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1250 SYSCTL_CHILDREN(bbr_hptsi), 1251 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW, 1252 &bbr_hdwr_pace_adjust, 2, 1253 "Multiplier to calculated tso size?"); 1254 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1255 SYSCTL_CHILDREN(bbr_hptsi), 1256 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW, 1257 &bbr_hdwr_pace_floor, 1, 1258 "Do we invoke the hardware pacing floor?"); 1259 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1260 SYSCTL_CHILDREN(bbr_hptsi), 1261 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW, 1262 &bbr_hdwr_pacing_delay_cnt, 10, 1263 "How many packets must be sent after hdwr pacing is enabled"); 1264 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1265 SYSCTL_CHILDREN(bbr_hptsi), 1266 OID_AUTO, "bw_cross", CTLFLAG_RW, 1267 &bbr_cross_over, 3000000, 1268 "What is the point where we cross over to linux like TSO size set"); 1269 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1270 SYSCTL_CHILDREN(bbr_hptsi), 1271 OID_AUTO, "seg_deltarg", CTLFLAG_RW, 1272 &bbr_hptsi_segments_delay_tar, 7000, 1273 "What is the worse case delay target for hptsi < 48Mbp connections"); 1274 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1275 SYSCTL_CHILDREN(bbr_hptsi), 1276 OID_AUTO, "enet_oh", CTLFLAG_RW, 1277 &bbr_include_enet_oh, 0, 1278 "Do we include the ethernet overhead in calculating pacing delay?"); 1279 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1280 SYSCTL_CHILDREN(bbr_hptsi), 1281 OID_AUTO, "ip_oh", CTLFLAG_RW, 1282 &bbr_include_ip_oh, 1, 1283 "Do we include the IP overhead in calculating pacing delay?"); 1284 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1285 SYSCTL_CHILDREN(bbr_hptsi), 1286 OID_AUTO, "tcp_oh", CTLFLAG_RW, 1287 &bbr_include_tcp_oh, 0, 1288 "Do we include the TCP overhead in calculating pacing delay?"); 1289 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1290 SYSCTL_CHILDREN(bbr_hptsi), 1291 OID_AUTO, "google_discount", CTLFLAG_RW, 1292 &bbr_google_discount, 10, 1293 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?"); 1294 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1295 SYSCTL_CHILDREN(bbr_hptsi), 1296 OID_AUTO, "all_get_min", CTLFLAG_RW, 1297 &bbr_all_get_min, 0, 1298 "If you are less than a MSS do you just get the min?"); 1299 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1300 SYSCTL_CHILDREN(bbr_hptsi), 1301 OID_AUTO, "tso_min", CTLFLAG_RW, 1302 &bbr_hptsi_bytes_min, 1460, 1303 "For 0 -> 24Mbps what is floor number of segments for TSO"); 1304 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1305 SYSCTL_CHILDREN(bbr_hptsi), 1306 OID_AUTO, "seg_tso_max", CTLFLAG_RW, 1307 &bbr_hptsi_segments_max, 6, 1308 "For 0 -> 24Mbps what is top number of segments for TSO"); 1309 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1310 SYSCTL_CHILDREN(bbr_hptsi), 1311 OID_AUTO, "seg_floor", CTLFLAG_RW, 1312 &bbr_hptsi_segments_floor, 1, 1313 "Minimum TSO size we will fall too in segments"); 1314 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1315 SYSCTL_CHILDREN(bbr_hptsi), 1316 OID_AUTO, "utter_max", CTLFLAG_RW, 1317 &bbr_hptsi_utter_max, 0, 1318 "The absolute maximum that any pacing (outside of hardware) can be"); 1319 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1320 SYSCTL_CHILDREN(bbr_hptsi), 1321 OID_AUTO, "seg_divisor", CTLFLAG_RW, 1322 &bbr_hptsi_per_second, 100, 1323 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps "); 1324 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1325 SYSCTL_CHILDREN(bbr_hptsi), 1326 OID_AUTO, "srtt_mul", CTLFLAG_RW, 1327 &bbr_hptsi_max_mul, 1, 1328 "The multiplier for pace len max"); 1329 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1330 SYSCTL_CHILDREN(bbr_hptsi), 1331 OID_AUTO, "srtt_div", CTLFLAG_RW, 1332 &bbr_hptsi_max_div, 2, 1333 "The divisor for pace len max"); 1334 /* Measurement controls */ 1335 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1336 SYSCTL_CHILDREN(bbr_sysctl_root), 1337 OID_AUTO, 1338 "measure", 1339 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1340 "Measurement controls"); 1341 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1342 SYSCTL_CHILDREN(bbr_measure), 1343 OID_AUTO, "min_i_bw", CTLFLAG_RW, 1344 &bbr_initial_bw_bps, 62500, 1345 "Minimum initial b/w in bytes per second"); 1346 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1347 SYSCTL_CHILDREN(bbr_measure), 1348 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1349 &bbr_sack_not_required, 0, 1350 "Do we allow bbr to run on connections not supporting SACK?"); 1351 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1352 SYSCTL_CHILDREN(bbr_measure), 1353 OID_AUTO, "use_google", CTLFLAG_RW, 1354 &bbr_use_google_algo, 0, 1355 "Use has close to google V1.0 has possible?"); 1356 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1357 SYSCTL_CHILDREN(bbr_measure), 1358 OID_AUTO, "ts_limiting", CTLFLAG_RW, 1359 &bbr_ts_limiting, 1, 1360 "Do we attempt to use the peers timestamp to limit b/w caculations?"); 1361 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1362 SYSCTL_CHILDREN(bbr_measure), 1363 OID_AUTO, "ts_can_raise", CTLFLAG_RW, 1364 &bbr_ts_can_raise, 0, 1365 "Can we raise the b/w via timestamp b/w calculation?"); 1366 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1367 SYSCTL_CHILDREN(bbr_measure), 1368 OID_AUTO, "ts_delta", CTLFLAG_RW, 1369 &bbr_min_usec_delta, 20000, 1370 "How long in usec between ts of our sends in ts validation code?"); 1371 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1372 SYSCTL_CHILDREN(bbr_measure), 1373 OID_AUTO, "ts_peer_delta", CTLFLAG_RW, 1374 &bbr_min_peer_delta, 20, 1375 "What min numerical value should be between the peer deltas?"); 1376 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1377 SYSCTL_CHILDREN(bbr_measure), 1378 OID_AUTO, "ts_delta_percent", CTLFLAG_RW, 1379 &bbr_delta_percent, 150, 1380 "What percentage (150 = 15.0) do we allow variance for?"); 1381 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1382 SYSCTL_CHILDREN(bbr_measure), 1383 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW, 1384 &bbr_min_measurements_req, 1, 1385 "What is the minimum measurement count we need before we switch to our b/w estimate"); 1386 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1387 SYSCTL_CHILDREN(bbr_measure), 1388 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW, 1389 &bbr_no_pacing_until, 4, 1390 "How many pkt-epoch's (0 is off) do we need before pacing is on?"); 1391 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1392 SYSCTL_CHILDREN(bbr_measure), 1393 OID_AUTO, "quanta", CTLFLAG_RW, 1394 &bbr_quanta, 2, 1395 "Extra quanta to add when calculating the target (ID section 4.2.3.2)."); 1396 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1397 SYSCTL_CHILDREN(bbr_measure), 1398 OID_AUTO, "noretran", CTLFLAG_RW, 1399 &bbr_no_retran, 0, 1400 "Should google mode not use retransmission measurements for the b/w estimation?"); 1401 /* State controls */ 1402 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1403 SYSCTL_CHILDREN(bbr_sysctl_root), 1404 OID_AUTO, 1405 "states", 1406 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1407 "State controls"); 1408 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1409 SYSCTL_CHILDREN(bbr_states), 1410 OID_AUTO, "idle_restart", CTLFLAG_RW, 1411 &bbr_uses_idle_restart, 0, 1412 "Do we use a new special idle_restart state to ramp back up quickly?"); 1413 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1414 SYSCTL_CHILDREN(bbr_states), 1415 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW, 1416 &bbr_idle_restart_threshold, 100000, 1417 "How long must we be idle before we restart??"); 1418 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1419 SYSCTL_CHILDREN(bbr_states), 1420 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW, 1421 &bbr_state_is_pkt_epoch, 0, 1422 "Do we use a pkt-epoch for substate if 0 rttProp?"); 1423 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1424 SYSCTL_CHILDREN(bbr_states), 1425 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW, 1426 &bbr_rtt_gain_thresh, 0, 1427 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?"); 1428 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1429 SYSCTL_CHILDREN(bbr_states), 1430 OID_AUTO, "drain_floor", CTLFLAG_RW, 1431 &bbr_drain_floor, 88, 1432 "What is the lowest we can drain (pg) too?"); 1433 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1434 SYSCTL_CHILDREN(bbr_states), 1435 OID_AUTO, "drain_2_target", CTLFLAG_RW, 1436 &bbr_state_drain_2_tar, 1, 1437 "Do we drain to target in drain substate?"); 1438 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1439 SYSCTL_CHILDREN(bbr_states), 1440 OID_AUTO, "gain_2_target", CTLFLAG_RW, 1441 &bbr_gain_to_target, 1, 1442 "Does probe bw gain to target??"); 1443 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1444 SYSCTL_CHILDREN(bbr_states), 1445 OID_AUTO, "gain_extra_time", CTLFLAG_RW, 1446 &bbr_gain_gets_extra_too, 1, 1447 "Does probe bw gain get the extra time too?"); 1448 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1449 SYSCTL_CHILDREN(bbr_states), 1450 OID_AUTO, "ld_div", CTLFLAG_RW, 1451 &bbr_drain_drop_div, 5, 1452 "Long drain drop divider?"); 1453 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1454 SYSCTL_CHILDREN(bbr_states), 1455 OID_AUTO, "ld_mul", CTLFLAG_RW, 1456 &bbr_drain_drop_mul, 4, 1457 "Long drain drop multiplier?"); 1458 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1459 SYSCTL_CHILDREN(bbr_states), 1460 OID_AUTO, "rand_ot_disc", CTLFLAG_RW, 1461 &bbr_rand_ot, 50, 1462 "Random discount of the ot?"); 1463 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1464 SYSCTL_CHILDREN(bbr_states), 1465 OID_AUTO, "dr_filter_life", CTLFLAG_RW, 1466 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT, 1467 "How many packet-epochs does the b/w delivery rate last?"); 1468 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1469 SYSCTL_CHILDREN(bbr_states), 1470 OID_AUTO, "subdrain_applimited", CTLFLAG_RW, 1471 &bbr_sub_drain_app_limit, 0, 1472 "Does our sub-state drain invoke app limited if its long?"); 1473 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1474 SYSCTL_CHILDREN(bbr_states), 1475 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW, 1476 &bbr_sub_drain_slam_cwnd, 0, 1477 "Should we set/recover cwnd for sub-state drain?"); 1478 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1479 SYSCTL_CHILDREN(bbr_states), 1480 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW, 1481 &bbr_slam_cwnd_in_main_drain, 0, 1482 "Should we set/recover cwnd for main-state drain?"); 1483 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1484 SYSCTL_CHILDREN(bbr_states), 1485 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW, 1486 &google_allow_early_out, 1, 1487 "Should we allow google probe-bw/drain to exit early at flight target?"); 1488 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1489 SYSCTL_CHILDREN(bbr_states), 1490 OID_AUTO, "google_exit_loss", CTLFLAG_RW, 1491 &google_consider_lost, 1, 1492 "Should we have losses exit gain of probebw in google mode??"); 1493 /* Startup controls */ 1494 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1495 SYSCTL_CHILDREN(bbr_sysctl_root), 1496 OID_AUTO, 1497 "startup", 1498 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1499 "Startup controls"); 1500 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1501 SYSCTL_CHILDREN(bbr_startup), 1502 OID_AUTO, "cheat_iwnd", CTLFLAG_RW, 1503 &bbr_sends_full_iwnd, 1, 1504 "Do we not pace but burst out initial windows has our TSO size?"); 1505 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1506 SYSCTL_CHILDREN(bbr_startup), 1507 OID_AUTO, "loss_threshold", CTLFLAG_RW, 1508 &bbr_startup_loss_thresh, 2000, 1509 "In startup what is the loss threshold in a pe that will exit us from startup?"); 1510 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1511 SYSCTL_CHILDREN(bbr_startup), 1512 OID_AUTO, "use_lowerpg", CTLFLAG_RW, 1513 &bbr_use_lower_gain_in_startup, 1, 1514 "Should we use a lower hptsi gain if we see loss in startup?"); 1515 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1516 SYSCTL_CHILDREN(bbr_startup), 1517 OID_AUTO, "gain", CTLFLAG_RW, 1518 &bbr_start_exit, 25, 1519 "What gain percent do we need to see to stay in startup??"); 1520 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1521 SYSCTL_CHILDREN(bbr_startup), 1522 OID_AUTO, "low_gain", CTLFLAG_RW, 1523 &bbr_low_start_exit, 15, 1524 "What gain percent do we need to see to stay in the lower gain startup??"); 1525 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1526 SYSCTL_CHILDREN(bbr_startup), 1527 OID_AUTO, "loss_exit", CTLFLAG_RW, 1528 &bbr_exit_startup_at_loss, 1, 1529 "Should we exit startup at loss in an epoch if we are not gaining?"); 1530 /* CWND controls */ 1531 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1532 SYSCTL_CHILDREN(bbr_sysctl_root), 1533 OID_AUTO, 1534 "cwnd", 1535 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1536 "Cwnd controls"); 1537 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1538 SYSCTL_CHILDREN(bbr_cwnd), 1539 OID_AUTO, "tar_rtt", CTLFLAG_RW, 1540 &bbr_cwndtarget_rtt_touse, 0, 1541 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?"); 1542 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1543 SYSCTL_CHILDREN(bbr_cwnd), 1544 OID_AUTO, "may_shrink", CTLFLAG_RW, 1545 &bbr_cwnd_may_shrink, 0, 1546 "Can the cwnd shrink if it would grow to more than the target?"); 1547 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1548 SYSCTL_CHILDREN(bbr_cwnd), 1549 OID_AUTO, "max_target_limit", CTLFLAG_RW, 1550 &bbr_target_cwnd_mult_limit, 8, 1551 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?"); 1552 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1553 SYSCTL_CHILDREN(bbr_cwnd), 1554 OID_AUTO, "highspeed_min", CTLFLAG_RW, 1555 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS, 1556 "What is the high-speed min cwnd (rttProp under 1ms)"); 1557 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1558 SYSCTL_CHILDREN(bbr_cwnd), 1559 OID_AUTO, "lowspeed_min", CTLFLAG_RW, 1560 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS, 1561 "What is the min cwnd (rttProp > 1ms)"); 1562 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1563 SYSCTL_CHILDREN(bbr_cwnd), 1564 OID_AUTO, "initwin", CTLFLAG_RW, 1565 &bbr_def_init_win, 10, 1566 "What is the BBR initial window, if 0 use tcp version"); 1567 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1568 SYSCTL_CHILDREN(bbr_cwnd), 1569 OID_AUTO, "do_loss_red", CTLFLAG_RW, 1570 &bbr_do_red, 600, 1571 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?"); 1572 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1573 SYSCTL_CHILDREN(bbr_cwnd), 1574 OID_AUTO, "red_scale", CTLFLAG_RW, 1575 &bbr_red_scale, 20000, 1576 "What RTT do we scale with?"); 1577 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1578 SYSCTL_CHILDREN(bbr_cwnd), 1579 OID_AUTO, "red_growslow", CTLFLAG_RW, 1580 &bbr_red_growth_restrict, 1, 1581 "Do we restrict cwnd growth for whats in flight?"); 1582 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1583 SYSCTL_CHILDREN(bbr_cwnd), 1584 OID_AUTO, "red_div", CTLFLAG_RW, 1585 &bbr_red_div, 2, 1586 "If we reduce whats the divisor?"); 1587 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1588 SYSCTL_CHILDREN(bbr_cwnd), 1589 OID_AUTO, "red_mul", CTLFLAG_RW, 1590 &bbr_red_mul, 1, 1591 "If we reduce whats the mulitiplier?"); 1592 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1593 SYSCTL_CHILDREN(bbr_cwnd), 1594 OID_AUTO, "target_is_unit", CTLFLAG_RW, 1595 &bbr_target_is_bbunit, 0, 1596 "Is the state target the pacing_gain or BBR_UNIT?"); 1597 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1598 SYSCTL_CHILDREN(bbr_cwnd), 1599 OID_AUTO, "drop_limit", CTLFLAG_RW, 1600 &bbr_drop_limit, 0, 1601 "Number of segments limit for drop (0=use min_cwnd w/flight)?"); 1602 1603 /* Timeout controls */ 1604 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1605 SYSCTL_CHILDREN(bbr_sysctl_root), 1606 OID_AUTO, 1607 "timeout", 1608 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1609 "Time out controls"); 1610 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1611 SYSCTL_CHILDREN(bbr_timeout), 1612 OID_AUTO, "delack", CTLFLAG_RW, 1613 &bbr_delack_time, 100000, 1614 "BBR's delayed ack time"); 1615 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1616 SYSCTL_CHILDREN(bbr_timeout), 1617 OID_AUTO, "tlp_uses", CTLFLAG_RW, 1618 &bbr_tlp_type_to_use, 3, 1619 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt"); 1620 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1621 SYSCTL_CHILDREN(bbr_timeout), 1622 OID_AUTO, "persmin", CTLFLAG_RW, 1623 &bbr_persist_min, 250000, 1624 "What is the minimum time in microseconds between persists"); 1625 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1626 SYSCTL_CHILDREN(bbr_timeout), 1627 OID_AUTO, "persmax", CTLFLAG_RW, 1628 &bbr_persist_max, 1000000, 1629 "What is the largest delay in microseconds between persists"); 1630 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1631 SYSCTL_CHILDREN(bbr_timeout), 1632 OID_AUTO, "tlp_minto", CTLFLAG_RW, 1633 &bbr_tlp_min, 10000, 1634 "TLP Min timeout in usecs"); 1635 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1636 SYSCTL_CHILDREN(bbr_timeout), 1637 OID_AUTO, "tlp_dack_time", CTLFLAG_RW, 1638 &bbr_delayed_ack_time, 200000, 1639 "TLP delayed ack compensation value"); 1640 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1641 SYSCTL_CHILDREN(bbr_sysctl_root), 1642 OID_AUTO, "minrto", CTLFLAG_RW, 1643 &bbr_rto_min_ms, 30, 1644 "Minimum RTO in ms"); 1645 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1646 SYSCTL_CHILDREN(bbr_timeout), 1647 OID_AUTO, "maxrto", CTLFLAG_RW, 1648 &bbr_rto_max_sec, 4, 1649 "Maximum RTO in seconds -- should be at least as large as min_rto"); 1650 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1651 SYSCTL_CHILDREN(bbr_timeout), 1652 OID_AUTO, "tlp_retry", CTLFLAG_RW, 1653 &bbr_tlp_max_resend, 2, 1654 "How many times does TLP retry a single segment or multiple with no ACK"); 1655 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1656 SYSCTL_CHILDREN(bbr_timeout), 1657 OID_AUTO, "minto", CTLFLAG_RW, 1658 &bbr_min_to, 1000, 1659 "Minimum rack timeout in useconds"); 1660 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1661 SYSCTL_CHILDREN(bbr_timeout), 1662 OID_AUTO, "pktdelay", CTLFLAG_RW, 1663 &bbr_pkt_delay, 1000, 1664 "Extra RACK time (in useconds) besides reordering thresh"); 1665 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1666 SYSCTL_CHILDREN(bbr_timeout), 1667 OID_AUTO, "incr_tmrs", CTLFLAG_RW, 1668 &bbr_incr_timers, 1, 1669 "Increase the RXT/TLP timer by the pacing time used?"); 1670 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1671 SYSCTL_CHILDREN(bbr_timeout), 1672 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW, 1673 &bbr_marks_rxt_sack_passed, 0, 1674 "Mark sack passed on all those not ack'd when a RXT hits?"); 1675 /* Policer controls */ 1676 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1677 SYSCTL_CHILDREN(bbr_sysctl_root), 1678 OID_AUTO, 1679 "policer", 1680 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1681 "Policer controls"); 1682 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1683 SYSCTL_CHILDREN(bbr_policer), 1684 OID_AUTO, "detect_enable", CTLFLAG_RW, 1685 &bbr_policer_detection_enabled, 1, 1686 "Is policer detection enabled??"); 1687 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1688 SYSCTL_CHILDREN(bbr_policer), 1689 OID_AUTO, "min_pes", CTLFLAG_RW, 1690 &bbr_lt_intvl_min_rtts, 4, 1691 "Minimum number of PE's?"); 1692 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1693 SYSCTL_CHILDREN(bbr_policer), 1694 OID_AUTO, "bwdiff", CTLFLAG_RW, 1695 &bbr_lt_bw_diff, (4000/8), 1696 "Minimal bw diff?"); 1697 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1698 SYSCTL_CHILDREN(bbr_policer), 1699 OID_AUTO, "bwratio", CTLFLAG_RW, 1700 &bbr_lt_bw_ratio, 8, 1701 "Minimal bw diff?"); 1702 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1703 SYSCTL_CHILDREN(bbr_policer), 1704 OID_AUTO, "from_rack_rxt", CTLFLAG_RW, 1705 &bbr_policer_call_from_rack_to, 0, 1706 "Do we call the policer detection code from a rack-timeout?"); 1707 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1708 SYSCTL_CHILDREN(bbr_policer), 1709 OID_AUTO, "false_postive", CTLFLAG_RW, 1710 &bbr_lt_intvl_fp, 0, 1711 "What packet epoch do we do false-positive detection at (0=no)?"); 1712 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1713 SYSCTL_CHILDREN(bbr_policer), 1714 OID_AUTO, "loss_thresh", CTLFLAG_RW, 1715 &bbr_lt_loss_thresh, 196, 1716 "Loss threshold 196 = 19.6%?"); 1717 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1718 SYSCTL_CHILDREN(bbr_policer), 1719 OID_AUTO, "false_postive_thresh", CTLFLAG_RW, 1720 &bbr_lt_fd_thresh, 100, 1721 "What percentage is the false detection threshold (150=15.0)?"); 1722 /* All the rest */ 1723 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1724 SYSCTL_CHILDREN(bbr_sysctl_root), 1725 OID_AUTO, "cheat_rxt", CTLFLAG_RW, 1726 &bbr_use_rack_resend_cheat, 0, 1727 "Do we burst 1ms between sends on retransmissions (like rack)?"); 1728 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1729 SYSCTL_CHILDREN(bbr_sysctl_root), 1730 OID_AUTO, "error_paceout", CTLFLAG_RW, 1731 &bbr_error_base_paceout, 10000, 1732 "When we hit an error what is the min to pace out in usec's?"); 1733 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1734 SYSCTL_CHILDREN(bbr_sysctl_root), 1735 OID_AUTO, "kill_paceout", CTLFLAG_RW, 1736 &bbr_max_net_error_cnt, 10, 1737 "When we hit this many errors in a row, kill the session?"); 1738 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1739 SYSCTL_CHILDREN(bbr_sysctl_root), 1740 OID_AUTO, "data_after_close", CTLFLAG_RW, 1741 &bbr_ignore_data_after_close, 1, 1742 "Do we hold off sending a RST until all pending data is ack'd"); 1743 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1744 SYSCTL_CHILDREN(bbr_sysctl_root), 1745 OID_AUTO, "resend_use_tso", CTLFLAG_RW, 1746 &bbr_resends_use_tso, 0, 1747 "Can resends use TSO?"); 1748 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1749 SYSCTL_CHILDREN(bbr_sysctl_root), 1750 OID_AUTO, "sblklimit", CTLFLAG_RW, 1751 &bbr_sack_block_limit, 128, 1752 "When do we start ignoring small sack blocks"); 1753 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1754 SYSCTL_CHILDREN(bbr_sysctl_root), 1755 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1756 &bbr_verbose_logging, 0, 1757 "Should BBR black box logging be verbose"); 1758 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1759 SYSCTL_CHILDREN(bbr_sysctl_root), 1760 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1761 &bbr_reorder_thresh, 2, 1762 "What factor for rack will be added when seeing reordering (shift right)"); 1763 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1764 SYSCTL_CHILDREN(bbr_sysctl_root), 1765 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1766 &bbr_reorder_fade, 0, 1767 "Does reorder detection fade, if so how many ms (0 means never)"); 1768 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1769 SYSCTL_CHILDREN(bbr_sysctl_root), 1770 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1771 &bbr_tlp_thresh, 1, 1772 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1773 /* Stats and counters */ 1774 /* The pacing counters for hdwr/software can't be in the array */ 1775 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1776 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1777 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1778 SYSCTL_CHILDREN(bbr_sysctl_root), 1779 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD, 1780 &bbr_hdwr_pacing_enobuf, 1781 "Total number of enobufs for hardware paced flows"); 1782 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1783 SYSCTL_CHILDREN(bbr_sysctl_root), 1784 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD, 1785 &bbr_nohdwr_pacing_enobuf, 1786 "Total number of enobufs for non-hardware paced flows"); 1787 1788 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK); 1789 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1790 SYSCTL_CHILDREN(bbr_sysctl_root), 1791 OID_AUTO, "hdwr_pacing", CTLFLAG_RD, 1792 &bbr_flows_whdwr_pacing, 1793 "Total number of hardware paced flows"); 1794 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK); 1795 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1796 SYSCTL_CHILDREN(bbr_sysctl_root), 1797 OID_AUTO, "software_pacing", CTLFLAG_RD, 1798 &bbr_flows_nohdwr_pacing, 1799 "Total number of software paced flows"); 1800 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK); 1801 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1802 OID_AUTO, "stats", CTLFLAG_RD, 1803 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats"); 1804 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK); 1805 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1806 OID_AUTO, "opts", CTLFLAG_RD, 1807 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats"); 1808 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK); 1809 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1810 OID_AUTO, "lost", CTLFLAG_RD, 1811 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur"); 1812 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK); 1813 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1814 OID_AUTO, "stateresend", CTLFLAG_RD, 1815 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend"); 1816 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK); 1817 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1818 OID_AUTO, "statetime", CTLFLAG_RD, 1819 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states"); 1820 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1821 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1822 OID_AUTO, "outsize", CTLFLAG_RD, 1823 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls"); 1824 SYSCTL_ADD_PROC(&bbr_sysctl_ctx, 1825 SYSCTL_CHILDREN(bbr_sysctl_root), 1826 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1827 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters"); 1828 } 1829 1830 static void 1831 bbr_counter_destroy(void) 1832 { 1833 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE); 1834 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE); 1835 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE); 1836 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT); 1837 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT); 1838 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT); 1839 counter_u64_free(bbr_nohdwr_pacing_enobuf); 1840 counter_u64_free(bbr_hdwr_pacing_enobuf); 1841 counter_u64_free(bbr_flows_whdwr_pacing); 1842 counter_u64_free(bbr_flows_nohdwr_pacing); 1843 1844 } 1845 1846 static __inline void 1847 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts) 1848 { 1849 memset(l, 0, sizeof(union tcp_log_stackspecific)); 1850 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate; 1851 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate); 1852 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 1853 l->bw_inuse = bbr_get_bw(bbr); 1854 l->inflight = ctf_flight_size(bbr->rc_tp, 1855 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 1856 l->applimited = bbr->r_ctl.r_app_limited_until; 1857 l->delivered = bbr->r_ctl.rc_delivered; 1858 l->timeStamp = cts; 1859 l->lost = bbr->r_ctl.rc_lost; 1860 l->bbr_state = bbr->rc_bbr_state; 1861 l->bbr_substate = bbr_state_val(bbr); 1862 l->epoch = bbr->r_ctl.rc_rtt_epoch; 1863 l->lt_epoch = bbr->r_ctl.rc_lt_epoch; 1864 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain; 1865 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain; 1866 l->inhpts = tcp_in_hpts(bbr->rc_tp); 1867 l->use_lt_bw = bbr->rc_lt_use_bw; 1868 l->pkts_out = bbr->r_ctl.rc_flight_at_input; 1869 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch; 1870 } 1871 1872 static void 1873 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason) 1874 { 1875 if (tcp_bblogging_on(bbr->rc_tp)) { 1876 union tcp_log_stackspecific log; 1877 1878 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1879 log.u_bbr.flex1 = 0; 1880 log.u_bbr.flex2 = 0; 1881 log.u_bbr.flex5 = 0; 1882 log.u_bbr.flex3 = 0; 1883 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate; 1884 log.u_bbr.flex7 = reason; 1885 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt; 1886 log.u_bbr.flex8 = 0; 1887 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1888 &bbr->rc_inp->inp_socket->so_rcv, 1889 &bbr->rc_inp->inp_socket->so_snd, 1890 BBR_LOG_BW_RED_EV, 0, 1891 0, &log, false, &bbr->rc_tv); 1892 } 1893 } 1894 1895 static void 1896 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count) 1897 { 1898 if (tcp_bblogging_on(bbr->rc_tp)) { 1899 union tcp_log_stackspecific log; 1900 1901 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1902 log.u_bbr.flex1 = seq; 1903 log.u_bbr.flex2 = count; 1904 log.u_bbr.flex8 = mode; 1905 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1906 &bbr->rc_inp->inp_socket->so_rcv, 1907 &bbr->rc_inp->inp_socket->so_snd, 1908 BBR_LOG_LOWGAIN, 0, 1909 0, &log, false, &bbr->rc_tv); 1910 } 1911 } 1912 1913 static void 1914 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling, 1915 uint8_t reason, uint32_t p_maxseg, int len) 1916 { 1917 if (tcp_bblogging_on(bbr->rc_tp)) { 1918 union tcp_log_stackspecific log; 1919 1920 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1921 log.u_bbr.flex1 = p_maxseg; 1922 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags; 1923 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 1924 log.u_bbr.flex4 = reason; 1925 log.u_bbr.flex5 = bbr->rc_in_persist; 1926 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val; 1927 log.u_bbr.flex7 = p_maxseg; 1928 log.u_bbr.flex8 = bbr->rc_in_persist; 1929 log.u_bbr.pkts_out = 0; 1930 log.u_bbr.applimited = len; 1931 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1932 &bbr->rc_inp->inp_socket->so_rcv, 1933 &bbr->rc_inp->inp_socket->so_snd, 1934 BBR_LOG_JUSTRET, 0, 1935 tlen, &log, false, &bbr->rc_tv); 1936 } 1937 } 1938 1939 static void 1940 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq) 1941 { 1942 if (tcp_bblogging_on(bbr->rc_tp)) { 1943 union tcp_log_stackspecific log; 1944 1945 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1946 log.u_bbr.flex1 = seq; 1947 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 1948 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start; 1949 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1950 &bbr->rc_inp->inp_socket->so_rcv, 1951 &bbr->rc_inp->inp_socket->so_snd, 1952 BBR_LOG_ENTREC, 0, 1953 0, &log, false, &bbr->rc_tv); 1954 } 1955 } 1956 1957 static void 1958 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) 1959 { 1960 if (tcp_bblogging_on(tp)) { 1961 union tcp_log_stackspecific log; 1962 1963 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1964 log.u_bbr.flex1 = tso; 1965 log.u_bbr.flex2 = maxseg; 1966 log.u_bbr.flex3 = mtu; 1967 log.u_bbr.flex4 = csum_flags; 1968 TCP_LOG_EVENTP(tp, NULL, 1969 &bbr->rc_inp->inp_socket->so_rcv, 1970 &bbr->rc_inp->inp_socket->so_snd, 1971 BBR_LOG_MSGSIZE, 0, 1972 0, &log, false, &bbr->rc_tv); 1973 } 1974 } 1975 1976 static void 1977 bbr_log_flowend(struct tcp_bbr *bbr) 1978 { 1979 if (tcp_bblogging_on(bbr->rc_tp)) { 1980 union tcp_log_stackspecific log; 1981 struct sockbuf *r, *s; 1982 struct timeval tv; 1983 1984 if (bbr->rc_inp->inp_socket) { 1985 r = &bbr->rc_inp->inp_socket->so_rcv; 1986 s = &bbr->rc_inp->inp_socket->so_snd; 1987 } else { 1988 r = s = NULL; 1989 } 1990 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv)); 1991 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1992 r, s, 1993 TCP_LOG_FLOWEND, 0, 1994 0, &log, false, &tv); 1995 } 1996 } 1997 1998 static void 1999 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, 2000 uint32_t lost, uint32_t del) 2001 { 2002 if (tcp_bblogging_on(bbr->rc_tp)) { 2003 union tcp_log_stackspecific log; 2004 2005 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2006 log.u_bbr.flex1 = lost; 2007 log.u_bbr.flex2 = del; 2008 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw; 2009 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt; 2010 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2011 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2012 log.u_bbr.flex7 = line; 2013 log.u_bbr.flex8 = 0; 2014 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count; 2015 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2016 &bbr->rc_inp->inp_socket->so_rcv, 2017 &bbr->rc_inp->inp_socket->so_snd, 2018 BBR_LOG_PKT_EPOCH, 0, 2019 0, &log, false, &bbr->rc_tv); 2020 } 2021 } 2022 2023 static void 2024 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time) 2025 { 2026 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2027 union tcp_log_stackspecific log; 2028 2029 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2030 log.u_bbr.flex1 = bbr->r_ctl.rc_lost; 2031 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat; 2032 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat; 2033 log.u_bbr.flex7 = line; 2034 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2035 &bbr->rc_inp->inp_socket->so_rcv, 2036 &bbr->rc_inp->inp_socket->so_snd, 2037 BBR_LOG_TIME_EPOCH, 0, 2038 0, &log, false, &bbr->rc_tv); 2039 } 2040 } 2041 2042 static void 2043 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth) 2044 { 2045 if (tcp_bblogging_on(bbr->rc_tp)) { 2046 union tcp_log_stackspecific log; 2047 2048 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2049 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2050 log.u_bbr.flex2 = new_tar; 2051 log.u_bbr.flex3 = line; 2052 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2053 log.u_bbr.flex5 = bbr_quanta; 2054 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs; 2055 log.u_bbr.flex7 = bbr->rc_last_options; 2056 log.u_bbr.flex8 = meth; 2057 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2058 &bbr->rc_inp->inp_socket->so_rcv, 2059 &bbr->rc_inp->inp_socket->so_snd, 2060 BBR_LOG_STATE_TARGET, 0, 2061 0, &log, false, &bbr->rc_tv); 2062 } 2063 2064 } 2065 2066 static void 2067 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2068 { 2069 if (tcp_bblogging_on(bbr->rc_tp)) { 2070 union tcp_log_stackspecific log; 2071 2072 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2073 log.u_bbr.flex1 = line; 2074 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2075 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2076 if (bbr_state_is_pkt_epoch) 2077 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 2078 else 2079 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP); 2080 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2081 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2082 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000); 2083 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra; 2084 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state; 2085 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2086 &bbr->rc_inp->inp_socket->so_rcv, 2087 &bbr->rc_inp->inp_socket->so_snd, 2088 BBR_LOG_STATE, 0, 2089 0, &log, false, &bbr->rc_tv); 2090 } 2091 } 2092 2093 static void 2094 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 2095 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond) 2096 { 2097 if (tcp_bblogging_on(bbr->rc_tp)) { 2098 union tcp_log_stackspecific log; 2099 2100 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2101 log.u_bbr.flex1 = line; 2102 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2103 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt; 2104 log.u_bbr.flex4 = applied; 2105 log.u_bbr.flex5 = rtt; 2106 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2107 log.u_bbr.flex7 = cond; 2108 log.u_bbr.flex8 = reas; 2109 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2110 &bbr->rc_inp->inp_socket->so_rcv, 2111 &bbr->rc_inp->inp_socket->so_snd, 2112 BBR_LOG_RTT_SHRINKS, 0, 2113 0, &log, false, &bbr->rc_tv); 2114 } 2115 } 2116 2117 static void 2118 bbr_log_type_exit_rec(struct tcp_bbr *bbr) 2119 { 2120 if (tcp_bblogging_on(bbr->rc_tp)) { 2121 union tcp_log_stackspecific log; 2122 2123 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2124 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start; 2125 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 2126 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2127 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2128 &bbr->rc_inp->inp_socket->so_rcv, 2129 &bbr->rc_inp->inp_socket->so_snd, 2130 BBR_LOG_EXITREC, 0, 2131 0, &log, false, &bbr->rc_tv); 2132 } 2133 } 2134 2135 static void 2136 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg, 2137 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line) 2138 { 2139 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2140 union tcp_log_stackspecific log; 2141 2142 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2143 log.u_bbr.flex1 = line; 2144 log.u_bbr.flex2 = prev_acked; 2145 log.u_bbr.flex3 = bytes_this_ack; 2146 log.u_bbr.flex4 = chg; 2147 log.u_bbr.flex5 = th_ack; 2148 log.u_bbr.flex6 = target; 2149 log.u_bbr.flex8 = meth; 2150 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2151 &bbr->rc_inp->inp_socket->so_rcv, 2152 &bbr->rc_inp->inp_socket->so_snd, 2153 BBR_LOG_CWND, 0, 2154 0, &log, false, &bbr->rc_tv); 2155 } 2156 } 2157 2158 static void 2159 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin) 2160 { 2161 /* 2162 * Log the rtt sample we are applying to the srtt algorithm in 2163 * useconds. 2164 */ 2165 if (tcp_bblogging_on(bbr->rc_tp)) { 2166 union tcp_log_stackspecific log; 2167 2168 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2169 log.u_bbr.flex1 = rtt; 2170 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time; 2171 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay; 2172 log.u_bbr.flex4 = bbr->rc_tp->ts_offset; 2173 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2174 log.u_bbr.pkts_out = tcp_tv_to_msec(&bbr->rc_tv); 2175 log.u_bbr.flex6 = tsin; 2176 log.u_bbr.flex7 = 0; 2177 log.u_bbr.flex8 = bbr->rc_ack_was_delayed; 2178 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2179 &bbr->rc_inp->inp_socket->so_rcv, 2180 &bbr->rc_inp->inp_socket->so_snd, 2181 TCP_LOG_RTT, 0, 2182 0, &log, false, &bbr->rc_tv); 2183 } 2184 } 2185 2186 static void 2187 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit) 2188 { 2189 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2190 union tcp_log_stackspecific log; 2191 2192 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2193 log.u_bbr.flex1 = time_in; 2194 log.u_bbr.flex2 = line; 2195 log.u_bbr.flex8 = enter_exit; 2196 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2197 &bbr->rc_inp->inp_socket->so_rcv, 2198 &bbr->rc_inp->inp_socket->so_snd, 2199 BBR_LOG_PERSIST, 0, 2200 0, &log, false, &bbr->rc_tv); 2201 } 2202 } 2203 static void 2204 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts) 2205 { 2206 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2207 union tcp_log_stackspecific log; 2208 2209 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2210 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age; 2211 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2212 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2213 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time; 2214 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2215 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2216 &bbr->rc_inp->inp_socket->so_rcv, 2217 &bbr->rc_inp->inp_socket->so_snd, 2218 BBR_LOG_ACKCLEAR, 0, 2219 0, &log, false, &bbr->rc_tv); 2220 } 2221 } 2222 2223 static void 2224 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen, 2225 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m) 2226 { 2227 if (tcp_bblogging_on(bbr->rc_tp)) { 2228 union tcp_log_stackspecific log; 2229 struct timeval tv; 2230 2231 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2232 log.u_bbr.flex1 = nsegs; 2233 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes; 2234 if (m) { 2235 struct timespec ts; 2236 2237 log.u_bbr.flex3 = m->m_flags; 2238 if (m->m_flags & M_TSTMP) { 2239 mbuf_tstmp2timespec(m, &ts); 2240 tv.tv_sec = ts.tv_sec; 2241 tv.tv_usec = ts.tv_nsec / 1000; 2242 log.u_bbr.lt_epoch = tcp_tv_to_usec(&tv); 2243 } else { 2244 log.u_bbr.lt_epoch = 0; 2245 } 2246 if (m->m_flags & M_TSTMP_LRO) { 2247 mbuf_tstmp2timeval(m, &tv); 2248 log.u_bbr.flex5 = tcp_tv_to_usec(&tv); 2249 } else { 2250 /* No arrival timestamp */ 2251 log.u_bbr.flex5 = 0; 2252 } 2253 2254 log.u_bbr.pkts_out = tcp_get_usecs(&tv); 2255 } else { 2256 log.u_bbr.flex3 = 0; 2257 log.u_bbr.flex5 = 0; 2258 log.u_bbr.flex6 = 0; 2259 log.u_bbr.pkts_out = 0; 2260 } 2261 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state; 2262 log.u_bbr.flex7 = bbr->r_wanted_output; 2263 log.u_bbr.flex8 = bbr->rc_in_persist; 2264 TCP_LOG_EVENTP(bbr->rc_tp, th, 2265 &bbr->rc_inp->inp_socket->so_rcv, 2266 &bbr->rc_inp->inp_socket->so_snd, 2267 TCP_LOG_IN, 0, 2268 tlen, &log, true, &bbr->rc_tv); 2269 } 2270 } 2271 2272 static void 2273 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out) 2274 { 2275 if (tcp_bblogging_on(bbr->rc_tp)) { 2276 union tcp_log_stackspecific log; 2277 2278 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2279 log.u_bbr.flex1 = did_out; 2280 log.u_bbr.flex2 = nxt_pkt; 2281 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val; 2282 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2283 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp; 2284 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes; 2285 log.u_bbr.flex7 = bbr->r_wanted_output; 2286 log.u_bbr.flex8 = bbr->rc_in_persist; 2287 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay; 2288 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2289 &bbr->rc_inp->inp_socket->so_rcv, 2290 &bbr->rc_inp->inp_socket->so_snd, 2291 BBR_LOG_DOSEG_DONE, 0, 2292 0, &log, true, &bbr->rc_tv); 2293 } 2294 } 2295 2296 static void 2297 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts, 2298 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz) 2299 { 2300 if (tcp_bblogging_on(bbr->rc_tp)) { 2301 union tcp_log_stackspecific log; 2302 2303 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2304 log.u_bbr.flex1 = line; 2305 log.u_bbr.flex2 = o_len; 2306 log.u_bbr.flex3 = segcnt; 2307 log.u_bbr.flex4 = segsiz; 2308 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2309 &bbr->rc_inp->inp_socket->so_rcv, 2310 &bbr->rc_inp->inp_socket->so_snd, 2311 BBR_LOG_ENOBUF_JMP, ENOBUFS, 2312 len, &log, true, &bbr->rc_tv); 2313 } 2314 } 2315 2316 static void 2317 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling) 2318 { 2319 if (tcp_bblogging_on(bbr->rc_tp)) { 2320 union tcp_log_stackspecific log; 2321 2322 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2323 log.u_bbr.flex1 = timers; 2324 log.u_bbr.flex2 = ret; 2325 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 2326 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2327 log.u_bbr.flex5 = cts; 2328 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2329 log.u_bbr.flex8 = hpts_calling; 2330 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2331 &bbr->rc_inp->inp_socket->so_rcv, 2332 &bbr->rc_inp->inp_socket->so_snd, 2333 BBR_LOG_TO_PROCESS, 0, 2334 0, &log, false, &bbr->rc_tv); 2335 } 2336 } 2337 2338 static void 2339 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num) 2340 { 2341 if (tcp_bblogging_on(bbr->rc_tp)) { 2342 union tcp_log_stackspecific log; 2343 uint64_t ar; 2344 2345 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2346 log.u_bbr.flex1 = bbr->bbr_timer_src; 2347 log.u_bbr.flex2 = 0; 2348 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2349 ar = (uintptr_t)(bbr->r_ctl.rc_resend); 2350 ar >>= 32; 2351 ar &= 0x00000000ffffffff; 2352 log.u_bbr.flex4 = (uint32_t)ar; 2353 ar = (uintptr_t)bbr->r_ctl.rc_resend; 2354 ar &= 0x00000000ffffffff; 2355 log.u_bbr.flex5 = (uint32_t)ar; 2356 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2357 log.u_bbr.flex8 = to_num; 2358 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2359 &bbr->rc_inp->inp_socket->so_rcv, 2360 &bbr->rc_inp->inp_socket->so_snd, 2361 BBR_LOG_RTO, 0, 2362 0, &log, false, &bbr->rc_tv); 2363 } 2364 } 2365 2366 static void 2367 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason) 2368 { 2369 if (tcp_bblogging_on(bbr->rc_tp)) { 2370 union tcp_log_stackspecific log; 2371 2372 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2373 log.u_bbr.flex1 = flex1; 2374 log.u_bbr.flex2 = flex2; 2375 log.u_bbr.flex3 = flex3; 2376 log.u_bbr.flex4 = 0; 2377 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2378 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2379 log.u_bbr.flex8 = reason; 2380 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw; 2381 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2382 &bbr->rc_inp->inp_socket->so_rcv, 2383 &bbr->rc_inp->inp_socket->so_snd, 2384 BBR_LOG_REDUCE, 0, 2385 0, &log, false, &bbr->rc_tv); 2386 } 2387 } 2388 2389 static void 2390 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag) 2391 { 2392 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2393 union tcp_log_stackspecific log; 2394 2395 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2396 log.u_bbr.flex1 = diag->p_nxt_slot; 2397 log.u_bbr.flex2 = diag->p_cur_slot; 2398 log.u_bbr.flex3 = diag->slot_req; 2399 log.u_bbr.flex4 = diag->inp_hptsslot; 2400 log.u_bbr.flex5 = diag->slot_remaining; 2401 log.u_bbr.flex6 = diag->need_new_to; 2402 log.u_bbr.flex7 = diag->p_hpts_active; 2403 log.u_bbr.flex8 = diag->p_on_min_sleep; 2404 /* Hijack other fields as needed */ 2405 log.u_bbr.epoch = diag->have_slept; 2406 log.u_bbr.lt_epoch = diag->yet_to_sleep; 2407 log.u_bbr.pkts_out = diag->co_ret; 2408 log.u_bbr.applimited = diag->hpts_sleep_time; 2409 log.u_bbr.delivered = diag->p_prev_slot; 2410 log.u_bbr.inflight = diag->p_runningslot; 2411 log.u_bbr.bw_inuse = diag->wheel_slot; 2412 log.u_bbr.rttProp = diag->wheel_cts; 2413 log.u_bbr.delRate = diag->maxslots; 2414 log.u_bbr.cur_del_rate = diag->p_curtick; 2415 log.u_bbr.cur_del_rate <<= 32; 2416 log.u_bbr.cur_del_rate |= diag->p_lasttick; 2417 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2418 &bbr->rc_inp->inp_socket->so_rcv, 2419 &bbr->rc_inp->inp_socket->so_snd, 2420 BBR_LOG_HPTSDIAG, 0, 2421 0, &log, false, &bbr->rc_tv); 2422 } 2423 } 2424 2425 static void 2426 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt, 2427 uint32_t thresh, uint32_t to) 2428 { 2429 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2430 union tcp_log_stackspecific log; 2431 2432 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2433 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar; 2434 log.u_bbr.flex2 = time_since_sent; 2435 log.u_bbr.flex3 = srtt; 2436 log.u_bbr.flex4 = thresh; 2437 log.u_bbr.flex5 = to; 2438 log.u_bbr.flex6 = bbr->rc_tp->t_srtt; 2439 log.u_bbr.flex8 = mode; 2440 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2441 &bbr->rc_inp->inp_socket->so_rcv, 2442 &bbr->rc_inp->inp_socket->so_snd, 2443 BBR_LOG_TIMERPREP, 0, 2444 0, &log, false, &bbr->rc_tv); 2445 } 2446 } 2447 2448 static void 2449 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 2450 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod) 2451 { 2452 if (tcp_bblogging_on(bbr->rc_tp)) { 2453 union tcp_log_stackspecific log; 2454 2455 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2456 log.u_bbr.flex1 = usecs; 2457 log.u_bbr.flex2 = len; 2458 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff); 2459 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff); 2460 if (override) 2461 log.u_bbr.flex5 = (1 << 2); 2462 else 2463 log.u_bbr.flex5 = 0; 2464 log.u_bbr.flex6 = override; 2465 log.u_bbr.flex7 = gain; 2466 log.u_bbr.flex8 = mod; 2467 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2468 &bbr->rc_inp->inp_socket->so_rcv, 2469 &bbr->rc_inp->inp_socket->so_snd, 2470 BBR_LOG_HPTSI_CALC, 0, 2471 len, &log, false, &bbr->rc_tv); 2472 } 2473 } 2474 2475 static void 2476 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2477 { 2478 if (tcp_bblogging_on(bbr->rc_tp)) { 2479 union tcp_log_stackspecific log; 2480 2481 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2482 2483 log.u_bbr.flex1 = bbr->bbr_timer_src; 2484 log.u_bbr.flex2 = to; 2485 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2486 log.u_bbr.flex4 = slot; 2487 log.u_bbr.flex5 = bbr->rc_tp->t_hpts_slot; 2488 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2489 log.u_bbr.pkts_out = bbr->rc_tp->t_flags2; 2490 log.u_bbr.flex8 = which; 2491 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2492 &bbr->rc_inp->inp_socket->so_rcv, 2493 &bbr->rc_inp->inp_socket->so_snd, 2494 BBR_LOG_TIMERSTAR, 0, 2495 0, &log, false, &bbr->rc_tv); 2496 } 2497 } 2498 2499 static void 2500 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) 2501 { 2502 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2503 union tcp_log_stackspecific log; 2504 2505 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2506 log.u_bbr.flex1 = thresh; 2507 log.u_bbr.flex2 = lro; 2508 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts; 2509 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 2510 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2511 log.u_bbr.flex6 = srtt; 2512 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift; 2513 log.u_bbr.flex8 = frm; 2514 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2515 &bbr->rc_inp->inp_socket->so_rcv, 2516 &bbr->rc_inp->inp_socket->so_snd, 2517 BBR_LOG_THRESH_CALC, 0, 2518 0, &log, false, &bbr->rc_tv); 2519 } 2520 } 2521 2522 static void 2523 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed) 2524 { 2525 if (tcp_bblogging_on(bbr->rc_tp)) { 2526 union tcp_log_stackspecific log; 2527 2528 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2529 log.u_bbr.flex1 = line; 2530 log.u_bbr.flex2 = bbr->bbr_timer_src; 2531 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2532 log.u_bbr.flex4 = bbr->rc_in_persist; 2533 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2534 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2535 log.u_bbr.flex8 = hpts_removed; 2536 log.u_bbr.pkts_out = bbr->rc_pacer_started; 2537 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2538 &bbr->rc_inp->inp_socket->so_rcv, 2539 &bbr->rc_inp->inp_socket->so_snd, 2540 BBR_LOG_TIMERCANC, 0, 2541 0, &log, false, &bbr->rc_tv); 2542 } 2543 } 2544 2545 static void 2546 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta) 2547 { 2548 if (tcp_bblogging_on(bbr->rc_tp)) { 2549 union tcp_log_stackspecific log; 2550 2551 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2552 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio; 2553 log.u_bbr.flex2 = (peer_delta >> 32); 2554 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff); 2555 log.u_bbr.flex4 = (delta >> 32); 2556 log.u_bbr.flex5 = (delta & 0x00000000ffffffff); 2557 log.u_bbr.flex7 = bbr->rc_ts_clock_set; 2558 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used; 2559 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2560 &bbr->rc_inp->inp_socket->so_rcv, 2561 &bbr->rc_inp->inp_socket->so_snd, 2562 BBR_LOG_TSTMP_VAL, 0, 2563 0, &log, false, &bbr->rc_tv); 2564 } 2565 } 2566 2567 static void 2568 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) 2569 { 2570 if (tcp_bblogging_on(bbr->rc_tp)) { 2571 union tcp_log_stackspecific log; 2572 2573 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2574 log.u_bbr.flex1 = tsosz; 2575 log.u_bbr.flex2 = tls; 2576 log.u_bbr.flex3 = tcp_min_hptsi_time; 2577 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min; 2578 log.u_bbr.flex5 = old_val; 2579 log.u_bbr.flex6 = maxseg; 2580 log.u_bbr.flex7 = bbr->rc_no_pacing; 2581 log.u_bbr.flex7 <<= 1; 2582 log.u_bbr.flex7 |= bbr->rc_past_init_win; 2583 if (hdwr) 2584 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google; 2585 else 2586 log.u_bbr.flex8 = bbr->rc_use_google; 2587 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2588 &bbr->rc_inp->inp_socket->so_rcv, 2589 &bbr->rc_inp->inp_socket->so_snd, 2590 BBR_LOG_BBRTSO, 0, 2591 0, &log, false, &bbr->rc_tv); 2592 } 2593 } 2594 2595 static void 2596 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, 2597 uint32_t flags, uint32_t line) 2598 { 2599 if (tcp_bblogging_on(bbr->rc_tp)) { 2600 union tcp_log_stackspecific log; 2601 2602 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2603 log.u_bbr.flex1 = line; 2604 log.u_bbr.flex2 = rsm->r_start; 2605 log.u_bbr.flex3 = rsm->r_end; 2606 log.u_bbr.flex4 = rsm->r_delivered; 2607 log.u_bbr.flex5 = rsm->r_rtr_cnt; 2608 log.u_bbr.flex6 = rsm->r_dupack; 2609 log.u_bbr.flex7 = rsm->r_tim_lastsent[0]; 2610 log.u_bbr.flex8 = rsm->r_flags; 2611 /* Hijack the pkts_out fids */ 2612 log.u_bbr.applimited = flags; 2613 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2614 &bbr->rc_inp->inp_socket->so_rcv, 2615 &bbr->rc_inp->inp_socket->so_snd, 2616 BBR_RSM_CLEARED, 0, 2617 0, &log, false, &bbr->rc_tv); 2618 } 2619 } 2620 2621 static void 2622 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts, 2623 uint32_t flex3, uint32_t flex2, uint32_t flex5, 2624 uint32_t flex6, uint32_t pkts_out, int flex7, 2625 uint32_t flex4, uint32_t flex1) 2626 { 2627 2628 if (tcp_bblogging_on(bbr->rc_tp)) { 2629 union tcp_log_stackspecific log; 2630 2631 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2632 log.u_bbr.flex1 = flex1; 2633 log.u_bbr.flex2 = flex2; 2634 log.u_bbr.flex3 = flex3; 2635 log.u_bbr.flex4 = flex4; 2636 log.u_bbr.flex5 = flex5; 2637 log.u_bbr.flex6 = flex6; 2638 log.u_bbr.flex7 = flex7; 2639 /* Hijack the pkts_out fids */ 2640 log.u_bbr.pkts_out = pkts_out; 2641 log.u_bbr.flex8 = flex8; 2642 if (bbr->rc_ack_was_delayed) 2643 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay; 2644 else 2645 log.u_bbr.epoch = 0; 2646 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2647 &bbr->rc_inp->inp_socket->so_rcv, 2648 &bbr->rc_inp->inp_socket->so_snd, 2649 BBR_LOG_BBRUPD, 0, 2650 flex2, &log, false, &bbr->rc_tv); 2651 } 2652 } 2653 2654 static void 2655 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason, 2656 uint32_t newbw, uint32_t obw, uint32_t diff, 2657 uint32_t tim) 2658 { 2659 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) { 2660 union tcp_log_stackspecific log; 2661 2662 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2663 log.u_bbr.flex1 = reason; 2664 log.u_bbr.flex2 = newbw; 2665 log.u_bbr.flex3 = obw; 2666 log.u_bbr.flex4 = diff; 2667 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost; 2668 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del; 2669 log.u_bbr.flex7 = bbr->rc_lt_is_sampling; 2670 log.u_bbr.pkts_out = tim; 2671 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw; 2672 if (bbr->rc_lt_use_bw == 0) 2673 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 2674 else 2675 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 2676 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2677 &bbr->rc_inp->inp_socket->so_rcv, 2678 &bbr->rc_inp->inp_socket->so_snd, 2679 BBR_LOG_BWSAMP, 0, 2680 0, &log, false, &bbr->rc_tv); 2681 } 2682 } 2683 2684 static inline void 2685 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line) 2686 { 2687 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2688 union tcp_log_stackspecific log; 2689 2690 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2691 log.u_bbr.flex1 = line; 2692 log.u_bbr.flex2 = tick; 2693 log.u_bbr.flex3 = tp->t_maxunacktime; 2694 log.u_bbr.flex4 = tp->t_acktime; 2695 log.u_bbr.flex8 = event; 2696 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2697 &bbr->rc_inp->inp_socket->so_rcv, 2698 &bbr->rc_inp->inp_socket->so_snd, 2699 BBR_LOG_PROGRESS, 0, 2700 0, &log, false, &bbr->rc_tv); 2701 } 2702 } 2703 2704 static void 2705 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp, 2706 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts, 2707 int error) 2708 { 2709 if (tcp_bblogging_on(bbr->rc_tp)) { 2710 union tcp_log_stackspecific log; 2711 uint64_t ifp64 = (uintptr_t)ifp; 2712 2713 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2714 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2715 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2716 log.u_bbr.flex3 = ((ifp64 >> 32) & 0x00000000ffffffff); 2717 log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff); 2718 log.u_bbr.bw_inuse = rate; 2719 log.u_bbr.flex5 = line; 2720 log.u_bbr.flex6 = error; 2721 log.u_bbr.flex8 = bbr->skip_gain; 2722 log.u_bbr.flex8 <<= 1; 2723 log.u_bbr.flex8 |= bbr->gain_is_limited; 2724 log.u_bbr.flex8 <<= 1; 2725 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing; 2726 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 2727 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2728 &bbr->rc_inp->inp_socket->so_rcv, 2729 &bbr->rc_inp->inp_socket->so_snd, 2730 BBR_LOG_HDWR_PACE, 0, 2731 0, &log, false, &bbr->rc_tv); 2732 } 2733 } 2734 2735 static void 2736 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) 2737 { 2738 if (tcp_bblogging_on(bbr->rc_tp)) { 2739 union tcp_log_stackspecific log; 2740 2741 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2742 log.u_bbr.flex1 = slot; 2743 log.u_bbr.flex2 = del_by; 2744 log.u_bbr.flex3 = prev_delay; 2745 log.u_bbr.flex4 = line; 2746 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val; 2747 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay; 2748 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags); 2749 log.u_bbr.flex8 = bbr->rc_in_persist; 2750 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2751 &bbr->rc_inp->inp_socket->so_rcv, 2752 &bbr->rc_inp->inp_socket->so_snd, 2753 BBR_LOG_BBRSND, 0, 2754 len, &log, false, &bbr->rc_tv); 2755 } 2756 } 2757 2758 static void 2759 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) 2760 { 2761 if (tcp_bblogging_on(bbr->rc_tp)) { 2762 union tcp_log_stackspecific log; 2763 2764 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2765 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered; 2766 log.u_bbr.flex2 = 0; 2767 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt; 2768 log.u_bbr.flex4 = end; 2769 log.u_bbr.flex5 = seq; 2770 log.u_bbr.flex6 = t; 2771 log.u_bbr.flex7 = match; 2772 log.u_bbr.flex8 = flags; 2773 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2774 &bbr->rc_inp->inp_socket->so_rcv, 2775 &bbr->rc_inp->inp_socket->so_snd, 2776 BBR_LOG_BBRRTT, 0, 2777 0, &log, false, &bbr->rc_tv); 2778 } 2779 } 2780 2781 static void 2782 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method) 2783 { 2784 if (tcp_bblogging_on(bbr->rc_tp)) { 2785 union tcp_log_stackspecific log; 2786 2787 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2788 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2789 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 2790 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch; 2791 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2792 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs; 2793 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight; 2794 log.u_bbr.flex7 = 0; 2795 log.u_bbr.flex8 = entry_method; 2796 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2797 &bbr->rc_inp->inp_socket->so_rcv, 2798 &bbr->rc_inp->inp_socket->so_snd, 2799 BBR_LOG_EXIT_GAIN, 0, 2800 0, &log, false, &bbr->rc_tv); 2801 } 2802 } 2803 2804 static void 2805 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired) 2806 { 2807 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2808 union tcp_log_stackspecific log; 2809 2810 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2811 /* R-HU */ 2812 log.u_bbr.flex1 = 0; 2813 log.u_bbr.flex2 = 0; 2814 log.u_bbr.flex3 = 0; 2815 log.u_bbr.flex4 = 0; 2816 log.u_bbr.flex7 = 0; 2817 log.u_bbr.flex8 = settings_desired; 2818 2819 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2820 &bbr->rc_inp->inp_socket->so_rcv, 2821 &bbr->rc_inp->inp_socket->so_snd, 2822 BBR_LOG_SETTINGS_CHG, 0, 2823 0, &log, false, &bbr->rc_tv); 2824 } 2825 } 2826 2827 /* 2828 * Returns the bw from the our filter. 2829 */ 2830 static inline uint64_t 2831 bbr_get_full_bw(struct tcp_bbr *bbr) 2832 { 2833 uint64_t bw; 2834 2835 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2836 2837 return (bw); 2838 } 2839 2840 static inline void 2841 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2842 { 2843 uint64_t calclr; 2844 uint32_t lost, del; 2845 2846 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch) 2847 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch; 2848 else 2849 lost = 0; 2850 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del; 2851 if (lost == 0) { 2852 calclr = 0; 2853 } else if (del) { 2854 calclr = lost; 2855 calclr *= (uint64_t)1000; 2856 calclr /= (uint64_t)del; 2857 } else { 2858 /* Nothing delivered? 100.0% loss */ 2859 calclr = 1000; 2860 } 2861 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr; 2862 if (IN_RECOVERY(bbr->rc_tp->t_flags)) 2863 bbr->r_ctl.recovery_lr += (uint32_t)calclr; 2864 bbr->r_ctl.rc_pkt_epoch++; 2865 if (bbr->rc_no_pacing && 2866 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) { 2867 bbr->rc_no_pacing = 0; 2868 tcp_bbr_tso_size_check(bbr, cts); 2869 } 2870 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time); 2871 bbr->r_ctl.rc_pkt_epoch_time = cts; 2872 /* What was our loss rate */ 2873 bbr_log_pkt_epoch(bbr, cts, line, lost, del); 2874 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered; 2875 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost; 2876 } 2877 2878 static inline void 2879 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2880 { 2881 uint32_t epoch_time; 2882 2883 /* Tick the RTT clock */ 2884 bbr->r_ctl.rc_rtt_epoch++; 2885 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start; 2886 bbr_log_time_epoch(bbr, cts, line, epoch_time); 2887 bbr->r_ctl.rc_rcv_epoch_start = cts; 2888 } 2889 2890 static inline void 2891 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked) 2892 { 2893 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) { 2894 bbr->rc_is_pkt_epoch_now = 1; 2895 } 2896 } 2897 2898 /* 2899 * Returns the bw from either the b/w filter 2900 * or from the lt_bw (if the connection is being 2901 * policed). 2902 */ 2903 static inline uint64_t 2904 __bbr_get_bw(struct tcp_bbr *bbr) 2905 { 2906 uint64_t bw, min_bw; 2907 uint64_t rtt; 2908 int gm_measure_cnt = 1; 2909 2910 /* 2911 * For startup we make, like google, a 2912 * minimum b/w. This is generated from the 2913 * IW and the rttProp. We do fall back to srtt 2914 * if for some reason (initial handshake) we don't 2915 * have a rttProp. We, in the worst case, fall back 2916 * to the configured min_bw (rc_initial_hptsi_bw). 2917 */ 2918 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 2919 /* Attempt first to use rttProp */ 2920 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2921 if (rtt && (rtt < 0xffffffff)) { 2922 measure: 2923 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2924 ((uint64_t)1000000); 2925 min_bw /= rtt; 2926 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2927 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2928 } 2929 2930 } else if (bbr->rc_tp->t_srtt != 0) { 2931 /* No rttProp, use srtt? */ 2932 rtt = bbr_get_rtt(bbr, BBR_SRTT); 2933 goto measure; 2934 } else { 2935 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2936 } 2937 } else 2938 min_bw = 0; 2939 2940 if ((bbr->rc_past_init_win == 0) && 2941 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp))) 2942 bbr->rc_past_init_win = 1; 2943 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1)) 2944 gm_measure_cnt = 0; 2945 if (gm_measure_cnt && 2946 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) || 2947 (bbr->rc_past_init_win == 0))) { 2948 /* For google we use our guess rate until we get 1 measurement */ 2949 2950 use_initial_window: 2951 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2952 if (rtt && (rtt < 0xffffffff)) { 2953 /* 2954 * We have an RTT measurement. Use that in 2955 * combination with our initial window to calculate 2956 * a b/w. 2957 */ 2958 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2959 ((uint64_t)1000000); 2960 bw /= rtt; 2961 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2962 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2963 } 2964 } else { 2965 /* Drop back to the 40 and punt to a default */ 2966 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2967 } 2968 if (bw < 1) 2969 /* Probably should panic */ 2970 bw = 1; 2971 if (bw > min_bw) 2972 return (bw); 2973 else 2974 return (min_bw); 2975 } 2976 if (bbr->rc_lt_use_bw) 2977 bw = bbr->r_ctl.rc_lt_bw; 2978 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0)) 2979 bw = bbr->r_ctl.red_bw; 2980 else 2981 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2982 if (bw == 0) { 2983 /* We should not be at 0, go to the initial window then */ 2984 goto use_initial_window; 2985 } 2986 if (bw < min_bw) 2987 bw = min_bw; 2988 return (bw); 2989 } 2990 2991 static inline uint64_t 2992 bbr_get_bw(struct tcp_bbr *bbr) 2993 { 2994 uint64_t bw; 2995 2996 bw = __bbr_get_bw(bbr); 2997 return (bw); 2998 } 2999 3000 static inline void 3001 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts) 3002 { 3003 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch; 3004 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time; 3005 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3006 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3007 } 3008 3009 static inline void 3010 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts) 3011 { 3012 bbr->rc_lt_is_sampling = 0; 3013 bbr->rc_lt_use_bw = 0; 3014 bbr->r_ctl.rc_lt_bw = 0; 3015 bbr_reset_lt_bw_interval(bbr, cts); 3016 } 3017 3018 static inline void 3019 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin) 3020 { 3021 uint64_t diff; 3022 3023 /* Do we have a previous sample? */ 3024 if (bbr->r_ctl.rc_lt_bw) { 3025 /* Get the diff in bytes per second */ 3026 if (bbr->r_ctl.rc_lt_bw > bw) 3027 diff = bbr->r_ctl.rc_lt_bw - bw; 3028 else 3029 diff = bw - bbr->r_ctl.rc_lt_bw; 3030 if ((diff <= bbr_lt_bw_diff) || 3031 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) { 3032 /* Consider us policed */ 3033 uint32_t saved_bw; 3034 3035 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw; 3036 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */ 3037 bbr->rc_lt_use_bw = 1; 3038 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 3039 /* 3040 * Use pkt based epoch for measuring length of 3041 * policer up 3042 */ 3043 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch; 3044 /* 3045 * reason 4 is we need to start consider being 3046 * policed 3047 */ 3048 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin); 3049 return; 3050 } 3051 } 3052 bbr->r_ctl.rc_lt_bw = bw; 3053 bbr_reset_lt_bw_interval(bbr, cts); 3054 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin); 3055 } 3056 3057 static void 3058 bbr_randomize_extra_state_time(struct tcp_bbr *bbr) 3059 { 3060 uint32_t ran, deduct; 3061 3062 ran = arc4random_uniform(bbr_rand_ot); 3063 if (ran) { 3064 deduct = bbr->r_ctl.rc_level_state_extra / ran; 3065 bbr->r_ctl.rc_level_state_extra -= deduct; 3066 } 3067 } 3068 /* 3069 * Return randomly the starting state 3070 * to use in probebw. 3071 */ 3072 static uint8_t 3073 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts) 3074 { 3075 uint32_t ran; 3076 uint8_t ret_val; 3077 3078 /* Initialize the offset to 0 */ 3079 bbr->r_ctl.rc_exta_time_gd = 0; 3080 bbr->rc_hit_state_1 = 0; 3081 bbr->r_ctl.rc_level_state_extra = 0; 3082 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1)); 3083 /* 3084 * The math works funny here :) the return value is used to set the 3085 * substate and then the state change is called which increments by 3086 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when 3087 * we fully enter the state. Note that the (8 - 1 - ran) assures that 3088 * we return 1 - 7, so we dont return 0 and end up starting in 3089 * state 1 (DRAIN). 3090 */ 3091 ret_val = BBR_SUBSTATE_COUNT - 1 - ran; 3092 /* Set an epoch */ 3093 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) 3094 bbr_set_epoch(bbr, cts, __LINE__); 3095 3096 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 3097 return (ret_val); 3098 } 3099 3100 static void 3101 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected) 3102 { 3103 uint32_t diff, d_time; 3104 uint64_t del_time, bw, lost, delivered; 3105 3106 if (bbr->r_use_policer == 0) 3107 return; 3108 if (bbr->rc_lt_use_bw) { 3109 /* We are using lt bw do we stop yet? */ 3110 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 3111 if (diff > bbr_lt_bw_max_rtts) { 3112 /* Reset it all */ 3113 reset_all: 3114 bbr_reset_lt_bw_sampling(bbr, cts); 3115 if (bbr->rc_filled_pipe) { 3116 bbr_set_epoch(bbr, cts, __LINE__); 3117 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 3118 bbr_substate_change(bbr, cts, __LINE__, 0); 3119 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 3120 bbr_log_type_statechange(bbr, cts, __LINE__); 3121 } else { 3122 /* 3123 * This should not happen really 3124 * unless we remove the startup/drain 3125 * restrictions above. 3126 */ 3127 bbr->rc_bbr_state = BBR_STATE_STARTUP; 3128 bbr_set_epoch(bbr, cts, __LINE__); 3129 bbr->r_ctl.rc_bbr_state_time = cts; 3130 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 3131 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 3132 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 3133 bbr_set_state_target(bbr, __LINE__); 3134 bbr_log_type_statechange(bbr, cts, __LINE__); 3135 } 3136 /* reason 0 is to stop using lt-bw */ 3137 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0); 3138 return; 3139 } 3140 if (bbr_lt_intvl_fp == 0) { 3141 /* Not doing false-positive detection */ 3142 return; 3143 } 3144 /* False positive detection */ 3145 if (diff == bbr_lt_intvl_fp) { 3146 /* At bbr_lt_intvl_fp we record the lost */ 3147 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3148 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3149 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) { 3150 /* Now is our loss rate still high? */ 3151 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3152 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3153 if ((delivered == 0) || 3154 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) { 3155 /* No still below our threshold */ 3156 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0); 3157 } else { 3158 /* Yikes its still high, it must be a false positive */ 3159 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0); 3160 goto reset_all; 3161 } 3162 } 3163 return; 3164 } 3165 /* 3166 * Wait for the first loss before sampling, to let the policer 3167 * exhaust its tokens and estimate the steady-state rate allowed by 3168 * the policer. Starting samples earlier includes bursts that 3169 * over-estimate the bw. 3170 */ 3171 if (bbr->rc_lt_is_sampling == 0) { 3172 /* reason 1 is to begin doing the sampling */ 3173 if (loss_detected == 0) 3174 return; 3175 bbr_reset_lt_bw_interval(bbr, cts); 3176 bbr->rc_lt_is_sampling = 1; 3177 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0); 3178 return; 3179 } 3180 /* Now how long were we delivering long term last> */ 3181 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time)) 3182 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time; 3183 else 3184 d_time = 0; 3185 3186 /* To avoid underestimates, reset sampling if we run out of data. */ 3187 if (bbr->r_ctl.r_app_limited_until) { 3188 /* Can not measure in app-limited state */ 3189 bbr_reset_lt_bw_sampling(bbr, cts); 3190 /* reason 2 is to reset sampling due to app limits */ 3191 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time); 3192 return; 3193 } 3194 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 3195 if (diff < bbr_lt_intvl_min_rtts) { 3196 /* 3197 * need more samples (we don't 3198 * start on a round like linux so 3199 * we need 1 more). 3200 */ 3201 /* 6 is not_enough time or no-loss */ 3202 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3203 return; 3204 } 3205 if (diff > (4 * bbr_lt_intvl_min_rtts)) { 3206 /* 3207 * For now if we wait too long, reset all sampling. We need 3208 * to do some research here, its possible that we should 3209 * base this on how much loss as occurred.. something like 3210 * if its under 10% (or some thresh) reset all otherwise 3211 * don't. Thats for phase II I guess. 3212 */ 3213 bbr_reset_lt_bw_sampling(bbr, cts); 3214 /* reason 3 is to reset sampling due too long of sampling */ 3215 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3216 return; 3217 } 3218 /* 3219 * End sampling interval when a packet is lost, so we estimate the 3220 * policer tokens were exhausted. Stopping the sampling before the 3221 * tokens are exhausted under-estimates the policed rate. 3222 */ 3223 if (loss_detected == 0) { 3224 /* 6 is not_enough time or no-loss */ 3225 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3226 return; 3227 } 3228 /* Calculate packets lost and delivered in sampling interval. */ 3229 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3230 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3231 if ((delivered == 0) || 3232 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) { 3233 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time); 3234 return; 3235 } 3236 if (d_time < 1000) { 3237 /* Not enough time. wait */ 3238 /* 6 is not_enough time or no-loss */ 3239 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3240 return; 3241 } 3242 if (d_time >= (0xffffffff / USECS_IN_MSEC)) { 3243 /* Too long */ 3244 bbr_reset_lt_bw_sampling(bbr, cts); 3245 /* reason 3 is to reset sampling due too long of sampling */ 3246 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3247 return; 3248 } 3249 del_time = d_time; 3250 bw = delivered; 3251 bw *= (uint64_t)USECS_IN_SECOND; 3252 bw /= del_time; 3253 bbr_lt_bw_samp_done(bbr, bw, cts, d_time); 3254 } 3255 3256 /* 3257 * Allocate a sendmap from our zone. 3258 */ 3259 static struct bbr_sendmap * 3260 bbr_alloc(struct tcp_bbr *bbr) 3261 { 3262 struct bbr_sendmap *rsm; 3263 3264 BBR_STAT_INC(bbr_to_alloc); 3265 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO)); 3266 if (rsm) { 3267 bbr->r_ctl.rc_num_maps_alloced++; 3268 return (rsm); 3269 } 3270 if (bbr->r_ctl.rc_free_cnt) { 3271 BBR_STAT_INC(bbr_to_alloc_emerg); 3272 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 3273 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 3274 bbr->r_ctl.rc_free_cnt--; 3275 return (rsm); 3276 } 3277 BBR_STAT_INC(bbr_to_alloc_failed); 3278 return (NULL); 3279 } 3280 3281 static struct bbr_sendmap * 3282 bbr_alloc_full_limit(struct tcp_bbr *bbr) 3283 { 3284 if ((V_tcp_map_entries_limit > 0) && 3285 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3286 BBR_STAT_INC(bbr_alloc_limited); 3287 if (!bbr->alloc_limit_reported) { 3288 bbr->alloc_limit_reported = 1; 3289 BBR_STAT_INC(bbr_alloc_limited_conns); 3290 } 3291 return (NULL); 3292 } 3293 return (bbr_alloc(bbr)); 3294 } 3295 3296 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3297 static struct bbr_sendmap * 3298 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type) 3299 { 3300 struct bbr_sendmap *rsm; 3301 3302 if (limit_type) { 3303 /* currently there is only one limit type */ 3304 if (V_tcp_map_split_limit > 0 && 3305 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3306 BBR_STAT_INC(bbr_split_limited); 3307 if (!bbr->alloc_limit_reported) { 3308 bbr->alloc_limit_reported = 1; 3309 BBR_STAT_INC(bbr_alloc_limited_conns); 3310 } 3311 return (NULL); 3312 } 3313 } 3314 3315 /* allocate and mark in the limit type, if set */ 3316 rsm = bbr_alloc(bbr); 3317 if (rsm != NULL && limit_type) { 3318 rsm->r_limit_type = limit_type; 3319 bbr->r_ctl.rc_num_split_allocs++; 3320 } 3321 return (rsm); 3322 } 3323 3324 static void 3325 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 3326 { 3327 if (rsm->r_limit_type) { 3328 /* currently there is only one limit type */ 3329 bbr->r_ctl.rc_num_split_allocs--; 3330 } 3331 if (rsm->r_is_smallmap) 3332 bbr->r_ctl.rc_num_small_maps_alloced--; 3333 if (bbr->r_ctl.rc_tlp_send == rsm) 3334 bbr->r_ctl.rc_tlp_send = NULL; 3335 if (bbr->r_ctl.rc_resend == rsm) { 3336 bbr->r_ctl.rc_resend = NULL; 3337 } 3338 if (bbr->r_ctl.rc_next == rsm) 3339 bbr->r_ctl.rc_next = NULL; 3340 if (bbr->r_ctl.rc_sacklast == rsm) 3341 bbr->r_ctl.rc_sacklast = NULL; 3342 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 3343 memset(rsm, 0, sizeof(struct bbr_sendmap)); 3344 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 3345 rsm->r_limit_type = 0; 3346 bbr->r_ctl.rc_free_cnt++; 3347 return; 3348 } 3349 bbr->r_ctl.rc_num_maps_alloced--; 3350 uma_zfree(bbr_zone, rsm); 3351 } 3352 3353 /* 3354 * Returns the BDP. 3355 */ 3356 static uint64_t 3357 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) { 3358 /* 3359 * Calculate the bytes in flight needed given the bw (in bytes per 3360 * second) and the specifyed rtt in useconds. We need to put out the 3361 * returned value per RTT to match that rate. Gain will normally 3362 * raise it up from there. 3363 * 3364 * This should not overflow as long as the bandwidth is below 1 3365 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller 3366 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30). 3367 */ 3368 uint64_t usec_per_sec; 3369 3370 usec_per_sec = USECS_IN_SECOND; 3371 return ((rtt * bw) / usec_per_sec); 3372 } 3373 3374 /* 3375 * Return the initial cwnd. 3376 */ 3377 static uint32_t 3378 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp) 3379 { 3380 uint32_t i_cwnd; 3381 3382 if (bbr->rc_init_win) { 3383 i_cwnd = bbr->rc_init_win * tp->t_maxseg; 3384 } else if (V_tcp_initcwnd_segments) 3385 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg), 3386 max(2 * tp->t_maxseg, 14600)); 3387 else if (V_tcp_do_rfc3390) 3388 i_cwnd = min(4 * tp->t_maxseg, 3389 max(2 * tp->t_maxseg, 4380)); 3390 else { 3391 /* Per RFC5681 Section 3.1 */ 3392 if (tp->t_maxseg > 2190) 3393 i_cwnd = 2 * tp->t_maxseg; 3394 else if (tp->t_maxseg > 1095) 3395 i_cwnd = 3 * tp->t_maxseg; 3396 else 3397 i_cwnd = 4 * tp->t_maxseg; 3398 } 3399 return (i_cwnd); 3400 } 3401 3402 /* 3403 * Given a specified gain, return the target 3404 * cwnd based on that gain. 3405 */ 3406 static uint32_t 3407 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw) 3408 { 3409 uint64_t bdp, rtt; 3410 uint32_t cwnd; 3411 3412 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) || 3413 (bbr_get_full_bw(bbr) == 0)) { 3414 /* No measurements yet */ 3415 return (bbr_initial_cwnd(bbr, bbr->rc_tp)); 3416 } 3417 /* 3418 * Get bytes per RTT needed (rttProp is normally in 3419 * bbr_cwndtarget_rtt_touse) 3420 */ 3421 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse); 3422 /* Get the bdp from the two values */ 3423 bdp = bbr_get_bw_delay_prod(rtt, bw); 3424 /* Now apply the gain */ 3425 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT)); 3426 3427 return (cwnd); 3428 } 3429 3430 static uint32_t 3431 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain) 3432 { 3433 uint32_t cwnd, mss; 3434 3435 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 3436 /* Get the base cwnd with gain rounded to a mss */ 3437 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss); 3438 /* 3439 * Add in N (2 default since we do not have a 3440 * fq layer to trap packets in) quanta's per the I-D 3441 * section 4.2.3.2 quanta adjust. 3442 */ 3443 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs); 3444 if (bbr->rc_use_google) { 3445 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3446 (bbr_state_val(bbr) == BBR_SUB_GAIN)) { 3447 /* 3448 * The linux implementation adds 3449 * an extra 2 x mss in gain cycle which 3450 * is documented no-where except in the code. 3451 * so we add more for Neal undocumented feature 3452 */ 3453 cwnd += 2 * mss; 3454 } 3455 if ((cwnd / mss) & 0x1) { 3456 /* Round up for odd num mss */ 3457 cwnd += mss; 3458 } 3459 } 3460 /* Are we below the min cwnd? */ 3461 if (cwnd < get_min_cwnd(bbr)) 3462 return (get_min_cwnd(bbr)); 3463 return (cwnd); 3464 } 3465 3466 static uint16_t 3467 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain) 3468 { 3469 if (gain < 1) 3470 gain = 1; 3471 return (gain); 3472 } 3473 3474 static uint32_t 3475 bbr_get_header_oh(struct tcp_bbr *bbr) 3476 { 3477 int seg_oh; 3478 3479 seg_oh = 0; 3480 if (bbr->r_ctl.rc_inc_tcp_oh) { 3481 /* Do we include TCP overhead? */ 3482 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr)); 3483 } 3484 if (bbr->r_ctl.rc_inc_ip_oh) { 3485 /* Do we include IP overhead? */ 3486 #ifdef INET6 3487 if (bbr->r_is_v6) { 3488 seg_oh += sizeof(struct ip6_hdr); 3489 } else 3490 #endif 3491 { 3492 3493 #ifdef INET 3494 seg_oh += sizeof(struct ip); 3495 #endif 3496 } 3497 } 3498 if (bbr->r_ctl.rc_inc_enet_oh) { 3499 /* Do we include the ethernet overhead? */ 3500 seg_oh += sizeof(struct ether_header); 3501 } 3502 return(seg_oh); 3503 } 3504 3505 static uint32_t 3506 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw) 3507 { 3508 uint64_t divor, res, tim; 3509 3510 if (useconds_time == 0) 3511 return (0); 3512 gain = bbr_gain_adjust(bbr, gain); 3513 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT; 3514 tim = useconds_time; 3515 res = (tim * bw * gain) / divor; 3516 if (res == 0) 3517 res = 1; 3518 return ((uint32_t)res); 3519 } 3520 3521 /* 3522 * Given a gain and a length return the delay in useconds that 3523 * should be used to evenly space out packets 3524 * on the connection (based on the gain factor). 3525 */ 3526 static uint32_t 3527 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog) 3528 { 3529 uint64_t bw, lentim, res; 3530 uint32_t usecs, srtt, over = 0; 3531 uint32_t seg_oh, num_segs, maxseg; 3532 3533 if (len == 0) 3534 return (0); 3535 3536 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 3537 num_segs = (len + maxseg - 1) / maxseg; 3538 if (bbr->rc_use_google == 0) { 3539 seg_oh = bbr_get_header_oh(bbr); 3540 len += (num_segs * seg_oh); 3541 } 3542 gain = bbr_gain_adjust(bbr, gain); 3543 bw = bbr_get_bw(bbr); 3544 if (bbr->rc_use_google) { 3545 uint64_t cbw; 3546 3547 /* 3548 * Reduce the b/w by the google discount 3549 * factor 10 = 1%. 3550 */ 3551 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount); 3552 cbw /= (uint64_t)1000; 3553 /* We don't apply a discount if it results in 0 */ 3554 if (cbw > 0) 3555 bw = cbw; 3556 } 3557 lentim = ((uint64_t)len * 3558 (uint64_t)USECS_IN_SECOND * 3559 (uint64_t)BBR_UNIT); 3560 res = lentim / ((uint64_t)gain * bw); 3561 if (res == 0) 3562 res = 1; 3563 usecs = (uint32_t)res; 3564 srtt = bbr_get_rtt(bbr, BBR_SRTT); 3565 if (bbr_hptsi_max_mul && bbr_hptsi_max_div && 3566 (bbr->rc_use_google == 0) && 3567 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) { 3568 /* 3569 * We cannot let the delay be more than 1/2 the srtt time. 3570 * Otherwise we cannot pace out or send properly. 3571 */ 3572 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div; 3573 BBR_STAT_INC(bbr_hpts_min_time); 3574 } 3575 if (!nolog) 3576 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1); 3577 return (usecs); 3578 } 3579 3580 static void 3581 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack, 3582 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses) 3583 { 3584 uint64_t bw; 3585 uint32_t cwnd, target_cwnd, saved_bytes, maxseg; 3586 int32_t meth; 3587 3588 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3589 3590 #ifdef STATS 3591 if ((tp->t_flags & TF_GPUTINPROG) && 3592 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 3593 /* 3594 * Strech acks and compressed acks will cause this to 3595 * oscillate but we are doing it the same way as the main 3596 * stack so it will be compariable (though possibly not 3597 * ideal). 3598 */ 3599 int32_t cgput; 3600 int64_t gput, time_stamp; 3601 3602 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8; 3603 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000)); 3604 cgput = gput / time_stamp; 3605 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 3606 cgput); 3607 if (tp->t_stats_gput_prev > 0) 3608 stats_voi_update_abs_s32(tp->t_stats, 3609 VOI_TCP_GPUT_ND, 3610 ((gput - tp->t_stats_gput_prev) * 100) / 3611 tp->t_stats_gput_prev); 3612 tp->t_flags &= ~TF_GPUTINPROG; 3613 tp->t_stats_gput_prev = cgput; 3614 } 3615 #endif 3616 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3617 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 3618 /* We don't change anything in probe-rtt */ 3619 return; 3620 } 3621 maxseg = tp->t_maxseg - bbr->rc_last_options; 3622 saved_bytes = bytes_this_ack; 3623 bytes_this_ack += sack_changed; 3624 if (bytes_this_ack > prev_acked) { 3625 bytes_this_ack -= prev_acked; 3626 /* 3627 * A byte ack'd gives us a full mss 3628 * to be like linux i.e. they count packets. 3629 */ 3630 if ((bytes_this_ack < maxseg) && bbr->rc_use_google) 3631 bytes_this_ack = maxseg; 3632 } else { 3633 /* Unlikely */ 3634 bytes_this_ack = 0; 3635 } 3636 cwnd = tp->snd_cwnd; 3637 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3638 if (bw) 3639 target_cwnd = bbr_get_target_cwnd(bbr, 3640 bw, 3641 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain); 3642 else 3643 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp); 3644 if (IN_RECOVERY(tp->t_flags) && 3645 (bbr->bbr_prev_in_rec == 0)) { 3646 /* 3647 * We are entering recovery and 3648 * thus packet conservation. 3649 */ 3650 bbr->pkt_conservation = 1; 3651 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime; 3652 cwnd = ctf_flight_size(tp, 3653 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3654 bytes_this_ack; 3655 } 3656 if (IN_RECOVERY(tp->t_flags)) { 3657 uint32_t flight; 3658 3659 bbr->bbr_prev_in_rec = 1; 3660 if (cwnd > losses) { 3661 cwnd -= losses; 3662 if (cwnd < maxseg) 3663 cwnd = maxseg; 3664 } else 3665 cwnd = maxseg; 3666 flight = ctf_flight_size(tp, 3667 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3668 bbr_log_type_cwndupd(bbr, flight, 0, 3669 losses, 10, 0, 0, line); 3670 if (bbr->pkt_conservation) { 3671 uint32_t time_in; 3672 3673 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start)) 3674 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start; 3675 else 3676 time_in = 0; 3677 3678 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 3679 /* Clear packet conservation after an rttProp */ 3680 bbr->pkt_conservation = 0; 3681 } else { 3682 if ((flight + bytes_this_ack) > cwnd) 3683 cwnd = flight + bytes_this_ack; 3684 if (cwnd < get_min_cwnd(bbr)) 3685 cwnd = get_min_cwnd(bbr); 3686 tp->snd_cwnd = cwnd; 3687 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, 3688 prev_acked, 1, target_cwnd, th->th_ack, line); 3689 return; 3690 } 3691 } 3692 } else 3693 bbr->bbr_prev_in_rec = 0; 3694 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) { 3695 bbr->r_ctl.restrict_growth--; 3696 if (bytes_this_ack > maxseg) 3697 bytes_this_ack = maxseg; 3698 } 3699 if (bbr->rc_filled_pipe) { 3700 /* 3701 * Here we have exited startup and filled the pipe. We will 3702 * thus allow the cwnd to shrink to the target. We hit here 3703 * mostly. 3704 */ 3705 uint32_t s_cwnd; 3706 3707 meth = 2; 3708 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd); 3709 if (s_cwnd > cwnd) 3710 cwnd = s_cwnd; 3711 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing) 3712 cwnd = s_cwnd; 3713 } else { 3714 /* 3715 * Here we are still in startup, we increase cwnd by what 3716 * has been acked. 3717 */ 3718 if ((cwnd < target_cwnd) || 3719 (bbr->rc_past_init_win == 0)) { 3720 meth = 3; 3721 cwnd += bytes_this_ack; 3722 } else { 3723 /* 3724 * Method 4 means we are at target so no gain in 3725 * startup and past the initial window. 3726 */ 3727 meth = 4; 3728 } 3729 } 3730 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr)); 3731 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line); 3732 } 3733 3734 static void 3735 tcp_bbr_partialack(struct tcpcb *tp) 3736 { 3737 struct tcp_bbr *bbr; 3738 3739 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3740 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3741 if (ctf_flight_size(tp, 3742 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 3743 tp->snd_cwnd) { 3744 bbr->r_wanted_output = 1; 3745 } 3746 } 3747 3748 static void 3749 bbr_post_recovery(struct tcpcb *tp) 3750 { 3751 struct tcp_bbr *bbr; 3752 uint32_t flight; 3753 3754 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3755 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3756 /* 3757 * Here we just exit recovery. 3758 */ 3759 EXIT_RECOVERY(tp->t_flags); 3760 /* Lock in our b/w reduction for the specified number of pkt-epochs */ 3761 bbr->r_recovery_bw = 0; 3762 tp->snd_recover = tp->snd_una; 3763 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3764 bbr->pkt_conservation = 0; 3765 if (bbr->rc_use_google == 0) { 3766 /* 3767 * For non-google mode lets 3768 * go ahead and make sure we clear 3769 * the recovery state so if we 3770 * bounce back in to recovery we 3771 * will do PC. 3772 */ 3773 bbr->bbr_prev_in_rec = 0; 3774 } 3775 bbr_log_type_exit_rec(bbr); 3776 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3777 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3778 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__); 3779 } else { 3780 /* For probe-rtt case lets fix up its saved_cwnd */ 3781 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) { 3782 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent; 3783 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__); 3784 } 3785 } 3786 flight = ctf_flight_size(tp, 3787 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3788 if ((bbr->rc_use_google == 0) && 3789 bbr_do_red) { 3790 uint64_t val, lr2use; 3791 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd; 3792 uint32_t *cwnd_p; 3793 3794 if (bbr_get_rtt(bbr, BBR_SRTT)) { 3795 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000); 3796 val /= bbr_get_rtt(bbr, BBR_SRTT); 3797 ratio = (uint32_t)val; 3798 } else 3799 ratio = 1000; 3800 3801 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, 3802 bbr->r_ctl.recovery_lr, 21, 3803 ratio, 3804 bbr->r_ctl.rc_red_cwnd_pe, 3805 __LINE__); 3806 if ((ratio < bbr_do_red) || (bbr_do_red == 0)) 3807 goto done; 3808 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3809 bbr_prtt_slam_cwnd) || 3810 (bbr_sub_drain_slam_cwnd && 3811 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3812 bbr->rc_hit_state_1 && 3813 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) || 3814 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) && 3815 bbr_slam_cwnd_in_main_drain)) { 3816 /* 3817 * Here we must poke at the saved cwnd 3818 * as well as the cwnd. 3819 */ 3820 cwnd = bbr->r_ctl.rc_saved_cwnd; 3821 cwnd_p = &bbr->r_ctl.rc_saved_cwnd; 3822 } else { 3823 cwnd = tp->snd_cwnd; 3824 cwnd_p = &tp->snd_cwnd; 3825 } 3826 maxseg = tp->t_maxseg - bbr->rc_last_options; 3827 /* Add the overall lr with the recovery lr */ 3828 if (bbr->r_ctl.rc_lost == 0) 3829 lr2use = 0; 3830 else if (bbr->r_ctl.rc_delivered == 0) 3831 lr2use = 1000; 3832 else { 3833 lr2use = (uint64_t)bbr->r_ctl.rc_lost * (uint64_t)1000; 3834 lr2use /= bbr->r_ctl.rc_delivered; 3835 } 3836 lr2use += bbr->r_ctl.recovery_lr; 3837 acks_inflight = (flight / (maxseg * 2)); 3838 if (bbr_red_scale) { 3839 lr2use *= bbr_get_rtt(bbr, BBR_SRTT); 3840 lr2use /= bbr_red_scale; 3841 if ((bbr_red_growth_restrict) && 3842 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1)) 3843 bbr->r_ctl.restrict_growth += acks_inflight; 3844 } 3845 if (lr2use) { 3846 val = (uint64_t)cwnd * lr2use; 3847 val /= 1000; 3848 if (cwnd > val) 3849 newcwnd = roundup((cwnd - val), maxseg); 3850 else 3851 newcwnd = maxseg; 3852 } else { 3853 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul; 3854 val /= (uint64_t)bbr_red_div; 3855 newcwnd = roundup((uint32_t)val, maxseg); 3856 } 3857 /* with standard delayed acks how many acks can I expect? */ 3858 if (bbr_drop_limit == 0) { 3859 /* 3860 * Anticpate how much we will 3861 * raise the cwnd based on the acks. 3862 */ 3863 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) { 3864 /* We do enforce the min (with the acks) */ 3865 newcwnd = (get_min_cwnd(bbr) - acks_inflight); 3866 } 3867 } else { 3868 /* 3869 * A strict drop limit of N is inplace 3870 */ 3871 if (newcwnd < (bbr_drop_limit * maxseg)) { 3872 newcwnd = bbr_drop_limit * maxseg; 3873 } 3874 } 3875 /* For the next N acks do we restrict the growth */ 3876 *cwnd_p = newcwnd; 3877 if (tp->snd_cwnd > newcwnd) 3878 tp->snd_cwnd = newcwnd; 3879 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22, 3880 (uint32_t)lr2use, 3881 bbr_get_rtt(bbr, BBR_SRTT), __LINE__); 3882 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch; 3883 } 3884 done: 3885 bbr->r_ctl.recovery_lr = 0; 3886 if (flight <= tp->snd_cwnd) { 3887 bbr->r_wanted_output = 1; 3888 } 3889 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3890 } 3891 3892 static void 3893 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts) 3894 { 3895 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3896 /* Limit the drop in b/w to 1/2 our current filter. */ 3897 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate) 3898 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate; 3899 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2)) 3900 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2; 3901 tcp_bbr_tso_size_check(bbr, cts); 3902 } 3903 3904 static void 3905 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm) 3906 { 3907 struct tcp_bbr *bbr; 3908 3909 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3910 #ifdef STATS 3911 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 3912 #endif 3913 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3914 switch (type) { 3915 case CC_NDUPACK: 3916 if (!IN_RECOVERY(tp->t_flags)) { 3917 tp->snd_recover = tp->snd_max; 3918 /* Start a new epoch */ 3919 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 3920 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) { 3921 /* 3922 * Move forward the lt epoch 3923 * so it won't count the truncated 3924 * epoch. 3925 */ 3926 bbr->r_ctl.rc_lt_epoch++; 3927 } 3928 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 3929 /* 3930 * Just like the policer detection code 3931 * if we are in startup we must push 3932 * forward the last startup epoch 3933 * to hide the truncated PE. 3934 */ 3935 bbr->r_ctl.rc_bbr_last_startup_epoch++; 3936 } 3937 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd; 3938 ENTER_RECOVERY(tp->t_flags); 3939 bbr->rc_tlp_rtx_out = 0; 3940 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate; 3941 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3942 if (tcp_in_hpts(bbr->rc_tp) && 3943 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) { 3944 /* 3945 * When we enter recovery, we need to restart 3946 * any timers. This may mean we gain an agg 3947 * early, which will be made up for at the last 3948 * rxt out. 3949 */ 3950 bbr->rc_timer_first = 1; 3951 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 3952 } 3953 /* 3954 * Calculate a new cwnd based on to the current 3955 * delivery rate with no gain. We get the bdp 3956 * without gaining it up like we normally would and 3957 * we use the last cur_del_rate. 3958 */ 3959 if ((bbr->rc_use_google == 0) && 3960 (bbr->r_ctl.bbr_rttprobe_gain_val || 3961 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) { 3962 tp->snd_cwnd = ctf_flight_size(tp, 3963 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3964 (tp->t_maxseg - bbr->rc_last_options); 3965 if (tp->snd_cwnd < get_min_cwnd(bbr)) { 3966 /* We always gate to min cwnd */ 3967 tp->snd_cwnd = get_min_cwnd(bbr); 3968 } 3969 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__); 3970 } 3971 bbr_log_type_enter_rec(bbr, rsm->r_start); 3972 } 3973 break; 3974 case CC_RTO_ERR: 3975 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 3976 /* RTO was unnecessary, so reset everything. */ 3977 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime); 3978 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3979 tp->snd_cwnd = tp->snd_cwnd_prev; 3980 tp->snd_ssthresh = tp->snd_ssthresh_prev; 3981 tp->snd_recover = tp->snd_recover_prev; 3982 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3983 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__); 3984 } 3985 tp->t_badrxtwin = 0; 3986 break; 3987 } 3988 } 3989 3990 /* 3991 * Indicate whether this ack should be delayed. We can delay the ack if 3992 * following conditions are met: 3993 * - There is no delayed ack timer in progress. 3994 * - Our last ack wasn't a 0-sized window. We never want to delay 3995 * the ack that opens up a 0-sized window. 3996 * - LRO wasn't used for this segment. We make sure by checking that the 3997 * segment size is not larger than the MSS. 3998 * - Delayed acks are enabled or this is a half-synchronized T/TCP 3999 * connection. 4000 * - The data being acked is less than a full segment (a stretch ack 4001 * of more than a segment we should ack. 4002 * - nsegs is 1 (if its more than that we received more than 1 ack). 4003 */ 4004 #define DELAY_ACK(tp, bbr, nsegs) \ 4005 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4006 ((tp->t_flags & TF_DELACK) == 0) && \ 4007 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \ 4008 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 4009 4010 /* 4011 * Return the lowest RSM in the map of 4012 * packets still in flight that is not acked. 4013 * This should normally find on the first one 4014 * since we remove packets from the send 4015 * map after they are marked ACKED. 4016 */ 4017 static struct bbr_sendmap * 4018 bbr_find_lowest_rsm(struct tcp_bbr *bbr) 4019 { 4020 struct bbr_sendmap *rsm; 4021 4022 /* 4023 * Walk the time-order transmitted list looking for an rsm that is 4024 * not acked. This will be the one that was sent the longest time 4025 * ago that is still outstanding. 4026 */ 4027 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) { 4028 if (rsm->r_flags & BBR_ACKED) { 4029 continue; 4030 } 4031 goto finish; 4032 } 4033 finish: 4034 return (rsm); 4035 } 4036 4037 static struct bbr_sendmap * 4038 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 4039 { 4040 struct bbr_sendmap *prsm; 4041 4042 /* 4043 * Walk the sequence order list backward until we hit and arrive at 4044 * the highest seq not acked. In theory when this is called it 4045 * should be the last segment (which it was not). 4046 */ 4047 prsm = rsm; 4048 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4049 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) { 4050 continue; 4051 } 4052 return (prsm); 4053 } 4054 return (NULL); 4055 } 4056 4057 /* 4058 * Returns to the caller the number of microseconds that 4059 * the packet can be outstanding before we think we 4060 * should have had an ack returned. 4061 */ 4062 static uint32_t 4063 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm) 4064 { 4065 /* 4066 * lro is the flag we use to determine if we have seen reordering. 4067 * If it gets set we have seen reordering. The reorder logic either 4068 * works in one of two ways: 4069 * 4070 * If reorder-fade is configured, then we track the last time we saw 4071 * re-ordering occur. If we reach the point where enough time as 4072 * passed we no longer consider reordering has occuring. 4073 * 4074 * Or if reorder-face is 0, then once we see reordering we consider 4075 * the connection to alway be subject to reordering and just set lro 4076 * to 1. 4077 * 4078 * In the end if lro is non-zero we add the extra time for 4079 * reordering in. 4080 */ 4081 int32_t lro; 4082 uint32_t thresh, t_rxtcur; 4083 4084 if (srtt == 0) 4085 srtt = 1; 4086 if (bbr->r_ctl.rc_reorder_ts) { 4087 if (bbr->r_ctl.rc_reorder_fade) { 4088 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) { 4089 lro = cts - bbr->r_ctl.rc_reorder_ts; 4090 if (lro == 0) { 4091 /* 4092 * No time as passed since the last 4093 * reorder, mark it as reordering. 4094 */ 4095 lro = 1; 4096 } 4097 } else { 4098 /* Negative time? */ 4099 lro = 0; 4100 } 4101 if (lro > bbr->r_ctl.rc_reorder_fade) { 4102 /* Turn off reordering seen too */ 4103 bbr->r_ctl.rc_reorder_ts = 0; 4104 lro = 0; 4105 } 4106 } else { 4107 /* Reodering does not fade */ 4108 lro = 1; 4109 } 4110 } else { 4111 lro = 0; 4112 } 4113 thresh = srtt + bbr->r_ctl.rc_pkt_delay; 4114 if (lro) { 4115 /* It must be set, if not you get 1/4 rtt */ 4116 if (bbr->r_ctl.rc_reorder_shift) 4117 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift); 4118 else 4119 thresh += (srtt >> 2); 4120 } else { 4121 thresh += 1000; 4122 } 4123 /* We don't let the rack timeout be above a RTO */ 4124 if ((bbr->rc_tp)->t_srtt == 0) 4125 t_rxtcur = BBR_INITIAL_RTO; 4126 else 4127 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 4128 if (thresh > t_rxtcur) { 4129 thresh = t_rxtcur; 4130 } 4131 /* And we don't want it above the RTO max either */ 4132 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4133 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4134 } 4135 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK); 4136 return (thresh); 4137 } 4138 4139 /* 4140 * Return to the caller the amount of time in mico-seconds 4141 * that should be used for the TLP timer from the last 4142 * send time of this packet. 4143 */ 4144 static uint32_t 4145 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 4146 struct bbr_sendmap *rsm, uint32_t srtt, 4147 uint32_t cts) 4148 { 4149 uint32_t thresh, len, maxseg, t_rxtcur; 4150 struct bbr_sendmap *prsm; 4151 4152 if (srtt == 0) 4153 srtt = 1; 4154 if (bbr->rc_tlp_threshold) 4155 thresh = srtt + (srtt / bbr->rc_tlp_threshold); 4156 else 4157 thresh = (srtt * 2); 4158 maxseg = tp->t_maxseg - bbr->rc_last_options; 4159 /* Get the previous sent packet, if any */ 4160 len = rsm->r_end - rsm->r_start; 4161 4162 /* 2.1 behavior */ 4163 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext); 4164 if (prsm && (len <= maxseg)) { 4165 /* 4166 * Two packets outstanding, thresh should be (2*srtt) + 4167 * possible inter-packet delay (if any). 4168 */ 4169 uint32_t inter_gap = 0; 4170 int idx, nidx; 4171 4172 idx = rsm->r_rtr_cnt - 1; 4173 nidx = prsm->r_rtr_cnt - 1; 4174 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 4175 /* Yes it was sent later (or at the same time) */ 4176 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 4177 } 4178 thresh += inter_gap; 4179 } else if (len <= maxseg) { 4180 /* 4181 * Possibly compensate for delayed-ack. 4182 */ 4183 uint32_t alt_thresh; 4184 4185 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time; 4186 if (alt_thresh > thresh) 4187 thresh = alt_thresh; 4188 } 4189 /* Not above the current RTO */ 4190 if (tp->t_srtt == 0) 4191 t_rxtcur = BBR_INITIAL_RTO; 4192 else 4193 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur); 4194 4195 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP); 4196 /* Not above an RTO */ 4197 if (thresh > t_rxtcur) { 4198 thresh = t_rxtcur; 4199 } 4200 /* Not above a RTO max */ 4201 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4202 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4203 } 4204 /* And now apply the user TLP min */ 4205 if (thresh < bbr_tlp_min) { 4206 thresh = bbr_tlp_min; 4207 } 4208 return (thresh); 4209 } 4210 4211 /* 4212 * Return one of three RTTs to use (in microseconds). 4213 */ 4214 static __inline uint32_t 4215 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type) 4216 { 4217 uint32_t f_rtt; 4218 uint32_t srtt; 4219 4220 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 4221 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) { 4222 /* We have no rtt at all */ 4223 if (bbr->rc_tp->t_srtt == 0) 4224 f_rtt = BBR_INITIAL_RTO; 4225 else 4226 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4227 /* 4228 * Since we don't know how good the rtt is apply a 4229 * delayed-ack min 4230 */ 4231 if (f_rtt < bbr_delayed_ack_time) { 4232 f_rtt = bbr_delayed_ack_time; 4233 } 4234 } 4235 /* Take the filter version or last measured pkt-rtt */ 4236 if (rtt_type == BBR_RTT_PROP) { 4237 srtt = f_rtt; 4238 } else if (rtt_type == BBR_RTT_PKTRTT) { 4239 if (bbr->r_ctl.rc_pkt_epoch_rtt) { 4240 srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 4241 } else { 4242 /* No pkt rtt yet */ 4243 srtt = f_rtt; 4244 } 4245 } else if (rtt_type == BBR_RTT_RACK) { 4246 srtt = bbr->r_ctl.rc_last_rtt; 4247 /* We need to add in any internal delay for our timer */ 4248 if (bbr->rc_ack_was_delayed) 4249 srtt += bbr->r_ctl.rc_ack_hdwr_delay; 4250 } else if (rtt_type == BBR_SRTT) { 4251 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4252 } else { 4253 /* TSNH */ 4254 srtt = f_rtt; 4255 #ifdef BBR_INVARIANTS 4256 panic("Unknown rtt request type %d", rtt_type); 4257 #endif 4258 } 4259 return (srtt); 4260 } 4261 4262 static int 4263 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts) 4264 { 4265 uint32_t thresh; 4266 4267 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK), 4268 cts, rsm); 4269 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) { 4270 /* It is lost (past time) */ 4271 return (1); 4272 } 4273 return (0); 4274 } 4275 4276 /* 4277 * Return a sendmap if we need to retransmit something. 4278 */ 4279 static struct bbr_sendmap * 4280 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4281 { 4282 /* 4283 * Check to see that we don't need to fall into recovery. We will 4284 * need to do so if our oldest transmit is past the time we should 4285 * have had an ack. 4286 */ 4287 4288 struct bbr_sendmap *rsm; 4289 int32_t idx; 4290 4291 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) { 4292 /* Nothing outstanding that we know of */ 4293 return (NULL); 4294 } 4295 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 4296 if (rsm == NULL) { 4297 /* Nothing in the transmit map */ 4298 return (NULL); 4299 } 4300 if (tp->t_flags & TF_SENTFIN) { 4301 /* Fin restricted, don't find anything once a fin is sent */ 4302 return (NULL); 4303 } 4304 if (rsm->r_flags & BBR_ACKED) { 4305 /* 4306 * Ok the first one is acked (this really should not happen 4307 * since we remove the from the tmap once they are acked) 4308 */ 4309 rsm = bbr_find_lowest_rsm(bbr); 4310 if (rsm == NULL) 4311 return (NULL); 4312 } 4313 idx = rsm->r_rtr_cnt - 1; 4314 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) { 4315 /* Send timestamp is the same or less? can't be ready */ 4316 return (NULL); 4317 } 4318 /* Get our RTT time */ 4319 if (bbr_is_lost(bbr, rsm, cts) && 4320 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 4321 (rsm->r_flags & BBR_SACK_PASSED))) { 4322 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4323 rsm->r_flags |= BBR_MARKED_LOST; 4324 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4325 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4326 } 4327 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm); 4328 #ifdef BBR_INVARIANTS 4329 if ((rsm->r_end - rsm->r_start) == 0) 4330 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm); 4331 #endif 4332 return (rsm); 4333 } 4334 return (NULL); 4335 } 4336 4337 /* 4338 * RACK Timer, here we simply do logging and house keeping. 4339 * the normal bbr_output_wtime() function will call the 4340 * appropriate thing to check if we need to do a RACK retransmit. 4341 * We return 1, saying don't proceed with bbr_output_wtime only 4342 * when all timers have been stopped (destroyed PCB?). 4343 */ 4344 static int 4345 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4346 { 4347 /* 4348 * This timer simply provides an internal trigger to send out data. 4349 * The check_recovery_mode call will see if there are needed 4350 * retransmissions, if so we will enter fast-recovery. The output 4351 * call may or may not do the same thing depending on sysctl 4352 * settings. 4353 */ 4354 uint32_t lost; 4355 4356 if (bbr->rc_all_timers_stopped) { 4357 return (1); 4358 } 4359 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4360 /* Its not time yet */ 4361 return (0); 4362 } 4363 BBR_STAT_INC(bbr_to_tot); 4364 lost = bbr->r_ctl.rc_lost; 4365 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4366 bbr_set_state(tp, bbr, 0); 4367 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK); 4368 if (bbr->r_ctl.rc_resend == NULL) { 4369 /* Lets do the check here */ 4370 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 4371 } 4372 if (bbr_policer_call_from_rack_to) 4373 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4374 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 4375 return (0); 4376 } 4377 4378 static __inline void 4379 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start) 4380 { 4381 int idx; 4382 4383 nrsm->r_start = start; 4384 nrsm->r_end = rsm->r_end; 4385 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4386 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed; 4387 nrsm->r_flags = rsm->r_flags; 4388 /* We don't transfer forward the SYN flag */ 4389 nrsm->r_flags &= ~BBR_HAS_SYN; 4390 /* We move forward the FIN flag, not that this should happen */ 4391 rsm->r_flags &= ~BBR_HAS_FIN; 4392 nrsm->r_dupack = rsm->r_dupack; 4393 nrsm->r_rtr_bytes = 0; 4394 nrsm->r_is_gain = rsm->r_is_gain; 4395 nrsm->r_is_drain = rsm->r_is_drain; 4396 nrsm->r_delivered = rsm->r_delivered; 4397 nrsm->r_ts_valid = rsm->r_ts_valid; 4398 nrsm->r_del_ack_ts = rsm->r_del_ack_ts; 4399 nrsm->r_del_time = rsm->r_del_time; 4400 nrsm->r_app_limited = rsm->r_app_limited; 4401 nrsm->r_first_sent_time = rsm->r_first_sent_time; 4402 nrsm->r_flight_at_send = rsm->r_flight_at_send; 4403 /* We split a piece the lower section looses any just_ret flag. */ 4404 nrsm->r_bbr_state = rsm->r_bbr_state; 4405 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4406 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4407 } 4408 rsm->r_end = nrsm->r_start; 4409 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 4410 idx /= 8; 4411 /* Check if we got too small */ 4412 if ((rsm->r_is_smallmap == 0) && 4413 ((rsm->r_end - rsm->r_start) <= idx)) { 4414 bbr->r_ctl.rc_num_small_maps_alloced++; 4415 rsm->r_is_smallmap = 1; 4416 } 4417 /* Check the new one as well */ 4418 if ((nrsm->r_end - nrsm->r_start) <= idx) { 4419 bbr->r_ctl.rc_num_small_maps_alloced++; 4420 nrsm->r_is_smallmap = 1; 4421 } 4422 } 4423 4424 static int 4425 bbr_sack_mergable(struct bbr_sendmap *at, 4426 uint32_t start, uint32_t end) 4427 { 4428 /* 4429 * Given a sack block defined by 4430 * start and end, and a current position 4431 * at. Return 1 if either side of at 4432 * would show that the block is mergable 4433 * to that side. A block to be mergable 4434 * must have overlap with the start/end 4435 * and be in the SACK'd state. 4436 */ 4437 struct bbr_sendmap *l_rsm; 4438 struct bbr_sendmap *r_rsm; 4439 4440 /* first get the either side blocks */ 4441 l_rsm = TAILQ_PREV(at, bbr_head, r_next); 4442 r_rsm = TAILQ_NEXT(at, r_next); 4443 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) { 4444 /* Potentially mergeable */ 4445 if ((l_rsm->r_end == start) || 4446 (SEQ_LT(start, l_rsm->r_end) && 4447 SEQ_GT(end, l_rsm->r_end))) { 4448 /* 4449 * map blk |------| 4450 * sack blk |------| 4451 * <or> 4452 * map blk |------| 4453 * sack blk |------| 4454 */ 4455 return (1); 4456 } 4457 } 4458 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) { 4459 /* Potentially mergeable */ 4460 if ((r_rsm->r_start == end) || 4461 (SEQ_LT(start, r_rsm->r_start) && 4462 SEQ_GT(end, r_rsm->r_start))) { 4463 /* 4464 * map blk |---------| 4465 * sack blk |----| 4466 * <or> 4467 * map blk |---------| 4468 * sack blk |-------| 4469 */ 4470 return (1); 4471 } 4472 } 4473 return (0); 4474 } 4475 4476 static struct bbr_sendmap * 4477 bbr_merge_rsm(struct tcp_bbr *bbr, 4478 struct bbr_sendmap *l_rsm, 4479 struct bbr_sendmap *r_rsm) 4480 { 4481 /* 4482 * We are merging two ack'd RSM's, 4483 * the l_rsm is on the left (lower seq 4484 * values) and the r_rsm is on the right 4485 * (higher seq value). The simplest way 4486 * to merge these is to move the right 4487 * one into the left. I don't think there 4488 * is any reason we need to try to find 4489 * the oldest (or last oldest retransmitted). 4490 */ 4491 l_rsm->r_end = r_rsm->r_end; 4492 if (l_rsm->r_dupack < r_rsm->r_dupack) 4493 l_rsm->r_dupack = r_rsm->r_dupack; 4494 if (r_rsm->r_rtr_bytes) 4495 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 4496 if (r_rsm->r_in_tmap) { 4497 /* This really should not happen */ 4498 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext); 4499 } 4500 if (r_rsm->r_app_limited) 4501 l_rsm->r_app_limited = r_rsm->r_app_limited; 4502 /* Now the flags */ 4503 if (r_rsm->r_flags & BBR_HAS_FIN) 4504 l_rsm->r_flags |= BBR_HAS_FIN; 4505 if (r_rsm->r_flags & BBR_TLP) 4506 l_rsm->r_flags |= BBR_TLP; 4507 if (r_rsm->r_flags & BBR_RWND_COLLAPSED) 4508 l_rsm->r_flags |= BBR_RWND_COLLAPSED; 4509 if (r_rsm->r_flags & BBR_MARKED_LOST) { 4510 /* This really should not happen */ 4511 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start; 4512 } 4513 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next); 4514 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 4515 /* Transfer the split limit to the map we free */ 4516 r_rsm->r_limit_type = l_rsm->r_limit_type; 4517 l_rsm->r_limit_type = 0; 4518 } 4519 bbr_free(bbr, r_rsm); 4520 return(l_rsm); 4521 } 4522 4523 /* 4524 * TLP Timer, here we simply setup what segment we want to 4525 * have the TLP expire on, the normal bbr_output_wtime() will then 4526 * send it out. 4527 * 4528 * We return 1, saying don't proceed with bbr_output_wtime only 4529 * when all timers have been stopped (destroyed PCB?). 4530 */ 4531 static int 4532 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4533 { 4534 /* 4535 * Tail Loss Probe. 4536 */ 4537 struct bbr_sendmap *rsm = NULL; 4538 struct socket *so; 4539 uint32_t amm; 4540 uint32_t out, avail; 4541 uint32_t maxseg; 4542 int collapsed_win = 0; 4543 4544 if (bbr->rc_all_timers_stopped) { 4545 return (1); 4546 } 4547 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4548 /* Its not time yet */ 4549 return (0); 4550 } 4551 if (ctf_progress_timeout_check(tp, true)) { 4552 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4553 return (-ETIMEDOUT); /* tcp_drop() */ 4554 } 4555 /* Did we somehow get into persists? */ 4556 if (bbr->rc_in_persist) { 4557 return (0); 4558 } 4559 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4560 bbr_set_state(tp, bbr, 0); 4561 BBR_STAT_INC(bbr_tlp_tot); 4562 maxseg = tp->t_maxseg - bbr->rc_last_options; 4563 /* 4564 * A TLP timer has expired. We have been idle for 2 rtts. So we now 4565 * need to figure out how to force a full MSS segment out. 4566 */ 4567 so = tptosocket(tp); 4568 avail = sbavail(&so->so_snd); 4569 out = ctf_outstanding(tp); 4570 if (out > tp->snd_wnd) { 4571 /* special case, we need a retransmission */ 4572 collapsed_win = 1; 4573 goto need_retran; 4574 } 4575 if (avail > out) { 4576 /* New data is available */ 4577 amm = avail - out; 4578 if (amm > maxseg) { 4579 amm = maxseg; 4580 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 4581 /* not enough to fill a MTU and no-delay is off */ 4582 goto need_retran; 4583 } 4584 /* Set the send-new override */ 4585 if ((out + amm) <= tp->snd_wnd) { 4586 bbr->rc_tlp_new_data = 1; 4587 } else { 4588 goto need_retran; 4589 } 4590 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4591 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max; 4592 bbr->r_ctl.rc_tlp_send = NULL; 4593 /* cap any slots */ 4594 BBR_STAT_INC(bbr_tlp_newdata); 4595 goto send; 4596 } 4597 need_retran: 4598 /* 4599 * Ok we need to arrange the last un-acked segment to be re-sent, or 4600 * optionally the first un-acked segment. 4601 */ 4602 if (collapsed_win == 0) { 4603 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 4604 if (rsm && (rsm->r_flags & (BBR_ACKED | BBR_HAS_FIN))) { 4605 rsm = bbr_find_high_nonack(bbr, rsm); 4606 } 4607 if (rsm == NULL) { 4608 goto restore; 4609 } 4610 } else { 4611 /* 4612 * We must find the last segment 4613 * that was acceptable by the client. 4614 */ 4615 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4616 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) { 4617 /* Found one */ 4618 break; 4619 } 4620 } 4621 if (rsm == NULL) { 4622 /* None? if so send the first */ 4623 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4624 if (rsm == NULL) 4625 goto restore; 4626 } 4627 } 4628 if ((rsm->r_end - rsm->r_start) > maxseg) { 4629 /* 4630 * We need to split this the last segment in two. 4631 */ 4632 struct bbr_sendmap *nrsm; 4633 4634 nrsm = bbr_alloc_full_limit(bbr); 4635 if (nrsm == NULL) { 4636 /* 4637 * We can't get memory to split, we can either just 4638 * not split it. Or retransmit the whole piece, lets 4639 * do the large send (BTLP :-) ). 4640 */ 4641 goto go_for_it; 4642 } 4643 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg)); 4644 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 4645 if (rsm->r_in_tmap) { 4646 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4647 nrsm->r_in_tmap = 1; 4648 } 4649 rsm->r_flags &= (~BBR_HAS_FIN); 4650 rsm = nrsm; 4651 } 4652 go_for_it: 4653 bbr->r_ctl.rc_tlp_send = rsm; 4654 bbr->rc_tlp_rtx_out = 1; 4655 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) { 4656 bbr->r_ctl.rc_tlp_seg_send_cnt++; 4657 tp->t_rxtshift++; 4658 } else { 4659 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 4660 bbr->r_ctl.rc_tlp_seg_send_cnt = 1; 4661 } 4662 send: 4663 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 4664 /* 4665 * Can't [re]/transmit a segment we have retransmitted the 4666 * max times. We need the retransmit timer to take over. 4667 */ 4668 restore: 4669 bbr->rc_tlp_new_data = 0; 4670 bbr->r_ctl.rc_tlp_send = NULL; 4671 if (rsm) 4672 rsm->r_flags &= ~BBR_TLP; 4673 BBR_STAT_INC(bbr_tlp_retran_fail); 4674 return (0); 4675 } else if (rsm) { 4676 rsm->r_flags |= BBR_TLP; 4677 } 4678 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) && 4679 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) { 4680 /* 4681 * We have retransmitted to many times for TLP. Switch to 4682 * the regular RTO timer 4683 */ 4684 goto restore; 4685 } 4686 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP); 4687 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 4688 return (0); 4689 } 4690 4691 /* 4692 * Delayed ack Timer, here we simply need to setup the 4693 * ACK_NOW flag and remove the DELACK flag. From there 4694 * the output routine will send the ack out. 4695 * 4696 * We only return 1, saying don't proceed, if all timers 4697 * are stopped (destroyed PCB?). 4698 */ 4699 static int 4700 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4701 { 4702 if (bbr->rc_all_timers_stopped) { 4703 return (1); 4704 } 4705 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK); 4706 tp->t_flags &= ~TF_DELACK; 4707 tp->t_flags |= TF_ACKNOW; 4708 KMOD_TCPSTAT_INC(tcps_delack); 4709 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 4710 return (0); 4711 } 4712 4713 /* 4714 * Here we send a KEEP-ALIVE like probe to the 4715 * peer, we do not send data. 4716 * 4717 * We only return 1, saying don't proceed, if all timers 4718 * are stopped (destroyed PCB?). 4719 */ 4720 static int 4721 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4722 { 4723 struct tcptemp *t_template; 4724 int32_t retval = 1; 4725 4726 if (bbr->rc_all_timers_stopped) { 4727 return (1); 4728 } 4729 if (bbr->rc_in_persist == 0) 4730 return (0); 4731 4732 /* 4733 * Persistence timer into zero window. Force a byte to be output, if 4734 * possible. 4735 */ 4736 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST); 4737 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 4738 KMOD_TCPSTAT_INC(tcps_persisttimeo); 4739 /* 4740 * Have we exceeded the user specified progress time? 4741 */ 4742 if (ctf_progress_timeout_check(tp, true)) { 4743 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4744 return (-ETIMEDOUT); /* tcp_drop() */ 4745 } 4746 /* 4747 * Hack: if the peer is dead/unreachable, we do not time out if the 4748 * window is closed. After a full backoff, drop the connection if 4749 * the idle time (no responses to probes) reaches the maximum 4750 * backoff that we would use if retransmitting. 4751 */ 4752 if (tp->t_rxtshift >= V_tcp_retries && 4753 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 4754 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 4755 KMOD_TCPSTAT_INC(tcps_persistdrop); 4756 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4757 return (-ETIMEDOUT); /* tcp_drop() */ 4758 } 4759 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) && 4760 tp->snd_una == tp->snd_max) { 4761 bbr_exit_persist(tp, bbr, cts, __LINE__); 4762 retval = 0; 4763 goto out; 4764 } 4765 /* 4766 * If the user has closed the socket then drop a persisting 4767 * connection after a much reduced timeout. 4768 */ 4769 if (tp->t_state > TCPS_CLOSE_WAIT && 4770 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 4771 KMOD_TCPSTAT_INC(tcps_persistdrop); 4772 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4773 return (-ETIMEDOUT); /* tcp_drop() */ 4774 } 4775 t_template = tcpip_maketemplate(bbr->rc_inp); 4776 if (t_template) { 4777 tcp_respond(tp, t_template->tt_ipgen, 4778 &t_template->tt_t, (struct mbuf *)NULL, 4779 tp->rcv_nxt, tp->snd_una - 1, 0); 4780 /* This sends an ack */ 4781 if (tp->t_flags & TF_DELACK) 4782 tp->t_flags &= ~TF_DELACK; 4783 free(t_template, M_TEMP); 4784 } 4785 if (tp->t_rxtshift < V_tcp_retries) 4786 tp->t_rxtshift++; 4787 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0); 4788 out: 4789 return (retval); 4790 } 4791 4792 /* 4793 * If a keepalive goes off, we had no other timers 4794 * happening. We always return 1 here since this 4795 * routine either drops the connection or sends 4796 * out a segment with respond. 4797 */ 4798 static int 4799 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4800 { 4801 struct tcptemp *t_template; 4802 struct inpcb *inp = tptoinpcb(tp); 4803 4804 if (bbr->rc_all_timers_stopped) { 4805 return (1); 4806 } 4807 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 4808 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP); 4809 /* 4810 * Keep-alive timer went off; send something or drop connection if 4811 * idle for too long. 4812 */ 4813 KMOD_TCPSTAT_INC(tcps_keeptimeo); 4814 if (tp->t_state < TCPS_ESTABLISHED) 4815 goto dropit; 4816 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 4817 tp->t_state <= TCPS_CLOSING) { 4818 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 4819 goto dropit; 4820 /* 4821 * Send a packet designed to force a response if the peer is 4822 * up and reachable: either an ACK if the connection is 4823 * still alive, or an RST if the peer has closed the 4824 * connection due to timeout or reboot. Using sequence 4825 * number tp->snd_una-1 causes the transmitted zero-length 4826 * segment to lie outside the receive window; by the 4827 * protocol spec, this requires the correspondent TCP to 4828 * respond. 4829 */ 4830 KMOD_TCPSTAT_INC(tcps_keepprobe); 4831 t_template = tcpip_maketemplate(inp); 4832 if (t_template) { 4833 tcp_respond(tp, t_template->tt_ipgen, 4834 &t_template->tt_t, (struct mbuf *)NULL, 4835 tp->rcv_nxt, tp->snd_una - 1, 0); 4836 free(t_template, M_TEMP); 4837 } 4838 } 4839 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0); 4840 return (1); 4841 dropit: 4842 KMOD_TCPSTAT_INC(tcps_keepdrops); 4843 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 4844 return (-ETIMEDOUT); /* tcp_drop() */ 4845 } 4846 4847 /* 4848 * Retransmit helper function, clear up all the ack 4849 * flags and take care of important book keeping. 4850 */ 4851 static void 4852 bbr_remxt_tmr(struct tcpcb *tp) 4853 { 4854 /* 4855 * The retransmit timer went off, all sack'd blocks must be 4856 * un-acked. 4857 */ 4858 struct bbr_sendmap *rsm, *trsm = NULL; 4859 struct tcp_bbr *bbr; 4860 uint32_t cts, lost; 4861 4862 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 4863 cts = tcp_get_usecs(&bbr->rc_tv); 4864 lost = bbr->r_ctl.rc_lost; 4865 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4866 bbr_set_state(tp, bbr, 0); 4867 4868 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 4869 if (rsm->r_flags & BBR_ACKED) { 4870 uint32_t old_flags; 4871 4872 rsm->r_dupack = 0; 4873 if (rsm->r_in_tmap == 0) { 4874 /* We must re-add it back to the tlist */ 4875 if (trsm == NULL) { 4876 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 4877 } else { 4878 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext); 4879 } 4880 rsm->r_in_tmap = 1; 4881 } 4882 old_flags = rsm->r_flags; 4883 rsm->r_flags |= BBR_RXT_CLEARED; 4884 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS); 4885 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 4886 } else { 4887 if ((tp->t_state < TCPS_ESTABLISHED) && 4888 (rsm->r_start == tp->snd_una)) { 4889 /* 4890 * Special case for TCP FO. Where 4891 * we sent more data beyond the snd_max. 4892 * We don't mark that as lost and stop here. 4893 */ 4894 break; 4895 } 4896 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4897 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4898 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4899 } 4900 if (bbr_marks_rxt_sack_passed) { 4901 /* 4902 * With this option, we will rack out 4903 * in 1ms increments the rest of the packets. 4904 */ 4905 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST; 4906 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4907 } else { 4908 /* 4909 * With this option we only mark them lost 4910 * and remove all sack'd markings. We will run 4911 * another RXT or a TLP. This will cause 4912 * us to eventually send more based on what 4913 * ack's come in. 4914 */ 4915 rsm->r_flags |= BBR_MARKED_LOST; 4916 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4917 rsm->r_flags &= ~BBR_SACK_PASSED; 4918 } 4919 } 4920 trsm = rsm; 4921 } 4922 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4923 /* Clear the count (we just un-acked them) */ 4924 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR); 4925 bbr->rc_tlp_new_data = 0; 4926 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4927 /* zap the behindness on a rxt */ 4928 bbr->r_ctl.rc_hptsi_agg_delay = 0; 4929 bbr->r_agg_early_set = 0; 4930 bbr->r_ctl.rc_agg_early = 0; 4931 bbr->rc_tlp_rtx_out = 0; 4932 bbr->r_ctl.rc_sacked = 0; 4933 bbr->r_ctl.rc_sacklast = NULL; 4934 bbr->r_timer_override = 1; 4935 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4936 } 4937 4938 /* 4939 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 4940 * we will setup to retransmit the lowest seq number outstanding. 4941 */ 4942 static int 4943 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4944 { 4945 struct inpcb *inp = tptoinpcb(tp); 4946 int32_t rexmt; 4947 int32_t retval = 0; 4948 bool isipv6; 4949 4950 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 4951 if (bbr->rc_all_timers_stopped) { 4952 return (1); 4953 } 4954 if (TCPS_HAVEESTABLISHED(tp->t_state) && 4955 (tp->snd_una == tp->snd_max)) { 4956 /* Nothing outstanding .. nothing to do */ 4957 return (0); 4958 } 4959 /* 4960 * Retransmission timer went off. Message has not been acked within 4961 * retransmit interval. Back off to a longer retransmit interval 4962 * and retransmit one segment. 4963 */ 4964 if (ctf_progress_timeout_check(tp, true)) { 4965 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4966 return (-ETIMEDOUT); /* tcp_drop() */ 4967 } 4968 bbr_remxt_tmr(tp); 4969 if ((bbr->r_ctl.rc_resend == NULL) || 4970 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) { 4971 /* 4972 * If the rwnd collapsed on 4973 * the one we are retransmitting 4974 * it does not count against the 4975 * rxt count. 4976 */ 4977 tp->t_rxtshift++; 4978 } 4979 if (tp->t_rxtshift > V_tcp_retries) { 4980 tp->t_rxtshift = V_tcp_retries; 4981 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 4982 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 4983 /* XXXGL: previously t_softerror was casted to uint16_t */ 4984 MPASS(tp->t_softerror >= 0); 4985 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 4986 return (retval); /* tcp_drop() */ 4987 } 4988 if (tp->t_state == TCPS_SYN_SENT) { 4989 /* 4990 * If the SYN was retransmitted, indicate CWND to be limited 4991 * to 1 segment in cc_conn_init(). 4992 */ 4993 tp->snd_cwnd = 1; 4994 } else if (tp->t_rxtshift == 1) { 4995 /* 4996 * first retransmit; record ssthresh and cwnd so they can be 4997 * recovered if this turns out to be a "bad" retransmit. A 4998 * retransmit is considered "bad" if an ACK for this segment 4999 * is received within RTT/2 interval; the assumption here is 5000 * that the ACK was already in flight. See "On Estimating 5001 * End-to-End Network Path Properties" by Allman and Paxson 5002 * for more details. 5003 */ 5004 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5005 if (!IN_RECOVERY(tp->t_flags)) { 5006 tp->snd_cwnd_prev = tp->snd_cwnd; 5007 tp->snd_ssthresh_prev = tp->snd_ssthresh; 5008 tp->snd_recover_prev = tp->snd_recover; 5009 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 5010 tp->t_flags |= TF_PREVVALID; 5011 } else { 5012 tp->t_flags &= ~TF_PREVVALID; 5013 } 5014 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5015 } else { 5016 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5017 tp->t_flags &= ~TF_PREVVALID; 5018 } 5019 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 5020 if ((tp->t_state == TCPS_SYN_SENT) || 5021 (tp->t_state == TCPS_SYN_RECEIVED)) 5022 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift]; 5023 else 5024 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 5025 TCPT_RANGESET(tp->t_rxtcur, rexmt, 5026 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), 5027 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 5028 /* 5029 * We enter the path for PLMTUD if connection is established or, if 5030 * connection is FIN_WAIT_1 status, reason for the last is that if 5031 * amount of data we send is very small, we could send it in couple 5032 * of packets and process straight to FIN. In that case we won't 5033 * catch ESTABLISHED state. 5034 */ 5035 #ifdef INET6 5036 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 5037 #else 5038 isipv6 = false; 5039 #endif 5040 if (((V_tcp_pmtud_blackhole_detect == 1) || 5041 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 5042 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 5043 ((tp->t_state == TCPS_ESTABLISHED) || 5044 (tp->t_state == TCPS_FIN_WAIT_1))) { 5045 /* 5046 * Idea here is that at each stage of mtu probe (usually, 5047 * 1448 -> 1188 -> 524) should be given 2 chances to recover 5048 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 5049 * should take care of that. 5050 */ 5051 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 5052 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 5053 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 5054 tp->t_rxtshift % 2 == 0)) { 5055 /* 5056 * Enter Path MTU Black-hole Detection mechanism: - 5057 * Disable Path MTU Discovery (IP "DF" bit). - 5058 * Reduce MTU to lower value than what we negotiated 5059 * with peer. 5060 */ 5061 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 5062 /* 5063 * Record that we may have found a black 5064 * hole. 5065 */ 5066 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 5067 /* Keep track of previous MSS. */ 5068 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 5069 } 5070 /* 5071 * Reduce the MSS to blackhole value or to the 5072 * default in an attempt to retransmit. 5073 */ 5074 #ifdef INET6 5075 isipv6 = bbr->r_is_v6; 5076 if (isipv6 && 5077 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 5078 /* Use the sysctl tuneable blackhole MSS. */ 5079 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 5080 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5081 } else if (isipv6) { 5082 /* Use the default MSS. */ 5083 tp->t_maxseg = V_tcp_v6mssdflt; 5084 /* 5085 * Disable Path MTU Discovery when we switch 5086 * to minmss. 5087 */ 5088 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5089 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5090 } 5091 #endif 5092 #if defined(INET6) && defined(INET) 5093 else 5094 #endif 5095 #ifdef INET 5096 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 5097 /* Use the sysctl tuneable blackhole MSS. */ 5098 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 5099 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5100 } else { 5101 /* Use the default MSS. */ 5102 tp->t_maxseg = V_tcp_mssdflt; 5103 /* 5104 * Disable Path MTU Discovery when we switch 5105 * to minmss. 5106 */ 5107 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5108 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5109 } 5110 #endif 5111 } else { 5112 /* 5113 * If further retransmissions are still unsuccessful 5114 * with a lowered MTU, maybe this isn't a blackhole 5115 * and we restore the previous MSS and blackhole 5116 * detection flags. The limit '6' is determined by 5117 * giving each probe stage (1448, 1188, 524) 2 5118 * chances to recover. 5119 */ 5120 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 5121 (tp->t_rxtshift >= 6)) { 5122 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 5123 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 5124 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 5125 if (tp->t_maxseg < V_tcp_mssdflt) { 5126 /* 5127 * The MSS is so small we should not 5128 * process incoming SACK's since we are 5129 * subject to attack in such a case. 5130 */ 5131 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 5132 } else { 5133 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 5134 } 5135 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 5136 } 5137 } 5138 } 5139 /* 5140 * Disable RFC1323 and SACK if we haven't got any response to our 5141 * third SYN to work-around some broken terminal servers (most of 5142 * which have hopefully been retired) that have bad VJ header 5143 * compression code which trashes TCP segments containing 5144 * unknown-to-them TCP options. 5145 */ 5146 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 5147 (tp->t_rxtshift == 3)) 5148 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 5149 /* 5150 * If we backed off this far, our srtt estimate is probably bogus. 5151 * Clobber it so we'll take the next rtt measurement as our srtt; 5152 * move the current srtt into rttvar to keep the current retransmit 5153 * times until then. 5154 */ 5155 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 5156 #ifdef INET6 5157 if (bbr->r_is_v6) 5158 in6_losing(inp); 5159 else 5160 #endif 5161 in_losing(inp); 5162 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 5163 tp->t_srtt = 0; 5164 } 5165 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 5166 tp->snd_recover = tp->snd_max; 5167 tp->t_flags |= TF_ACKNOW; 5168 tp->t_rtttime = 0; 5169 5170 return (retval); 5171 } 5172 5173 static int 5174 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling) 5175 { 5176 int32_t ret = 0; 5177 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 5178 5179 if (timers == 0) { 5180 return (0); 5181 } 5182 if (tp->t_state == TCPS_LISTEN) { 5183 /* no timers on listen sockets */ 5184 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 5185 return (0); 5186 return (1); 5187 } 5188 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 5189 uint32_t left; 5190 5191 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 5192 ret = -1; 5193 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5194 return (0); 5195 } 5196 if (hpts_calling == 0) { 5197 ret = -2; 5198 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5199 return (0); 5200 } 5201 /* 5202 * Ok our timer went off early and we are not paced false 5203 * alarm, go back to sleep. 5204 */ 5205 left = bbr->r_ctl.rc_timer_exp - cts; 5206 ret = -3; 5207 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling); 5208 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left)); 5209 return (1); 5210 } 5211 bbr->rc_tmr_stopped = 0; 5212 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 5213 if (timers & PACE_TMR_DELACK) { 5214 ret = bbr_timeout_delack(tp, bbr, cts); 5215 } else if (timers & PACE_TMR_PERSIT) { 5216 ret = bbr_timeout_persist(tp, bbr, cts); 5217 } else if (timers & PACE_TMR_RACK) { 5218 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5219 ret = bbr_timeout_rack(tp, bbr, cts); 5220 } else if (timers & PACE_TMR_TLP) { 5221 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5222 ret = bbr_timeout_tlp(tp, bbr, cts); 5223 } else if (timers & PACE_TMR_RXT) { 5224 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5225 ret = bbr_timeout_rxt(tp, bbr, cts); 5226 } else if (timers & PACE_TMR_KEEP) { 5227 ret = bbr_timeout_keepalive(tp, bbr, cts); 5228 } 5229 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling); 5230 return (ret); 5231 } 5232 5233 static void 5234 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts) 5235 { 5236 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 5237 uint8_t hpts_removed = 0; 5238 5239 if (tcp_in_hpts(bbr->rc_tp) && 5240 (bbr->rc_timer_first == 1)) { 5241 /* 5242 * If we are canceling timer's when we have the 5243 * timer ahead of the output being paced. We also 5244 * must remove ourselves from the hpts. 5245 */ 5246 hpts_removed = 1; 5247 tcp_hpts_remove(bbr->rc_tp); 5248 if (bbr->r_ctl.rc_last_delay_val) { 5249 /* Update the last hptsi delay too */ 5250 uint32_t time_since_send; 5251 5252 if (TSTMP_GT(cts, bbr->rc_pacer_started)) 5253 time_since_send = cts - bbr->rc_pacer_started; 5254 else 5255 time_since_send = 0; 5256 if (bbr->r_ctl.rc_last_delay_val > time_since_send) { 5257 /* Cut down our slot time */ 5258 bbr->r_ctl.rc_last_delay_val -= time_since_send; 5259 } else { 5260 bbr->r_ctl.rc_last_delay_val = 0; 5261 } 5262 bbr->rc_pacer_started = cts; 5263 } 5264 } 5265 bbr->rc_timer_first = 0; 5266 bbr_log_to_cancel(bbr, line, cts, hpts_removed); 5267 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 5268 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 5269 } 5270 } 5271 5272 static int 5273 bbr_stopall(struct tcpcb *tp) 5274 { 5275 struct tcp_bbr *bbr; 5276 5277 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 5278 bbr->rc_all_timers_stopped = 1; 5279 5280 tcp_hpts_remove(tp); 5281 5282 return (0); 5283 } 5284 5285 static uint32_t 5286 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts) 5287 { 5288 struct bbr_sendmap *rsm; 5289 5290 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 5291 if ((rsm == NULL) || (u_rsm == rsm)) 5292 return (cts); 5293 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 5294 } 5295 5296 static void 5297 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr, 5298 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time) 5299 { 5300 int32_t idx; 5301 5302 rsm->r_rtr_cnt++; 5303 rsm->r_dupack = 0; 5304 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) { 5305 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS; 5306 rsm->r_flags |= BBR_OVERMAX; 5307 } 5308 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 5309 /* Take off the collapsed flag at rxt */ 5310 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 5311 } 5312 if (rsm->r_flags & BBR_MARKED_LOST) { 5313 /* We have retransmitted, its no longer lost */ 5314 rsm->r_flags &= ~BBR_MARKED_LOST; 5315 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 5316 } 5317 if (rsm->r_flags & BBR_RXT_CLEARED) { 5318 /* 5319 * We hit a RXT timer on it and 5320 * we cleared the "acked" flag. 5321 * We now have it going back into 5322 * flight, we can remove the cleared 5323 * flag and possibly do accounting on 5324 * this piece. 5325 */ 5326 rsm->r_flags &= ~BBR_RXT_CLEARED; 5327 } 5328 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) { 5329 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 5330 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 5331 } 5332 idx = rsm->r_rtr_cnt - 1; 5333 rsm->r_tim_lastsent[idx] = cts; 5334 rsm->r_pacing_delay = pacing_time; 5335 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5336 rsm->r_ts_valid = bbr->rc_ts_valid; 5337 if (bbr->rc_ts_valid) 5338 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5339 if (bbr->r_ctl.r_app_limited_until) 5340 rsm->r_app_limited = 1; 5341 else 5342 rsm->r_app_limited = 0; 5343 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5344 rsm->r_bbr_state = bbr_state_val(bbr); 5345 else 5346 rsm->r_bbr_state = 8; 5347 if (rsm->r_flags & BBR_ACKED) { 5348 /* Problably MTU discovery messing with us */ 5349 uint32_t old_flags; 5350 5351 old_flags = rsm->r_flags; 5352 rsm->r_flags &= ~BBR_ACKED; 5353 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 5354 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 5355 if (bbr->r_ctl.rc_sacked == 0) 5356 bbr->r_ctl.rc_sacklast = NULL; 5357 } 5358 if (rsm->r_in_tmap) { 5359 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5360 } 5361 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5362 rsm->r_in_tmap = 1; 5363 if (rsm->r_flags & BBR_SACK_PASSED) { 5364 /* We have retransmitted due to the SACK pass */ 5365 rsm->r_flags &= ~BBR_SACK_PASSED; 5366 rsm->r_flags |= BBR_WAS_SACKPASS; 5367 } 5368 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5369 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5370 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5371 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 5372 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5373 rsm->r_is_gain = 1; 5374 rsm->r_is_drain = 0; 5375 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5376 rsm->r_is_drain = 1; 5377 rsm->r_is_gain = 0; 5378 } else { 5379 rsm->r_is_drain = 0; 5380 rsm->r_is_gain = 0; 5381 } 5382 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */ 5383 } 5384 5385 /* 5386 * Returns 0, or the sequence where we stopped 5387 * updating. We also update the lenp to be the amount 5388 * of data left. 5389 */ 5390 5391 static uint32_t 5392 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr, 5393 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time) 5394 { 5395 /* 5396 * We (re-)transmitted starting at rsm->r_start for some length 5397 * (possibly less than r_end. 5398 */ 5399 struct bbr_sendmap *nrsm; 5400 uint32_t c_end; 5401 int32_t len; 5402 5403 len = *lenp; 5404 c_end = rsm->r_start + len; 5405 if (SEQ_GEQ(c_end, rsm->r_end)) { 5406 /* 5407 * We retransmitted the whole piece or more than the whole 5408 * slopping into the next rsm. 5409 */ 5410 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5411 if (c_end == rsm->r_end) { 5412 *lenp = 0; 5413 return (0); 5414 } else { 5415 int32_t act_len; 5416 5417 /* Hangs over the end return whats left */ 5418 act_len = rsm->r_end - rsm->r_start; 5419 *lenp = (len - act_len); 5420 return (rsm->r_end); 5421 } 5422 /* We don't get out of this block. */ 5423 } 5424 /* 5425 * Here we retransmitted less than the whole thing which means we 5426 * have to split this into what was transmitted and what was not. 5427 */ 5428 nrsm = bbr_alloc_full_limit(bbr); 5429 if (nrsm == NULL) { 5430 *lenp = 0; 5431 return (0); 5432 } 5433 /* 5434 * So here we are going to take the original rsm and make it what we 5435 * retransmitted. nrsm will be the tail portion we did not 5436 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 5437 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 5438 * 1, 6 and the new piece will be 6, 11. 5439 */ 5440 bbr_clone_rsm(bbr, nrsm, rsm, c_end); 5441 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 5442 nrsm->r_dupack = 0; 5443 if (rsm->r_in_tmap) { 5444 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 5445 nrsm->r_in_tmap = 1; 5446 } 5447 rsm->r_flags &= (~BBR_HAS_FIN); 5448 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5449 *lenp = 0; 5450 return (0); 5451 } 5452 5453 static uint64_t 5454 bbr_get_hardware_rate(struct tcp_bbr *bbr) 5455 { 5456 uint64_t bw; 5457 5458 bw = bbr_get_bw(bbr); 5459 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]; 5460 bw /= (uint64_t)BBR_UNIT; 5461 return(bw); 5462 } 5463 5464 static void 5465 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts, 5466 uint64_t act_rate, uint64_t rate_wanted) 5467 { 5468 /* 5469 * We could not get a full gains worth 5470 * of rate. 5471 */ 5472 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) { 5473 /* we can't even get the real rate */ 5474 uint64_t red; 5475 5476 bbr->skip_gain = 1; 5477 bbr->gain_is_limited = 0; 5478 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate; 5479 if (red) 5480 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts); 5481 } else { 5482 /* We can use a lower gain */ 5483 bbr->skip_gain = 0; 5484 bbr->gain_is_limited = 1; 5485 } 5486 } 5487 5488 static void 5489 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts) 5490 { 5491 const struct tcp_hwrate_limit_table *nrte; 5492 int error, rate = -1; 5493 5494 if (bbr->r_ctl.crte == NULL) 5495 return; 5496 if ((bbr->rc_inp->inp_route.ro_nh == NULL) || 5497 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) { 5498 /* Lost our routes? */ 5499 /* Clear the way for a re-attempt */ 5500 bbr->bbr_attempt_hdwr_pace = 0; 5501 lost_rate: 5502 bbr->gain_is_limited = 0; 5503 bbr->skip_gain = 0; 5504 bbr->bbr_hdrw_pacing = 0; 5505 counter_u64_add(bbr_flows_whdwr_pacing, -1); 5506 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 5507 tcp_bbr_tso_size_check(bbr, cts); 5508 return; 5509 } 5510 rate = bbr_get_hardware_rate(bbr); 5511 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte, 5512 bbr->rc_tp, 5513 bbr->rc_inp->inp_route.ro_nh->nh_ifp, 5514 rate, 5515 (RS_PACING_GEQ|RS_PACING_SUB_OK), 5516 &error, NULL); 5517 if (nrte == NULL) { 5518 goto lost_rate; 5519 } 5520 if (nrte != bbr->r_ctl.crte) { 5521 bbr->r_ctl.crte = nrte; 5522 if (error == 0) { 5523 BBR_STAT_INC(bbr_hdwr_rl_mod_ok); 5524 if (bbr->r_ctl.crte->rate < rate) { 5525 /* We have a problem */ 5526 bbr_setup_less_of_rate(bbr, cts, 5527 bbr->r_ctl.crte->rate, rate); 5528 } else { 5529 /* We are good */ 5530 bbr->gain_is_limited = 0; 5531 bbr->skip_gain = 0; 5532 } 5533 } else { 5534 /* A failure should release the tag */ 5535 BBR_STAT_INC(bbr_hdwr_rl_mod_fail); 5536 bbr->gain_is_limited = 0; 5537 bbr->skip_gain = 0; 5538 bbr->bbr_hdrw_pacing = 0; 5539 } 5540 bbr_type_log_hdwr_pacing(bbr, 5541 bbr->r_ctl.crte->ptbl->rs_ifp, 5542 rate, 5543 bbr->r_ctl.crte->rate, 5544 __LINE__, 5545 cts, 5546 error); 5547 } 5548 } 5549 5550 static void 5551 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts) 5552 { 5553 /* 5554 * If we have hardware pacing support 5555 * we need to factor that in for our 5556 * TSO size. 5557 */ 5558 const struct tcp_hwrate_limit_table *rlp; 5559 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay; 5560 5561 if ((bbr->bbr_hdrw_pacing == 0) || 5562 (IN_RECOVERY(bbr->rc_tp->t_flags)) || 5563 (bbr->r_ctl.crte == NULL)) 5564 return; 5565 if (bbr->hw_pacing_set == 0) { 5566 /* Not yet by the hdwr pacing count delay */ 5567 return; 5568 } 5569 if (bbr_hdwr_pace_adjust == 0) { 5570 /* No adjustment */ 5571 return; 5572 } 5573 rlp = bbr->r_ctl.crte; 5574 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) 5575 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5576 else 5577 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5578 /* 5579 * So lets first get the 5580 * time we will take between 5581 * TSO sized sends currently without 5582 * hardware help. 5583 */ 5584 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT, 5585 bbr->r_ctl.rc_pace_max_segs, cts, 1); 5586 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg; 5587 hdwr_delay *= rlp->time_between; 5588 if (cur_delay > hdwr_delay) 5589 delta = cur_delay - hdwr_delay; 5590 else 5591 delta = 0; 5592 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay, 5593 (bbr->r_ctl.rc_pace_max_segs / maxseg), 5594 1); 5595 if (delta && 5596 (delta < (max(rlp->time_between, 5597 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) { 5598 /* 5599 * Now lets divide by the pacing 5600 * time between each segment the 5601 * hardware sends rounding up and 5602 * derive a bytes from that. We multiply 5603 * that by bbr_hdwr_pace_adjust to get 5604 * more bang for our buck. 5605 * 5606 * The goal is to have the software pacer 5607 * waiting no more than an additional 5608 * pacing delay if we can (without the 5609 * compensation i.e. x bbr_hdwr_pace_adjust). 5610 */ 5611 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between), 5612 (bbr->r_ctl.rc_pace_max_segs/maxseg)); 5613 seg_sz *= bbr_hdwr_pace_adjust; 5614 if (bbr_hdwr_pace_floor && 5615 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5616 /* Currently hardware paces 5617 * out rs_min_seg segments at a time. 5618 * We need to make sure we always send at least 5619 * a full burst of bbr_hdwr_pace_floor down. 5620 */ 5621 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5622 } 5623 seg_sz *= maxseg; 5624 } else if (delta == 0) { 5625 /* 5626 * The highest pacing rate is 5627 * above our b/w gained. This means 5628 * we probably are going quite fast at 5629 * the hardware highest rate. Lets just multiply 5630 * the calculated TSO size by the 5631 * multiplier factor (its probably 5632 * 4 segments in the default config for 5633 * mlx). 5634 */ 5635 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust; 5636 if (bbr_hdwr_pace_floor && 5637 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5638 /* Currently hardware paces 5639 * out rs_min_seg segments at a time. 5640 * We need to make sure we always send at least 5641 * a full burst of bbr_hdwr_pace_floor down. 5642 */ 5643 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5644 } 5645 } else { 5646 /* 5647 * The pacing time difference is so 5648 * big that the hardware will 5649 * pace out more rapidly then we 5650 * really want and then we 5651 * will have a long delay. Lets just keep 5652 * the same TSO size so its as if 5653 * we were not using hdwr pacing (we 5654 * just gain a bit of spacing from the 5655 * hardware if seg_sz > 1). 5656 */ 5657 seg_sz = bbr->r_ctl.rc_pace_max_segs; 5658 } 5659 if (seg_sz > bbr->r_ctl.rc_pace_max_segs) 5660 new_tso = seg_sz; 5661 else 5662 new_tso = bbr->r_ctl.rc_pace_max_segs; 5663 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg)) 5664 new_tso = PACE_MAX_IP_BYTES - maxseg; 5665 5666 if (new_tso != bbr->r_ctl.rc_pace_max_segs) { 5667 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0); 5668 bbr->r_ctl.rc_pace_max_segs = new_tso; 5669 } 5670 } 5671 5672 static void 5673 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts) 5674 { 5675 uint64_t bw; 5676 uint32_t old_tso = 0, new_tso; 5677 uint32_t maxseg, bytes; 5678 uint32_t tls_seg=0; 5679 /* 5680 * Google/linux uses the following algorithm to determine 5681 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18): 5682 * 5683 * bytes = bw_in_bytes_per_second / 1000 5684 * bytes = min(bytes, 64k) 5685 * tso_segs = bytes / MSS 5686 * if (bw < 1.2Mbs) 5687 * min_tso_segs = 1 5688 * else 5689 * min_tso_segs = 2 5690 * tso_segs = max(tso_segs, min_tso_segs) 5691 * 5692 * * Note apply a device specific limit (we apply this in the 5693 * tcp_m_copym). 5694 * Note that before the initial measurement is made google bursts out 5695 * a full iwnd just like new-reno/cubic. 5696 * 5697 * We do not use this algorithm. Instead we 5698 * use a two phased approach: 5699 * 5700 * if ( bw <= per-tcb-cross-over) 5701 * goal_tso = calculate how much with this bw we 5702 * can send in goal-time seconds. 5703 * if (goal_tso > mss) 5704 * seg = goal_tso / mss 5705 * tso = seg * mss 5706 * else 5707 * tso = mss 5708 * if (tso > per-tcb-max) 5709 * tso = per-tcb-max 5710 * else if ( bw > 512Mbps) 5711 * tso = max-tso (64k/mss) 5712 * else 5713 * goal_tso = bw / per-tcb-divsor 5714 * seg = (goal_tso + mss-1)/mss 5715 * tso = seg * mss 5716 * 5717 * if (tso < per-tcb-floor) 5718 * tso = per-tcb-floor 5719 * if (tso > per-tcb-utter_max) 5720 * tso = per-tcb-utter_max 5721 * 5722 * Note the default per-tcb-divisor is 1000 (same as google). 5723 * the goal cross over is 30Mbps however. To recreate googles 5724 * algorithm you need to set: 5725 * 5726 * cross-over = 23,168,000 bps 5727 * goal-time = 18000 5728 * per-tcb-max = 2 5729 * per-tcb-divisor = 1000 5730 * per-tcb-floor = 1 5731 * 5732 * This will get you "google bbr" behavior with respect to tso size. 5733 * 5734 * Note we do set anything TSO size until we are past the initial 5735 * window. Before that we gnerally use either a single MSS 5736 * or we use the full IW size (so we burst a IW at a time) 5737 */ 5738 5739 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) { 5740 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5741 } else { 5742 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5743 } 5744 old_tso = bbr->r_ctl.rc_pace_max_segs; 5745 if (bbr->rc_past_init_win == 0) { 5746 /* 5747 * Not enough data has been acknowledged to make a 5748 * judgement. Set up the initial TSO based on if we 5749 * are sending a full IW at once or not. 5750 */ 5751 if (bbr->rc_use_google) 5752 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2); 5753 else if (bbr->bbr_init_win_cheat) 5754 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp); 5755 else 5756 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5757 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg) 5758 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg; 5759 if (bbr->r_ctl.rc_pace_max_segs == 0) { 5760 bbr->r_ctl.rc_pace_max_segs = maxseg; 5761 } 5762 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0); 5763 bbr_adjust_for_hw_pacing(bbr, cts); 5764 return; 5765 } 5766 /** 5767 * Now lets set the TSO goal based on our delivery rate in 5768 * bytes per second. Note we only do this if 5769 * we have acked at least the initial cwnd worth of data. 5770 */ 5771 bw = bbr_get_bw(bbr); 5772 if (IN_RECOVERY(bbr->rc_tp->t_flags) && 5773 (bbr->rc_use_google == 0)) { 5774 /* We clamp to one MSS in recovery */ 5775 new_tso = maxseg; 5776 } else if (bbr->rc_use_google) { 5777 int min_tso_segs; 5778 5779 /* Google considers the gain too */ 5780 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) { 5781 bw *= bbr->r_ctl.rc_bbr_hptsi_gain; 5782 bw /= BBR_UNIT; 5783 } 5784 bytes = bw / 1024; 5785 if (bytes > (64 * 1024)) 5786 bytes = 64 * 1024; 5787 new_tso = bytes / maxseg; 5788 if (bw < ONE_POINT_TWO_MEG) 5789 min_tso_segs = 1; 5790 else 5791 min_tso_segs = 2; 5792 if (new_tso < min_tso_segs) 5793 new_tso = min_tso_segs; 5794 new_tso *= maxseg; 5795 } else if (bbr->rc_no_pacing) { 5796 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg; 5797 } else if (bw <= bbr->r_ctl.bbr_cross_over) { 5798 /* 5799 * Calculate the worse case b/w TSO if we are inserting no 5800 * more than a delay_target number of TSO's. 5801 */ 5802 uint32_t tso_len, min_tso; 5803 5804 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw); 5805 if (tso_len > maxseg) { 5806 new_tso = tso_len / maxseg; 5807 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max) 5808 new_tso = bbr->r_ctl.bbr_hptsi_segments_max; 5809 new_tso *= maxseg; 5810 } else { 5811 /* 5812 * less than a full sized frame yikes.. long rtt or 5813 * low bw? 5814 */ 5815 min_tso = bbr_minseg(bbr); 5816 if ((tso_len > min_tso) && (bbr_all_get_min == 0)) 5817 new_tso = rounddown(tso_len, min_tso); 5818 else 5819 new_tso = min_tso; 5820 } 5821 } else if (bw > FIVETWELVE_MBPS) { 5822 /* 5823 * This guy is so fast b/w wise that we can TSO as large as 5824 * possible of segments that the NIC will allow. 5825 */ 5826 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5827 } else { 5828 /* 5829 * This formula is based on attempting to send a segment or 5830 * more every bbr_hptsi_per_second. The default is 1000 5831 * which means you are targeting what you can send every 1ms 5832 * based on the peers bw. 5833 * 5834 * If the number drops to say 500, then you are looking more 5835 * at 2ms and you will raise how much we send in a single 5836 * TSO thus saving CPU (less bbr_output_wtime() calls). The 5837 * trade off of course is you will send more at once and 5838 * thus tend to clump up the sends into larger "bursts" 5839 * building a queue. 5840 */ 5841 bw /= bbr->r_ctl.bbr_hptsi_per_second; 5842 new_tso = roundup(bw, (uint64_t)maxseg); 5843 /* 5844 * Gate the floor to match what our lower than 48Mbps 5845 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus 5846 * becomes the floor for this calculation. 5847 */ 5848 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg)) 5849 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg); 5850 } 5851 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor))) 5852 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor; 5853 if (new_tso > PACE_MAX_IP_BYTES) 5854 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5855 /* Enforce an utter maximum. */ 5856 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) { 5857 new_tso = bbr->r_ctl.bbr_utter_max * maxseg; 5858 } 5859 if (old_tso != new_tso) { 5860 /* Only log changes */ 5861 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0); 5862 bbr->r_ctl.rc_pace_max_segs = new_tso; 5863 } 5864 /* We have hardware pacing! */ 5865 bbr_adjust_for_hw_pacing(bbr, cts); 5866 } 5867 5868 static void 5869 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len, 5870 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts, 5871 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc, 5872 struct sockbuf *sb) 5873 { 5874 5875 struct bbr_sendmap *rsm, *nrsm; 5876 register uint32_t snd_max, snd_una; 5877 uint32_t pacing_time; 5878 /* 5879 * Add to the RACK log of packets in flight or retransmitted. If 5880 * there is a TS option we will use the TS echoed, if not we will 5881 * grab a TS. 5882 * 5883 * Retransmissions will increment the count and move the ts to its 5884 * proper place. Note that if options do not include TS's then we 5885 * won't be able to effectively use the ACK for an RTT on a retran. 5886 * 5887 * Notes about r_start and r_end. Lets consider a send starting at 5888 * sequence 1 for 10 bytes. In such an example the r_start would be 5889 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 5890 * This means that r_end is actually the first sequence for the next 5891 * slot (11). 5892 * 5893 */ 5894 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5895 if (err) { 5896 /* 5897 * We don't log errors -- we could but snd_max does not 5898 * advance in this case either. 5899 */ 5900 return; 5901 } 5902 if (th_flags & TH_RST) { 5903 /* 5904 * We don't log resets and we return immediately from 5905 * sending 5906 */ 5907 *abandon = 1; 5908 return; 5909 } 5910 snd_una = tp->snd_una; 5911 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) { 5912 /* 5913 * The call to bbr_log_output is made before bumping 5914 * snd_max. This means we can record one extra byte on a SYN 5915 * or FIN if seq_out is adding more on and a FIN is present 5916 * (and we are not resending). 5917 */ 5918 if ((th_flags & TH_SYN) && (tp->iss == seq_out)) 5919 len++; 5920 if (th_flags & TH_FIN) 5921 len++; 5922 } 5923 if (SEQ_LEQ((seq_out + len), snd_una)) { 5924 /* Are sending an old segment to induce an ack (keep-alive)? */ 5925 return; 5926 } 5927 if (SEQ_LT(seq_out, snd_una)) { 5928 /* huh? should we panic? */ 5929 uint32_t end; 5930 5931 end = seq_out + len; 5932 seq_out = snd_una; 5933 len = end - seq_out; 5934 } 5935 snd_max = tp->snd_max; 5936 if (len == 0) { 5937 /* We don't log zero window probes */ 5938 return; 5939 } 5940 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1); 5941 /* First question is it a retransmission? */ 5942 if (seq_out == snd_max) { 5943 again: 5944 rsm = bbr_alloc(bbr); 5945 if (rsm == NULL) { 5946 return; 5947 } 5948 rsm->r_flags = 0; 5949 if (th_flags & TH_SYN) 5950 rsm->r_flags |= BBR_HAS_SYN; 5951 if (th_flags & TH_FIN) 5952 rsm->r_flags |= BBR_HAS_FIN; 5953 rsm->r_tim_lastsent[0] = cts; 5954 rsm->r_rtr_cnt = 1; 5955 rsm->r_rtr_bytes = 0; 5956 rsm->r_start = seq_out; 5957 rsm->r_end = rsm->r_start + len; 5958 rsm->r_dupack = 0; 5959 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5960 rsm->r_pacing_delay = pacing_time; 5961 rsm->r_ts_valid = bbr->rc_ts_valid; 5962 if (bbr->rc_ts_valid) 5963 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5964 rsm->r_del_time = bbr->r_ctl.rc_del_time; 5965 if (bbr->r_ctl.r_app_limited_until) 5966 rsm->r_app_limited = 1; 5967 else 5968 rsm->r_app_limited = 0; 5969 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5970 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5971 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5972 /* 5973 * Here we must also add in this rsm since snd_max 5974 * is updated after we return from a new send. 5975 */ 5976 rsm->r_flight_at_send += len; 5977 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 5978 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5979 rsm->r_in_tmap = 1; 5980 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5981 rsm->r_bbr_state = bbr_state_val(bbr); 5982 else 5983 rsm->r_bbr_state = 8; 5984 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5985 rsm->r_is_gain = 1; 5986 rsm->r_is_drain = 0; 5987 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5988 rsm->r_is_drain = 1; 5989 rsm->r_is_gain = 0; 5990 } else { 5991 rsm->r_is_drain = 0; 5992 rsm->r_is_gain = 0; 5993 } 5994 return; 5995 } 5996 /* 5997 * If we reach here its a retransmission and we need to find it. 5998 */ 5999 more: 6000 if (hintrsm && (hintrsm->r_start == seq_out)) { 6001 rsm = hintrsm; 6002 hintrsm = NULL; 6003 } else if (bbr->r_ctl.rc_next) { 6004 /* We have a hint from a previous run */ 6005 rsm = bbr->r_ctl.rc_next; 6006 } else { 6007 /* No hints sorry */ 6008 rsm = NULL; 6009 } 6010 if ((rsm) && (rsm->r_start == seq_out)) { 6011 /* 6012 * We used rc_next or hintrsm to retransmit, hopefully the 6013 * likely case. 6014 */ 6015 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6016 if (len == 0) { 6017 return; 6018 } else { 6019 goto more; 6020 } 6021 } 6022 /* Ok it was not the last pointer go through it the hard way. */ 6023 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6024 if (rsm->r_start == seq_out) { 6025 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6026 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 6027 if (len == 0) { 6028 return; 6029 } else { 6030 continue; 6031 } 6032 } 6033 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 6034 /* Transmitted within this piece */ 6035 /* 6036 * Ok we must split off the front and then let the 6037 * update do the rest 6038 */ 6039 nrsm = bbr_alloc_full_limit(bbr); 6040 if (nrsm == NULL) { 6041 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 6042 return; 6043 } 6044 /* 6045 * copy rsm to nrsm and then trim the front of rsm 6046 * to not include this part. 6047 */ 6048 bbr_clone_rsm(bbr, nrsm, rsm, seq_out); 6049 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 6050 if (rsm->r_in_tmap) { 6051 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6052 nrsm->r_in_tmap = 1; 6053 } 6054 rsm->r_flags &= (~BBR_HAS_FIN); 6055 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time); 6056 if (len == 0) { 6057 return; 6058 } 6059 } 6060 } 6061 /* 6062 * Hmm not found in map did they retransmit both old and on into the 6063 * new? 6064 */ 6065 if (seq_out == tp->snd_max) { 6066 goto again; 6067 } else if (SEQ_LT(seq_out, tp->snd_max)) { 6068 #ifdef BBR_INVARIANTS 6069 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 6070 seq_out, len, tp->snd_una, tp->snd_max); 6071 printf("Starting Dump of all rack entries\n"); 6072 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6073 printf("rsm:%p start:%u end:%u\n", 6074 rsm, rsm->r_start, rsm->r_end); 6075 } 6076 printf("Dump complete\n"); 6077 panic("seq_out not found rack:%p tp:%p", 6078 bbr, tp); 6079 #endif 6080 } else { 6081 #ifdef BBR_INVARIANTS 6082 /* 6083 * Hmm beyond sndmax? (only if we are using the new rtt-pack 6084 * flag) 6085 */ 6086 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 6087 seq_out, len, tp->snd_max, tp); 6088 #endif 6089 } 6090 } 6091 6092 static void 6093 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt) 6094 { 6095 /* 6096 * Collapse timeout back the cum-ack moved. 6097 */ 6098 tp->t_rxtshift = 0; 6099 tp->t_softerror = 0; 6100 } 6101 6102 static void 6103 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin) 6104 { 6105 bbr->rtt_valid = 1; 6106 bbr->r_ctl.cur_rtt = rtt_usecs; 6107 bbr->r_ctl.ts_in = tsin; 6108 if (rsm_send_time) 6109 bbr->r_ctl.cur_rtt_send_time = rsm_send_time; 6110 } 6111 6112 static void 6113 bbr_make_timestamp_determination(struct tcp_bbr *bbr) 6114 { 6115 /** 6116 * We have in our bbr control: 6117 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp). 6118 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts). 6119 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts) 6120 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time) 6121 * 6122 * Now we can calculate the time between the sends by doing: 6123 * 6124 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts 6125 * 6126 * And the peer's time between receiving them by doing: 6127 * 6128 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp 6129 * 6130 * We want to figure out if the timestamp values are in msec, 10msec or usec. 6131 * We also may find that we can't use the timestamps if say we see 6132 * that the peer_delta indicates that though we may have taken 10ms to 6133 * pace out the data, it only saw 1ms between the two packets. This would 6134 * indicate that somewhere on the path is a batching entity that is giving 6135 * out time-slices of the actual b/w. This would mean we could not use 6136 * reliably the peers timestamps. 6137 * 6138 * We expect delta > peer_delta initially. Until we figure out the 6139 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio. 6140 * If we place 1000 there then its a ms vs our usec. If we place 10000 there 6141 * then its 10ms vs our usec. If the peer is running a usec clock we would 6142 * put a 1 there. If the value is faster then ours, we will disable the 6143 * use of timestamps (though we could revist this later if we find it to be not 6144 * just an isolated one or two flows)). 6145 * 6146 * To detect the batching middle boxes we will come up with our compensation and 6147 * if with it in place, we find the peer is drastically off (by some margin) in 6148 * the smaller direction, then we will assume the worst case and disable use of timestamps. 6149 * 6150 */ 6151 uint64_t delta, peer_delta, delta_up; 6152 6153 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts; 6154 if (delta < bbr_min_usec_delta) { 6155 /* 6156 * Have not seen a min amount of time 6157 * between our send times so we can 6158 * make a determination of the timestamp 6159 * yet. 6160 */ 6161 return; 6162 } 6163 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp; 6164 if (peer_delta < bbr_min_peer_delta) { 6165 /* 6166 * We may have enough in the form of 6167 * our delta but the peers number 6168 * has not changed that much. It could 6169 * be its clock ratio is such that 6170 * we need more data (10ms tick) or 6171 * there may be other compression scenarios 6172 * going on. In any event we need the 6173 * spread to be larger. 6174 */ 6175 return; 6176 } 6177 /* Ok lets first see which way our delta is going */ 6178 if (peer_delta > delta) { 6179 /* Very unlikely, the peer without 6180 * compensation shows that it saw 6181 * the two sends arrive further apart 6182 * then we saw then in micro-seconds. 6183 */ 6184 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) { 6185 /* well it looks like the peer is a micro-second clock. */ 6186 bbr->rc_ts_clock_set = 1; 6187 bbr->r_ctl.bbr_peer_tsratio = 1; 6188 } else { 6189 bbr->rc_ts_cant_be_used = 1; 6190 bbr->rc_ts_clock_set = 1; 6191 } 6192 return; 6193 } 6194 /* Ok we know that the peer_delta is smaller than our send distance */ 6195 bbr->rc_ts_clock_set = 1; 6196 /* First question is it within the percentage that they are using usec time? */ 6197 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent; 6198 if ((peer_delta + delta_up) >= delta) { 6199 /* Its a usec clock */ 6200 bbr->r_ctl.bbr_peer_tsratio = 1; 6201 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6202 return; 6203 } 6204 /* Ok if not usec, what about 10usec (though unlikely)? */ 6205 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent; 6206 if (((peer_delta * 10) + delta_up) >= delta) { 6207 bbr->r_ctl.bbr_peer_tsratio = 10; 6208 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6209 return; 6210 } 6211 /* And what about 100usec (though again unlikely)? */ 6212 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent; 6213 if (((peer_delta * 100) + delta_up) >= delta) { 6214 bbr->r_ctl.bbr_peer_tsratio = 100; 6215 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6216 return; 6217 } 6218 /* And how about 1 msec (the most likely one)? */ 6219 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent; 6220 if (((peer_delta * 1000) + delta_up) >= delta) { 6221 bbr->r_ctl.bbr_peer_tsratio = 1000; 6222 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6223 return; 6224 } 6225 /* Ok if not msec could it be 10 msec? */ 6226 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent; 6227 if (((peer_delta * 10000) + delta_up) >= delta) { 6228 bbr->r_ctl.bbr_peer_tsratio = 10000; 6229 return; 6230 } 6231 /* If we fall down here the clock tick so slowly we can't use it */ 6232 bbr->rc_ts_cant_be_used = 1; 6233 bbr->r_ctl.bbr_peer_tsratio = 0; 6234 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6235 } 6236 6237 /* 6238 * Collect new round-trip time estimate 6239 * and update averages and current timeout. 6240 */ 6241 static void 6242 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts) 6243 { 6244 int32_t delta; 6245 uint32_t rtt, tsin; 6246 int32_t rtt_ticks; 6247 6248 if (bbr->rtt_valid == 0) 6249 /* No valid sample */ 6250 return; 6251 6252 rtt = bbr->r_ctl.cur_rtt; 6253 tsin = bbr->r_ctl.ts_in; 6254 if (bbr->rc_prtt_set_ts) { 6255 /* 6256 * We are to force feed the rttProp filter due 6257 * to an entry into PROBE_RTT. This assures 6258 * that the times are sync'd between when we 6259 * go into PROBE_RTT and the filter expiration. 6260 * 6261 * Google does not use a true filter, so they do 6262 * this implicitly since they only keep one value 6263 * and when they enter probe-rtt they update the 6264 * value to the newest rtt. 6265 */ 6266 uint32_t rtt_prop; 6267 6268 bbr->rc_prtt_set_ts = 0; 6269 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 6270 if (rtt > rtt_prop) 6271 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts); 6272 else 6273 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6274 } 6275 #ifdef STATS 6276 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt)); 6277 #endif 6278 if (bbr->rc_ack_was_delayed) 6279 rtt += bbr->r_ctl.rc_ack_hdwr_delay; 6280 6281 if (rtt < bbr->r_ctl.rc_lowest_rtt) 6282 bbr->r_ctl.rc_lowest_rtt = rtt; 6283 bbr_log_rtt_sample(bbr, rtt, tsin); 6284 if (bbr->r_init_rtt) { 6285 /* 6286 * The initial rtt is not-trusted, nuke it and lets get 6287 * our first valid measurement in. 6288 */ 6289 bbr->r_init_rtt = 0; 6290 tp->t_srtt = 0; 6291 } 6292 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) { 6293 /* 6294 * So we have not yet figured out 6295 * what the peers TSTMP value is 6296 * in (most likely ms). We need a 6297 * series of cum-ack's to determine 6298 * this reliably. 6299 */ 6300 if (bbr->rc_ack_is_cumack) { 6301 if (bbr->rc_ts_data_set) { 6302 /* Lets attempt to determine the timestamp granularity. */ 6303 bbr_make_timestamp_determination(bbr); 6304 } else { 6305 bbr->rc_ts_data_set = 1; 6306 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts; 6307 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time; 6308 } 6309 } else { 6310 /* 6311 * We have to have consecutive acks 6312 * reset any "filled" state to none. 6313 */ 6314 bbr->rc_ts_data_set = 0; 6315 } 6316 } 6317 /* Round it up */ 6318 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1))); 6319 if (tp->t_srtt != 0) { 6320 /* 6321 * srtt is stored as fixed point with 5 bits after the 6322 * binary point (i.e., scaled by 8). The following magic is 6323 * equivalent to the smoothing algorithm in rfc793 with an 6324 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 6325 * Adjust rtt to origin 0. 6326 */ 6327 6328 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT) 6329 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 6330 6331 tp->t_srtt += delta; 6332 if (tp->t_srtt <= 0) 6333 tp->t_srtt = 1; 6334 6335 /* 6336 * We accumulate a smoothed rtt variance (actually, a 6337 * smoothed mean difference), then set the retransmit timer 6338 * to smoothed rtt + 4 times the smoothed variance. rttvar 6339 * is stored as fixed point with 4 bits after the binary 6340 * point (scaled by 16). The following is equivalent to 6341 * rfc793 smoothing with an alpha of .75 (rttvar = 6342 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 6343 * wired-in beta. 6344 */ 6345 if (delta < 0) 6346 delta = -delta; 6347 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 6348 tp->t_rttvar += delta; 6349 if (tp->t_rttvar <= 0) 6350 tp->t_rttvar = 1; 6351 } else { 6352 /* 6353 * No rtt measurement yet - use the unsmoothed rtt. Set the 6354 * variance to half the rtt (so our first retransmit happens 6355 * at 3*rtt). 6356 */ 6357 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT; 6358 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1); 6359 } 6360 KMOD_TCPSTAT_INC(tcps_rttupdated); 6361 if (tp->t_rttupdated < UCHAR_MAX) 6362 tp->t_rttupdated++; 6363 #ifdef STATS 6364 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks)); 6365 #endif 6366 /* 6367 * the retransmit should happen at rtt + 4 * rttvar. Because of the 6368 * way we do the smoothing, srtt and rttvar will each average +1/2 6369 * tick of bias. When we compute the retransmit timer, we want 1/2 6370 * tick of rounding and 1 extra tick because of +-1/2 tick 6371 * uncertainty in the firing of the timer. The bias will give us 6372 * exactly the 1.5 tick we need. But, because the bias is 6373 * statistical, we have to test that we don't drop below the minimum 6374 * feasible timer (which is 2 ticks). 6375 */ 6376 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 6377 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2), 6378 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 6379 6380 /* 6381 * We received an ack for a packet that wasn't retransmitted; it is 6382 * probably safe to discard any error indications we've received 6383 * recently. This isn't quite right, but close enough for now (a 6384 * route might have failed after we sent a segment, and the return 6385 * path might not be symmetrical). 6386 */ 6387 tp->t_softerror = 0; 6388 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 6389 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt) 6390 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt; 6391 } 6392 6393 static void 6394 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line) 6395 { 6396 bbr->r_ctl.rc_rtt_shrinks = cts; 6397 if (bbr_can_force_probertt && 6398 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 6399 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 6400 /* 6401 * We should enter probe-rtt its been too long 6402 * since we have been there. 6403 */ 6404 bbr_enter_probe_rtt(bbr, cts, __LINE__); 6405 } else 6406 bbr_check_probe_rtt_limits(bbr, cts); 6407 } 6408 6409 static void 6410 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts) 6411 { 6412 uint64_t orig_bw; 6413 6414 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) { 6415 /* We never apply a zero measurement */ 6416 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0, 6417 0, 0, 0, 0, 0, 0); 6418 return; 6419 } 6420 if (bbr->r_ctl.r_measurement_count < 0xffffffff) 6421 bbr->r_ctl.r_measurement_count++; 6422 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 6423 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch); 6424 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw, 6425 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate), 6426 0, 0, 0, 0, 0, 0); 6427 if (orig_bw && 6428 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) { 6429 if (bbr->bbr_hdrw_pacing) { 6430 /* 6431 * Apply a new rate to the hardware 6432 * possibly. 6433 */ 6434 bbr_update_hardware_pacing_rate(bbr, cts); 6435 } 6436 bbr_set_state_target(bbr, __LINE__); 6437 tcp_bbr_tso_size_check(bbr, cts); 6438 if (bbr->r_recovery_bw) { 6439 bbr_setup_red_bw(bbr, cts); 6440 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW); 6441 } 6442 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate)) 6443 tcp_bbr_tso_size_check(bbr, cts); 6444 } 6445 6446 static void 6447 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6448 { 6449 if (bbr->rc_in_persist == 0) { 6450 /* We log only when not in persist */ 6451 /* Translate to a Bytes Per Second */ 6452 uint64_t tim, bw, ts_diff, ts_bw; 6453 uint32_t delivered; 6454 6455 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6456 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6457 else 6458 tim = 1; 6459 /* 6460 * Now that we have processed the tim (skipping the sample 6461 * or possibly updating the time, go ahead and 6462 * calculate the cdr. 6463 */ 6464 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6465 bw = (uint64_t)delivered; 6466 bw *= (uint64_t)USECS_IN_SECOND; 6467 bw /= tim; 6468 if (bw == 0) { 6469 /* We must have a calculatable amount */ 6470 return; 6471 } 6472 /* 6473 * If we are using this b/w shove it in now so we 6474 * can see in the trace viewer if it gets over-ridden. 6475 */ 6476 if (rsm->r_ts_valid && 6477 bbr->rc_ts_valid && 6478 bbr->rc_ts_clock_set && 6479 (bbr->rc_ts_cant_be_used == 0) && 6480 bbr->rc_use_ts_limit) { 6481 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1); 6482 ts_diff *= bbr->r_ctl.bbr_peer_tsratio; 6483 if ((delivered == 0) || 6484 (rtt < 1000)) { 6485 /* Can't use the ts */ 6486 bbr_log_type_bbrupd(bbr, 61, cts, 6487 ts_diff, 6488 bbr->r_ctl.last_inbound_ts, 6489 rsm->r_del_ack_ts, 0, 6490 0, 0, 0, delivered); 6491 } else { 6492 ts_bw = (uint64_t)delivered; 6493 ts_bw *= (uint64_t)USECS_IN_SECOND; 6494 ts_bw /= ts_diff; 6495 bbr_log_type_bbrupd(bbr, 62, cts, 6496 (ts_bw >> 32), 6497 (ts_bw & 0xffffffff), 0, 0, 6498 0, 0, ts_diff, delivered); 6499 if ((bbr->ts_can_raise) && 6500 (ts_bw > bw)) { 6501 bbr_log_type_bbrupd(bbr, 8, cts, 6502 delivered, 6503 ts_diff, 6504 (bw >> 32), 6505 (bw & 0x00000000ffffffff), 6506 0, 0, 0, 0); 6507 bw = ts_bw; 6508 } else if (ts_bw && (ts_bw < bw)) { 6509 bbr_log_type_bbrupd(bbr, 7, cts, 6510 delivered, 6511 ts_diff, 6512 (bw >> 32), 6513 (bw & 0x00000000ffffffff), 6514 0, 0, 0, 0); 6515 bw = ts_bw; 6516 } 6517 } 6518 } 6519 if (rsm->r_first_sent_time && 6520 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6521 uint64_t sbw, sti; 6522 /* 6523 * We use what was in flight at the time of our 6524 * send and the size of this send to figure 6525 * out what we have been sending at (amount). 6526 * For the time we take from the time of 6527 * the send of the first send outstanding 6528 * until this send plus this sends pacing 6529 * time. This gives us a good calculation 6530 * as to the rate we have been sending at. 6531 */ 6532 6533 sbw = (uint64_t)(rsm->r_flight_at_send); 6534 sbw *= (uint64_t)USECS_IN_SECOND; 6535 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6536 sti += rsm->r_pacing_delay; 6537 sbw /= sti; 6538 if (sbw < bw) { 6539 bbr_log_type_bbrupd(bbr, 6, cts, 6540 delivered, 6541 (uint32_t)sti, 6542 (bw >> 32), 6543 (uint32_t)bw, 6544 rsm->r_first_sent_time, 0, (sbw >> 32), 6545 (uint32_t)sbw); 6546 bw = sbw; 6547 } 6548 } 6549 /* Use the google algorithm for b/w measurements */ 6550 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6551 if ((rsm->r_app_limited == 0) || 6552 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) { 6553 tcp_bbr_commit_bw(bbr, cts); 6554 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6555 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6556 } 6557 } 6558 } 6559 6560 static void 6561 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6562 { 6563 if (bbr->rc_in_persist == 0) { 6564 /* We log only when not in persist */ 6565 /* Translate to a Bytes Per Second */ 6566 uint64_t tim, bw; 6567 uint32_t delivered; 6568 int no_apply = 0; 6569 6570 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6571 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6572 else 6573 tim = 1; 6574 /* 6575 * Now that we have processed the tim (skipping the sample 6576 * or possibly updating the time, go ahead and 6577 * calculate the cdr. 6578 */ 6579 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6580 bw = (uint64_t)delivered; 6581 bw *= (uint64_t)USECS_IN_SECOND; 6582 bw /= tim; 6583 if (tim < bbr->r_ctl.rc_lowest_rtt) { 6584 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6585 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6586 6587 no_apply = 1; 6588 } 6589 /* 6590 * If we are using this b/w shove it in now so we 6591 * can see in the trace viewer if it gets over-ridden. 6592 */ 6593 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6594 /* Gate by the sending rate */ 6595 if (rsm->r_first_sent_time && 6596 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6597 uint64_t sbw, sti; 6598 /* 6599 * We use what was in flight at the time of our 6600 * send and the size of this send to figure 6601 * out what we have been sending at (amount). 6602 * For the time we take from the time of 6603 * the send of the first send outstanding 6604 * until this send plus this sends pacing 6605 * time. This gives us a good calculation 6606 * as to the rate we have been sending at. 6607 */ 6608 6609 sbw = (uint64_t)(rsm->r_flight_at_send); 6610 sbw *= (uint64_t)USECS_IN_SECOND; 6611 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6612 sti += rsm->r_pacing_delay; 6613 sbw /= sti; 6614 if (sbw < bw) { 6615 bbr_log_type_bbrupd(bbr, 6, cts, 6616 delivered, 6617 (uint32_t)sti, 6618 (bw >> 32), 6619 (uint32_t)bw, 6620 rsm->r_first_sent_time, 0, (sbw >> 32), 6621 (uint32_t)sbw); 6622 bw = sbw; 6623 } 6624 if ((sti > tim) && 6625 (sti < bbr->r_ctl.rc_lowest_rtt)) { 6626 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6627 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6628 no_apply = 1; 6629 } else 6630 no_apply = 0; 6631 } 6632 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6633 if ((no_apply == 0) && 6634 ((rsm->r_app_limited == 0) || 6635 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) { 6636 tcp_bbr_commit_bw(bbr, cts); 6637 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6638 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6639 } 6640 } 6641 } 6642 6643 static void 6644 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin, 6645 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to) 6646 { 6647 uint64_t old_rttprop; 6648 6649 /* Update our delivery time and amount */ 6650 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start); 6651 bbr->r_ctl.rc_del_time = cts; 6652 if (rtt == 0) { 6653 /* 6654 * 0 means its a retransmit, for now we don't use these for 6655 * the rest of BBR. 6656 */ 6657 return; 6658 } 6659 if ((bbr->rc_use_google == 0) && 6660 (match != BBR_RTT_BY_EXACTMATCH) && 6661 (match != BBR_RTT_BY_TIMESTAMP)){ 6662 /* 6663 * We get a lot of rtt updates, lets not pay attention to 6664 * any that are not an exact match. That way we don't have 6665 * to worry about timestamps and the whole nonsense of 6666 * unsure if its a retransmission etc (if we ever had the 6667 * timestamp fixed to always have the last thing sent this 6668 * would not be a issue). 6669 */ 6670 return; 6671 } 6672 if ((bbr_no_retran && bbr->rc_use_google) && 6673 (match != BBR_RTT_BY_EXACTMATCH) && 6674 (match != BBR_RTT_BY_TIMESTAMP)){ 6675 /* 6676 * We only do measurements in google mode 6677 * with bbr_no_retran on for sure things. 6678 */ 6679 return; 6680 } 6681 /* Only update srtt if we know by exact match */ 6682 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin); 6683 if (ack_type == BBR_CUM_ACKED) 6684 bbr->rc_ack_is_cumack = 1; 6685 else 6686 bbr->rc_ack_is_cumack = 0; 6687 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP); 6688 /* 6689 * Note the following code differs to the original 6690 * BBR spec. It calls for <= not <. However after a 6691 * long discussion in email with Neal, he acknowledged 6692 * that it should be < than so that we will have flows 6693 * going into probe-rtt (we were seeing cases where that 6694 * did not happen and caused ugly things to occur). We 6695 * have added this agreed upon fix to our code base. 6696 */ 6697 if (rtt < old_rttprop) { 6698 /* Update when we last saw a rtt drop */ 6699 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0); 6700 bbr_set_reduced_rtt(bbr, cts, __LINE__); 6701 } 6702 bbr_log_type_bbrrttprop(bbr, rtt, rsm->r_end, uts, cts, 6703 match, rsm->r_start, rsm->r_flags); 6704 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6705 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) { 6706 /* 6707 * The RTT-prop moved, reset the target (may be a 6708 * nop for some states). 6709 */ 6710 bbr_set_state_target(bbr, __LINE__); 6711 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) 6712 bbr_log_rtt_shrinks(bbr, cts, 0, 0, 6713 __LINE__, BBR_RTTS_NEW_TARGET, 0); 6714 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP)) 6715 /* It went up */ 6716 bbr_check_probe_rtt_limits(bbr, cts); 6717 } 6718 if ((bbr->rc_use_google == 0) && 6719 (match == BBR_RTT_BY_TIMESTAMP)) { 6720 /* 6721 * We don't do b/w update with 6722 * these since they are not really 6723 * reliable. 6724 */ 6725 return; 6726 } 6727 if (bbr->r_ctl.r_app_limited_until && 6728 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) { 6729 /* We are no longer app-limited */ 6730 bbr->r_ctl.r_app_limited_until = 0; 6731 } 6732 if (bbr->rc_use_google) { 6733 bbr_google_measurement(bbr, rsm, rtt, cts); 6734 } else { 6735 bbr_nf_measurement(bbr, rsm, rtt, cts); 6736 } 6737 } 6738 6739 /* 6740 * Convert a timestamp that the main stack 6741 * uses (milliseconds) into one that bbr uses 6742 * (microseconds). Return that converted timestamp. 6743 */ 6744 static uint32_t 6745 bbr_ts_convert(uint32_t cts) { 6746 uint32_t sec, msec; 6747 6748 sec = cts / MS_IN_USEC; 6749 msec = cts - (MS_IN_USEC * sec); 6750 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC)); 6751 } 6752 6753 /* 6754 * Return 0 if we did not update the RTT time, return 6755 * 1 if we did. 6756 */ 6757 static int 6758 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, 6759 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack) 6760 { 6761 int32_t i; 6762 uint32_t t, uts = 0; 6763 6764 if ((rsm->r_flags & BBR_ACKED) || 6765 (rsm->r_flags & BBR_WAS_RENEGED) || 6766 (rsm->r_flags & BBR_RXT_CLEARED)) { 6767 /* Already done */ 6768 return (0); 6769 } 6770 if (rsm->r_rtt_not_allowed) { 6771 /* Not allowed */ 6772 return (0); 6773 } 6774 if (rsm->r_rtr_cnt == 1) { 6775 /* 6776 * Only one transmit. Hopefully the normal case. 6777 */ 6778 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0])) 6779 t = cts - rsm->r_tim_lastsent[0]; 6780 else 6781 t = 1; 6782 bbr->r_ctl.rc_last_rtt = t; 6783 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6784 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to); 6785 return (1); 6786 } 6787 /* Convert to usecs */ 6788 if ((bbr_can_use_ts_for_rtt == 1) && 6789 (bbr->rc_use_google == 1) && 6790 (ack_type == BBR_CUM_ACKED) && 6791 (to->to_flags & TOF_TS) && 6792 (to->to_tsecr != 0)) { 6793 t = tcp_tv_to_msec(&bbr->rc_tv) - to->to_tsecr; 6794 if (t < 1) 6795 t = 1; 6796 t *= MS_IN_USEC; 6797 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6798 BBR_RTT_BY_TIMESTAMP, 6799 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)], 6800 ack_type, to); 6801 return (1); 6802 } 6803 uts = bbr_ts_convert(to->to_tsecr); 6804 if ((to->to_flags & TOF_TS) && 6805 (to->to_tsecr != 0) && 6806 (ack_type == BBR_CUM_ACKED) && 6807 ((rsm->r_flags & BBR_OVERMAX) == 0)) { 6808 /* 6809 * Now which timestamp does it match? In this block the ACK 6810 * may be coming from a previous transmission. 6811 */ 6812 uint32_t fudge; 6813 6814 fudge = BBR_TIMER_FUDGE; 6815 for (i = 0; i < rsm->r_rtr_cnt; i++) { 6816 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) && 6817 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) { 6818 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6819 t = cts - rsm->r_tim_lastsent[i]; 6820 else 6821 t = 1; 6822 bbr->r_ctl.rc_last_rtt = t; 6823 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING, 6824 rsm->r_tim_lastsent[i], ack_type, to); 6825 if ((i + 1) < rsm->r_rtr_cnt) { 6826 /* Likely */ 6827 return (0); 6828 } else if (rsm->r_flags & BBR_TLP) { 6829 bbr->rc_tlp_rtx_out = 0; 6830 } 6831 return (1); 6832 } 6833 } 6834 /* Fall through if we can't find a matching timestamp */ 6835 } 6836 /* 6837 * Ok its a SACK block that we retransmitted. or a windows 6838 * machine without timestamps. We can tell nothing from the 6839 * time-stamp since its not there or the time the peer last 6840 * received a segment that moved forward its cum-ack point. 6841 * 6842 * Lets look at the last retransmit and see what we can tell 6843 * (with BBR for space we only keep 2 note we have to keep 6844 * at least 2 so the map can not be condensed more). 6845 */ 6846 i = rsm->r_rtr_cnt - 1; 6847 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6848 t = cts - rsm->r_tim_lastsent[i]; 6849 else 6850 goto not_sure; 6851 if (t < bbr->r_ctl.rc_lowest_rtt) { 6852 /* 6853 * We retransmitted and the ack came back in less 6854 * than the smallest rtt we have observed in the 6855 * windowed rtt. We most likey did an improper 6856 * retransmit as outlined in 4.2 Step 3 point 2 in 6857 * the rack-draft. 6858 * 6859 * Use the prior transmission to update all the 6860 * information as long as there is only one prior 6861 * transmission. 6862 */ 6863 if ((rsm->r_flags & BBR_OVERMAX) == 0) { 6864 #ifdef BBR_INVARIANTS 6865 if (rsm->r_rtr_cnt == 1) 6866 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags); 6867 #endif 6868 i = rsm->r_rtr_cnt - 2; 6869 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6870 t = cts - rsm->r_tim_lastsent[i]; 6871 else 6872 t = 1; 6873 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET, 6874 rsm->r_tim_lastsent[i], ack_type, to); 6875 return (0); 6876 } else { 6877 /* 6878 * Too many prior transmissions, just 6879 * updated BBR delivered 6880 */ 6881 not_sure: 6882 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6883 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6884 } 6885 } else { 6886 /* 6887 * We retransmitted it and the retransmit did the 6888 * job. 6889 */ 6890 if (rsm->r_flags & BBR_TLP) 6891 bbr->rc_tlp_rtx_out = 0; 6892 if ((rsm->r_flags & BBR_OVERMAX) == 0) 6893 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, 6894 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to); 6895 else 6896 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6897 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6898 return (1); 6899 } 6900 return (0); 6901 } 6902 6903 /* 6904 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 6905 */ 6906 static void 6907 bbr_log_sack_passed(struct tcpcb *tp, 6908 struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 6909 { 6910 struct bbr_sendmap *nrsm; 6911 6912 nrsm = rsm; 6913 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap, 6914 bbr_head, r_tnext) { 6915 if (nrsm == rsm) { 6916 /* Skip original segment he is acked */ 6917 continue; 6918 } 6919 if (nrsm->r_flags & BBR_ACKED) { 6920 /* Skip ack'd segments */ 6921 continue; 6922 } 6923 if (nrsm->r_flags & BBR_SACK_PASSED) { 6924 /* 6925 * We found one that is already marked 6926 * passed, we have been here before and 6927 * so all others below this are marked. 6928 */ 6929 break; 6930 } 6931 BBR_STAT_INC(bbr_sack_passed); 6932 nrsm->r_flags |= BBR_SACK_PASSED; 6933 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) && 6934 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) { 6935 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start; 6936 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start; 6937 nrsm->r_flags |= BBR_MARKED_LOST; 6938 } 6939 nrsm->r_flags &= ~BBR_WAS_SACKPASS; 6940 } 6941 } 6942 6943 /* 6944 * Returns the number of bytes that were 6945 * newly ack'd by sack blocks. 6946 */ 6947 static uint32_t 6948 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack, 6949 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts) 6950 { 6951 int32_t times = 0; 6952 uint32_t start, end, changed = 0; 6953 struct bbr_sendmap *rsm, *nrsm; 6954 int32_t used_ref = 1; 6955 uint8_t went_back = 0, went_fwd = 0; 6956 6957 start = sack->start; 6958 end = sack->end; 6959 rsm = *prsm; 6960 if (rsm == NULL) 6961 used_ref = 0; 6962 6963 /* Do we locate the block behind where we last were? */ 6964 if (rsm && SEQ_LT(start, rsm->r_start)) { 6965 went_back = 1; 6966 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 6967 if (SEQ_GEQ(start, rsm->r_start) && 6968 SEQ_LT(start, rsm->r_end)) { 6969 goto do_rest_ofb; 6970 } 6971 } 6972 } 6973 start_at_beginning: 6974 went_fwd = 1; 6975 /* 6976 * Ok lets locate the block where this guy is fwd from rsm (if its 6977 * set) 6978 */ 6979 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) { 6980 if (SEQ_GEQ(start, rsm->r_start) && 6981 SEQ_LT(start, rsm->r_end)) { 6982 break; 6983 } 6984 } 6985 do_rest_ofb: 6986 if (rsm == NULL) { 6987 /* 6988 * This happens when we get duplicate sack blocks with the 6989 * same end. For example SACK 4: 100 SACK 3: 100 The sort 6990 * will not change there location so we would just start at 6991 * the end of the first one and get lost. 6992 */ 6993 if (tp->t_flags & TF_SENTFIN) { 6994 /* 6995 * Check to see if we have not logged the FIN that 6996 * went out. 6997 */ 6998 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 6999 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 7000 /* 7001 * Ok we did not get the FIN logged. 7002 */ 7003 nrsm->r_end++; 7004 rsm = nrsm; 7005 goto do_rest_ofb; 7006 } 7007 } 7008 if (times == 1) { 7009 #ifdef BBR_INVARIANTS 7010 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p", 7011 tp, bbr, sack, to, prsm); 7012 #else 7013 goto out; 7014 #endif 7015 } 7016 times++; 7017 BBR_STAT_INC(bbr_sack_proc_restart); 7018 rsm = NULL; 7019 goto start_at_beginning; 7020 } 7021 /* Ok we have an ACK for some piece of rsm */ 7022 if (rsm->r_start != start) { 7023 /* 7024 * Need to split this in two pieces the before and after. 7025 */ 7026 if (bbr_sack_mergable(rsm, start, end)) 7027 nrsm = bbr_alloc_full_limit(bbr); 7028 else 7029 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7030 if (nrsm == NULL) { 7031 /* We could not allocate ignore the sack */ 7032 struct sackblk blk; 7033 7034 blk.start = start; 7035 blk.end = end; 7036 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7037 goto out; 7038 } 7039 bbr_clone_rsm(bbr, nrsm, rsm, start); 7040 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7041 if (rsm->r_in_tmap) { 7042 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7043 nrsm->r_in_tmap = 1; 7044 } 7045 rsm->r_flags &= (~BBR_HAS_FIN); 7046 rsm = nrsm; 7047 } 7048 if (SEQ_GEQ(end, rsm->r_end)) { 7049 /* 7050 * The end of this block is either beyond this guy or right 7051 * at this guy. 7052 */ 7053 if ((rsm->r_flags & BBR_ACKED) == 0) { 7054 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7055 changed += (rsm->r_end - rsm->r_start); 7056 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7057 bbr_log_sack_passed(tp, bbr, rsm); 7058 if (rsm->r_flags & BBR_MARKED_LOST) { 7059 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7060 } 7061 /* Is Reordering occuring? */ 7062 if (rsm->r_flags & BBR_SACK_PASSED) { 7063 BBR_STAT_INC(bbr_reorder_seen); 7064 bbr->r_ctl.rc_reorder_ts = cts; 7065 if (rsm->r_flags & BBR_MARKED_LOST) { 7066 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7067 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7068 /* LT sampling also needs adjustment */ 7069 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7070 } 7071 } 7072 rsm->r_flags |= BBR_ACKED; 7073 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7074 if (rsm->r_in_tmap) { 7075 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7076 rsm->r_in_tmap = 0; 7077 } 7078 } 7079 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7080 if (end == rsm->r_end) { 7081 /* This block only - done */ 7082 goto out; 7083 } 7084 /* There is more not coverend by this rsm move on */ 7085 start = rsm->r_end; 7086 nrsm = TAILQ_NEXT(rsm, r_next); 7087 rsm = nrsm; 7088 times = 0; 7089 goto do_rest_ofb; 7090 } 7091 if (rsm->r_flags & BBR_ACKED) { 7092 /* Been here done that */ 7093 goto out; 7094 } 7095 /* Ok we need to split off this one at the tail */ 7096 if (bbr_sack_mergable(rsm, start, end)) 7097 nrsm = bbr_alloc_full_limit(bbr); 7098 else 7099 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7100 if (nrsm == NULL) { 7101 /* failed XXXrrs what can we do but loose the sack info? */ 7102 struct sackblk blk; 7103 7104 blk.start = start; 7105 blk.end = end; 7106 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7107 goto out; 7108 } 7109 /* Clone it */ 7110 bbr_clone_rsm(bbr, nrsm, rsm, end); 7111 /* The sack block does not cover this guy fully */ 7112 rsm->r_flags &= (~BBR_HAS_FIN); 7113 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7114 if (rsm->r_in_tmap) { 7115 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7116 nrsm->r_in_tmap = 1; 7117 } 7118 nrsm->r_dupack = 0; 7119 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7120 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7121 changed += (rsm->r_end - rsm->r_start); 7122 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7123 bbr_log_sack_passed(tp, bbr, rsm); 7124 /* Is Reordering occuring? */ 7125 if (rsm->r_flags & BBR_MARKED_LOST) { 7126 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7127 } 7128 if (rsm->r_flags & BBR_SACK_PASSED) { 7129 BBR_STAT_INC(bbr_reorder_seen); 7130 bbr->r_ctl.rc_reorder_ts = cts; 7131 if (rsm->r_flags & BBR_MARKED_LOST) { 7132 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7133 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7134 /* LT sampling also needs adjustment */ 7135 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7136 } 7137 } 7138 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7139 rsm->r_flags |= BBR_ACKED; 7140 if (rsm->r_in_tmap) { 7141 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7142 rsm->r_in_tmap = 0; 7143 } 7144 out: 7145 if (rsm && (rsm->r_flags & BBR_ACKED)) { 7146 /* 7147 * Now can we merge this newly acked 7148 * block with either the previous or 7149 * next block? 7150 */ 7151 nrsm = TAILQ_NEXT(rsm, r_next); 7152 if (nrsm && 7153 (nrsm->r_flags & BBR_ACKED)) { 7154 /* yep this and next can be merged */ 7155 rsm = bbr_merge_rsm(bbr, rsm, nrsm); 7156 } 7157 /* Now what about the previous? */ 7158 nrsm = TAILQ_PREV(rsm, bbr_head, r_next); 7159 if (nrsm && 7160 (nrsm->r_flags & BBR_ACKED)) { 7161 /* yep the previous and this can be merged */ 7162 rsm = bbr_merge_rsm(bbr, nrsm, rsm); 7163 } 7164 } 7165 if (used_ref == 0) { 7166 BBR_STAT_INC(bbr_sack_proc_all); 7167 } else { 7168 BBR_STAT_INC(bbr_sack_proc_short); 7169 } 7170 if (went_fwd && went_back) { 7171 BBR_STAT_INC(bbr_sack_search_both); 7172 } else if (went_fwd) { 7173 BBR_STAT_INC(bbr_sack_search_fwd); 7174 } else if (went_back) { 7175 BBR_STAT_INC(bbr_sack_search_back); 7176 } 7177 /* Save off where the next seq is */ 7178 if (rsm) 7179 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 7180 else 7181 bbr->r_ctl.rc_sacklast = NULL; 7182 *prsm = rsm; 7183 return (changed); 7184 } 7185 7186 static void inline 7187 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack) 7188 { 7189 struct bbr_sendmap *tmap; 7190 7191 BBR_STAT_INC(bbr_reneges_seen); 7192 tmap = NULL; 7193 while (rsm && (rsm->r_flags & BBR_ACKED)) { 7194 /* Its no longer sacked, mark it so */ 7195 uint32_t oflags; 7196 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7197 #ifdef BBR_INVARIANTS 7198 if (rsm->r_in_tmap) { 7199 panic("bbr:%p rsm:%p flags:0x%x in tmap?", 7200 bbr, rsm, rsm->r_flags); 7201 } 7202 #endif 7203 oflags = rsm->r_flags; 7204 if (rsm->r_flags & BBR_MARKED_LOST) { 7205 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7206 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7207 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7208 /* LT sampling also needs adjustment */ 7209 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7210 } 7211 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST); 7212 rsm->r_flags |= BBR_WAS_RENEGED; 7213 rsm->r_flags |= BBR_RXT_CLEARED; 7214 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__); 7215 /* Rebuild it into our tmap */ 7216 if (tmap == NULL) { 7217 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7218 tmap = rsm; 7219 } else { 7220 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext); 7221 tmap = rsm; 7222 } 7223 tmap->r_in_tmap = 1; 7224 /* 7225 * XXXrrs Delivered? Should we do anything here? 7226 * 7227 * Of course we don't on a rxt timeout so maybe its ok that 7228 * we don't? 7229 * 7230 * For now lets not. 7231 */ 7232 rsm = TAILQ_NEXT(rsm, r_next); 7233 } 7234 /* 7235 * Now lets possibly clear the sack filter so we start recognizing 7236 * sacks that cover this area. 7237 */ 7238 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack); 7239 } 7240 7241 static void 7242 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to) 7243 { 7244 struct tcp_bbr *bbr; 7245 struct bbr_sendmap *rsm; 7246 uint32_t cts; 7247 7248 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7249 cts = bbr->r_ctl.rc_rcvtime; 7250 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7251 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) { 7252 if ((rsm->r_end - rsm->r_start) <= 1) { 7253 /* Log out the SYN completely */ 7254 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7255 rsm->r_rtr_bytes = 0; 7256 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7257 if (rsm->r_in_tmap) { 7258 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7259 rsm->r_in_tmap = 0; 7260 } 7261 if (bbr->r_ctl.rc_next == rsm) { 7262 /* scoot along the marker */ 7263 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7264 } 7265 if (to != NULL) 7266 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0); 7267 bbr_free(bbr, rsm); 7268 } else { 7269 /* There is more (Fast open)? strip out SYN. */ 7270 rsm->r_flags &= ~BBR_HAS_SYN; 7271 rsm->r_start++; 7272 } 7273 } 7274 } 7275 7276 /* 7277 * Returns the number of bytes that were 7278 * acknowledged by SACK blocks. 7279 */ 7280 7281 static uint32_t 7282 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, 7283 uint32_t *prev_acked) 7284 { 7285 uint32_t changed, last_seq, entered_recovery = 0; 7286 struct tcp_bbr *bbr; 7287 struct bbr_sendmap *rsm; 7288 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 7289 register uint32_t th_ack; 7290 int32_t i, j, k, new_sb, num_sack_blks = 0; 7291 uint32_t cts, acked, ack_point, sack_changed = 0; 7292 uint32_t p_maxseg, maxseg, p_acked = 0; 7293 7294 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7295 if (tcp_get_flags(th) & TH_RST) { 7296 /* We don't log resets */ 7297 return (0); 7298 } 7299 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7300 cts = bbr->r_ctl.rc_rcvtime; 7301 7302 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7303 changed = 0; 7304 maxseg = tp->t_maxseg - bbr->rc_last_options; 7305 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg); 7306 th_ack = th->th_ack; 7307 if (SEQ_GT(th_ack, tp->snd_una)) { 7308 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__); 7309 bbr->rc_tp->t_acktime = ticks; 7310 } 7311 if (SEQ_LEQ(th_ack, tp->snd_una)) { 7312 /* Only sent here for sack processing */ 7313 goto proc_sack; 7314 } 7315 if (rsm && SEQ_GT(th_ack, rsm->r_start)) { 7316 changed = th_ack - rsm->r_start; 7317 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) { 7318 /* 7319 * For the SYN incoming case we will not have called 7320 * tcp_output for the sending of the SYN, so there will be 7321 * no map. All other cases should probably be a panic. 7322 */ 7323 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) { 7324 /* 7325 * We have a timestamp that can be used to generate 7326 * an initial RTT. 7327 */ 7328 uint32_t ts, now, rtt; 7329 7330 ts = bbr_ts_convert(to->to_tsecr); 7331 now = bbr_ts_convert(tcp_tv_to_msec(&bbr->rc_tv)); 7332 rtt = now - ts; 7333 if (rtt < 1) 7334 rtt = 1; 7335 bbr_log_type_bbrrttprop(bbr, rtt, 7336 tp->iss, 0, cts, 7337 BBR_RTT_BY_TIMESTAMP, tp->iss, 0); 7338 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 7339 changed = 1; 7340 bbr->r_wanted_output = 1; 7341 goto out; 7342 } 7343 goto proc_sack; 7344 } else if (rsm == NULL) { 7345 goto out; 7346 } 7347 if (changed) { 7348 /* 7349 * The ACK point is advancing to th_ack, we must drop off 7350 * the packets in the rack log and calculate any eligble 7351 * RTT's. 7352 */ 7353 bbr->r_wanted_output = 1; 7354 more: 7355 if (rsm == NULL) { 7356 if (tp->t_flags & TF_SENTFIN) { 7357 /* if we send a FIN we will not hav a map */ 7358 goto proc_sack; 7359 } 7360 #ifdef BBR_INVARIANTS 7361 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n", 7362 tp, 7363 th, tp->t_state, bbr, 7364 tp->snd_una, tp->snd_max, changed); 7365 #endif 7366 goto proc_sack; 7367 } 7368 } 7369 if (SEQ_LT(th_ack, rsm->r_start)) { 7370 /* Huh map is missing this */ 7371 #ifdef BBR_INVARIANTS 7372 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n", 7373 rsm->r_start, 7374 th_ack, tp->t_state, 7375 bbr->r_state, bbr); 7376 panic("th-ack is bad bbr:%p tp:%p", bbr, tp); 7377 #endif 7378 goto proc_sack; 7379 } else if (th_ack == rsm->r_start) { 7380 /* None here to ack */ 7381 goto proc_sack; 7382 } 7383 /* 7384 * Clear the dup ack counter, it will 7385 * either be freed or if there is some 7386 * remaining we need to start it at zero. 7387 */ 7388 rsm->r_dupack = 0; 7389 /* Now do we consume the whole thing? */ 7390 if (SEQ_GEQ(th_ack, rsm->r_end)) { 7391 /* Its all consumed. */ 7392 uint32_t left; 7393 7394 if (rsm->r_flags & BBR_ACKED) { 7395 /* 7396 * It was acked on the scoreboard -- remove it from 7397 * total 7398 */ 7399 p_acked += (rsm->r_end - rsm->r_start); 7400 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7401 if (bbr->r_ctl.rc_sacked == 0) 7402 bbr->r_ctl.rc_sacklast = NULL; 7403 } else { 7404 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack); 7405 if (rsm->r_flags & BBR_MARKED_LOST) { 7406 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7407 } 7408 if (rsm->r_flags & BBR_SACK_PASSED) { 7409 /* 7410 * There are acked segments ACKED on the 7411 * scoreboard further up. We are seeing 7412 * reordering. 7413 */ 7414 BBR_STAT_INC(bbr_reorder_seen); 7415 bbr->r_ctl.rc_reorder_ts = cts; 7416 if (rsm->r_flags & BBR_MARKED_LOST) { 7417 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7418 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7419 /* LT sampling also needs adjustment */ 7420 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7421 } 7422 } 7423 rsm->r_flags &= ~BBR_MARKED_LOST; 7424 } 7425 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7426 rsm->r_rtr_bytes = 0; 7427 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7428 if (rsm->r_in_tmap) { 7429 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7430 rsm->r_in_tmap = 0; 7431 } 7432 if (bbr->r_ctl.rc_next == rsm) { 7433 /* scoot along the marker */ 7434 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7435 } 7436 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7437 /* Adjust the packet counts */ 7438 left = th_ack - rsm->r_end; 7439 /* Free back to zone */ 7440 bbr_free(bbr, rsm); 7441 if (left) { 7442 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7443 goto more; 7444 } 7445 goto proc_sack; 7446 } 7447 if (rsm->r_flags & BBR_ACKED) { 7448 /* 7449 * It was acked on the scoreboard -- remove it from total 7450 * for the part being cum-acked. 7451 */ 7452 p_acked += (rsm->r_end - rsm->r_start); 7453 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 7454 if (bbr->r_ctl.rc_sacked == 0) 7455 bbr->r_ctl.rc_sacklast = NULL; 7456 } else { 7457 /* 7458 * It was acked up to th_ack point for the first time 7459 */ 7460 struct bbr_sendmap lrsm; 7461 7462 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap)); 7463 lrsm.r_end = th_ack; 7464 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack); 7465 } 7466 if ((rsm->r_flags & BBR_MARKED_LOST) && 7467 ((rsm->r_flags & BBR_ACKED) == 0)) { 7468 /* 7469 * It was marked lost and partly ack'd now 7470 * for the first time. We lower the rc_lost_bytes 7471 * and still leave it MARKED. 7472 */ 7473 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start; 7474 } 7475 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7476 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7477 rsm->r_rtr_bytes = 0; 7478 /* adjust packet count */ 7479 rsm->r_start = th_ack; 7480 proc_sack: 7481 /* Check for reneging */ 7482 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7483 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) { 7484 /* 7485 * The peer has moved snd_una up to the edge of this send, 7486 * i.e. one that it had previously acked. The only way that 7487 * can be true if the peer threw away data (space issues) 7488 * that it had previously sacked (else it would have given 7489 * us snd_una up to (rsm->r_end). We need to undo the acked 7490 * markings here. 7491 * 7492 * Note we have to look to make sure th_ack is our 7493 * rsm->r_start in case we get an old ack where th_ack is 7494 * behind snd_una. 7495 */ 7496 bbr_peer_reneges(bbr, rsm, th->th_ack); 7497 } 7498 if ((to->to_flags & TOF_SACK) == 0) { 7499 /* We are done nothing left to log */ 7500 goto out; 7501 } 7502 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7503 if (rsm) { 7504 last_seq = rsm->r_end; 7505 } else { 7506 last_seq = tp->snd_max; 7507 } 7508 /* Sack block processing */ 7509 if (SEQ_GT(th_ack, tp->snd_una)) 7510 ack_point = th_ack; 7511 else 7512 ack_point = tp->snd_una; 7513 for (i = 0; i < to->to_nsacks; i++) { 7514 bcopy((to->to_sacks + i * TCPOLEN_SACK), 7515 &sack, sizeof(sack)); 7516 sack.start = ntohl(sack.start); 7517 sack.end = ntohl(sack.end); 7518 if (SEQ_GT(sack.end, sack.start) && 7519 SEQ_GT(sack.start, ack_point) && 7520 SEQ_LT(sack.start, tp->snd_max) && 7521 SEQ_GT(sack.end, ack_point) && 7522 SEQ_LEQ(sack.end, tp->snd_max)) { 7523 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) && 7524 (SEQ_LT(sack.end, last_seq)) && 7525 ((sack.end - sack.start) < (p_maxseg / 8))) { 7526 /* 7527 * Not the last piece and its smaller than 7528 * 1/8th of a p_maxseg. We ignore this. 7529 */ 7530 BBR_STAT_INC(bbr_runt_sacks); 7531 continue; 7532 } 7533 sack_blocks[num_sack_blks] = sack; 7534 num_sack_blks++; 7535 } else if (SEQ_LEQ(sack.start, th_ack) && 7536 SEQ_LEQ(sack.end, th_ack)) { 7537 /* 7538 * Its a D-SACK block. 7539 */ 7540 tcp_record_dsack(tp, sack.start, sack.end, 0); 7541 } 7542 } 7543 if (num_sack_blks == 0) 7544 goto out; 7545 /* 7546 * Sort the SACK blocks so we can update the rack scoreboard with 7547 * just one pass. 7548 */ 7549 new_sb = sack_filter_blks(tp, &bbr->r_ctl.bbr_sf, sack_blocks, 7550 num_sack_blks, th->th_ack); 7551 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks); 7552 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks); 7553 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb)); 7554 num_sack_blks = new_sb; 7555 if (num_sack_blks < 2) { 7556 goto do_sack_work; 7557 } 7558 /* Sort the sacks */ 7559 for (i = 0; i < num_sack_blks; i++) { 7560 for (j = i + 1; j < num_sack_blks; j++) { 7561 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 7562 sack = sack_blocks[i]; 7563 sack_blocks[i] = sack_blocks[j]; 7564 sack_blocks[j] = sack; 7565 } 7566 } 7567 } 7568 /* 7569 * Now are any of the sack block ends the same (yes some 7570 * implememtations send these)? 7571 */ 7572 again: 7573 if (num_sack_blks > 1) { 7574 for (i = 0; i < num_sack_blks; i++) { 7575 for (j = i + 1; j < num_sack_blks; j++) { 7576 if (sack_blocks[i].end == sack_blocks[j].end) { 7577 /* 7578 * Ok these two have the same end we 7579 * want the smallest end and then 7580 * throw away the larger and start 7581 * again. 7582 */ 7583 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 7584 /* 7585 * The second block covers 7586 * more area use that 7587 */ 7588 sack_blocks[i].start = sack_blocks[j].start; 7589 } 7590 /* 7591 * Now collapse out the dup-sack and 7592 * lower the count 7593 */ 7594 for (k = (j + 1); k < num_sack_blks; k++) { 7595 sack_blocks[j].start = sack_blocks[k].start; 7596 sack_blocks[j].end = sack_blocks[k].end; 7597 j++; 7598 } 7599 num_sack_blks--; 7600 goto again; 7601 } 7602 } 7603 } 7604 } 7605 do_sack_work: 7606 rsm = bbr->r_ctl.rc_sacklast; 7607 for (i = 0; i < num_sack_blks; i++) { 7608 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts); 7609 if (acked) { 7610 bbr->r_wanted_output = 1; 7611 changed += acked; 7612 sack_changed += acked; 7613 } 7614 } 7615 out: 7616 *prev_acked = p_acked; 7617 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 7618 /* 7619 * Ok we have a high probability that we need to go in to 7620 * recovery since we have data sack'd 7621 */ 7622 struct bbr_sendmap *rsm; 7623 7624 rsm = bbr_check_recovery_mode(tp, bbr, cts); 7625 if (rsm) { 7626 /* Enter recovery */ 7627 entered_recovery = 1; 7628 bbr->r_wanted_output = 1; 7629 /* 7630 * When we enter recovery we need to assure we send 7631 * one packet. 7632 */ 7633 if (bbr->r_ctl.rc_resend == NULL) { 7634 bbr->r_ctl.rc_resend = rsm; 7635 } 7636 } 7637 } 7638 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 7639 /* 7640 * See if we need to rack-retransmit anything if so set it 7641 * up as the thing to resend assuming something else is not 7642 * already in that position. 7643 */ 7644 if (bbr->r_ctl.rc_resend == NULL) { 7645 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 7646 } 7647 } 7648 /* 7649 * We return the amount that changed via sack, this is used by the 7650 * ack-received code to augment what was changed between th_ack <-> 7651 * snd_una. 7652 */ 7653 return (sack_changed); 7654 } 7655 7656 static void 7657 bbr_strike_dupack(struct tcp_bbr *bbr) 7658 { 7659 struct bbr_sendmap *rsm; 7660 7661 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 7662 if (rsm && (rsm->r_dupack < 0xff)) { 7663 rsm->r_dupack++; 7664 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) 7665 bbr->r_wanted_output = 1; 7666 } 7667 } 7668 7669 /* 7670 * Return value of 1, we do not need to call bbr_process_data(). 7671 * return value of 0, bbr_process_data can be called. 7672 * For ret_val if its 0 the TCB is locked and valid, if its non-zero 7673 * its unlocked and probably unsafe to touch the TCB. 7674 */ 7675 static int 7676 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 7677 struct tcpcb *tp, struct tcpopt *to, 7678 uint32_t tiwin, int32_t tlen, 7679 int32_t * ofia, int32_t thflags, int32_t * ret_val) 7680 { 7681 int32_t ourfinisacked = 0; 7682 int32_t acked_amount; 7683 uint16_t nsegs; 7684 int32_t acked; 7685 uint32_t lost, sack_changed = 0; 7686 struct mbuf *mfree; 7687 struct tcp_bbr *bbr; 7688 uint32_t prev_acked = 0; 7689 7690 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7691 lost = bbr->r_ctl.rc_lost; 7692 nsegs = max(1, m->m_pkthdr.lro_nsegs); 7693 if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) { 7694 /* Checking SEG.ACK against ISS is definitely redundant. */ 7695 tp->t_flags2 |= TF2_NO_ISS_CHECK; 7696 } 7697 if (!V_tcp_insecure_ack) { 7698 tcp_seq seq_min; 7699 bool ghost_ack_check; 7700 7701 if (tp->t_flags2 & TF2_NO_ISS_CHECK) { 7702 /* Check for too old ACKs (RFC 5961, Section 5.2). */ 7703 seq_min = tp->snd_una - tp->max_sndwnd; 7704 ghost_ack_check = false; 7705 } else { 7706 if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) { 7707 /* Checking for ghost ACKs is stricter. */ 7708 seq_min = tp->iss + 1; 7709 ghost_ack_check = true; 7710 } else { 7711 /* 7712 * Checking for too old ACKs (RFC 5961, 7713 * Section 5.2) is stricter. 7714 */ 7715 seq_min = tp->snd_una - tp->max_sndwnd; 7716 ghost_ack_check = false; 7717 } 7718 } 7719 if (SEQ_LT(th->th_ack, seq_min)) { 7720 if (ghost_ack_check) 7721 TCPSTAT_INC(tcps_rcvghostack); 7722 else 7723 TCPSTAT_INC(tcps_rcvacktooold); 7724 /* Send challenge ACK. */ 7725 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7726 bbr->r_wanted_output = 1; 7727 return (1); 7728 } 7729 } 7730 if (SEQ_GT(th->th_ack, tp->snd_max)) { 7731 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7732 bbr->r_wanted_output = 1; 7733 return (1); 7734 } 7735 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 7736 /* Process the ack */ 7737 if (bbr->rc_in_persist) 7738 tp->t_rxtshift = 0; 7739 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd)) 7740 bbr_strike_dupack(bbr); 7741 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 7742 } 7743 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost)); 7744 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 7745 /* 7746 * Old ack, behind the last one rcv'd or a duplicate ack 7747 * with SACK info. 7748 */ 7749 if (th->th_ack == tp->snd_una) { 7750 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0); 7751 if (bbr->r_state == TCPS_SYN_SENT) { 7752 /* 7753 * Special case on where we sent SYN. When 7754 * the SYN-ACK is processed in syn_sent 7755 * state it bumps the snd_una. This causes 7756 * us to hit here even though we did ack 1 7757 * byte. 7758 * 7759 * Go through the nothing left case so we 7760 * send data. 7761 */ 7762 goto nothing_left; 7763 } 7764 } 7765 return (0); 7766 } 7767 /* 7768 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 7769 * something we sent. 7770 */ 7771 if (tp->t_flags & TF_NEEDSYN) { 7772 /* 7773 * T/TCP: Connection was half-synchronized, and our SYN has 7774 * been ACK'd (so connection is now fully synchronized). Go 7775 * to non-starred state, increment snd_una for ACK of SYN, 7776 * and check if we can do window scaling. 7777 */ 7778 tp->t_flags &= ~TF_NEEDSYN; 7779 tp->snd_una++; 7780 /* Do window scaling? */ 7781 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 7782 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 7783 tp->rcv_scale = tp->request_r_scale; 7784 /* Send window already scaled. */ 7785 } 7786 } 7787 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7788 7789 acked = BYTES_THIS_ACK(tp, th); 7790 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 7791 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 7792 7793 /* 7794 * If we just performed our first retransmit, and the ACK arrives 7795 * within our recovery window, then it was a mistake to do the 7796 * retransmit in the first place. Recover our original cwnd and 7797 * ssthresh, and proceed to transmit where we left off. 7798 */ 7799 if (tp->t_flags & TF_PREVVALID) { 7800 tp->t_flags &= ~TF_PREVVALID; 7801 if (tp->t_rxtshift == 1 && 7802 (int)(ticks - tp->t_badrxtwin) < 0) 7803 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 7804 } 7805 SOCK_SENDBUF_LOCK(so); 7806 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 7807 tp->snd_wnd -= acked_amount; 7808 mfree = sbcut_locked(&so->so_snd, acked_amount); 7809 /* NB: sowwakeup_locked() does an implicit unlock. */ 7810 sowwakeup_locked(so); 7811 m_freem(mfree); 7812 if (SEQ_GT(th->th_ack, tp->snd_una)) { 7813 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 7814 } 7815 tp->snd_una = th->th_ack; 7816 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost)); 7817 if (IN_RECOVERY(tp->t_flags)) { 7818 if (SEQ_LT(th->th_ack, tp->snd_recover) && 7819 (SEQ_LT(th->th_ack, tp->snd_max))) { 7820 tcp_bbr_partialack(tp); 7821 } else { 7822 bbr_post_recovery(tp); 7823 } 7824 } 7825 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 7826 tp->snd_recover = tp->snd_una; 7827 } 7828 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 7829 tp->snd_nxt = tp->snd_max; 7830 } 7831 if (tp->snd_una == tp->snd_max) { 7832 /* Nothing left outstanding */ 7833 nothing_left: 7834 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 7835 if (sbavail(&so->so_snd) == 0) 7836 bbr->rc_tp->t_acktime = 0; 7837 if ((sbused(&so->so_snd) == 0) && 7838 (tp->t_flags & TF_SENTFIN)) { 7839 ourfinisacked = 1; 7840 } 7841 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 7842 if (bbr->rc_in_persist == 0) { 7843 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 7844 } 7845 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 7846 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 7847 /* 7848 * We invalidate the last ack here since we 7849 * don't want to transfer forward the time 7850 * for our sum's calculations. 7851 */ 7852 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7853 (sbavail(&so->so_snd) == 0) && 7854 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 7855 /* 7856 * The socket was gone and the peer sent data, time 7857 * to reset him. 7858 */ 7859 *ret_val = 1; 7860 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 7861 /* tcp_close will kill the inp pre-log the Reset */ 7862 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 7863 tp = tcp_close(tp); 7864 ctf_do_dropwithreset(m, tp, th, tlen); 7865 BBR_STAT_INC(bbr_dropped_af_data); 7866 return (1); 7867 } 7868 /* Set need output so persist might get set */ 7869 bbr->r_wanted_output = 1; 7870 } 7871 if (ofia) 7872 *ofia = ourfinisacked; 7873 return (0); 7874 } 7875 7876 static void 7877 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7878 { 7879 if (bbr->rc_in_persist == 0) { 7880 bbr_timer_cancel(bbr, __LINE__, cts); 7881 bbr->r_ctl.rc_last_delay_val = 0; 7882 tp->t_rxtshift = 0; 7883 bbr->rc_in_persist = 1; 7884 bbr->r_ctl.rc_went_idle_time = cts; 7885 /* We should be capped when rw went to 0 but just in case */ 7886 bbr_log_type_pesist(bbr, cts, 0, line, 1); 7887 /* Time freezes for the state, so do the accounting now */ 7888 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 7889 uint32_t time_in; 7890 7891 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 7892 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7893 int32_t idx; 7894 7895 idx = bbr_state_val(bbr); 7896 counter_u64_add(bbr_state_time[(idx + 5)], time_in); 7897 } else { 7898 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 7899 } 7900 } 7901 bbr->r_ctl.rc_bbr_state_time = cts; 7902 } 7903 } 7904 7905 static void 7906 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time) 7907 { 7908 /* 7909 * Note that if idle time does not exceed our 7910 * threshold, we do nothing continuing the state 7911 * transitions we were last walking through. 7912 */ 7913 if (idle_time >= bbr_idle_restart_threshold) { 7914 if (bbr->rc_use_idle_restart) { 7915 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT; 7916 /* 7917 * Set our target using BBR_UNIT, so 7918 * we increase at a dramatic rate but 7919 * we stop when we get the pipe 7920 * full again for our current b/w estimate. 7921 */ 7922 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 7923 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 7924 bbr_set_state_target(bbr, __LINE__); 7925 /* Now setup our gains to ramp up */ 7926 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 7927 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 7928 bbr_log_type_statechange(bbr, cts, __LINE__); 7929 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7930 bbr_substate_change(bbr, cts, __LINE__, 1); 7931 } 7932 } 7933 } 7934 7935 static void 7936 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7937 { 7938 uint32_t idle_time; 7939 7940 if (bbr->rc_in_persist == 0) 7941 return; 7942 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time); 7943 bbr->rc_in_persist = 0; 7944 bbr->rc_hit_state_1 = 0; 7945 bbr->r_ctl.rc_del_time = cts; 7946 /* 7947 * We invalidate the last ack here since we 7948 * don't want to transfer forward the time 7949 * for our sum's calculations. 7950 */ 7951 if (tcp_in_hpts(bbr->rc_tp)) { 7952 tcp_hpts_remove(bbr->rc_tp); 7953 bbr->rc_timer_first = 0; 7954 bbr->r_ctl.rc_hpts_flags = 0; 7955 bbr->r_ctl.rc_last_delay_val = 0; 7956 bbr->r_ctl.rc_hptsi_agg_delay = 0; 7957 bbr->r_agg_early_set = 0; 7958 bbr->r_ctl.rc_agg_early = 0; 7959 } 7960 bbr_log_type_pesist(bbr, cts, idle_time, line, 0); 7961 if (idle_time >= bbr_rtt_probe_time) { 7962 /* 7963 * This qualifies as a RTT_PROBE session since we drop the 7964 * data outstanding to nothing and waited more than 7965 * bbr_rtt_probe_time. 7966 */ 7967 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0); 7968 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts; 7969 } 7970 tp->t_rxtshift = 0; 7971 /* 7972 * If in probeBW and we have persisted more than an RTT lets do 7973 * special handling. 7974 */ 7975 /* Force a time based epoch */ 7976 bbr_set_epoch(bbr, cts, __LINE__); 7977 /* 7978 * Setup the lost so we don't count anything against the guy 7979 * we have been stuck with during persists. 7980 */ 7981 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 7982 /* Time un-freezes for the state */ 7983 bbr->r_ctl.rc_bbr_state_time = cts; 7984 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) || 7985 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) { 7986 /* 7987 * If we are going back to probe-bw 7988 * or probe_rtt, we may need to possibly 7989 * do a fast restart. 7990 */ 7991 bbr_restart_after_idle(bbr, cts, idle_time); 7992 } 7993 } 7994 7995 static void 7996 bbr_collapsed_window(struct tcp_bbr *bbr) 7997 { 7998 /* 7999 * Now we must walk the 8000 * send map and divide the 8001 * ones left stranded. These 8002 * guys can't cause us to abort 8003 * the connection and are really 8004 * "unsent". However if a buggy 8005 * client actually did keep some 8006 * of the data i.e. collapsed the win 8007 * and refused to ack and then opened 8008 * the win and acked that data. We would 8009 * get into an ack war, the simplier 8010 * method then of just pretending we 8011 * did not send those segments something 8012 * won't work. 8013 */ 8014 struct bbr_sendmap *rsm, *nrsm; 8015 tcp_seq max_seq; 8016 uint32_t maxseg; 8017 int can_split = 0; 8018 int fnd = 0; 8019 8020 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 8021 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd; 8022 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0); 8023 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 8024 /* Find the first seq past or at maxseq */ 8025 if (rsm->r_flags & BBR_RWND_COLLAPSED) 8026 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8027 if (SEQ_GEQ(max_seq, rsm->r_start) && 8028 SEQ_GEQ(rsm->r_end, max_seq)) { 8029 fnd = 1; 8030 break; 8031 } 8032 } 8033 bbr->rc_has_collapsed = 0; 8034 if (!fnd) { 8035 /* Nothing to do strange */ 8036 return; 8037 } 8038 /* 8039 * Now can we split? 8040 * 8041 * We don't want to split if splitting 8042 * would generate too many small segments 8043 * less we let an attacker fragment our 8044 * send_map and leave us out of memory. 8045 */ 8046 if ((max_seq != rsm->r_start) && 8047 (max_seq != rsm->r_end)){ 8048 /* can we split? */ 8049 int res1, res2; 8050 8051 res1 = max_seq - rsm->r_start; 8052 res2 = rsm->r_end - max_seq; 8053 if ((res1 >= (maxseg/8)) && 8054 (res2 >= (maxseg/8))) { 8055 /* No small pieces here */ 8056 can_split = 1; 8057 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) { 8058 /* We are under the limit */ 8059 can_split = 1; 8060 } 8061 } 8062 /* Ok do we need to split this rsm? */ 8063 if (max_seq == rsm->r_start) { 8064 /* It's this guy no split required */ 8065 nrsm = rsm; 8066 } else if (max_seq == rsm->r_end) { 8067 /* It's the next one no split required. */ 8068 nrsm = TAILQ_NEXT(rsm, r_next); 8069 if (nrsm == NULL) { 8070 /* Huh? */ 8071 return; 8072 } 8073 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) { 8074 /* yep we need to split it */ 8075 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 8076 if (nrsm == NULL) { 8077 /* failed XXXrrs what can we do mark the whole? */ 8078 nrsm = rsm; 8079 goto no_split; 8080 } 8081 /* Clone it */ 8082 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0); 8083 bbr_clone_rsm(bbr, nrsm, rsm, max_seq); 8084 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 8085 if (rsm->r_in_tmap) { 8086 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8087 nrsm->r_in_tmap = 1; 8088 } 8089 } else { 8090 /* 8091 * Split not allowed just start here just 8092 * use this guy. 8093 */ 8094 nrsm = rsm; 8095 } 8096 no_split: 8097 BBR_STAT_INC(bbr_collapsed_win); 8098 /* reuse fnd as a count */ 8099 fnd = 0; 8100 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) { 8101 nrsm->r_flags |= BBR_RWND_COLLAPSED; 8102 fnd++; 8103 bbr->rc_has_collapsed = 1; 8104 } 8105 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd); 8106 } 8107 8108 static void 8109 bbr_un_collapse_window(struct tcp_bbr *bbr) 8110 { 8111 struct bbr_sendmap *rsm; 8112 int cleared = 0; 8113 8114 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 8115 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 8116 /* Clear the flag */ 8117 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8118 cleared++; 8119 } else 8120 break; 8121 } 8122 bbr_log_type_rwnd_collapse(bbr, 8123 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared); 8124 bbr->rc_has_collapsed = 0; 8125 } 8126 8127 /* 8128 * Return value of 1, the TCB is unlocked and most 8129 * likely gone, return value of 0, the TCB is still 8130 * locked. 8131 */ 8132 static int 8133 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 8134 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 8135 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 8136 { 8137 /* 8138 * Update window information. Don't look at window if no ACK: TAC's 8139 * send garbage on first SYN. 8140 */ 8141 uint16_t nsegs; 8142 int32_t tfo_syn; 8143 struct tcp_bbr *bbr; 8144 8145 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8146 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8147 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8148 if ((thflags & TH_ACK) && 8149 (SEQ_LT(tp->snd_wl1, th->th_seq) || 8150 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 8151 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 8152 /* keep track of pure window updates */ 8153 if (tlen == 0 && 8154 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 8155 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 8156 tp->snd_wnd = tiwin; 8157 tp->snd_wl1 = th->th_seq; 8158 tp->snd_wl2 = th->th_ack; 8159 if (tp->snd_wnd > tp->max_sndwnd) 8160 tp->max_sndwnd = tp->snd_wnd; 8161 bbr->r_wanted_output = 1; 8162 } else if (thflags & TH_ACK) { 8163 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 8164 tp->snd_wnd = tiwin; 8165 tp->snd_wl1 = th->th_seq; 8166 tp->snd_wl2 = th->th_ack; 8167 } 8168 } 8169 if (tp->snd_wnd < ctf_outstanding(tp)) 8170 /* The peer collapsed its window on us */ 8171 bbr_collapsed_window(bbr); 8172 else if (bbr->rc_has_collapsed) 8173 bbr_un_collapse_window(bbr); 8174 /* Was persist timer active and now we have window space? */ 8175 if ((bbr->rc_in_persist != 0) && 8176 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8177 bbr_minseg(bbr)))) { 8178 /* 8179 * Make the rate persist at end of persist mode if idle long 8180 * enough 8181 */ 8182 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8183 8184 /* Make sure we output to start the timer */ 8185 bbr->r_wanted_output = 1; 8186 } 8187 /* Do we need to enter persist? */ 8188 if ((bbr->rc_in_persist == 0) && 8189 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8190 TCPS_HAVEESTABLISHED(tp->t_state) && 8191 (tp->snd_max == tp->snd_una) && 8192 sbavail(&so->so_snd) && 8193 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8194 /* No send window.. we must enter persist */ 8195 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8196 } 8197 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 8198 m_freem(m); 8199 return (0); 8200 } 8201 /* 8202 * We don't support urgent data but 8203 * drag along the up just to make sure 8204 * if there is a stack switch no one 8205 * is surprised. 8206 */ 8207 tp->rcv_up = tp->rcv_nxt; 8208 8209 /* 8210 * Process the segment text, merging it into the TCP sequencing 8211 * queue, and arranging for acknowledgment of receipt if necessary. 8212 * This process logically involves adjusting tp->rcv_wnd as data is 8213 * presented to the user (this happens in tcp_usrreq.c, case 8214 * PRU_RCVD). If a FIN has already been received on this connection 8215 * then we just ignore the text. 8216 */ 8217 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 8218 (tp->t_flags & TF_FASTOPEN)); 8219 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 8220 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8221 tcp_seq save_start = th->th_seq; 8222 tcp_seq save_rnxt = tp->rcv_nxt; 8223 int save_tlen = tlen; 8224 8225 m_adj(m, drop_hdrlen); /* delayed header drop */ 8226 /* 8227 * Insert segment which includes th into TCP reassembly 8228 * queue with control block tp. Set thflags to whether 8229 * reassembly now includes a segment with FIN. This handles 8230 * the common case inline (segment is the next to be 8231 * received on an established connection, and the queue is 8232 * empty), avoiding linkage into and removal from the queue 8233 * and repetition of various conversions. Set DELACK for 8234 * segments received in order, but ack immediately when 8235 * segments are out of order (so fast retransmit can work). 8236 */ 8237 if (th->th_seq == tp->rcv_nxt && 8238 SEGQ_EMPTY(tp) && 8239 (TCPS_HAVEESTABLISHED(tp->t_state) || 8240 tfo_syn)) { 8241 #ifdef NETFLIX_SB_LIMITS 8242 u_int mcnt, appended; 8243 8244 if (so->so_rcv.sb_shlim) { 8245 mcnt = m_memcnt(m); 8246 appended = 0; 8247 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8248 CFO_NOSLEEP, NULL) == false) { 8249 counter_u64_add(tcp_sb_shlim_fails, 1); 8250 m_freem(m); 8251 return (0); 8252 } 8253 } 8254 8255 #endif 8256 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) { 8257 bbr->bbr_segs_rcvd += max(1, nsegs); 8258 tp->t_flags |= TF_DELACK; 8259 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8260 } else { 8261 bbr->r_wanted_output = 1; 8262 tp->t_flags |= TF_ACKNOW; 8263 } 8264 tp->rcv_nxt += tlen; 8265 if (tlen && 8266 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8267 (tp->t_fbyte_in == 0)) { 8268 tp->t_fbyte_in = ticks; 8269 if (tp->t_fbyte_in == 0) 8270 tp->t_fbyte_in = 1; 8271 if (tp->t_fbyte_out && tp->t_fbyte_in) 8272 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8273 } 8274 thflags = tcp_get_flags(th) & TH_FIN; 8275 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8276 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8277 SOCK_RECVBUF_LOCK(so); 8278 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 8279 m_freem(m); 8280 else 8281 #ifdef NETFLIX_SB_LIMITS 8282 appended = 8283 #endif 8284 sbappendstream_locked(&so->so_rcv, m, 0); 8285 /* NB: sorwakeup_locked() does an implicit unlock. */ 8286 sorwakeup_locked(so); 8287 #ifdef NETFLIX_SB_LIMITS 8288 if (so->so_rcv.sb_shlim && appended != mcnt) 8289 counter_fo_release(so->so_rcv.sb_shlim, 8290 mcnt - appended); 8291 #endif 8292 8293 } else { 8294 /* 8295 * XXX: Due to the header drop above "th" is 8296 * theoretically invalid by now. Fortunately 8297 * m_adj() doesn't actually frees any mbufs when 8298 * trimming from the head. 8299 */ 8300 tcp_seq temp = save_start; 8301 8302 thflags = tcp_reass(tp, th, &temp, &tlen, m); 8303 tp->t_flags |= TF_ACKNOW; 8304 if (tp->t_flags & TF_WAKESOR) { 8305 tp->t_flags &= ~TF_WAKESOR; 8306 /* NB: sorwakeup_locked() does an implicit unlock. */ 8307 sorwakeup_locked(so); 8308 } 8309 } 8310 if ((tp->t_flags & TF_SACK_PERMIT) && 8311 (save_tlen > 0) && 8312 TCPS_HAVEESTABLISHED(tp->t_state)) { 8313 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 8314 /* 8315 * DSACK actually handled in the fastpath 8316 * above. 8317 */ 8318 tcp_update_sack_list(tp, save_start, 8319 save_start + save_tlen); 8320 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 8321 if ((tp->rcv_numsacks >= 1) && 8322 (tp->sackblks[0].end == save_start)) { 8323 /* 8324 * Partial overlap, recorded at todrop 8325 * above. 8326 */ 8327 tcp_update_sack_list(tp, 8328 tp->sackblks[0].start, 8329 tp->sackblks[0].end); 8330 } else { 8331 tcp_update_dsack_list(tp, save_start, 8332 save_start + save_tlen); 8333 } 8334 } else if (tlen >= save_tlen) { 8335 /* Update of sackblks. */ 8336 tcp_update_dsack_list(tp, save_start, 8337 save_start + save_tlen); 8338 } else if (tlen > 0) { 8339 tcp_update_dsack_list(tp, save_start, 8340 save_start + tlen); 8341 } 8342 } 8343 } else { 8344 m_freem(m); 8345 thflags &= ~TH_FIN; 8346 } 8347 8348 /* 8349 * If FIN is received ACK the FIN and let the user know that the 8350 * connection is closing. 8351 */ 8352 if (thflags & TH_FIN) { 8353 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8354 /* The socket upcall is handled by socantrcvmore. */ 8355 socantrcvmore(so); 8356 /* 8357 * If connection is half-synchronized (ie NEEDSYN 8358 * flag on) then delay ACK, so it may be piggybacked 8359 * when SYN is sent. Otherwise, since we received a 8360 * FIN then no more input can be expected, send ACK 8361 * now. 8362 */ 8363 if (tp->t_flags & TF_NEEDSYN) { 8364 tp->t_flags |= TF_DELACK; 8365 bbr_timer_cancel(bbr, 8366 __LINE__, bbr->r_ctl.rc_rcvtime); 8367 } else { 8368 tp->t_flags |= TF_ACKNOW; 8369 } 8370 tp->rcv_nxt++; 8371 } 8372 switch (tp->t_state) { 8373 /* 8374 * In SYN_RECEIVED and ESTABLISHED STATES enter the 8375 * CLOSE_WAIT state. 8376 */ 8377 case TCPS_SYN_RECEIVED: 8378 tp->t_starttime = ticks; 8379 /* FALLTHROUGH */ 8380 case TCPS_ESTABLISHED: 8381 tcp_state_change(tp, TCPS_CLOSE_WAIT); 8382 break; 8383 8384 /* 8385 * If still in FIN_WAIT_1 STATE FIN has not been 8386 * acked so enter the CLOSING state. 8387 */ 8388 case TCPS_FIN_WAIT_1: 8389 tcp_state_change(tp, TCPS_CLOSING); 8390 break; 8391 8392 /* 8393 * In FIN_WAIT_2 state enter the TIME_WAIT state, 8394 * starting the time-wait timer, turning off the 8395 * other standard timers. 8396 */ 8397 case TCPS_FIN_WAIT_2: 8398 bbr->rc_timer_first = 1; 8399 bbr_timer_cancel(bbr, 8400 __LINE__, bbr->r_ctl.rc_rcvtime); 8401 tcp_twstart(tp); 8402 return (1); 8403 } 8404 } 8405 /* 8406 * Return any desired output. 8407 */ 8408 if ((tp->t_flags & TF_ACKNOW) || 8409 (sbavail(&so->so_snd) > ctf_outstanding(tp))) { 8410 bbr->r_wanted_output = 1; 8411 } 8412 return (0); 8413 } 8414 8415 /* 8416 * Here nothing is really faster, its just that we 8417 * have broken out the fast-data path also just like 8418 * the fast-ack. Return 1 if we processed the packet 8419 * return 0 if you need to take the "slow-path". 8420 */ 8421 static int 8422 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 8423 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8424 uint32_t tiwin, int32_t nxt_pkt) 8425 { 8426 uint16_t nsegs; 8427 int32_t newsize = 0; /* automatic sockbuf scaling */ 8428 struct tcp_bbr *bbr; 8429 #ifdef NETFLIX_SB_LIMITS 8430 u_int mcnt, appended; 8431 #endif 8432 8433 /* On the hpts and we would have called output */ 8434 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8435 8436 /* 8437 * If last ACK falls within this segment's sequence numbers, record 8438 * the timestamp. NOTE that the test is modified according to the 8439 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8440 */ 8441 if (bbr->r_ctl.rc_resend != NULL) { 8442 return (0); 8443 } 8444 if (tiwin && tiwin != tp->snd_wnd) { 8445 return (0); 8446 } 8447 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 8448 return (0); 8449 } 8450 if (__predict_false((to->to_flags & TOF_TS) && 8451 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 8452 return (0); 8453 } 8454 if (__predict_false((th->th_ack != tp->snd_una))) { 8455 return (0); 8456 } 8457 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 8458 return (0); 8459 } 8460 if ((to->to_flags & TOF_TS) != 0 && 8461 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8462 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 8463 tp->ts_recent = to->to_tsval; 8464 } 8465 /* 8466 * This is a pure, in-sequence data packet with nothing on the 8467 * reassembly queue and we have enough buffer space to take it. 8468 */ 8469 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8470 8471 #ifdef NETFLIX_SB_LIMITS 8472 if (so->so_rcv.sb_shlim) { 8473 mcnt = m_memcnt(m); 8474 appended = 0; 8475 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8476 CFO_NOSLEEP, NULL) == false) { 8477 counter_u64_add(tcp_sb_shlim_fails, 1); 8478 m_freem(m); 8479 return (1); 8480 } 8481 } 8482 #endif 8483 /* Clean receiver SACK report if present */ 8484 if (tp->rcv_numsacks) 8485 tcp_clean_sackreport(tp); 8486 KMOD_TCPSTAT_INC(tcps_preddat); 8487 tp->rcv_nxt += tlen; 8488 if (tlen && 8489 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8490 (tp->t_fbyte_in == 0)) { 8491 tp->t_fbyte_in = ticks; 8492 if (tp->t_fbyte_in == 0) 8493 tp->t_fbyte_in = 1; 8494 if (tp->t_fbyte_out && tp->t_fbyte_in) 8495 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8496 } 8497 /* 8498 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 8499 */ 8500 tp->snd_wl1 = th->th_seq; 8501 /* 8502 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 8503 */ 8504 tp->rcv_up = tp->rcv_nxt; 8505 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8506 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8507 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 8508 8509 /* Add data to socket buffer. */ 8510 SOCK_RECVBUF_LOCK(so); 8511 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8512 m_freem(m); 8513 } else { 8514 /* 8515 * Set new socket buffer size. Give up when limit is 8516 * reached. 8517 */ 8518 if (newsize) 8519 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 8520 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 8521 m_adj(m, drop_hdrlen); /* delayed header drop */ 8522 8523 #ifdef NETFLIX_SB_LIMITS 8524 appended = 8525 #endif 8526 sbappendstream_locked(&so->so_rcv, m, 0); 8527 ctf_calc_rwin(so, tp); 8528 } 8529 /* NB: sorwakeup_locked() does an implicit unlock. */ 8530 sorwakeup_locked(so); 8531 #ifdef NETFLIX_SB_LIMITS 8532 if (so->so_rcv.sb_shlim && mcnt != appended) 8533 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 8534 #endif 8535 if (DELAY_ACK(tp, bbr, nsegs)) { 8536 bbr->bbr_segs_rcvd += max(1, nsegs); 8537 tp->t_flags |= TF_DELACK; 8538 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8539 } else { 8540 bbr->r_wanted_output = 1; 8541 tp->t_flags |= TF_ACKNOW; 8542 } 8543 return (1); 8544 } 8545 8546 /* 8547 * This subfunction is used to try to highly optimize the 8548 * fast path. We again allow window updates that are 8549 * in sequence to remain in the fast-path. We also add 8550 * in the __predict's to attempt to help the compiler. 8551 * Note that if we return a 0, then we can *not* process 8552 * it and the caller should push the packet into the 8553 * slow-path. If we return 1, then all is well and 8554 * the packet is fully processed. 8555 */ 8556 static int 8557 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 8558 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8559 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 8560 { 8561 int32_t acked; 8562 uint16_t nsegs; 8563 uint32_t sack_changed; 8564 uint32_t prev_acked = 0; 8565 struct tcp_bbr *bbr; 8566 8567 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 8568 /* Old ack, behind (or duplicate to) the last one rcv'd */ 8569 return (0); 8570 } 8571 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 8572 /* Above what we have sent? */ 8573 return (0); 8574 } 8575 if (__predict_false(tiwin == 0)) { 8576 /* zero window */ 8577 return (0); 8578 } 8579 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 8580 /* We need a SYN or a FIN, unlikely.. */ 8581 return (0); 8582 } 8583 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 8584 /* Timestamp is behind .. old ack with seq wrap? */ 8585 return (0); 8586 } 8587 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 8588 /* Still recovering */ 8589 return (0); 8590 } 8591 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8592 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) { 8593 /* We are retransmitting */ 8594 return (0); 8595 } 8596 if (__predict_false(bbr->rc_in_persist != 0)) { 8597 /* In persist mode */ 8598 return (0); 8599 } 8600 if (bbr->r_ctl.rc_sacked) { 8601 /* We have sack holes on our scoreboard */ 8602 return (0); 8603 } 8604 /* Ok if we reach here, we can process a fast-ack */ 8605 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8606 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 8607 /* 8608 * We never detect loss in fast ack [we can't 8609 * have a sack and can't be in recovery so 8610 * we always pass 0 (nothing detected)]. 8611 */ 8612 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0); 8613 /* Did the window get updated? */ 8614 if (tiwin != tp->snd_wnd) { 8615 tp->snd_wnd = tiwin; 8616 tp->snd_wl1 = th->th_seq; 8617 if (tp->snd_wnd > tp->max_sndwnd) 8618 tp->max_sndwnd = tp->snd_wnd; 8619 } 8620 /* Do we need to exit persists? */ 8621 if ((bbr->rc_in_persist != 0) && 8622 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8623 bbr_minseg(bbr)))) { 8624 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8625 bbr->r_wanted_output = 1; 8626 } 8627 /* Do we need to enter persists? */ 8628 if ((bbr->rc_in_persist == 0) && 8629 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8630 TCPS_HAVEESTABLISHED(tp->t_state) && 8631 (tp->snd_max == tp->snd_una) && 8632 sbavail(&so->so_snd) && 8633 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8634 /* No send window.. we must enter persist */ 8635 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8636 } 8637 /* 8638 * If last ACK falls within this segment's sequence numbers, record 8639 * the timestamp. NOTE that the test is modified according to the 8640 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8641 */ 8642 if ((to->to_flags & TOF_TS) != 0 && 8643 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8644 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime; 8645 tp->ts_recent = to->to_tsval; 8646 } 8647 /* 8648 * This is a pure ack for outstanding data. 8649 */ 8650 KMOD_TCPSTAT_INC(tcps_predack); 8651 8652 /* 8653 * "bad retransmit" recovery. 8654 */ 8655 if (tp->t_flags & TF_PREVVALID) { 8656 tp->t_flags &= ~TF_PREVVALID; 8657 if (tp->t_rxtshift == 1 && 8658 (int)(ticks - tp->t_badrxtwin) < 0) 8659 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 8660 } 8661 /* 8662 * Recalculate the transmit timer / rtt. 8663 * 8664 * Some boxes send broken timestamp replies during the SYN+ACK 8665 * phase, ignore timestamps of 0 or we could calculate a huge RTT 8666 * and blow up the retransmit timer. 8667 */ 8668 acked = BYTES_THIS_ACK(tp, th); 8669 8670 #ifdef TCP_HHOOK 8671 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 8672 hhook_run_tcp_est_in(tp, th, to); 8673 #endif 8674 8675 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 8676 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 8677 sbdrop(&so->so_snd, acked); 8678 8679 if (SEQ_GT(th->th_ack, tp->snd_una)) 8680 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 8681 tp->snd_una = th->th_ack; 8682 if (tp->snd_wnd < ctf_outstanding(tp)) 8683 /* The peer collapsed its window on us */ 8684 bbr_collapsed_window(bbr); 8685 else if (bbr->rc_has_collapsed) 8686 bbr_un_collapse_window(bbr); 8687 8688 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 8689 tp->snd_recover = tp->snd_una; 8690 } 8691 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0); 8692 /* 8693 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 8694 */ 8695 tp->snd_wl2 = th->th_ack; 8696 m_freem(m); 8697 /* 8698 * If all outstanding data are acked, stop retransmit timer, 8699 * otherwise restart timer using current (possibly backed-off) 8700 * value. If process is waiting for space, wakeup/selwakeup/signal. 8701 * If data are ready to send, let tcp_output decide between more 8702 * output or persist. 8703 * Wake up the socket if we have room to write more. 8704 */ 8705 sowwakeup(so); 8706 if (tp->snd_una == tp->snd_max) { 8707 /* Nothing left outstanding */ 8708 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 8709 if (sbavail(&so->so_snd) == 0) 8710 bbr->rc_tp->t_acktime = 0; 8711 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8712 if (bbr->rc_in_persist == 0) { 8713 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 8714 } 8715 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 8716 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 8717 /* 8718 * We invalidate the last ack here since we 8719 * don't want to transfer forward the time 8720 * for our sum's calculations. 8721 */ 8722 bbr->r_wanted_output = 1; 8723 } 8724 if (sbavail(&so->so_snd)) { 8725 bbr->r_wanted_output = 1; 8726 } 8727 return (1); 8728 } 8729 8730 /* 8731 * Return value of 1, the TCB is unlocked and most 8732 * likely gone, return value of 0, the TCB is still 8733 * locked. 8734 */ 8735 static int 8736 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 8737 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8738 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8739 { 8740 int32_t todrop; 8741 int32_t ourfinisacked = 0; 8742 struct tcp_bbr *bbr; 8743 int32_t ret_val = 0; 8744 8745 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8746 8747 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8748 ctf_calc_rwin(so, tp); 8749 /* 8750 * If the state is SYN_SENT: if seg contains an ACK, but not for our 8751 * SYN, drop the input. if seg contains a RST, then drop the 8752 * connection. if seg does not contain SYN, then drop it. Otherwise 8753 * this is an acceptable SYN segment initialize tp->rcv_nxt and 8754 * tp->irs if seg contains ack then advance tp->snd_una. BRR does 8755 * not support ECN so we will not say we are capable. if SYN has 8756 * been acked change to ESTABLISHED else SYN_RCVD state arrange for 8757 * segment to be acked (eventually) continue processing rest of 8758 * data/controls, beginning with URG 8759 */ 8760 if ((thflags & TH_ACK) && 8761 (SEQ_LEQ(th->th_ack, tp->iss) || 8762 SEQ_GT(th->th_ack, tp->snd_max))) { 8763 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8764 ctf_do_dropwithreset(m, tp, th, tlen); 8765 return (1); 8766 } 8767 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 8768 TCP_PROBE5(connect__refused, NULL, tp, 8769 mtod(m, const char *), tp, th); 8770 tp = tcp_drop(tp, ECONNREFUSED); 8771 ctf_do_drop(m, tp); 8772 return (1); 8773 } 8774 if (thflags & TH_RST) { 8775 ctf_do_drop(m, tp); 8776 return (1); 8777 } 8778 if (!(thflags & TH_SYN)) { 8779 ctf_do_drop(m, tp); 8780 return (1); 8781 } 8782 tp->irs = th->th_seq; 8783 tcp_rcvseqinit(tp); 8784 if (thflags & TH_ACK) { 8785 int tfo_partial = 0; 8786 8787 KMOD_TCPSTAT_INC(tcps_connects); 8788 soisconnected(so); 8789 #ifdef MAC 8790 mac_socketpeer_set_from_mbuf(m, so); 8791 #endif 8792 /* Do window scaling on this connection? */ 8793 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 8794 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 8795 tp->rcv_scale = tp->request_r_scale; 8796 } 8797 tp->rcv_adv += min(tp->rcv_wnd, 8798 TCP_MAXWIN << tp->rcv_scale); 8799 /* 8800 * If not all the data that was sent in the TFO SYN 8801 * has been acked, resend the remainder right away. 8802 */ 8803 if ((tp->t_flags & TF_FASTOPEN) && 8804 (tp->snd_una != tp->snd_max)) { 8805 tp->snd_nxt = th->th_ack; 8806 tfo_partial = 1; 8807 } 8808 /* 8809 * If there's data, delay ACK; if there's also a FIN ACKNOW 8810 * will be turned on later. 8811 */ 8812 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) { 8813 bbr->bbr_segs_rcvd += 1; 8814 tp->t_flags |= TF_DELACK; 8815 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8816 } else { 8817 bbr->r_wanted_output = 1; 8818 tp->t_flags |= TF_ACKNOW; 8819 } 8820 if (SEQ_GT(th->th_ack, tp->iss)) { 8821 /* 8822 * The SYN is acked 8823 * handle it specially. 8824 */ 8825 bbr_log_syn(tp, to); 8826 } 8827 if (SEQ_GT(th->th_ack, tp->snd_una)) { 8828 /* 8829 * We advance snd_una for the 8830 * fast open case. If th_ack is 8831 * acknowledging data beyond 8832 * snd_una we can't just call 8833 * ack-processing since the 8834 * data stream in our send-map 8835 * will start at snd_una + 1 (one 8836 * beyond the SYN). If its just 8837 * equal we don't need to do that 8838 * and there is no send_map. 8839 */ 8840 tp->snd_una++; 8841 } 8842 /* 8843 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 8844 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 8845 */ 8846 tp->t_starttime = ticks; 8847 if (tp->t_flags & TF_NEEDFIN) { 8848 tcp_state_change(tp, TCPS_FIN_WAIT_1); 8849 tp->t_flags &= ~TF_NEEDFIN; 8850 thflags &= ~TH_SYN; 8851 } else { 8852 tcp_state_change(tp, TCPS_ESTABLISHED); 8853 TCP_PROBE5(connect__established, NULL, tp, 8854 mtod(m, const char *), tp, th); 8855 cc_conn_init(tp); 8856 } 8857 } else { 8858 /* 8859 * Received initial SYN in SYN-SENT[*] state => simultaneous 8860 * open. If segment contains CC option and there is a 8861 * cached CC, apply TAO test. If it succeeds, connection is * 8862 * half-synchronized. Otherwise, do 3-way handshake: 8863 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 8864 * there was no CC option, clear cached CC value. 8865 */ 8866 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 8867 tcp_state_change(tp, TCPS_SYN_RECEIVED); 8868 } 8869 /* 8870 * Advance th->th_seq to correspond to first data byte. If data, 8871 * trim to stay within window, dropping FIN if necessary. 8872 */ 8873 th->th_seq++; 8874 if (tlen > tp->rcv_wnd) { 8875 todrop = tlen - tp->rcv_wnd; 8876 m_adj(m, -todrop); 8877 tlen = tp->rcv_wnd; 8878 thflags &= ~TH_FIN; 8879 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 8880 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 8881 } 8882 tp->snd_wl1 = th->th_seq - 1; 8883 tp->rcv_up = th->th_seq; 8884 /* 8885 * Client side of transaction: already sent SYN and data. If the 8886 * remote host used T/TCP to validate the SYN, our data will be 8887 * ACK'd; if so, enter normal data segment processing in the middle 8888 * of step 5, ack processing. Otherwise, goto step 6. 8889 */ 8890 if (thflags & TH_ACK) { 8891 if ((to->to_flags & TOF_TS) != 0) { 8892 uint32_t t, rtt; 8893 8894 t = tcp_tv_to_msec(&bbr->rc_tv); 8895 if (TSTMP_GEQ(t, to->to_tsecr)) { 8896 rtt = t - to->to_tsecr; 8897 if (rtt == 0) { 8898 rtt = 1; 8899 } 8900 rtt *= MS_IN_USEC; 8901 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 8902 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, 8903 rtt, bbr->r_ctl.rc_rcvtime); 8904 } 8905 } 8906 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 8907 return (ret_val); 8908 /* We may have changed to FIN_WAIT_1 above */ 8909 if (tp->t_state == TCPS_FIN_WAIT_1) { 8910 /* 8911 * In FIN_WAIT_1 STATE in addition to the processing 8912 * for the ESTABLISHED state if our FIN is now 8913 * acknowledged then enter FIN_WAIT_2. 8914 */ 8915 if (ourfinisacked) { 8916 /* 8917 * If we can't receive any more data, then 8918 * closing user can proceed. Starting the 8919 * timer is contrary to the specification, 8920 * but if we don't get a FIN we'll hang 8921 * forever. 8922 * 8923 * XXXjl: we should release the tp also, and 8924 * use a compressed state. 8925 */ 8926 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8927 soisdisconnected(so); 8928 tcp_timer_activate(tp, TT_2MSL, 8929 (tcp_fast_finwait2_recycle ? 8930 tcp_finwait2_timeout : 8931 TP_MAXIDLE(tp))); 8932 } 8933 tcp_state_change(tp, TCPS_FIN_WAIT_2); 8934 } 8935 } 8936 } 8937 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 8938 tiwin, thflags, nxt_pkt)); 8939 } 8940 8941 /* 8942 * Return value of 1, the TCB is unlocked and most 8943 * likely gone, return value of 0, the TCB is still 8944 * locked. 8945 */ 8946 static int 8947 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 8948 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8949 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8950 { 8951 int32_t ourfinisacked = 0; 8952 int32_t ret_val; 8953 struct tcp_bbr *bbr; 8954 8955 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8956 8957 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8958 ctf_calc_rwin(so, tp); 8959 if ((thflags & TH_RST) || 8960 (tp->t_fin_is_rst && (thflags & TH_FIN))) 8961 return (ctf_process_rst(m, th, so, tp)); 8962 if ((thflags & TH_ACK) && 8963 (SEQ_LEQ(th->th_ack, tp->snd_una) || 8964 SEQ_GT(th->th_ack, tp->snd_max))) { 8965 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8966 ctf_do_dropwithreset(m, tp, th, tlen); 8967 return (1); 8968 } 8969 if (tp->t_flags & TF_FASTOPEN) { 8970 /* 8971 * When a TFO connection is in SYN_RECEIVED, the only valid 8972 * packets are the initial SYN, a retransmit/copy of the 8973 * initial SYN (possibly with a subset of the original 8974 * data), a valid ACK, a FIN, or a RST. 8975 */ 8976 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 8977 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8978 ctf_do_dropwithreset(m, tp, th, tlen); 8979 return (1); 8980 } else if (thflags & TH_SYN) { 8981 /* non-initial SYN is ignored */ 8982 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 8983 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 8984 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 8985 ctf_do_drop(m, NULL); 8986 return (0); 8987 } 8988 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 8989 ctf_do_drop(m, NULL); 8990 return (0); 8991 } 8992 } 8993 /* 8994 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 8995 * it's less than ts_recent, drop it. 8996 */ 8997 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 8998 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 8999 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9000 return (ret_val); 9001 } 9002 /* 9003 * In the SYN-RECEIVED state, validate that the packet belongs to 9004 * this connection before trimming the data to fit the receive 9005 * window. Check the sequence number versus IRS since we know the 9006 * sequence numbers haven't wrapped. This is a partial fix for the 9007 * "LAND" DoS attack. 9008 */ 9009 if (SEQ_LT(th->th_seq, tp->irs)) { 9010 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 9011 ctf_do_dropwithreset(m, tp, th, tlen); 9012 return (1); 9013 } 9014 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9015 return (ret_val); 9016 } 9017 /* 9018 * If last ACK falls within this segment's sequence numbers, record 9019 * its timestamp. NOTE: 1) That the test incorporates suggestions 9020 * from the latest proposal of the tcplw@cray.com list (Braden 9021 * 1993/04/26). 2) That updating only on newer timestamps interferes 9022 * with our earlier PAWS tests, so this check should be solely 9023 * predicated on the sequence space of this segment. 3) That we 9024 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9025 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9026 * SEG.Len, This modified check allows us to overcome RFC1323's 9027 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9028 * p.869. In such cases, we can still calculate the RTT correctly 9029 * when RCV.NXT == Last.ACK.Sent. 9030 */ 9031 if ((to->to_flags & TOF_TS) != 0 && 9032 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9033 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9034 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9035 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9036 tp->ts_recent = to->to_tsval; 9037 } 9038 tp->snd_wnd = tiwin; 9039 /* 9040 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9041 * is on (half-synchronized state), then queue data for later 9042 * processing; else drop segment and return. 9043 */ 9044 if ((thflags & TH_ACK) == 0) { 9045 if (tp->t_flags & TF_FASTOPEN) { 9046 cc_conn_init(tp); 9047 } 9048 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9049 tiwin, thflags, nxt_pkt)); 9050 } 9051 KMOD_TCPSTAT_INC(tcps_connects); 9052 if (tp->t_flags & TF_SONOTCONN) { 9053 tp->t_flags &= ~TF_SONOTCONN; 9054 soisconnected(so); 9055 } 9056 /* Do window scaling? */ 9057 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 9058 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 9059 tp->rcv_scale = tp->request_r_scale; 9060 } 9061 /* 9062 * ok for the first time in lets see if we can use the ts to figure 9063 * out what the initial RTT was. 9064 */ 9065 if ((to->to_flags & TOF_TS) != 0) { 9066 uint32_t t, rtt; 9067 9068 t = tcp_tv_to_msec(&bbr->rc_tv); 9069 if (TSTMP_GEQ(t, to->to_tsecr)) { 9070 rtt = t - to->to_tsecr; 9071 if (rtt == 0) { 9072 rtt = 1; 9073 } 9074 rtt *= MS_IN_USEC; 9075 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 9076 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime); 9077 } 9078 } 9079 /* Drop off any SYN in the send map (probably not there) */ 9080 if (thflags & TH_ACK) 9081 bbr_log_syn(tp, to); 9082 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 9083 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 9084 tp->t_tfo_pending = NULL; 9085 } 9086 /* 9087 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 9088 * FIN-WAIT-1 9089 */ 9090 tp->t_starttime = ticks; 9091 if (tp->t_flags & TF_NEEDFIN) { 9092 tcp_state_change(tp, TCPS_FIN_WAIT_1); 9093 tp->t_flags &= ~TF_NEEDFIN; 9094 } else { 9095 tcp_state_change(tp, TCPS_ESTABLISHED); 9096 TCP_PROBE5(accept__established, NULL, tp, 9097 mtod(m, const char *), tp, th); 9098 /* 9099 * TFO connections call cc_conn_init() during SYN 9100 * processing. Calling it again here for such connections 9101 * is not harmless as it would undo the snd_cwnd reduction 9102 * that occurs when a TFO SYN|ACK is retransmitted. 9103 */ 9104 if (!(tp->t_flags & TF_FASTOPEN)) 9105 cc_conn_init(tp); 9106 } 9107 /* 9108 * Account for the ACK of our SYN prior to 9109 * regular ACK processing below, except for 9110 * simultaneous SYN, which is handled later. 9111 */ 9112 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 9113 tp->snd_una++; 9114 /* 9115 * If segment contains data or ACK, will call tcp_reass() later; if 9116 * not, do so now to pass queued data to user. 9117 */ 9118 if (tlen == 0 && (thflags & TH_FIN) == 0) { 9119 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 9120 (struct mbuf *)0); 9121 if (tp->t_flags & TF_WAKESOR) { 9122 tp->t_flags &= ~TF_WAKESOR; 9123 /* NB: sorwakeup_locked() does an implicit unlock. */ 9124 sorwakeup_locked(so); 9125 } 9126 } 9127 tp->snd_wl1 = th->th_seq - 1; 9128 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9129 return (ret_val); 9130 } 9131 if (tp->t_state == TCPS_FIN_WAIT_1) { 9132 /* We could have went to FIN_WAIT_1 (or EST) above */ 9133 /* 9134 * In FIN_WAIT_1 STATE in addition to the processing for the 9135 * ESTABLISHED state if our FIN is now acknowledged then 9136 * enter FIN_WAIT_2. 9137 */ 9138 if (ourfinisacked) { 9139 /* 9140 * If we can't receive any more data, then closing 9141 * user can proceed. Starting the timer is contrary 9142 * to the specification, but if we don't get a FIN 9143 * we'll hang forever. 9144 * 9145 * XXXjl: we should release the tp also, and use a 9146 * compressed state. 9147 */ 9148 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9149 soisdisconnected(so); 9150 tcp_timer_activate(tp, TT_2MSL, 9151 (tcp_fast_finwait2_recycle ? 9152 tcp_finwait2_timeout : 9153 TP_MAXIDLE(tp))); 9154 } 9155 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9156 } 9157 } 9158 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9159 tiwin, thflags, nxt_pkt)); 9160 } 9161 9162 /* 9163 * Return value of 1, the TCB is unlocked and most 9164 * likely gone, return value of 0, the TCB is still 9165 * locked. 9166 */ 9167 static int 9168 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 9169 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9170 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9171 { 9172 struct tcp_bbr *bbr; 9173 int32_t ret_val; 9174 9175 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9176 9177 /* 9178 * Header prediction: check for the two common cases of a 9179 * uni-directional data xfer. If the packet has no control flags, 9180 * is in-sequence, the window didn't change and we're not 9181 * retransmitting, it's a candidate. If the length is zero and the 9182 * ack moved forward, we're the sender side of the xfer. Just free 9183 * the data acked & wake any higher level process that was blocked 9184 * waiting for space. If the length is non-zero and the ack didn't 9185 * move, we're the receiver side. If we're getting packets in-order 9186 * (the reassembly queue is empty), add the data toc The socket 9187 * buffer and note that we need a delayed ack. Make sure that the 9188 * hidden state-flags are also off. Since we check for 9189 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 9190 */ 9191 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9192 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) { 9193 /* 9194 * If we have delived under 4 segments increase the initial 9195 * window if raised by the peer. We use this to determine 9196 * dynamic and static rwnd's at the end of a connection. 9197 */ 9198 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd); 9199 } 9200 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 9201 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 9202 __predict_true(SEGQ_EMPTY(tp)) && 9203 __predict_true(th->th_seq == tp->rcv_nxt)) { 9204 if (tlen == 0) { 9205 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 9206 tiwin, nxt_pkt, iptos)) { 9207 return (0); 9208 } 9209 } else { 9210 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 9211 tiwin, nxt_pkt)) { 9212 return (0); 9213 } 9214 } 9215 } 9216 ctf_calc_rwin(so, tp); 9217 9218 if ((thflags & TH_RST) || 9219 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9220 return (ctf_process_rst(m, th, so, tp)); 9221 /* 9222 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9223 * synchronized state. 9224 */ 9225 if (thflags & TH_SYN) { 9226 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9227 return (ret_val); 9228 } 9229 /* 9230 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9231 * it's less than ts_recent, drop it. 9232 */ 9233 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9234 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9235 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9236 return (ret_val); 9237 } 9238 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9239 return (ret_val); 9240 } 9241 /* 9242 * If last ACK falls within this segment's sequence numbers, record 9243 * its timestamp. NOTE: 1) That the test incorporates suggestions 9244 * from the latest proposal of the tcplw@cray.com list (Braden 9245 * 1993/04/26). 2) That updating only on newer timestamps interferes 9246 * with our earlier PAWS tests, so this check should be solely 9247 * predicated on the sequence space of this segment. 3) That we 9248 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9249 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9250 * SEG.Len, This modified check allows us to overcome RFC1323's 9251 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9252 * p.869. In such cases, we can still calculate the RTT correctly 9253 * when RCV.NXT == Last.ACK.Sent. 9254 */ 9255 if ((to->to_flags & TOF_TS) != 0 && 9256 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9257 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9258 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9259 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9260 tp->ts_recent = to->to_tsval; 9261 } 9262 /* 9263 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9264 * is on (half-synchronized state), then queue data for later 9265 * processing; else drop segment and return. 9266 */ 9267 if ((thflags & TH_ACK) == 0) { 9268 if (tp->t_flags & TF_NEEDSYN) { 9269 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9270 tiwin, thflags, nxt_pkt)); 9271 } else if (tp->t_flags & TF_ACKNOW) { 9272 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9273 bbr->r_wanted_output = 1; 9274 return (ret_val); 9275 } else { 9276 ctf_do_drop(m, NULL); 9277 return (0); 9278 } 9279 } 9280 /* 9281 * Ack processing. 9282 */ 9283 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9284 return (ret_val); 9285 } 9286 if (sbavail(&so->so_snd)) { 9287 if (ctf_progress_timeout_check(tp, true)) { 9288 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9289 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9290 return (1); 9291 } 9292 } 9293 /* State changes only happen in bbr_process_data() */ 9294 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9295 tiwin, thflags, nxt_pkt)); 9296 } 9297 9298 /* 9299 * Return value of 1, the TCB is unlocked and most 9300 * likely gone, return value of 0, the TCB is still 9301 * locked. 9302 */ 9303 static int 9304 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 9305 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9306 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9307 { 9308 struct tcp_bbr *bbr; 9309 int32_t ret_val; 9310 9311 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9312 9313 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9314 ctf_calc_rwin(so, tp); 9315 if ((thflags & TH_RST) || 9316 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9317 return (ctf_process_rst(m, th, so, tp)); 9318 /* 9319 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9320 * synchronized state. 9321 */ 9322 if (thflags & TH_SYN) { 9323 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9324 return (ret_val); 9325 } 9326 /* 9327 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9328 * it's less than ts_recent, drop it. 9329 */ 9330 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9331 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9332 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9333 return (ret_val); 9334 } 9335 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9336 return (ret_val); 9337 } 9338 /* 9339 * If last ACK falls within this segment's sequence numbers, record 9340 * its timestamp. NOTE: 1) That the test incorporates suggestions 9341 * from the latest proposal of the tcplw@cray.com list (Braden 9342 * 1993/04/26). 2) That updating only on newer timestamps interferes 9343 * with our earlier PAWS tests, so this check should be solely 9344 * predicated on the sequence space of this segment. 3) That we 9345 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9346 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9347 * SEG.Len, This modified check allows us to overcome RFC1323's 9348 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9349 * p.869. In such cases, we can still calculate the RTT correctly 9350 * when RCV.NXT == Last.ACK.Sent. 9351 */ 9352 if ((to->to_flags & TOF_TS) != 0 && 9353 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9354 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9355 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9356 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9357 tp->ts_recent = to->to_tsval; 9358 } 9359 /* 9360 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9361 * is on (half-synchronized state), then queue data for later 9362 * processing; else drop segment and return. 9363 */ 9364 if ((thflags & TH_ACK) == 0) { 9365 if (tp->t_flags & TF_NEEDSYN) { 9366 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9367 tiwin, thflags, nxt_pkt)); 9368 } else if (tp->t_flags & TF_ACKNOW) { 9369 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9370 bbr->r_wanted_output = 1; 9371 return (ret_val); 9372 } else { 9373 ctf_do_drop(m, NULL); 9374 return (0); 9375 } 9376 } 9377 /* 9378 * Ack processing. 9379 */ 9380 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9381 return (ret_val); 9382 } 9383 if (sbavail(&so->so_snd)) { 9384 if (ctf_progress_timeout_check(tp, true)) { 9385 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9386 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9387 return (1); 9388 } 9389 } 9390 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9391 tiwin, thflags, nxt_pkt)); 9392 } 9393 9394 static int 9395 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr, 9396 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so) 9397 { 9398 9399 if (bbr->rc_allow_data_af_clo == 0) { 9400 close_now: 9401 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 9402 /* tcp_close will kill the inp pre-log the Reset */ 9403 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 9404 tp = tcp_close(tp); 9405 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 9406 ctf_do_dropwithreset(m, tp, th, *tlen); 9407 return (1); 9408 } 9409 if (sbavail(&so->so_snd) == 0) 9410 goto close_now; 9411 /* Ok we allow data that is ignored and a followup reset */ 9412 tp->rcv_nxt = th->th_seq + *tlen; 9413 tp->t_flags2 |= TF2_DROP_AF_DATA; 9414 bbr->r_wanted_output = 1; 9415 *tlen = 0; 9416 return (0); 9417 } 9418 9419 /* 9420 * Return value of 1, the TCB is unlocked and most 9421 * likely gone, return value of 0, the TCB is still 9422 * locked. 9423 */ 9424 static int 9425 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 9426 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9427 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9428 { 9429 int32_t ourfinisacked = 0; 9430 int32_t ret_val; 9431 struct tcp_bbr *bbr; 9432 9433 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9434 9435 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9436 ctf_calc_rwin(so, tp); 9437 if ((thflags & TH_RST) || 9438 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9439 return (ctf_process_rst(m, th, so, tp)); 9440 /* 9441 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9442 * synchronized state. 9443 */ 9444 if (thflags & TH_SYN) { 9445 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9446 return (ret_val); 9447 } 9448 /* 9449 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9450 * it's less than ts_recent, drop it. 9451 */ 9452 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9453 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9454 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9455 return (ret_val); 9456 } 9457 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9458 return (ret_val); 9459 } 9460 /* 9461 * If new data are received on a connection after the user processes 9462 * are gone, then RST the other end. 9463 * We call a new function now so we might continue and setup 9464 * to reset at all data being ack'd. 9465 */ 9466 if ((tp->t_flags & TF_CLOSED) && tlen && 9467 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9468 return (1); 9469 /* 9470 * If last ACK falls within this segment's sequence numbers, record 9471 * its timestamp. NOTE: 1) That the test incorporates suggestions 9472 * from the latest proposal of the tcplw@cray.com list (Braden 9473 * 1993/04/26). 2) That updating only on newer timestamps interferes 9474 * with our earlier PAWS tests, so this check should be solely 9475 * predicated on the sequence space of this segment. 3) That we 9476 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9477 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9478 * SEG.Len, This modified check allows us to overcome RFC1323's 9479 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9480 * p.869. In such cases, we can still calculate the RTT correctly 9481 * when RCV.NXT == Last.ACK.Sent. 9482 */ 9483 if ((to->to_flags & TOF_TS) != 0 && 9484 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9485 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9486 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9487 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9488 tp->ts_recent = to->to_tsval; 9489 } 9490 /* 9491 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9492 * is on (half-synchronized state), then queue data for later 9493 * processing; else drop segment and return. 9494 */ 9495 if ((thflags & TH_ACK) == 0) { 9496 if (tp->t_flags & TF_NEEDSYN) { 9497 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9498 tiwin, thflags, nxt_pkt)); 9499 } else if (tp->t_flags & TF_ACKNOW) { 9500 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9501 bbr->r_wanted_output = 1; 9502 return (ret_val); 9503 } else { 9504 ctf_do_drop(m, NULL); 9505 return (0); 9506 } 9507 } 9508 /* 9509 * Ack processing. 9510 */ 9511 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9512 return (ret_val); 9513 } 9514 if (ourfinisacked) { 9515 /* 9516 * If we can't receive any more data, then closing user can 9517 * proceed. Starting the timer is contrary to the 9518 * specification, but if we don't get a FIN we'll hang 9519 * forever. 9520 * 9521 * XXXjl: we should release the tp also, and use a 9522 * compressed state. 9523 */ 9524 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9525 soisdisconnected(so); 9526 tcp_timer_activate(tp, TT_2MSL, 9527 (tcp_fast_finwait2_recycle ? 9528 tcp_finwait2_timeout : 9529 TP_MAXIDLE(tp))); 9530 } 9531 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9532 } 9533 if (sbavail(&so->so_snd)) { 9534 if (ctf_progress_timeout_check(tp, true)) { 9535 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9536 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9537 return (1); 9538 } 9539 } 9540 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9541 tiwin, thflags, nxt_pkt)); 9542 } 9543 9544 /* 9545 * Return value of 1, the TCB is unlocked and most 9546 * likely gone, return value of 0, the TCB is still 9547 * locked. 9548 */ 9549 static int 9550 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 9551 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9552 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9553 { 9554 int32_t ourfinisacked = 0; 9555 int32_t ret_val; 9556 struct tcp_bbr *bbr; 9557 9558 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9559 9560 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9561 ctf_calc_rwin(so, tp); 9562 if ((thflags & TH_RST) || 9563 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9564 return (ctf_process_rst(m, th, so, tp)); 9565 /* 9566 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9567 * synchronized state. 9568 */ 9569 if (thflags & TH_SYN) { 9570 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9571 return (ret_val); 9572 } 9573 /* 9574 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9575 * it's less than ts_recent, drop it. 9576 */ 9577 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9578 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9579 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9580 return (ret_val); 9581 } 9582 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9583 return (ret_val); 9584 } 9585 /* 9586 * If last ACK falls within this segment's sequence numbers, record 9587 * its timestamp. NOTE: 1) That the test incorporates suggestions 9588 * from the latest proposal of the tcplw@cray.com list (Braden 9589 * 1993/04/26). 2) That updating only on newer timestamps interferes 9590 * with our earlier PAWS tests, so this check should be solely 9591 * predicated on the sequence space of this segment. 3) That we 9592 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9593 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9594 * SEG.Len, This modified check allows us to overcome RFC1323's 9595 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9596 * p.869. In such cases, we can still calculate the RTT correctly 9597 * when RCV.NXT == Last.ACK.Sent. 9598 */ 9599 if ((to->to_flags & TOF_TS) != 0 && 9600 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9601 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9602 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9603 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9604 tp->ts_recent = to->to_tsval; 9605 } 9606 /* 9607 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9608 * is on (half-synchronized state), then queue data for later 9609 * processing; else drop segment and return. 9610 */ 9611 if ((thflags & TH_ACK) == 0) { 9612 if (tp->t_flags & TF_NEEDSYN) { 9613 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9614 tiwin, thflags, nxt_pkt)); 9615 } else if (tp->t_flags & TF_ACKNOW) { 9616 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9617 bbr->r_wanted_output = 1; 9618 return (ret_val); 9619 } else { 9620 ctf_do_drop(m, NULL); 9621 return (0); 9622 } 9623 } 9624 /* 9625 * Ack processing. 9626 */ 9627 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9628 return (ret_val); 9629 } 9630 if (ourfinisacked) { 9631 tcp_twstart(tp); 9632 m_freem(m); 9633 return (1); 9634 } 9635 if (sbavail(&so->so_snd)) { 9636 if (ctf_progress_timeout_check(tp, true)) { 9637 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9638 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9639 return (1); 9640 } 9641 } 9642 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9643 tiwin, thflags, nxt_pkt)); 9644 } 9645 9646 /* 9647 * Return value of 1, the TCB is unlocked and most 9648 * likely gone, return value of 0, the TCB is still 9649 * locked. 9650 */ 9651 static int 9652 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 9653 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9654 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9655 { 9656 int32_t ourfinisacked = 0; 9657 int32_t ret_val; 9658 struct tcp_bbr *bbr; 9659 9660 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9661 9662 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9663 ctf_calc_rwin(so, tp); 9664 if ((thflags & TH_RST) || 9665 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9666 return (ctf_process_rst(m, th, so, tp)); 9667 /* 9668 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9669 * synchronized state. 9670 */ 9671 if (thflags & TH_SYN) { 9672 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9673 return (ret_val); 9674 } 9675 /* 9676 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9677 * it's less than ts_recent, drop it. 9678 */ 9679 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9680 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9681 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9682 return (ret_val); 9683 } 9684 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9685 return (ret_val); 9686 } 9687 /* 9688 * If last ACK falls within this segment's sequence numbers, record 9689 * its timestamp. NOTE: 1) That the test incorporates suggestions 9690 * from the latest proposal of the tcplw@cray.com list (Braden 9691 * 1993/04/26). 2) That updating only on newer timestamps interferes 9692 * with our earlier PAWS tests, so this check should be solely 9693 * predicated on the sequence space of this segment. 3) That we 9694 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9695 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9696 * SEG.Len, This modified check allows us to overcome RFC1323's 9697 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9698 * p.869. In such cases, we can still calculate the RTT correctly 9699 * when RCV.NXT == Last.ACK.Sent. 9700 */ 9701 if ((to->to_flags & TOF_TS) != 0 && 9702 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9703 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9704 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9705 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9706 tp->ts_recent = to->to_tsval; 9707 } 9708 /* 9709 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9710 * is on (half-synchronized state), then queue data for later 9711 * processing; else drop segment and return. 9712 */ 9713 if ((thflags & TH_ACK) == 0) { 9714 if (tp->t_flags & TF_NEEDSYN) { 9715 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9716 tiwin, thflags, nxt_pkt)); 9717 } else if (tp->t_flags & TF_ACKNOW) { 9718 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9719 bbr->r_wanted_output = 1; 9720 return (ret_val); 9721 } else { 9722 ctf_do_drop(m, NULL); 9723 return (0); 9724 } 9725 } 9726 /* 9727 * case TCPS_LAST_ACK: Ack processing. 9728 */ 9729 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9730 return (ret_val); 9731 } 9732 if (ourfinisacked) { 9733 tp = tcp_close(tp); 9734 ctf_do_drop(m, tp); 9735 return (1); 9736 } 9737 if (sbavail(&so->so_snd)) { 9738 if (ctf_progress_timeout_check(tp, true)) { 9739 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9740 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9741 return (1); 9742 } 9743 } 9744 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9745 tiwin, thflags, nxt_pkt)); 9746 } 9747 9748 /* 9749 * Return value of 1, the TCB is unlocked and most 9750 * likely gone, return value of 0, the TCB is still 9751 * locked. 9752 */ 9753 static int 9754 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 9755 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9756 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9757 { 9758 int32_t ourfinisacked = 0; 9759 int32_t ret_val; 9760 struct tcp_bbr *bbr; 9761 9762 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9763 9764 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9765 ctf_calc_rwin(so, tp); 9766 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 9767 if ((thflags & TH_RST) || 9768 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9769 return (ctf_process_rst(m, th, so, tp)); 9770 9771 /* 9772 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9773 * synchronized state. 9774 */ 9775 if (thflags & TH_SYN) { 9776 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9777 return (ret_val); 9778 } 9779 /* 9780 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9781 * it's less than ts_recent, drop it. 9782 */ 9783 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9784 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9785 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9786 return (ret_val); 9787 } 9788 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9789 return (ret_val); 9790 } 9791 /* 9792 * If new data are received on a connection after the user processes 9793 * are gone, then we may RST the other end depending on the outcome 9794 * of bbr_check_data_after_close. 9795 * We call a new function now so we might continue and setup 9796 * to reset at all data being ack'd. 9797 */ 9798 if ((tp->t_flags & TF_CLOSED) && tlen && 9799 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9800 return (1); 9801 /* 9802 * If last ACK falls within this segment's sequence numbers, record 9803 * its timestamp. NOTE: 1) That the test incorporates suggestions 9804 * from the latest proposal of the tcplw@cray.com list (Braden 9805 * 1993/04/26). 2) That updating only on newer timestamps interferes 9806 * with our earlier PAWS tests, so this check should be solely 9807 * predicated on the sequence space of this segment. 3) That we 9808 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9809 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9810 * SEG.Len, This modified check allows us to overcome RFC1323's 9811 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9812 * p.869. In such cases, we can still calculate the RTT correctly 9813 * when RCV.NXT == Last.ACK.Sent. 9814 */ 9815 if ((to->to_flags & TOF_TS) != 0 && 9816 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9817 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9818 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9819 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 9820 tp->ts_recent = to->to_tsval; 9821 } 9822 /* 9823 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9824 * is on (half-synchronized state), then queue data for later 9825 * processing; else drop segment and return. 9826 */ 9827 if ((thflags & TH_ACK) == 0) { 9828 if (tp->t_flags & TF_NEEDSYN) { 9829 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9830 tiwin, thflags, nxt_pkt)); 9831 } else if (tp->t_flags & TF_ACKNOW) { 9832 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9833 bbr->r_wanted_output = 1; 9834 return (ret_val); 9835 } else { 9836 ctf_do_drop(m, NULL); 9837 return (0); 9838 } 9839 } 9840 /* 9841 * Ack processing. 9842 */ 9843 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9844 return (ret_val); 9845 } 9846 if (sbavail(&so->so_snd)) { 9847 if (ctf_progress_timeout_check(tp, true)) { 9848 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9849 ctf_do_dropwithreset_conn(m, tp, th, tlen); 9850 return (1); 9851 } 9852 } 9853 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9854 tiwin, thflags, nxt_pkt)); 9855 } 9856 9857 static void 9858 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr) 9859 { 9860 /* 9861 * Assure no timers are running. 9862 */ 9863 if (tcp_timer_active(tp, TT_PERSIST)) { 9864 /* We enter in persists, set the flag appropriately */ 9865 bbr->rc_in_persist = 1; 9866 } 9867 if (tcp_in_hpts(bbr->rc_tp)) { 9868 tcp_hpts_remove(bbr->rc_tp); 9869 } 9870 } 9871 9872 static void 9873 bbr_google_mode_on(struct tcp_bbr *bbr) 9874 { 9875 bbr->rc_use_google = 1; 9876 bbr->rc_no_pacing = 0; 9877 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9878 bbr->r_use_policer = bbr_policer_detection_enabled; 9879 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 9880 bbr->bbr_use_rack_cheat = 0; 9881 bbr->r_ctl.rc_incr_tmrs = 0; 9882 bbr->r_ctl.rc_inc_tcp_oh = 0; 9883 bbr->r_ctl.rc_inc_ip_oh = 0; 9884 bbr->r_ctl.rc_inc_enet_oh = 0; 9885 reset_time(&bbr->r_ctl.rc_delrate, 9886 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 9887 reset_time_small(&bbr->r_ctl.rc_rttprop, 9888 (11 * USECS_IN_SECOND)); 9889 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9890 } 9891 9892 static void 9893 bbr_google_mode_off(struct tcp_bbr *bbr) 9894 { 9895 bbr->rc_use_google = 0; 9896 bbr->r_ctl.bbr_google_discount = 0; 9897 bbr->no_pacing_until = bbr_no_pacing_until; 9898 bbr->r_use_policer = 0; 9899 if (bbr->no_pacing_until) 9900 bbr->rc_no_pacing = 1; 9901 else 9902 bbr->rc_no_pacing = 0; 9903 if (bbr_use_rack_resend_cheat) 9904 bbr->bbr_use_rack_cheat = 1; 9905 else 9906 bbr->bbr_use_rack_cheat = 0; 9907 if (bbr_incr_timers) 9908 bbr->r_ctl.rc_incr_tmrs = 1; 9909 else 9910 bbr->r_ctl.rc_incr_tmrs = 0; 9911 if (bbr_include_tcp_oh) 9912 bbr->r_ctl.rc_inc_tcp_oh = 1; 9913 else 9914 bbr->r_ctl.rc_inc_tcp_oh = 0; 9915 if (bbr_include_ip_oh) 9916 bbr->r_ctl.rc_inc_ip_oh = 1; 9917 else 9918 bbr->r_ctl.rc_inc_ip_oh = 0; 9919 if (bbr_include_enet_oh) 9920 bbr->r_ctl.rc_inc_enet_oh = 1; 9921 else 9922 bbr->r_ctl.rc_inc_enet_oh = 0; 9923 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 9924 reset_time(&bbr->r_ctl.rc_delrate, 9925 bbr_num_pktepo_for_del_limit); 9926 reset_time_small(&bbr->r_ctl.rc_rttprop, 9927 (bbr_filter_len_sec * USECS_IN_SECOND)); 9928 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9929 } 9930 /* 9931 * Return 0 on success, non-zero on failure 9932 * which indicates the error (usually no memory). 9933 */ 9934 static int 9935 bbr_init(struct tcpcb *tp, void **ptr) 9936 { 9937 struct inpcb *inp = tptoinpcb(tp); 9938 struct tcp_bbr *bbr = NULL; 9939 uint32_t cts; 9940 9941 tcp_hpts_init(tp); 9942 9943 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO)); 9944 if (*ptr == NULL) { 9945 /* 9946 * We need to allocate memory but cant. The INP and INP_INFO 9947 * locks and they are recursive (happens during setup. So a 9948 * scheme to drop the locks fails :( 9949 * 9950 */ 9951 return (ENOMEM); 9952 } 9953 bbr = (struct tcp_bbr *)*ptr; 9954 bbr->rtt_valid = 0; 9955 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 9956 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 9957 /* Take off any undesired flags */ 9958 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 9959 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 9960 tp->t_flags2 &= ~TF2_MBUF_ACKCMP; 9961 tp->t_flags2 &= ~TF2_MBUF_L_ACKS; 9962 9963 TAILQ_INIT(&bbr->r_ctl.rc_map); 9964 TAILQ_INIT(&bbr->r_ctl.rc_free); 9965 TAILQ_INIT(&bbr->r_ctl.rc_tmap); 9966 bbr->rc_tp = tp; 9967 bbr->rc_inp = inp; 9968 cts = tcp_get_usecs(&bbr->rc_tv); 9969 tp->t_acktime = 0; 9970 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close; 9971 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade; 9972 bbr->rc_tlp_threshold = bbr_tlp_thresh; 9973 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh; 9974 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay; 9975 bbr->r_ctl.rc_min_to = bbr_min_to; 9976 bbr->rc_bbr_state = BBR_STATE_STARTUP; 9977 bbr->r_ctl.bbr_lost_at_state = 0; 9978 bbr->r_ctl.rc_lost_at_startup = 0; 9979 bbr->rc_all_timers_stopped = 0; 9980 bbr->r_ctl.rc_bbr_lastbtlbw = 0; 9981 bbr->r_ctl.rc_pkt_epoch_del = 0; 9982 bbr->r_ctl.rc_pkt_epoch = 0; 9983 bbr->r_ctl.rc_lowest_rtt = 0xffffffff; 9984 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain; 9985 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 9986 bbr->r_ctl.rc_went_idle_time = cts; 9987 bbr->rc_pacer_started = cts; 9988 bbr->r_ctl.rc_pkt_epoch_time = cts; 9989 bbr->r_ctl.rc_rcvtime = cts; 9990 bbr->r_ctl.rc_bbr_state_time = cts; 9991 bbr->r_ctl.rc_del_time = cts; 9992 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 9993 bbr->r_ctl.last_in_probertt = cts; 9994 bbr->skip_gain = 0; 9995 bbr->gain_is_limited = 0; 9996 bbr->no_pacing_until = bbr_no_pacing_until; 9997 if (bbr->no_pacing_until) 9998 bbr->rc_no_pacing = 1; 9999 if (bbr_use_google_algo) { 10000 bbr->rc_no_pacing = 0; 10001 bbr->rc_use_google = 1; 10002 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 10003 bbr->r_use_policer = bbr_policer_detection_enabled; 10004 } else { 10005 bbr->rc_use_google = 0; 10006 bbr->r_ctl.bbr_google_discount = 0; 10007 bbr->r_use_policer = 0; 10008 } 10009 if (bbr_ts_limiting) 10010 bbr->rc_use_ts_limit = 1; 10011 else 10012 bbr->rc_use_ts_limit = 0; 10013 if (bbr_ts_can_raise) 10014 bbr->ts_can_raise = 1; 10015 else 10016 bbr->ts_can_raise = 0; 10017 if (V_tcp_delack_enabled == 1) 10018 tp->t_delayed_ack = 2; 10019 else if (V_tcp_delack_enabled == 0) 10020 tp->t_delayed_ack = 0; 10021 else if (V_tcp_delack_enabled < 100) 10022 tp->t_delayed_ack = V_tcp_delack_enabled; 10023 else 10024 tp->t_delayed_ack = 2; 10025 if (bbr->rc_use_google == 0) 10026 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10027 else 10028 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 10029 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms; 10030 bbr->rc_max_rto_sec = bbr_rto_max_sec; 10031 bbr->rc_init_win = bbr_def_init_win; 10032 if (tp->t_flags & TF_REQ_TSTMP) 10033 bbr->rc_last_options = TCP_TS_OVERHEAD; 10034 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options; 10035 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd; 10036 bbr->r_init_rtt = 1; 10037 10038 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 10039 if (bbr_allow_hdwr_pacing) 10040 bbr->bbr_hdw_pace_ena = 1; 10041 else 10042 bbr->bbr_hdw_pace_ena = 0; 10043 if (bbr_sends_full_iwnd) 10044 bbr->bbr_init_win_cheat = 1; 10045 else 10046 bbr->bbr_init_win_cheat = 0; 10047 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max; 10048 bbr->r_ctl.rc_drain_pg = bbr_drain_gain; 10049 bbr->r_ctl.rc_startup_pg = bbr_high_gain; 10050 bbr->rc_loss_exit = bbr_exit_startup_at_loss; 10051 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain; 10052 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second; 10053 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar; 10054 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max; 10055 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor; 10056 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min; 10057 bbr->r_ctl.bbr_cross_over = bbr_cross_over; 10058 bbr->r_ctl.rc_rtt_shrinks = cts; 10059 if (bbr->rc_use_google) { 10060 setup_time_filter(&bbr->r_ctl.rc_delrate, 10061 FILTER_TYPE_MAX, 10062 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 10063 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10064 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND)); 10065 } else { 10066 setup_time_filter(&bbr->r_ctl.rc_delrate, 10067 FILTER_TYPE_MAX, 10068 bbr_num_pktepo_for_del_limit); 10069 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10070 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND)); 10071 } 10072 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0); 10073 if (bbr_uses_idle_restart) 10074 bbr->rc_use_idle_restart = 1; 10075 else 10076 bbr->rc_use_idle_restart = 0; 10077 bbr->r_ctl.rc_bbr_cur_del_rate = 0; 10078 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps; 10079 if (bbr_resends_use_tso) 10080 bbr->rc_resends_use_tso = 1; 10081 if (tp->snd_una != tp->snd_max) { 10082 /* Create a send map for the current outstanding data */ 10083 struct bbr_sendmap *rsm; 10084 10085 rsm = bbr_alloc(bbr); 10086 if (rsm == NULL) { 10087 uma_zfree(bbr_pcb_zone, *ptr); 10088 *ptr = NULL; 10089 return (ENOMEM); 10090 } 10091 rsm->r_rtt_not_allowed = 1; 10092 rsm->r_tim_lastsent[0] = cts; 10093 rsm->r_rtr_cnt = 1; 10094 rsm->r_rtr_bytes = 0; 10095 rsm->r_start = tp->snd_una; 10096 rsm->r_end = tp->snd_max; 10097 rsm->r_dupack = 0; 10098 rsm->r_delivered = bbr->r_ctl.rc_delivered; 10099 rsm->r_ts_valid = 0; 10100 rsm->r_del_ack_ts = tp->ts_recent; 10101 rsm->r_del_time = cts; 10102 if (bbr->r_ctl.r_app_limited_until) 10103 rsm->r_app_limited = 1; 10104 else 10105 rsm->r_app_limited = 0; 10106 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 10107 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 10108 rsm->r_in_tmap = 1; 10109 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 10110 rsm->r_bbr_state = bbr_state_val(bbr); 10111 else 10112 rsm->r_bbr_state = 8; 10113 } 10114 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0)) 10115 bbr->bbr_use_rack_cheat = 1; 10116 if (bbr_incr_timers && (bbr->rc_use_google == 0)) 10117 bbr->r_ctl.rc_incr_tmrs = 1; 10118 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0)) 10119 bbr->r_ctl.rc_inc_tcp_oh = 1; 10120 if (bbr_include_ip_oh && (bbr->rc_use_google == 0)) 10121 bbr->r_ctl.rc_inc_ip_oh = 1; 10122 if (bbr_include_enet_oh && (bbr->rc_use_google == 0)) 10123 bbr->r_ctl.rc_inc_enet_oh = 1; 10124 10125 bbr_log_type_statechange(bbr, cts, __LINE__); 10126 if (TCPS_HAVEESTABLISHED(tp->t_state) && 10127 (tp->t_srtt)) { 10128 uint32_t rtt; 10129 10130 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 10131 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 10132 } 10133 /* announce the settings and state */ 10134 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT); 10135 tcp_bbr_tso_size_check(bbr, cts); 10136 /* 10137 * Now call the generic function to start a timer. This will place 10138 * the TCB on the hptsi wheel if a timer is needed with appropriate 10139 * flags. 10140 */ 10141 bbr_stop_all_timers(tp, bbr); 10142 /* 10143 * Validate the timers are not in usec, if they are convert. 10144 * BBR should in theory move to USEC and get rid of a 10145 * lot of the TICKS_2 calls.. but for now we stay 10146 * with tick timers. 10147 */ 10148 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 10149 TCPT_RANGESET(tp->t_rxtcur, 10150 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 10151 tp->t_rttmin, tcp_rexmit_max); 10152 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0); 10153 return (0); 10154 } 10155 10156 /* 10157 * Return 0 if we can accept the connection. Return 10158 * non-zero if we can't handle the connection. A EAGAIN 10159 * means you need to wait until the connection is up. 10160 * a EADDRNOTAVAIL means we can never handle the connection 10161 * (no SACK). 10162 */ 10163 static int 10164 bbr_handoff_ok(struct tcpcb *tp) 10165 { 10166 if ((tp->t_state == TCPS_CLOSED) || 10167 (tp->t_state == TCPS_LISTEN)) { 10168 /* Sure no problem though it may not stick */ 10169 return (0); 10170 } 10171 if ((tp->t_state == TCPS_SYN_SENT) || 10172 (tp->t_state == TCPS_SYN_RECEIVED)) { 10173 /* 10174 * We really don't know you have to get to ESTAB or beyond 10175 * to tell. 10176 */ 10177 return (EAGAIN); 10178 } 10179 if (tp->t_flags & TF_SENTFIN) 10180 return (EINVAL); 10181 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) { 10182 return (0); 10183 } 10184 /* 10185 * If we reach here we don't do SACK on this connection so we can 10186 * never do rack. 10187 */ 10188 return (EINVAL); 10189 } 10190 10191 static void 10192 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged) 10193 { 10194 if (tp->t_fb_ptr) { 10195 uint32_t calc; 10196 struct tcp_bbr *bbr; 10197 struct bbr_sendmap *rsm; 10198 10199 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 10200 if (bbr->r_ctl.crte) 10201 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 10202 bbr_log_flowend(bbr); 10203 bbr->rc_tp = NULL; 10204 if (bbr->bbr_hdrw_pacing) 10205 counter_u64_add(bbr_flows_whdwr_pacing, -1); 10206 else 10207 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 10208 if (bbr->r_ctl.crte != NULL) { 10209 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 10210 bbr->r_ctl.crte = NULL; 10211 } 10212 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10213 while (rsm) { 10214 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 10215 uma_zfree(bbr_zone, rsm); 10216 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10217 } 10218 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10219 while (rsm) { 10220 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 10221 uma_zfree(bbr_zone, rsm); 10222 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10223 } 10224 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd; 10225 if (calc > (bbr->r_ctl.rc_init_rwnd / 10)) 10226 BBR_STAT_INC(bbr_dynamic_rwnd); 10227 else 10228 BBR_STAT_INC(bbr_static_rwnd); 10229 bbr->r_ctl.rc_free_cnt = 0; 10230 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr); 10231 tp->t_fb_ptr = NULL; 10232 } 10233 /* Make sure snd_nxt is correctly set */ 10234 tp->snd_nxt = tp->snd_max; 10235 } 10236 10237 static void 10238 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win) 10239 { 10240 switch (tp->t_state) { 10241 case TCPS_SYN_SENT: 10242 bbr->r_state = TCPS_SYN_SENT; 10243 bbr->r_substate = bbr_do_syn_sent; 10244 break; 10245 case TCPS_SYN_RECEIVED: 10246 bbr->r_state = TCPS_SYN_RECEIVED; 10247 bbr->r_substate = bbr_do_syn_recv; 10248 break; 10249 case TCPS_ESTABLISHED: 10250 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd); 10251 bbr->r_state = TCPS_ESTABLISHED; 10252 bbr->r_substate = bbr_do_established; 10253 break; 10254 case TCPS_CLOSE_WAIT: 10255 bbr->r_state = TCPS_CLOSE_WAIT; 10256 bbr->r_substate = bbr_do_close_wait; 10257 break; 10258 case TCPS_FIN_WAIT_1: 10259 bbr->r_state = TCPS_FIN_WAIT_1; 10260 bbr->r_substate = bbr_do_fin_wait_1; 10261 break; 10262 case TCPS_CLOSING: 10263 bbr->r_state = TCPS_CLOSING; 10264 bbr->r_substate = bbr_do_closing; 10265 break; 10266 case TCPS_LAST_ACK: 10267 bbr->r_state = TCPS_LAST_ACK; 10268 bbr->r_substate = bbr_do_lastack; 10269 break; 10270 case TCPS_FIN_WAIT_2: 10271 bbr->r_state = TCPS_FIN_WAIT_2; 10272 bbr->r_substate = bbr_do_fin_wait_2; 10273 break; 10274 case TCPS_LISTEN: 10275 case TCPS_CLOSED: 10276 case TCPS_TIME_WAIT: 10277 default: 10278 break; 10279 }; 10280 } 10281 10282 static void 10283 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog) 10284 { 10285 /* 10286 * Now what state are we going into now? Is there adjustments 10287 * needed? 10288 */ 10289 int32_t old_state; 10290 10291 old_state = bbr_state_val(bbr); 10292 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) { 10293 /* Save the lowest srtt we saw in our end of the sub-state */ 10294 bbr->rc_hit_state_1 = 0; 10295 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff) 10296 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state; 10297 } 10298 bbr->rc_bbr_substate++; 10299 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10300 /* 10301 * We enter the gain(5/4) cycle (possibly less if 10302 * shallow buffer detection is enabled) 10303 */ 10304 if (bbr->skip_gain) { 10305 /* 10306 * Hardware pacing has set our rate to 10307 * the max and limited our b/w just 10308 * do level i.e. no gain. 10309 */ 10310 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1]; 10311 } else if (bbr->gain_is_limited && 10312 bbr->bbr_hdrw_pacing && 10313 bbr->r_ctl.crte) { 10314 /* 10315 * We can't gain above the hardware pacing 10316 * rate which is less than our rate + the gain 10317 * calculate the gain needed to reach the hardware 10318 * pacing rate.. 10319 */ 10320 uint64_t bw, rate, gain_calc; 10321 10322 bw = bbr_get_bw(bbr); 10323 rate = bbr->r_ctl.crte->rate; 10324 if ((rate > bw) && 10325 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) { 10326 gain_calc = (rate * BBR_UNIT) / bw; 10327 if (gain_calc < BBR_UNIT) 10328 gain_calc = BBR_UNIT; 10329 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc; 10330 } else { 10331 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10332 } 10333 } else 10334 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10335 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) { 10336 bbr->r_ctl.rc_bbr_state_atflight = cts; 10337 } else 10338 bbr->r_ctl.rc_bbr_state_atflight = 0; 10339 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10340 bbr->rc_hit_state_1 = 1; 10341 bbr->r_ctl.rc_exta_time_gd = 0; 10342 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10343 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10344 if (bbr_state_drain_2_tar) { 10345 bbr->r_ctl.rc_bbr_state_atflight = 0; 10346 } else 10347 bbr->r_ctl.rc_bbr_state_atflight = cts; 10348 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN]; 10349 } else { 10350 /* All other cycles hit here 2-7 */ 10351 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) { 10352 if (bbr_sub_drain_slam_cwnd && 10353 (bbr->rc_use_google == 0) && 10354 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10355 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10356 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10357 } 10358 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP)) 10359 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) - 10360 bbr_get_rtt(bbr, BBR_RTT_PROP)); 10361 else 10362 bbr->r_ctl.rc_exta_time_gd = 0; 10363 if (bbr->r_ctl.rc_exta_time_gd) { 10364 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd; 10365 /* Now chop up the time for each state (div by 7) */ 10366 bbr->r_ctl.rc_level_state_extra /= 7; 10367 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) { 10368 /* Add a randomization */ 10369 bbr_randomize_extra_state_time(bbr); 10370 } 10371 } 10372 } 10373 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10374 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)]; 10375 } 10376 if (bbr->rc_use_google) { 10377 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10378 } 10379 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10380 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10381 if (dolog) 10382 bbr_log_type_statechange(bbr, cts, line); 10383 10384 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10385 uint32_t time_in; 10386 10387 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10388 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 10389 counter_u64_add(bbr_state_time[(old_state + 5)], time_in); 10390 } else { 10391 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10392 } 10393 } 10394 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 10395 bbr_set_state_target(bbr, __LINE__); 10396 if (bbr_sub_drain_slam_cwnd && 10397 (bbr->rc_use_google == 0) && 10398 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10399 /* Slam down the cwnd */ 10400 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10401 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10402 if (bbr_sub_drain_app_limit) { 10403 /* Go app limited if we are on a long drain */ 10404 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + 10405 ctf_flight_size(bbr->rc_tp, 10406 (bbr->r_ctl.rc_sacked + 10407 bbr->r_ctl.rc_lost_bytes))); 10408 } 10409 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10410 } 10411 if (bbr->rc_lt_use_bw) { 10412 /* In policed mode we clamp pacing_gain to BBR_UNIT */ 10413 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10414 } 10415 /* Google changes TSO size every cycle */ 10416 if (bbr->rc_use_google) 10417 tcp_bbr_tso_size_check(bbr, cts); 10418 bbr->r_ctl.gain_epoch = cts; 10419 bbr->r_ctl.rc_bbr_state_time = cts; 10420 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch; 10421 } 10422 10423 static void 10424 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10425 { 10426 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) && 10427 (google_allow_early_out == 1) && 10428 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) { 10429 /* We have reached out target flight size possibly early */ 10430 goto change_state; 10431 } 10432 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10433 return; 10434 } 10435 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) { 10436 /* 10437 * Must be a rttProp movement forward before 10438 * we can change states. 10439 */ 10440 return; 10441 } 10442 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10443 /* 10444 * The needed time has passed but for 10445 * the gain cycle extra rules apply: 10446 * 1) If we have seen loss, we exit 10447 * 2) If we have not reached the target 10448 * we stay in GAIN (gain-to-target). 10449 */ 10450 if (google_consider_lost && losses) 10451 goto change_state; 10452 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) { 10453 return; 10454 } 10455 } 10456 change_state: 10457 /* For gain we must reach our target, all others last 1 rttProp */ 10458 bbr_substate_change(bbr, cts, __LINE__, 1); 10459 } 10460 10461 static void 10462 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10463 { 10464 uint32_t flight, bbr_cur_cycle_time; 10465 10466 if (bbr->rc_use_google) { 10467 bbr_set_probebw_google_gains(bbr, cts, losses); 10468 return; 10469 } 10470 if (cts == 0) { 10471 /* 10472 * Never alow cts to be 0 we 10473 * do this so we can judge if 10474 * we have set a timestamp. 10475 */ 10476 cts = 1; 10477 } 10478 if (bbr_state_is_pkt_epoch) 10479 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 10480 else 10481 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP); 10482 10483 if (bbr->r_ctl.rc_bbr_state_atflight == 0) { 10484 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10485 flight = ctf_flight_size(bbr->rc_tp, 10486 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10487 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) { 10488 /* Keep it slam down */ 10489 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) { 10490 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10491 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10492 } 10493 if (bbr_sub_drain_app_limit) { 10494 /* Go app limited if we are on a long drain */ 10495 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight); 10496 } 10497 } 10498 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) && 10499 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) || 10500 (flight >= bbr->r_ctl.flightsize_at_drain))) { 10501 /* 10502 * Still here after the same time as 10503 * the gain. We need to drain harder 10504 * for the next srtt. Reduce by a set amount 10505 * the gain drop is capped at DRAIN states 10506 * value (88). 10507 */ 10508 bbr->r_ctl.flightsize_at_drain = flight; 10509 if (bbr_drain_drop_mul && 10510 bbr_drain_drop_div && 10511 (bbr_drain_drop_mul < bbr_drain_drop_div)) { 10512 /* Use your specific drop value (def 4/5 = 20%) */ 10513 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul; 10514 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div; 10515 } else { 10516 /* You get drop of 20% */ 10517 bbr->r_ctl.rc_bbr_hptsi_gain *= 4; 10518 bbr->r_ctl.rc_bbr_hptsi_gain /= 5; 10519 } 10520 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) { 10521 /* Reduce our gain again to the bottom */ 10522 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 10523 } 10524 bbr_log_exit_gain(bbr, cts, 4); 10525 /* 10526 * Extend out so we wait another 10527 * epoch before dropping again. 10528 */ 10529 bbr->r_ctl.gain_epoch = cts; 10530 } 10531 if (flight <= bbr->r_ctl.rc_target_at_state) { 10532 if (bbr_sub_drain_slam_cwnd && 10533 (bbr->rc_use_google == 0) && 10534 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10535 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10536 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10537 } 10538 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10539 bbr_log_exit_gain(bbr, cts, 3); 10540 } 10541 } else { 10542 /* Its a gain */ 10543 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) { 10544 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10545 goto change_state; 10546 } 10547 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) || 10548 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >= 10549 bbr->rc_tp->snd_wnd)) { 10550 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10551 bbr_log_exit_gain(bbr, cts, 2); 10552 } 10553 } 10554 /** 10555 * We fall through and return always one of two things has 10556 * occurred. 10557 * 1) We are still not at target 10558 * <or> 10559 * 2) We reached the target and set rc_bbr_state_atflight 10560 * which means we no longer hit this block 10561 * next time we are called. 10562 */ 10563 return; 10564 } 10565 change_state: 10566 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) 10567 return; 10568 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) { 10569 /* Less than a full time-period has passed */ 10570 return; 10571 } 10572 if (bbr->r_ctl.rc_level_state_extra && 10573 (bbr_state_val(bbr) > BBR_SUB_DRAIN) && 10574 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10575 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10576 /* Less than a full time-period + extra has passed */ 10577 return; 10578 } 10579 if (bbr_gain_gets_extra_too && 10580 bbr->r_ctl.rc_level_state_extra && 10581 (bbr_state_val(bbr) == BBR_SUB_GAIN) && 10582 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10583 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10584 /* Less than a full time-period + extra has passed */ 10585 return; 10586 } 10587 bbr_substate_change(bbr, cts, __LINE__, 1); 10588 } 10589 10590 static uint32_t 10591 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain) 10592 { 10593 uint32_t mss, tar; 10594 10595 if (bbr->rc_use_google) { 10596 /* Google just uses the cwnd target */ 10597 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain); 10598 } else { 10599 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 10600 bbr->r_ctl.rc_pace_max_segs); 10601 /* Get the base cwnd with gain rounded to a mss */ 10602 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr), 10603 gain), mss); 10604 /* Make sure it is within our min */ 10605 if (tar < get_min_cwnd(bbr)) 10606 return (get_min_cwnd(bbr)); 10607 } 10608 return (tar); 10609 } 10610 10611 static void 10612 bbr_set_state_target(struct tcp_bbr *bbr, int line) 10613 { 10614 uint32_t tar, meth; 10615 10616 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 10617 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 10618 /* Special case using old probe-rtt method */ 10619 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10620 meth = 1; 10621 } else { 10622 /* Non-probe-rtt case and reduced probe-rtt */ 10623 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 10624 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) { 10625 /* For gain cycle we use the hptsi gain */ 10626 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10627 meth = 2; 10628 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) { 10629 /* 10630 * If configured, or for google all other states 10631 * get BBR_UNIT. 10632 */ 10633 tar = bbr_get_a_state_target(bbr, BBR_UNIT); 10634 meth = 3; 10635 } else { 10636 /* 10637 * Or we set a target based on the pacing gain 10638 * for non-google mode and default (non-configured). 10639 * Note we don't set a target goal below drain (192). 10640 */ 10641 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) { 10642 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]); 10643 meth = 4; 10644 } else { 10645 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10646 meth = 5; 10647 } 10648 } 10649 } 10650 bbr_log_set_of_state_target(bbr, tar, line, meth); 10651 bbr->r_ctl.rc_target_at_state = tar; 10652 } 10653 10654 static void 10655 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 10656 { 10657 /* Change to probe_rtt */ 10658 uint32_t time_in; 10659 10660 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10661 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10662 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10663 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain 10664 + bbr->r_ctl.rc_delivered); 10665 /* Setup so we force feed the filter */ 10666 if (bbr->rc_use_google || bbr_probertt_sets_rtt) 10667 bbr->rc_prtt_set_ts = 1; 10668 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10669 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10670 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10671 } 10672 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0); 10673 bbr->r_ctl.rc_rtt_shrinks = cts; 10674 bbr->r_ctl.last_in_probertt = cts; 10675 bbr->r_ctl.rc_probertt_srttchktim = cts; 10676 bbr->r_ctl.rc_bbr_state_time = cts; 10677 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT; 10678 /* We need to force the filter to update */ 10679 10680 if ((bbr_sub_drain_slam_cwnd) && 10681 bbr->rc_hit_state_1 && 10682 (bbr->rc_use_google == 0) && 10683 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10684 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd) 10685 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10686 } else 10687 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10688 /* Update the lost */ 10689 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10690 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){ 10691 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */ 10692 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10693 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10694 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10695 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10696 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6); 10697 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd; 10698 } else { 10699 /* 10700 * We bring it down slowly by using a hptsi gain that is 10701 * probably 75%. This will slowly float down our outstanding 10702 * without tampering with the cwnd. 10703 */ 10704 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 10705 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10706 bbr_set_state_target(bbr, __LINE__); 10707 if (bbr_prtt_slam_cwnd && 10708 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 10709 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10710 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10711 } 10712 } 10713 if (ctf_flight_size(bbr->rc_tp, 10714 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 10715 bbr->r_ctl.rc_target_at_state) { 10716 /* We are at target */ 10717 bbr->r_ctl.rc_bbr_enters_probertt = cts; 10718 } else { 10719 /* We need to come down to reach target before our time begins */ 10720 bbr->r_ctl.rc_bbr_enters_probertt = 0; 10721 } 10722 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch; 10723 BBR_STAT_INC(bbr_enter_probertt); 10724 bbr_log_exit_gain(bbr, cts, 0); 10725 bbr_log_type_statechange(bbr, cts, line); 10726 } 10727 10728 static void 10729 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts) 10730 { 10731 /* 10732 * Sanity check on probe-rtt intervals. 10733 * In crazy situations where we are competing 10734 * against new-reno flows with huge buffers 10735 * our rtt-prop interval could come to dominate 10736 * things if we can't get through a full set 10737 * of cycles, we need to adjust it. 10738 */ 10739 if (bbr_can_adjust_probertt && 10740 (bbr->rc_use_google == 0)) { 10741 uint16_t val = 0; 10742 uint32_t cur_rttp, fval, newval, baseval; 10743 10744 /* Are we to small and go into probe-rtt to often? */ 10745 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1)); 10746 cur_rttp = roundup(baseval, USECS_IN_SECOND); 10747 fval = bbr_filter_len_sec * USECS_IN_SECOND; 10748 if (bbr_is_ratio == 0) { 10749 if (fval > bbr_rtt_probe_limit) 10750 newval = cur_rttp + (fval - bbr_rtt_probe_limit); 10751 else 10752 newval = cur_rttp; 10753 } else { 10754 int mul; 10755 10756 mul = fval / bbr_rtt_probe_limit; 10757 newval = cur_rttp * mul; 10758 } 10759 if (cur_rttp > bbr->r_ctl.rc_probertt_int) { 10760 bbr->r_ctl.rc_probertt_int = cur_rttp; 10761 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10762 val = 1; 10763 } else { 10764 /* 10765 * No adjustments were made 10766 * do we need to shrink it? 10767 */ 10768 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) { 10769 if (cur_rttp <= bbr_rtt_probe_limit) { 10770 /* 10771 * Things have calmed down lets 10772 * shrink all the way to default 10773 */ 10774 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10775 reset_time_small(&bbr->r_ctl.rc_rttprop, 10776 (bbr_filter_len_sec * USECS_IN_SECOND)); 10777 cur_rttp = bbr_rtt_probe_limit; 10778 newval = (bbr_filter_len_sec * USECS_IN_SECOND); 10779 val = 2; 10780 } else { 10781 /* 10782 * Well does some adjustment make sense? 10783 */ 10784 if (cur_rttp < bbr->r_ctl.rc_probertt_int) { 10785 /* We can reduce interval time some */ 10786 bbr->r_ctl.rc_probertt_int = cur_rttp; 10787 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10788 val = 3; 10789 } 10790 } 10791 } 10792 } 10793 if (val) 10794 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val); 10795 } 10796 } 10797 10798 static void 10799 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 10800 { 10801 /* Exit probe-rtt */ 10802 10803 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) { 10804 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10805 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10806 } 10807 bbr_log_exit_gain(bbr, cts, 1); 10808 bbr->rc_hit_state_1 = 0; 10809 bbr->r_ctl.rc_rtt_shrinks = cts; 10810 bbr->r_ctl.last_in_probertt = cts; 10811 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0); 10812 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10813 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, 10814 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 10815 bbr->r_ctl.rc_delivered); 10816 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10817 uint32_t time_in; 10818 10819 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10820 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10821 } 10822 if (bbr->rc_filled_pipe) { 10823 /* Switch to probe_bw */ 10824 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 10825 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 10826 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10827 bbr_substate_change(bbr, cts, __LINE__, 0); 10828 bbr_log_type_statechange(bbr, cts, __LINE__); 10829 } else { 10830 /* Back to startup */ 10831 bbr->rc_bbr_state = BBR_STATE_STARTUP; 10832 bbr->r_ctl.rc_bbr_state_time = cts; 10833 /* 10834 * We don't want to give a complete free 3 10835 * measurements until we exit, so we use 10836 * the number of pe's we were in probe-rtt 10837 * to add to the startup_epoch. That way 10838 * we will still retain the old state. 10839 */ 10840 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt); 10841 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10842 /* Make sure to use the lower pg when shifting back in */ 10843 if (bbr->r_ctl.rc_lost && 10844 bbr_use_lower_gain_in_startup && 10845 (bbr->rc_use_google == 0)) 10846 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10847 else 10848 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 10849 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 10850 /* Probably not needed but set it anyway */ 10851 bbr_set_state_target(bbr, __LINE__); 10852 bbr_log_type_statechange(bbr, cts, __LINE__); 10853 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10854 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0); 10855 } 10856 bbr_check_probe_rtt_limits(bbr, cts); 10857 } 10858 10859 static int32_t inline 10860 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts) 10861 { 10862 if ((bbr->rc_past_init_win == 1) && 10863 (bbr->rc_in_persist == 0) && 10864 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) { 10865 return (1); 10866 } 10867 if (bbr_can_force_probertt && 10868 (bbr->rc_in_persist == 0) && 10869 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 10870 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 10871 return (1); 10872 } 10873 return (0); 10874 } 10875 10876 static int32_t 10877 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch) 10878 { 10879 uint64_t btlbw, gain; 10880 if (pkt_epoch == 0) { 10881 /* 10882 * Need to be on a pkt-epoch to continue. 10883 */ 10884 return (0); 10885 } 10886 btlbw = bbr_get_full_bw(bbr); 10887 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10888 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10889 if (btlbw >= gain) { 10890 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 10891 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10892 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 10893 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10894 } 10895 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) 10896 return (1); 10897 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10898 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 10899 return(0); 10900 } 10901 10902 static int32_t inline 10903 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch) 10904 { 10905 /* Have we gained 25% in the last 3 packet based epoch's? */ 10906 uint64_t btlbw, gain; 10907 int do_exit; 10908 int delta, rtt_gain; 10909 10910 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 10911 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 10912 /* 10913 * This qualifies as a RTT_PROBE session since we drop the 10914 * data outstanding to nothing and waited more than 10915 * bbr_rtt_probe_time. 10916 */ 10917 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 10918 bbr_set_reduced_rtt(bbr, cts, __LINE__); 10919 } 10920 if (bbr_should_enter_probe_rtt(bbr, cts)) { 10921 bbr_enter_probe_rtt(bbr, cts, __LINE__); 10922 return (0); 10923 } 10924 if (bbr->rc_use_google) 10925 return (bbr_google_startup(bbr, cts, pkt_epoch)); 10926 10927 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 10928 (bbr_use_lower_gain_in_startup)) { 10929 /* Drop to a lower gain 1.5 x since we saw loss */ 10930 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10931 } 10932 if (pkt_epoch == 0) { 10933 /* 10934 * Need to be on a pkt-epoch to continue. 10935 */ 10936 return (0); 10937 } 10938 if (bbr_rtt_gain_thresh) { 10939 /* 10940 * Do we allow a flow to stay 10941 * in startup with no loss and no 10942 * gain in rtt over a set threshold? 10943 */ 10944 if (bbr->r_ctl.rc_pkt_epoch_rtt && 10945 bbr->r_ctl.startup_last_srtt && 10946 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) { 10947 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt; 10948 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt; 10949 } else 10950 rtt_gain = 0; 10951 if ((bbr->r_ctl.startup_last_srtt == 0) || 10952 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt)) 10953 /* First time or new lower value */ 10954 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 10955 10956 if ((bbr->r_ctl.rc_lost == 0) && 10957 (rtt_gain < bbr_rtt_gain_thresh)) { 10958 /* 10959 * No loss, and we are under 10960 * our gain threhold for 10961 * increasing RTT. 10962 */ 10963 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10964 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10965 bbr_log_startup_event(bbr, cts, rtt_gain, 10966 delta, bbr->r_ctl.startup_last_srtt, 10); 10967 return (0); 10968 } 10969 } 10970 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) && 10971 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) && 10972 (!IN_RECOVERY(bbr->rc_tp->t_flags))) { 10973 /* 10974 * We only assess if we have a new measurement when 10975 * we have no loss and are not in recovery. 10976 * Drag up by one our last_startup epoch so we will hold 10977 * the number of non-gain we have already accumulated. 10978 */ 10979 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10980 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10981 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10982 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9); 10983 return (0); 10984 } 10985 /* Case where we reduced the lost (bad retransmit) */ 10986 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost) 10987 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10988 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count; 10989 btlbw = bbr_get_full_bw(bbr); 10990 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower) 10991 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10992 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10993 else 10994 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10995 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10996 do_exit = 0; 10997 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw) 10998 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10999 if (btlbw >= gain) { 11000 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 11001 /* Update the lost so we won't exit in next set of tests */ 11002 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11003 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11004 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 11005 } 11006 if ((bbr->rc_loss_exit && 11007 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 11008 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) && 11009 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) { 11010 /* 11011 * If we had no gain, we had loss and that loss was above 11012 * our threshould, the rwnd is not constrained, and we have 11013 * had at least 3 packet epochs exit. Note that this is 11014 * switched off by sysctl. Google does not do this by the 11015 * way. 11016 */ 11017 if ((ctf_flight_size(bbr->rc_tp, 11018 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 11019 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) { 11020 do_exit = 1; 11021 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11022 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4); 11023 } else { 11024 /* Just record an updated loss value */ 11025 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11026 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11027 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5); 11028 } 11029 } else 11030 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11031 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) || 11032 do_exit) { 11033 /* Return 1 to exit the startup state. */ 11034 return (1); 11035 } 11036 /* Stay in startup */ 11037 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11038 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 11039 return (0); 11040 } 11041 11042 static void 11043 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses) 11044 { 11045 /* 11046 * A tick occurred in the rtt epoch do we need to do anything? 11047 */ 11048 #ifdef BBR_INVARIANTS 11049 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) && 11050 (bbr->rc_bbr_state != BBR_STATE_DRAIN) && 11051 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) && 11052 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 11053 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) { 11054 /* Debug code? */ 11055 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state); 11056 } 11057 #endif 11058 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 11059 /* Do we exit the startup state? */ 11060 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) { 11061 uint32_t time_in; 11062 11063 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11064 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6); 11065 bbr->rc_filled_pipe = 1; 11066 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11067 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11068 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11069 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11070 } else 11071 time_in = 0; 11072 if (bbr->rc_no_pacing) 11073 bbr->rc_no_pacing = 0; 11074 bbr->r_ctl.rc_bbr_state_time = cts; 11075 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg; 11076 bbr->rc_bbr_state = BBR_STATE_DRAIN; 11077 bbr_set_state_target(bbr, __LINE__); 11078 if ((bbr->rc_use_google == 0) && 11079 bbr_slam_cwnd_in_main_drain) { 11080 /* Here we don't have to worry about probe-rtt */ 11081 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 11082 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11083 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11084 } 11085 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 11086 bbr_log_type_statechange(bbr, cts, __LINE__); 11087 if (ctf_flight_size(bbr->rc_tp, 11088 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 11089 bbr->r_ctl.rc_target_at_state) { 11090 /* 11091 * Switch to probe_bw if we are already 11092 * there 11093 */ 11094 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11095 bbr_substate_change(bbr, cts, __LINE__, 0); 11096 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11097 bbr_log_type_statechange(bbr, cts, __LINE__); 11098 } 11099 } 11100 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) { 11101 uint32_t inflight; 11102 struct tcpcb *tp; 11103 11104 tp = bbr->rc_tp; 11105 inflight = ctf_flight_size(tp, 11106 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11107 if (inflight >= bbr->r_ctl.rc_target_at_state) { 11108 /* We have reached a flight of the cwnd target */ 11109 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11110 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11111 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 11112 bbr_set_state_target(bbr, __LINE__); 11113 /* 11114 * Rig it so we don't do anything crazy and 11115 * start fresh with a new randomization. 11116 */ 11117 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 11118 bbr->rc_bbr_substate = BBR_SUB_LEVEL6; 11119 bbr_substate_change(bbr, cts, __LINE__, 1); 11120 } 11121 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) { 11122 /* Has in-flight reached the bdp (or less)? */ 11123 uint32_t inflight; 11124 struct tcpcb *tp; 11125 11126 tp = bbr->rc_tp; 11127 inflight = ctf_flight_size(tp, 11128 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11129 if ((bbr->rc_use_google == 0) && 11130 bbr_slam_cwnd_in_main_drain && 11131 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11132 /* 11133 * Here we don't have to worry about probe-rtt 11134 * re-slam it, but keep it slammed down. 11135 */ 11136 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11137 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11138 } 11139 if (inflight <= bbr->r_ctl.rc_target_at_state) { 11140 /* We have drained */ 11141 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11142 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11143 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11144 uint32_t time_in; 11145 11146 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11147 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11148 } 11149 if ((bbr->rc_use_google == 0) && 11150 bbr_slam_cwnd_in_main_drain && 11151 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 11152 /* Restore the cwnd */ 11153 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 11154 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11155 } 11156 /* Setup probe-rtt has being done now RRS-HERE */ 11157 bbr->r_ctl.rc_rtt_shrinks = cts; 11158 bbr->r_ctl.last_in_probertt = cts; 11159 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0); 11160 /* Randomly pick a sub-state */ 11161 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11162 bbr_substate_change(bbr, cts, __LINE__, 0); 11163 bbr_log_type_statechange(bbr, cts, __LINE__); 11164 } 11165 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) { 11166 uint32_t flight; 11167 11168 flight = ctf_flight_size(bbr->rc_tp, 11169 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11170 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered); 11171 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) && 11172 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11173 /* 11174 * We must keep cwnd at the desired MSS. 11175 */ 11176 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 11177 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11178 } else if ((bbr_prtt_slam_cwnd) && 11179 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11180 /* Re-slam it */ 11181 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11182 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11183 } 11184 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) { 11185 /* Has outstanding reached our target? */ 11186 if (flight <= bbr->r_ctl.rc_target_at_state) { 11187 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0); 11188 bbr->r_ctl.rc_bbr_enters_probertt = cts; 11189 /* If time is exactly 0, be 1usec off */ 11190 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) 11191 bbr->r_ctl.rc_bbr_enters_probertt = 1; 11192 if (bbr->rc_use_google == 0) { 11193 /* 11194 * Restore any lowering that as occurred to 11195 * reach here 11196 */ 11197 if (bbr->r_ctl.bbr_rttprobe_gain_val) 11198 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 11199 else 11200 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11201 } 11202 } 11203 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) && 11204 (bbr->rc_use_google == 0) && 11205 bbr->r_ctl.bbr_rttprobe_gain_val && 11206 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) || 11207 (flight >= bbr->r_ctl.flightsize_at_drain))) { 11208 /* 11209 * We have doddled with our current hptsi 11210 * gain an srtt and have still not made it 11211 * to target, or we have increased our flight. 11212 * Lets reduce the gain by xx% 11213 * flooring the reduce at DRAIN (based on 11214 * mul/div) 11215 */ 11216 int red; 11217 11218 bbr->r_ctl.flightsize_at_drain = flight; 11219 bbr->r_ctl.rc_probertt_srttchktim = cts; 11220 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1); 11221 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) { 11222 /* Reduce our gain again */ 11223 bbr->r_ctl.rc_bbr_hptsi_gain -= red; 11224 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0); 11225 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) { 11226 /* one more chance before we give up */ 11227 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 11228 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0); 11229 } else { 11230 /* At the very bottom */ 11231 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1); 11232 } 11233 } 11234 } 11235 if (bbr->r_ctl.rc_bbr_enters_probertt && 11236 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) && 11237 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) { 11238 /* Time to exit probe RTT normally */ 11239 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts); 11240 } 11241 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 11242 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 11243 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 11244 /* 11245 * This qualifies as a RTT_PROBE session since we 11246 * drop the data outstanding to nothing and waited 11247 * more than bbr_rtt_probe_time. 11248 */ 11249 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 11250 bbr_set_reduced_rtt(bbr, cts, __LINE__); 11251 } 11252 if (bbr_should_enter_probe_rtt(bbr, cts)) { 11253 bbr_enter_probe_rtt(bbr, cts, __LINE__); 11254 } else { 11255 bbr_set_probebw_gains(bbr, cts, losses); 11256 } 11257 } 11258 } 11259 11260 static void 11261 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses) 11262 { 11263 int32_t epoch = 0; 11264 11265 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 11266 bbr_set_epoch(bbr, cts, line); 11267 /* At each epoch doe lt bw sampling */ 11268 epoch = 1; 11269 } 11270 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses); 11271 } 11272 11273 static int 11274 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11275 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt, 11276 struct timeval *tv) 11277 { 11278 struct inpcb *inp = tptoinpcb(tp); 11279 struct socket *so = tptosocket(tp); 11280 int32_t thflags, retval; 11281 uint32_t cts, lcts; 11282 uint32_t tiwin; 11283 struct tcpopt to; 11284 struct tcp_bbr *bbr; 11285 struct bbr_sendmap *rsm; 11286 struct timeval ltv; 11287 int32_t did_out = 0; 11288 uint16_t nsegs; 11289 int32_t prev_state; 11290 uint32_t lost; 11291 11292 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11293 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11294 /* add in our stats */ 11295 kern_prefetch(bbr, &prev_state); 11296 prev_state = 0; 11297 thflags = tcp_get_flags(th); 11298 /* 11299 * If this is either a state-changing packet or current state isn't 11300 * established, we require a write lock on tcbinfo. Otherwise, we 11301 * allow the tcbinfo to be in either alocked or unlocked, as the 11302 * caller may have unnecessarily acquired a write lock due to a 11303 * race. 11304 */ 11305 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11306 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 11307 __func__)); 11308 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 11309 __func__)); 11310 11311 tp->t_rcvtime = ticks; 11312 /* 11313 * Unscale the window into a 32-bit value. For the SYN_SENT state 11314 * the scale is zero. 11315 */ 11316 tiwin = th->th_win << tp->snd_scale; 11317 #ifdef STATS 11318 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 11319 #endif 11320 11321 if (m->m_flags & M_TSTMP) { 11322 /* Prefer the hardware timestamp if present */ 11323 struct timespec ts; 11324 11325 mbuf_tstmp2timespec(m, &ts); 11326 bbr->rc_tv.tv_sec = ts.tv_sec; 11327 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11328 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usec(&bbr->rc_tv); 11329 } else if (m->m_flags & M_TSTMP_LRO) { 11330 /* Next the arrival timestamp */ 11331 struct timespec ts; 11332 11333 mbuf_tstmp2timespec(m, &ts); 11334 bbr->rc_tv.tv_sec = ts.tv_sec; 11335 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11336 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usec(&bbr->rc_tv); 11337 } else { 11338 /* 11339 * Ok just get the current time. 11340 */ 11341 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv); 11342 } 11343 /* 11344 * Parse options on any incoming segment. 11345 */ 11346 tcp_dooptions(&to, (u_char *)(th + 1), 11347 (th->th_off << 2) - sizeof(struct tcphdr), 11348 (thflags & TH_SYN) ? TO_SYN : 0); 11349 if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) { 11350 /* 11351 * We don't look at sack's from the 11352 * peer because the MSS is too small which 11353 * can subject us to an attack. 11354 */ 11355 to.to_flags &= ~TOF_SACK; 11356 } 11357 /* 11358 * If timestamps were negotiated during SYN/ACK and a 11359 * segment without a timestamp is received, silently drop 11360 * the segment, unless it is a RST segment or missing timestamps are 11361 * tolerated. 11362 * See section 3.2 of RFC 7323. 11363 */ 11364 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 11365 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 11366 retval = 0; 11367 m_freem(m); 11368 goto done_with_input; 11369 } 11370 /* 11371 * If echoed timestamp is later than the current time, fall back to 11372 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 11373 * were used when this connection was established. 11374 */ 11375 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 11376 to.to_tsecr -= tp->ts_offset; 11377 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_msec(&bbr->rc_tv))) 11378 to.to_tsecr = 0; 11379 } 11380 /* 11381 * If its the first time in we need to take care of options and 11382 * verify we can do SACK for rack! 11383 */ 11384 if (bbr->r_state == 0) { 11385 /* 11386 * Process options only when we get SYN/ACK back. The SYN 11387 * case for incoming connections is handled in tcp_syncache. 11388 * According to RFC1323 the window field in a SYN (i.e., a 11389 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 11390 * this is traditional behavior, may need to be cleaned up. 11391 */ 11392 if (bbr->rc_inp == NULL) { 11393 bbr->rc_inp = inp; 11394 } 11395 /* 11396 * We need to init rc_inp here since its not init'd when 11397 * bbr_init is called 11398 */ 11399 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 11400 if ((to.to_flags & TOF_SCALE) && 11401 (tp->t_flags & TF_REQ_SCALE)) { 11402 tp->t_flags |= TF_RCVD_SCALE; 11403 tp->snd_scale = to.to_wscale; 11404 } else 11405 tp->t_flags &= ~TF_REQ_SCALE; 11406 /* 11407 * Initial send window. It will be updated with the 11408 * next incoming segment to the scaled value. 11409 */ 11410 tp->snd_wnd = th->th_win; 11411 if ((to.to_flags & TOF_TS) && 11412 (tp->t_flags & TF_REQ_TSTMP)) { 11413 tp->t_flags |= TF_RCVD_TSTMP; 11414 tp->ts_recent = to.to_tsval; 11415 tp->ts_recent_age = tcp_tv_to_msec(&bbr->rc_tv); 11416 } else 11417 tp->t_flags &= ~TF_REQ_TSTMP; 11418 if (to.to_flags & TOF_MSS) 11419 tcp_mss(tp, to.to_mss); 11420 if ((tp->t_flags & TF_SACK_PERMIT) && 11421 (to.to_flags & TOF_SACKPERM) == 0) 11422 tp->t_flags &= ~TF_SACK_PERMIT; 11423 if (tp->t_flags & TF_FASTOPEN) { 11424 if (to.to_flags & TOF_FASTOPEN) { 11425 uint16_t mss; 11426 11427 if (to.to_flags & TOF_MSS) 11428 mss = to.to_mss; 11429 else 11430 if ((inp->inp_vflag & INP_IPV6) != 0) 11431 mss = TCP6_MSS; 11432 else 11433 mss = TCP_MSS; 11434 tcp_fastopen_update_cache(tp, mss, 11435 to.to_tfo_len, to.to_tfo_cookie); 11436 } else 11437 tcp_fastopen_disable_path(tp); 11438 } 11439 } 11440 /* 11441 * At this point we are at the initial call. Here we decide 11442 * if we are doing RACK or not. We do this by seeing if 11443 * TF_SACK_PERMIT is set, if not rack is *not* possible and 11444 * we switch to the default code. 11445 */ 11446 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 11447 /* Bail */ 11448 tcp_switch_back_to_default(tp); 11449 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen, 11450 tlen, iptos); 11451 return (1); 11452 } 11453 /* Set the flag */ 11454 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 11455 tcp_set_hpts(tp); 11456 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack); 11457 } 11458 if (thflags & TH_ACK) { 11459 /* Track ack types */ 11460 if (to.to_flags & TOF_SACK) 11461 BBR_STAT_INC(bbr_acks_with_sacks); 11462 else 11463 BBR_STAT_INC(bbr_plain_acks); 11464 } 11465 /* 11466 * This is the one exception case where we set the rack state 11467 * always. All other times (timers etc) we must have a rack-state 11468 * set (so we assure we have done the checks above for SACK). 11469 */ 11470 if (thflags & TH_FIN) 11471 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 11472 if (bbr->r_state != tp->t_state) 11473 bbr_set_state(tp, bbr, tiwin); 11474 11475 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL) 11476 kern_prefetch(rsm, &prev_state); 11477 prev_state = bbr->r_state; 11478 bbr->rc_ack_was_delayed = 0; 11479 lost = bbr->r_ctl.rc_lost; 11480 bbr->rc_is_pkt_epoch_now = 0; 11481 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) { 11482 /* Get the real time into lcts and figure the real delay */ 11483 lcts = tcp_get_usecs(<v); 11484 if (TSTMP_GT(lcts, cts)) { 11485 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts; 11486 bbr->rc_ack_was_delayed = 1; 11487 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay, 11488 bbr->r_ctl.highest_hdwr_delay)) 11489 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay; 11490 } else { 11491 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11492 bbr->rc_ack_was_delayed = 0; 11493 } 11494 } else { 11495 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11496 bbr->rc_ack_was_delayed = 0; 11497 } 11498 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m); 11499 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 11500 retval = 0; 11501 m_freem(m); 11502 goto done_with_input; 11503 } 11504 /* 11505 * If a segment with the ACK-bit set arrives in the SYN-SENT state 11506 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 11507 */ 11508 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 11509 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 11510 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11511 ctf_do_dropwithreset_conn(m, tp, th, tlen); 11512 return (1); 11513 } 11514 if (tiwin > bbr->r_ctl.rc_high_rwnd) 11515 bbr->r_ctl.rc_high_rwnd = tiwin; 11516 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp, 11517 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11518 bbr->rtt_valid = 0; 11519 if (to.to_flags & TOF_TS) { 11520 bbr->rc_ts_valid = 1; 11521 bbr->r_ctl.last_inbound_ts = to.to_tsval; 11522 } else { 11523 bbr->rc_ts_valid = 0; 11524 bbr->r_ctl.last_inbound_ts = 0; 11525 } 11526 retval = (*bbr->r_substate) (m, th, so, 11527 tp, &to, drop_hdrlen, 11528 tlen, tiwin, thflags, nxt_pkt, iptos); 11529 if (nxt_pkt == 0) 11530 BBR_STAT_INC(bbr_rlock_left_ret0); 11531 else 11532 BBR_STAT_INC(bbr_rlock_left_ret1); 11533 if (retval == 0) { 11534 /* 11535 * If retval is 1 the tcb is unlocked and most likely the tp 11536 * is gone. 11537 */ 11538 INP_WLOCK_ASSERT(inp); 11539 tcp_bbr_xmit_timer_commit(bbr, tp, cts); 11540 if (bbr->rc_is_pkt_epoch_now) 11541 bbr_set_pktepoch(bbr, cts, __LINE__); 11542 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost)); 11543 if (nxt_pkt == 0) { 11544 if ((bbr->r_wanted_output != 0) || 11545 (tp->t_flags & TF_ACKNOW)) { 11546 11547 bbr->rc_output_starts_timer = 0; 11548 did_out = 1; 11549 if (tcp_output(tp) < 0) 11550 return (1); 11551 } else 11552 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0); 11553 } 11554 if ((nxt_pkt == 0) && 11555 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 11556 (SEQ_GT(tp->snd_max, tp->snd_una) || 11557 (tp->t_flags & TF_DELACK) || 11558 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 11559 (tp->t_state <= TCPS_CLOSING)))) { 11560 /* 11561 * We could not send (probably in the hpts but 11562 * stopped the timer)? 11563 */ 11564 if ((tp->snd_max == tp->snd_una) && 11565 ((tp->t_flags & TF_DELACK) == 0) && 11566 (tcp_in_hpts(tp)) && 11567 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11568 /* 11569 * keep alive not needed if we are hptsi 11570 * output yet 11571 */ 11572 ; 11573 } else { 11574 if (tcp_in_hpts(tp)) { 11575 tcp_hpts_remove(tp); 11576 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11577 (TSTMP_GT(lcts, bbr->rc_pacer_started))) { 11578 uint32_t del; 11579 11580 del = lcts - bbr->rc_pacer_started; 11581 if (bbr->r_ctl.rc_last_delay_val > del) { 11582 BBR_STAT_INC(bbr_force_timer_start); 11583 bbr->r_ctl.rc_last_delay_val -= del; 11584 bbr->rc_pacer_started = lcts; 11585 } else { 11586 /* We are late */ 11587 bbr->r_ctl.rc_last_delay_val = 0; 11588 BBR_STAT_INC(bbr_force_output); 11589 if (tcp_output(tp) < 0) 11590 return (1); 11591 } 11592 } 11593 } 11594 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val, 11595 0); 11596 } 11597 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) { 11598 /* Do we have the correct timer running? */ 11599 bbr_timer_audit(tp, bbr, lcts, &so->so_snd); 11600 } 11601 /* Clear the flag, it may have been cleared by output but we may not have */ 11602 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS)) 11603 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11604 /* Do we have a new state */ 11605 if (bbr->r_state != tp->t_state) 11606 bbr_set_state(tp, bbr, tiwin); 11607 done_with_input: 11608 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out); 11609 if (did_out) 11610 bbr->r_wanted_output = 0; 11611 } 11612 return (retval); 11613 } 11614 11615 static void 11616 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11617 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 11618 { 11619 struct timeval tv; 11620 int retval; 11621 11622 /* First lets see if we have old packets */ 11623 if (!STAILQ_EMPTY(&tp->t_inqueue)) { 11624 if (ctf_do_queued_segments(tp, 1)) { 11625 m_freem(m); 11626 return; 11627 } 11628 } 11629 if (m->m_flags & M_TSTMP_LRO) { 11630 mbuf_tstmp2timeval(m, &tv); 11631 } else { 11632 /* Should not be should we kassert instead? */ 11633 tcp_get_usecs(&tv); 11634 } 11635 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 11636 0, &tv); 11637 if (retval == 0) { 11638 INP_WUNLOCK(tptoinpcb(tp)); 11639 } 11640 } 11641 11642 /* 11643 * Return how much data can be sent without violating the 11644 * cwnd or rwnd. 11645 */ 11646 11647 static inline uint32_t 11648 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin, 11649 uint32_t avail, int32_t sb_offset, uint32_t cts) 11650 { 11651 uint32_t len; 11652 11653 if (ctf_outstanding(tp) >= tp->snd_wnd) { 11654 /* We never want to go over our peers rcv-window */ 11655 len = 0; 11656 } else { 11657 uint32_t flight; 11658 11659 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11660 if (flight >= sendwin) { 11661 /* 11662 * We have in flight what we are allowed by cwnd (if 11663 * it was rwnd blocking it would have hit above out 11664 * >= tp->snd_wnd). 11665 */ 11666 return (0); 11667 } 11668 len = sendwin - flight; 11669 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 11670 /* We would send too much (beyond the rwnd) */ 11671 len = tp->snd_wnd - ctf_outstanding(tp); 11672 } 11673 if ((len + sb_offset) > avail) { 11674 /* 11675 * We don't have that much in the SB, how much is 11676 * there? 11677 */ 11678 len = avail - sb_offset; 11679 } 11680 } 11681 return (len); 11682 } 11683 11684 static inline void 11685 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11686 { 11687 if (error) { 11688 return; 11689 } 11690 if (rsm) { 11691 if (rsm->r_flags & BBR_TLP) { 11692 /* 11693 * TLP should not count in retran count, but in its 11694 * own bin 11695 */ 11696 KMOD_TCPSTAT_INC(tcps_tlpresends); 11697 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len); 11698 } else { 11699 /* Retransmit */ 11700 tp->t_sndrexmitpack++; 11701 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 11702 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 11703 #ifdef STATS 11704 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 11705 len); 11706 #endif 11707 } 11708 /* 11709 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is 11710 * sub-state 11711 */ 11712 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len); 11713 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) { 11714 /* Non probe_bw log in 1, 2, or 4. */ 11715 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len); 11716 } else { 11717 /* 11718 * Log our probe state 3, and log also 5-13 to show 11719 * us the recovery sub-state for the send. This 11720 * means that 3 == (5+6+7+8+9+10+11+12+13) 11721 */ 11722 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len); 11723 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len); 11724 } 11725 /* Place in both 16's the totals of retransmitted */ 11726 counter_u64_add(bbr_state_lost[16], len); 11727 counter_u64_add(bbr_state_resend[16], len); 11728 /* Place in 17's the total sent */ 11729 counter_u64_add(bbr_state_resend[17], len); 11730 counter_u64_add(bbr_state_lost[17], len); 11731 11732 } else { 11733 /* New sends */ 11734 KMOD_TCPSTAT_INC(tcps_sndpack); 11735 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 11736 /* Place in 17's the total sent */ 11737 counter_u64_add(bbr_state_resend[17], len); 11738 counter_u64_add(bbr_state_lost[17], len); 11739 #ifdef STATS 11740 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 11741 len); 11742 #endif 11743 } 11744 } 11745 11746 static void 11747 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level) 11748 { 11749 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) { 11750 /* 11751 * Limit the cwnd to not be above N x the target plus whats 11752 * is outstanding. The target is based on the current b/w 11753 * estimate. 11754 */ 11755 uint32_t target; 11756 11757 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT); 11758 target += ctf_outstanding(tp); 11759 target *= bbr_target_cwnd_mult_limit; 11760 if (tp->snd_cwnd > target) 11761 tp->snd_cwnd = target; 11762 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__); 11763 } 11764 } 11765 11766 static int 11767 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg) 11768 { 11769 /* 11770 * "adv" is the amount we could increase the window, taking into 11771 * account that we are limited by TCP_MAXWIN << tp->rcv_scale. 11772 */ 11773 int32_t adv; 11774 int32_t oldwin; 11775 11776 adv = recwin; 11777 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 11778 oldwin = (tp->rcv_adv - tp->rcv_nxt); 11779 if (adv > oldwin) 11780 adv -= oldwin; 11781 else { 11782 /* We can't increase the window */ 11783 adv = 0; 11784 } 11785 } else 11786 oldwin = 0; 11787 11788 /* 11789 * If the new window size ends up being the same as or less 11790 * than the old size when it is scaled, then don't force 11791 * a window update. 11792 */ 11793 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 11794 return (0); 11795 11796 if (adv >= (2 * maxseg) && 11797 (adv >= (so->so_rcv.sb_hiwat / 4) || 11798 recwin <= (so->so_rcv.sb_hiwat / 8) || 11799 so->so_rcv.sb_hiwat <= 8 * maxseg)) { 11800 return (1); 11801 } 11802 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 11803 return (1); 11804 return (0); 11805 } 11806 11807 /* 11808 * Return 0 on success and a errno on failure to send. 11809 * Note that a 0 return may not mean we sent anything 11810 * if the TCB was on the hpts. A non-zero return 11811 * does indicate the error we got from ip[6]_output. 11812 */ 11813 static int 11814 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv) 11815 { 11816 struct socket *so; 11817 int32_t len; 11818 uint32_t cts; 11819 uint32_t recwin, sendwin; 11820 int32_t sb_offset; 11821 int32_t flags, abandon, error = 0; 11822 struct tcp_log_buffer *lgb; 11823 struct mbuf *m; 11824 struct mbuf *mb; 11825 uint32_t if_hw_tsomaxsegcount = 0; 11826 uint32_t if_hw_tsomaxsegsize = 0; 11827 uint32_t if_hw_tsomax = 0; 11828 struct ip *ip = NULL; 11829 struct tcp_bbr *bbr; 11830 struct tcphdr *th; 11831 struct udphdr *udp = NULL; 11832 u_char opt[TCP_MAXOLEN]; 11833 unsigned ipoptlen, optlen, hdrlen; 11834 unsigned ulen; 11835 uint32_t bbr_seq; 11836 uint32_t delay_calc=0; 11837 uint8_t doing_tlp = 0; 11838 uint8_t local_options; 11839 #ifdef BBR_INVARIANTS 11840 uint8_t doing_retran_from = 0; 11841 uint8_t picked_up_retran = 0; 11842 #endif 11843 uint8_t wanted_cookie = 0; 11844 uint8_t more_to_rxt=0; 11845 int32_t prefetch_so_done = 0; 11846 int32_t prefetch_rsm = 0; 11847 uint32_t tot_len = 0; 11848 uint32_t maxseg, pace_max_segs, p_maxseg; 11849 int32_t csum_flags = 0; 11850 int32_t hw_tls; 11851 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 11852 unsigned ipsec_optlen = 0; 11853 11854 #endif 11855 volatile int32_t sack_rxmit; 11856 struct bbr_sendmap *rsm = NULL; 11857 int32_t tso, mtu; 11858 struct tcpopt to; 11859 int32_t slot = 0; 11860 struct inpcb *inp; 11861 struct sockbuf *sb; 11862 bool hpts_calling; 11863 #ifdef INET6 11864 struct ip6_hdr *ip6 = NULL; 11865 int32_t isipv6; 11866 #endif 11867 uint8_t app_limited = BBR_JR_SENT_DATA; 11868 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11869 /* We take a cache hit here */ 11870 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval)); 11871 cts = tcp_tv_to_usec(&bbr->rc_tv); 11872 inp = bbr->rc_inp; 11873 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS); 11874 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11875 so = inp->inp_socket; 11876 sb = &so->so_snd; 11877 if (tp->t_nic_ktls_xmit) 11878 hw_tls = 1; 11879 else 11880 hw_tls = 0; 11881 kern_prefetch(sb, &maxseg); 11882 maxseg = tp->t_maxseg - bbr->rc_last_options; 11883 if (bbr_minseg(bbr) < maxseg) { 11884 tcp_bbr_tso_size_check(bbr, cts); 11885 } 11886 /* Remove any flags that indicate we are pacing on the inp */ 11887 pace_max_segs = bbr->r_ctl.rc_pace_max_segs; 11888 p_maxseg = min(maxseg, pace_max_segs); 11889 INP_WLOCK_ASSERT(inp); 11890 #ifdef TCP_OFFLOAD 11891 if (tp->t_flags & TF_TOE) 11892 return (tcp_offload_output(tp)); 11893 #endif 11894 11895 #ifdef INET6 11896 if (bbr->r_state) { 11897 /* Use the cache line loaded if possible */ 11898 isipv6 = bbr->r_is_v6; 11899 } else { 11900 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 11901 } 11902 #endif 11903 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 11904 tcp_in_hpts(tp)) { 11905 /* 11906 * We are on the hpts for some timer but not hptsi output. 11907 * Possibly remove from the hpts so we can send/recv etc. 11908 */ 11909 if ((tp->t_flags & TF_ACKNOW) == 0) { 11910 /* 11911 * No immediate demand right now to send an ack, but 11912 * the user may have read, making room for new data 11913 * (a window update). If so we may want to cancel 11914 * whatever timer is running (KEEP/DEL-ACK?) and 11915 * continue to send out a window update. Or we may 11916 * have gotten more data into the socket buffer to 11917 * send. 11918 */ 11919 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 11920 (long)TCP_MAXWIN << tp->rcv_scale); 11921 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) && 11922 ((tcp_outflags[tp->t_state] & TH_RST) == 0) && 11923 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <= 11924 (tp->snd_max - tp->snd_una))) { 11925 /* 11926 * Nothing new to send and no window update 11927 * is needed to send. Lets just return and 11928 * let the timer-run off. 11929 */ 11930 return (0); 11931 } 11932 } 11933 tcp_hpts_remove(tp); 11934 bbr_timer_cancel(bbr, __LINE__, cts); 11935 } 11936 if (bbr->r_ctl.rc_last_delay_val) { 11937 /* Calculate a rough delay for early escape to sending */ 11938 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11939 delay_calc = cts - bbr->rc_pacer_started; 11940 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11941 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11942 else 11943 delay_calc = 0; 11944 } 11945 /* Mark that we have called bbr_output(). */ 11946 if ((bbr->r_timer_override) || 11947 (tp->t_state < TCPS_ESTABLISHED)) { 11948 /* Timeouts or early states are exempt */ 11949 if (tcp_in_hpts(tp)) 11950 tcp_hpts_remove(tp); 11951 } else if (tcp_in_hpts(tp)) { 11952 if ((bbr->r_ctl.rc_last_delay_val) && 11953 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11954 delay_calc) { 11955 /* 11956 * We were being paced for output and the delay has 11957 * already exceeded when we were supposed to be 11958 * called, lets go ahead and pull out of the hpts 11959 * and call output. 11960 */ 11961 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1); 11962 bbr->r_ctl.rc_last_delay_val = 0; 11963 tcp_hpts_remove(tp); 11964 } else if (tp->t_state == TCPS_CLOSED) { 11965 bbr->r_ctl.rc_last_delay_val = 0; 11966 tcp_hpts_remove(tp); 11967 } else { 11968 /* 11969 * On the hpts, you shall not pass! even if ACKNOW 11970 * is on, we will when the hpts fires, unless of 11971 * course we are overdue. 11972 */ 11973 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1); 11974 return (0); 11975 } 11976 } 11977 bbr->rc_cwnd_limited = 0; 11978 if (bbr->r_ctl.rc_last_delay_val) { 11979 /* recalculate the real delay and deal with over/under */ 11980 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11981 delay_calc = cts - bbr->rc_pacer_started; 11982 else 11983 delay_calc = 0; 11984 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11985 /* Setup the delay which will be added in */ 11986 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11987 else { 11988 /* 11989 * We are early setup to adjust 11990 * our slot time. 11991 */ 11992 uint64_t merged_val; 11993 11994 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc); 11995 bbr->r_agg_early_set = 1; 11996 if (bbr->r_ctl.rc_hptsi_agg_delay) { 11997 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) { 11998 /* Nope our previous late cancels out the early */ 11999 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early; 12000 bbr->r_agg_early_set = 0; 12001 bbr->r_ctl.rc_agg_early = 0; 12002 } else { 12003 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay; 12004 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12005 } 12006 } 12007 merged_val = bbr->rc_pacer_started; 12008 merged_val <<= 32; 12009 merged_val |= bbr->r_ctl.rc_last_delay_val; 12010 bbr_log_pacing_delay_calc(bbr, hpts_calling, 12011 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val, 12012 bbr->r_agg_early_set, 3); 12013 bbr->r_ctl.rc_last_delay_val = 0; 12014 BBR_STAT_INC(bbr_early); 12015 delay_calc = 0; 12016 } 12017 } else { 12018 /* We were not delayed due to hptsi */ 12019 if (bbr->r_agg_early_set) 12020 bbr->r_ctl.rc_agg_early = 0; 12021 bbr->r_agg_early_set = 0; 12022 delay_calc = 0; 12023 } 12024 if (delay_calc) { 12025 /* 12026 * We had a hptsi delay which means we are falling behind on 12027 * sending at the expected rate. Calculate an extra amount 12028 * of data we can send, if any, to put us back on track. 12029 */ 12030 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay) 12031 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff; 12032 else 12033 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc; 12034 } 12035 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12036 if ((tp->snd_una == tp->snd_max) && 12037 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 12038 (sbavail(sb))) { 12039 /* 12040 * Ok we have been idle with nothing outstanding 12041 * we possibly need to start fresh with either a new 12042 * suite of states or a fast-ramp up. 12043 */ 12044 bbr_restart_after_idle(bbr, 12045 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time)); 12046 } 12047 /* 12048 * Now was there a hptsi delay where we are behind? We only count 12049 * being behind if: a) We are not in recovery. b) There was a delay. 12050 * <and> c) We had room to send something. 12051 * 12052 */ 12053 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 12054 int retval; 12055 12056 retval = bbr_process_timers(tp, bbr, cts, hpts_calling); 12057 if (retval != 0) { 12058 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1); 12059 /* 12060 * If timers want tcp_drop(), then pass error out, 12061 * otherwise suppress it. 12062 */ 12063 return (retval < 0 ? retval : 0); 12064 } 12065 } 12066 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 12067 if (hpts_calling && 12068 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 12069 bbr->r_ctl.rc_last_delay_val = 0; 12070 } 12071 bbr->r_timer_override = 0; 12072 bbr->r_wanted_output = 0; 12073 /* 12074 * For TFO connections in SYN_RECEIVED, only allow the initial 12075 * SYN|ACK and those sent by the retransmit timer. 12076 */ 12077 if ((tp->t_flags & TF_FASTOPEN) && 12078 ((tp->t_state == TCPS_SYN_RECEIVED) || 12079 (tp->t_state == TCPS_SYN_SENT)) && 12080 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 12081 (tp->t_rxtshift == 0)) { /* not a retransmit */ 12082 len = 0; 12083 goto just_return_nolock; 12084 } 12085 /* 12086 * Before sending anything check for a state update. For hpts 12087 * calling without input this is important. If its input calling 12088 * then this was already done. 12089 */ 12090 if (bbr->rc_use_google == 0) 12091 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12092 again: 12093 /* 12094 * If we've recently taken a timeout, snd_max will be greater than 12095 * snd_max. BBR in general does not pay much attention to snd_nxt 12096 * for historic reasons the persist timer still uses it. This means 12097 * we have to look at it. All retransmissions that are not persits 12098 * use the rsm that needs to be sent so snd_nxt is ignored. At the 12099 * end of this routine we pull snd_nxt always up to snd_max. 12100 */ 12101 doing_tlp = 0; 12102 #ifdef BBR_INVARIANTS 12103 doing_retran_from = picked_up_retran = 0; 12104 #endif 12105 error = 0; 12106 tso = 0; 12107 slot = 0; 12108 mtu = 0; 12109 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12110 sb_offset = tp->snd_max - tp->snd_una; 12111 flags = tcp_outflags[tp->t_state]; 12112 sack_rxmit = 0; 12113 len = 0; 12114 rsm = NULL; 12115 if (flags & TH_RST) { 12116 SOCK_SENDBUF_LOCK(so); 12117 goto send; 12118 } 12119 recheck_resend: 12120 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 12121 /* We need to always have one in reserve */ 12122 rsm = bbr_alloc(bbr); 12123 if (rsm == NULL) { 12124 error = ENOMEM; 12125 /* Lie to get on the hpts */ 12126 tot_len = tp->t_maxseg; 12127 if (hpts_calling) 12128 /* Retry in a ms */ 12129 slot = 1001; 12130 goto just_return_nolock; 12131 } 12132 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 12133 bbr->r_ctl.rc_free_cnt++; 12134 rsm = NULL; 12135 } 12136 /* What do we send, a resend? */ 12137 if (bbr->r_ctl.rc_resend == NULL) { 12138 /* Check for rack timeout */ 12139 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 12140 if (bbr->r_ctl.rc_resend) { 12141 #ifdef BBR_INVARIANTS 12142 picked_up_retran = 1; 12143 #endif 12144 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend); 12145 } 12146 } 12147 if (bbr->r_ctl.rc_resend) { 12148 rsm = bbr->r_ctl.rc_resend; 12149 #ifdef BBR_INVARIANTS 12150 doing_retran_from = 1; 12151 #endif 12152 /* Remove any TLP flags its a RACK or T-O */ 12153 rsm->r_flags &= ~BBR_TLP; 12154 bbr->r_ctl.rc_resend = NULL; 12155 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 12156 #ifdef BBR_INVARIANTS 12157 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n", 12158 tp, bbr, rsm, rsm->r_start, tp->snd_una); 12159 goto recheck_resend; 12160 #else 12161 /* TSNH */ 12162 rsm = NULL; 12163 goto recheck_resend; 12164 #endif 12165 } 12166 if (rsm->r_flags & BBR_HAS_SYN) { 12167 /* Only retransmit a SYN by itself */ 12168 len = 0; 12169 if ((flags & TH_SYN) == 0) { 12170 /* Huh something is wrong */ 12171 rsm->r_start++; 12172 if (rsm->r_start == rsm->r_end) { 12173 /* Clean it up, somehow we missed the ack? */ 12174 bbr_log_syn(tp, NULL); 12175 } else { 12176 /* TFO with data? */ 12177 rsm->r_flags &= ~BBR_HAS_SYN; 12178 len = rsm->r_end - rsm->r_start; 12179 } 12180 } else { 12181 /* Retransmitting SYN */ 12182 rsm = NULL; 12183 SOCK_SENDBUF_LOCK(so); 12184 goto send; 12185 } 12186 } else 12187 len = rsm->r_end - rsm->r_start; 12188 if ((bbr->rc_resends_use_tso == 0) && 12189 (len > maxseg)) { 12190 len = maxseg; 12191 more_to_rxt = 1; 12192 } 12193 sb_offset = rsm->r_start - tp->snd_una; 12194 if (len > 0) { 12195 sack_rxmit = 1; 12196 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 12197 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 12198 min(len, maxseg)); 12199 } else { 12200 /* I dont think this can happen */ 12201 rsm = NULL; 12202 goto recheck_resend; 12203 } 12204 BBR_STAT_INC(bbr_resends_set); 12205 } else if (bbr->r_ctl.rc_tlp_send) { 12206 /* 12207 * Tail loss probe 12208 */ 12209 doing_tlp = 1; 12210 rsm = bbr->r_ctl.rc_tlp_send; 12211 bbr->r_ctl.rc_tlp_send = NULL; 12212 sack_rxmit = 1; 12213 len = rsm->r_end - rsm->r_start; 12214 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12215 len = maxseg; 12216 12217 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 12218 #ifdef BBR_INVARIANTS 12219 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u", 12220 tp, bbr, tp->snd_una, rsm, rsm->r_start); 12221 #else 12222 /* TSNH */ 12223 rsm = NULL; 12224 goto recheck_resend; 12225 #endif 12226 } 12227 sb_offset = rsm->r_start - tp->snd_una; 12228 BBR_STAT_INC(bbr_tlp_set); 12229 } 12230 /* 12231 * Enforce a connection sendmap count limit if set 12232 * as long as we are not retransmiting. 12233 */ 12234 if ((rsm == NULL) && 12235 (V_tcp_map_entries_limit > 0) && 12236 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 12237 BBR_STAT_INC(bbr_alloc_limited); 12238 if (!bbr->alloc_limit_reported) { 12239 bbr->alloc_limit_reported = 1; 12240 BBR_STAT_INC(bbr_alloc_limited_conns); 12241 } 12242 goto just_return_nolock; 12243 } 12244 #ifdef BBR_INVARIANTS 12245 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) { 12246 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u", 12247 tp, bbr, rsm, sb_offset, len); 12248 } 12249 #endif 12250 /* 12251 * Get standard flags, and add SYN or FIN if requested by 'hidden' 12252 * state flags. 12253 */ 12254 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL)) 12255 flags |= TH_FIN; 12256 if (tp->t_flags & TF_NEEDSYN) 12257 flags |= TH_SYN; 12258 12259 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) { 12260 /* we are retransmitting the fin */ 12261 len--; 12262 if (len) { 12263 /* 12264 * When retransmitting data do *not* include the 12265 * FIN. This could happen from a TLP probe if we 12266 * allowed data with a FIN. 12267 */ 12268 flags &= ~TH_FIN; 12269 } 12270 } else if (rsm) { 12271 if (flags & TH_FIN) 12272 flags &= ~TH_FIN; 12273 } 12274 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 12275 void *end_rsm; 12276 12277 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 12278 if (end_rsm) 12279 kern_prefetch(end_rsm, &prefetch_rsm); 12280 prefetch_rsm = 1; 12281 } 12282 SOCK_SENDBUF_LOCK(so); 12283 /* 12284 * If snd_nxt == snd_max and we have transmitted a FIN, the 12285 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 12286 * negative length. This can also occur when TCP opens up its 12287 * congestion window while receiving additional duplicate acks after 12288 * fast-retransmit because TCP will reset snd_nxt to snd_max after 12289 * the fast-retransmit. 12290 * 12291 * In the normal retransmit-FIN-only case, however, snd_nxt will be 12292 * set to snd_una, the sb_offset will be 0, and the length may wind 12293 * up 0. 12294 * 12295 * If sack_rxmit is true we are retransmitting from the scoreboard 12296 * in which case len is already set. 12297 */ 12298 if (sack_rxmit == 0) { 12299 uint32_t avail; 12300 12301 avail = sbavail(sb); 12302 if (SEQ_GT(tp->snd_max, tp->snd_una)) 12303 sb_offset = tp->snd_max - tp->snd_una; 12304 else 12305 sb_offset = 0; 12306 if (bbr->rc_tlp_new_data) { 12307 /* TLP is forcing out new data */ 12308 uint32_t tlplen; 12309 12310 doing_tlp = 1; 12311 tlplen = maxseg; 12312 12313 if (tlplen > (uint32_t)(avail - sb_offset)) { 12314 tlplen = (uint32_t)(avail - sb_offset); 12315 } 12316 if (tlplen > tp->snd_wnd) { 12317 len = tp->snd_wnd; 12318 } else { 12319 len = tlplen; 12320 } 12321 bbr->rc_tlp_new_data = 0; 12322 } else { 12323 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts); 12324 if ((len < p_maxseg) && 12325 (bbr->rc_in_persist == 0) && 12326 (ctf_outstanding(tp) >= (2 * p_maxseg)) && 12327 ((avail - sb_offset) >= p_maxseg)) { 12328 /* 12329 * We are not completing whats in the socket 12330 * buffer (i.e. there is at least a segment 12331 * waiting to send) and we have 2 or more 12332 * segments outstanding. There is no sense 12333 * of sending a little piece. Lets defer and 12334 * and wait until we can send a whole 12335 * segment. 12336 */ 12337 len = 0; 12338 } 12339 if (bbr->rc_in_persist) { 12340 /* 12341 * We are in persists, figure out if 12342 * a retransmit is available (maybe the previous 12343 * persists we sent) or if we have to send new 12344 * data. 12345 */ 12346 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 12347 if (rsm) { 12348 len = rsm->r_end - rsm->r_start; 12349 if (rsm->r_flags & BBR_HAS_FIN) 12350 len--; 12351 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12352 len = maxseg; 12353 if (len > 1) 12354 BBR_STAT_INC(bbr_persist_reneg); 12355 /* 12356 * XXXrrs we could force the len to 12357 * 1 byte here to cause the chunk to 12358 * split apart.. but that would then 12359 * mean we always retransmit it as 12360 * one byte even after the window 12361 * opens. 12362 */ 12363 sack_rxmit = 1; 12364 sb_offset = rsm->r_start - tp->snd_una; 12365 } else { 12366 /* 12367 * First time through in persists or peer 12368 * acked our one byte. Though we do have 12369 * to have something in the sb. 12370 */ 12371 len = 1; 12372 sb_offset = 0; 12373 if (avail == 0) 12374 len = 0; 12375 } 12376 } 12377 } 12378 } 12379 if (prefetch_so_done == 0) { 12380 kern_prefetch(so, &prefetch_so_done); 12381 prefetch_so_done = 1; 12382 } 12383 /* 12384 * Lop off SYN bit if it has already been sent. However, if this is 12385 * SYN-SENT state and if segment contains data and if we don't know 12386 * that foreign host supports TAO, suppress sending segment. 12387 */ 12388 if ((flags & TH_SYN) && (rsm == NULL) && 12389 SEQ_GT(tp->snd_max, tp->snd_una)) { 12390 if (tp->t_state != TCPS_SYN_RECEIVED) 12391 flags &= ~TH_SYN; 12392 /* 12393 * When sending additional segments following a TFO SYN|ACK, 12394 * do not include the SYN bit. 12395 */ 12396 if ((tp->t_flags & TF_FASTOPEN) && 12397 (tp->t_state == TCPS_SYN_RECEIVED)) 12398 flags &= ~TH_SYN; 12399 sb_offset--, len++; 12400 if (sbavail(sb) == 0) 12401 len = 0; 12402 } else if ((flags & TH_SYN) && rsm) { 12403 /* 12404 * Subtract one from the len for the SYN being 12405 * retransmitted. 12406 */ 12407 len--; 12408 } 12409 /* 12410 * Be careful not to send data and/or FIN on SYN segments. This 12411 * measure is needed to prevent interoperability problems with not 12412 * fully conformant TCP implementations. 12413 */ 12414 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 12415 len = 0; 12416 flags &= ~TH_FIN; 12417 } 12418 /* 12419 * On TFO sockets, ensure no data is sent in the following cases: 12420 * 12421 * - When retransmitting SYN|ACK on a passively-created socket 12422 * - When retransmitting SYN on an actively created socket 12423 * - When sending a zero-length cookie (cookie request) on an 12424 * actively created socket 12425 * - When the socket is in the CLOSED state (RST is being sent) 12426 */ 12427 if ((tp->t_flags & TF_FASTOPEN) && 12428 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 12429 ((tp->t_state == TCPS_SYN_SENT) && 12430 (tp->t_tfo_client_cookie_len == 0)) || 12431 (flags & TH_RST))) { 12432 len = 0; 12433 sack_rxmit = 0; 12434 rsm = NULL; 12435 } 12436 /* Without fast-open there should never be data sent on a SYN */ 12437 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) 12438 len = 0; 12439 if (len <= 0) { 12440 /* 12441 * If FIN has been sent but not acked, but we haven't been 12442 * called to retransmit, len will be < 0. Otherwise, window 12443 * shrank after we sent into it. If window shrank to 0, 12444 * cancel pending retransmit, pull snd_nxt back to (closed) 12445 * window, and set the persist timer if it isn't already 12446 * going. If the window didn't close completely, just wait 12447 * for an ACK. 12448 * 12449 * We also do a general check here to ensure that we will 12450 * set the persist timer when we have data to send, but a 12451 * 0-byte window. This makes sure the persist timer is set 12452 * even if the packet hits one of the "goto send" lines 12453 * below. 12454 */ 12455 len = 0; 12456 if ((tp->snd_wnd == 0) && 12457 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12458 (tp->snd_una == tp->snd_max) && 12459 (sb_offset < (int)sbavail(sb))) { 12460 /* 12461 * Not enough room in the rwnd to send 12462 * a paced segment out. 12463 */ 12464 bbr_enter_persist(tp, bbr, cts, __LINE__); 12465 } 12466 } else if ((rsm == NULL) && 12467 (doing_tlp == 0) && 12468 (len < bbr->r_ctl.rc_pace_max_segs)) { 12469 /* 12470 * We are not sending a full segment for 12471 * some reason. Should we not send anything (think 12472 * sws or persists)? 12473 */ 12474 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12475 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12476 (len < (int)(sbavail(sb) - sb_offset))) { 12477 /* 12478 * Here the rwnd is less than 12479 * the pacing size, this is not a retransmit, 12480 * we are established and 12481 * the send is not the last in the socket buffer 12482 * lets not send, and possibly enter persists. 12483 */ 12484 len = 0; 12485 if (tp->snd_max == tp->snd_una) 12486 bbr_enter_persist(tp, bbr, cts, __LINE__); 12487 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) && 12488 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12489 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12490 (len < (int)(sbavail(sb) - sb_offset)) && 12491 (len < bbr_minseg(bbr))) { 12492 /* 12493 * Here we are not retransmitting, and 12494 * the cwnd is not so small that we could 12495 * not send at least a min size (rxt timer 12496 * not having gone off), We have 2 segments or 12497 * more already in flight, its not the tail end 12498 * of the socket buffer and the cwnd is blocking 12499 * us from sending out minimum pacing segment size. 12500 * Lets not send anything. 12501 */ 12502 bbr->rc_cwnd_limited = 1; 12503 len = 0; 12504 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 12505 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12506 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12507 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12508 (len < (int)(sbavail(sb) - sb_offset)) && 12509 (TCPS_HAVEESTABLISHED(tp->t_state))) { 12510 /* 12511 * Here we have a send window but we have 12512 * filled it up and we can't send another pacing segment. 12513 * We also have in flight more than 2 segments 12514 * and we are not completing the sb i.e. we allow 12515 * the last bytes of the sb to go out even if 12516 * its not a full pacing segment. 12517 */ 12518 len = 0; 12519 } 12520 } 12521 /* len will be >= 0 after this point. */ 12522 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 12523 tcp_sndbuf_autoscale(tp, so, sendwin); 12524 /* 12525 * 12526 */ 12527 if (bbr->rc_in_persist && 12528 len && 12529 (rsm == NULL) && 12530 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) { 12531 /* 12532 * We are in persist, not doing a retransmit and don't have enough space 12533 * yet to send a full TSO. So is it at the end of the sb 12534 * if so we need to send else nuke to 0 and don't send. 12535 */ 12536 int sbleft; 12537 if (sbavail(sb) > sb_offset) 12538 sbleft = sbavail(sb) - sb_offset; 12539 else 12540 sbleft = 0; 12541 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) { 12542 /* not at end of sb lets not send */ 12543 len = 0; 12544 } 12545 } 12546 /* 12547 * Decide if we can use TCP Segmentation Offloading (if supported by 12548 * hardware). 12549 * 12550 * TSO may only be used if we are in a pure bulk sending state. The 12551 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 12552 * options prevent using TSO. With TSO the TCP header is the same 12553 * (except for the sequence number) for all generated packets. This 12554 * makes it impossible to transmit any options which vary per 12555 * generated segment or packet. 12556 * 12557 * IPv4 handling has a clear separation of ip options and ip header 12558 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() 12559 * does the right thing below to provide length of just ip options 12560 * and thus checking for ipoptlen is enough to decide if ip options 12561 * are present. 12562 */ 12563 #ifdef INET6 12564 if (isipv6) 12565 ipoptlen = ip6_optlen(inp); 12566 else 12567 #endif 12568 if (inp->inp_options) 12569 ipoptlen = inp->inp_options->m_len - 12570 offsetof(struct ipoption, ipopt_list); 12571 else 12572 ipoptlen = 0; 12573 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12574 /* 12575 * Pre-calculate here as we save another lookup into the darknesses 12576 * of IPsec that way and can actually decide if TSO is ok. 12577 */ 12578 #ifdef INET6 12579 if (isipv6 && IPSEC_ENABLED(ipv6)) 12580 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 12581 #ifdef INET 12582 else 12583 #endif 12584 #endif /* INET6 */ 12585 #ifdef INET 12586 if (IPSEC_ENABLED(ipv4)) 12587 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 12588 #endif /* INET */ 12589 #endif /* IPSEC */ 12590 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12591 ipoptlen += ipsec_optlen; 12592 #endif 12593 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && 12594 (len > maxseg) && 12595 (tp->t_port == 0) && 12596 ((tp->t_flags & TF_SIGNATURE) == 0) && 12597 ipoptlen == 0) 12598 tso = 1; 12599 12600 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 12601 (long)TCP_MAXWIN << tp->rcv_scale); 12602 /* 12603 * Sender silly window avoidance. We transmit under the following 12604 * conditions when len is non-zero: 12605 * 12606 * - We have a full segment (or more with TSO) - This is the last 12607 * buffer in a write()/send() and we are either idle or running 12608 * NODELAY - we've timed out (e.g. persist timer) - we have more 12609 * then 1/2 the maximum send window's worth of data (receiver may be 12610 * limited the window size) - we need to retransmit 12611 */ 12612 if (rsm) 12613 goto send; 12614 if (len) { 12615 if (sack_rxmit) 12616 goto send; 12617 if (len >= p_maxseg) 12618 goto send; 12619 /* 12620 * NOTE! on localhost connections an 'ack' from the remote 12621 * end may occur synchronously with the output and cause us 12622 * to flush a buffer queued with moretocome. XXX 12623 * 12624 */ 12625 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */ 12626 ((tp->t_flags & TF_NODELAY) || 12627 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) && 12628 (tp->t_flags & TF_NOPUSH) == 0) { 12629 goto send; 12630 } 12631 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 12632 goto send; 12633 } 12634 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 12635 goto send; 12636 } 12637 } 12638 /* 12639 * Sending of standalone window updates. 12640 * 12641 * Window updates are important when we close our window due to a 12642 * full socket buffer and are opening it again after the application 12643 * reads data from it. Once the window has opened again and the 12644 * remote end starts to send again the ACK clock takes over and 12645 * provides the most current window information. 12646 * 12647 * We must avoid the silly window syndrome whereas every read from 12648 * the receive buffer, no matter how small, causes a window update 12649 * to be sent. We also should avoid sending a flurry of window 12650 * updates when the socket buffer had queued a lot of data and the 12651 * application is doing small reads. 12652 * 12653 * Prevent a flurry of pointless window updates by only sending an 12654 * update when we can increase the advertized window by more than 12655 * 1/4th of the socket buffer capacity. When the buffer is getting 12656 * full or is very small be more aggressive and send an update 12657 * whenever we can increase by two mss sized segments. In all other 12658 * situations the ACK's to new incoming data will carry further 12659 * window increases. 12660 * 12661 * Don't send an independent window update if a delayed ACK is 12662 * pending (it will get piggy-backed on it) or the remote side 12663 * already has done a half-close and won't send more data. Skip 12664 * this if the connection is in T/TCP half-open state. 12665 */ 12666 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 12667 !(tp->t_flags & TF_DELACK) && 12668 !TCPS_HAVERCVDFIN(tp->t_state)) { 12669 /* Check to see if we should do a window update */ 12670 if (bbr_window_update_needed(tp, so, recwin, maxseg)) 12671 goto send; 12672 } 12673 /* 12674 * Send if we owe the peer an ACK, RST, SYN. ACKNOW 12675 * is also a catch-all for the retransmit timer timeout case. 12676 */ 12677 if (tp->t_flags & TF_ACKNOW) { 12678 goto send; 12679 } 12680 if (flags & TH_RST) { 12681 /* Always send a RST if one is due */ 12682 goto send; 12683 } 12684 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) { 12685 goto send; 12686 } 12687 /* 12688 * If our state indicates that FIN should be sent and we have not 12689 * yet done so, then we need to send. 12690 */ 12691 if (flags & TH_FIN && 12692 ((tp->t_flags & TF_SENTFIN) == 0)) { 12693 goto send; 12694 } 12695 /* 12696 * No reason to send a segment, just return. 12697 */ 12698 just_return: 12699 SOCK_SENDBUF_UNLOCK(so); 12700 just_return_nolock: 12701 if (tot_len) 12702 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 12703 if (bbr->rc_no_pacing) 12704 slot = 0; 12705 if (tot_len == 0) { 12706 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >= 12707 tp->snd_wnd) { 12708 BBR_STAT_INC(bbr_rwnd_limited); 12709 app_limited = BBR_JR_RWND_LIMITED; 12710 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12711 if ((bbr->rc_in_persist == 0) && 12712 TCPS_HAVEESTABLISHED(tp->t_state) && 12713 (tp->snd_max == tp->snd_una) && 12714 sbavail(&so->so_snd)) { 12715 /* No send window.. we must enter persist */ 12716 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 12717 } 12718 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 12719 BBR_STAT_INC(bbr_app_limited); 12720 app_limited = BBR_JR_APP_LIMITED; 12721 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12722 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12723 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) { 12724 BBR_STAT_INC(bbr_cwnd_limited); 12725 app_limited = BBR_JR_CWND_LIMITED; 12726 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12727 bbr->r_ctl.rc_lost_bytes))); 12728 bbr->rc_cwnd_limited = 1; 12729 } else { 12730 BBR_STAT_INC(bbr_app_limited); 12731 app_limited = BBR_JR_APP_LIMITED; 12732 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12733 } 12734 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12735 bbr->r_agg_early_set = 0; 12736 bbr->r_ctl.rc_agg_early = 0; 12737 bbr->r_ctl.rc_last_delay_val = 0; 12738 } else if (bbr->rc_use_google == 0) 12739 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12740 /* Are we app limited? */ 12741 if ((app_limited == BBR_JR_APP_LIMITED) || 12742 (app_limited == BBR_JR_RWND_LIMITED)) { 12743 /** 12744 * We are application limited. 12745 */ 12746 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12747 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered); 12748 } 12749 if (tot_len == 0) 12750 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1); 12751 /* Dont update the time if we did not send */ 12752 bbr->r_ctl.rc_last_delay_val = 0; 12753 bbr->rc_output_starts_timer = 1; 12754 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len); 12755 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len); 12756 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 12757 /* Make sure snd_nxt is drug up */ 12758 tp->snd_nxt = tp->snd_max; 12759 } 12760 return (error); 12761 12762 send: 12763 if (doing_tlp == 0) { 12764 /* 12765 * Data not a TLP, and its not the rxt firing. If it is the 12766 * rxt firing, we want to leave the tlp_in_progress flag on 12767 * so we don't send another TLP. It has to be a rack timer 12768 * or normal send (response to acked data) to clear the tlp 12769 * in progress flag. 12770 */ 12771 bbr->rc_tlp_in_progress = 0; 12772 bbr->rc_tlp_rtx_out = 0; 12773 } else { 12774 /* 12775 * Its a TLP. 12776 */ 12777 bbr->rc_tlp_in_progress = 1; 12778 } 12779 bbr_timer_cancel(bbr, __LINE__, cts); 12780 if (rsm == NULL) { 12781 if (sbused(sb) > 0) { 12782 /* 12783 * This is sub-optimal. We only send a stand alone 12784 * FIN on its own segment. 12785 */ 12786 if (flags & TH_FIN) { 12787 flags &= ~TH_FIN; 12788 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) { 12789 /* Lets not send this */ 12790 slot = 0; 12791 goto just_return; 12792 } 12793 } 12794 } 12795 } else { 12796 /* 12797 * We do *not* send a FIN on a retransmit if it has data. 12798 * The if clause here where len > 1 should never come true. 12799 */ 12800 if ((len > 0) && 12801 (((rsm->r_flags & BBR_HAS_FIN) == 0) && 12802 (flags & TH_FIN))) { 12803 flags &= ~TH_FIN; 12804 len--; 12805 } 12806 } 12807 SOCK_SENDBUF_LOCK_ASSERT(so); 12808 if (len > 0) { 12809 if ((tp->snd_una == tp->snd_max) && 12810 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 12811 /* 12812 * This qualifies as a RTT_PROBE session since we 12813 * drop the data outstanding to nothing and waited 12814 * more than bbr_rtt_probe_time. 12815 */ 12816 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 12817 bbr_set_reduced_rtt(bbr, cts, __LINE__); 12818 } 12819 if (len >= maxseg) 12820 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 12821 else 12822 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 12823 } 12824 /* 12825 * Before ESTABLISHED, force sending of initial options unless TCP 12826 * set not to do any options. NOTE: we assume that the IP/TCP header 12827 * plus TCP options always fit in a single mbuf, leaving room for a 12828 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 12829 * + optlen <= MCLBYTES 12830 */ 12831 optlen = 0; 12832 #ifdef INET6 12833 if (isipv6) 12834 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12835 else 12836 #endif 12837 hdrlen = sizeof(struct tcpiphdr); 12838 12839 /* 12840 * Compute options for segment. We only have to care about SYN and 12841 * established connection segments. Options for SYN-ACK segments 12842 * are handled in TCP syncache. 12843 */ 12844 to.to_flags = 0; 12845 local_options = 0; 12846 if ((tp->t_flags & TF_NOOPT) == 0) { 12847 /* Maximum segment size. */ 12848 if (flags & TH_SYN) { 12849 to.to_mss = tcp_mssopt(&inp->inp_inc); 12850 if (tp->t_port) 12851 to.to_mss -= V_tcp_udp_tunneling_overhead; 12852 to.to_flags |= TOF_MSS; 12853 /* 12854 * On SYN or SYN|ACK transmits on TFO connections, 12855 * only include the TFO option if it is not a 12856 * retransmit, as the presence of the TFO option may 12857 * have caused the original SYN or SYN|ACK to have 12858 * been dropped by a middlebox. 12859 */ 12860 if ((tp->t_flags & TF_FASTOPEN) && 12861 (tp->t_rxtshift == 0)) { 12862 if (tp->t_state == TCPS_SYN_RECEIVED) { 12863 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 12864 to.to_tfo_cookie = 12865 (u_int8_t *)&tp->t_tfo_cookie.server; 12866 to.to_flags |= TOF_FASTOPEN; 12867 wanted_cookie = 1; 12868 } else if (tp->t_state == TCPS_SYN_SENT) { 12869 to.to_tfo_len = 12870 tp->t_tfo_client_cookie_len; 12871 to.to_tfo_cookie = 12872 tp->t_tfo_cookie.client; 12873 to.to_flags |= TOF_FASTOPEN; 12874 wanted_cookie = 1; 12875 } 12876 } 12877 } 12878 /* Window scaling. */ 12879 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 12880 to.to_wscale = tp->request_r_scale; 12881 to.to_flags |= TOF_SCALE; 12882 } 12883 /* Timestamps. */ 12884 if ((tp->t_flags & TF_RCVD_TSTMP) || 12885 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 12886 to.to_tsval = tcp_tv_to_msec(&bbr->rc_tv) + tp->ts_offset; 12887 to.to_tsecr = tp->ts_recent; 12888 to.to_flags |= TOF_TS; 12889 local_options += TCPOLEN_TIMESTAMP + 2; 12890 } 12891 /* Set receive buffer autosizing timestamp. */ 12892 if (tp->rfbuf_ts == 0 && 12893 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 12894 tp->rfbuf_ts = tcp_tv_to_msec(&bbr->rc_tv); 12895 /* Selective ACK's. */ 12896 if (flags & TH_SYN) 12897 to.to_flags |= TOF_SACKPERM; 12898 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 12899 tp->rcv_numsacks > 0) { 12900 to.to_flags |= TOF_SACK; 12901 to.to_nsacks = tp->rcv_numsacks; 12902 to.to_sacks = (u_char *)tp->sackblks; 12903 } 12904 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 12905 /* TCP-MD5 (RFC2385). */ 12906 if (tp->t_flags & TF_SIGNATURE) 12907 to.to_flags |= TOF_SIGNATURE; 12908 #endif /* TCP_SIGNATURE */ 12909 12910 /* Processing the options. */ 12911 hdrlen += (optlen = tcp_addoptions(&to, opt)); 12912 /* 12913 * If we wanted a TFO option to be added, but it was unable 12914 * to fit, ensure no data is sent. 12915 */ 12916 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 12917 !(to.to_flags & TOF_FASTOPEN)) 12918 len = 0; 12919 } 12920 if (tp->t_port) { 12921 if (V_tcp_udp_tunneling_port == 0) { 12922 /* The port was removed?? */ 12923 SOCK_SENDBUF_UNLOCK(so); 12924 return (EHOSTUNREACH); 12925 } 12926 hdrlen += sizeof(struct udphdr); 12927 } 12928 #ifdef INET6 12929 if (isipv6) 12930 ipoptlen = ip6_optlen(inp); 12931 else 12932 #endif 12933 if (inp->inp_options) 12934 ipoptlen = inp->inp_options->m_len - 12935 offsetof(struct ipoption, ipopt_list); 12936 else 12937 ipoptlen = 0; 12938 ipoptlen = 0; 12939 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12940 ipoptlen += ipsec_optlen; 12941 #endif 12942 if (bbr->rc_last_options != local_options) { 12943 /* 12944 * Cache the options length this generally does not change 12945 * on a connection. We use this to calculate TSO. 12946 */ 12947 bbr->rc_last_options = local_options; 12948 } 12949 maxseg = tp->t_maxseg - (ipoptlen + optlen); 12950 p_maxseg = min(maxseg, pace_max_segs); 12951 /* 12952 * Adjust data length if insertion of options will bump the packet 12953 * length beyond the t_maxseg length. Clear the FIN bit because we 12954 * cut off the tail of the segment. 12955 */ 12956 if (len > maxseg) { 12957 if (len != 0 && (flags & TH_FIN)) { 12958 flags &= ~TH_FIN; 12959 } 12960 if (tso) { 12961 uint32_t moff; 12962 int32_t max_len; 12963 12964 /* extract TSO information */ 12965 if_hw_tsomax = tp->t_tsomax; 12966 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 12967 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 12968 KASSERT(ipoptlen == 0, 12969 ("%s: TSO can't do IP options", __func__)); 12970 12971 /* 12972 * Check if we should limit by maximum payload 12973 * length: 12974 */ 12975 if (if_hw_tsomax != 0) { 12976 /* compute maximum TSO length */ 12977 max_len = (if_hw_tsomax - hdrlen - 12978 max_linkhdr); 12979 if (max_len <= 0) { 12980 len = 0; 12981 } else if (len > max_len) { 12982 len = max_len; 12983 } 12984 } 12985 /* 12986 * Prevent the last segment from being fractional 12987 * unless the send sockbuf can be emptied: 12988 */ 12989 if ((sb_offset + len) < sbavail(sb)) { 12990 moff = len % (uint32_t)maxseg; 12991 if (moff != 0) { 12992 len -= moff; 12993 } 12994 } 12995 /* 12996 * In case there are too many small fragments don't 12997 * use TSO: 12998 */ 12999 if (len <= maxseg) { 13000 len = maxseg; 13001 tso = 0; 13002 } 13003 } else { 13004 /* Not doing TSO */ 13005 if (optlen + ipoptlen >= tp->t_maxseg) { 13006 /* 13007 * Since we don't have enough space to put 13008 * the IP header chain and the TCP header in 13009 * one packet as required by RFC 7112, don't 13010 * send it. Also ensure that at least one 13011 * byte of the payload can be put into the 13012 * TCP segment. 13013 */ 13014 SOCK_SENDBUF_UNLOCK(so); 13015 error = EMSGSIZE; 13016 sack_rxmit = 0; 13017 goto out; 13018 } 13019 len = maxseg; 13020 } 13021 } else { 13022 /* Not doing TSO */ 13023 if_hw_tsomaxsegcount = 0; 13024 tso = 0; 13025 } 13026 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 13027 ("%s: len > IP_MAXPACKET", __func__)); 13028 #ifdef DIAGNOSTIC 13029 #ifdef INET6 13030 if (max_linkhdr + hdrlen > MCLBYTES) 13031 #else 13032 if (max_linkhdr + hdrlen > MHLEN) 13033 #endif 13034 panic("tcphdr too big"); 13035 #endif 13036 /* 13037 * This KASSERT is here to catch edge cases at a well defined place. 13038 * Before, those had triggered (random) panic conditions further 13039 * down. 13040 */ 13041 #ifdef BBR_INVARIANTS 13042 if (sack_rxmit) { 13043 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 13044 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u", 13045 rsm, tp, bbr, rsm->r_start, tp->snd_una); 13046 } 13047 } 13048 #endif 13049 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 13050 if ((len == 0) && 13051 (flags & TH_FIN) && 13052 (sbused(sb))) { 13053 /* 13054 * We have outstanding data, don't send a fin by itself!. 13055 */ 13056 slot = 0; 13057 goto just_return; 13058 } 13059 /* 13060 * Grab a header mbuf, attaching a copy of data to be transmitted, 13061 * and initialize the header from the template for sends on this 13062 * connection. 13063 */ 13064 if (len) { 13065 uint32_t moff; 13066 13067 /* 13068 * We place a limit on sending with hptsi. 13069 */ 13070 if ((rsm == NULL) && len > pace_max_segs) 13071 len = pace_max_segs; 13072 if (len <= maxseg) 13073 tso = 0; 13074 #ifdef INET6 13075 if (MHLEN < hdrlen + max_linkhdr) 13076 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13077 else 13078 #endif 13079 m = m_gethdr(M_NOWAIT, MT_DATA); 13080 13081 if (m == NULL) { 13082 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13083 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13084 SOCK_SENDBUF_UNLOCK(so); 13085 error = ENOBUFS; 13086 sack_rxmit = 0; 13087 goto out; 13088 } 13089 m->m_data += max_linkhdr; 13090 m->m_len = hdrlen; 13091 /* 13092 * Start the m_copy functions from the closest mbuf to the 13093 * sb_offset in the socket buffer chain. 13094 */ 13095 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) { 13096 #ifdef BBR_INVARIANTS 13097 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) 13098 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u", 13099 tp, bbr, len, sb_offset, sbavail(sb), rsm, 13100 doing_retran_from, 13101 picked_up_retran, 13102 doing_tlp); 13103 13104 #endif 13105 /* 13106 * In this messed up situation we have two choices, 13107 * a) pretend the send worked, and just start timers 13108 * and what not (not good since that may lead us 13109 * back here a lot). <or> b) Send the lowest segment 13110 * in the map. <or> c) Drop the connection. Lets do 13111 * <b> which if it continues to happen will lead to 13112 * <c> via timeouts. 13113 */ 13114 BBR_STAT_INC(bbr_offset_recovery); 13115 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 13116 sb_offset = 0; 13117 if (rsm == NULL) { 13118 sack_rxmit = 0; 13119 len = sbavail(sb); 13120 } else { 13121 sack_rxmit = 1; 13122 if (rsm->r_start != tp->snd_una) { 13123 /* 13124 * Things are really messed up, <c> 13125 * is the only thing to do. 13126 */ 13127 BBR_STAT_INC(bbr_offset_drop); 13128 SOCK_SENDBUF_UNLOCK(so); 13129 (void)m_free(m); 13130 return (-EFAULT); /* tcp_drop() */ 13131 } 13132 len = rsm->r_end - rsm->r_start; 13133 } 13134 if (len > sbavail(sb)) 13135 len = sbavail(sb); 13136 if (len > maxseg) 13137 len = maxseg; 13138 } 13139 mb = sbsndptr_noadv(sb, sb_offset, &moff); 13140 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 13141 m_copydata(mb, moff, (int)len, 13142 mtod(m, caddr_t)+hdrlen); 13143 if (rsm == NULL) 13144 sbsndptr_adv(sb, mb, len); 13145 m->m_len += len; 13146 } else { 13147 struct sockbuf *msb; 13148 13149 if (rsm) 13150 msb = NULL; 13151 else 13152 msb = sb; 13153 #ifdef BBR_INVARIANTS 13154 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) { 13155 if (rsm) { 13156 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u rsm:%p snd_una:%u rsm_start:%u flg:%x %u:%u:%u sr:%d ", 13157 tp, bbr, len, moff, 13158 sbavail(sb), rsm, 13159 tp->snd_una, rsm->r_flags, rsm->r_start, 13160 doing_retran_from, 13161 picked_up_retran, 13162 doing_tlp, sack_rxmit); 13163 } else { 13164 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u", 13165 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una); 13166 } 13167 } 13168 #endif 13169 m->m_next = tcp_m_copym( 13170 mb, moff, &len, 13171 if_hw_tsomaxsegcount, 13172 if_hw_tsomaxsegsize, msb, 13173 ((rsm == NULL) ? hw_tls : 0)); 13174 if (len <= maxseg) { 13175 /* 13176 * Must have ran out of mbufs for the copy 13177 * shorten it to no longer need tso. Lets 13178 * not put on sendalot since we are low on 13179 * mbufs. 13180 */ 13181 tso = 0; 13182 } 13183 if (m->m_next == NULL) { 13184 SOCK_SENDBUF_UNLOCK(so); 13185 (void)m_free(m); 13186 error = ENOBUFS; 13187 sack_rxmit = 0; 13188 goto out; 13189 } 13190 } 13191 #ifdef BBR_INVARIANTS 13192 if (tso && len < maxseg) { 13193 panic("tp:%p tso on, but len:%d < maxseg:%d", 13194 tp, len, maxseg); 13195 } 13196 if (tso && if_hw_tsomaxsegcount) { 13197 int32_t seg_cnt = 0; 13198 struct mbuf *foo; 13199 13200 foo = m; 13201 while (foo) { 13202 seg_cnt++; 13203 foo = foo->m_next; 13204 } 13205 if (seg_cnt > if_hw_tsomaxsegcount) { 13206 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount); 13207 } 13208 } 13209 #endif 13210 /* 13211 * If we're sending everything we've got, set PUSH. (This 13212 * will keep happy those implementations which only give 13213 * data to the user when a buffer fills or a PUSH comes in.) 13214 */ 13215 if (sb_offset + len == sbused(sb) && 13216 sbused(sb) && 13217 !(flags & TH_SYN)) { 13218 flags |= TH_PUSH; 13219 } 13220 SOCK_SENDBUF_UNLOCK(so); 13221 } else { 13222 SOCK_SENDBUF_UNLOCK(so); 13223 if (tp->t_flags & TF_ACKNOW) 13224 KMOD_TCPSTAT_INC(tcps_sndacks); 13225 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 13226 KMOD_TCPSTAT_INC(tcps_sndctrl); 13227 else 13228 KMOD_TCPSTAT_INC(tcps_sndwinup); 13229 13230 m = m_gethdr(M_NOWAIT, MT_DATA); 13231 if (m == NULL) { 13232 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13233 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13234 error = ENOBUFS; 13235 /* Fudge the send time since we could not send */ 13236 sack_rxmit = 0; 13237 goto out; 13238 } 13239 #ifdef INET6 13240 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 13241 MHLEN >= hdrlen) { 13242 M_ALIGN(m, hdrlen); 13243 } else 13244 #endif 13245 m->m_data += max_linkhdr; 13246 m->m_len = hdrlen; 13247 } 13248 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13249 m->m_pkthdr.rcvif = (struct ifnet *)0; 13250 #ifdef MAC 13251 mac_inpcb_create_mbuf(inp, m); 13252 #endif 13253 #ifdef INET6 13254 if (isipv6) { 13255 ip6 = mtod(m, struct ip6_hdr *); 13256 if (tp->t_port) { 13257 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 13258 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13259 udp->uh_dport = tp->t_port; 13260 ulen = hdrlen + len - sizeof(struct ip6_hdr); 13261 udp->uh_ulen = htons(ulen); 13262 th = (struct tcphdr *)(udp + 1); 13263 } else { 13264 th = (struct tcphdr *)(ip6 + 1); 13265 } 13266 tcpip_fillheaders(inp, tp->t_port, ip6, th); 13267 } else 13268 #endif /* INET6 */ 13269 { 13270 ip = mtod(m, struct ip *); 13271 if (tp->t_port) { 13272 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 13273 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13274 udp->uh_dport = tp->t_port; 13275 ulen = hdrlen + len - sizeof(struct ip); 13276 udp->uh_ulen = htons(ulen); 13277 th = (struct tcphdr *)(udp + 1); 13278 } else { 13279 th = (struct tcphdr *)(ip + 1); 13280 } 13281 tcpip_fillheaders(inp, tp->t_port, ip, th); 13282 } 13283 /* 13284 * If we are doing retransmissions, then snd_nxt will not reflect 13285 * the first unsent octet. For ACK only packets, we do not want the 13286 * sequence number of the retransmitted packet, we want the sequence 13287 * number of the next unsent octet. So, if there is no data (and no 13288 * SYN or FIN), use snd_max instead of snd_nxt when filling in 13289 * ti_seq. But if we are in persist state, snd_max might reflect 13290 * one byte beyond the right edge of the window, so use snd_nxt in 13291 * that case, since we know we aren't doing a retransmission. 13292 * (retransmit and persist are mutually exclusive...) 13293 */ 13294 if (sack_rxmit == 0) { 13295 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) { 13296 /* New data (including new persists) */ 13297 th->th_seq = htonl(tp->snd_max); 13298 bbr_seq = tp->snd_max; 13299 } else if (flags & TH_SYN) { 13300 /* Syn's always send from iss */ 13301 th->th_seq = htonl(tp->iss); 13302 bbr_seq = tp->iss; 13303 } else if (flags & TH_FIN) { 13304 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) { 13305 /* 13306 * If we sent the fin already its 1 minus 13307 * snd_max 13308 */ 13309 th->th_seq = (htonl(tp->snd_max - 1)); 13310 bbr_seq = (tp->snd_max - 1); 13311 } else { 13312 /* First time FIN use snd_max */ 13313 th->th_seq = htonl(tp->snd_max); 13314 bbr_seq = tp->snd_max; 13315 } 13316 } else { 13317 /* 13318 * len == 0 and not persist we use snd_max, sending 13319 * an ack unless we have sent the fin then its 1 13320 * minus. 13321 */ 13322 /* 13323 * XXXRRS Question if we are in persists and we have 13324 * nothing outstanding to send and we have not sent 13325 * a FIN, we will send an ACK. In such a case it 13326 * might be better to send (tp->snd_una - 1) which 13327 * would force the peer to ack. 13328 */ 13329 if (tp->t_flags & TF_SENTFIN) { 13330 th->th_seq = htonl(tp->snd_max - 1); 13331 bbr_seq = (tp->snd_max - 1); 13332 } else { 13333 th->th_seq = htonl(tp->snd_max); 13334 bbr_seq = tp->snd_max; 13335 } 13336 } 13337 } else { 13338 /* All retransmits use the rsm to guide the send */ 13339 th->th_seq = htonl(rsm->r_start); 13340 bbr_seq = rsm->r_start; 13341 } 13342 th->th_ack = htonl(tp->rcv_nxt); 13343 if (optlen) { 13344 bcopy(opt, th + 1, optlen); 13345 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 13346 } 13347 tcp_set_flags(th, flags); 13348 /* 13349 * Calculate receive window. Don't shrink window, but avoid silly 13350 * window syndrome. 13351 */ 13352 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) && 13353 recwin < maxseg))) 13354 recwin = 0; 13355 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 13356 recwin < (tp->rcv_adv - tp->rcv_nxt)) 13357 recwin = (tp->rcv_adv - tp->rcv_nxt); 13358 if (recwin > TCP_MAXWIN << tp->rcv_scale) 13359 recwin = TCP_MAXWIN << tp->rcv_scale; 13360 13361 /* 13362 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 13363 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 13364 * handled in syncache. 13365 */ 13366 if (flags & TH_SYN) 13367 th->th_win = htons((u_short) 13368 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 13369 else { 13370 /* Avoid shrinking window with window scaling. */ 13371 recwin = roundup2(recwin, 1 << tp->rcv_scale); 13372 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13373 } 13374 /* 13375 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 13376 * window. This may cause the remote transmitter to stall. This 13377 * flag tells soreceive() to disable delayed acknowledgements when 13378 * draining the buffer. This can occur if the receiver is 13379 * attempting to read more data than can be buffered prior to 13380 * transmitting on the connection. 13381 */ 13382 if (th->th_win == 0) { 13383 tp->t_sndzerowin++; 13384 tp->t_flags |= TF_RXWIN0SENT; 13385 } else 13386 tp->t_flags &= ~TF_RXWIN0SENT; 13387 /* 13388 * We don't support urgent data, but drag along 13389 * the pointer in case of a stack switch. 13390 */ 13391 tp->snd_up = tp->snd_una; 13392 /* 13393 * Put TCP length in extended header, and then checksum extended 13394 * header and data. 13395 */ 13396 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 13397 13398 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 13399 if (to.to_flags & TOF_SIGNATURE) { 13400 /* 13401 * Calculate MD5 signature and put it into the place 13402 * determined before. NOTE: since TCP options buffer doesn't 13403 * point into mbuf's data, calculate offset and use it. 13404 */ 13405 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 13406 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 13407 /* 13408 * Do not send segment if the calculation of MD5 13409 * digest has failed. 13410 */ 13411 goto out; 13412 } 13413 } 13414 #endif 13415 13416 #ifdef INET6 13417 if (isipv6) { 13418 /* 13419 * ip6_plen is not need to be filled now, and will be filled 13420 * in ip6_output. 13421 */ 13422 if (tp->t_port) { 13423 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13424 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13425 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13426 th->th_sum = htons(0); 13427 UDPSTAT_INC(udps_opackets); 13428 } else { 13429 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13430 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13431 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 13432 optlen + len, IPPROTO_TCP, 0); 13433 } 13434 } 13435 #endif 13436 #if defined(INET6) && defined(INET) 13437 else 13438 #endif 13439 #ifdef INET 13440 { 13441 if (tp->t_port) { 13442 m->m_pkthdr.csum_flags = CSUM_UDP; 13443 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13444 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13445 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13446 th->th_sum = htons(0); 13447 UDPSTAT_INC(udps_opackets); 13448 } else { 13449 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP; 13450 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13451 th->th_sum = in_pseudo(ip->ip_src.s_addr, 13452 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13453 IPPROTO_TCP + len + optlen)); 13454 } 13455 /* IP version must be set here for ipv4/ipv6 checking later */ 13456 KASSERT(ip->ip_v == IPVERSION, 13457 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 13458 } 13459 #endif 13460 13461 /* 13462 * Enable TSO and specify the size of the segments. The TCP pseudo 13463 * header checksum is always provided. XXX: Fixme: This is currently 13464 * not the case for IPv6. 13465 */ 13466 if (tso) { 13467 KASSERT(len > maxseg, 13468 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg)); 13469 m->m_pkthdr.csum_flags |= CSUM_TSO; 13470 csum_flags |= CSUM_TSO; 13471 m->m_pkthdr.tso_segsz = maxseg; 13472 } 13473 KASSERT(len + hdrlen == m_length(m, NULL), 13474 ("%s: mbuf chain different than expected: %d + %u != %u", 13475 __func__, len, hdrlen, m_length(m, NULL))); 13476 13477 #ifdef TCP_HHOOK 13478 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */ 13479 hhook_run_tcp_est_out(tp, th, &to, len, tso); 13480 #endif 13481 13482 /* Log to the black box */ 13483 if (tcp_bblogging_on(tp)) { 13484 union tcp_log_stackspecific log; 13485 13486 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 13487 /* Record info on type of transmission */ 13488 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay; 13489 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3); 13490 log.u_bbr.flex3 = maxseg; 13491 log.u_bbr.flex4 = delay_calc; 13492 log.u_bbr.flex5 = bbr->rc_past_init_win; 13493 log.u_bbr.flex5 <<= 1; 13494 log.u_bbr.flex5 |= bbr->rc_no_pacing; 13495 log.u_bbr.flex5 <<= 29; 13496 log.u_bbr.flex5 |= tp->t_maxseg; 13497 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs; 13498 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr); 13499 /* lets poke in the low and the high here for debugging */ 13500 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 13501 if (rsm || sack_rxmit) { 13502 if (doing_tlp) 13503 log.u_bbr.flex8 = 2; 13504 else 13505 log.u_bbr.flex8 = 1; 13506 } else { 13507 log.u_bbr.flex8 = 0; 13508 } 13509 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 13510 len, &log, false, NULL, NULL, 0, tv); 13511 } else { 13512 lgb = NULL; 13513 } 13514 /* 13515 * Fill in IP length and desired time to live and send to IP level. 13516 * There should be a better way to handle ttl and tos; we could keep 13517 * them in the template, but need a way to checksum without them. 13518 */ 13519 /* 13520 * m->m_pkthdr.len should have been set before cksum calcuration, 13521 * because in6_cksum() need it. 13522 */ 13523 #ifdef INET6 13524 if (isipv6) { 13525 /* 13526 * we separately set hoplimit for every segment, since the 13527 * user might want to change the value via setsockopt. Also, 13528 * desired default hop limit might be changed via Neighbor 13529 * Discovery. 13530 */ 13531 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 13532 13533 /* 13534 * Set the packet size here for the benefit of DTrace 13535 * probes. ip6_output() will set it properly; it's supposed 13536 * to include the option header lengths as well. 13537 */ 13538 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 13539 13540 if (V_path_mtu_discovery && maxseg > V_tcp_minmss) 13541 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13542 else 13543 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13544 13545 if (tp->t_state == TCPS_SYN_SENT) 13546 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 13547 13548 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 13549 /* TODO: IPv6 IP6TOS_ECT bit on */ 13550 error = ip6_output(m, inp->in6p_outputopts, 13551 &inp->inp_route6, 13552 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 13553 NULL, NULL, inp); 13554 13555 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 13556 mtu = inp->inp_route6.ro_nh->nh_mtu; 13557 } 13558 #endif /* INET6 */ 13559 #if defined(INET) && defined(INET6) 13560 else 13561 #endif 13562 #ifdef INET 13563 { 13564 ip->ip_len = htons(m->m_pkthdr.len); 13565 #ifdef INET6 13566 if (isipv6) 13567 ip->ip_ttl = in6_selecthlim(inp, NULL); 13568 #endif /* INET6 */ 13569 /* 13570 * If we do path MTU discovery, then we set DF on every 13571 * packet. This might not be the best thing to do according 13572 * to RFC3390 Section 2. However the tcp hostcache migitates 13573 * the problem so it affects only the first tcp connection 13574 * with a host. 13575 * 13576 * NB: Don't set DF on small MTU/MSS to have a safe 13577 * fallback. 13578 */ 13579 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 13580 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13581 if (tp->t_port == 0 || len < V_tcp_minmss) { 13582 ip->ip_off |= htons(IP_DF); 13583 } 13584 } else { 13585 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13586 } 13587 13588 if (tp->t_state == TCPS_SYN_SENT) 13589 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 13590 13591 TCP_PROBE5(send, NULL, tp, ip, tp, th); 13592 13593 error = ip_output(m, inp->inp_options, &inp->inp_route, 13594 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 13595 inp); 13596 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 13597 mtu = inp->inp_route.ro_nh->nh_mtu; 13598 } 13599 #endif /* INET */ 13600 if (lgb) { 13601 lgb->tlb_errno = error; 13602 lgb = NULL; 13603 } 13604 13605 out: 13606 /* 13607 * In transmit state, time the transmission and arrange for the 13608 * retransmit. In persist state, just set snd_max. 13609 */ 13610 if (error == 0) { 13611 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 13612 if (TCPS_HAVEESTABLISHED(tp->t_state) && 13613 (tp->t_flags & TF_SACK_PERMIT) && 13614 tp->rcv_numsacks > 0) 13615 tcp_clean_dsack_blocks(tp); 13616 /* We sent an ack clear the bbr_segs_rcvd count */ 13617 bbr->output_error_seen = 0; 13618 bbr->oerror_cnt = 0; 13619 bbr->bbr_segs_rcvd = 0; 13620 if (len == 0) 13621 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1); 13622 /* Do accounting for new sends */ 13623 if ((len > 0) && (rsm == NULL)) { 13624 int idx; 13625 if (tp->snd_una == tp->snd_max) { 13626 /* 13627 * Special case to match google, when 13628 * nothing is in flight the delivered 13629 * time does get updated to the current 13630 * time (see tcp_rate_bsd.c). 13631 */ 13632 bbr->r_ctl.rc_del_time = cts; 13633 } 13634 if (len >= maxseg) { 13635 idx = (len / maxseg) + 3; 13636 if (idx >= TCP_MSS_ACCT_ATIMER) 13637 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1); 13638 else 13639 counter_u64_add(bbr_out_size[idx], 1); 13640 } else { 13641 /* smaller than a MSS */ 13642 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options); 13643 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV) 13644 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1); 13645 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1); 13646 } 13647 } 13648 } 13649 abandon = 0; 13650 /* 13651 * We must do the send accounting before we log the output, 13652 * otherwise the state of the rsm could change and we account to the 13653 * wrong bucket. 13654 */ 13655 if (len > 0) { 13656 bbr_do_send_accounting(tp, bbr, rsm, len, error); 13657 if (error == 0) { 13658 if (tp->snd_una == tp->snd_max) 13659 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 13660 } 13661 } 13662 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error, 13663 cts, mb, &abandon, rsm, 0, sb); 13664 if (abandon) { 13665 /* 13666 * If bbr_log_output destroys the TCB or sees a TH_RST being 13667 * sent we should hit this condition. 13668 */ 13669 return (0); 13670 } 13671 if (bbr->rc_in_persist == 0) { 13672 /* 13673 * Advance snd_nxt over sequence space of this segment. 13674 */ 13675 if (error) 13676 /* We don't log or do anything with errors */ 13677 goto skip_upd; 13678 13679 if (tp->snd_una == tp->snd_max && 13680 (len || (flags & (TH_SYN | TH_FIN)))) { 13681 /* 13682 * Update the time we just added data since none was 13683 * outstanding. 13684 */ 13685 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13686 bbr->rc_tp->t_acktime = ticks; 13687 } 13688 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) { 13689 if (flags & TH_SYN) { 13690 /* 13691 * Smack the snd_max to iss + 1 13692 * if its a FO we will add len below. 13693 */ 13694 tp->snd_max = tp->iss + 1; 13695 } 13696 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13697 tp->snd_max++; 13698 tp->t_flags |= TF_SENTFIN; 13699 } 13700 } 13701 if (sack_rxmit == 0) 13702 tp->snd_max += len; 13703 skip_upd: 13704 if ((error == 0) && len) 13705 tot_len += len; 13706 } else { 13707 /* Persists case */ 13708 int32_t xlen = len; 13709 13710 if (error) 13711 goto nomore; 13712 13713 if (flags & TH_SYN) 13714 ++xlen; 13715 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13716 ++xlen; 13717 tp->t_flags |= TF_SENTFIN; 13718 } 13719 if (xlen && (tp->snd_una == tp->snd_max)) { 13720 /* 13721 * Update the time we just added data since none was 13722 * outstanding. 13723 */ 13724 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13725 bbr->rc_tp->t_acktime = ticks; 13726 } 13727 if (sack_rxmit == 0) 13728 tp->snd_max += xlen; 13729 tot_len += (len + optlen + ipoptlen); 13730 } 13731 nomore: 13732 if (error) { 13733 /* 13734 * Failures do not advance the seq counter above. For the 13735 * case of ENOBUFS we will fall out and become ack-clocked. 13736 * capping the cwnd at the current flight. 13737 * Everything else will just have to retransmit with the timer 13738 * (no pacer). 13739 */ 13740 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13741 BBR_STAT_INC(bbr_saw_oerr); 13742 /* Clear all delay/early tracks */ 13743 bbr->r_ctl.rc_hptsi_agg_delay = 0; 13744 bbr->r_ctl.rc_agg_early = 0; 13745 bbr->r_agg_early_set = 0; 13746 bbr->output_error_seen = 1; 13747 if (bbr->oerror_cnt < 0xf) 13748 bbr->oerror_cnt++; 13749 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) { 13750 /* drop the session */ 13751 return (-ENETDOWN); 13752 } 13753 switch (error) { 13754 case ENOBUFS: 13755 /* 13756 * Make this guy have to get ack's to send 13757 * more but lets make sure we don't 13758 * slam him below a T-O (1MSS). 13759 */ 13760 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 13761 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13762 bbr->r_ctl.rc_lost_bytes)) - maxseg; 13763 if (tp->snd_cwnd < maxseg) 13764 tp->snd_cwnd = maxseg; 13765 } 13766 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt; 13767 BBR_STAT_INC(bbr_saw_enobuf); 13768 if (bbr->bbr_hdrw_pacing) 13769 counter_u64_add(bbr_hdwr_pacing_enobuf, 1); 13770 else 13771 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1); 13772 /* 13773 * Here even in the enobuf's case we want to do our 13774 * state update. The reason being we may have been 13775 * called by the input function. If so we have had 13776 * things change. 13777 */ 13778 error = 0; 13779 goto enobufs; 13780 case EMSGSIZE: 13781 /* 13782 * For some reason the interface we used initially 13783 * to send segments changed to another or lowered 13784 * its MTU. If TSO was active we either got an 13785 * interface without TSO capabilits or TSO was 13786 * turned off. If we obtained mtu from ip_output() 13787 * then update it and try again. 13788 */ 13789 /* Turn on tracing (or try to) */ 13790 { 13791 int old_maxseg; 13792 13793 old_maxseg = tp->t_maxseg; 13794 BBR_STAT_INC(bbr_saw_emsgsiz); 13795 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts); 13796 if (mtu != 0) 13797 tcp_mss_update(tp, -1, mtu, NULL, NULL); 13798 if (old_maxseg <= tp->t_maxseg) { 13799 /* Huh it did not shrink? */ 13800 tp->t_maxseg = old_maxseg - 40; 13801 if (tp->t_maxseg < V_tcp_mssdflt) { 13802 /* 13803 * The MSS is so small we should not 13804 * process incoming SACK's since we are 13805 * subject to attack in such a case. 13806 */ 13807 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 13808 } else { 13809 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 13810 } 13811 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts); 13812 } 13813 /* 13814 * Nuke all other things that can interfere 13815 * with slot 13816 */ 13817 if ((tot_len + len) && (len >= tp->t_maxseg)) { 13818 slot = bbr_get_pacing_delay(bbr, 13819 bbr->r_ctl.rc_bbr_hptsi_gain, 13820 (tot_len + len), cts, 0); 13821 if (slot < bbr_error_base_paceout) 13822 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13823 } else 13824 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13825 bbr->rc_output_starts_timer = 1; 13826 bbr_start_hpts_timer(bbr, tp, cts, 10, slot, 13827 tot_len); 13828 return (error); 13829 } 13830 case EPERM: 13831 case EACCES: 13832 tp->t_softerror = error; 13833 /* FALLTHROUGH */ 13834 case EHOSTDOWN: 13835 case EHOSTUNREACH: 13836 case ENETDOWN: 13837 case ENETUNREACH: 13838 if (TCPS_HAVERCVDSYN(tp->t_state)) { 13839 tp->t_softerror = error; 13840 error = 0; 13841 } 13842 /* FALLTHROUGH */ 13843 default: 13844 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt; 13845 bbr->rc_output_starts_timer = 1; 13846 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0); 13847 return (error); 13848 } 13849 #ifdef STATS 13850 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) && 13851 len && 13852 (rsm == NULL) && 13853 (bbr->rc_in_persist == 0)) { 13854 tp->gput_seq = bbr_seq; 13855 tp->gput_ack = bbr_seq + 13856 min(sbavail(&so->so_snd) - sb_offset, sendwin); 13857 tp->gput_ts = cts; 13858 tp->t_flags |= TF_GPUTINPROG; 13859 #endif 13860 } 13861 KMOD_TCPSTAT_INC(tcps_sndtotal); 13862 if ((bbr->bbr_hdw_pace_ena) && 13863 (bbr->bbr_attempt_hdwr_pace == 0) && 13864 (bbr->rc_past_init_win) && 13865 (bbr->rc_bbr_state != BBR_STATE_STARTUP) && 13866 (get_filter_value(&bbr->r_ctl.rc_delrate)) && 13867 (inp->inp_route.ro_nh && 13868 inp->inp_route.ro_nh->nh_ifp)) { 13869 /* 13870 * We are past the initial window and 13871 * have at least one measurement so we 13872 * could use hardware pacing if its available. 13873 * We have an interface and we have not attempted 13874 * to setup hardware pacing, lets try to now. 13875 */ 13876 uint64_t rate_wanted; 13877 int err = 0; 13878 13879 rate_wanted = bbr_get_hardware_rate(bbr); 13880 bbr->bbr_attempt_hdwr_pace = 1; 13881 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp, 13882 inp->inp_route.ro_nh->nh_ifp, 13883 rate_wanted, 13884 (RS_PACING_GEQ|RS_PACING_SUB_OK), 13885 &err, NULL); 13886 if (bbr->r_ctl.crte) { 13887 bbr_type_log_hdwr_pacing(bbr, 13888 bbr->r_ctl.crte->ptbl->rs_ifp, 13889 rate_wanted, 13890 bbr->r_ctl.crte->rate, 13891 __LINE__, cts, err); 13892 BBR_STAT_INC(bbr_hdwr_rl_add_ok); 13893 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 13894 counter_u64_add(bbr_flows_whdwr_pacing, 1); 13895 bbr->bbr_hdrw_pacing = 1; 13896 /* Now what is our gain status? */ 13897 if (bbr->r_ctl.crte->rate < rate_wanted) { 13898 /* We have a problem */ 13899 bbr_setup_less_of_rate(bbr, cts, 13900 bbr->r_ctl.crte->rate, rate_wanted); 13901 } else { 13902 /* We are good */ 13903 bbr->gain_is_limited = 0; 13904 bbr->skip_gain = 0; 13905 } 13906 tcp_bbr_tso_size_check(bbr, cts); 13907 } else { 13908 bbr_type_log_hdwr_pacing(bbr, 13909 inp->inp_route.ro_nh->nh_ifp, 13910 rate_wanted, 13911 0, 13912 __LINE__, cts, err); 13913 BBR_STAT_INC(bbr_hdwr_rl_add_fail); 13914 } 13915 } 13916 if (bbr->bbr_hdrw_pacing) { 13917 /* 13918 * Worry about cases where the route 13919 * changes or something happened that we 13920 * lost our hardware pacing possibly during 13921 * the last ip_output call. 13922 */ 13923 if (inp->inp_snd_tag == NULL) { 13924 /* A change during ip output disabled hw pacing? */ 13925 bbr->bbr_hdrw_pacing = 0; 13926 } else if ((inp->inp_route.ro_nh == NULL) || 13927 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) { 13928 /* 13929 * We had an interface or route change, 13930 * detach from the current hdwr pacing 13931 * and setup to re-attempt next go 13932 * round. 13933 */ 13934 bbr->bbr_hdrw_pacing = 0; 13935 bbr->bbr_attempt_hdwr_pace = 0; 13936 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 13937 tcp_bbr_tso_size_check(bbr, cts); 13938 } 13939 } 13940 /* 13941 * Data sent (as far as we can tell). If this advertises a larger 13942 * window than any other segment, then remember the size of the 13943 * advertised window. Any pending ACK has now been sent. 13944 */ 13945 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 13946 tp->rcv_adv = tp->rcv_nxt + recwin; 13947 13948 tp->last_ack_sent = tp->rcv_nxt; 13949 if ((error == 0) && 13950 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) && 13951 (doing_tlp == 0) && 13952 (tso == 0) && 13953 (len > 0) && 13954 ((flags & TH_RST) == 0) && 13955 ((flags & TH_SYN) == 0) && 13956 (IN_RECOVERY(tp->t_flags) == 0) && 13957 (bbr->rc_in_persist == 0) && 13958 (tot_len < bbr->r_ctl.rc_pace_max_segs)) { 13959 /* 13960 * For non-tso we need to goto again until we have sent out 13961 * enough data to match what we are hptsi out every hptsi 13962 * interval. 13963 */ 13964 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13965 /* Make sure snd_nxt is drug up */ 13966 tp->snd_nxt = tp->snd_max; 13967 } 13968 if (rsm != NULL) { 13969 rsm = NULL; 13970 goto skip_again; 13971 } 13972 rsm = NULL; 13973 sack_rxmit = 0; 13974 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13975 goto again; 13976 } 13977 skip_again: 13978 if ((error == 0) && (flags & TH_FIN)) 13979 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 13980 if ((error == 0) && (flags & TH_RST)) 13981 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 13982 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) { 13983 /* 13984 * Calculate/Re-Calculate the hptsi slot in usecs based on 13985 * what we have sent so far 13986 */ 13987 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 13988 if (bbr->rc_no_pacing) 13989 slot = 0; 13990 } 13991 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13992 enobufs: 13993 if (bbr->rc_use_google == 0) 13994 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 13995 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13996 bbr->r_ctl.rc_lost_bytes))); 13997 bbr->rc_output_starts_timer = 1; 13998 if (bbr->bbr_use_rack_cheat && 13999 (more_to_rxt || 14000 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) { 14001 /* Rack cheats and shotguns out all rxt's 1ms apart */ 14002 if (slot > 1000) 14003 slot = 1000; 14004 } 14005 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) { 14006 /* 14007 * We don't change the tso size until some number of sends 14008 * to give the hardware commands time to get down 14009 * to the interface. 14010 */ 14011 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++; 14012 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) { 14013 bbr->hw_pacing_set = 1; 14014 tcp_bbr_tso_size_check(bbr, cts); 14015 } 14016 } 14017 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len); 14018 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 14019 /* Make sure snd_nxt is drug up */ 14020 tp->snd_nxt = tp->snd_max; 14021 } 14022 return (error); 14023 14024 } 14025 14026 /* 14027 * See bbr_output_wtime() for return values. 14028 */ 14029 static int 14030 bbr_output(struct tcpcb *tp) 14031 { 14032 int32_t ret; 14033 struct timeval tv; 14034 14035 NET_EPOCH_ASSERT(); 14036 14037 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14038 (void)tcp_get_usecs(&tv); 14039 ret = bbr_output_wtime(tp, &tv); 14040 return (ret); 14041 } 14042 14043 static void 14044 bbr_mtu_chg(struct tcpcb *tp) 14045 { 14046 struct tcp_bbr *bbr; 14047 struct bbr_sendmap *rsm, *frsm = NULL; 14048 uint32_t maxseg; 14049 14050 /* 14051 * The MTU has changed. a) Clear the sack filter. b) Mark everything 14052 * over the current size as SACK_PASS so a retransmit will occur. 14053 */ 14054 14055 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14056 maxseg = tp->t_maxseg - bbr->rc_last_options; 14057 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 14058 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 14059 /* Don't mess with ones acked (by sack?) */ 14060 if (rsm->r_flags & BBR_ACKED) 14061 continue; 14062 if ((rsm->r_end - rsm->r_start) > maxseg) { 14063 /* 14064 * We mark sack-passed on all the previous large 14065 * sends we did. This will force them to retransmit. 14066 */ 14067 rsm->r_flags |= BBR_SACK_PASSED; 14068 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) && 14069 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) { 14070 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 14071 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 14072 rsm->r_flags |= BBR_MARKED_LOST; 14073 } 14074 if (frsm == NULL) 14075 frsm = rsm; 14076 } 14077 } 14078 if (frsm) { 14079 bbr->r_ctl.rc_resend = frsm; 14080 } 14081 } 14082 14083 static int 14084 bbr_pru_options(struct tcpcb *tp, int flags) 14085 { 14086 if (flags & PRUS_OOB) 14087 return (EOPNOTSUPP); 14088 return (0); 14089 } 14090 14091 static void 14092 bbr_switch_failed(struct tcpcb *tp) 14093 { 14094 /* 14095 * If a switch fails we only need to 14096 * make sure mbuf_queuing is still in place. 14097 * We also need to make sure we are still in 14098 * ticks granularity (though we should probably 14099 * change bbr to go to USECs). 14100 * 14101 * For timers we need to see if we are still in the 14102 * pacer (if our flags are up) if so we are good, if 14103 * not we need to get back into the pacer. 14104 */ 14105 struct timeval tv; 14106 uint32_t cts; 14107 uint32_t toval; 14108 struct tcp_bbr *bbr; 14109 struct hpts_diag diag; 14110 14111 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 14112 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 14113 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 14114 if (tp->t_in_hpts > IHPTS_NONE) { 14115 return; 14116 } 14117 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14118 cts = tcp_get_usecs(&tv); 14119 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14120 if (TSTMP_GT(bbr->rc_pacer_started, cts)) { 14121 toval = bbr->rc_pacer_started - cts; 14122 } else { 14123 /* one slot please */ 14124 toval = HPTS_USECS_PER_SLOT; 14125 } 14126 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 14127 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 14128 toval = bbr->r_ctl.rc_timer_exp - cts; 14129 } else { 14130 /* one slot please */ 14131 toval = HPTS_USECS_PER_SLOT; 14132 } 14133 } else 14134 toval = HPTS_USECS_PER_SLOT; 14135 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval), 14136 __LINE__, &diag); 14137 bbr_log_hpts_diag(bbr, cts, &diag); 14138 } 14139 14140 struct tcp_function_block __tcp_bbr = { 14141 .tfb_tcp_block_name = __XSTRING(STACKNAME), 14142 .tfb_tcp_output = bbr_output, 14143 .tfb_do_queued_segments = ctf_do_queued_segments, 14144 .tfb_do_segment_nounlock = bbr_do_segment_nounlock, 14145 .tfb_tcp_do_segment = bbr_do_segment, 14146 .tfb_tcp_ctloutput = bbr_ctloutput, 14147 .tfb_tcp_fb_init = bbr_init, 14148 .tfb_tcp_fb_fini = bbr_fini, 14149 .tfb_tcp_timer_stop_all = bbr_stopall, 14150 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr, 14151 .tfb_tcp_handoff_ok = bbr_handoff_ok, 14152 .tfb_tcp_mtu_chg = bbr_mtu_chg, 14153 .tfb_pru_options = bbr_pru_options, 14154 .tfb_switch_failed = bbr_switch_failed, 14155 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK, 14156 }; 14157 14158 /* 14159 * bbr_ctloutput() must drop the inpcb lock before performing copyin on 14160 * socket option arguments. When it re-acquires the lock after the copy, it 14161 * has to revalidate that the connection is still valid for the socket 14162 * option. 14163 */ 14164 static int 14165 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14166 { 14167 struct epoch_tracker et; 14168 struct inpcb *inp = tptoinpcb(tp); 14169 struct tcp_bbr *bbr; 14170 int32_t error = 0, optval; 14171 14172 switch (sopt->sopt_level) { 14173 case IPPROTO_IPV6: 14174 case IPPROTO_IP: 14175 return (tcp_default_ctloutput(tp, sopt)); 14176 } 14177 14178 switch (sopt->sopt_name) { 14179 case TCP_RACK_PACE_MAX_SEG: 14180 case TCP_RACK_MIN_TO: 14181 case TCP_RACK_REORD_THRESH: 14182 case TCP_RACK_REORD_FADE: 14183 case TCP_RACK_TLP_THRESH: 14184 case TCP_RACK_PKT_DELAY: 14185 case TCP_BBR_ALGORITHM: 14186 case TCP_BBR_TSLIMITS: 14187 case TCP_BBR_IWINTSO: 14188 case TCP_BBR_STARTUP_PG: 14189 case TCP_BBR_DRAIN_PG: 14190 case TCP_BBR_PROBE_RTT_INT: 14191 case TCP_BBR_PROBE_RTT_GAIN: 14192 case TCP_BBR_PROBE_RTT_LEN: 14193 case TCP_BBR_STARTUP_LOSS_EXIT: 14194 case TCP_BBR_USEDEL_RATE: 14195 case TCP_BBR_MIN_RTO: 14196 case TCP_BBR_MAX_RTO: 14197 case TCP_BBR_PACE_PER_SEC: 14198 case TCP_DELACK: 14199 case TCP_BBR_PACE_DEL_TAR: 14200 case TCP_BBR_SEND_IWND_IN_TSO: 14201 case TCP_BBR_EXTRA_STATE: 14202 case TCP_BBR_UTTER_MAX_TSO: 14203 case TCP_BBR_MIN_TOPACEOUT: 14204 case TCP_BBR_FLOOR_MIN_TSO: 14205 case TCP_BBR_TSTMP_RAISES: 14206 case TCP_BBR_POLICER_DETECT: 14207 case TCP_BBR_USE_RACK_CHEAT: 14208 case TCP_DATA_AFTER_CLOSE: 14209 case TCP_BBR_HDWR_PACE: 14210 case TCP_BBR_PACE_SEG_MAX: 14211 case TCP_BBR_PACE_SEG_MIN: 14212 case TCP_BBR_PACE_CROSS: 14213 case TCP_BBR_PACE_OH: 14214 case TCP_BBR_TMR_PACE_OH: 14215 case TCP_BBR_RACK_RTT_USE: 14216 case TCP_BBR_RETRAN_WTSO: 14217 break; 14218 default: 14219 return (tcp_default_ctloutput(tp, sopt)); 14220 break; 14221 } 14222 INP_WUNLOCK(inp); 14223 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 14224 if (error) 14225 return (error); 14226 INP_WLOCK(inp); 14227 if (inp->inp_flags & INP_DROPPED) { 14228 INP_WUNLOCK(inp); 14229 return (ECONNRESET); 14230 } 14231 if (tp->t_fb != &__tcp_bbr) { 14232 INP_WUNLOCK(inp); 14233 return (ENOPROTOOPT); 14234 } 14235 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14236 switch (sopt->sopt_name) { 14237 case TCP_BBR_PACE_PER_SEC: 14238 BBR_OPTS_INC(tcp_bbr_pace_per_sec); 14239 bbr->r_ctl.bbr_hptsi_per_second = optval; 14240 break; 14241 case TCP_BBR_PACE_DEL_TAR: 14242 BBR_OPTS_INC(tcp_bbr_pace_del_tar); 14243 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval; 14244 break; 14245 case TCP_BBR_PACE_SEG_MAX: 14246 BBR_OPTS_INC(tcp_bbr_pace_seg_max); 14247 bbr->r_ctl.bbr_hptsi_segments_max = optval; 14248 break; 14249 case TCP_BBR_PACE_SEG_MIN: 14250 BBR_OPTS_INC(tcp_bbr_pace_seg_min); 14251 bbr->r_ctl.bbr_hptsi_bytes_min = optval; 14252 break; 14253 case TCP_BBR_PACE_CROSS: 14254 BBR_OPTS_INC(tcp_bbr_pace_cross); 14255 bbr->r_ctl.bbr_cross_over = optval; 14256 break; 14257 case TCP_BBR_ALGORITHM: 14258 BBR_OPTS_INC(tcp_bbr_algorithm); 14259 if (optval && (bbr->rc_use_google == 0)) { 14260 /* Turn on the google mode */ 14261 bbr_google_mode_on(bbr); 14262 if ((optval > 3) && (optval < 500)) { 14263 /* 14264 * Must be at least greater than .3% 14265 * and must be less than 50.0%. 14266 */ 14267 bbr->r_ctl.bbr_google_discount = optval; 14268 } 14269 } else if ((optval == 0) && (bbr->rc_use_google == 1)) { 14270 /* Turn off the google mode */ 14271 bbr_google_mode_off(bbr); 14272 } 14273 break; 14274 case TCP_BBR_TSLIMITS: 14275 BBR_OPTS_INC(tcp_bbr_tslimits); 14276 if (optval == 1) 14277 bbr->rc_use_ts_limit = 1; 14278 else if (optval == 0) 14279 bbr->rc_use_ts_limit = 0; 14280 else 14281 error = EINVAL; 14282 break; 14283 14284 case TCP_BBR_IWINTSO: 14285 BBR_OPTS_INC(tcp_bbr_iwintso); 14286 if ((optval >= 0) && (optval < 128)) { 14287 uint32_t twin; 14288 14289 bbr->rc_init_win = optval; 14290 twin = bbr_initial_cwnd(bbr, tp); 14291 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd)) 14292 tp->snd_cwnd = twin; 14293 else 14294 error = EBUSY; 14295 } else 14296 error = EINVAL; 14297 break; 14298 case TCP_BBR_STARTUP_PG: 14299 BBR_OPTS_INC(tcp_bbr_startup_pg); 14300 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) { 14301 bbr->r_ctl.rc_startup_pg = optval; 14302 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 14303 bbr->r_ctl.rc_bbr_hptsi_gain = optval; 14304 } 14305 } else 14306 error = EINVAL; 14307 break; 14308 case TCP_BBR_DRAIN_PG: 14309 BBR_OPTS_INC(tcp_bbr_drain_pg); 14310 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) 14311 bbr->r_ctl.rc_drain_pg = optval; 14312 else 14313 error = EINVAL; 14314 break; 14315 case TCP_BBR_PROBE_RTT_LEN: 14316 BBR_OPTS_INC(tcp_bbr_probertt_len); 14317 if (optval <= 1) 14318 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND)); 14319 else 14320 error = EINVAL; 14321 break; 14322 case TCP_BBR_PROBE_RTT_GAIN: 14323 BBR_OPTS_INC(tcp_bbr_probertt_gain); 14324 if (optval <= BBR_UNIT) 14325 bbr->r_ctl.bbr_rttprobe_gain_val = optval; 14326 else 14327 error = EINVAL; 14328 break; 14329 case TCP_BBR_PROBE_RTT_INT: 14330 BBR_OPTS_INC(tcp_bbr_probe_rtt_int); 14331 if (optval > 1000) 14332 bbr->r_ctl.rc_probertt_int = optval; 14333 else 14334 error = EINVAL; 14335 break; 14336 case TCP_BBR_MIN_TOPACEOUT: 14337 BBR_OPTS_INC(tcp_bbr_topaceout); 14338 if (optval == 0) { 14339 bbr->no_pacing_until = 0; 14340 bbr->rc_no_pacing = 0; 14341 } else if (optval <= 0x00ff) { 14342 bbr->no_pacing_until = optval; 14343 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) && 14344 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){ 14345 /* Turn on no pacing */ 14346 bbr->rc_no_pacing = 1; 14347 } 14348 } else 14349 error = EINVAL; 14350 break; 14351 case TCP_BBR_STARTUP_LOSS_EXIT: 14352 BBR_OPTS_INC(tcp_bbr_startup_loss_exit); 14353 bbr->rc_loss_exit = optval; 14354 break; 14355 case TCP_BBR_USEDEL_RATE: 14356 error = EINVAL; 14357 break; 14358 case TCP_BBR_MIN_RTO: 14359 BBR_OPTS_INC(tcp_bbr_min_rto); 14360 bbr->r_ctl.rc_min_rto_ms = optval; 14361 break; 14362 case TCP_BBR_MAX_RTO: 14363 BBR_OPTS_INC(tcp_bbr_max_rto); 14364 bbr->rc_max_rto_sec = optval; 14365 break; 14366 case TCP_RACK_MIN_TO: 14367 /* Minimum time between rack t-o's in ms */ 14368 BBR_OPTS_INC(tcp_rack_min_to); 14369 bbr->r_ctl.rc_min_to = optval; 14370 break; 14371 case TCP_RACK_REORD_THRESH: 14372 /* RACK reorder threshold (shift amount) */ 14373 BBR_OPTS_INC(tcp_rack_reord_thresh); 14374 if ((optval > 0) && (optval < 31)) 14375 bbr->r_ctl.rc_reorder_shift = optval; 14376 else 14377 error = EINVAL; 14378 break; 14379 case TCP_RACK_REORD_FADE: 14380 /* Does reordering fade after ms time */ 14381 BBR_OPTS_INC(tcp_rack_reord_fade); 14382 bbr->r_ctl.rc_reorder_fade = optval; 14383 break; 14384 case TCP_RACK_TLP_THRESH: 14385 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14386 BBR_OPTS_INC(tcp_rack_tlp_thresh); 14387 if (optval) 14388 bbr->rc_tlp_threshold = optval; 14389 else 14390 error = EINVAL; 14391 break; 14392 case TCP_BBR_USE_RACK_CHEAT: 14393 BBR_OPTS_INC(tcp_use_rackcheat); 14394 if (bbr->rc_use_google) { 14395 error = EINVAL; 14396 break; 14397 } 14398 BBR_OPTS_INC(tcp_rack_cheat); 14399 if (optval) 14400 bbr->bbr_use_rack_cheat = 1; 14401 else 14402 bbr->bbr_use_rack_cheat = 0; 14403 break; 14404 case TCP_BBR_FLOOR_MIN_TSO: 14405 BBR_OPTS_INC(tcp_utter_max_tso); 14406 if ((optval >= 0) && (optval < 40)) 14407 bbr->r_ctl.bbr_hptsi_segments_floor = optval; 14408 else 14409 error = EINVAL; 14410 break; 14411 case TCP_BBR_UTTER_MAX_TSO: 14412 BBR_OPTS_INC(tcp_utter_max_tso); 14413 if ((optval >= 0) && (optval < 0xffff)) 14414 bbr->r_ctl.bbr_utter_max = optval; 14415 else 14416 error = EINVAL; 14417 break; 14418 14419 case TCP_BBR_EXTRA_STATE: 14420 BBR_OPTS_INC(tcp_extra_state); 14421 if (optval) 14422 bbr->rc_use_idle_restart = 1; 14423 else 14424 bbr->rc_use_idle_restart = 0; 14425 break; 14426 case TCP_BBR_SEND_IWND_IN_TSO: 14427 BBR_OPTS_INC(tcp_iwnd_tso); 14428 if (optval) { 14429 bbr->bbr_init_win_cheat = 1; 14430 if (bbr->rc_past_init_win == 0) { 14431 uint32_t cts; 14432 cts = tcp_get_usecs(&bbr->rc_tv); 14433 tcp_bbr_tso_size_check(bbr, cts); 14434 } 14435 } else 14436 bbr->bbr_init_win_cheat = 0; 14437 break; 14438 case TCP_BBR_HDWR_PACE: 14439 BBR_OPTS_INC(tcp_hdwr_pacing); 14440 if (optval){ 14441 bbr->bbr_hdw_pace_ena = 1; 14442 bbr->bbr_attempt_hdwr_pace = 0; 14443 } else { 14444 bbr->bbr_hdw_pace_ena = 0; 14445 #ifdef RATELIMIT 14446 if (bbr->r_ctl.crte != NULL) { 14447 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 14448 bbr->r_ctl.crte = NULL; 14449 } 14450 #endif 14451 } 14452 break; 14453 14454 case TCP_DELACK: 14455 BBR_OPTS_INC(tcp_delack); 14456 if (optval < 100) { 14457 if (optval == 0) /* off */ 14458 tp->t_delayed_ack = 0; 14459 else if (optval == 1) /* on which is 2 */ 14460 tp->t_delayed_ack = 2; 14461 else /* higher than 2 and less than 100 */ 14462 tp->t_delayed_ack = optval; 14463 if (tp->t_flags & TF_DELACK) { 14464 tp->t_flags &= ~TF_DELACK; 14465 tp->t_flags |= TF_ACKNOW; 14466 NET_EPOCH_ENTER(et); 14467 bbr_output(tp); 14468 NET_EPOCH_EXIT(et); 14469 } 14470 } else 14471 error = EINVAL; 14472 break; 14473 case TCP_RACK_PKT_DELAY: 14474 /* RACK added ms i.e. rack-rtt + reord + N */ 14475 BBR_OPTS_INC(tcp_rack_pkt_delay); 14476 bbr->r_ctl.rc_pkt_delay = optval; 14477 break; 14478 14479 case TCP_BBR_RETRAN_WTSO: 14480 BBR_OPTS_INC(tcp_retran_wtso); 14481 if (optval) 14482 bbr->rc_resends_use_tso = 1; 14483 else 14484 bbr->rc_resends_use_tso = 0; 14485 break; 14486 case TCP_DATA_AFTER_CLOSE: 14487 BBR_OPTS_INC(tcp_data_ac); 14488 if (optval) 14489 bbr->rc_allow_data_af_clo = 1; 14490 else 14491 bbr->rc_allow_data_af_clo = 0; 14492 break; 14493 case TCP_BBR_POLICER_DETECT: 14494 BBR_OPTS_INC(tcp_policer_det); 14495 if (bbr->rc_use_google == 0) 14496 error = EINVAL; 14497 else if (optval) 14498 bbr->r_use_policer = 1; 14499 else 14500 bbr->r_use_policer = 0; 14501 break; 14502 14503 case TCP_BBR_TSTMP_RAISES: 14504 BBR_OPTS_INC(tcp_ts_raises); 14505 if (optval) 14506 bbr->ts_can_raise = 1; 14507 else 14508 bbr->ts_can_raise = 0; 14509 break; 14510 case TCP_BBR_TMR_PACE_OH: 14511 BBR_OPTS_INC(tcp_pacing_oh_tmr); 14512 if (bbr->rc_use_google) { 14513 error = EINVAL; 14514 } else { 14515 if (optval) 14516 bbr->r_ctl.rc_incr_tmrs = 1; 14517 else 14518 bbr->r_ctl.rc_incr_tmrs = 0; 14519 } 14520 break; 14521 case TCP_BBR_PACE_OH: 14522 BBR_OPTS_INC(tcp_pacing_oh); 14523 if (bbr->rc_use_google) { 14524 error = EINVAL; 14525 } else { 14526 if (optval > (BBR_INCL_TCP_OH| 14527 BBR_INCL_IP_OH| 14528 BBR_INCL_ENET_OH)) { 14529 error = EINVAL; 14530 break; 14531 } 14532 if (optval & BBR_INCL_TCP_OH) 14533 bbr->r_ctl.rc_inc_tcp_oh = 1; 14534 else 14535 bbr->r_ctl.rc_inc_tcp_oh = 0; 14536 if (optval & BBR_INCL_IP_OH) 14537 bbr->r_ctl.rc_inc_ip_oh = 1; 14538 else 14539 bbr->r_ctl.rc_inc_ip_oh = 0; 14540 if (optval & BBR_INCL_ENET_OH) 14541 bbr->r_ctl.rc_inc_enet_oh = 1; 14542 else 14543 bbr->r_ctl.rc_inc_enet_oh = 0; 14544 } 14545 break; 14546 default: 14547 return (tcp_default_ctloutput(tp, sopt)); 14548 break; 14549 } 14550 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 14551 INP_WUNLOCK(inp); 14552 return (error); 14553 } 14554 14555 /* 14556 * return 0 on success, error-num on failure 14557 */ 14558 static int 14559 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14560 { 14561 struct inpcb *inp = tptoinpcb(tp); 14562 struct tcp_bbr *bbr; 14563 uint64_t loptval; 14564 int32_t error, optval; 14565 14566 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14567 if (bbr == NULL) { 14568 INP_WUNLOCK(inp); 14569 return (EINVAL); 14570 } 14571 /* 14572 * Because all our options are either boolean or an int, we can just 14573 * pull everything into optval and then unlock and copy. If we ever 14574 * add a option that is not a int, then this will have quite an 14575 * impact to this routine. 14576 */ 14577 switch (sopt->sopt_name) { 14578 case TCP_BBR_PACE_PER_SEC: 14579 optval = bbr->r_ctl.bbr_hptsi_per_second; 14580 break; 14581 case TCP_BBR_PACE_DEL_TAR: 14582 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar; 14583 break; 14584 case TCP_BBR_PACE_SEG_MAX: 14585 optval = bbr->r_ctl.bbr_hptsi_segments_max; 14586 break; 14587 case TCP_BBR_MIN_TOPACEOUT: 14588 optval = bbr->no_pacing_until; 14589 break; 14590 case TCP_BBR_PACE_SEG_MIN: 14591 optval = bbr->r_ctl.bbr_hptsi_bytes_min; 14592 break; 14593 case TCP_BBR_PACE_CROSS: 14594 optval = bbr->r_ctl.bbr_cross_over; 14595 break; 14596 case TCP_BBR_ALGORITHM: 14597 optval = bbr->rc_use_google; 14598 break; 14599 case TCP_BBR_TSLIMITS: 14600 optval = bbr->rc_use_ts_limit; 14601 break; 14602 case TCP_BBR_IWINTSO: 14603 optval = bbr->rc_init_win; 14604 break; 14605 case TCP_BBR_STARTUP_PG: 14606 optval = bbr->r_ctl.rc_startup_pg; 14607 break; 14608 case TCP_BBR_DRAIN_PG: 14609 optval = bbr->r_ctl.rc_drain_pg; 14610 break; 14611 case TCP_BBR_PROBE_RTT_INT: 14612 optval = bbr->r_ctl.rc_probertt_int; 14613 break; 14614 case TCP_BBR_PROBE_RTT_LEN: 14615 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND); 14616 break; 14617 case TCP_BBR_PROBE_RTT_GAIN: 14618 optval = bbr->r_ctl.bbr_rttprobe_gain_val; 14619 break; 14620 case TCP_BBR_STARTUP_LOSS_EXIT: 14621 optval = bbr->rc_loss_exit; 14622 break; 14623 case TCP_BBR_USEDEL_RATE: 14624 loptval = get_filter_value(&bbr->r_ctl.rc_delrate); 14625 break; 14626 case TCP_BBR_MIN_RTO: 14627 optval = bbr->r_ctl.rc_min_rto_ms; 14628 break; 14629 case TCP_BBR_MAX_RTO: 14630 optval = bbr->rc_max_rto_sec; 14631 break; 14632 case TCP_RACK_PACE_MAX_SEG: 14633 /* Max segments in a pace */ 14634 optval = bbr->r_ctl.rc_pace_max_segs; 14635 break; 14636 case TCP_RACK_MIN_TO: 14637 /* Minimum time between rack t-o's in ms */ 14638 optval = bbr->r_ctl.rc_min_to; 14639 break; 14640 case TCP_RACK_REORD_THRESH: 14641 /* RACK reorder threshold (shift amount) */ 14642 optval = bbr->r_ctl.rc_reorder_shift; 14643 break; 14644 case TCP_RACK_REORD_FADE: 14645 /* Does reordering fade after ms time */ 14646 optval = bbr->r_ctl.rc_reorder_fade; 14647 break; 14648 case TCP_BBR_USE_RACK_CHEAT: 14649 /* Do we use the rack cheat for rxt */ 14650 optval = bbr->bbr_use_rack_cheat; 14651 break; 14652 case TCP_BBR_FLOOR_MIN_TSO: 14653 optval = bbr->r_ctl.bbr_hptsi_segments_floor; 14654 break; 14655 case TCP_BBR_UTTER_MAX_TSO: 14656 optval = bbr->r_ctl.bbr_utter_max; 14657 break; 14658 case TCP_BBR_SEND_IWND_IN_TSO: 14659 /* Do we send TSO size segments initially */ 14660 optval = bbr->bbr_init_win_cheat; 14661 break; 14662 case TCP_BBR_EXTRA_STATE: 14663 optval = bbr->rc_use_idle_restart; 14664 break; 14665 case TCP_RACK_TLP_THRESH: 14666 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14667 optval = bbr->rc_tlp_threshold; 14668 break; 14669 case TCP_RACK_PKT_DELAY: 14670 /* RACK added ms i.e. rack-rtt + reord + N */ 14671 optval = bbr->r_ctl.rc_pkt_delay; 14672 break; 14673 case TCP_BBR_RETRAN_WTSO: 14674 optval = bbr->rc_resends_use_tso; 14675 break; 14676 case TCP_DATA_AFTER_CLOSE: 14677 optval = bbr->rc_allow_data_af_clo; 14678 break; 14679 case TCP_DELACK: 14680 optval = tp->t_delayed_ack; 14681 break; 14682 case TCP_BBR_HDWR_PACE: 14683 optval = bbr->bbr_hdw_pace_ena; 14684 break; 14685 case TCP_BBR_POLICER_DETECT: 14686 optval = bbr->r_use_policer; 14687 break; 14688 case TCP_BBR_TSTMP_RAISES: 14689 optval = bbr->ts_can_raise; 14690 break; 14691 case TCP_BBR_TMR_PACE_OH: 14692 optval = bbr->r_ctl.rc_incr_tmrs; 14693 break; 14694 case TCP_BBR_PACE_OH: 14695 optval = 0; 14696 if (bbr->r_ctl.rc_inc_tcp_oh) 14697 optval |= BBR_INCL_TCP_OH; 14698 if (bbr->r_ctl.rc_inc_ip_oh) 14699 optval |= BBR_INCL_IP_OH; 14700 if (bbr->r_ctl.rc_inc_enet_oh) 14701 optval |= BBR_INCL_ENET_OH; 14702 break; 14703 default: 14704 return (tcp_default_ctloutput(tp, sopt)); 14705 break; 14706 } 14707 INP_WUNLOCK(inp); 14708 if (sopt->sopt_name == TCP_BBR_USEDEL_RATE) 14709 error = sooptcopyout(sopt, &loptval, sizeof loptval); 14710 else 14711 error = sooptcopyout(sopt, &optval, sizeof optval); 14712 return (error); 14713 } 14714 14715 /* 14716 * return 0 on success, error-num on failure 14717 */ 14718 static int 14719 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 14720 { 14721 if (sopt->sopt_dir == SOPT_SET) { 14722 return (bbr_set_sockopt(tp, sopt)); 14723 } else if (sopt->sopt_dir == SOPT_GET) { 14724 return (bbr_get_sockopt(tp, sopt)); 14725 } else { 14726 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 14727 } 14728 } 14729 14730 static const char *bbr_stack_names[] = { 14731 __XSTRING(STACKNAME), 14732 #ifdef STACKALIAS 14733 __XSTRING(STACKALIAS), 14734 #endif 14735 }; 14736 14737 static bool bbr_mod_inited = false; 14738 14739 static int 14740 tcp_addbbr(module_t mod, int32_t type, void *data) 14741 { 14742 int32_t err = 0; 14743 int num_stacks; 14744 14745 switch (type) { 14746 case MOD_LOAD: 14747 printf("Attempting to load " __XSTRING(MODNAME) "\n"); 14748 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 14749 sizeof(struct bbr_sendmap), 14750 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 14751 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 14752 sizeof(struct tcp_bbr), 14753 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 14754 sysctl_ctx_init(&bbr_sysctl_ctx); 14755 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 14756 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 14757 OID_AUTO, 14758 #ifdef STACKALIAS 14759 __XSTRING(STACKALIAS), 14760 #else 14761 __XSTRING(STACKNAME), 14762 #endif 14763 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 14764 ""); 14765 if (bbr_sysctl_root == NULL) { 14766 printf("Failed to add sysctl node\n"); 14767 err = EFAULT; 14768 goto free_uma; 14769 } 14770 bbr_init_sysctls(); 14771 num_stacks = nitems(bbr_stack_names); 14772 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK, 14773 bbr_stack_names, &num_stacks); 14774 if (err) { 14775 printf("Failed to register %s stack name for " 14776 "%s module\n", bbr_stack_names[num_stacks], 14777 __XSTRING(MODNAME)); 14778 sysctl_ctx_free(&bbr_sysctl_ctx); 14779 free_uma: 14780 uma_zdestroy(bbr_zone); 14781 uma_zdestroy(bbr_pcb_zone); 14782 bbr_counter_destroy(); 14783 printf("Failed to register " __XSTRING(MODNAME) 14784 " module err:%d\n", err); 14785 return (err); 14786 } 14787 tcp_lro_reg_mbufq(); 14788 bbr_mod_inited = true; 14789 printf(__XSTRING(MODNAME) " is now available\n"); 14790 break; 14791 case MOD_QUIESCE: 14792 err = deregister_tcp_functions(&__tcp_bbr, true, false); 14793 break; 14794 case MOD_UNLOAD: 14795 err = deregister_tcp_functions(&__tcp_bbr, false, true); 14796 if (err == EBUSY) 14797 break; 14798 if (bbr_mod_inited) { 14799 uma_zdestroy(bbr_zone); 14800 uma_zdestroy(bbr_pcb_zone); 14801 sysctl_ctx_free(&bbr_sysctl_ctx); 14802 bbr_counter_destroy(); 14803 printf(__XSTRING(MODNAME) 14804 " is now no longer available\n"); 14805 bbr_mod_inited = false; 14806 } 14807 tcp_lro_dereg_mbufq(); 14808 err = 0; 14809 break; 14810 default: 14811 return (EOPNOTSUPP); 14812 } 14813 return (err); 14814 } 14815 14816 static moduledata_t tcp_bbr = { 14817 .name = __XSTRING(MODNAME), 14818 .evhand = tcp_addbbr, 14819 .priv = 0 14820 }; 14821 14822 MODULE_VERSION(MODNAME, 1); 14823 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 14824 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 14825