1 /*- 2 * Copyright (c) 2016-2020 Netflix, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 /** 27 * Author: Randall Stewart <rrs@netflix.com> 28 * This work is based on the ACM Queue paper 29 * BBR - Congestion Based Congestion Control 30 * and also numerous discussions with Neal, Yuchung and Van. 31 */ 32 33 #include <sys/cdefs.h> 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 #include "opt_ratelimit.h" 38 #include <sys/param.h> 39 #include <sys/arb.h> 40 #include <sys/module.h> 41 #include <sys/kernel.h> 42 #include <sys/libkern.h> 43 #ifdef TCP_HHOOK 44 #include <sys/hhook.h> 45 #endif 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 #ifdef STATS 54 #include <sys/qmath.h> 55 #include <sys/tree.h> 56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 57 #endif 58 #include <sys/refcount.h> 59 #include <sys/queue.h> 60 #include <sys/eventhandler.h> 61 #include <sys/smp.h> 62 #include <sys/kthread.h> 63 #include <sys/lock.h> 64 #include <sys/mutex.h> 65 #include <sys/tim_filter.h> 66 #include <sys/time.h> 67 #include <sys/protosw.h> 68 #include <vm/uma.h> 69 #include <sys/kern_prefetch.h> 70 71 #include <net/route.h> 72 #include <net/route/nhop.h> 73 #include <net/vnet.h> 74 75 #define TCPSTATES /* for logging */ 76 77 #include <netinet/in.h> 78 #include <netinet/in_kdtrace.h> 79 #include <netinet/in_pcb.h> 80 #include <netinet/ip.h> 81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 83 #include <netinet/ip_var.h> 84 #include <netinet/ip6.h> 85 #include <netinet6/in6_pcb.h> 86 #include <netinet6/ip6_var.h> 87 #define TCPOUTFLAGS 88 #include <netinet/tcp.h> 89 #include <netinet/tcp_fsm.h> 90 #include <netinet/tcp_seq.h> 91 #include <netinet/tcp_timer.h> 92 #include <netinet/tcp_var.h> 93 #include <netinet/tcpip.h> 94 #include <netinet/tcp_hpts.h> 95 #include <netinet/cc/cc.h> 96 #include <netinet/tcp_log_buf.h> 97 #include <netinet/tcp_ratelimit.h> 98 #include <netinet/tcp_lro.h> 99 #ifdef TCP_OFFLOAD 100 #include <netinet/tcp_offload.h> 101 #endif 102 #ifdef INET6 103 #include <netinet6/tcp6_var.h> 104 #endif 105 #include <netinet/tcp_fastopen.h> 106 107 #include <netipsec/ipsec_support.h> 108 #include <net/if.h> 109 #include <net/if_var.h> 110 #include <net/ethernet.h> 111 112 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 113 #include <netipsec/ipsec.h> 114 #include <netipsec/ipsec6.h> 115 #endif /* IPSEC */ 116 117 #include <netinet/udp.h> 118 #include <netinet/udp_var.h> 119 #include <machine/in_cksum.h> 120 121 #ifdef MAC 122 #include <security/mac/mac_framework.h> 123 #endif 124 125 #include "sack_filter.h" 126 #include "tcp_bbr.h" 127 #include "rack_bbr_common.h" 128 uma_zone_t bbr_zone; 129 uma_zone_t bbr_pcb_zone; 130 131 struct sysctl_ctx_list bbr_sysctl_ctx; 132 struct sysctl_oid *bbr_sysctl_root; 133 134 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \ 135 (tv) = (value); \ 136 if ((u_long)(tv) < (u_long)(tvmin)) \ 137 (tv) = (tvmin); \ 138 if ((u_long)(tv) > (u_long)(tvmax)) \ 139 (tv) = (tvmax); \ 140 } while(0) 141 142 /*#define BBR_INVARIANT 1*/ 143 144 /* 145 * initial window 146 */ 147 static uint32_t bbr_def_init_win = 10; 148 static int32_t bbr_persist_min = 250000; /* 250ms */ 149 static int32_t bbr_persist_max = 1000000; /* 1 Second */ 150 static int32_t bbr_cwnd_may_shrink = 0; 151 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP; 152 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT; 153 static int32_t bbr_hardware_pacing_limit = 8000; 154 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */ 155 static int32_t bbr_no_retran = 0; 156 157 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */ 158 static int32_t bbr_max_net_error_cnt = 10; 159 /* Should the following be dynamic too -- loss wise */ 160 static int32_t bbr_rtt_gain_thresh = 0; 161 /* Measurement controls */ 162 static int32_t bbr_use_google_algo = 1; 163 static int32_t bbr_ts_limiting = 1; 164 static int32_t bbr_ts_can_raise = 0; 165 static int32_t bbr_do_red = 600; 166 static int32_t bbr_red_scale = 20000; 167 static int32_t bbr_red_mul = 1; 168 static int32_t bbr_red_div = 2; 169 static int32_t bbr_red_growth_restrict = 1; 170 static int32_t bbr_target_is_bbunit = 0; 171 static int32_t bbr_drop_limit = 0; 172 /* 173 * How much gain do we need to see to 174 * stay in startup? 175 */ 176 static int32_t bbr_marks_rxt_sack_passed = 0; 177 static int32_t bbr_start_exit = 25; 178 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */ 179 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */ 180 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */ 181 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this 182 * if we go back ever to where the pacer 183 * has priority over timers. 184 */ 185 static int32_t bbr_policer_call_from_rack_to = 0; 186 static int32_t bbr_policer_detection_enabled = 1; 187 static int32_t bbr_min_measurements_req = 1; /* We need at least 2 188 * measurements before we are 189 * "good" note that 2 == 1. 190 * This is because we use a > 191 * comparison. This means if 192 * min_measure was 0, it takes 193 * num-measures > min(0) and 194 * you get 1 measurement and 195 * you are good. Set to 1, you 196 * have to have two 197 * measurements (this is done 198 * to prevent it from being ok 199 * to have no measurements). */ 200 static int32_t bbr_no_pacing_until = 4; 201 202 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */ 203 static int32_t bbr_min_peer_delta = 20; /* 20 units */ 204 static int32_t bbr_delta_percent = 150; /* 15.0 % */ 205 206 static int32_t bbr_target_cwnd_mult_limit = 8; 207 /* 208 * bbr_cwnd_min_val is the number of 209 * segments we hold to in the RTT probe 210 * state typically 4. 211 */ 212 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS; 213 214 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS; 215 216 static int32_t bbr_gain_to_target = 1; 217 static int32_t bbr_gain_gets_extra_too = 1; 218 /* 219 * bbr_high_gain is the 2/ln(2) value we need 220 * to double the sending rate in startup. This 221 * is used for both cwnd and hptsi gain's. 222 */ 223 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1; 224 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1; 225 static int32_t bbr_use_lower_gain_in_startup = 1; 226 227 /* thresholds for reduction on drain in sub-states/drain */ 228 static int32_t bbr_drain_rtt = BBR_SRTT; 229 static int32_t bbr_drain_floor = 88; 230 static int32_t google_allow_early_out = 1; 231 static int32_t google_consider_lost = 1; 232 static int32_t bbr_drain_drop_mul = 4; 233 static int32_t bbr_drain_drop_div = 5; 234 static int32_t bbr_rand_ot = 50; 235 static int32_t bbr_can_force_probertt = 0; 236 static int32_t bbr_can_adjust_probertt = 1; 237 static int32_t bbr_probertt_sets_rtt = 0; 238 static int32_t bbr_can_use_ts_for_rtt = 1; 239 static int32_t bbr_is_ratio = 0; 240 static int32_t bbr_sub_drain_app_limit = 1; 241 static int32_t bbr_prtt_slam_cwnd = 1; 242 static int32_t bbr_sub_drain_slam_cwnd = 1; 243 static int32_t bbr_slam_cwnd_in_main_drain = 1; 244 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter 245 * hold */ 246 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4); 247 /* 248 * bbr_drain_gain is the reverse of the high_gain 249 * designed to drain back out the standing queue 250 * that is formed in startup by causing a larger 251 * hptsi gain and thus drainging the packets 252 * in flight. 253 */ 254 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885; 255 static int32_t bbr_rttprobe_gain = 192; 256 257 /* 258 * The cwnd_gain is the default cwnd gain applied when 259 * calculating a target cwnd. Note that the cwnd is 260 * a secondary factor in the way BBR works (see the 261 * paper and think about it, it will take some time). 262 * Basically the hptsi_gain spreads the packets out 263 * so you never get more than BDP to the peer even 264 * if the cwnd is high. In our implemenation that 265 * means in non-recovery/retransmission scenarios 266 * cwnd will never be reached by the flight-size. 267 */ 268 static int32_t bbr_cwnd_gain = BBR_UNIT * 2; 269 static int32_t bbr_tlp_type_to_use = BBR_SRTT; 270 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */ 271 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */ 272 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */ 273 static int32_t bbr_ignore_data_after_close = 1; 274 static int16_t bbr_hptsi_gain[] = { 275 (BBR_UNIT *5 / 4), 276 (BBR_UNIT * 3 / 4), 277 BBR_UNIT, 278 BBR_UNIT, 279 BBR_UNIT, 280 BBR_UNIT, 281 BBR_UNIT, 282 BBR_UNIT 283 }; 284 int32_t bbr_use_rack_resend_cheat = 1; 285 int32_t bbr_sends_full_iwnd = 1; 286 287 #define BBR_HPTSI_GAIN_MAX 8 288 /* 289 * The BBR module incorporates a number of 290 * TCP ideas that have been put out into the IETF 291 * over the last few years: 292 * - Yuchung Cheng's RACK TCP (for which its named) that 293 * will stop us using the number of dup acks and instead 294 * use time as the gage of when we retransmit. 295 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 296 * of Dukkipati et.al. 297 * - Van Jacobson's et.al BBR. 298 * 299 * RACK depends on SACK, so if an endpoint arrives that 300 * cannot do SACK the state machine below will shuttle the 301 * connection back to using the "default" TCP stack that is 302 * in FreeBSD. 303 * 304 * To implement BBR and RACK the original TCP stack was first decomposed 305 * into a functional state machine with individual states 306 * for each of the possible TCP connection states. The do_segment 307 * functions role in life is to mandate the connection supports SACK 308 * initially and then assure that the RACK state matches the conenction 309 * state before calling the states do_segment function. Data processing 310 * of inbound segments also now happens in the hpts_do_segment in general 311 * with only one exception. This is so we can keep the connection on 312 * a single CPU. 313 * 314 * Each state is simplified due to the fact that the original do_segment 315 * has been decomposed and we *know* what state we are in (no 316 * switches on the state) and all tests for SACK are gone. This 317 * greatly simplifies what each state does. 318 * 319 * TCP output is also over-written with a new version since it 320 * must maintain the new rack scoreboard and has had hptsi 321 * integrated as a requirment. Still todo is to eliminate the 322 * use of the callout_() system and use the hpts for all 323 * timers as well. 324 */ 325 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */ 326 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */ 327 static const int32_t bbr_min_req_free = 2; /* The min we must have on the 328 * free list */ 329 static int32_t bbr_tlp_thresh = 1; 330 static int32_t bbr_reorder_thresh = 2; 331 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def 332 * 60,000,000 - 60 seconds */ 333 static int32_t bbr_pkt_delay = 1000; 334 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */ 335 static int32_t bbr_incr_timers = 1; 336 337 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */ 338 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */ 339 static int32_t bbr_exit_startup_at_loss = 1; 340 341 /* 342 * bbr_lt_bw_ratio is 1/8th 343 * bbr_lt_bw_diff is < 4 Kbit/sec 344 */ 345 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */ 346 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */ 347 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use 348 * the lt_bw for */ 349 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure 350 * lt_bw */ 351 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */ 352 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */ 353 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */ 354 355 static int32_t bbr_verbose_logging = 0; 356 /* 357 * Currently regular tcp has a rto_min of 30ms 358 * the backoff goes 12 times so that ends up 359 * being a total of 122.850 seconds before a 360 * connection is killed. 361 */ 362 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */ 363 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */ 364 365 /****************************************************/ 366 /* DEFAULT TSO SIZING (cpu performance impacting) */ 367 /****************************************************/ 368 /* What amount is our formula using to get TSO size */ 369 static int32_t bbr_hptsi_per_second = 1000; 370 371 /* 372 * For hptsi under bbr_cross_over connections what is delay 373 * target 7ms (in usec) combined with a seg_max of 2 374 * gets us close to identical google behavior in 375 * TSO size selection (possibly more 1MSS sends). 376 */ 377 static int32_t bbr_hptsi_segments_delay_tar = 7000; 378 379 /* Does pacing delay include overhead's in its time calculations? */ 380 static int32_t bbr_include_enet_oh = 0; 381 static int32_t bbr_include_ip_oh = 1; 382 static int32_t bbr_include_tcp_oh = 1; 383 static int32_t bbr_google_discount = 10; 384 385 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */ 386 static int32_t bbr_state_is_pkt_epoch = 0; 387 static int32_t bbr_state_drain_2_tar = 1; 388 /* What is the max the 0 - bbr_cross_over MBPS TSO target 389 * can reach using our delay target. Note that this 390 * value becomes the floor for the cross over 391 * algorithm. 392 */ 393 static int32_t bbr_hptsi_segments_max = 2; 394 static int32_t bbr_hptsi_segments_floor = 1; 395 static int32_t bbr_hptsi_utter_max = 0; 396 397 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */ 398 static int32_t bbr_hptsi_bytes_min = 1460; 399 static int32_t bbr_all_get_min = 0; 400 401 /* Cross over point from algo-a to algo-b */ 402 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS; 403 404 /* Do we deal with our restart state? */ 405 static int32_t bbr_uses_idle_restart = 0; 406 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */ 407 408 /* Do we allow hardware pacing? */ 409 static int32_t bbr_allow_hdwr_pacing = 0; 410 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */ 411 static int32_t bbr_hdwr_pace_floor = 1; 412 static int32_t bbr_hdwr_pacing_delay_cnt = 10; 413 414 /****************************************************/ 415 static int32_t bbr_resends_use_tso = 0; 416 static int32_t bbr_tlp_max_resend = 2; 417 static int32_t bbr_sack_block_limit = 128; 418 419 #define BBR_MAX_STAT 19 420 counter_u64_t bbr_state_time[BBR_MAX_STAT]; 421 counter_u64_t bbr_state_lost[BBR_MAX_STAT]; 422 counter_u64_t bbr_state_resend[BBR_MAX_STAT]; 423 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE]; 424 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE]; 425 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE]; 426 counter_u64_t bbr_flows_whdwr_pacing; 427 counter_u64_t bbr_flows_nohdwr_pacing; 428 429 counter_u64_t bbr_nohdwr_pacing_enobuf; 430 counter_u64_t bbr_hdwr_pacing_enobuf; 431 432 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr); 433 434 /* 435 * Static defintions we need for forward declarations. 436 */ 437 static uint32_t 438 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, 439 uint32_t useconds_time, uint64_t bw); 440 static uint32_t 441 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain); 442 static void 443 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win); 444 static void 445 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses); 446 static void 447 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line, 448 int dolog); 449 static uint32_t 450 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain); 451 static void 452 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, 453 int32_t pkt_epoch, uint32_t losses); 454 static uint32_t 455 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, 456 struct bbr_sendmap *rsm); 457 static uint32_t 458 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp); 459 static uint32_t 460 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 461 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts); 462 static void 463 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 464 int32_t line); 465 static void 466 bbr_set_state_target(struct tcp_bbr *bbr, int line); 467 static void 468 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line); 469 static void 470 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, 471 int event, int line); 472 static void 473 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts); 474 static void 475 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts); 476 static void 477 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 478 uint32_t rtt, uint32_t line, uint8_t is_start, 479 uint16_t set); 480 static struct bbr_sendmap * 481 bbr_find_lowest_rsm(struct tcp_bbr *bbr); 482 static __inline uint32_t 483 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type); 484 static void 485 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, 486 uint8_t which); 487 static void 488 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, 489 uint32_t time_since_sent, uint32_t srtt, 490 uint32_t thresh, uint32_t to); 491 static void 492 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag); 493 static void 494 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, 495 uint32_t del_by, uint32_t cts, uint32_t sloton, 496 uint32_t prev_delay); 497 static void 498 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 499 int32_t line); 500 static void 501 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr); 502 static void 503 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts); 504 static void 505 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts); 506 static void 507 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts); 508 static void 509 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 510 uint32_t cts, uint32_t usecs, uint64_t bw, 511 uint32_t override, int mod); 512 static int bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt); 513 514 static inline uint8_t 515 bbr_state_val(struct tcp_bbr *bbr) 516 { 517 return(bbr->rc_bbr_substate); 518 } 519 520 static inline uint32_t 521 get_min_cwnd(struct tcp_bbr *bbr) 522 { 523 int mss; 524 525 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 526 bbr->r_ctl.rc_pace_max_segs); 527 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED) 528 return (bbr_cwnd_min_val_hs * mss); 529 else 530 return (bbr_cwnd_min_val * mss); 531 } 532 533 static uint32_t 534 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr) 535 { 536 uint64_t srtt, var; 537 uint64_t ret_val; 538 539 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 540 if (tp->t_srtt == 0) { 541 srtt = (uint64_t)BBR_INITIAL_RTO; 542 var = 0; 543 } else { 544 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 545 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT); 546 } 547 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]), 548 bbr_persist_min, bbr_persist_max); 549 return ((uint32_t)ret_val); 550 } 551 552 static uint32_t 553 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 554 { 555 /* 556 * Start the FR timer, we do this based on getting the first one in 557 * the rc_tmap. Note that if its NULL we must stop the timer. in all 558 * events we need to stop the running timer (if its running) before 559 * starting the new one. 560 */ 561 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 562 int32_t idx; 563 int32_t is_tlp_timer = 0; 564 struct bbr_sendmap *rsm; 565 566 if (bbr->rc_all_timers_stopped) { 567 /* All timers have been stopped none are to run */ 568 return (0); 569 } 570 if (bbr->rc_in_persist) { 571 /* We can't start any timer in persists */ 572 return (bbr_get_persists_timer_val(tp, bbr)); 573 } 574 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 575 if ((rsm == NULL) || 576 ((tp->t_flags & TF_SACK_PERMIT) == 0) || 577 (tp->t_state < TCPS_ESTABLISHED)) { 578 /* Nothing on the send map */ 579 activate_rxt: 580 if (SEQ_LT(tp->snd_una, tp->snd_max) || 581 sbavail(&tptosocket(tp)->so_snd)) { 582 uint64_t tov; 583 584 time_since_sent = 0; 585 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 586 if (rsm) { 587 idx = rsm->r_rtr_cnt - 1; 588 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 589 tstmp_touse = rsm->r_tim_lastsent[idx]; 590 else 591 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 592 if (TSTMP_GT(tstmp_touse, cts)) 593 time_since_sent = cts - tstmp_touse; 594 } 595 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 596 if (tp->t_srtt == 0) 597 tov = BBR_INITIAL_RTO; 598 else 599 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) + 600 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT); 601 if (tp->t_rxtshift) 602 tov *= tcp_backoff[tp->t_rxtshift]; 603 if (tov > time_since_sent) 604 tov -= time_since_sent; 605 else 606 tov = bbr->r_ctl.rc_min_to; 607 TCPT_RANGESET_NOSLOP(to, tov, 608 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC), 609 (bbr->rc_max_rto_sec * USECS_IN_SECOND)); 610 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to); 611 return (to); 612 } 613 return (0); 614 } 615 if (rsm->r_flags & BBR_ACKED) { 616 rsm = bbr_find_lowest_rsm(bbr); 617 if (rsm == NULL) { 618 /* No lowest? */ 619 goto activate_rxt; 620 } 621 } 622 /* Convert from ms to usecs */ 623 if (rsm->r_flags & BBR_SACK_PASSED) { 624 if ((tp->t_flags & TF_SENTFIN) && 625 ((tp->snd_max - tp->snd_una) == 1) && 626 (rsm->r_flags & BBR_HAS_FIN)) { 627 /* 628 * We don't start a bbr rack timer if all we have is 629 * a FIN outstanding. 630 */ 631 goto activate_rxt; 632 } 633 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK); 634 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm); 635 idx = rsm->r_rtr_cnt - 1; 636 exp = rsm->r_tim_lastsent[idx] + thresh; 637 if (SEQ_GEQ(exp, cts)) { 638 to = exp - cts; 639 if (to < bbr->r_ctl.rc_min_to) { 640 to = bbr->r_ctl.rc_min_to; 641 } 642 } else { 643 to = bbr->r_ctl.rc_min_to; 644 } 645 } else { 646 /* Ok we need to do a TLP not RACK */ 647 if (bbr->rc_tlp_in_progress != 0) { 648 /* 649 * The previous send was a TLP. 650 */ 651 goto activate_rxt; 652 } 653 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 654 if (rsm == NULL) { 655 /* We found no rsm to TLP with. */ 656 goto activate_rxt; 657 } 658 if (rsm->r_flags & BBR_HAS_FIN) { 659 /* If its a FIN we don't do TLP */ 660 rsm = NULL; 661 goto activate_rxt; 662 } 663 time_since_sent = 0; 664 idx = rsm->r_rtr_cnt - 1; 665 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 666 tstmp_touse = rsm->r_tim_lastsent[idx]; 667 else 668 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 669 if (TSTMP_GT(tstmp_touse, cts)) 670 time_since_sent = cts - tstmp_touse; 671 is_tlp_timer = 1; 672 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use); 673 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts); 674 if (thresh > time_since_sent) 675 to = thresh - time_since_sent; 676 else 677 to = bbr->r_ctl.rc_min_to; 678 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 679 /* 680 * If the TLP time works out to larger than the max 681 * RTO lets not do TLP.. just RTO. 682 */ 683 goto activate_rxt; 684 } 685 if ((bbr->rc_tlp_rtx_out == 1) && 686 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) { 687 /* 688 * Second retransmit of the same TLP 689 * lets not. 690 */ 691 bbr->rc_tlp_rtx_out = 0; 692 goto activate_rxt; 693 } 694 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) { 695 /* 696 * The tail is no longer the last one I did a probe 697 * on 698 */ 699 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 700 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 701 } 702 } 703 if (is_tlp_timer == 0) { 704 BBR_STAT_INC(bbr_to_arm_rack); 705 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 706 } else { 707 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to); 708 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 709 /* 710 * We have exceeded how many times we can retran the 711 * current TLP timer, switch to the RTO timer. 712 */ 713 goto activate_rxt; 714 } else { 715 BBR_STAT_INC(bbr_to_arm_tlp); 716 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 717 } 718 } 719 return (to); 720 } 721 722 static inline int32_t 723 bbr_minseg(struct tcp_bbr *bbr) 724 { 725 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options); 726 } 727 728 static void 729 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len) 730 { 731 struct inpcb *inp = tptoinpcb(tp); 732 struct hpts_diag diag; 733 uint32_t delayed_ack = 0; 734 uint32_t left = 0; 735 uint32_t hpts_timeout; 736 uint8_t stopped; 737 int32_t delay_calc = 0; 738 uint32_t prev_delay = 0; 739 740 if (tcp_in_hpts(tp)) { 741 /* A previous call is already set up */ 742 return; 743 } 744 if ((tp->t_state == TCPS_CLOSED) || 745 (tp->t_state == TCPS_LISTEN)) { 746 return; 747 } 748 stopped = bbr->rc_tmr_stopped; 749 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 750 left = bbr->r_ctl.rc_timer_exp - cts; 751 } 752 bbr->r_ctl.rc_hpts_flags = 0; 753 bbr->r_ctl.rc_timer_exp = 0; 754 prev_delay = bbr->r_ctl.rc_last_delay_val; 755 if (bbr->r_ctl.rc_last_delay_val && 756 (slot == 0)) { 757 /* 758 * If a previous pacer delay was in place we 759 * are not coming from the output side (where 760 * we calculate a delay, more likely a timer). 761 */ 762 slot = bbr->r_ctl.rc_last_delay_val; 763 if (TSTMP_GT(cts, bbr->rc_pacer_started)) { 764 /* Compensate for time passed */ 765 delay_calc = cts - bbr->rc_pacer_started; 766 if (delay_calc <= slot) 767 slot -= delay_calc; 768 } 769 } 770 /* Do we have early to make up for by pushing out the pacing time? */ 771 if (bbr->r_agg_early_set) { 772 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2); 773 slot += bbr->r_ctl.rc_agg_early; 774 bbr->r_ctl.rc_agg_early = 0; 775 bbr->r_agg_early_set = 0; 776 } 777 /* Are we running a total debt that needs to be compensated for? */ 778 if (bbr->r_ctl.rc_hptsi_agg_delay) { 779 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) { 780 /* We nuke the delay */ 781 slot -= bbr->r_ctl.rc_hptsi_agg_delay; 782 bbr->r_ctl.rc_hptsi_agg_delay = 0; 783 } else { 784 /* We nuke some of the delay, put in a minimal 100usecs */ 785 bbr->r_ctl.rc_hptsi_agg_delay -= slot; 786 bbr->r_ctl.rc_last_delay_val = slot = 100; 787 } 788 } 789 bbr->r_ctl.rc_last_delay_val = slot; 790 hpts_timeout = bbr_timer_start(tp, bbr, cts); 791 if (tp->t_flags & TF_DELACK) { 792 if (bbr->rc_in_persist == 0) { 793 delayed_ack = bbr_delack_time; 794 } else { 795 /* 796 * We are in persists and have 797 * gotten a new data element. 798 */ 799 if (hpts_timeout > bbr_delack_time) { 800 /* 801 * Lets make the persists timer (which acks) 802 * be the smaller of hpts_timeout and bbr_delack_time. 803 */ 804 hpts_timeout = bbr_delack_time; 805 } 806 } 807 } 808 if (delayed_ack && 809 ((hpts_timeout == 0) || 810 (delayed_ack < hpts_timeout))) { 811 /* We need a Delayed ack timer */ 812 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 813 hpts_timeout = delayed_ack; 814 } 815 if (slot) { 816 /* Mark that we have a pacing timer up */ 817 BBR_STAT_INC(bbr_paced_segments); 818 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 819 } 820 /* 821 * If no timers are going to run and we will fall off thfe hptsi 822 * wheel, we resort to a keep-alive timer if its configured. 823 */ 824 if ((hpts_timeout == 0) && 825 (slot == 0)) { 826 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 827 (tp->t_state <= TCPS_CLOSING)) { 828 /* 829 * Ok we have no timer (persists, rack, tlp, rxt or 830 * del-ack), we don't have segments being paced. So 831 * all that is left is the keepalive timer. 832 */ 833 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 834 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 835 } else { 836 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 837 } 838 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 839 } 840 } 841 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 842 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 843 /* 844 * RACK, TLP, persists and RXT timers all are restartable 845 * based on actions input .. i.e we received a packet (ack 846 * or sack) and that changes things (rw, or snd_una etc). 847 * Thus we can restart them with a new value. For 848 * keep-alive, delayed_ack we keep track of what was left 849 * and restart the timer with a smaller value. 850 */ 851 if (left < hpts_timeout) 852 hpts_timeout = left; 853 } 854 if (bbr->r_ctl.rc_incr_tmrs && slot && 855 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 856 /* 857 * If configured to do so, and the timer is either 858 * the TLP or RXT timer, we need to increase the timeout 859 * by the pacing time. Consider the bottleneck at my 860 * machine as an example, we are sending something 861 * to start a TLP on. The last packet won't be emitted 862 * fully until the pacing time (the bottleneck will hold 863 * the data in place). Once the packet is emitted that 864 * is when we want to start waiting for the TLP. This 865 * is most evident with hardware pacing (where the nic 866 * is holding the packet(s) before emitting). But it 867 * can also show up in the network so we do it for all 868 * cases. Technically we would take off one packet from 869 * this extra delay but this is easier and being more 870 * conservative is probably better. 871 */ 872 hpts_timeout += slot; 873 } 874 if (hpts_timeout) { 875 /* 876 * Hack alert for now we can't time-out over 2147 seconds (a 877 * bit more than 35min) 878 */ 879 if (hpts_timeout > 0x7ffffffe) 880 hpts_timeout = 0x7ffffffe; 881 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 882 } else 883 bbr->r_ctl.rc_timer_exp = 0; 884 if ((slot) && 885 (bbr->rc_use_google || 886 bbr->output_error_seen || 887 (slot <= hpts_timeout)) ) { 888 /* 889 * Tell LRO that it can queue packets while 890 * we pace. 891 */ 892 bbr->rc_tp->t_flags2 |= TF2_MBUF_QUEUE_READY; 893 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 894 (bbr->rc_cwnd_limited == 0)) { 895 /* 896 * If we are not cwnd limited and we 897 * are running a rack timer we put on 898 * the do not disturbe even for sack. 899 */ 900 tp->t_flags2 |= TF2_DONT_SACK_QUEUE; 901 } else 902 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 903 bbr->rc_pacer_started = cts; 904 905 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot), 906 __LINE__, &diag); 907 bbr->rc_timer_first = 0; 908 bbr->bbr_timer_src = frm; 909 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1); 910 bbr_log_hpts_diag(bbr, cts, &diag); 911 } else if (hpts_timeout) { 912 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout), 913 __LINE__, &diag); 914 /* 915 * We add the flag here as well if the slot is set, 916 * since hpts will call in to clear the queue first before 917 * calling the output routine (which does our timers). 918 * We don't want to set the flag if its just a timer 919 * else the arrival of data might (that causes us 920 * to send more) might get delayed. Imagine being 921 * on a keep-alive timer and a request comes in for 922 * more data. 923 */ 924 if (slot) 925 bbr->rc_pacer_started = cts; 926 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 927 (bbr->rc_cwnd_limited == 0)) { 928 /* 929 * For a rack timer, don't wake us even 930 * if a sack arrives as long as we are 931 * not cwnd limited. 932 */ 933 tp->t_flags2 |= (TF2_MBUF_QUEUE_READY | 934 TF2_DONT_SACK_QUEUE); 935 } else { 936 /* All other timers wake us up */ 937 tp->t_flags2 &= ~(TF2_MBUF_QUEUE_READY | 938 TF2_DONT_SACK_QUEUE); 939 } 940 bbr->bbr_timer_src = frm; 941 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0); 942 bbr_log_hpts_diag(bbr, cts, &diag); 943 bbr->rc_timer_first = 1; 944 } 945 bbr->rc_tmr_stopped = 0; 946 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay); 947 } 948 949 static void 950 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb) 951 { 952 /* 953 * We received an ack, and then did not call send or were bounced 954 * out due to the hpts was running. Now a timer is up as well, is it 955 * the right timer? 956 */ 957 struct inpcb *inp; 958 struct bbr_sendmap *rsm; 959 uint32_t hpts_timeout; 960 int tmr_up; 961 962 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 963 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 964 return; 965 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 966 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 967 (tmr_up == PACE_TMR_RXT)) { 968 /* Should be an RXT */ 969 return; 970 } 971 inp = bbr->rc_inp; 972 if (rsm == NULL) { 973 /* Nothing outstanding? */ 974 if (tp->t_flags & TF_DELACK) { 975 if (tmr_up == PACE_TMR_DELACK) 976 /* 977 * We are supposed to have delayed ack up 978 * and we do 979 */ 980 return; 981 } else if (sbavail(&inp->inp_socket->so_snd) && 982 (tmr_up == PACE_TMR_RXT)) { 983 /* 984 * if we hit enobufs then we would expect the 985 * possibility of nothing outstanding and the RXT up 986 * (and the hptsi timer). 987 */ 988 return; 989 } else if (((V_tcp_always_keepalive || 990 inp->inp_socket->so_options & SO_KEEPALIVE) && 991 (tp->t_state <= TCPS_CLOSING)) && 992 (tmr_up == PACE_TMR_KEEP) && 993 (tp->snd_max == tp->snd_una)) { 994 /* We should have keep alive up and we do */ 995 return; 996 } 997 } 998 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) { 999 if ((tp->t_flags & TF_SENTFIN) && 1000 ((tp->snd_max - tp->snd_una) == 1) && 1001 (rsm->r_flags & BBR_HAS_FIN)) { 1002 /* needs to be a RXT */ 1003 if (tmr_up == PACE_TMR_RXT) 1004 return; 1005 else 1006 goto wrong_timer; 1007 } else if (tmr_up == PACE_TMR_RACK) 1008 return; 1009 else 1010 goto wrong_timer; 1011 } else if (rsm && (tmr_up == PACE_TMR_RACK)) { 1012 /* Rack timer has priority if we have data out */ 1013 return; 1014 } else if (SEQ_GT(tp->snd_max, tp->snd_una) && 1015 ((tmr_up == PACE_TMR_TLP) || 1016 (tmr_up == PACE_TMR_RXT))) { 1017 /* 1018 * Either a TLP or RXT is fine if no sack-passed is in place 1019 * and data is outstanding. 1020 */ 1021 return; 1022 } else if (tmr_up == PACE_TMR_DELACK) { 1023 /* 1024 * If the delayed ack was going to go off before the 1025 * rtx/tlp/rack timer were going to expire, then that would 1026 * be the timer in control. Note we don't check the time 1027 * here trusting the code is correct. 1028 */ 1029 return; 1030 } 1031 if (SEQ_GT(tp->snd_max, tp->snd_una) && 1032 ((tmr_up == PACE_TMR_RXT) || 1033 (tmr_up == PACE_TMR_TLP) || 1034 (tmr_up == PACE_TMR_RACK))) { 1035 /* 1036 * We have outstanding data and 1037 * we *do* have a RACK, TLP or RXT 1038 * timer running. We won't restart 1039 * anything here since thats probably ok we 1040 * will get called with some timer here shortly. 1041 */ 1042 return; 1043 } 1044 /* 1045 * Ok the timer originally started is not what we want now. We will 1046 * force the hpts to be stopped if any, and restart with the slot 1047 * set to what was in the saved slot. 1048 */ 1049 wrong_timer: 1050 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) { 1051 if (tcp_in_hpts(tp)) 1052 tcp_hpts_remove(tp); 1053 bbr_timer_cancel(bbr, __LINE__, cts); 1054 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val, 1055 0); 1056 } else { 1057 /* 1058 * Output is hptsi so we just need to switch the type of 1059 * timer. We don't bother with keep-alive, since when we 1060 * jump through the output, it will start the keep-alive if 1061 * nothing is sent. 1062 * 1063 * We only need a delayed-ack added and or the hpts_timeout. 1064 */ 1065 hpts_timeout = bbr_timer_start(tp, bbr, cts); 1066 if (tp->t_flags & TF_DELACK) { 1067 if (hpts_timeout == 0) { 1068 hpts_timeout = bbr_delack_time; 1069 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1070 } 1071 else if (hpts_timeout > bbr_delack_time) { 1072 hpts_timeout = bbr_delack_time; 1073 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1074 } 1075 } 1076 if (hpts_timeout) { 1077 if (hpts_timeout > 0x7ffffffe) 1078 hpts_timeout = 0x7ffffffe; 1079 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 1080 } 1081 } 1082 } 1083 1084 int32_t bbr_clear_lost = 0; 1085 1086 /* 1087 * Considers the two time values now (cts) and earlier. 1088 * If cts is smaller than earlier, we could have 1089 * had a sequence wrap (our counter wraps every 1090 * 70 min or so) or it could be just clock skew 1091 * getting us two different time values. Clock skew 1092 * will show up within 10ms or so. So in such 1093 * a case (where cts is behind earlier time by 1094 * less than 10ms) we return 0. Otherwise we 1095 * return the true difference between them. 1096 */ 1097 static inline uint32_t 1098 bbr_calc_time(uint32_t cts, uint32_t earlier_time) { 1099 /* 1100 * Given two timestamps, the current time stamp cts, and some other 1101 * time-stamp taken in theory earlier return the difference. The 1102 * trick is here sometimes locking will get the other timestamp 1103 * after the cts. If this occurs we need to return 0. 1104 */ 1105 if (TSTMP_GEQ(cts, earlier_time)) 1106 return (cts - earlier_time); 1107 /* 1108 * cts is behind earlier_time if its less than 10ms consider it 0. 1109 * If its more than 10ms difference then we had a time wrap. Else 1110 * its just the normal locking foo. I wonder if we should not go to 1111 * 64bit TS and get rid of this issue. 1112 */ 1113 if (TSTMP_GEQ((cts + 10000), earlier_time)) 1114 return (0); 1115 /* 1116 * Ok the time must have wrapped. So we need to answer a large 1117 * amount of time, which the normal subtraction should do. 1118 */ 1119 return (cts - earlier_time); 1120 } 1121 1122 static int 1123 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS) 1124 { 1125 uint32_t stat; 1126 int32_t error; 1127 1128 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t)); 1129 if (error || req->newptr == NULL) 1130 return error; 1131 1132 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 1133 if (error) 1134 return (error); 1135 if (stat == 1) { 1136 #ifdef BBR_INVARIANTS 1137 printf("Clearing BBR lost counters\n"); 1138 #endif 1139 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT); 1140 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT); 1141 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT); 1142 } else if (stat == 2) { 1143 #ifdef BBR_INVARIANTS 1144 printf("Clearing BBR option counters\n"); 1145 #endif 1146 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE); 1147 } else if (stat == 3) { 1148 #ifdef BBR_INVARIANTS 1149 printf("Clearing BBR stats counters\n"); 1150 #endif 1151 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE); 1152 } else if (stat == 4) { 1153 #ifdef BBR_INVARIANTS 1154 printf("Clearing BBR out-size counters\n"); 1155 #endif 1156 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE); 1157 } 1158 bbr_clear_lost = 0; 1159 return (0); 1160 } 1161 1162 static void 1163 bbr_init_sysctls(void) 1164 { 1165 struct sysctl_oid *bbr_probertt; 1166 struct sysctl_oid *bbr_hptsi; 1167 struct sysctl_oid *bbr_measure; 1168 struct sysctl_oid *bbr_cwnd; 1169 struct sysctl_oid *bbr_timeout; 1170 struct sysctl_oid *bbr_states; 1171 struct sysctl_oid *bbr_startup; 1172 struct sysctl_oid *bbr_policer; 1173 1174 /* Probe rtt controls */ 1175 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1176 SYSCTL_CHILDREN(bbr_sysctl_root), 1177 OID_AUTO, 1178 "probertt", 1179 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1180 ""); 1181 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1182 SYSCTL_CHILDREN(bbr_probertt), 1183 OID_AUTO, "gain", CTLFLAG_RW, 1184 &bbr_rttprobe_gain, 192, 1185 "What is the filter gain drop in probe_rtt (0=disable)?"); 1186 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1187 SYSCTL_CHILDREN(bbr_probertt), 1188 OID_AUTO, "cwnd", CTLFLAG_RW, 1189 &bbr_rtt_probe_cwndtarg, 4, 1190 "How many mss's are outstanding during probe-rtt"); 1191 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1192 SYSCTL_CHILDREN(bbr_probertt), 1193 OID_AUTO, "int", CTLFLAG_RW, 1194 &bbr_rtt_probe_limit, 4000000, 1195 "If RTT has not shrank in this many micro-seconds enter probe-rtt"); 1196 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1197 SYSCTL_CHILDREN(bbr_probertt), 1198 OID_AUTO, "mintime", CTLFLAG_RW, 1199 &bbr_rtt_probe_time, 200000, 1200 "How many microseconds in probe-rtt"); 1201 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1202 SYSCTL_CHILDREN(bbr_probertt), 1203 OID_AUTO, "filter_len_sec", CTLFLAG_RW, 1204 &bbr_filter_len_sec, 6, 1205 "How long in seconds does the rttProp filter run?"); 1206 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1207 SYSCTL_CHILDREN(bbr_probertt), 1208 OID_AUTO, "drain_rtt", CTLFLAG_RW, 1209 &bbr_drain_rtt, BBR_SRTT, 1210 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?"); 1211 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1212 SYSCTL_CHILDREN(bbr_probertt), 1213 OID_AUTO, "can_force", CTLFLAG_RW, 1214 &bbr_can_force_probertt, 0, 1215 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??"); 1216 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1217 SYSCTL_CHILDREN(bbr_probertt), 1218 OID_AUTO, "enter_sets_force", CTLFLAG_RW, 1219 &bbr_probertt_sets_rtt, 0, 1220 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?"); 1221 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1222 SYSCTL_CHILDREN(bbr_probertt), 1223 OID_AUTO, "can_adjust", CTLFLAG_RW, 1224 &bbr_can_adjust_probertt, 1, 1225 "Can we dynamically adjust the probe-rtt limits and times?"); 1226 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1227 SYSCTL_CHILDREN(bbr_probertt), 1228 OID_AUTO, "is_ratio", CTLFLAG_RW, 1229 &bbr_is_ratio, 0, 1230 "is the limit to filter a ratio?"); 1231 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1232 SYSCTL_CHILDREN(bbr_probertt), 1233 OID_AUTO, "use_cwnd", CTLFLAG_RW, 1234 &bbr_prtt_slam_cwnd, 0, 1235 "Should we set/recover cwnd?"); 1236 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1237 SYSCTL_CHILDREN(bbr_probertt), 1238 OID_AUTO, "can_use_ts", CTLFLAG_RW, 1239 &bbr_can_use_ts_for_rtt, 1, 1240 "Can we use the ms timestamp if available for retransmistted rtt calculations?"); 1241 1242 /* Pacing controls */ 1243 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1244 SYSCTL_CHILDREN(bbr_sysctl_root), 1245 OID_AUTO, 1246 "pacing", 1247 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1248 ""); 1249 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1250 SYSCTL_CHILDREN(bbr_hptsi), 1251 OID_AUTO, "hw_pacing", CTLFLAG_RW, 1252 &bbr_allow_hdwr_pacing, 1, 1253 "Do we allow hardware pacing?"); 1254 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1255 SYSCTL_CHILDREN(bbr_hptsi), 1256 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW, 1257 &bbr_hardware_pacing_limit, 4000, 1258 "Do we have a limited number of connections for pacing chelsio (0=no limit)?"); 1259 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1260 SYSCTL_CHILDREN(bbr_hptsi), 1261 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW, 1262 &bbr_hdwr_pace_adjust, 2, 1263 "Multiplier to calculated tso size?"); 1264 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1265 SYSCTL_CHILDREN(bbr_hptsi), 1266 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW, 1267 &bbr_hdwr_pace_floor, 1, 1268 "Do we invoke the hardware pacing floor?"); 1269 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1270 SYSCTL_CHILDREN(bbr_hptsi), 1271 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW, 1272 &bbr_hdwr_pacing_delay_cnt, 10, 1273 "How many packets must be sent after hdwr pacing is enabled"); 1274 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1275 SYSCTL_CHILDREN(bbr_hptsi), 1276 OID_AUTO, "bw_cross", CTLFLAG_RW, 1277 &bbr_cross_over, 3000000, 1278 "What is the point where we cross over to linux like TSO size set"); 1279 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1280 SYSCTL_CHILDREN(bbr_hptsi), 1281 OID_AUTO, "seg_deltarg", CTLFLAG_RW, 1282 &bbr_hptsi_segments_delay_tar, 7000, 1283 "What is the worse case delay target for hptsi < 48Mbp connections"); 1284 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1285 SYSCTL_CHILDREN(bbr_hptsi), 1286 OID_AUTO, "enet_oh", CTLFLAG_RW, 1287 &bbr_include_enet_oh, 0, 1288 "Do we include the ethernet overhead in calculating pacing delay?"); 1289 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1290 SYSCTL_CHILDREN(bbr_hptsi), 1291 OID_AUTO, "ip_oh", CTLFLAG_RW, 1292 &bbr_include_ip_oh, 1, 1293 "Do we include the IP overhead in calculating pacing delay?"); 1294 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1295 SYSCTL_CHILDREN(bbr_hptsi), 1296 OID_AUTO, "tcp_oh", CTLFLAG_RW, 1297 &bbr_include_tcp_oh, 0, 1298 "Do we include the TCP overhead in calculating pacing delay?"); 1299 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1300 SYSCTL_CHILDREN(bbr_hptsi), 1301 OID_AUTO, "google_discount", CTLFLAG_RW, 1302 &bbr_google_discount, 10, 1303 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?"); 1304 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1305 SYSCTL_CHILDREN(bbr_hptsi), 1306 OID_AUTO, "all_get_min", CTLFLAG_RW, 1307 &bbr_all_get_min, 0, 1308 "If you are less than a MSS do you just get the min?"); 1309 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1310 SYSCTL_CHILDREN(bbr_hptsi), 1311 OID_AUTO, "tso_min", CTLFLAG_RW, 1312 &bbr_hptsi_bytes_min, 1460, 1313 "For 0 -> 24Mbps what is floor number of segments for TSO"); 1314 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1315 SYSCTL_CHILDREN(bbr_hptsi), 1316 OID_AUTO, "seg_tso_max", CTLFLAG_RW, 1317 &bbr_hptsi_segments_max, 6, 1318 "For 0 -> 24Mbps what is top number of segments for TSO"); 1319 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1320 SYSCTL_CHILDREN(bbr_hptsi), 1321 OID_AUTO, "seg_floor", CTLFLAG_RW, 1322 &bbr_hptsi_segments_floor, 1, 1323 "Minimum TSO size we will fall too in segments"); 1324 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1325 SYSCTL_CHILDREN(bbr_hptsi), 1326 OID_AUTO, "utter_max", CTLFLAG_RW, 1327 &bbr_hptsi_utter_max, 0, 1328 "The absolute maximum that any pacing (outside of hardware) can be"); 1329 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1330 SYSCTL_CHILDREN(bbr_hptsi), 1331 OID_AUTO, "seg_divisor", CTLFLAG_RW, 1332 &bbr_hptsi_per_second, 100, 1333 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps "); 1334 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1335 SYSCTL_CHILDREN(bbr_hptsi), 1336 OID_AUTO, "srtt_mul", CTLFLAG_RW, 1337 &bbr_hptsi_max_mul, 1, 1338 "The multiplier for pace len max"); 1339 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1340 SYSCTL_CHILDREN(bbr_hptsi), 1341 OID_AUTO, "srtt_div", CTLFLAG_RW, 1342 &bbr_hptsi_max_div, 2, 1343 "The divisor for pace len max"); 1344 /* Measurement controls */ 1345 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1346 SYSCTL_CHILDREN(bbr_sysctl_root), 1347 OID_AUTO, 1348 "measure", 1349 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1350 "Measurement controls"); 1351 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1352 SYSCTL_CHILDREN(bbr_measure), 1353 OID_AUTO, "min_i_bw", CTLFLAG_RW, 1354 &bbr_initial_bw_bps, 62500, 1355 "Minimum initial b/w in bytes per second"); 1356 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1357 SYSCTL_CHILDREN(bbr_measure), 1358 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1359 &bbr_sack_not_required, 0, 1360 "Do we allow bbr to run on connections not supporting SACK?"); 1361 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1362 SYSCTL_CHILDREN(bbr_measure), 1363 OID_AUTO, "use_google", CTLFLAG_RW, 1364 &bbr_use_google_algo, 0, 1365 "Use has close to google V1.0 has possible?"); 1366 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1367 SYSCTL_CHILDREN(bbr_measure), 1368 OID_AUTO, "ts_limiting", CTLFLAG_RW, 1369 &bbr_ts_limiting, 1, 1370 "Do we attempt to use the peers timestamp to limit b/w caculations?"); 1371 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1372 SYSCTL_CHILDREN(bbr_measure), 1373 OID_AUTO, "ts_can_raise", CTLFLAG_RW, 1374 &bbr_ts_can_raise, 0, 1375 "Can we raise the b/w via timestamp b/w calculation?"); 1376 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1377 SYSCTL_CHILDREN(bbr_measure), 1378 OID_AUTO, "ts_delta", CTLFLAG_RW, 1379 &bbr_min_usec_delta, 20000, 1380 "How long in usec between ts of our sends in ts validation code?"); 1381 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1382 SYSCTL_CHILDREN(bbr_measure), 1383 OID_AUTO, "ts_peer_delta", CTLFLAG_RW, 1384 &bbr_min_peer_delta, 20, 1385 "What min numerical value should be between the peer deltas?"); 1386 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1387 SYSCTL_CHILDREN(bbr_measure), 1388 OID_AUTO, "ts_delta_percent", CTLFLAG_RW, 1389 &bbr_delta_percent, 150, 1390 "What percentage (150 = 15.0) do we allow variance for?"); 1391 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1392 SYSCTL_CHILDREN(bbr_measure), 1393 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW, 1394 &bbr_min_measurements_req, 1, 1395 "What is the minimum measurement count we need before we switch to our b/w estimate"); 1396 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1397 SYSCTL_CHILDREN(bbr_measure), 1398 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW, 1399 &bbr_no_pacing_until, 4, 1400 "How many pkt-epoch's (0 is off) do we need before pacing is on?"); 1401 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1402 SYSCTL_CHILDREN(bbr_measure), 1403 OID_AUTO, "quanta", CTLFLAG_RW, 1404 &bbr_quanta, 2, 1405 "Extra quanta to add when calculating the target (ID section 4.2.3.2)."); 1406 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1407 SYSCTL_CHILDREN(bbr_measure), 1408 OID_AUTO, "noretran", CTLFLAG_RW, 1409 &bbr_no_retran, 0, 1410 "Should google mode not use retransmission measurements for the b/w estimation?"); 1411 /* State controls */ 1412 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1413 SYSCTL_CHILDREN(bbr_sysctl_root), 1414 OID_AUTO, 1415 "states", 1416 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1417 "State controls"); 1418 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1419 SYSCTL_CHILDREN(bbr_states), 1420 OID_AUTO, "idle_restart", CTLFLAG_RW, 1421 &bbr_uses_idle_restart, 0, 1422 "Do we use a new special idle_restart state to ramp back up quickly?"); 1423 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1424 SYSCTL_CHILDREN(bbr_states), 1425 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW, 1426 &bbr_idle_restart_threshold, 100000, 1427 "How long must we be idle before we restart??"); 1428 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1429 SYSCTL_CHILDREN(bbr_states), 1430 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW, 1431 &bbr_state_is_pkt_epoch, 0, 1432 "Do we use a pkt-epoch for substate if 0 rttProp?"); 1433 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1434 SYSCTL_CHILDREN(bbr_states), 1435 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW, 1436 &bbr_rtt_gain_thresh, 0, 1437 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?"); 1438 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1439 SYSCTL_CHILDREN(bbr_states), 1440 OID_AUTO, "drain_floor", CTLFLAG_RW, 1441 &bbr_drain_floor, 88, 1442 "What is the lowest we can drain (pg) too?"); 1443 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1444 SYSCTL_CHILDREN(bbr_states), 1445 OID_AUTO, "drain_2_target", CTLFLAG_RW, 1446 &bbr_state_drain_2_tar, 1, 1447 "Do we drain to target in drain substate?"); 1448 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1449 SYSCTL_CHILDREN(bbr_states), 1450 OID_AUTO, "gain_2_target", CTLFLAG_RW, 1451 &bbr_gain_to_target, 1, 1452 "Does probe bw gain to target??"); 1453 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1454 SYSCTL_CHILDREN(bbr_states), 1455 OID_AUTO, "gain_extra_time", CTLFLAG_RW, 1456 &bbr_gain_gets_extra_too, 1, 1457 "Does probe bw gain get the extra time too?"); 1458 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1459 SYSCTL_CHILDREN(bbr_states), 1460 OID_AUTO, "ld_div", CTLFLAG_RW, 1461 &bbr_drain_drop_div, 5, 1462 "Long drain drop divider?"); 1463 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1464 SYSCTL_CHILDREN(bbr_states), 1465 OID_AUTO, "ld_mul", CTLFLAG_RW, 1466 &bbr_drain_drop_mul, 4, 1467 "Long drain drop multiplier?"); 1468 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1469 SYSCTL_CHILDREN(bbr_states), 1470 OID_AUTO, "rand_ot_disc", CTLFLAG_RW, 1471 &bbr_rand_ot, 50, 1472 "Random discount of the ot?"); 1473 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1474 SYSCTL_CHILDREN(bbr_states), 1475 OID_AUTO, "dr_filter_life", CTLFLAG_RW, 1476 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT, 1477 "How many packet-epochs does the b/w delivery rate last?"); 1478 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1479 SYSCTL_CHILDREN(bbr_states), 1480 OID_AUTO, "subdrain_applimited", CTLFLAG_RW, 1481 &bbr_sub_drain_app_limit, 0, 1482 "Does our sub-state drain invoke app limited if its long?"); 1483 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1484 SYSCTL_CHILDREN(bbr_states), 1485 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW, 1486 &bbr_sub_drain_slam_cwnd, 0, 1487 "Should we set/recover cwnd for sub-state drain?"); 1488 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1489 SYSCTL_CHILDREN(bbr_states), 1490 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW, 1491 &bbr_slam_cwnd_in_main_drain, 0, 1492 "Should we set/recover cwnd for main-state drain?"); 1493 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1494 SYSCTL_CHILDREN(bbr_states), 1495 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW, 1496 &google_allow_early_out, 1, 1497 "Should we allow google probe-bw/drain to exit early at flight target?"); 1498 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1499 SYSCTL_CHILDREN(bbr_states), 1500 OID_AUTO, "google_exit_loss", CTLFLAG_RW, 1501 &google_consider_lost, 1, 1502 "Should we have losses exit gain of probebw in google mode??"); 1503 /* Startup controls */ 1504 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1505 SYSCTL_CHILDREN(bbr_sysctl_root), 1506 OID_AUTO, 1507 "startup", 1508 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1509 "Startup controls"); 1510 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1511 SYSCTL_CHILDREN(bbr_startup), 1512 OID_AUTO, "cheat_iwnd", CTLFLAG_RW, 1513 &bbr_sends_full_iwnd, 1, 1514 "Do we not pace but burst out initial windows has our TSO size?"); 1515 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1516 SYSCTL_CHILDREN(bbr_startup), 1517 OID_AUTO, "loss_threshold", CTLFLAG_RW, 1518 &bbr_startup_loss_thresh, 2000, 1519 "In startup what is the loss threshold in a pe that will exit us from startup?"); 1520 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1521 SYSCTL_CHILDREN(bbr_startup), 1522 OID_AUTO, "use_lowerpg", CTLFLAG_RW, 1523 &bbr_use_lower_gain_in_startup, 1, 1524 "Should we use a lower hptsi gain if we see loss in startup?"); 1525 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1526 SYSCTL_CHILDREN(bbr_startup), 1527 OID_AUTO, "gain", CTLFLAG_RW, 1528 &bbr_start_exit, 25, 1529 "What gain percent do we need to see to stay in startup??"); 1530 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1531 SYSCTL_CHILDREN(bbr_startup), 1532 OID_AUTO, "low_gain", CTLFLAG_RW, 1533 &bbr_low_start_exit, 15, 1534 "What gain percent do we need to see to stay in the lower gain startup??"); 1535 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1536 SYSCTL_CHILDREN(bbr_startup), 1537 OID_AUTO, "loss_exit", CTLFLAG_RW, 1538 &bbr_exit_startup_at_loss, 1, 1539 "Should we exit startup at loss in an epoch if we are not gaining?"); 1540 /* CWND controls */ 1541 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1542 SYSCTL_CHILDREN(bbr_sysctl_root), 1543 OID_AUTO, 1544 "cwnd", 1545 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1546 "Cwnd controls"); 1547 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1548 SYSCTL_CHILDREN(bbr_cwnd), 1549 OID_AUTO, "tar_rtt", CTLFLAG_RW, 1550 &bbr_cwndtarget_rtt_touse, 0, 1551 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?"); 1552 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1553 SYSCTL_CHILDREN(bbr_cwnd), 1554 OID_AUTO, "may_shrink", CTLFLAG_RW, 1555 &bbr_cwnd_may_shrink, 0, 1556 "Can the cwnd shrink if it would grow to more than the target?"); 1557 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1558 SYSCTL_CHILDREN(bbr_cwnd), 1559 OID_AUTO, "max_target_limit", CTLFLAG_RW, 1560 &bbr_target_cwnd_mult_limit, 8, 1561 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?"); 1562 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1563 SYSCTL_CHILDREN(bbr_cwnd), 1564 OID_AUTO, "highspeed_min", CTLFLAG_RW, 1565 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS, 1566 "What is the high-speed min cwnd (rttProp under 1ms)"); 1567 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1568 SYSCTL_CHILDREN(bbr_cwnd), 1569 OID_AUTO, "lowspeed_min", CTLFLAG_RW, 1570 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS, 1571 "What is the min cwnd (rttProp > 1ms)"); 1572 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1573 SYSCTL_CHILDREN(bbr_cwnd), 1574 OID_AUTO, "initwin", CTLFLAG_RW, 1575 &bbr_def_init_win, 10, 1576 "What is the BBR initial window, if 0 use tcp version"); 1577 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1578 SYSCTL_CHILDREN(bbr_cwnd), 1579 OID_AUTO, "do_loss_red", CTLFLAG_RW, 1580 &bbr_do_red, 600, 1581 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?"); 1582 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1583 SYSCTL_CHILDREN(bbr_cwnd), 1584 OID_AUTO, "red_scale", CTLFLAG_RW, 1585 &bbr_red_scale, 20000, 1586 "What RTT do we scale with?"); 1587 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1588 SYSCTL_CHILDREN(bbr_cwnd), 1589 OID_AUTO, "red_growslow", CTLFLAG_RW, 1590 &bbr_red_growth_restrict, 1, 1591 "Do we restrict cwnd growth for whats in flight?"); 1592 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1593 SYSCTL_CHILDREN(bbr_cwnd), 1594 OID_AUTO, "red_div", CTLFLAG_RW, 1595 &bbr_red_div, 2, 1596 "If we reduce whats the divisor?"); 1597 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1598 SYSCTL_CHILDREN(bbr_cwnd), 1599 OID_AUTO, "red_mul", CTLFLAG_RW, 1600 &bbr_red_mul, 1, 1601 "If we reduce whats the mulitiplier?"); 1602 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1603 SYSCTL_CHILDREN(bbr_cwnd), 1604 OID_AUTO, "target_is_unit", CTLFLAG_RW, 1605 &bbr_target_is_bbunit, 0, 1606 "Is the state target the pacing_gain or BBR_UNIT?"); 1607 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1608 SYSCTL_CHILDREN(bbr_cwnd), 1609 OID_AUTO, "drop_limit", CTLFLAG_RW, 1610 &bbr_drop_limit, 0, 1611 "Number of segments limit for drop (0=use min_cwnd w/flight)?"); 1612 1613 /* Timeout controls */ 1614 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1615 SYSCTL_CHILDREN(bbr_sysctl_root), 1616 OID_AUTO, 1617 "timeout", 1618 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1619 "Time out controls"); 1620 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1621 SYSCTL_CHILDREN(bbr_timeout), 1622 OID_AUTO, "delack", CTLFLAG_RW, 1623 &bbr_delack_time, 100000, 1624 "BBR's delayed ack time"); 1625 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1626 SYSCTL_CHILDREN(bbr_timeout), 1627 OID_AUTO, "tlp_uses", CTLFLAG_RW, 1628 &bbr_tlp_type_to_use, 3, 1629 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt"); 1630 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1631 SYSCTL_CHILDREN(bbr_timeout), 1632 OID_AUTO, "persmin", CTLFLAG_RW, 1633 &bbr_persist_min, 250000, 1634 "What is the minimum time in microseconds between persists"); 1635 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1636 SYSCTL_CHILDREN(bbr_timeout), 1637 OID_AUTO, "persmax", CTLFLAG_RW, 1638 &bbr_persist_max, 1000000, 1639 "What is the largest delay in microseconds between persists"); 1640 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1641 SYSCTL_CHILDREN(bbr_timeout), 1642 OID_AUTO, "tlp_minto", CTLFLAG_RW, 1643 &bbr_tlp_min, 10000, 1644 "TLP Min timeout in usecs"); 1645 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1646 SYSCTL_CHILDREN(bbr_timeout), 1647 OID_AUTO, "tlp_dack_time", CTLFLAG_RW, 1648 &bbr_delayed_ack_time, 200000, 1649 "TLP delayed ack compensation value"); 1650 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1651 SYSCTL_CHILDREN(bbr_sysctl_root), 1652 OID_AUTO, "minrto", CTLFLAG_RW, 1653 &bbr_rto_min_ms, 30, 1654 "Minimum RTO in ms"); 1655 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1656 SYSCTL_CHILDREN(bbr_timeout), 1657 OID_AUTO, "maxrto", CTLFLAG_RW, 1658 &bbr_rto_max_sec, 4, 1659 "Maximum RTO in seconds -- should be at least as large as min_rto"); 1660 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1661 SYSCTL_CHILDREN(bbr_timeout), 1662 OID_AUTO, "tlp_retry", CTLFLAG_RW, 1663 &bbr_tlp_max_resend, 2, 1664 "How many times does TLP retry a single segment or multiple with no ACK"); 1665 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1666 SYSCTL_CHILDREN(bbr_timeout), 1667 OID_AUTO, "minto", CTLFLAG_RW, 1668 &bbr_min_to, 1000, 1669 "Minimum rack timeout in useconds"); 1670 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1671 SYSCTL_CHILDREN(bbr_timeout), 1672 OID_AUTO, "pktdelay", CTLFLAG_RW, 1673 &bbr_pkt_delay, 1000, 1674 "Extra RACK time (in useconds) besides reordering thresh"); 1675 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1676 SYSCTL_CHILDREN(bbr_timeout), 1677 OID_AUTO, "incr_tmrs", CTLFLAG_RW, 1678 &bbr_incr_timers, 1, 1679 "Increase the RXT/TLP timer by the pacing time used?"); 1680 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1681 SYSCTL_CHILDREN(bbr_timeout), 1682 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW, 1683 &bbr_marks_rxt_sack_passed, 0, 1684 "Mark sack passed on all those not ack'd when a RXT hits?"); 1685 /* Policer controls */ 1686 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1687 SYSCTL_CHILDREN(bbr_sysctl_root), 1688 OID_AUTO, 1689 "policer", 1690 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1691 "Policer controls"); 1692 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1693 SYSCTL_CHILDREN(bbr_policer), 1694 OID_AUTO, "detect_enable", CTLFLAG_RW, 1695 &bbr_policer_detection_enabled, 1, 1696 "Is policer detection enabled??"); 1697 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1698 SYSCTL_CHILDREN(bbr_policer), 1699 OID_AUTO, "min_pes", CTLFLAG_RW, 1700 &bbr_lt_intvl_min_rtts, 4, 1701 "Minimum number of PE's?"); 1702 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1703 SYSCTL_CHILDREN(bbr_policer), 1704 OID_AUTO, "bwdiff", CTLFLAG_RW, 1705 &bbr_lt_bw_diff, (4000/8), 1706 "Minimal bw diff?"); 1707 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1708 SYSCTL_CHILDREN(bbr_policer), 1709 OID_AUTO, "bwratio", CTLFLAG_RW, 1710 &bbr_lt_bw_ratio, 8, 1711 "Minimal bw diff?"); 1712 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1713 SYSCTL_CHILDREN(bbr_policer), 1714 OID_AUTO, "from_rack_rxt", CTLFLAG_RW, 1715 &bbr_policer_call_from_rack_to, 0, 1716 "Do we call the policer detection code from a rack-timeout?"); 1717 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1718 SYSCTL_CHILDREN(bbr_policer), 1719 OID_AUTO, "false_postive", CTLFLAG_RW, 1720 &bbr_lt_intvl_fp, 0, 1721 "What packet epoch do we do false-positive detection at (0=no)?"); 1722 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1723 SYSCTL_CHILDREN(bbr_policer), 1724 OID_AUTO, "loss_thresh", CTLFLAG_RW, 1725 &bbr_lt_loss_thresh, 196, 1726 "Loss threshold 196 = 19.6%?"); 1727 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1728 SYSCTL_CHILDREN(bbr_policer), 1729 OID_AUTO, "false_postive_thresh", CTLFLAG_RW, 1730 &bbr_lt_fd_thresh, 100, 1731 "What percentage is the false detection threshold (150=15.0)?"); 1732 /* All the rest */ 1733 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1734 SYSCTL_CHILDREN(bbr_sysctl_root), 1735 OID_AUTO, "cheat_rxt", CTLFLAG_RW, 1736 &bbr_use_rack_resend_cheat, 0, 1737 "Do we burst 1ms between sends on retransmissions (like rack)?"); 1738 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1739 SYSCTL_CHILDREN(bbr_sysctl_root), 1740 OID_AUTO, "error_paceout", CTLFLAG_RW, 1741 &bbr_error_base_paceout, 10000, 1742 "When we hit an error what is the min to pace out in usec's?"); 1743 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1744 SYSCTL_CHILDREN(bbr_sysctl_root), 1745 OID_AUTO, "kill_paceout", CTLFLAG_RW, 1746 &bbr_max_net_error_cnt, 10, 1747 "When we hit this many errors in a row, kill the session?"); 1748 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1749 SYSCTL_CHILDREN(bbr_sysctl_root), 1750 OID_AUTO, "data_after_close", CTLFLAG_RW, 1751 &bbr_ignore_data_after_close, 1, 1752 "Do we hold off sending a RST until all pending data is ack'd"); 1753 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1754 SYSCTL_CHILDREN(bbr_sysctl_root), 1755 OID_AUTO, "resend_use_tso", CTLFLAG_RW, 1756 &bbr_resends_use_tso, 0, 1757 "Can resends use TSO?"); 1758 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1759 SYSCTL_CHILDREN(bbr_sysctl_root), 1760 OID_AUTO, "sblklimit", CTLFLAG_RW, 1761 &bbr_sack_block_limit, 128, 1762 "When do we start ignoring small sack blocks"); 1763 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1764 SYSCTL_CHILDREN(bbr_sysctl_root), 1765 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1766 &bbr_verbose_logging, 0, 1767 "Should BBR black box logging be verbose"); 1768 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1769 SYSCTL_CHILDREN(bbr_sysctl_root), 1770 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1771 &bbr_reorder_thresh, 2, 1772 "What factor for rack will be added when seeing reordering (shift right)"); 1773 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1774 SYSCTL_CHILDREN(bbr_sysctl_root), 1775 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1776 &bbr_reorder_fade, 0, 1777 "Does reorder detection fade, if so how many ms (0 means never)"); 1778 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1779 SYSCTL_CHILDREN(bbr_sysctl_root), 1780 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1781 &bbr_tlp_thresh, 1, 1782 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1783 /* Stats and counters */ 1784 /* The pacing counters for hdwr/software can't be in the array */ 1785 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1786 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1787 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1788 SYSCTL_CHILDREN(bbr_sysctl_root), 1789 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD, 1790 &bbr_hdwr_pacing_enobuf, 1791 "Total number of enobufs for hardware paced flows"); 1792 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1793 SYSCTL_CHILDREN(bbr_sysctl_root), 1794 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD, 1795 &bbr_nohdwr_pacing_enobuf, 1796 "Total number of enobufs for non-hardware paced flows"); 1797 1798 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK); 1799 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1800 SYSCTL_CHILDREN(bbr_sysctl_root), 1801 OID_AUTO, "hdwr_pacing", CTLFLAG_RD, 1802 &bbr_flows_whdwr_pacing, 1803 "Total number of hardware paced flows"); 1804 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK); 1805 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1806 SYSCTL_CHILDREN(bbr_sysctl_root), 1807 OID_AUTO, "software_pacing", CTLFLAG_RD, 1808 &bbr_flows_nohdwr_pacing, 1809 "Total number of software paced flows"); 1810 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK); 1811 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1812 OID_AUTO, "stats", CTLFLAG_RD, 1813 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats"); 1814 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK); 1815 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1816 OID_AUTO, "opts", CTLFLAG_RD, 1817 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats"); 1818 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK); 1819 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1820 OID_AUTO, "lost", CTLFLAG_RD, 1821 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur"); 1822 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK); 1823 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1824 OID_AUTO, "stateresend", CTLFLAG_RD, 1825 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend"); 1826 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK); 1827 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1828 OID_AUTO, "statetime", CTLFLAG_RD, 1829 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states"); 1830 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1831 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1832 OID_AUTO, "outsize", CTLFLAG_RD, 1833 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls"); 1834 SYSCTL_ADD_PROC(&bbr_sysctl_ctx, 1835 SYSCTL_CHILDREN(bbr_sysctl_root), 1836 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1837 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters"); 1838 } 1839 1840 static void 1841 bbr_counter_destroy(void) 1842 { 1843 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE); 1844 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE); 1845 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE); 1846 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT); 1847 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT); 1848 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT); 1849 counter_u64_free(bbr_nohdwr_pacing_enobuf); 1850 counter_u64_free(bbr_hdwr_pacing_enobuf); 1851 counter_u64_free(bbr_flows_whdwr_pacing); 1852 counter_u64_free(bbr_flows_nohdwr_pacing); 1853 1854 } 1855 1856 static __inline void 1857 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts) 1858 { 1859 memset(l, 0, sizeof(union tcp_log_stackspecific)); 1860 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate; 1861 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate); 1862 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 1863 l->bw_inuse = bbr_get_bw(bbr); 1864 l->inflight = ctf_flight_size(bbr->rc_tp, 1865 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 1866 l->applimited = bbr->r_ctl.r_app_limited_until; 1867 l->delivered = bbr->r_ctl.rc_delivered; 1868 l->timeStamp = cts; 1869 l->lost = bbr->r_ctl.rc_lost; 1870 l->bbr_state = bbr->rc_bbr_state; 1871 l->bbr_substate = bbr_state_val(bbr); 1872 l->epoch = bbr->r_ctl.rc_rtt_epoch; 1873 l->lt_epoch = bbr->r_ctl.rc_lt_epoch; 1874 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain; 1875 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain; 1876 l->inhpts = tcp_in_hpts(bbr->rc_tp); 1877 l->use_lt_bw = bbr->rc_lt_use_bw; 1878 l->pkts_out = bbr->r_ctl.rc_flight_at_input; 1879 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch; 1880 } 1881 1882 static void 1883 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason) 1884 { 1885 if (tcp_bblogging_on(bbr->rc_tp)) { 1886 union tcp_log_stackspecific log; 1887 1888 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1889 log.u_bbr.flex1 = 0; 1890 log.u_bbr.flex2 = 0; 1891 log.u_bbr.flex5 = 0; 1892 log.u_bbr.flex3 = 0; 1893 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate; 1894 log.u_bbr.flex7 = reason; 1895 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt; 1896 log.u_bbr.flex8 = 0; 1897 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1898 &bbr->rc_inp->inp_socket->so_rcv, 1899 &bbr->rc_inp->inp_socket->so_snd, 1900 BBR_LOG_BW_RED_EV, 0, 1901 0, &log, false, &bbr->rc_tv); 1902 } 1903 } 1904 1905 static void 1906 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count) 1907 { 1908 if (tcp_bblogging_on(bbr->rc_tp)) { 1909 union tcp_log_stackspecific log; 1910 1911 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1912 log.u_bbr.flex1 = seq; 1913 log.u_bbr.flex2 = count; 1914 log.u_bbr.flex8 = mode; 1915 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1916 &bbr->rc_inp->inp_socket->so_rcv, 1917 &bbr->rc_inp->inp_socket->so_snd, 1918 BBR_LOG_LOWGAIN, 0, 1919 0, &log, false, &bbr->rc_tv); 1920 } 1921 } 1922 1923 static void 1924 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling, 1925 uint8_t reason, uint32_t p_maxseg, int len) 1926 { 1927 if (tcp_bblogging_on(bbr->rc_tp)) { 1928 union tcp_log_stackspecific log; 1929 1930 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1931 log.u_bbr.flex1 = p_maxseg; 1932 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags; 1933 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 1934 log.u_bbr.flex4 = reason; 1935 log.u_bbr.flex5 = bbr->rc_in_persist; 1936 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val; 1937 log.u_bbr.flex7 = p_maxseg; 1938 log.u_bbr.flex8 = bbr->rc_in_persist; 1939 log.u_bbr.pkts_out = 0; 1940 log.u_bbr.applimited = len; 1941 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1942 &bbr->rc_inp->inp_socket->so_rcv, 1943 &bbr->rc_inp->inp_socket->so_snd, 1944 BBR_LOG_JUSTRET, 0, 1945 tlen, &log, false, &bbr->rc_tv); 1946 } 1947 } 1948 1949 static void 1950 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq) 1951 { 1952 if (tcp_bblogging_on(bbr->rc_tp)) { 1953 union tcp_log_stackspecific log; 1954 1955 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1956 log.u_bbr.flex1 = seq; 1957 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 1958 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start; 1959 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1960 &bbr->rc_inp->inp_socket->so_rcv, 1961 &bbr->rc_inp->inp_socket->so_snd, 1962 BBR_LOG_ENTREC, 0, 1963 0, &log, false, &bbr->rc_tv); 1964 } 1965 } 1966 1967 static void 1968 bbr_log_msgsize_fail(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t len, uint32_t maxseg, uint32_t mtu, int32_t csum_flags, int32_t tso, uint32_t cts) 1969 { 1970 if (tcp_bblogging_on(tp)) { 1971 union tcp_log_stackspecific log; 1972 1973 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1974 log.u_bbr.flex1 = tso; 1975 log.u_bbr.flex2 = maxseg; 1976 log.u_bbr.flex3 = mtu; 1977 log.u_bbr.flex4 = csum_flags; 1978 TCP_LOG_EVENTP(tp, NULL, 1979 &bbr->rc_inp->inp_socket->so_rcv, 1980 &bbr->rc_inp->inp_socket->so_snd, 1981 BBR_LOG_MSGSIZE, 0, 1982 0, &log, false, &bbr->rc_tv); 1983 } 1984 } 1985 1986 static void 1987 bbr_log_flowend(struct tcp_bbr *bbr) 1988 { 1989 if (tcp_bblogging_on(bbr->rc_tp)) { 1990 union tcp_log_stackspecific log; 1991 struct sockbuf *r, *s; 1992 struct timeval tv; 1993 1994 if (bbr->rc_inp->inp_socket) { 1995 r = &bbr->rc_inp->inp_socket->so_rcv; 1996 s = &bbr->rc_inp->inp_socket->so_snd; 1997 } else { 1998 r = s = NULL; 1999 } 2000 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv)); 2001 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2002 r, s, 2003 TCP_LOG_FLOWEND, 0, 2004 0, &log, false, &tv); 2005 } 2006 } 2007 2008 static void 2009 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, 2010 uint32_t lost, uint32_t del) 2011 { 2012 if (tcp_bblogging_on(bbr->rc_tp)) { 2013 union tcp_log_stackspecific log; 2014 2015 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2016 log.u_bbr.flex1 = lost; 2017 log.u_bbr.flex2 = del; 2018 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw; 2019 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt; 2020 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2021 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2022 log.u_bbr.flex7 = line; 2023 log.u_bbr.flex8 = 0; 2024 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count; 2025 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2026 &bbr->rc_inp->inp_socket->so_rcv, 2027 &bbr->rc_inp->inp_socket->so_snd, 2028 BBR_LOG_PKT_EPOCH, 0, 2029 0, &log, false, &bbr->rc_tv); 2030 } 2031 } 2032 2033 static void 2034 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time) 2035 { 2036 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2037 union tcp_log_stackspecific log; 2038 2039 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2040 log.u_bbr.flex1 = bbr->r_ctl.rc_lost; 2041 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat; 2042 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat; 2043 log.u_bbr.flex7 = line; 2044 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2045 &bbr->rc_inp->inp_socket->so_rcv, 2046 &bbr->rc_inp->inp_socket->so_snd, 2047 BBR_LOG_TIME_EPOCH, 0, 2048 0, &log, false, &bbr->rc_tv); 2049 } 2050 } 2051 2052 static void 2053 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth) 2054 { 2055 if (tcp_bblogging_on(bbr->rc_tp)) { 2056 union tcp_log_stackspecific log; 2057 2058 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2059 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2060 log.u_bbr.flex2 = new_tar; 2061 log.u_bbr.flex3 = line; 2062 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2063 log.u_bbr.flex5 = bbr_quanta; 2064 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs; 2065 log.u_bbr.flex7 = bbr->rc_last_options; 2066 log.u_bbr.flex8 = meth; 2067 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2068 &bbr->rc_inp->inp_socket->so_rcv, 2069 &bbr->rc_inp->inp_socket->so_snd, 2070 BBR_LOG_STATE_TARGET, 0, 2071 0, &log, false, &bbr->rc_tv); 2072 } 2073 2074 } 2075 2076 static void 2077 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2078 { 2079 if (tcp_bblogging_on(bbr->rc_tp)) { 2080 union tcp_log_stackspecific log; 2081 2082 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2083 log.u_bbr.flex1 = line; 2084 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2085 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2086 if (bbr_state_is_pkt_epoch) 2087 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 2088 else 2089 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP); 2090 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2091 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2092 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000); 2093 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra; 2094 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state; 2095 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2096 &bbr->rc_inp->inp_socket->so_rcv, 2097 &bbr->rc_inp->inp_socket->so_snd, 2098 BBR_LOG_STATE, 0, 2099 0, &log, false, &bbr->rc_tv); 2100 } 2101 } 2102 2103 static void 2104 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 2105 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond) 2106 { 2107 if (tcp_bblogging_on(bbr->rc_tp)) { 2108 union tcp_log_stackspecific log; 2109 2110 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2111 log.u_bbr.flex1 = line; 2112 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2113 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt; 2114 log.u_bbr.flex4 = applied; 2115 log.u_bbr.flex5 = rtt; 2116 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2117 log.u_bbr.flex7 = cond; 2118 log.u_bbr.flex8 = reas; 2119 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2120 &bbr->rc_inp->inp_socket->so_rcv, 2121 &bbr->rc_inp->inp_socket->so_snd, 2122 BBR_LOG_RTT_SHRINKS, 0, 2123 0, &log, false, &bbr->rc_tv); 2124 } 2125 } 2126 2127 static void 2128 bbr_log_type_exit_rec(struct tcp_bbr *bbr) 2129 { 2130 if (tcp_bblogging_on(bbr->rc_tp)) { 2131 union tcp_log_stackspecific log; 2132 2133 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2134 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start; 2135 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 2136 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2137 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2138 &bbr->rc_inp->inp_socket->so_rcv, 2139 &bbr->rc_inp->inp_socket->so_snd, 2140 BBR_LOG_EXITREC, 0, 2141 0, &log, false, &bbr->rc_tv); 2142 } 2143 } 2144 2145 static void 2146 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg, 2147 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line) 2148 { 2149 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2150 union tcp_log_stackspecific log; 2151 2152 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2153 log.u_bbr.flex1 = line; 2154 log.u_bbr.flex2 = prev_acked; 2155 log.u_bbr.flex3 = bytes_this_ack; 2156 log.u_bbr.flex4 = chg; 2157 log.u_bbr.flex5 = th_ack; 2158 log.u_bbr.flex6 = target; 2159 log.u_bbr.flex8 = meth; 2160 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2161 &bbr->rc_inp->inp_socket->so_rcv, 2162 &bbr->rc_inp->inp_socket->so_snd, 2163 BBR_LOG_CWND, 0, 2164 0, &log, false, &bbr->rc_tv); 2165 } 2166 } 2167 2168 static void 2169 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin) 2170 { 2171 /* 2172 * Log the rtt sample we are applying to the srtt algorithm in 2173 * useconds. 2174 */ 2175 if (tcp_bblogging_on(bbr->rc_tp)) { 2176 union tcp_log_stackspecific log; 2177 2178 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2179 log.u_bbr.flex1 = rtt; 2180 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time; 2181 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay; 2182 log.u_bbr.flex4 = bbr->rc_tp->ts_offset; 2183 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2184 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv); 2185 log.u_bbr.flex6 = tsin; 2186 log.u_bbr.flex7 = 0; 2187 log.u_bbr.flex8 = bbr->rc_ack_was_delayed; 2188 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2189 &bbr->rc_inp->inp_socket->so_rcv, 2190 &bbr->rc_inp->inp_socket->so_snd, 2191 TCP_LOG_RTT, 0, 2192 0, &log, false, &bbr->rc_tv); 2193 } 2194 } 2195 2196 static void 2197 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit) 2198 { 2199 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2200 union tcp_log_stackspecific log; 2201 2202 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2203 log.u_bbr.flex1 = time_in; 2204 log.u_bbr.flex2 = line; 2205 log.u_bbr.flex8 = enter_exit; 2206 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2207 &bbr->rc_inp->inp_socket->so_rcv, 2208 &bbr->rc_inp->inp_socket->so_snd, 2209 BBR_LOG_PERSIST, 0, 2210 0, &log, false, &bbr->rc_tv); 2211 } 2212 } 2213 static void 2214 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts) 2215 { 2216 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2217 union tcp_log_stackspecific log; 2218 2219 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2220 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age; 2221 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2222 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2223 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time; 2224 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2225 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2226 &bbr->rc_inp->inp_socket->so_rcv, 2227 &bbr->rc_inp->inp_socket->so_snd, 2228 BBR_LOG_ACKCLEAR, 0, 2229 0, &log, false, &bbr->rc_tv); 2230 } 2231 } 2232 2233 static void 2234 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen, 2235 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m) 2236 { 2237 if (tcp_bblogging_on(bbr->rc_tp)) { 2238 union tcp_log_stackspecific log; 2239 struct timeval tv; 2240 2241 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2242 log.u_bbr.flex1 = nsegs; 2243 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes; 2244 if (m) { 2245 struct timespec ts; 2246 2247 log.u_bbr.flex3 = m->m_flags; 2248 if (m->m_flags & M_TSTMP) { 2249 mbuf_tstmp2timespec(m, &ts); 2250 tv.tv_sec = ts.tv_sec; 2251 tv.tv_usec = ts.tv_nsec / 1000; 2252 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv); 2253 } else { 2254 log.u_bbr.lt_epoch = 0; 2255 } 2256 if (m->m_flags & M_TSTMP_LRO) { 2257 mbuf_tstmp2timeval(m, &tv); 2258 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv); 2259 } else { 2260 /* No arrival timestamp */ 2261 log.u_bbr.flex5 = 0; 2262 } 2263 2264 log.u_bbr.pkts_out = tcp_get_usecs(&tv); 2265 } else { 2266 log.u_bbr.flex3 = 0; 2267 log.u_bbr.flex5 = 0; 2268 log.u_bbr.flex6 = 0; 2269 log.u_bbr.pkts_out = 0; 2270 } 2271 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state; 2272 log.u_bbr.flex7 = bbr->r_wanted_output; 2273 log.u_bbr.flex8 = bbr->rc_in_persist; 2274 TCP_LOG_EVENTP(bbr->rc_tp, th, 2275 &bbr->rc_inp->inp_socket->so_rcv, 2276 &bbr->rc_inp->inp_socket->so_snd, 2277 TCP_LOG_IN, 0, 2278 tlen, &log, true, &bbr->rc_tv); 2279 } 2280 } 2281 2282 static void 2283 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out) 2284 { 2285 if (tcp_bblogging_on(bbr->rc_tp)) { 2286 union tcp_log_stackspecific log; 2287 2288 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2289 log.u_bbr.flex1 = did_out; 2290 log.u_bbr.flex2 = nxt_pkt; 2291 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val; 2292 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2293 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp; 2294 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes; 2295 log.u_bbr.flex7 = bbr->r_wanted_output; 2296 log.u_bbr.flex8 = bbr->rc_in_persist; 2297 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay; 2298 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2299 &bbr->rc_inp->inp_socket->so_rcv, 2300 &bbr->rc_inp->inp_socket->so_snd, 2301 BBR_LOG_DOSEG_DONE, 0, 2302 0, &log, true, &bbr->rc_tv); 2303 } 2304 } 2305 2306 static void 2307 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts, 2308 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz) 2309 { 2310 if (tcp_bblogging_on(bbr->rc_tp)) { 2311 union tcp_log_stackspecific log; 2312 2313 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2314 log.u_bbr.flex1 = line; 2315 log.u_bbr.flex2 = o_len; 2316 log.u_bbr.flex3 = segcnt; 2317 log.u_bbr.flex4 = segsiz; 2318 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2319 &bbr->rc_inp->inp_socket->so_rcv, 2320 &bbr->rc_inp->inp_socket->so_snd, 2321 BBR_LOG_ENOBUF_JMP, ENOBUFS, 2322 len, &log, true, &bbr->rc_tv); 2323 } 2324 } 2325 2326 static void 2327 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling) 2328 { 2329 if (tcp_bblogging_on(bbr->rc_tp)) { 2330 union tcp_log_stackspecific log; 2331 2332 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2333 log.u_bbr.flex1 = timers; 2334 log.u_bbr.flex2 = ret; 2335 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 2336 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2337 log.u_bbr.flex5 = cts; 2338 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2339 log.u_bbr.flex8 = hpts_calling; 2340 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2341 &bbr->rc_inp->inp_socket->so_rcv, 2342 &bbr->rc_inp->inp_socket->so_snd, 2343 BBR_LOG_TO_PROCESS, 0, 2344 0, &log, false, &bbr->rc_tv); 2345 } 2346 } 2347 2348 static void 2349 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num) 2350 { 2351 if (tcp_bblogging_on(bbr->rc_tp)) { 2352 union tcp_log_stackspecific log; 2353 uint64_t ar; 2354 2355 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2356 log.u_bbr.flex1 = bbr->bbr_timer_src; 2357 log.u_bbr.flex2 = 0; 2358 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2359 ar = (uintptr_t)(bbr->r_ctl.rc_resend); 2360 ar >>= 32; 2361 ar &= 0x00000000ffffffff; 2362 log.u_bbr.flex4 = (uint32_t)ar; 2363 ar = (uintptr_t)bbr->r_ctl.rc_resend; 2364 ar &= 0x00000000ffffffff; 2365 log.u_bbr.flex5 = (uint32_t)ar; 2366 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2367 log.u_bbr.flex8 = to_num; 2368 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2369 &bbr->rc_inp->inp_socket->so_rcv, 2370 &bbr->rc_inp->inp_socket->so_snd, 2371 BBR_LOG_RTO, 0, 2372 0, &log, false, &bbr->rc_tv); 2373 } 2374 } 2375 2376 static void 2377 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason) 2378 { 2379 if (tcp_bblogging_on(bbr->rc_tp)) { 2380 union tcp_log_stackspecific log; 2381 2382 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2383 log.u_bbr.flex1 = flex1; 2384 log.u_bbr.flex2 = flex2; 2385 log.u_bbr.flex3 = flex3; 2386 log.u_bbr.flex4 = 0; 2387 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2388 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2389 log.u_bbr.flex8 = reason; 2390 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw; 2391 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2392 &bbr->rc_inp->inp_socket->so_rcv, 2393 &bbr->rc_inp->inp_socket->so_snd, 2394 BBR_LOG_REDUCE, 0, 2395 0, &log, false, &bbr->rc_tv); 2396 } 2397 } 2398 2399 static void 2400 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag) 2401 { 2402 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2403 union tcp_log_stackspecific log; 2404 2405 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2406 log.u_bbr.flex1 = diag->p_nxt_slot; 2407 log.u_bbr.flex2 = diag->p_cur_slot; 2408 log.u_bbr.flex3 = diag->slot_req; 2409 log.u_bbr.flex4 = diag->inp_hptsslot; 2410 log.u_bbr.flex5 = diag->slot_remaining; 2411 log.u_bbr.flex6 = diag->need_new_to; 2412 log.u_bbr.flex7 = diag->p_hpts_active; 2413 log.u_bbr.flex8 = diag->p_on_min_sleep; 2414 /* Hijack other fields as needed */ 2415 log.u_bbr.epoch = diag->have_slept; 2416 log.u_bbr.lt_epoch = diag->yet_to_sleep; 2417 log.u_bbr.pkts_out = diag->co_ret; 2418 log.u_bbr.applimited = diag->hpts_sleep_time; 2419 log.u_bbr.delivered = diag->p_prev_slot; 2420 log.u_bbr.inflight = diag->p_runningslot; 2421 log.u_bbr.bw_inuse = diag->wheel_slot; 2422 log.u_bbr.rttProp = diag->wheel_cts; 2423 log.u_bbr.delRate = diag->maxslots; 2424 log.u_bbr.cur_del_rate = diag->p_curtick; 2425 log.u_bbr.cur_del_rate <<= 32; 2426 log.u_bbr.cur_del_rate |= diag->p_lasttick; 2427 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2428 &bbr->rc_inp->inp_socket->so_rcv, 2429 &bbr->rc_inp->inp_socket->so_snd, 2430 BBR_LOG_HPTSDIAG, 0, 2431 0, &log, false, &bbr->rc_tv); 2432 } 2433 } 2434 2435 static void 2436 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt, 2437 uint32_t thresh, uint32_t to) 2438 { 2439 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2440 union tcp_log_stackspecific log; 2441 2442 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2443 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar; 2444 log.u_bbr.flex2 = time_since_sent; 2445 log.u_bbr.flex3 = srtt; 2446 log.u_bbr.flex4 = thresh; 2447 log.u_bbr.flex5 = to; 2448 log.u_bbr.flex6 = bbr->rc_tp->t_srtt; 2449 log.u_bbr.flex8 = mode; 2450 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2451 &bbr->rc_inp->inp_socket->so_rcv, 2452 &bbr->rc_inp->inp_socket->so_snd, 2453 BBR_LOG_TIMERPREP, 0, 2454 0, &log, false, &bbr->rc_tv); 2455 } 2456 } 2457 2458 static void 2459 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 2460 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod) 2461 { 2462 if (tcp_bblogging_on(bbr->rc_tp)) { 2463 union tcp_log_stackspecific log; 2464 2465 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2466 log.u_bbr.flex1 = usecs; 2467 log.u_bbr.flex2 = len; 2468 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff); 2469 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff); 2470 if (override) 2471 log.u_bbr.flex5 = (1 << 2); 2472 else 2473 log.u_bbr.flex5 = 0; 2474 log.u_bbr.flex6 = override; 2475 log.u_bbr.flex7 = gain; 2476 log.u_bbr.flex8 = mod; 2477 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2478 &bbr->rc_inp->inp_socket->so_rcv, 2479 &bbr->rc_inp->inp_socket->so_snd, 2480 BBR_LOG_HPTSI_CALC, 0, 2481 len, &log, false, &bbr->rc_tv); 2482 } 2483 } 2484 2485 static void 2486 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2487 { 2488 if (tcp_bblogging_on(bbr->rc_tp)) { 2489 union tcp_log_stackspecific log; 2490 2491 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2492 2493 log.u_bbr.flex1 = bbr->bbr_timer_src; 2494 log.u_bbr.flex2 = to; 2495 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2496 log.u_bbr.flex4 = slot; 2497 log.u_bbr.flex5 = bbr->rc_tp->t_hpts_slot; 2498 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2499 log.u_bbr.pkts_out = bbr->rc_tp->t_flags2; 2500 log.u_bbr.flex8 = which; 2501 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2502 &bbr->rc_inp->inp_socket->so_rcv, 2503 &bbr->rc_inp->inp_socket->so_snd, 2504 BBR_LOG_TIMERSTAR, 0, 2505 0, &log, false, &bbr->rc_tv); 2506 } 2507 } 2508 2509 static void 2510 bbr_log_thresh_choice(struct tcp_bbr *bbr, uint32_t cts, uint32_t thresh, uint32_t lro, uint32_t srtt, struct bbr_sendmap *rsm, uint8_t frm) 2511 { 2512 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2513 union tcp_log_stackspecific log; 2514 2515 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2516 log.u_bbr.flex1 = thresh; 2517 log.u_bbr.flex2 = lro; 2518 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts; 2519 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 2520 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2521 log.u_bbr.flex6 = srtt; 2522 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift; 2523 log.u_bbr.flex8 = frm; 2524 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2525 &bbr->rc_inp->inp_socket->so_rcv, 2526 &bbr->rc_inp->inp_socket->so_snd, 2527 BBR_LOG_THRESH_CALC, 0, 2528 0, &log, false, &bbr->rc_tv); 2529 } 2530 } 2531 2532 static void 2533 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed) 2534 { 2535 if (tcp_bblogging_on(bbr->rc_tp)) { 2536 union tcp_log_stackspecific log; 2537 2538 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2539 log.u_bbr.flex1 = line; 2540 log.u_bbr.flex2 = bbr->bbr_timer_src; 2541 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2542 log.u_bbr.flex4 = bbr->rc_in_persist; 2543 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2544 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2545 log.u_bbr.flex8 = hpts_removed; 2546 log.u_bbr.pkts_out = bbr->rc_pacer_started; 2547 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2548 &bbr->rc_inp->inp_socket->so_rcv, 2549 &bbr->rc_inp->inp_socket->so_snd, 2550 BBR_LOG_TIMERCANC, 0, 2551 0, &log, false, &bbr->rc_tv); 2552 } 2553 } 2554 2555 static void 2556 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta) 2557 { 2558 if (tcp_bblogging_on(bbr->rc_tp)) { 2559 union tcp_log_stackspecific log; 2560 2561 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2562 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio; 2563 log.u_bbr.flex2 = (peer_delta >> 32); 2564 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff); 2565 log.u_bbr.flex4 = (delta >> 32); 2566 log.u_bbr.flex5 = (delta & 0x00000000ffffffff); 2567 log.u_bbr.flex7 = bbr->rc_ts_clock_set; 2568 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used; 2569 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2570 &bbr->rc_inp->inp_socket->so_rcv, 2571 &bbr->rc_inp->inp_socket->so_snd, 2572 BBR_LOG_TSTMP_VAL, 0, 2573 0, &log, false, &bbr->rc_tv); 2574 } 2575 } 2576 2577 static void 2578 bbr_log_type_tsosize(struct tcp_bbr *bbr, uint32_t cts, uint32_t tsosz, uint32_t tls, uint32_t old_val, uint32_t maxseg, int hdwr) 2579 { 2580 if (tcp_bblogging_on(bbr->rc_tp)) { 2581 union tcp_log_stackspecific log; 2582 2583 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2584 log.u_bbr.flex1 = tsosz; 2585 log.u_bbr.flex2 = tls; 2586 log.u_bbr.flex3 = tcp_min_hptsi_time; 2587 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min; 2588 log.u_bbr.flex5 = old_val; 2589 log.u_bbr.flex6 = maxseg; 2590 log.u_bbr.flex7 = bbr->rc_no_pacing; 2591 log.u_bbr.flex7 <<= 1; 2592 log.u_bbr.flex7 |= bbr->rc_past_init_win; 2593 if (hdwr) 2594 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google; 2595 else 2596 log.u_bbr.flex8 = bbr->rc_use_google; 2597 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2598 &bbr->rc_inp->inp_socket->so_rcv, 2599 &bbr->rc_inp->inp_socket->so_snd, 2600 BBR_LOG_BBRTSO, 0, 2601 0, &log, false, &bbr->rc_tv); 2602 } 2603 } 2604 2605 static void 2606 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, 2607 uint32_t flags, uint32_t line) 2608 { 2609 if (tcp_bblogging_on(bbr->rc_tp)) { 2610 union tcp_log_stackspecific log; 2611 2612 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2613 log.u_bbr.flex1 = line; 2614 log.u_bbr.flex2 = rsm->r_start; 2615 log.u_bbr.flex3 = rsm->r_end; 2616 log.u_bbr.flex4 = rsm->r_delivered; 2617 log.u_bbr.flex5 = rsm->r_rtr_cnt; 2618 log.u_bbr.flex6 = rsm->r_dupack; 2619 log.u_bbr.flex7 = rsm->r_tim_lastsent[0]; 2620 log.u_bbr.flex8 = rsm->r_flags; 2621 /* Hijack the pkts_out fids */ 2622 log.u_bbr.applimited = flags; 2623 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2624 &bbr->rc_inp->inp_socket->so_rcv, 2625 &bbr->rc_inp->inp_socket->so_snd, 2626 BBR_RSM_CLEARED, 0, 2627 0, &log, false, &bbr->rc_tv); 2628 } 2629 } 2630 2631 static void 2632 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts, 2633 uint32_t flex3, uint32_t flex2, uint32_t flex5, 2634 uint32_t flex6, uint32_t pkts_out, int flex7, 2635 uint32_t flex4, uint32_t flex1) 2636 { 2637 2638 if (tcp_bblogging_on(bbr->rc_tp)) { 2639 union tcp_log_stackspecific log; 2640 2641 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2642 log.u_bbr.flex1 = flex1; 2643 log.u_bbr.flex2 = flex2; 2644 log.u_bbr.flex3 = flex3; 2645 log.u_bbr.flex4 = flex4; 2646 log.u_bbr.flex5 = flex5; 2647 log.u_bbr.flex6 = flex6; 2648 log.u_bbr.flex7 = flex7; 2649 /* Hijack the pkts_out fids */ 2650 log.u_bbr.pkts_out = pkts_out; 2651 log.u_bbr.flex8 = flex8; 2652 if (bbr->rc_ack_was_delayed) 2653 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay; 2654 else 2655 log.u_bbr.epoch = 0; 2656 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2657 &bbr->rc_inp->inp_socket->so_rcv, 2658 &bbr->rc_inp->inp_socket->so_snd, 2659 BBR_LOG_BBRUPD, 0, 2660 flex2, &log, false, &bbr->rc_tv); 2661 } 2662 } 2663 2664 static void 2665 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason, 2666 uint32_t newbw, uint32_t obw, uint32_t diff, 2667 uint32_t tim) 2668 { 2669 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) { 2670 union tcp_log_stackspecific log; 2671 2672 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2673 log.u_bbr.flex1 = reason; 2674 log.u_bbr.flex2 = newbw; 2675 log.u_bbr.flex3 = obw; 2676 log.u_bbr.flex4 = diff; 2677 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost; 2678 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del; 2679 log.u_bbr.flex7 = bbr->rc_lt_is_sampling; 2680 log.u_bbr.pkts_out = tim; 2681 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw; 2682 if (bbr->rc_lt_use_bw == 0) 2683 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 2684 else 2685 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 2686 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2687 &bbr->rc_inp->inp_socket->so_rcv, 2688 &bbr->rc_inp->inp_socket->so_snd, 2689 BBR_LOG_BWSAMP, 0, 2690 0, &log, false, &bbr->rc_tv); 2691 } 2692 } 2693 2694 static inline void 2695 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line) 2696 { 2697 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2698 union tcp_log_stackspecific log; 2699 2700 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2701 log.u_bbr.flex1 = line; 2702 log.u_bbr.flex2 = tick; 2703 log.u_bbr.flex3 = tp->t_maxunacktime; 2704 log.u_bbr.flex4 = tp->t_acktime; 2705 log.u_bbr.flex8 = event; 2706 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2707 &bbr->rc_inp->inp_socket->so_rcv, 2708 &bbr->rc_inp->inp_socket->so_snd, 2709 BBR_LOG_PROGRESS, 0, 2710 0, &log, false, &bbr->rc_tv); 2711 } 2712 } 2713 2714 static void 2715 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp, 2716 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts, 2717 int error) 2718 { 2719 if (tcp_bblogging_on(bbr->rc_tp)) { 2720 union tcp_log_stackspecific log; 2721 uint64_t ifp64 = (uintptr_t)ifp; 2722 2723 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2724 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2725 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2726 log.u_bbr.flex3 = ((ifp64 >> 32) & 0x00000000ffffffff); 2727 log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff); 2728 log.u_bbr.bw_inuse = rate; 2729 log.u_bbr.flex5 = line; 2730 log.u_bbr.flex6 = error; 2731 log.u_bbr.flex8 = bbr->skip_gain; 2732 log.u_bbr.flex8 <<= 1; 2733 log.u_bbr.flex8 |= bbr->gain_is_limited; 2734 log.u_bbr.flex8 <<= 1; 2735 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing; 2736 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 2737 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2738 &bbr->rc_inp->inp_socket->so_rcv, 2739 &bbr->rc_inp->inp_socket->so_snd, 2740 BBR_LOG_HDWR_PACE, 0, 2741 0, &log, false, &bbr->rc_tv); 2742 } 2743 } 2744 2745 static void 2746 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, uint32_t del_by, uint32_t cts, uint32_t line, uint32_t prev_delay) 2747 { 2748 if (tcp_bblogging_on(bbr->rc_tp)) { 2749 union tcp_log_stackspecific log; 2750 2751 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2752 log.u_bbr.flex1 = slot; 2753 log.u_bbr.flex2 = del_by; 2754 log.u_bbr.flex3 = prev_delay; 2755 log.u_bbr.flex4 = line; 2756 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val; 2757 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay; 2758 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags); 2759 log.u_bbr.flex8 = bbr->rc_in_persist; 2760 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2761 &bbr->rc_inp->inp_socket->so_rcv, 2762 &bbr->rc_inp->inp_socket->so_snd, 2763 BBR_LOG_BBRSND, 0, 2764 len, &log, false, &bbr->rc_tv); 2765 } 2766 } 2767 2768 static void 2769 bbr_log_type_bbrrttprop(struct tcp_bbr *bbr, uint32_t t, uint32_t end, uint32_t tsconv, uint32_t cts, int32_t match, uint32_t seq, uint8_t flags) 2770 { 2771 if (tcp_bblogging_on(bbr->rc_tp)) { 2772 union tcp_log_stackspecific log; 2773 2774 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2775 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered; 2776 log.u_bbr.flex2 = 0; 2777 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt; 2778 log.u_bbr.flex4 = end; 2779 log.u_bbr.flex5 = seq; 2780 log.u_bbr.flex6 = t; 2781 log.u_bbr.flex7 = match; 2782 log.u_bbr.flex8 = flags; 2783 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2784 &bbr->rc_inp->inp_socket->so_rcv, 2785 &bbr->rc_inp->inp_socket->so_snd, 2786 BBR_LOG_BBRRTT, 0, 2787 0, &log, false, &bbr->rc_tv); 2788 } 2789 } 2790 2791 static void 2792 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method) 2793 { 2794 if (tcp_bblogging_on(bbr->rc_tp)) { 2795 union tcp_log_stackspecific log; 2796 2797 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2798 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2799 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 2800 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch; 2801 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2802 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs; 2803 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight; 2804 log.u_bbr.flex7 = 0; 2805 log.u_bbr.flex8 = entry_method; 2806 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2807 &bbr->rc_inp->inp_socket->so_rcv, 2808 &bbr->rc_inp->inp_socket->so_snd, 2809 BBR_LOG_EXIT_GAIN, 0, 2810 0, &log, false, &bbr->rc_tv); 2811 } 2812 } 2813 2814 static void 2815 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired) 2816 { 2817 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2818 union tcp_log_stackspecific log; 2819 2820 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2821 /* R-HU */ 2822 log.u_bbr.flex1 = 0; 2823 log.u_bbr.flex2 = 0; 2824 log.u_bbr.flex3 = 0; 2825 log.u_bbr.flex4 = 0; 2826 log.u_bbr.flex7 = 0; 2827 log.u_bbr.flex8 = settings_desired; 2828 2829 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2830 &bbr->rc_inp->inp_socket->so_rcv, 2831 &bbr->rc_inp->inp_socket->so_snd, 2832 BBR_LOG_SETTINGS_CHG, 0, 2833 0, &log, false, &bbr->rc_tv); 2834 } 2835 } 2836 2837 /* 2838 * Returns the bw from the our filter. 2839 */ 2840 static inline uint64_t 2841 bbr_get_full_bw(struct tcp_bbr *bbr) 2842 { 2843 uint64_t bw; 2844 2845 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2846 2847 return (bw); 2848 } 2849 2850 static inline void 2851 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2852 { 2853 uint64_t calclr; 2854 uint32_t lost, del; 2855 2856 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch) 2857 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch; 2858 else 2859 lost = 0; 2860 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del; 2861 if (lost == 0) { 2862 calclr = 0; 2863 } else if (del) { 2864 calclr = lost; 2865 calclr *= (uint64_t)1000; 2866 calclr /= (uint64_t)del; 2867 } else { 2868 /* Nothing delivered? 100.0% loss */ 2869 calclr = 1000; 2870 } 2871 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr; 2872 if (IN_RECOVERY(bbr->rc_tp->t_flags)) 2873 bbr->r_ctl.recovery_lr += (uint32_t)calclr; 2874 bbr->r_ctl.rc_pkt_epoch++; 2875 if (bbr->rc_no_pacing && 2876 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) { 2877 bbr->rc_no_pacing = 0; 2878 tcp_bbr_tso_size_check(bbr, cts); 2879 } 2880 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time); 2881 bbr->r_ctl.rc_pkt_epoch_time = cts; 2882 /* What was our loss rate */ 2883 bbr_log_pkt_epoch(bbr, cts, line, lost, del); 2884 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered; 2885 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost; 2886 } 2887 2888 static inline void 2889 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2890 { 2891 uint32_t epoch_time; 2892 2893 /* Tick the RTT clock */ 2894 bbr->r_ctl.rc_rtt_epoch++; 2895 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start; 2896 bbr_log_time_epoch(bbr, cts, line, epoch_time); 2897 bbr->r_ctl.rc_rcv_epoch_start = cts; 2898 } 2899 2900 static inline void 2901 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked) 2902 { 2903 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) { 2904 bbr->rc_is_pkt_epoch_now = 1; 2905 } 2906 } 2907 2908 /* 2909 * Returns the bw from either the b/w filter 2910 * or from the lt_bw (if the connection is being 2911 * policed). 2912 */ 2913 static inline uint64_t 2914 __bbr_get_bw(struct tcp_bbr *bbr) 2915 { 2916 uint64_t bw, min_bw; 2917 uint64_t rtt; 2918 int gm_measure_cnt = 1; 2919 2920 /* 2921 * For startup we make, like google, a 2922 * minimum b/w. This is generated from the 2923 * IW and the rttProp. We do fall back to srtt 2924 * if for some reason (initial handshake) we don't 2925 * have a rttProp. We, in the worst case, fall back 2926 * to the configured min_bw (rc_initial_hptsi_bw). 2927 */ 2928 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 2929 /* Attempt first to use rttProp */ 2930 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2931 if (rtt && (rtt < 0xffffffff)) { 2932 measure: 2933 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2934 ((uint64_t)1000000); 2935 min_bw /= rtt; 2936 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2937 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2938 } 2939 2940 } else if (bbr->rc_tp->t_srtt != 0) { 2941 /* No rttProp, use srtt? */ 2942 rtt = bbr_get_rtt(bbr, BBR_SRTT); 2943 goto measure; 2944 } else { 2945 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2946 } 2947 } else 2948 min_bw = 0; 2949 2950 if ((bbr->rc_past_init_win == 0) && 2951 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp))) 2952 bbr->rc_past_init_win = 1; 2953 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1)) 2954 gm_measure_cnt = 0; 2955 if (gm_measure_cnt && 2956 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) || 2957 (bbr->rc_past_init_win == 0))) { 2958 /* For google we use our guess rate until we get 1 measurement */ 2959 2960 use_initial_window: 2961 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2962 if (rtt && (rtt < 0xffffffff)) { 2963 /* 2964 * We have an RTT measurement. Use that in 2965 * combination with our initial window to calculate 2966 * a b/w. 2967 */ 2968 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2969 ((uint64_t)1000000); 2970 bw /= rtt; 2971 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2972 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2973 } 2974 } else { 2975 /* Drop back to the 40 and punt to a default */ 2976 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2977 } 2978 if (bw < 1) 2979 /* Probably should panic */ 2980 bw = 1; 2981 if (bw > min_bw) 2982 return (bw); 2983 else 2984 return (min_bw); 2985 } 2986 if (bbr->rc_lt_use_bw) 2987 bw = bbr->r_ctl.rc_lt_bw; 2988 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0)) 2989 bw = bbr->r_ctl.red_bw; 2990 else 2991 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2992 if (bw == 0) { 2993 /* We should not be at 0, go to the initial window then */ 2994 goto use_initial_window; 2995 } 2996 if (bw < 1) 2997 /* Probably should panic */ 2998 bw = 1; 2999 if (bw < min_bw) 3000 bw = min_bw; 3001 return (bw); 3002 } 3003 3004 static inline uint64_t 3005 bbr_get_bw(struct tcp_bbr *bbr) 3006 { 3007 uint64_t bw; 3008 3009 bw = __bbr_get_bw(bbr); 3010 return (bw); 3011 } 3012 3013 static inline void 3014 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts) 3015 { 3016 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch; 3017 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time; 3018 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3019 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3020 } 3021 3022 static inline void 3023 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts) 3024 { 3025 bbr->rc_lt_is_sampling = 0; 3026 bbr->rc_lt_use_bw = 0; 3027 bbr->r_ctl.rc_lt_bw = 0; 3028 bbr_reset_lt_bw_interval(bbr, cts); 3029 } 3030 3031 static inline void 3032 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin) 3033 { 3034 uint64_t diff; 3035 3036 /* Do we have a previous sample? */ 3037 if (bbr->r_ctl.rc_lt_bw) { 3038 /* Get the diff in bytes per second */ 3039 if (bbr->r_ctl.rc_lt_bw > bw) 3040 diff = bbr->r_ctl.rc_lt_bw - bw; 3041 else 3042 diff = bw - bbr->r_ctl.rc_lt_bw; 3043 if ((diff <= bbr_lt_bw_diff) || 3044 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) { 3045 /* Consider us policed */ 3046 uint32_t saved_bw; 3047 3048 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw; 3049 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */ 3050 bbr->rc_lt_use_bw = 1; 3051 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 3052 /* 3053 * Use pkt based epoch for measuring length of 3054 * policer up 3055 */ 3056 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch; 3057 /* 3058 * reason 4 is we need to start consider being 3059 * policed 3060 */ 3061 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin); 3062 return; 3063 } 3064 } 3065 bbr->r_ctl.rc_lt_bw = bw; 3066 bbr_reset_lt_bw_interval(bbr, cts); 3067 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin); 3068 } 3069 3070 static void 3071 bbr_randomize_extra_state_time(struct tcp_bbr *bbr) 3072 { 3073 uint32_t ran, deduct; 3074 3075 ran = arc4random_uniform(bbr_rand_ot); 3076 if (ran) { 3077 deduct = bbr->r_ctl.rc_level_state_extra / ran; 3078 bbr->r_ctl.rc_level_state_extra -= deduct; 3079 } 3080 } 3081 /* 3082 * Return randomly the starting state 3083 * to use in probebw. 3084 */ 3085 static uint8_t 3086 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts) 3087 { 3088 uint32_t ran; 3089 uint8_t ret_val; 3090 3091 /* Initialize the offset to 0 */ 3092 bbr->r_ctl.rc_exta_time_gd = 0; 3093 bbr->rc_hit_state_1 = 0; 3094 bbr->r_ctl.rc_level_state_extra = 0; 3095 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1)); 3096 /* 3097 * The math works funny here :) the return value is used to set the 3098 * substate and then the state change is called which increments by 3099 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when 3100 * we fully enter the state. Note that the (8 - 1 - ran) assures that 3101 * we return 1 - 7, so we dont return 0 and end up starting in 3102 * state 1 (DRAIN). 3103 */ 3104 ret_val = BBR_SUBSTATE_COUNT - 1 - ran; 3105 /* Set an epoch */ 3106 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) 3107 bbr_set_epoch(bbr, cts, __LINE__); 3108 3109 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 3110 return (ret_val); 3111 } 3112 3113 static void 3114 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected) 3115 { 3116 uint32_t diff, d_time; 3117 uint64_t del_time, bw, lost, delivered; 3118 3119 if (bbr->r_use_policer == 0) 3120 return; 3121 if (bbr->rc_lt_use_bw) { 3122 /* We are using lt bw do we stop yet? */ 3123 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 3124 if (diff > bbr_lt_bw_max_rtts) { 3125 /* Reset it all */ 3126 reset_all: 3127 bbr_reset_lt_bw_sampling(bbr, cts); 3128 if (bbr->rc_filled_pipe) { 3129 bbr_set_epoch(bbr, cts, __LINE__); 3130 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 3131 bbr_substate_change(bbr, cts, __LINE__, 0); 3132 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 3133 bbr_log_type_statechange(bbr, cts, __LINE__); 3134 } else { 3135 /* 3136 * This should not happen really 3137 * unless we remove the startup/drain 3138 * restrictions above. 3139 */ 3140 bbr->rc_bbr_state = BBR_STATE_STARTUP; 3141 bbr_set_epoch(bbr, cts, __LINE__); 3142 bbr->r_ctl.rc_bbr_state_time = cts; 3143 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 3144 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 3145 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 3146 bbr_set_state_target(bbr, __LINE__); 3147 bbr_log_type_statechange(bbr, cts, __LINE__); 3148 } 3149 /* reason 0 is to stop using lt-bw */ 3150 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0); 3151 return; 3152 } 3153 if (bbr_lt_intvl_fp == 0) { 3154 /* Not doing false-positive detection */ 3155 return; 3156 } 3157 /* False positive detection */ 3158 if (diff == bbr_lt_intvl_fp) { 3159 /* At bbr_lt_intvl_fp we record the lost */ 3160 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3161 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3162 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) { 3163 /* Now is our loss rate still high? */ 3164 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3165 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3166 if ((delivered == 0) || 3167 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) { 3168 /* No still below our threshold */ 3169 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0); 3170 } else { 3171 /* Yikes its still high, it must be a false positive */ 3172 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0); 3173 goto reset_all; 3174 } 3175 } 3176 return; 3177 } 3178 /* 3179 * Wait for the first loss before sampling, to let the policer 3180 * exhaust its tokens and estimate the steady-state rate allowed by 3181 * the policer. Starting samples earlier includes bursts that 3182 * over-estimate the bw. 3183 */ 3184 if (bbr->rc_lt_is_sampling == 0) { 3185 /* reason 1 is to begin doing the sampling */ 3186 if (loss_detected == 0) 3187 return; 3188 bbr_reset_lt_bw_interval(bbr, cts); 3189 bbr->rc_lt_is_sampling = 1; 3190 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0); 3191 return; 3192 } 3193 /* Now how long were we delivering long term last> */ 3194 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time)) 3195 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time; 3196 else 3197 d_time = 0; 3198 3199 /* To avoid underestimates, reset sampling if we run out of data. */ 3200 if (bbr->r_ctl.r_app_limited_until) { 3201 /* Can not measure in app-limited state */ 3202 bbr_reset_lt_bw_sampling(bbr, cts); 3203 /* reason 2 is to reset sampling due to app limits */ 3204 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time); 3205 return; 3206 } 3207 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 3208 if (diff < bbr_lt_intvl_min_rtts) { 3209 /* 3210 * need more samples (we don't 3211 * start on a round like linux so 3212 * we need 1 more). 3213 */ 3214 /* 6 is not_enough time or no-loss */ 3215 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3216 return; 3217 } 3218 if (diff > (4 * bbr_lt_intvl_min_rtts)) { 3219 /* 3220 * For now if we wait too long, reset all sampling. We need 3221 * to do some research here, its possible that we should 3222 * base this on how much loss as occurred.. something like 3223 * if its under 10% (or some thresh) reset all otherwise 3224 * don't. Thats for phase II I guess. 3225 */ 3226 bbr_reset_lt_bw_sampling(bbr, cts); 3227 /* reason 3 is to reset sampling due too long of sampling */ 3228 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3229 return; 3230 } 3231 /* 3232 * End sampling interval when a packet is lost, so we estimate the 3233 * policer tokens were exhausted. Stopping the sampling before the 3234 * tokens are exhausted under-estimates the policed rate. 3235 */ 3236 if (loss_detected == 0) { 3237 /* 6 is not_enough time or no-loss */ 3238 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3239 return; 3240 } 3241 /* Calculate packets lost and delivered in sampling interval. */ 3242 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3243 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3244 if ((delivered == 0) || 3245 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) { 3246 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time); 3247 return; 3248 } 3249 if (d_time < 1000) { 3250 /* Not enough time. wait */ 3251 /* 6 is not_enough time or no-loss */ 3252 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3253 return; 3254 } 3255 if (d_time >= (0xffffffff / USECS_IN_MSEC)) { 3256 /* Too long */ 3257 bbr_reset_lt_bw_sampling(bbr, cts); 3258 /* reason 3 is to reset sampling due too long of sampling */ 3259 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3260 return; 3261 } 3262 del_time = d_time; 3263 bw = delivered; 3264 bw *= (uint64_t)USECS_IN_SECOND; 3265 bw /= del_time; 3266 bbr_lt_bw_samp_done(bbr, bw, cts, d_time); 3267 } 3268 3269 /* 3270 * Allocate a sendmap from our zone. 3271 */ 3272 static struct bbr_sendmap * 3273 bbr_alloc(struct tcp_bbr *bbr) 3274 { 3275 struct bbr_sendmap *rsm; 3276 3277 BBR_STAT_INC(bbr_to_alloc); 3278 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO)); 3279 if (rsm) { 3280 bbr->r_ctl.rc_num_maps_alloced++; 3281 return (rsm); 3282 } 3283 if (bbr->r_ctl.rc_free_cnt) { 3284 BBR_STAT_INC(bbr_to_alloc_emerg); 3285 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 3286 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 3287 bbr->r_ctl.rc_free_cnt--; 3288 return (rsm); 3289 } 3290 BBR_STAT_INC(bbr_to_alloc_failed); 3291 return (NULL); 3292 } 3293 3294 static struct bbr_sendmap * 3295 bbr_alloc_full_limit(struct tcp_bbr *bbr) 3296 { 3297 if ((V_tcp_map_entries_limit > 0) && 3298 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3299 BBR_STAT_INC(bbr_alloc_limited); 3300 if (!bbr->alloc_limit_reported) { 3301 bbr->alloc_limit_reported = 1; 3302 BBR_STAT_INC(bbr_alloc_limited_conns); 3303 } 3304 return (NULL); 3305 } 3306 return (bbr_alloc(bbr)); 3307 } 3308 3309 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3310 static struct bbr_sendmap * 3311 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type) 3312 { 3313 struct bbr_sendmap *rsm; 3314 3315 if (limit_type) { 3316 /* currently there is only one limit type */ 3317 if (V_tcp_map_split_limit > 0 && 3318 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3319 BBR_STAT_INC(bbr_split_limited); 3320 if (!bbr->alloc_limit_reported) { 3321 bbr->alloc_limit_reported = 1; 3322 BBR_STAT_INC(bbr_alloc_limited_conns); 3323 } 3324 return (NULL); 3325 } 3326 } 3327 3328 /* allocate and mark in the limit type, if set */ 3329 rsm = bbr_alloc(bbr); 3330 if (rsm != NULL && limit_type) { 3331 rsm->r_limit_type = limit_type; 3332 bbr->r_ctl.rc_num_split_allocs++; 3333 } 3334 return (rsm); 3335 } 3336 3337 static void 3338 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 3339 { 3340 if (rsm->r_limit_type) { 3341 /* currently there is only one limit type */ 3342 bbr->r_ctl.rc_num_split_allocs--; 3343 } 3344 if (rsm->r_is_smallmap) 3345 bbr->r_ctl.rc_num_small_maps_alloced--; 3346 if (bbr->r_ctl.rc_tlp_send == rsm) 3347 bbr->r_ctl.rc_tlp_send = NULL; 3348 if (bbr->r_ctl.rc_resend == rsm) { 3349 bbr->r_ctl.rc_resend = NULL; 3350 } 3351 if (bbr->r_ctl.rc_next == rsm) 3352 bbr->r_ctl.rc_next = NULL; 3353 if (bbr->r_ctl.rc_sacklast == rsm) 3354 bbr->r_ctl.rc_sacklast = NULL; 3355 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 3356 memset(rsm, 0, sizeof(struct bbr_sendmap)); 3357 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 3358 rsm->r_limit_type = 0; 3359 bbr->r_ctl.rc_free_cnt++; 3360 return; 3361 } 3362 bbr->r_ctl.rc_num_maps_alloced--; 3363 uma_zfree(bbr_zone, rsm); 3364 } 3365 3366 /* 3367 * Returns the BDP. 3368 */ 3369 static uint64_t 3370 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) { 3371 /* 3372 * Calculate the bytes in flight needed given the bw (in bytes per 3373 * second) and the specifyed rtt in useconds. We need to put out the 3374 * returned value per RTT to match that rate. Gain will normally 3375 * raise it up from there. 3376 * 3377 * This should not overflow as long as the bandwidth is below 1 3378 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller 3379 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30). 3380 */ 3381 uint64_t usec_per_sec; 3382 3383 usec_per_sec = USECS_IN_SECOND; 3384 return ((rtt * bw) / usec_per_sec); 3385 } 3386 3387 /* 3388 * Return the initial cwnd. 3389 */ 3390 static uint32_t 3391 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp) 3392 { 3393 uint32_t i_cwnd; 3394 3395 if (bbr->rc_init_win) { 3396 i_cwnd = bbr->rc_init_win * tp->t_maxseg; 3397 } else if (V_tcp_initcwnd_segments) 3398 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg), 3399 max(2 * tp->t_maxseg, 14600)); 3400 else if (V_tcp_do_rfc3390) 3401 i_cwnd = min(4 * tp->t_maxseg, 3402 max(2 * tp->t_maxseg, 4380)); 3403 else { 3404 /* Per RFC5681 Section 3.1 */ 3405 if (tp->t_maxseg > 2190) 3406 i_cwnd = 2 * tp->t_maxseg; 3407 else if (tp->t_maxseg > 1095) 3408 i_cwnd = 3 * tp->t_maxseg; 3409 else 3410 i_cwnd = 4 * tp->t_maxseg; 3411 } 3412 return (i_cwnd); 3413 } 3414 3415 /* 3416 * Given a specified gain, return the target 3417 * cwnd based on that gain. 3418 */ 3419 static uint32_t 3420 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw) 3421 { 3422 uint64_t bdp, rtt; 3423 uint32_t cwnd; 3424 3425 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) || 3426 (bbr_get_full_bw(bbr) == 0)) { 3427 /* No measurements yet */ 3428 return (bbr_initial_cwnd(bbr, bbr->rc_tp)); 3429 } 3430 /* 3431 * Get bytes per RTT needed (rttProp is normally in 3432 * bbr_cwndtarget_rtt_touse) 3433 */ 3434 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse); 3435 /* Get the bdp from the two values */ 3436 bdp = bbr_get_bw_delay_prod(rtt, bw); 3437 /* Now apply the gain */ 3438 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT)); 3439 3440 return (cwnd); 3441 } 3442 3443 static uint32_t 3444 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain) 3445 { 3446 uint32_t cwnd, mss; 3447 3448 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 3449 /* Get the base cwnd with gain rounded to a mss */ 3450 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss); 3451 /* 3452 * Add in N (2 default since we do not have a 3453 * fq layer to trap packets in) quanta's per the I-D 3454 * section 4.2.3.2 quanta adjust. 3455 */ 3456 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs); 3457 if (bbr->rc_use_google) { 3458 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3459 (bbr_state_val(bbr) == BBR_SUB_GAIN)) { 3460 /* 3461 * The linux implementation adds 3462 * an extra 2 x mss in gain cycle which 3463 * is documented no-where except in the code. 3464 * so we add more for Neal undocumented feature 3465 */ 3466 cwnd += 2 * mss; 3467 } 3468 if ((cwnd / mss) & 0x1) { 3469 /* Round up for odd num mss */ 3470 cwnd += mss; 3471 } 3472 } 3473 /* Are we below the min cwnd? */ 3474 if (cwnd < get_min_cwnd(bbr)) 3475 return (get_min_cwnd(bbr)); 3476 return (cwnd); 3477 } 3478 3479 static uint16_t 3480 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain) 3481 { 3482 if (gain < 1) 3483 gain = 1; 3484 return (gain); 3485 } 3486 3487 static uint32_t 3488 bbr_get_header_oh(struct tcp_bbr *bbr) 3489 { 3490 int seg_oh; 3491 3492 seg_oh = 0; 3493 if (bbr->r_ctl.rc_inc_tcp_oh) { 3494 /* Do we include TCP overhead? */ 3495 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr)); 3496 } 3497 if (bbr->r_ctl.rc_inc_ip_oh) { 3498 /* Do we include IP overhead? */ 3499 #ifdef INET6 3500 if (bbr->r_is_v6) { 3501 seg_oh += sizeof(struct ip6_hdr); 3502 } else 3503 #endif 3504 { 3505 3506 #ifdef INET 3507 seg_oh += sizeof(struct ip); 3508 #endif 3509 } 3510 } 3511 if (bbr->r_ctl.rc_inc_enet_oh) { 3512 /* Do we include the ethernet overhead? */ 3513 seg_oh += sizeof(struct ether_header); 3514 } 3515 return(seg_oh); 3516 } 3517 3518 static uint32_t 3519 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw) 3520 { 3521 uint64_t divor, res, tim; 3522 3523 if (useconds_time == 0) 3524 return (0); 3525 gain = bbr_gain_adjust(bbr, gain); 3526 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT; 3527 tim = useconds_time; 3528 res = (tim * bw * gain) / divor; 3529 if (res == 0) 3530 res = 1; 3531 return ((uint32_t)res); 3532 } 3533 3534 /* 3535 * Given a gain and a length return the delay in useconds that 3536 * should be used to evenly space out packets 3537 * on the connection (based on the gain factor). 3538 */ 3539 static uint32_t 3540 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog) 3541 { 3542 uint64_t bw, lentim, res; 3543 uint32_t usecs, srtt, over = 0; 3544 uint32_t seg_oh, num_segs, maxseg; 3545 3546 if (len == 0) 3547 return (0); 3548 3549 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 3550 num_segs = (len + maxseg - 1) / maxseg; 3551 if (bbr->rc_use_google == 0) { 3552 seg_oh = bbr_get_header_oh(bbr); 3553 len += (num_segs * seg_oh); 3554 } 3555 gain = bbr_gain_adjust(bbr, gain); 3556 bw = bbr_get_bw(bbr); 3557 if (bbr->rc_use_google) { 3558 uint64_t cbw; 3559 3560 /* 3561 * Reduce the b/w by the google discount 3562 * factor 10 = 1%. 3563 */ 3564 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount); 3565 cbw /= (uint64_t)1000; 3566 /* We don't apply a discount if it results in 0 */ 3567 if (cbw > 0) 3568 bw = cbw; 3569 } 3570 lentim = ((uint64_t)len * 3571 (uint64_t)USECS_IN_SECOND * 3572 (uint64_t)BBR_UNIT); 3573 res = lentim / ((uint64_t)gain * bw); 3574 if (res == 0) 3575 res = 1; 3576 usecs = (uint32_t)res; 3577 srtt = bbr_get_rtt(bbr, BBR_SRTT); 3578 if (bbr_hptsi_max_mul && bbr_hptsi_max_div && 3579 (bbr->rc_use_google == 0) && 3580 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) { 3581 /* 3582 * We cannot let the delay be more than 1/2 the srtt time. 3583 * Otherwise we cannot pace out or send properly. 3584 */ 3585 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div; 3586 BBR_STAT_INC(bbr_hpts_min_time); 3587 } 3588 if (!nolog) 3589 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1); 3590 return (usecs); 3591 } 3592 3593 static void 3594 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack, 3595 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses) 3596 { 3597 uint64_t bw; 3598 uint32_t cwnd, target_cwnd, saved_bytes, maxseg; 3599 int32_t meth; 3600 3601 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3602 3603 #ifdef STATS 3604 if ((tp->t_flags & TF_GPUTINPROG) && 3605 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 3606 /* 3607 * Strech acks and compressed acks will cause this to 3608 * oscillate but we are doing it the same way as the main 3609 * stack so it will be compariable (though possibly not 3610 * ideal). 3611 */ 3612 int32_t cgput; 3613 int64_t gput, time_stamp; 3614 3615 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8; 3616 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000)); 3617 cgput = gput / time_stamp; 3618 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 3619 cgput); 3620 if (tp->t_stats_gput_prev > 0) 3621 stats_voi_update_abs_s32(tp->t_stats, 3622 VOI_TCP_GPUT_ND, 3623 ((gput - tp->t_stats_gput_prev) * 100) / 3624 tp->t_stats_gput_prev); 3625 tp->t_flags &= ~TF_GPUTINPROG; 3626 tp->t_stats_gput_prev = cgput; 3627 } 3628 #endif 3629 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3630 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 3631 /* We don't change anything in probe-rtt */ 3632 return; 3633 } 3634 maxseg = tp->t_maxseg - bbr->rc_last_options; 3635 saved_bytes = bytes_this_ack; 3636 bytes_this_ack += sack_changed; 3637 if (bytes_this_ack > prev_acked) { 3638 bytes_this_ack -= prev_acked; 3639 /* 3640 * A byte ack'd gives us a full mss 3641 * to be like linux i.e. they count packets. 3642 */ 3643 if ((bytes_this_ack < maxseg) && bbr->rc_use_google) 3644 bytes_this_ack = maxseg; 3645 } else { 3646 /* Unlikely */ 3647 bytes_this_ack = 0; 3648 } 3649 cwnd = tp->snd_cwnd; 3650 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3651 if (bw) 3652 target_cwnd = bbr_get_target_cwnd(bbr, 3653 bw, 3654 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain); 3655 else 3656 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp); 3657 if (IN_RECOVERY(tp->t_flags) && 3658 (bbr->bbr_prev_in_rec == 0)) { 3659 /* 3660 * We are entering recovery and 3661 * thus packet conservation. 3662 */ 3663 bbr->pkt_conservation = 1; 3664 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime; 3665 cwnd = ctf_flight_size(tp, 3666 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3667 bytes_this_ack; 3668 } 3669 if (IN_RECOVERY(tp->t_flags)) { 3670 uint32_t flight; 3671 3672 bbr->bbr_prev_in_rec = 1; 3673 if (cwnd > losses) { 3674 cwnd -= losses; 3675 if (cwnd < maxseg) 3676 cwnd = maxseg; 3677 } else 3678 cwnd = maxseg; 3679 flight = ctf_flight_size(tp, 3680 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3681 bbr_log_type_cwndupd(bbr, flight, 0, 3682 losses, 10, 0, 0, line); 3683 if (bbr->pkt_conservation) { 3684 uint32_t time_in; 3685 3686 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start)) 3687 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start; 3688 else 3689 time_in = 0; 3690 3691 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 3692 /* Clear packet conservation after an rttProp */ 3693 bbr->pkt_conservation = 0; 3694 } else { 3695 if ((flight + bytes_this_ack) > cwnd) 3696 cwnd = flight + bytes_this_ack; 3697 if (cwnd < get_min_cwnd(bbr)) 3698 cwnd = get_min_cwnd(bbr); 3699 tp->snd_cwnd = cwnd; 3700 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, 3701 prev_acked, 1, target_cwnd, th->th_ack, line); 3702 return; 3703 } 3704 } 3705 } else 3706 bbr->bbr_prev_in_rec = 0; 3707 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) { 3708 bbr->r_ctl.restrict_growth--; 3709 if (bytes_this_ack > maxseg) 3710 bytes_this_ack = maxseg; 3711 } 3712 if (bbr->rc_filled_pipe) { 3713 /* 3714 * Here we have exited startup and filled the pipe. We will 3715 * thus allow the cwnd to shrink to the target. We hit here 3716 * mostly. 3717 */ 3718 uint32_t s_cwnd; 3719 3720 meth = 2; 3721 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd); 3722 if (s_cwnd > cwnd) 3723 cwnd = s_cwnd; 3724 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing) 3725 cwnd = s_cwnd; 3726 } else { 3727 /* 3728 * Here we are still in startup, we increase cwnd by what 3729 * has been acked. 3730 */ 3731 if ((cwnd < target_cwnd) || 3732 (bbr->rc_past_init_win == 0)) { 3733 meth = 3; 3734 cwnd += bytes_this_ack; 3735 } else { 3736 /* 3737 * Method 4 means we are at target so no gain in 3738 * startup and past the initial window. 3739 */ 3740 meth = 4; 3741 } 3742 } 3743 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr)); 3744 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line); 3745 } 3746 3747 static void 3748 tcp_bbr_partialack(struct tcpcb *tp) 3749 { 3750 struct tcp_bbr *bbr; 3751 3752 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3753 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3754 if (ctf_flight_size(tp, 3755 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 3756 tp->snd_cwnd) { 3757 bbr->r_wanted_output = 1; 3758 } 3759 } 3760 3761 static void 3762 bbr_post_recovery(struct tcpcb *tp) 3763 { 3764 struct tcp_bbr *bbr; 3765 uint32_t flight; 3766 3767 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3768 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3769 /* 3770 * Here we just exit recovery. 3771 */ 3772 EXIT_RECOVERY(tp->t_flags); 3773 /* Lock in our b/w reduction for the specified number of pkt-epochs */ 3774 bbr->r_recovery_bw = 0; 3775 tp->snd_recover = tp->snd_una; 3776 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3777 bbr->pkt_conservation = 0; 3778 if (bbr->rc_use_google == 0) { 3779 /* 3780 * For non-google mode lets 3781 * go ahead and make sure we clear 3782 * the recovery state so if we 3783 * bounce back in to recovery we 3784 * will do PC. 3785 */ 3786 bbr->bbr_prev_in_rec = 0; 3787 } 3788 bbr_log_type_exit_rec(bbr); 3789 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3790 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3791 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__); 3792 } else { 3793 /* For probe-rtt case lets fix up its saved_cwnd */ 3794 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) { 3795 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent; 3796 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__); 3797 } 3798 } 3799 flight = ctf_flight_size(tp, 3800 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3801 if ((bbr->rc_use_google == 0) && 3802 bbr_do_red) { 3803 uint64_t val, lr2use; 3804 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd; 3805 uint32_t *cwnd_p; 3806 3807 if (bbr_get_rtt(bbr, BBR_SRTT)) { 3808 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000); 3809 val /= bbr_get_rtt(bbr, BBR_SRTT); 3810 ratio = (uint32_t)val; 3811 } else 3812 ratio = 1000; 3813 3814 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, 3815 bbr->r_ctl.recovery_lr, 21, 3816 ratio, 3817 bbr->r_ctl.rc_red_cwnd_pe, 3818 __LINE__); 3819 if ((ratio < bbr_do_red) || (bbr_do_red == 0)) 3820 goto done; 3821 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3822 bbr_prtt_slam_cwnd) || 3823 (bbr_sub_drain_slam_cwnd && 3824 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3825 bbr->rc_hit_state_1 && 3826 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) || 3827 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) && 3828 bbr_slam_cwnd_in_main_drain)) { 3829 /* 3830 * Here we must poke at the saved cwnd 3831 * as well as the cwnd. 3832 */ 3833 cwnd = bbr->r_ctl.rc_saved_cwnd; 3834 cwnd_p = &bbr->r_ctl.rc_saved_cwnd; 3835 } else { 3836 cwnd = tp->snd_cwnd; 3837 cwnd_p = &tp->snd_cwnd; 3838 } 3839 maxseg = tp->t_maxseg - bbr->rc_last_options; 3840 /* Add the overall lr with the recovery lr */ 3841 if (bbr->r_ctl.rc_lost == 0) 3842 lr2use = 0; 3843 else if (bbr->r_ctl.rc_delivered == 0) 3844 lr2use = 1000; 3845 else { 3846 lr2use = bbr->r_ctl.rc_lost * 1000; 3847 lr2use /= bbr->r_ctl.rc_delivered; 3848 } 3849 lr2use += bbr->r_ctl.recovery_lr; 3850 acks_inflight = (flight / (maxseg * 2)); 3851 if (bbr_red_scale) { 3852 lr2use *= bbr_get_rtt(bbr, BBR_SRTT); 3853 lr2use /= bbr_red_scale; 3854 if ((bbr_red_growth_restrict) && 3855 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1)) 3856 bbr->r_ctl.restrict_growth += acks_inflight; 3857 } 3858 if (lr2use) { 3859 val = (uint64_t)cwnd * lr2use; 3860 val /= 1000; 3861 if (cwnd > val) 3862 newcwnd = roundup((cwnd - val), maxseg); 3863 else 3864 newcwnd = maxseg; 3865 } else { 3866 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul; 3867 val /= (uint64_t)bbr_red_div; 3868 newcwnd = roundup((uint32_t)val, maxseg); 3869 } 3870 /* with standard delayed acks how many acks can I expect? */ 3871 if (bbr_drop_limit == 0) { 3872 /* 3873 * Anticpate how much we will 3874 * raise the cwnd based on the acks. 3875 */ 3876 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) { 3877 /* We do enforce the min (with the acks) */ 3878 newcwnd = (get_min_cwnd(bbr) - acks_inflight); 3879 } 3880 } else { 3881 /* 3882 * A strict drop limit of N is inplace 3883 */ 3884 if (newcwnd < (bbr_drop_limit * maxseg)) { 3885 newcwnd = bbr_drop_limit * maxseg; 3886 } 3887 } 3888 /* For the next N acks do we restrict the growth */ 3889 *cwnd_p = newcwnd; 3890 if (tp->snd_cwnd > newcwnd) 3891 tp->snd_cwnd = newcwnd; 3892 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22, 3893 (uint32_t)lr2use, 3894 bbr_get_rtt(bbr, BBR_SRTT), __LINE__); 3895 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch; 3896 } 3897 done: 3898 bbr->r_ctl.recovery_lr = 0; 3899 if (flight <= tp->snd_cwnd) { 3900 bbr->r_wanted_output = 1; 3901 } 3902 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3903 } 3904 3905 static void 3906 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts) 3907 { 3908 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3909 /* Limit the drop in b/w to 1/2 our current filter. */ 3910 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate) 3911 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate; 3912 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2)) 3913 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2; 3914 tcp_bbr_tso_size_check(bbr, cts); 3915 } 3916 3917 static void 3918 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm) 3919 { 3920 struct tcp_bbr *bbr; 3921 3922 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3923 #ifdef STATS 3924 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 3925 #endif 3926 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3927 switch (type) { 3928 case CC_NDUPACK: 3929 if (!IN_RECOVERY(tp->t_flags)) { 3930 tp->snd_recover = tp->snd_max; 3931 /* Start a new epoch */ 3932 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 3933 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) { 3934 /* 3935 * Move forward the lt epoch 3936 * so it won't count the truncated 3937 * epoch. 3938 */ 3939 bbr->r_ctl.rc_lt_epoch++; 3940 } 3941 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 3942 /* 3943 * Just like the policer detection code 3944 * if we are in startup we must push 3945 * forward the last startup epoch 3946 * to hide the truncated PE. 3947 */ 3948 bbr->r_ctl.rc_bbr_last_startup_epoch++; 3949 } 3950 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd; 3951 ENTER_RECOVERY(tp->t_flags); 3952 bbr->rc_tlp_rtx_out = 0; 3953 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate; 3954 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3955 if (tcp_in_hpts(bbr->rc_tp) && 3956 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) { 3957 /* 3958 * When we enter recovery, we need to restart 3959 * any timers. This may mean we gain an agg 3960 * early, which will be made up for at the last 3961 * rxt out. 3962 */ 3963 bbr->rc_timer_first = 1; 3964 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 3965 } 3966 /* 3967 * Calculate a new cwnd based on to the current 3968 * delivery rate with no gain. We get the bdp 3969 * without gaining it up like we normally would and 3970 * we use the last cur_del_rate. 3971 */ 3972 if ((bbr->rc_use_google == 0) && 3973 (bbr->r_ctl.bbr_rttprobe_gain_val || 3974 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) { 3975 tp->snd_cwnd = ctf_flight_size(tp, 3976 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3977 (tp->t_maxseg - bbr->rc_last_options); 3978 if (tp->snd_cwnd < get_min_cwnd(bbr)) { 3979 /* We always gate to min cwnd */ 3980 tp->snd_cwnd = get_min_cwnd(bbr); 3981 } 3982 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__); 3983 } 3984 bbr_log_type_enter_rec(bbr, rsm->r_start); 3985 } 3986 break; 3987 case CC_RTO_ERR: 3988 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 3989 /* RTO was unnecessary, so reset everything. */ 3990 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime); 3991 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3992 tp->snd_cwnd = tp->snd_cwnd_prev; 3993 tp->snd_ssthresh = tp->snd_ssthresh_prev; 3994 tp->snd_recover = tp->snd_recover_prev; 3995 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3996 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__); 3997 } 3998 tp->t_badrxtwin = 0; 3999 break; 4000 } 4001 } 4002 4003 /* 4004 * Indicate whether this ack should be delayed. We can delay the ack if 4005 * following conditions are met: 4006 * - There is no delayed ack timer in progress. 4007 * - Our last ack wasn't a 0-sized window. We never want to delay 4008 * the ack that opens up a 0-sized window. 4009 * - LRO wasn't used for this segment. We make sure by checking that the 4010 * segment size is not larger than the MSS. 4011 * - Delayed acks are enabled or this is a half-synchronized T/TCP 4012 * connection. 4013 * - The data being acked is less than a full segment (a stretch ack 4014 * of more than a segment we should ack. 4015 * - nsegs is 1 (if its more than that we received more than 1 ack). 4016 */ 4017 #define DELAY_ACK(tp, bbr, nsegs) \ 4018 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4019 ((tp->t_flags & TF_DELACK) == 0) && \ 4020 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \ 4021 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 4022 4023 /* 4024 * Return the lowest RSM in the map of 4025 * packets still in flight that is not acked. 4026 * This should normally find on the first one 4027 * since we remove packets from the send 4028 * map after they are marked ACKED. 4029 */ 4030 static struct bbr_sendmap * 4031 bbr_find_lowest_rsm(struct tcp_bbr *bbr) 4032 { 4033 struct bbr_sendmap *rsm; 4034 4035 /* 4036 * Walk the time-order transmitted list looking for an rsm that is 4037 * not acked. This will be the one that was sent the longest time 4038 * ago that is still outstanding. 4039 */ 4040 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) { 4041 if (rsm->r_flags & BBR_ACKED) { 4042 continue; 4043 } 4044 goto finish; 4045 } 4046 finish: 4047 return (rsm); 4048 } 4049 4050 static struct bbr_sendmap * 4051 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 4052 { 4053 struct bbr_sendmap *prsm; 4054 4055 /* 4056 * Walk the sequence order list backward until we hit and arrive at 4057 * the highest seq not acked. In theory when this is called it 4058 * should be the last segment (which it was not). 4059 */ 4060 prsm = rsm; 4061 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4062 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) { 4063 continue; 4064 } 4065 return (prsm); 4066 } 4067 return (NULL); 4068 } 4069 4070 /* 4071 * Returns to the caller the number of microseconds that 4072 * the packet can be outstanding before we think we 4073 * should have had an ack returned. 4074 */ 4075 static uint32_t 4076 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm) 4077 { 4078 /* 4079 * lro is the flag we use to determine if we have seen reordering. 4080 * If it gets set we have seen reordering. The reorder logic either 4081 * works in one of two ways: 4082 * 4083 * If reorder-fade is configured, then we track the last time we saw 4084 * re-ordering occur. If we reach the point where enough time as 4085 * passed we no longer consider reordering has occuring. 4086 * 4087 * Or if reorder-face is 0, then once we see reordering we consider 4088 * the connection to alway be subject to reordering and just set lro 4089 * to 1. 4090 * 4091 * In the end if lro is non-zero we add the extra time for 4092 * reordering in. 4093 */ 4094 int32_t lro; 4095 uint32_t thresh, t_rxtcur; 4096 4097 if (srtt == 0) 4098 srtt = 1; 4099 if (bbr->r_ctl.rc_reorder_ts) { 4100 if (bbr->r_ctl.rc_reorder_fade) { 4101 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) { 4102 lro = cts - bbr->r_ctl.rc_reorder_ts; 4103 if (lro == 0) { 4104 /* 4105 * No time as passed since the last 4106 * reorder, mark it as reordering. 4107 */ 4108 lro = 1; 4109 } 4110 } else { 4111 /* Negative time? */ 4112 lro = 0; 4113 } 4114 if (lro > bbr->r_ctl.rc_reorder_fade) { 4115 /* Turn off reordering seen too */ 4116 bbr->r_ctl.rc_reorder_ts = 0; 4117 lro = 0; 4118 } 4119 } else { 4120 /* Reodering does not fade */ 4121 lro = 1; 4122 } 4123 } else { 4124 lro = 0; 4125 } 4126 thresh = srtt + bbr->r_ctl.rc_pkt_delay; 4127 if (lro) { 4128 /* It must be set, if not you get 1/4 rtt */ 4129 if (bbr->r_ctl.rc_reorder_shift) 4130 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift); 4131 else 4132 thresh += (srtt >> 2); 4133 } else { 4134 thresh += 1000; 4135 } 4136 /* We don't let the rack timeout be above a RTO */ 4137 if ((bbr->rc_tp)->t_srtt == 0) 4138 t_rxtcur = BBR_INITIAL_RTO; 4139 else 4140 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 4141 if (thresh > t_rxtcur) { 4142 thresh = t_rxtcur; 4143 } 4144 /* And we don't want it above the RTO max either */ 4145 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4146 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4147 } 4148 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK); 4149 return (thresh); 4150 } 4151 4152 /* 4153 * Return to the caller the amount of time in mico-seconds 4154 * that should be used for the TLP timer from the last 4155 * send time of this packet. 4156 */ 4157 static uint32_t 4158 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 4159 struct bbr_sendmap *rsm, uint32_t srtt, 4160 uint32_t cts) 4161 { 4162 uint32_t thresh, len, maxseg, t_rxtcur; 4163 struct bbr_sendmap *prsm; 4164 4165 if (srtt == 0) 4166 srtt = 1; 4167 if (bbr->rc_tlp_threshold) 4168 thresh = srtt + (srtt / bbr->rc_tlp_threshold); 4169 else 4170 thresh = (srtt * 2); 4171 maxseg = tp->t_maxseg - bbr->rc_last_options; 4172 /* Get the previous sent packet, if any */ 4173 len = rsm->r_end - rsm->r_start; 4174 4175 /* 2.1 behavior */ 4176 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext); 4177 if (prsm && (len <= maxseg)) { 4178 /* 4179 * Two packets outstanding, thresh should be (2*srtt) + 4180 * possible inter-packet delay (if any). 4181 */ 4182 uint32_t inter_gap = 0; 4183 int idx, nidx; 4184 4185 idx = rsm->r_rtr_cnt - 1; 4186 nidx = prsm->r_rtr_cnt - 1; 4187 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 4188 /* Yes it was sent later (or at the same time) */ 4189 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 4190 } 4191 thresh += inter_gap; 4192 } else if (len <= maxseg) { 4193 /* 4194 * Possibly compensate for delayed-ack. 4195 */ 4196 uint32_t alt_thresh; 4197 4198 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time; 4199 if (alt_thresh > thresh) 4200 thresh = alt_thresh; 4201 } 4202 /* Not above the current RTO */ 4203 if (tp->t_srtt == 0) 4204 t_rxtcur = BBR_INITIAL_RTO; 4205 else 4206 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur); 4207 4208 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP); 4209 /* Not above an RTO */ 4210 if (thresh > t_rxtcur) { 4211 thresh = t_rxtcur; 4212 } 4213 /* Not above a RTO max */ 4214 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4215 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4216 } 4217 /* And now apply the user TLP min */ 4218 if (thresh < bbr_tlp_min) { 4219 thresh = bbr_tlp_min; 4220 } 4221 return (thresh); 4222 } 4223 4224 /* 4225 * Return one of three RTTs to use (in microseconds). 4226 */ 4227 static __inline uint32_t 4228 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type) 4229 { 4230 uint32_t f_rtt; 4231 uint32_t srtt; 4232 4233 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 4234 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) { 4235 /* We have no rtt at all */ 4236 if (bbr->rc_tp->t_srtt == 0) 4237 f_rtt = BBR_INITIAL_RTO; 4238 else 4239 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4240 /* 4241 * Since we don't know how good the rtt is apply a 4242 * delayed-ack min 4243 */ 4244 if (f_rtt < bbr_delayed_ack_time) { 4245 f_rtt = bbr_delayed_ack_time; 4246 } 4247 } 4248 /* Take the filter version or last measured pkt-rtt */ 4249 if (rtt_type == BBR_RTT_PROP) { 4250 srtt = f_rtt; 4251 } else if (rtt_type == BBR_RTT_PKTRTT) { 4252 if (bbr->r_ctl.rc_pkt_epoch_rtt) { 4253 srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 4254 } else { 4255 /* No pkt rtt yet */ 4256 srtt = f_rtt; 4257 } 4258 } else if (rtt_type == BBR_RTT_RACK) { 4259 srtt = bbr->r_ctl.rc_last_rtt; 4260 /* We need to add in any internal delay for our timer */ 4261 if (bbr->rc_ack_was_delayed) 4262 srtt += bbr->r_ctl.rc_ack_hdwr_delay; 4263 } else if (rtt_type == BBR_SRTT) { 4264 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4265 } else { 4266 /* TSNH */ 4267 srtt = f_rtt; 4268 #ifdef BBR_INVARIANTS 4269 panic("Unknown rtt request type %d", rtt_type); 4270 #endif 4271 } 4272 return (srtt); 4273 } 4274 4275 static int 4276 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts) 4277 { 4278 uint32_t thresh; 4279 4280 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK), 4281 cts, rsm); 4282 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) { 4283 /* It is lost (past time) */ 4284 return (1); 4285 } 4286 return (0); 4287 } 4288 4289 /* 4290 * Return a sendmap if we need to retransmit something. 4291 */ 4292 static struct bbr_sendmap * 4293 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4294 { 4295 /* 4296 * Check to see that we don't need to fall into recovery. We will 4297 * need to do so if our oldest transmit is past the time we should 4298 * have had an ack. 4299 */ 4300 4301 struct bbr_sendmap *rsm; 4302 int32_t idx; 4303 4304 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) { 4305 /* Nothing outstanding that we know of */ 4306 return (NULL); 4307 } 4308 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 4309 if (rsm == NULL) { 4310 /* Nothing in the transmit map */ 4311 return (NULL); 4312 } 4313 if (tp->t_flags & TF_SENTFIN) { 4314 /* Fin restricted, don't find anything once a fin is sent */ 4315 return (NULL); 4316 } 4317 if (rsm->r_flags & BBR_ACKED) { 4318 /* 4319 * Ok the first one is acked (this really should not happen 4320 * since we remove the from the tmap once they are acked) 4321 */ 4322 rsm = bbr_find_lowest_rsm(bbr); 4323 if (rsm == NULL) 4324 return (NULL); 4325 } 4326 idx = rsm->r_rtr_cnt - 1; 4327 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) { 4328 /* Send timestamp is the same or less? can't be ready */ 4329 return (NULL); 4330 } 4331 /* Get our RTT time */ 4332 if (bbr_is_lost(bbr, rsm, cts) && 4333 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 4334 (rsm->r_flags & BBR_SACK_PASSED))) { 4335 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4336 rsm->r_flags |= BBR_MARKED_LOST; 4337 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4338 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4339 } 4340 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm); 4341 #ifdef BBR_INVARIANTS 4342 if ((rsm->r_end - rsm->r_start) == 0) 4343 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm); 4344 #endif 4345 return (rsm); 4346 } 4347 return (NULL); 4348 } 4349 4350 /* 4351 * RACK Timer, here we simply do logging and house keeping. 4352 * the normal bbr_output_wtime() function will call the 4353 * appropriate thing to check if we need to do a RACK retransmit. 4354 * We return 1, saying don't proceed with bbr_output_wtime only 4355 * when all timers have been stopped (destroyed PCB?). 4356 */ 4357 static int 4358 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4359 { 4360 /* 4361 * This timer simply provides an internal trigger to send out data. 4362 * The check_recovery_mode call will see if there are needed 4363 * retransmissions, if so we will enter fast-recovery. The output 4364 * call may or may not do the same thing depending on sysctl 4365 * settings. 4366 */ 4367 uint32_t lost; 4368 4369 if (bbr->rc_all_timers_stopped) { 4370 return (1); 4371 } 4372 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4373 /* Its not time yet */ 4374 return (0); 4375 } 4376 BBR_STAT_INC(bbr_to_tot); 4377 lost = bbr->r_ctl.rc_lost; 4378 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4379 bbr_set_state(tp, bbr, 0); 4380 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK); 4381 if (bbr->r_ctl.rc_resend == NULL) { 4382 /* Lets do the check here */ 4383 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 4384 } 4385 if (bbr_policer_call_from_rack_to) 4386 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4387 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 4388 return (0); 4389 } 4390 4391 static __inline void 4392 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start) 4393 { 4394 int idx; 4395 4396 nrsm->r_start = start; 4397 nrsm->r_end = rsm->r_end; 4398 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4399 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed; 4400 nrsm->r_flags = rsm->r_flags; 4401 /* We don't transfer forward the SYN flag */ 4402 nrsm->r_flags &= ~BBR_HAS_SYN; 4403 /* We move forward the FIN flag, not that this should happen */ 4404 rsm->r_flags &= ~BBR_HAS_FIN; 4405 nrsm->r_dupack = rsm->r_dupack; 4406 nrsm->r_rtr_bytes = 0; 4407 nrsm->r_is_gain = rsm->r_is_gain; 4408 nrsm->r_is_drain = rsm->r_is_drain; 4409 nrsm->r_delivered = rsm->r_delivered; 4410 nrsm->r_ts_valid = rsm->r_ts_valid; 4411 nrsm->r_del_ack_ts = rsm->r_del_ack_ts; 4412 nrsm->r_del_time = rsm->r_del_time; 4413 nrsm->r_app_limited = rsm->r_app_limited; 4414 nrsm->r_first_sent_time = rsm->r_first_sent_time; 4415 nrsm->r_flight_at_send = rsm->r_flight_at_send; 4416 /* We split a piece the lower section looses any just_ret flag. */ 4417 nrsm->r_bbr_state = rsm->r_bbr_state; 4418 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4419 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4420 } 4421 rsm->r_end = nrsm->r_start; 4422 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 4423 idx /= 8; 4424 /* Check if we got too small */ 4425 if ((rsm->r_is_smallmap == 0) && 4426 ((rsm->r_end - rsm->r_start) <= idx)) { 4427 bbr->r_ctl.rc_num_small_maps_alloced++; 4428 rsm->r_is_smallmap = 1; 4429 } 4430 /* Check the new one as well */ 4431 if ((nrsm->r_end - nrsm->r_start) <= idx) { 4432 bbr->r_ctl.rc_num_small_maps_alloced++; 4433 nrsm->r_is_smallmap = 1; 4434 } 4435 } 4436 4437 static int 4438 bbr_sack_mergable(struct bbr_sendmap *at, 4439 uint32_t start, uint32_t end) 4440 { 4441 /* 4442 * Given a sack block defined by 4443 * start and end, and a current position 4444 * at. Return 1 if either side of at 4445 * would show that the block is mergable 4446 * to that side. A block to be mergable 4447 * must have overlap with the start/end 4448 * and be in the SACK'd state. 4449 */ 4450 struct bbr_sendmap *l_rsm; 4451 struct bbr_sendmap *r_rsm; 4452 4453 /* first get the either side blocks */ 4454 l_rsm = TAILQ_PREV(at, bbr_head, r_next); 4455 r_rsm = TAILQ_NEXT(at, r_next); 4456 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) { 4457 /* Potentially mergeable */ 4458 if ((l_rsm->r_end == start) || 4459 (SEQ_LT(start, l_rsm->r_end) && 4460 SEQ_GT(end, l_rsm->r_end))) { 4461 /* 4462 * map blk |------| 4463 * sack blk |------| 4464 * <or> 4465 * map blk |------| 4466 * sack blk |------| 4467 */ 4468 return (1); 4469 } 4470 } 4471 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) { 4472 /* Potentially mergeable */ 4473 if ((r_rsm->r_start == end) || 4474 (SEQ_LT(start, r_rsm->r_start) && 4475 SEQ_GT(end, r_rsm->r_start))) { 4476 /* 4477 * map blk |---------| 4478 * sack blk |----| 4479 * <or> 4480 * map blk |---------| 4481 * sack blk |-------| 4482 */ 4483 return (1); 4484 } 4485 } 4486 return (0); 4487 } 4488 4489 static struct bbr_sendmap * 4490 bbr_merge_rsm(struct tcp_bbr *bbr, 4491 struct bbr_sendmap *l_rsm, 4492 struct bbr_sendmap *r_rsm) 4493 { 4494 /* 4495 * We are merging two ack'd RSM's, 4496 * the l_rsm is on the left (lower seq 4497 * values) and the r_rsm is on the right 4498 * (higher seq value). The simplest way 4499 * to merge these is to move the right 4500 * one into the left. I don't think there 4501 * is any reason we need to try to find 4502 * the oldest (or last oldest retransmitted). 4503 */ 4504 l_rsm->r_end = r_rsm->r_end; 4505 if (l_rsm->r_dupack < r_rsm->r_dupack) 4506 l_rsm->r_dupack = r_rsm->r_dupack; 4507 if (r_rsm->r_rtr_bytes) 4508 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 4509 if (r_rsm->r_in_tmap) { 4510 /* This really should not happen */ 4511 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext); 4512 } 4513 if (r_rsm->r_app_limited) 4514 l_rsm->r_app_limited = r_rsm->r_app_limited; 4515 /* Now the flags */ 4516 if (r_rsm->r_flags & BBR_HAS_FIN) 4517 l_rsm->r_flags |= BBR_HAS_FIN; 4518 if (r_rsm->r_flags & BBR_TLP) 4519 l_rsm->r_flags |= BBR_TLP; 4520 if (r_rsm->r_flags & BBR_RWND_COLLAPSED) 4521 l_rsm->r_flags |= BBR_RWND_COLLAPSED; 4522 if (r_rsm->r_flags & BBR_MARKED_LOST) { 4523 /* This really should not happen */ 4524 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start; 4525 } 4526 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next); 4527 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 4528 /* Transfer the split limit to the map we free */ 4529 r_rsm->r_limit_type = l_rsm->r_limit_type; 4530 l_rsm->r_limit_type = 0; 4531 } 4532 bbr_free(bbr, r_rsm); 4533 return(l_rsm); 4534 } 4535 4536 /* 4537 * TLP Timer, here we simply setup what segment we want to 4538 * have the TLP expire on, the normal bbr_output_wtime() will then 4539 * send it out. 4540 * 4541 * We return 1, saying don't proceed with bbr_output_wtime only 4542 * when all timers have been stopped (destroyed PCB?). 4543 */ 4544 static int 4545 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4546 { 4547 /* 4548 * Tail Loss Probe. 4549 */ 4550 struct bbr_sendmap *rsm = NULL; 4551 struct socket *so; 4552 uint32_t amm; 4553 uint32_t out, avail; 4554 uint32_t maxseg; 4555 int collapsed_win = 0; 4556 4557 if (bbr->rc_all_timers_stopped) { 4558 return (1); 4559 } 4560 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4561 /* Its not time yet */ 4562 return (0); 4563 } 4564 if (ctf_progress_timeout_check(tp, true)) { 4565 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4566 return (-ETIMEDOUT); /* tcp_drop() */ 4567 } 4568 /* Did we somehow get into persists? */ 4569 if (bbr->rc_in_persist) { 4570 return (0); 4571 } 4572 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4573 bbr_set_state(tp, bbr, 0); 4574 BBR_STAT_INC(bbr_tlp_tot); 4575 maxseg = tp->t_maxseg - bbr->rc_last_options; 4576 /* 4577 * A TLP timer has expired. We have been idle for 2 rtts. So we now 4578 * need to figure out how to force a full MSS segment out. 4579 */ 4580 so = tptosocket(tp); 4581 avail = sbavail(&so->so_snd); 4582 out = ctf_outstanding(tp); 4583 if (out > tp->snd_wnd) { 4584 /* special case, we need a retransmission */ 4585 collapsed_win = 1; 4586 goto need_retran; 4587 } 4588 if (avail > out) { 4589 /* New data is available */ 4590 amm = avail - out; 4591 if (amm > maxseg) { 4592 amm = maxseg; 4593 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 4594 /* not enough to fill a MTU and no-delay is off */ 4595 goto need_retran; 4596 } 4597 /* Set the send-new override */ 4598 if ((out + amm) <= tp->snd_wnd) { 4599 bbr->rc_tlp_new_data = 1; 4600 } else { 4601 goto need_retran; 4602 } 4603 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4604 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max; 4605 bbr->r_ctl.rc_tlp_send = NULL; 4606 /* cap any slots */ 4607 BBR_STAT_INC(bbr_tlp_newdata); 4608 goto send; 4609 } 4610 need_retran: 4611 /* 4612 * Ok we need to arrange the last un-acked segment to be re-sent, or 4613 * optionally the first un-acked segment. 4614 */ 4615 if (collapsed_win == 0) { 4616 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 4617 if (rsm && (rsm->r_flags & (BBR_ACKED | BBR_HAS_FIN))) { 4618 rsm = bbr_find_high_nonack(bbr, rsm); 4619 } 4620 if (rsm == NULL) { 4621 goto restore; 4622 } 4623 } else { 4624 /* 4625 * We must find the last segment 4626 * that was acceptable by the client. 4627 */ 4628 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4629 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) { 4630 /* Found one */ 4631 break; 4632 } 4633 } 4634 if (rsm == NULL) { 4635 /* None? if so send the first */ 4636 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4637 if (rsm == NULL) 4638 goto restore; 4639 } 4640 } 4641 if ((rsm->r_end - rsm->r_start) > maxseg) { 4642 /* 4643 * We need to split this the last segment in two. 4644 */ 4645 struct bbr_sendmap *nrsm; 4646 4647 nrsm = bbr_alloc_full_limit(bbr); 4648 if (nrsm == NULL) { 4649 /* 4650 * We can't get memory to split, we can either just 4651 * not split it. Or retransmit the whole piece, lets 4652 * do the large send (BTLP :-) ). 4653 */ 4654 goto go_for_it; 4655 } 4656 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg)); 4657 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 4658 if (rsm->r_in_tmap) { 4659 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4660 nrsm->r_in_tmap = 1; 4661 } 4662 rsm->r_flags &= (~BBR_HAS_FIN); 4663 rsm = nrsm; 4664 } 4665 go_for_it: 4666 bbr->r_ctl.rc_tlp_send = rsm; 4667 bbr->rc_tlp_rtx_out = 1; 4668 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) { 4669 bbr->r_ctl.rc_tlp_seg_send_cnt++; 4670 tp->t_rxtshift++; 4671 } else { 4672 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 4673 bbr->r_ctl.rc_tlp_seg_send_cnt = 1; 4674 } 4675 send: 4676 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 4677 /* 4678 * Can't [re]/transmit a segment we have retransmitted the 4679 * max times. We need the retransmit timer to take over. 4680 */ 4681 restore: 4682 bbr->rc_tlp_new_data = 0; 4683 bbr->r_ctl.rc_tlp_send = NULL; 4684 if (rsm) 4685 rsm->r_flags &= ~BBR_TLP; 4686 BBR_STAT_INC(bbr_tlp_retran_fail); 4687 return (0); 4688 } else if (rsm) { 4689 rsm->r_flags |= BBR_TLP; 4690 } 4691 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) && 4692 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) { 4693 /* 4694 * We have retransmitted to many times for TLP. Switch to 4695 * the regular RTO timer 4696 */ 4697 goto restore; 4698 } 4699 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP); 4700 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 4701 return (0); 4702 } 4703 4704 /* 4705 * Delayed ack Timer, here we simply need to setup the 4706 * ACK_NOW flag and remove the DELACK flag. From there 4707 * the output routine will send the ack out. 4708 * 4709 * We only return 1, saying don't proceed, if all timers 4710 * are stopped (destroyed PCB?). 4711 */ 4712 static int 4713 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4714 { 4715 if (bbr->rc_all_timers_stopped) { 4716 return (1); 4717 } 4718 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK); 4719 tp->t_flags &= ~TF_DELACK; 4720 tp->t_flags |= TF_ACKNOW; 4721 KMOD_TCPSTAT_INC(tcps_delack); 4722 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 4723 return (0); 4724 } 4725 4726 /* 4727 * Here we send a KEEP-ALIVE like probe to the 4728 * peer, we do not send data. 4729 * 4730 * We only return 1, saying don't proceed, if all timers 4731 * are stopped (destroyed PCB?). 4732 */ 4733 static int 4734 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4735 { 4736 struct tcptemp *t_template; 4737 int32_t retval = 1; 4738 4739 if (bbr->rc_all_timers_stopped) { 4740 return (1); 4741 } 4742 if (bbr->rc_in_persist == 0) 4743 return (0); 4744 4745 /* 4746 * Persistence timer into zero window. Force a byte to be output, if 4747 * possible. 4748 */ 4749 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST); 4750 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 4751 KMOD_TCPSTAT_INC(tcps_persisttimeo); 4752 /* 4753 * Have we exceeded the user specified progress time? 4754 */ 4755 if (ctf_progress_timeout_check(tp, true)) { 4756 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4757 return (-ETIMEDOUT); /* tcp_drop() */ 4758 } 4759 /* 4760 * Hack: if the peer is dead/unreachable, we do not time out if the 4761 * window is closed. After a full backoff, drop the connection if 4762 * the idle time (no responses to probes) reaches the maximum 4763 * backoff that we would use if retransmitting. 4764 */ 4765 if (tp->t_rxtshift >= V_tcp_retries && 4766 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 4767 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 4768 KMOD_TCPSTAT_INC(tcps_persistdrop); 4769 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4770 return (-ETIMEDOUT); /* tcp_drop() */ 4771 } 4772 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) && 4773 tp->snd_una == tp->snd_max) { 4774 bbr_exit_persist(tp, bbr, cts, __LINE__); 4775 retval = 0; 4776 goto out; 4777 } 4778 /* 4779 * If the user has closed the socket then drop a persisting 4780 * connection after a much reduced timeout. 4781 */ 4782 if (tp->t_state > TCPS_CLOSE_WAIT && 4783 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 4784 KMOD_TCPSTAT_INC(tcps_persistdrop); 4785 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4786 return (-ETIMEDOUT); /* tcp_drop() */ 4787 } 4788 t_template = tcpip_maketemplate(bbr->rc_inp); 4789 if (t_template) { 4790 tcp_respond(tp, t_template->tt_ipgen, 4791 &t_template->tt_t, (struct mbuf *)NULL, 4792 tp->rcv_nxt, tp->snd_una - 1, 0); 4793 /* This sends an ack */ 4794 if (tp->t_flags & TF_DELACK) 4795 tp->t_flags &= ~TF_DELACK; 4796 free(t_template, M_TEMP); 4797 } 4798 if (tp->t_rxtshift < V_tcp_retries) 4799 tp->t_rxtshift++; 4800 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0); 4801 out: 4802 return (retval); 4803 } 4804 4805 /* 4806 * If a keepalive goes off, we had no other timers 4807 * happening. We always return 1 here since this 4808 * routine either drops the connection or sends 4809 * out a segment with respond. 4810 */ 4811 static int 4812 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4813 { 4814 struct tcptemp *t_template; 4815 struct inpcb *inp = tptoinpcb(tp); 4816 4817 if (bbr->rc_all_timers_stopped) { 4818 return (1); 4819 } 4820 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 4821 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP); 4822 /* 4823 * Keep-alive timer went off; send something or drop connection if 4824 * idle for too long. 4825 */ 4826 KMOD_TCPSTAT_INC(tcps_keeptimeo); 4827 if (tp->t_state < TCPS_ESTABLISHED) 4828 goto dropit; 4829 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 4830 tp->t_state <= TCPS_CLOSING) { 4831 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 4832 goto dropit; 4833 /* 4834 * Send a packet designed to force a response if the peer is 4835 * up and reachable: either an ACK if the connection is 4836 * still alive, or an RST if the peer has closed the 4837 * connection due to timeout or reboot. Using sequence 4838 * number tp->snd_una-1 causes the transmitted zero-length 4839 * segment to lie outside the receive window; by the 4840 * protocol spec, this requires the correspondent TCP to 4841 * respond. 4842 */ 4843 KMOD_TCPSTAT_INC(tcps_keepprobe); 4844 t_template = tcpip_maketemplate(inp); 4845 if (t_template) { 4846 tcp_respond(tp, t_template->tt_ipgen, 4847 &t_template->tt_t, (struct mbuf *)NULL, 4848 tp->rcv_nxt, tp->snd_una - 1, 0); 4849 free(t_template, M_TEMP); 4850 } 4851 } 4852 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0); 4853 return (1); 4854 dropit: 4855 KMOD_TCPSTAT_INC(tcps_keepdrops); 4856 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 4857 return (-ETIMEDOUT); /* tcp_drop() */ 4858 } 4859 4860 /* 4861 * Retransmit helper function, clear up all the ack 4862 * flags and take care of important book keeping. 4863 */ 4864 static void 4865 bbr_remxt_tmr(struct tcpcb *tp) 4866 { 4867 /* 4868 * The retransmit timer went off, all sack'd blocks must be 4869 * un-acked. 4870 */ 4871 struct bbr_sendmap *rsm, *trsm = NULL; 4872 struct tcp_bbr *bbr; 4873 uint32_t cts, lost; 4874 4875 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 4876 cts = tcp_get_usecs(&bbr->rc_tv); 4877 lost = bbr->r_ctl.rc_lost; 4878 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4879 bbr_set_state(tp, bbr, 0); 4880 4881 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 4882 if (rsm->r_flags & BBR_ACKED) { 4883 uint32_t old_flags; 4884 4885 rsm->r_dupack = 0; 4886 if (rsm->r_in_tmap == 0) { 4887 /* We must re-add it back to the tlist */ 4888 if (trsm == NULL) { 4889 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 4890 } else { 4891 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext); 4892 } 4893 rsm->r_in_tmap = 1; 4894 } 4895 old_flags = rsm->r_flags; 4896 rsm->r_flags |= BBR_RXT_CLEARED; 4897 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS); 4898 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 4899 } else { 4900 if ((tp->t_state < TCPS_ESTABLISHED) && 4901 (rsm->r_start == tp->snd_una)) { 4902 /* 4903 * Special case for TCP FO. Where 4904 * we sent more data beyond the snd_max. 4905 * We don't mark that as lost and stop here. 4906 */ 4907 break; 4908 } 4909 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4910 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4911 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4912 } 4913 if (bbr_marks_rxt_sack_passed) { 4914 /* 4915 * With this option, we will rack out 4916 * in 1ms increments the rest of the packets. 4917 */ 4918 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST; 4919 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4920 } else { 4921 /* 4922 * With this option we only mark them lost 4923 * and remove all sack'd markings. We will run 4924 * another RXT or a TLP. This will cause 4925 * us to eventually send more based on what 4926 * ack's come in. 4927 */ 4928 rsm->r_flags |= BBR_MARKED_LOST; 4929 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4930 rsm->r_flags &= ~BBR_SACK_PASSED; 4931 } 4932 } 4933 trsm = rsm; 4934 } 4935 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4936 /* Clear the count (we just un-acked them) */ 4937 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR); 4938 bbr->rc_tlp_new_data = 0; 4939 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4940 /* zap the behindness on a rxt */ 4941 bbr->r_ctl.rc_hptsi_agg_delay = 0; 4942 bbr->r_agg_early_set = 0; 4943 bbr->r_ctl.rc_agg_early = 0; 4944 bbr->rc_tlp_rtx_out = 0; 4945 bbr->r_ctl.rc_sacked = 0; 4946 bbr->r_ctl.rc_sacklast = NULL; 4947 bbr->r_timer_override = 1; 4948 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4949 } 4950 4951 /* 4952 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 4953 * we will setup to retransmit the lowest seq number outstanding. 4954 */ 4955 static int 4956 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4957 { 4958 struct inpcb *inp = tptoinpcb(tp); 4959 int32_t rexmt; 4960 int32_t retval = 0; 4961 bool isipv6; 4962 4963 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 4964 if (bbr->rc_all_timers_stopped) { 4965 return (1); 4966 } 4967 if (TCPS_HAVEESTABLISHED(tp->t_state) && 4968 (tp->snd_una == tp->snd_max)) { 4969 /* Nothing outstanding .. nothing to do */ 4970 return (0); 4971 } 4972 /* 4973 * Retransmission timer went off. Message has not been acked within 4974 * retransmit interval. Back off to a longer retransmit interval 4975 * and retransmit one segment. 4976 */ 4977 if (ctf_progress_timeout_check(tp, true)) { 4978 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4979 return (-ETIMEDOUT); /* tcp_drop() */ 4980 } 4981 bbr_remxt_tmr(tp); 4982 if ((bbr->r_ctl.rc_resend == NULL) || 4983 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) { 4984 /* 4985 * If the rwnd collapsed on 4986 * the one we are retransmitting 4987 * it does not count against the 4988 * rxt count. 4989 */ 4990 tp->t_rxtshift++; 4991 } 4992 if (tp->t_rxtshift > V_tcp_retries) { 4993 tp->t_rxtshift = V_tcp_retries; 4994 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 4995 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 4996 /* XXXGL: previously t_softerror was casted to uint16_t */ 4997 MPASS(tp->t_softerror >= 0); 4998 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 4999 return (retval); /* tcp_drop() */ 5000 } 5001 if (tp->t_state == TCPS_SYN_SENT) { 5002 /* 5003 * If the SYN was retransmitted, indicate CWND to be limited 5004 * to 1 segment in cc_conn_init(). 5005 */ 5006 tp->snd_cwnd = 1; 5007 } else if (tp->t_rxtshift == 1) { 5008 /* 5009 * first retransmit; record ssthresh and cwnd so they can be 5010 * recovered if this turns out to be a "bad" retransmit. A 5011 * retransmit is considered "bad" if an ACK for this segment 5012 * is received within RTT/2 interval; the assumption here is 5013 * that the ACK was already in flight. See "On Estimating 5014 * End-to-End Network Path Properties" by Allman and Paxson 5015 * for more details. 5016 */ 5017 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5018 if (!IN_RECOVERY(tp->t_flags)) { 5019 tp->snd_cwnd_prev = tp->snd_cwnd; 5020 tp->snd_ssthresh_prev = tp->snd_ssthresh; 5021 tp->snd_recover_prev = tp->snd_recover; 5022 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 5023 tp->t_flags |= TF_PREVVALID; 5024 } else { 5025 tp->t_flags &= ~TF_PREVVALID; 5026 } 5027 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5028 } else { 5029 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5030 tp->t_flags &= ~TF_PREVVALID; 5031 } 5032 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 5033 if ((tp->t_state == TCPS_SYN_SENT) || 5034 (tp->t_state == TCPS_SYN_RECEIVED)) 5035 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift]; 5036 else 5037 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 5038 TCPT_RANGESET(tp->t_rxtcur, rexmt, 5039 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), 5040 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 5041 /* 5042 * We enter the path for PLMTUD if connection is established or, if 5043 * connection is FIN_WAIT_1 status, reason for the last is that if 5044 * amount of data we send is very small, we could send it in couple 5045 * of packets and process straight to FIN. In that case we won't 5046 * catch ESTABLISHED state. 5047 */ 5048 #ifdef INET6 5049 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 5050 #else 5051 isipv6 = false; 5052 #endif 5053 if (((V_tcp_pmtud_blackhole_detect == 1) || 5054 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 5055 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 5056 ((tp->t_state == TCPS_ESTABLISHED) || 5057 (tp->t_state == TCPS_FIN_WAIT_1))) { 5058 /* 5059 * Idea here is that at each stage of mtu probe (usually, 5060 * 1448 -> 1188 -> 524) should be given 2 chances to recover 5061 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 5062 * should take care of that. 5063 */ 5064 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 5065 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 5066 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 5067 tp->t_rxtshift % 2 == 0)) { 5068 /* 5069 * Enter Path MTU Black-hole Detection mechanism: - 5070 * Disable Path MTU Discovery (IP "DF" bit). - 5071 * Reduce MTU to lower value than what we negotiated 5072 * with peer. 5073 */ 5074 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 5075 /* 5076 * Record that we may have found a black 5077 * hole. 5078 */ 5079 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 5080 /* Keep track of previous MSS. */ 5081 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 5082 } 5083 /* 5084 * Reduce the MSS to blackhole value or to the 5085 * default in an attempt to retransmit. 5086 */ 5087 #ifdef INET6 5088 isipv6 = bbr->r_is_v6; 5089 if (isipv6 && 5090 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 5091 /* Use the sysctl tuneable blackhole MSS. */ 5092 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 5093 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5094 } else if (isipv6) { 5095 /* Use the default MSS. */ 5096 tp->t_maxseg = V_tcp_v6mssdflt; 5097 /* 5098 * Disable Path MTU Discovery when we switch 5099 * to minmss. 5100 */ 5101 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5102 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5103 } 5104 #endif 5105 #if defined(INET6) && defined(INET) 5106 else 5107 #endif 5108 #ifdef INET 5109 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 5110 /* Use the sysctl tuneable blackhole MSS. */ 5111 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 5112 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5113 } else { 5114 /* Use the default MSS. */ 5115 tp->t_maxseg = V_tcp_mssdflt; 5116 /* 5117 * Disable Path MTU Discovery when we switch 5118 * to minmss. 5119 */ 5120 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5121 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5122 } 5123 #endif 5124 } else { 5125 /* 5126 * If further retransmissions are still unsuccessful 5127 * with a lowered MTU, maybe this isn't a blackhole 5128 * and we restore the previous MSS and blackhole 5129 * detection flags. The limit '6' is determined by 5130 * giving each probe stage (1448, 1188, 524) 2 5131 * chances to recover. 5132 */ 5133 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 5134 (tp->t_rxtshift >= 6)) { 5135 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 5136 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 5137 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 5138 if (tp->t_maxseg < V_tcp_mssdflt) { 5139 /* 5140 * The MSS is so small we should not 5141 * process incoming SACK's since we are 5142 * subject to attack in such a case. 5143 */ 5144 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 5145 } else { 5146 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 5147 } 5148 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 5149 } 5150 } 5151 } 5152 /* 5153 * Disable RFC1323 and SACK if we haven't got any response to our 5154 * third SYN to work-around some broken terminal servers (most of 5155 * which have hopefully been retired) that have bad VJ header 5156 * compression code which trashes TCP segments containing 5157 * unknown-to-them TCP options. 5158 */ 5159 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 5160 (tp->t_rxtshift == 3)) 5161 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 5162 /* 5163 * If we backed off this far, our srtt estimate is probably bogus. 5164 * Clobber it so we'll take the next rtt measurement as our srtt; 5165 * move the current srtt into rttvar to keep the current retransmit 5166 * times until then. 5167 */ 5168 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 5169 #ifdef INET6 5170 if (bbr->r_is_v6) 5171 in6_losing(inp); 5172 else 5173 #endif 5174 in_losing(inp); 5175 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 5176 tp->t_srtt = 0; 5177 } 5178 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 5179 tp->snd_recover = tp->snd_max; 5180 tp->t_flags |= TF_ACKNOW; 5181 tp->t_rtttime = 0; 5182 5183 return (retval); 5184 } 5185 5186 static int 5187 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling) 5188 { 5189 int32_t ret = 0; 5190 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 5191 5192 if (timers == 0) { 5193 return (0); 5194 } 5195 if (tp->t_state == TCPS_LISTEN) { 5196 /* no timers on listen sockets */ 5197 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 5198 return (0); 5199 return (1); 5200 } 5201 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 5202 uint32_t left; 5203 5204 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 5205 ret = -1; 5206 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5207 return (0); 5208 } 5209 if (hpts_calling == 0) { 5210 ret = -2; 5211 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5212 return (0); 5213 } 5214 /* 5215 * Ok our timer went off early and we are not paced false 5216 * alarm, go back to sleep. 5217 */ 5218 left = bbr->r_ctl.rc_timer_exp - cts; 5219 ret = -3; 5220 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling); 5221 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left)); 5222 return (1); 5223 } 5224 bbr->rc_tmr_stopped = 0; 5225 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 5226 if (timers & PACE_TMR_DELACK) { 5227 ret = bbr_timeout_delack(tp, bbr, cts); 5228 } else if (timers & PACE_TMR_PERSIT) { 5229 ret = bbr_timeout_persist(tp, bbr, cts); 5230 } else if (timers & PACE_TMR_RACK) { 5231 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5232 ret = bbr_timeout_rack(tp, bbr, cts); 5233 } else if (timers & PACE_TMR_TLP) { 5234 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5235 ret = bbr_timeout_tlp(tp, bbr, cts); 5236 } else if (timers & PACE_TMR_RXT) { 5237 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5238 ret = bbr_timeout_rxt(tp, bbr, cts); 5239 } else if (timers & PACE_TMR_KEEP) { 5240 ret = bbr_timeout_keepalive(tp, bbr, cts); 5241 } 5242 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling); 5243 return (ret); 5244 } 5245 5246 static void 5247 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts) 5248 { 5249 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 5250 uint8_t hpts_removed = 0; 5251 5252 if (tcp_in_hpts(bbr->rc_tp) && 5253 (bbr->rc_timer_first == 1)) { 5254 /* 5255 * If we are canceling timer's when we have the 5256 * timer ahead of the output being paced. We also 5257 * must remove ourselves from the hpts. 5258 */ 5259 hpts_removed = 1; 5260 tcp_hpts_remove(bbr->rc_tp); 5261 if (bbr->r_ctl.rc_last_delay_val) { 5262 /* Update the last hptsi delay too */ 5263 uint32_t time_since_send; 5264 5265 if (TSTMP_GT(cts, bbr->rc_pacer_started)) 5266 time_since_send = cts - bbr->rc_pacer_started; 5267 else 5268 time_since_send = 0; 5269 if (bbr->r_ctl.rc_last_delay_val > time_since_send) { 5270 /* Cut down our slot time */ 5271 bbr->r_ctl.rc_last_delay_val -= time_since_send; 5272 } else { 5273 bbr->r_ctl.rc_last_delay_val = 0; 5274 } 5275 bbr->rc_pacer_started = cts; 5276 } 5277 } 5278 bbr->rc_timer_first = 0; 5279 bbr_log_to_cancel(bbr, line, cts, hpts_removed); 5280 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 5281 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 5282 } 5283 } 5284 5285 static int 5286 bbr_stopall(struct tcpcb *tp) 5287 { 5288 struct tcp_bbr *bbr; 5289 5290 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 5291 bbr->rc_all_timers_stopped = 1; 5292 5293 tcp_hpts_remove(tp); 5294 5295 return (0); 5296 } 5297 5298 static uint32_t 5299 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts) 5300 { 5301 struct bbr_sendmap *rsm; 5302 5303 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 5304 if ((rsm == NULL) || (u_rsm == rsm)) 5305 return (cts); 5306 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 5307 } 5308 5309 static void 5310 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr, 5311 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time) 5312 { 5313 int32_t idx; 5314 5315 rsm->r_rtr_cnt++; 5316 rsm->r_dupack = 0; 5317 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) { 5318 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS; 5319 rsm->r_flags |= BBR_OVERMAX; 5320 } 5321 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 5322 /* Take off the collapsed flag at rxt */ 5323 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 5324 } 5325 if (rsm->r_flags & BBR_MARKED_LOST) { 5326 /* We have retransmitted, its no longer lost */ 5327 rsm->r_flags &= ~BBR_MARKED_LOST; 5328 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 5329 } 5330 if (rsm->r_flags & BBR_RXT_CLEARED) { 5331 /* 5332 * We hit a RXT timer on it and 5333 * we cleared the "acked" flag. 5334 * We now have it going back into 5335 * flight, we can remove the cleared 5336 * flag and possibly do accounting on 5337 * this piece. 5338 */ 5339 rsm->r_flags &= ~BBR_RXT_CLEARED; 5340 } 5341 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) { 5342 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 5343 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 5344 } 5345 idx = rsm->r_rtr_cnt - 1; 5346 rsm->r_tim_lastsent[idx] = cts; 5347 rsm->r_pacing_delay = pacing_time; 5348 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5349 rsm->r_ts_valid = bbr->rc_ts_valid; 5350 if (bbr->rc_ts_valid) 5351 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5352 if (bbr->r_ctl.r_app_limited_until) 5353 rsm->r_app_limited = 1; 5354 else 5355 rsm->r_app_limited = 0; 5356 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5357 rsm->r_bbr_state = bbr_state_val(bbr); 5358 else 5359 rsm->r_bbr_state = 8; 5360 if (rsm->r_flags & BBR_ACKED) { 5361 /* Problably MTU discovery messing with us */ 5362 uint32_t old_flags; 5363 5364 old_flags = rsm->r_flags; 5365 rsm->r_flags &= ~BBR_ACKED; 5366 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 5367 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 5368 if (bbr->r_ctl.rc_sacked == 0) 5369 bbr->r_ctl.rc_sacklast = NULL; 5370 } 5371 if (rsm->r_in_tmap) { 5372 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5373 } 5374 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5375 rsm->r_in_tmap = 1; 5376 if (rsm->r_flags & BBR_SACK_PASSED) { 5377 /* We have retransmitted due to the SACK pass */ 5378 rsm->r_flags &= ~BBR_SACK_PASSED; 5379 rsm->r_flags |= BBR_WAS_SACKPASS; 5380 } 5381 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5382 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5383 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5384 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 5385 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5386 rsm->r_is_gain = 1; 5387 rsm->r_is_drain = 0; 5388 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5389 rsm->r_is_drain = 1; 5390 rsm->r_is_gain = 0; 5391 } else { 5392 rsm->r_is_drain = 0; 5393 rsm->r_is_gain = 0; 5394 } 5395 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */ 5396 } 5397 5398 /* 5399 * Returns 0, or the sequence where we stopped 5400 * updating. We also update the lenp to be the amount 5401 * of data left. 5402 */ 5403 5404 static uint32_t 5405 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr, 5406 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time) 5407 { 5408 /* 5409 * We (re-)transmitted starting at rsm->r_start for some length 5410 * (possibly less than r_end. 5411 */ 5412 struct bbr_sendmap *nrsm; 5413 uint32_t c_end; 5414 int32_t len; 5415 5416 len = *lenp; 5417 c_end = rsm->r_start + len; 5418 if (SEQ_GEQ(c_end, rsm->r_end)) { 5419 /* 5420 * We retransmitted the whole piece or more than the whole 5421 * slopping into the next rsm. 5422 */ 5423 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5424 if (c_end == rsm->r_end) { 5425 *lenp = 0; 5426 return (0); 5427 } else { 5428 int32_t act_len; 5429 5430 /* Hangs over the end return whats left */ 5431 act_len = rsm->r_end - rsm->r_start; 5432 *lenp = (len - act_len); 5433 return (rsm->r_end); 5434 } 5435 /* We don't get out of this block. */ 5436 } 5437 /* 5438 * Here we retransmitted less than the whole thing which means we 5439 * have to split this into what was transmitted and what was not. 5440 */ 5441 nrsm = bbr_alloc_full_limit(bbr); 5442 if (nrsm == NULL) { 5443 *lenp = 0; 5444 return (0); 5445 } 5446 /* 5447 * So here we are going to take the original rsm and make it what we 5448 * retransmitted. nrsm will be the tail portion we did not 5449 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 5450 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 5451 * 1, 6 and the new piece will be 6, 11. 5452 */ 5453 bbr_clone_rsm(bbr, nrsm, rsm, c_end); 5454 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 5455 nrsm->r_dupack = 0; 5456 if (rsm->r_in_tmap) { 5457 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 5458 nrsm->r_in_tmap = 1; 5459 } 5460 rsm->r_flags &= (~BBR_HAS_FIN); 5461 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5462 *lenp = 0; 5463 return (0); 5464 } 5465 5466 static uint64_t 5467 bbr_get_hardware_rate(struct tcp_bbr *bbr) 5468 { 5469 uint64_t bw; 5470 5471 bw = bbr_get_bw(bbr); 5472 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]; 5473 bw /= (uint64_t)BBR_UNIT; 5474 return(bw); 5475 } 5476 5477 static void 5478 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts, 5479 uint64_t act_rate, uint64_t rate_wanted) 5480 { 5481 /* 5482 * We could not get a full gains worth 5483 * of rate. 5484 */ 5485 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) { 5486 /* we can't even get the real rate */ 5487 uint64_t red; 5488 5489 bbr->skip_gain = 1; 5490 bbr->gain_is_limited = 0; 5491 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate; 5492 if (red) 5493 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts); 5494 } else { 5495 /* We can use a lower gain */ 5496 bbr->skip_gain = 0; 5497 bbr->gain_is_limited = 1; 5498 } 5499 } 5500 5501 static void 5502 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts) 5503 { 5504 const struct tcp_hwrate_limit_table *nrte; 5505 int error, rate = -1; 5506 5507 if (bbr->r_ctl.crte == NULL) 5508 return; 5509 if ((bbr->rc_inp->inp_route.ro_nh == NULL) || 5510 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) { 5511 /* Lost our routes? */ 5512 /* Clear the way for a re-attempt */ 5513 bbr->bbr_attempt_hdwr_pace = 0; 5514 lost_rate: 5515 bbr->gain_is_limited = 0; 5516 bbr->skip_gain = 0; 5517 bbr->bbr_hdrw_pacing = 0; 5518 counter_u64_add(bbr_flows_whdwr_pacing, -1); 5519 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 5520 tcp_bbr_tso_size_check(bbr, cts); 5521 return; 5522 } 5523 rate = bbr_get_hardware_rate(bbr); 5524 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte, 5525 bbr->rc_tp, 5526 bbr->rc_inp->inp_route.ro_nh->nh_ifp, 5527 rate, 5528 (RS_PACING_GEQ|RS_PACING_SUB_OK), 5529 &error, NULL); 5530 if (nrte == NULL) { 5531 goto lost_rate; 5532 } 5533 if (nrte != bbr->r_ctl.crte) { 5534 bbr->r_ctl.crte = nrte; 5535 if (error == 0) { 5536 BBR_STAT_INC(bbr_hdwr_rl_mod_ok); 5537 if (bbr->r_ctl.crte->rate < rate) { 5538 /* We have a problem */ 5539 bbr_setup_less_of_rate(bbr, cts, 5540 bbr->r_ctl.crte->rate, rate); 5541 } else { 5542 /* We are good */ 5543 bbr->gain_is_limited = 0; 5544 bbr->skip_gain = 0; 5545 } 5546 } else { 5547 /* A failure should release the tag */ 5548 BBR_STAT_INC(bbr_hdwr_rl_mod_fail); 5549 bbr->gain_is_limited = 0; 5550 bbr->skip_gain = 0; 5551 bbr->bbr_hdrw_pacing = 0; 5552 } 5553 bbr_type_log_hdwr_pacing(bbr, 5554 bbr->r_ctl.crte->ptbl->rs_ifp, 5555 rate, 5556 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate), 5557 __LINE__, 5558 cts, 5559 error); 5560 } 5561 } 5562 5563 static void 5564 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts) 5565 { 5566 /* 5567 * If we have hardware pacing support 5568 * we need to factor that in for our 5569 * TSO size. 5570 */ 5571 const struct tcp_hwrate_limit_table *rlp; 5572 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay; 5573 5574 if ((bbr->bbr_hdrw_pacing == 0) || 5575 (IN_RECOVERY(bbr->rc_tp->t_flags)) || 5576 (bbr->r_ctl.crte == NULL)) 5577 return; 5578 if (bbr->hw_pacing_set == 0) { 5579 /* Not yet by the hdwr pacing count delay */ 5580 return; 5581 } 5582 if (bbr_hdwr_pace_adjust == 0) { 5583 /* No adjustment */ 5584 return; 5585 } 5586 rlp = bbr->r_ctl.crte; 5587 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) 5588 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5589 else 5590 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5591 /* 5592 * So lets first get the 5593 * time we will take between 5594 * TSO sized sends currently without 5595 * hardware help. 5596 */ 5597 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT, 5598 bbr->r_ctl.rc_pace_max_segs, cts, 1); 5599 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg; 5600 hdwr_delay *= rlp->time_between; 5601 if (cur_delay > hdwr_delay) 5602 delta = cur_delay - hdwr_delay; 5603 else 5604 delta = 0; 5605 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay, 5606 (bbr->r_ctl.rc_pace_max_segs / maxseg), 5607 1); 5608 if (delta && 5609 (delta < (max(rlp->time_between, 5610 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) { 5611 /* 5612 * Now lets divide by the pacing 5613 * time between each segment the 5614 * hardware sends rounding up and 5615 * derive a bytes from that. We multiply 5616 * that by bbr_hdwr_pace_adjust to get 5617 * more bang for our buck. 5618 * 5619 * The goal is to have the software pacer 5620 * waiting no more than an additional 5621 * pacing delay if we can (without the 5622 * compensation i.e. x bbr_hdwr_pace_adjust). 5623 */ 5624 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between), 5625 (bbr->r_ctl.rc_pace_max_segs/maxseg)); 5626 seg_sz *= bbr_hdwr_pace_adjust; 5627 if (bbr_hdwr_pace_floor && 5628 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5629 /* Currently hardware paces 5630 * out rs_min_seg segments at a time. 5631 * We need to make sure we always send at least 5632 * a full burst of bbr_hdwr_pace_floor down. 5633 */ 5634 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5635 } 5636 seg_sz *= maxseg; 5637 } else if (delta == 0) { 5638 /* 5639 * The highest pacing rate is 5640 * above our b/w gained. This means 5641 * we probably are going quite fast at 5642 * the hardware highest rate. Lets just multiply 5643 * the calculated TSO size by the 5644 * multiplier factor (its probably 5645 * 4 segments in the default config for 5646 * mlx). 5647 */ 5648 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust; 5649 if (bbr_hdwr_pace_floor && 5650 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5651 /* Currently hardware paces 5652 * out rs_min_seg segments at a time. 5653 * We need to make sure we always send at least 5654 * a full burst of bbr_hdwr_pace_floor down. 5655 */ 5656 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5657 } 5658 } else { 5659 /* 5660 * The pacing time difference is so 5661 * big that the hardware will 5662 * pace out more rapidly then we 5663 * really want and then we 5664 * will have a long delay. Lets just keep 5665 * the same TSO size so its as if 5666 * we were not using hdwr pacing (we 5667 * just gain a bit of spacing from the 5668 * hardware if seg_sz > 1). 5669 */ 5670 seg_sz = bbr->r_ctl.rc_pace_max_segs; 5671 } 5672 if (seg_sz > bbr->r_ctl.rc_pace_max_segs) 5673 new_tso = seg_sz; 5674 else 5675 new_tso = bbr->r_ctl.rc_pace_max_segs; 5676 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg)) 5677 new_tso = PACE_MAX_IP_BYTES - maxseg; 5678 5679 if (new_tso != bbr->r_ctl.rc_pace_max_segs) { 5680 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0); 5681 bbr->r_ctl.rc_pace_max_segs = new_tso; 5682 } 5683 } 5684 5685 static void 5686 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts) 5687 { 5688 uint64_t bw; 5689 uint32_t old_tso = 0, new_tso; 5690 uint32_t maxseg, bytes; 5691 uint32_t tls_seg=0; 5692 /* 5693 * Google/linux uses the following algorithm to determine 5694 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18): 5695 * 5696 * bytes = bw_in_bytes_per_second / 1000 5697 * bytes = min(bytes, 64k) 5698 * tso_segs = bytes / MSS 5699 * if (bw < 1.2Mbs) 5700 * min_tso_segs = 1 5701 * else 5702 * min_tso_segs = 2 5703 * tso_segs = max(tso_segs, min_tso_segs) 5704 * 5705 * * Note apply a device specific limit (we apply this in the 5706 * tcp_m_copym). 5707 * Note that before the initial measurement is made google bursts out 5708 * a full iwnd just like new-reno/cubic. 5709 * 5710 * We do not use this algorithm. Instead we 5711 * use a two phased approach: 5712 * 5713 * if ( bw <= per-tcb-cross-over) 5714 * goal_tso = calculate how much with this bw we 5715 * can send in goal-time seconds. 5716 * if (goal_tso > mss) 5717 * seg = goal_tso / mss 5718 * tso = seg * mss 5719 * else 5720 * tso = mss 5721 * if (tso > per-tcb-max) 5722 * tso = per-tcb-max 5723 * else if ( bw > 512Mbps) 5724 * tso = max-tso (64k/mss) 5725 * else 5726 * goal_tso = bw / per-tcb-divsor 5727 * seg = (goal_tso + mss-1)/mss 5728 * tso = seg * mss 5729 * 5730 * if (tso < per-tcb-floor) 5731 * tso = per-tcb-floor 5732 * if (tso > per-tcb-utter_max) 5733 * tso = per-tcb-utter_max 5734 * 5735 * Note the default per-tcb-divisor is 1000 (same as google). 5736 * the goal cross over is 30Mbps however. To recreate googles 5737 * algorithm you need to set: 5738 * 5739 * cross-over = 23,168,000 bps 5740 * goal-time = 18000 5741 * per-tcb-max = 2 5742 * per-tcb-divisor = 1000 5743 * per-tcb-floor = 1 5744 * 5745 * This will get you "google bbr" behavior with respect to tso size. 5746 * 5747 * Note we do set anything TSO size until we are past the initial 5748 * window. Before that we gnerally use either a single MSS 5749 * or we use the full IW size (so we burst a IW at a time) 5750 */ 5751 5752 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) { 5753 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5754 } else { 5755 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5756 } 5757 old_tso = bbr->r_ctl.rc_pace_max_segs; 5758 if (bbr->rc_past_init_win == 0) { 5759 /* 5760 * Not enough data has been acknowledged to make a 5761 * judgement. Set up the initial TSO based on if we 5762 * are sending a full IW at once or not. 5763 */ 5764 if (bbr->rc_use_google) 5765 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2); 5766 else if (bbr->bbr_init_win_cheat) 5767 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp); 5768 else 5769 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5770 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg) 5771 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg; 5772 if (bbr->r_ctl.rc_pace_max_segs == 0) { 5773 bbr->r_ctl.rc_pace_max_segs = maxseg; 5774 } 5775 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0); 5776 bbr_adjust_for_hw_pacing(bbr, cts); 5777 return; 5778 } 5779 /** 5780 * Now lets set the TSO goal based on our delivery rate in 5781 * bytes per second. Note we only do this if 5782 * we have acked at least the initial cwnd worth of data. 5783 */ 5784 bw = bbr_get_bw(bbr); 5785 if (IN_RECOVERY(bbr->rc_tp->t_flags) && 5786 (bbr->rc_use_google == 0)) { 5787 /* We clamp to one MSS in recovery */ 5788 new_tso = maxseg; 5789 } else if (bbr->rc_use_google) { 5790 int min_tso_segs; 5791 5792 /* Google considers the gain too */ 5793 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) { 5794 bw *= bbr->r_ctl.rc_bbr_hptsi_gain; 5795 bw /= BBR_UNIT; 5796 } 5797 bytes = bw / 1024; 5798 if (bytes > (64 * 1024)) 5799 bytes = 64 * 1024; 5800 new_tso = bytes / maxseg; 5801 if (bw < ONE_POINT_TWO_MEG) 5802 min_tso_segs = 1; 5803 else 5804 min_tso_segs = 2; 5805 if (new_tso < min_tso_segs) 5806 new_tso = min_tso_segs; 5807 new_tso *= maxseg; 5808 } else if (bbr->rc_no_pacing) { 5809 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg; 5810 } else if (bw <= bbr->r_ctl.bbr_cross_over) { 5811 /* 5812 * Calculate the worse case b/w TSO if we are inserting no 5813 * more than a delay_target number of TSO's. 5814 */ 5815 uint32_t tso_len, min_tso; 5816 5817 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw); 5818 if (tso_len > maxseg) { 5819 new_tso = tso_len / maxseg; 5820 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max) 5821 new_tso = bbr->r_ctl.bbr_hptsi_segments_max; 5822 new_tso *= maxseg; 5823 } else { 5824 /* 5825 * less than a full sized frame yikes.. long rtt or 5826 * low bw? 5827 */ 5828 min_tso = bbr_minseg(bbr); 5829 if ((tso_len > min_tso) && (bbr_all_get_min == 0)) 5830 new_tso = rounddown(tso_len, min_tso); 5831 else 5832 new_tso = min_tso; 5833 } 5834 } else if (bw > FIVETWELVE_MBPS) { 5835 /* 5836 * This guy is so fast b/w wise that we can TSO as large as 5837 * possible of segments that the NIC will allow. 5838 */ 5839 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5840 } else { 5841 /* 5842 * This formula is based on attempting to send a segment or 5843 * more every bbr_hptsi_per_second. The default is 1000 5844 * which means you are targeting what you can send every 1ms 5845 * based on the peers bw. 5846 * 5847 * If the number drops to say 500, then you are looking more 5848 * at 2ms and you will raise how much we send in a single 5849 * TSO thus saving CPU (less bbr_output_wtime() calls). The 5850 * trade off of course is you will send more at once and 5851 * thus tend to clump up the sends into larger "bursts" 5852 * building a queue. 5853 */ 5854 bw /= bbr->r_ctl.bbr_hptsi_per_second; 5855 new_tso = roundup(bw, (uint64_t)maxseg); 5856 /* 5857 * Gate the floor to match what our lower than 48Mbps 5858 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus 5859 * becomes the floor for this calculation. 5860 */ 5861 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg)) 5862 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg); 5863 } 5864 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor))) 5865 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor; 5866 if (new_tso > PACE_MAX_IP_BYTES) 5867 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5868 /* Enforce an utter maximum. */ 5869 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) { 5870 new_tso = bbr->r_ctl.bbr_utter_max * maxseg; 5871 } 5872 if (old_tso != new_tso) { 5873 /* Only log changes */ 5874 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0); 5875 bbr->r_ctl.rc_pace_max_segs = new_tso; 5876 } 5877 /* We have hardware pacing! */ 5878 bbr_adjust_for_hw_pacing(bbr, cts); 5879 } 5880 5881 static void 5882 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len, 5883 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts, 5884 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc, 5885 struct sockbuf *sb) 5886 { 5887 5888 struct bbr_sendmap *rsm, *nrsm; 5889 register uint32_t snd_max, snd_una; 5890 uint32_t pacing_time; 5891 /* 5892 * Add to the RACK log of packets in flight or retransmitted. If 5893 * there is a TS option we will use the TS echoed, if not we will 5894 * grab a TS. 5895 * 5896 * Retransmissions will increment the count and move the ts to its 5897 * proper place. Note that if options do not include TS's then we 5898 * won't be able to effectively use the ACK for an RTT on a retran. 5899 * 5900 * Notes about r_start and r_end. Lets consider a send starting at 5901 * sequence 1 for 10 bytes. In such an example the r_start would be 5902 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 5903 * This means that r_end is actually the first sequence for the next 5904 * slot (11). 5905 * 5906 */ 5907 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5908 if (err) { 5909 /* 5910 * We don't log errors -- we could but snd_max does not 5911 * advance in this case either. 5912 */ 5913 return; 5914 } 5915 if (th_flags & TH_RST) { 5916 /* 5917 * We don't log resets and we return immediately from 5918 * sending 5919 */ 5920 *abandon = 1; 5921 return; 5922 } 5923 snd_una = tp->snd_una; 5924 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) { 5925 /* 5926 * The call to bbr_log_output is made before bumping 5927 * snd_max. This means we can record one extra byte on a SYN 5928 * or FIN if seq_out is adding more on and a FIN is present 5929 * (and we are not resending). 5930 */ 5931 if ((th_flags & TH_SYN) && (tp->iss == seq_out)) 5932 len++; 5933 if (th_flags & TH_FIN) 5934 len++; 5935 } 5936 if (SEQ_LEQ((seq_out + len), snd_una)) { 5937 /* Are sending an old segment to induce an ack (keep-alive)? */ 5938 return; 5939 } 5940 if (SEQ_LT(seq_out, snd_una)) { 5941 /* huh? should we panic? */ 5942 uint32_t end; 5943 5944 end = seq_out + len; 5945 seq_out = snd_una; 5946 len = end - seq_out; 5947 } 5948 snd_max = tp->snd_max; 5949 if (len == 0) { 5950 /* We don't log zero window probes */ 5951 return; 5952 } 5953 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1); 5954 /* First question is it a retransmission? */ 5955 if (seq_out == snd_max) { 5956 again: 5957 rsm = bbr_alloc(bbr); 5958 if (rsm == NULL) { 5959 return; 5960 } 5961 rsm->r_flags = 0; 5962 if (th_flags & TH_SYN) 5963 rsm->r_flags |= BBR_HAS_SYN; 5964 if (th_flags & TH_FIN) 5965 rsm->r_flags |= BBR_HAS_FIN; 5966 rsm->r_tim_lastsent[0] = cts; 5967 rsm->r_rtr_cnt = 1; 5968 rsm->r_rtr_bytes = 0; 5969 rsm->r_start = seq_out; 5970 rsm->r_end = rsm->r_start + len; 5971 rsm->r_dupack = 0; 5972 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5973 rsm->r_pacing_delay = pacing_time; 5974 rsm->r_ts_valid = bbr->rc_ts_valid; 5975 if (bbr->rc_ts_valid) 5976 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5977 rsm->r_del_time = bbr->r_ctl.rc_del_time; 5978 if (bbr->r_ctl.r_app_limited_until) 5979 rsm->r_app_limited = 1; 5980 else 5981 rsm->r_app_limited = 0; 5982 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5983 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5984 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5985 /* 5986 * Here we must also add in this rsm since snd_max 5987 * is updated after we return from a new send. 5988 */ 5989 rsm->r_flight_at_send += len; 5990 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 5991 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5992 rsm->r_in_tmap = 1; 5993 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5994 rsm->r_bbr_state = bbr_state_val(bbr); 5995 else 5996 rsm->r_bbr_state = 8; 5997 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5998 rsm->r_is_gain = 1; 5999 rsm->r_is_drain = 0; 6000 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 6001 rsm->r_is_drain = 1; 6002 rsm->r_is_gain = 0; 6003 } else { 6004 rsm->r_is_drain = 0; 6005 rsm->r_is_gain = 0; 6006 } 6007 return; 6008 } 6009 /* 6010 * If we reach here its a retransmission and we need to find it. 6011 */ 6012 more: 6013 if (hintrsm && (hintrsm->r_start == seq_out)) { 6014 rsm = hintrsm; 6015 hintrsm = NULL; 6016 } else if (bbr->r_ctl.rc_next) { 6017 /* We have a hint from a previous run */ 6018 rsm = bbr->r_ctl.rc_next; 6019 } else { 6020 /* No hints sorry */ 6021 rsm = NULL; 6022 } 6023 if ((rsm) && (rsm->r_start == seq_out)) { 6024 /* 6025 * We used rc_next or hintrsm to retransmit, hopefully the 6026 * likely case. 6027 */ 6028 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6029 if (len == 0) { 6030 return; 6031 } else { 6032 goto more; 6033 } 6034 } 6035 /* Ok it was not the last pointer go through it the hard way. */ 6036 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6037 if (rsm->r_start == seq_out) { 6038 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6039 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 6040 if (len == 0) { 6041 return; 6042 } else { 6043 continue; 6044 } 6045 } 6046 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 6047 /* Transmitted within this piece */ 6048 /* 6049 * Ok we must split off the front and then let the 6050 * update do the rest 6051 */ 6052 nrsm = bbr_alloc_full_limit(bbr); 6053 if (nrsm == NULL) { 6054 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 6055 return; 6056 } 6057 /* 6058 * copy rsm to nrsm and then trim the front of rsm 6059 * to not include this part. 6060 */ 6061 bbr_clone_rsm(bbr, nrsm, rsm, seq_out); 6062 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 6063 if (rsm->r_in_tmap) { 6064 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6065 nrsm->r_in_tmap = 1; 6066 } 6067 rsm->r_flags &= (~BBR_HAS_FIN); 6068 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time); 6069 if (len == 0) { 6070 return; 6071 } 6072 } 6073 } 6074 /* 6075 * Hmm not found in map did they retransmit both old and on into the 6076 * new? 6077 */ 6078 if (seq_out == tp->snd_max) { 6079 goto again; 6080 } else if (SEQ_LT(seq_out, tp->snd_max)) { 6081 #ifdef BBR_INVARIANTS 6082 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 6083 seq_out, len, tp->snd_una, tp->snd_max); 6084 printf("Starting Dump of all rack entries\n"); 6085 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6086 printf("rsm:%p start:%u end:%u\n", 6087 rsm, rsm->r_start, rsm->r_end); 6088 } 6089 printf("Dump complete\n"); 6090 panic("seq_out not found rack:%p tp:%p", 6091 bbr, tp); 6092 #endif 6093 } else { 6094 #ifdef BBR_INVARIANTS 6095 /* 6096 * Hmm beyond sndmax? (only if we are using the new rtt-pack 6097 * flag) 6098 */ 6099 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 6100 seq_out, len, tp->snd_max, tp); 6101 #endif 6102 } 6103 } 6104 6105 static void 6106 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt) 6107 { 6108 /* 6109 * Collapse timeout back the cum-ack moved. 6110 */ 6111 tp->t_rxtshift = 0; 6112 tp->t_softerror = 0; 6113 } 6114 6115 static void 6116 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin) 6117 { 6118 bbr->rtt_valid = 1; 6119 bbr->r_ctl.cur_rtt = rtt_usecs; 6120 bbr->r_ctl.ts_in = tsin; 6121 if (rsm_send_time) 6122 bbr->r_ctl.cur_rtt_send_time = rsm_send_time; 6123 } 6124 6125 static void 6126 bbr_make_timestamp_determination(struct tcp_bbr *bbr) 6127 { 6128 /** 6129 * We have in our bbr control: 6130 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp). 6131 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts). 6132 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts) 6133 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time) 6134 * 6135 * Now we can calculate the time between the sends by doing: 6136 * 6137 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts 6138 * 6139 * And the peer's time between receiving them by doing: 6140 * 6141 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp 6142 * 6143 * We want to figure out if the timestamp values are in msec, 10msec or usec. 6144 * We also may find that we can't use the timestamps if say we see 6145 * that the peer_delta indicates that though we may have taken 10ms to 6146 * pace out the data, it only saw 1ms between the two packets. This would 6147 * indicate that somewhere on the path is a batching entity that is giving 6148 * out time-slices of the actual b/w. This would mean we could not use 6149 * reliably the peers timestamps. 6150 * 6151 * We expect delta > peer_delta initially. Until we figure out the 6152 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio. 6153 * If we place 1000 there then its a ms vs our usec. If we place 10000 there 6154 * then its 10ms vs our usec. If the peer is running a usec clock we would 6155 * put a 1 there. If the value is faster then ours, we will disable the 6156 * use of timestamps (though we could revist this later if we find it to be not 6157 * just an isolated one or two flows)). 6158 * 6159 * To detect the batching middle boxes we will come up with our compensation and 6160 * if with it in place, we find the peer is drastically off (by some margin) in 6161 * the smaller direction, then we will assume the worst case and disable use of timestamps. 6162 * 6163 */ 6164 uint64_t delta, peer_delta, delta_up; 6165 6166 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts; 6167 if (delta < bbr_min_usec_delta) { 6168 /* 6169 * Have not seen a min amount of time 6170 * between our send times so we can 6171 * make a determination of the timestamp 6172 * yet. 6173 */ 6174 return; 6175 } 6176 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp; 6177 if (peer_delta < bbr_min_peer_delta) { 6178 /* 6179 * We may have enough in the form of 6180 * our delta but the peers number 6181 * has not changed that much. It could 6182 * be its clock ratio is such that 6183 * we need more data (10ms tick) or 6184 * there may be other compression scenarios 6185 * going on. In any event we need the 6186 * spread to be larger. 6187 */ 6188 return; 6189 } 6190 /* Ok lets first see which way our delta is going */ 6191 if (peer_delta > delta) { 6192 /* Very unlikely, the peer without 6193 * compensation shows that it saw 6194 * the two sends arrive further apart 6195 * then we saw then in micro-seconds. 6196 */ 6197 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) { 6198 /* well it looks like the peer is a micro-second clock. */ 6199 bbr->rc_ts_clock_set = 1; 6200 bbr->r_ctl.bbr_peer_tsratio = 1; 6201 } else { 6202 bbr->rc_ts_cant_be_used = 1; 6203 bbr->rc_ts_clock_set = 1; 6204 } 6205 return; 6206 } 6207 /* Ok we know that the peer_delta is smaller than our send distance */ 6208 bbr->rc_ts_clock_set = 1; 6209 /* First question is it within the percentage that they are using usec time? */ 6210 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent; 6211 if ((peer_delta + delta_up) >= delta) { 6212 /* Its a usec clock */ 6213 bbr->r_ctl.bbr_peer_tsratio = 1; 6214 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6215 return; 6216 } 6217 /* Ok if not usec, what about 10usec (though unlikely)? */ 6218 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent; 6219 if (((peer_delta * 10) + delta_up) >= delta) { 6220 bbr->r_ctl.bbr_peer_tsratio = 10; 6221 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6222 return; 6223 } 6224 /* And what about 100usec (though again unlikely)? */ 6225 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent; 6226 if (((peer_delta * 100) + delta_up) >= delta) { 6227 bbr->r_ctl.bbr_peer_tsratio = 100; 6228 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6229 return; 6230 } 6231 /* And how about 1 msec (the most likely one)? */ 6232 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent; 6233 if (((peer_delta * 1000) + delta_up) >= delta) { 6234 bbr->r_ctl.bbr_peer_tsratio = 1000; 6235 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6236 return; 6237 } 6238 /* Ok if not msec could it be 10 msec? */ 6239 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent; 6240 if (((peer_delta * 10000) + delta_up) >= delta) { 6241 bbr->r_ctl.bbr_peer_tsratio = 10000; 6242 return; 6243 } 6244 /* If we fall down here the clock tick so slowly we can't use it */ 6245 bbr->rc_ts_cant_be_used = 1; 6246 bbr->r_ctl.bbr_peer_tsratio = 0; 6247 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6248 } 6249 6250 /* 6251 * Collect new round-trip time estimate 6252 * and update averages and current timeout. 6253 */ 6254 static void 6255 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts) 6256 { 6257 int32_t delta; 6258 uint32_t rtt, tsin; 6259 int32_t rtt_ticks; 6260 6261 if (bbr->rtt_valid == 0) 6262 /* No valid sample */ 6263 return; 6264 6265 rtt = bbr->r_ctl.cur_rtt; 6266 tsin = bbr->r_ctl.ts_in; 6267 if (bbr->rc_prtt_set_ts) { 6268 /* 6269 * We are to force feed the rttProp filter due 6270 * to an entry into PROBE_RTT. This assures 6271 * that the times are sync'd between when we 6272 * go into PROBE_RTT and the filter expiration. 6273 * 6274 * Google does not use a true filter, so they do 6275 * this implicitly since they only keep one value 6276 * and when they enter probe-rtt they update the 6277 * value to the newest rtt. 6278 */ 6279 uint32_t rtt_prop; 6280 6281 bbr->rc_prtt_set_ts = 0; 6282 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 6283 if (rtt > rtt_prop) 6284 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts); 6285 else 6286 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6287 } 6288 #ifdef STATS 6289 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt)); 6290 #endif 6291 if (bbr->rc_ack_was_delayed) 6292 rtt += bbr->r_ctl.rc_ack_hdwr_delay; 6293 6294 if (rtt < bbr->r_ctl.rc_lowest_rtt) 6295 bbr->r_ctl.rc_lowest_rtt = rtt; 6296 bbr_log_rtt_sample(bbr, rtt, tsin); 6297 if (bbr->r_init_rtt) { 6298 /* 6299 * The initial rtt is not-trusted, nuke it and lets get 6300 * our first valid measurement in. 6301 */ 6302 bbr->r_init_rtt = 0; 6303 tp->t_srtt = 0; 6304 } 6305 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) { 6306 /* 6307 * So we have not yet figured out 6308 * what the peers TSTMP value is 6309 * in (most likely ms). We need a 6310 * series of cum-ack's to determine 6311 * this reliably. 6312 */ 6313 if (bbr->rc_ack_is_cumack) { 6314 if (bbr->rc_ts_data_set) { 6315 /* Lets attempt to determine the timestamp granularity. */ 6316 bbr_make_timestamp_determination(bbr); 6317 } else { 6318 bbr->rc_ts_data_set = 1; 6319 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts; 6320 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time; 6321 } 6322 } else { 6323 /* 6324 * We have to have consecutive acks 6325 * reset any "filled" state to none. 6326 */ 6327 bbr->rc_ts_data_set = 0; 6328 } 6329 } 6330 /* Round it up */ 6331 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1))); 6332 if (rtt_ticks == 0) 6333 rtt_ticks = 1; 6334 if (tp->t_srtt != 0) { 6335 /* 6336 * srtt is stored as fixed point with 5 bits after the 6337 * binary point (i.e., scaled by 8). The following magic is 6338 * equivalent to the smoothing algorithm in rfc793 with an 6339 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 6340 * Adjust rtt to origin 0. 6341 */ 6342 6343 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT) 6344 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 6345 6346 tp->t_srtt += delta; 6347 if (tp->t_srtt <= 0) 6348 tp->t_srtt = 1; 6349 6350 /* 6351 * We accumulate a smoothed rtt variance (actually, a 6352 * smoothed mean difference), then set the retransmit timer 6353 * to smoothed rtt + 4 times the smoothed variance. rttvar 6354 * is stored as fixed point with 4 bits after the binary 6355 * point (scaled by 16). The following is equivalent to 6356 * rfc793 smoothing with an alpha of .75 (rttvar = 6357 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 6358 * wired-in beta. 6359 */ 6360 if (delta < 0) 6361 delta = -delta; 6362 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 6363 tp->t_rttvar += delta; 6364 if (tp->t_rttvar <= 0) 6365 tp->t_rttvar = 1; 6366 } else { 6367 /* 6368 * No rtt measurement yet - use the unsmoothed rtt. Set the 6369 * variance to half the rtt (so our first retransmit happens 6370 * at 3*rtt). 6371 */ 6372 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT; 6373 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1); 6374 } 6375 KMOD_TCPSTAT_INC(tcps_rttupdated); 6376 if (tp->t_rttupdated < UCHAR_MAX) 6377 tp->t_rttupdated++; 6378 #ifdef STATS 6379 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks)); 6380 #endif 6381 /* 6382 * the retransmit should happen at rtt + 4 * rttvar. Because of the 6383 * way we do the smoothing, srtt and rttvar will each average +1/2 6384 * tick of bias. When we compute the retransmit timer, we want 1/2 6385 * tick of rounding and 1 extra tick because of +-1/2 tick 6386 * uncertainty in the firing of the timer. The bias will give us 6387 * exactly the 1.5 tick we need. But, because the bias is 6388 * statistical, we have to test that we don't drop below the minimum 6389 * feasible timer (which is 2 ticks). 6390 */ 6391 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 6392 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2), 6393 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 6394 6395 /* 6396 * We received an ack for a packet that wasn't retransmitted; it is 6397 * probably safe to discard any error indications we've received 6398 * recently. This isn't quite right, but close enough for now (a 6399 * route might have failed after we sent a segment, and the return 6400 * path might not be symmetrical). 6401 */ 6402 tp->t_softerror = 0; 6403 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 6404 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt) 6405 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt; 6406 } 6407 6408 static void 6409 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line) 6410 { 6411 bbr->r_ctl.rc_rtt_shrinks = cts; 6412 if (bbr_can_force_probertt && 6413 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 6414 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 6415 /* 6416 * We should enter probe-rtt its been too long 6417 * since we have been there. 6418 */ 6419 bbr_enter_probe_rtt(bbr, cts, __LINE__); 6420 } else 6421 bbr_check_probe_rtt_limits(bbr, cts); 6422 } 6423 6424 static void 6425 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts) 6426 { 6427 uint64_t orig_bw; 6428 6429 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) { 6430 /* We never apply a zero measurement */ 6431 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0, 6432 0, 0, 0, 0, 0, 0); 6433 return; 6434 } 6435 if (bbr->r_ctl.r_measurement_count < 0xffffffff) 6436 bbr->r_ctl.r_measurement_count++; 6437 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 6438 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch); 6439 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw, 6440 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate), 6441 0, 0, 0, 0, 0, 0); 6442 if (orig_bw && 6443 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) { 6444 if (bbr->bbr_hdrw_pacing) { 6445 /* 6446 * Apply a new rate to the hardware 6447 * possibly. 6448 */ 6449 bbr_update_hardware_pacing_rate(bbr, cts); 6450 } 6451 bbr_set_state_target(bbr, __LINE__); 6452 tcp_bbr_tso_size_check(bbr, cts); 6453 if (bbr->r_recovery_bw) { 6454 bbr_setup_red_bw(bbr, cts); 6455 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW); 6456 } 6457 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate)) 6458 tcp_bbr_tso_size_check(bbr, cts); 6459 } 6460 6461 static void 6462 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6463 { 6464 if (bbr->rc_in_persist == 0) { 6465 /* We log only when not in persist */ 6466 /* Translate to a Bytes Per Second */ 6467 uint64_t tim, bw, ts_diff, ts_bw; 6468 uint32_t delivered; 6469 6470 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6471 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6472 else 6473 tim = 1; 6474 /* 6475 * Now that we have processed the tim (skipping the sample 6476 * or possibly updating the time, go ahead and 6477 * calculate the cdr. 6478 */ 6479 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6480 bw = (uint64_t)delivered; 6481 bw *= (uint64_t)USECS_IN_SECOND; 6482 bw /= tim; 6483 if (bw == 0) { 6484 /* We must have a calculatable amount */ 6485 return; 6486 } 6487 /* 6488 * If we are using this b/w shove it in now so we 6489 * can see in the trace viewer if it gets over-ridden. 6490 */ 6491 if (rsm->r_ts_valid && 6492 bbr->rc_ts_valid && 6493 bbr->rc_ts_clock_set && 6494 (bbr->rc_ts_cant_be_used == 0) && 6495 bbr->rc_use_ts_limit) { 6496 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1); 6497 ts_diff *= bbr->r_ctl.bbr_peer_tsratio; 6498 if ((delivered == 0) || 6499 (rtt < 1000)) { 6500 /* Can't use the ts */ 6501 bbr_log_type_bbrupd(bbr, 61, cts, 6502 ts_diff, 6503 bbr->r_ctl.last_inbound_ts, 6504 rsm->r_del_ack_ts, 0, 6505 0, 0, 0, delivered); 6506 } else { 6507 ts_bw = (uint64_t)delivered; 6508 ts_bw *= (uint64_t)USECS_IN_SECOND; 6509 ts_bw /= ts_diff; 6510 bbr_log_type_bbrupd(bbr, 62, cts, 6511 (ts_bw >> 32), 6512 (ts_bw & 0xffffffff), 0, 0, 6513 0, 0, ts_diff, delivered); 6514 if ((bbr->ts_can_raise) && 6515 (ts_bw > bw)) { 6516 bbr_log_type_bbrupd(bbr, 8, cts, 6517 delivered, 6518 ts_diff, 6519 (bw >> 32), 6520 (bw & 0x00000000ffffffff), 6521 0, 0, 0, 0); 6522 bw = ts_bw; 6523 } else if (ts_bw && (ts_bw < bw)) { 6524 bbr_log_type_bbrupd(bbr, 7, cts, 6525 delivered, 6526 ts_diff, 6527 (bw >> 32), 6528 (bw & 0x00000000ffffffff), 6529 0, 0, 0, 0); 6530 bw = ts_bw; 6531 } 6532 } 6533 } 6534 if (rsm->r_first_sent_time && 6535 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6536 uint64_t sbw, sti; 6537 /* 6538 * We use what was in flight at the time of our 6539 * send and the size of this send to figure 6540 * out what we have been sending at (amount). 6541 * For the time we take from the time of 6542 * the send of the first send outstanding 6543 * until this send plus this sends pacing 6544 * time. This gives us a good calculation 6545 * as to the rate we have been sending at. 6546 */ 6547 6548 sbw = (uint64_t)(rsm->r_flight_at_send); 6549 sbw *= (uint64_t)USECS_IN_SECOND; 6550 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6551 sti += rsm->r_pacing_delay; 6552 sbw /= sti; 6553 if (sbw < bw) { 6554 bbr_log_type_bbrupd(bbr, 6, cts, 6555 delivered, 6556 (uint32_t)sti, 6557 (bw >> 32), 6558 (uint32_t)bw, 6559 rsm->r_first_sent_time, 0, (sbw >> 32), 6560 (uint32_t)sbw); 6561 bw = sbw; 6562 } 6563 } 6564 /* Use the google algorithm for b/w measurements */ 6565 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6566 if ((rsm->r_app_limited == 0) || 6567 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) { 6568 tcp_bbr_commit_bw(bbr, cts); 6569 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6570 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6571 } 6572 } 6573 } 6574 6575 static void 6576 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6577 { 6578 if (bbr->rc_in_persist == 0) { 6579 /* We log only when not in persist */ 6580 /* Translate to a Bytes Per Second */ 6581 uint64_t tim, bw; 6582 uint32_t delivered; 6583 int no_apply = 0; 6584 6585 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6586 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6587 else 6588 tim = 1; 6589 /* 6590 * Now that we have processed the tim (skipping the sample 6591 * or possibly updating the time, go ahead and 6592 * calculate the cdr. 6593 */ 6594 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6595 bw = (uint64_t)delivered; 6596 bw *= (uint64_t)USECS_IN_SECOND; 6597 bw /= tim; 6598 if (tim < bbr->r_ctl.rc_lowest_rtt) { 6599 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6600 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6601 6602 no_apply = 1; 6603 } 6604 /* 6605 * If we are using this b/w shove it in now so we 6606 * can see in the trace viewer if it gets over-ridden. 6607 */ 6608 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6609 /* Gate by the sending rate */ 6610 if (rsm->r_first_sent_time && 6611 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6612 uint64_t sbw, sti; 6613 /* 6614 * We use what was in flight at the time of our 6615 * send and the size of this send to figure 6616 * out what we have been sending at (amount). 6617 * For the time we take from the time of 6618 * the send of the first send outstanding 6619 * until this send plus this sends pacing 6620 * time. This gives us a good calculation 6621 * as to the rate we have been sending at. 6622 */ 6623 6624 sbw = (uint64_t)(rsm->r_flight_at_send); 6625 sbw *= (uint64_t)USECS_IN_SECOND; 6626 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6627 sti += rsm->r_pacing_delay; 6628 sbw /= sti; 6629 if (sbw < bw) { 6630 bbr_log_type_bbrupd(bbr, 6, cts, 6631 delivered, 6632 (uint32_t)sti, 6633 (bw >> 32), 6634 (uint32_t)bw, 6635 rsm->r_first_sent_time, 0, (sbw >> 32), 6636 (uint32_t)sbw); 6637 bw = sbw; 6638 } 6639 if ((sti > tim) && 6640 (sti < bbr->r_ctl.rc_lowest_rtt)) { 6641 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6642 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6643 no_apply = 1; 6644 } else 6645 no_apply = 0; 6646 } 6647 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6648 if ((no_apply == 0) && 6649 ((rsm->r_app_limited == 0) || 6650 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) { 6651 tcp_bbr_commit_bw(bbr, cts); 6652 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6653 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6654 } 6655 } 6656 } 6657 6658 static void 6659 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin, 6660 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to) 6661 { 6662 uint64_t old_rttprop; 6663 6664 /* Update our delivery time and amount */ 6665 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start); 6666 bbr->r_ctl.rc_del_time = cts; 6667 if (rtt == 0) { 6668 /* 6669 * 0 means its a retransmit, for now we don't use these for 6670 * the rest of BBR. 6671 */ 6672 return; 6673 } 6674 if ((bbr->rc_use_google == 0) && 6675 (match != BBR_RTT_BY_EXACTMATCH) && 6676 (match != BBR_RTT_BY_TIMESTAMP)){ 6677 /* 6678 * We get a lot of rtt updates, lets not pay attention to 6679 * any that are not an exact match. That way we don't have 6680 * to worry about timestamps and the whole nonsense of 6681 * unsure if its a retransmission etc (if we ever had the 6682 * timestamp fixed to always have the last thing sent this 6683 * would not be a issue). 6684 */ 6685 return; 6686 } 6687 if ((bbr_no_retran && bbr->rc_use_google) && 6688 (match != BBR_RTT_BY_EXACTMATCH) && 6689 (match != BBR_RTT_BY_TIMESTAMP)){ 6690 /* 6691 * We only do measurements in google mode 6692 * with bbr_no_retran on for sure things. 6693 */ 6694 return; 6695 } 6696 /* Only update srtt if we know by exact match */ 6697 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin); 6698 if (ack_type == BBR_CUM_ACKED) 6699 bbr->rc_ack_is_cumack = 1; 6700 else 6701 bbr->rc_ack_is_cumack = 0; 6702 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP); 6703 /* 6704 * Note the following code differs to the original 6705 * BBR spec. It calls for <= not <. However after a 6706 * long discussion in email with Neal, he acknowledged 6707 * that it should be < than so that we will have flows 6708 * going into probe-rtt (we were seeing cases where that 6709 * did not happen and caused ugly things to occur). We 6710 * have added this agreed upon fix to our code base. 6711 */ 6712 if (rtt < old_rttprop) { 6713 /* Update when we last saw a rtt drop */ 6714 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0); 6715 bbr_set_reduced_rtt(bbr, cts, __LINE__); 6716 } 6717 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts, 6718 match, rsm->r_start, rsm->r_flags); 6719 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6720 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) { 6721 /* 6722 * The RTT-prop moved, reset the target (may be a 6723 * nop for some states). 6724 */ 6725 bbr_set_state_target(bbr, __LINE__); 6726 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) 6727 bbr_log_rtt_shrinks(bbr, cts, 0, 0, 6728 __LINE__, BBR_RTTS_NEW_TARGET, 0); 6729 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP)) 6730 /* It went up */ 6731 bbr_check_probe_rtt_limits(bbr, cts); 6732 } 6733 if ((bbr->rc_use_google == 0) && 6734 (match == BBR_RTT_BY_TIMESTAMP)) { 6735 /* 6736 * We don't do b/w update with 6737 * these since they are not really 6738 * reliable. 6739 */ 6740 return; 6741 } 6742 if (bbr->r_ctl.r_app_limited_until && 6743 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) { 6744 /* We are no longer app-limited */ 6745 bbr->r_ctl.r_app_limited_until = 0; 6746 } 6747 if (bbr->rc_use_google) { 6748 bbr_google_measurement(bbr, rsm, rtt, cts); 6749 } else { 6750 bbr_nf_measurement(bbr, rsm, rtt, cts); 6751 } 6752 } 6753 6754 /* 6755 * Convert a timestamp that the main stack 6756 * uses (milliseconds) into one that bbr uses 6757 * (microseconds). Return that converted timestamp. 6758 */ 6759 static uint32_t 6760 bbr_ts_convert(uint32_t cts) { 6761 uint32_t sec, msec; 6762 6763 sec = cts / MS_IN_USEC; 6764 msec = cts - (MS_IN_USEC * sec); 6765 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC)); 6766 } 6767 6768 /* 6769 * Return 0 if we did not update the RTT time, return 6770 * 1 if we did. 6771 */ 6772 static int 6773 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, 6774 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack) 6775 { 6776 int32_t i; 6777 uint32_t t, uts = 0; 6778 6779 if ((rsm->r_flags & BBR_ACKED) || 6780 (rsm->r_flags & BBR_WAS_RENEGED) || 6781 (rsm->r_flags & BBR_RXT_CLEARED)) { 6782 /* Already done */ 6783 return (0); 6784 } 6785 if (rsm->r_rtt_not_allowed) { 6786 /* Not allowed */ 6787 return (0); 6788 } 6789 if (rsm->r_rtr_cnt == 1) { 6790 /* 6791 * Only one transmit. Hopefully the normal case. 6792 */ 6793 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0])) 6794 t = cts - rsm->r_tim_lastsent[0]; 6795 else 6796 t = 1; 6797 if ((int)t <= 0) 6798 t = 1; 6799 bbr->r_ctl.rc_last_rtt = t; 6800 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6801 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to); 6802 return (1); 6803 } 6804 /* Convert to usecs */ 6805 if ((bbr_can_use_ts_for_rtt == 1) && 6806 (bbr->rc_use_google == 1) && 6807 (ack_type == BBR_CUM_ACKED) && 6808 (to->to_flags & TOF_TS) && 6809 (to->to_tsecr != 0)) { 6810 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr; 6811 if (t < 1) 6812 t = 1; 6813 t *= MS_IN_USEC; 6814 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6815 BBR_RTT_BY_TIMESTAMP, 6816 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)], 6817 ack_type, to); 6818 return (1); 6819 } 6820 uts = bbr_ts_convert(to->to_tsecr); 6821 if ((to->to_flags & TOF_TS) && 6822 (to->to_tsecr != 0) && 6823 (ack_type == BBR_CUM_ACKED) && 6824 ((rsm->r_flags & BBR_OVERMAX) == 0)) { 6825 /* 6826 * Now which timestamp does it match? In this block the ACK 6827 * may be coming from a previous transmission. 6828 */ 6829 uint32_t fudge; 6830 6831 fudge = BBR_TIMER_FUDGE; 6832 for (i = 0; i < rsm->r_rtr_cnt; i++) { 6833 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) && 6834 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) { 6835 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6836 t = cts - rsm->r_tim_lastsent[i]; 6837 else 6838 t = 1; 6839 if ((int)t <= 0) 6840 t = 1; 6841 bbr->r_ctl.rc_last_rtt = t; 6842 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING, 6843 rsm->r_tim_lastsent[i], ack_type, to); 6844 if ((i + 1) < rsm->r_rtr_cnt) { 6845 /* Likely */ 6846 return (0); 6847 } else if (rsm->r_flags & BBR_TLP) { 6848 bbr->rc_tlp_rtx_out = 0; 6849 } 6850 return (1); 6851 } 6852 } 6853 /* Fall through if we can't find a matching timestamp */ 6854 } 6855 /* 6856 * Ok its a SACK block that we retransmitted. or a windows 6857 * machine without timestamps. We can tell nothing from the 6858 * time-stamp since its not there or the time the peer last 6859 * received a segment that moved forward its cum-ack point. 6860 * 6861 * Lets look at the last retransmit and see what we can tell 6862 * (with BBR for space we only keep 2 note we have to keep 6863 * at least 2 so the map can not be condensed more). 6864 */ 6865 i = rsm->r_rtr_cnt - 1; 6866 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6867 t = cts - rsm->r_tim_lastsent[i]; 6868 else 6869 goto not_sure; 6870 if (t < bbr->r_ctl.rc_lowest_rtt) { 6871 /* 6872 * We retransmitted and the ack came back in less 6873 * than the smallest rtt we have observed in the 6874 * windowed rtt. We most likey did an improper 6875 * retransmit as outlined in 4.2 Step 3 point 2 in 6876 * the rack-draft. 6877 * 6878 * Use the prior transmission to update all the 6879 * information as long as there is only one prior 6880 * transmission. 6881 */ 6882 if ((rsm->r_flags & BBR_OVERMAX) == 0) { 6883 #ifdef BBR_INVARIANTS 6884 if (rsm->r_rtr_cnt == 1) 6885 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags); 6886 #endif 6887 i = rsm->r_rtr_cnt - 2; 6888 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6889 t = cts - rsm->r_tim_lastsent[i]; 6890 else 6891 t = 1; 6892 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET, 6893 rsm->r_tim_lastsent[i], ack_type, to); 6894 return (0); 6895 } else { 6896 /* 6897 * Too many prior transmissions, just 6898 * updated BBR delivered 6899 */ 6900 not_sure: 6901 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6902 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6903 } 6904 } else { 6905 /* 6906 * We retransmitted it and the retransmit did the 6907 * job. 6908 */ 6909 if (rsm->r_flags & BBR_TLP) 6910 bbr->rc_tlp_rtx_out = 0; 6911 if ((rsm->r_flags & BBR_OVERMAX) == 0) 6912 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, 6913 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to); 6914 else 6915 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6916 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6917 return (1); 6918 } 6919 return (0); 6920 } 6921 6922 /* 6923 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 6924 */ 6925 static void 6926 bbr_log_sack_passed(struct tcpcb *tp, 6927 struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 6928 { 6929 struct bbr_sendmap *nrsm; 6930 6931 nrsm = rsm; 6932 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap, 6933 bbr_head, r_tnext) { 6934 if (nrsm == rsm) { 6935 /* Skip original segment he is acked */ 6936 continue; 6937 } 6938 if (nrsm->r_flags & BBR_ACKED) { 6939 /* Skip ack'd segments */ 6940 continue; 6941 } 6942 if (nrsm->r_flags & BBR_SACK_PASSED) { 6943 /* 6944 * We found one that is already marked 6945 * passed, we have been here before and 6946 * so all others below this are marked. 6947 */ 6948 break; 6949 } 6950 BBR_STAT_INC(bbr_sack_passed); 6951 nrsm->r_flags |= BBR_SACK_PASSED; 6952 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) && 6953 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) { 6954 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start; 6955 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start; 6956 nrsm->r_flags |= BBR_MARKED_LOST; 6957 } 6958 nrsm->r_flags &= ~BBR_WAS_SACKPASS; 6959 } 6960 } 6961 6962 /* 6963 * Returns the number of bytes that were 6964 * newly ack'd by sack blocks. 6965 */ 6966 static uint32_t 6967 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack, 6968 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts) 6969 { 6970 int32_t times = 0; 6971 uint32_t start, end, changed = 0; 6972 struct bbr_sendmap *rsm, *nrsm; 6973 int32_t used_ref = 1; 6974 uint8_t went_back = 0, went_fwd = 0; 6975 6976 start = sack->start; 6977 end = sack->end; 6978 rsm = *prsm; 6979 if (rsm == NULL) 6980 used_ref = 0; 6981 6982 /* Do we locate the block behind where we last were? */ 6983 if (rsm && SEQ_LT(start, rsm->r_start)) { 6984 went_back = 1; 6985 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 6986 if (SEQ_GEQ(start, rsm->r_start) && 6987 SEQ_LT(start, rsm->r_end)) { 6988 goto do_rest_ofb; 6989 } 6990 } 6991 } 6992 start_at_beginning: 6993 went_fwd = 1; 6994 /* 6995 * Ok lets locate the block where this guy is fwd from rsm (if its 6996 * set) 6997 */ 6998 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) { 6999 if (SEQ_GEQ(start, rsm->r_start) && 7000 SEQ_LT(start, rsm->r_end)) { 7001 break; 7002 } 7003 } 7004 do_rest_ofb: 7005 if (rsm == NULL) { 7006 /* 7007 * This happens when we get duplicate sack blocks with the 7008 * same end. For example SACK 4: 100 SACK 3: 100 The sort 7009 * will not change there location so we would just start at 7010 * the end of the first one and get lost. 7011 */ 7012 if (tp->t_flags & TF_SENTFIN) { 7013 /* 7014 * Check to see if we have not logged the FIN that 7015 * went out. 7016 */ 7017 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7018 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 7019 /* 7020 * Ok we did not get the FIN logged. 7021 */ 7022 nrsm->r_end++; 7023 rsm = nrsm; 7024 goto do_rest_ofb; 7025 } 7026 } 7027 if (times == 1) { 7028 #ifdef BBR_INVARIANTS 7029 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p", 7030 tp, bbr, sack, to, prsm); 7031 #else 7032 goto out; 7033 #endif 7034 } 7035 times++; 7036 BBR_STAT_INC(bbr_sack_proc_restart); 7037 rsm = NULL; 7038 goto start_at_beginning; 7039 } 7040 /* Ok we have an ACK for some piece of rsm */ 7041 if (rsm->r_start != start) { 7042 /* 7043 * Need to split this in two pieces the before and after. 7044 */ 7045 if (bbr_sack_mergable(rsm, start, end)) 7046 nrsm = bbr_alloc_full_limit(bbr); 7047 else 7048 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7049 if (nrsm == NULL) { 7050 /* We could not allocate ignore the sack */ 7051 struct sackblk blk; 7052 7053 blk.start = start; 7054 blk.end = end; 7055 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7056 goto out; 7057 } 7058 bbr_clone_rsm(bbr, nrsm, rsm, start); 7059 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7060 if (rsm->r_in_tmap) { 7061 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7062 nrsm->r_in_tmap = 1; 7063 } 7064 rsm->r_flags &= (~BBR_HAS_FIN); 7065 rsm = nrsm; 7066 } 7067 if (SEQ_GEQ(end, rsm->r_end)) { 7068 /* 7069 * The end of this block is either beyond this guy or right 7070 * at this guy. 7071 */ 7072 if ((rsm->r_flags & BBR_ACKED) == 0) { 7073 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7074 changed += (rsm->r_end - rsm->r_start); 7075 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7076 bbr_log_sack_passed(tp, bbr, rsm); 7077 if (rsm->r_flags & BBR_MARKED_LOST) { 7078 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7079 } 7080 /* Is Reordering occuring? */ 7081 if (rsm->r_flags & BBR_SACK_PASSED) { 7082 BBR_STAT_INC(bbr_reorder_seen); 7083 bbr->r_ctl.rc_reorder_ts = cts; 7084 if (rsm->r_flags & BBR_MARKED_LOST) { 7085 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7086 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7087 /* LT sampling also needs adjustment */ 7088 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7089 } 7090 } 7091 rsm->r_flags |= BBR_ACKED; 7092 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7093 if (rsm->r_in_tmap) { 7094 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7095 rsm->r_in_tmap = 0; 7096 } 7097 } 7098 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7099 if (end == rsm->r_end) { 7100 /* This block only - done */ 7101 goto out; 7102 } 7103 /* There is more not coverend by this rsm move on */ 7104 start = rsm->r_end; 7105 nrsm = TAILQ_NEXT(rsm, r_next); 7106 rsm = nrsm; 7107 times = 0; 7108 goto do_rest_ofb; 7109 } 7110 if (rsm->r_flags & BBR_ACKED) { 7111 /* Been here done that */ 7112 goto out; 7113 } 7114 /* Ok we need to split off this one at the tail */ 7115 if (bbr_sack_mergable(rsm, start, end)) 7116 nrsm = bbr_alloc_full_limit(bbr); 7117 else 7118 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7119 if (nrsm == NULL) { 7120 /* failed XXXrrs what can we do but loose the sack info? */ 7121 struct sackblk blk; 7122 7123 blk.start = start; 7124 blk.end = end; 7125 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7126 goto out; 7127 } 7128 /* Clone it */ 7129 bbr_clone_rsm(bbr, nrsm, rsm, end); 7130 /* The sack block does not cover this guy fully */ 7131 rsm->r_flags &= (~BBR_HAS_FIN); 7132 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7133 if (rsm->r_in_tmap) { 7134 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7135 nrsm->r_in_tmap = 1; 7136 } 7137 nrsm->r_dupack = 0; 7138 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7139 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7140 changed += (rsm->r_end - rsm->r_start); 7141 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7142 bbr_log_sack_passed(tp, bbr, rsm); 7143 /* Is Reordering occuring? */ 7144 if (rsm->r_flags & BBR_MARKED_LOST) { 7145 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7146 } 7147 if (rsm->r_flags & BBR_SACK_PASSED) { 7148 BBR_STAT_INC(bbr_reorder_seen); 7149 bbr->r_ctl.rc_reorder_ts = cts; 7150 if (rsm->r_flags & BBR_MARKED_LOST) { 7151 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7152 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7153 /* LT sampling also needs adjustment */ 7154 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7155 } 7156 } 7157 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7158 rsm->r_flags |= BBR_ACKED; 7159 if (rsm->r_in_tmap) { 7160 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7161 rsm->r_in_tmap = 0; 7162 } 7163 out: 7164 if (rsm && (rsm->r_flags & BBR_ACKED)) { 7165 /* 7166 * Now can we merge this newly acked 7167 * block with either the previous or 7168 * next block? 7169 */ 7170 nrsm = TAILQ_NEXT(rsm, r_next); 7171 if (nrsm && 7172 (nrsm->r_flags & BBR_ACKED)) { 7173 /* yep this and next can be merged */ 7174 rsm = bbr_merge_rsm(bbr, rsm, nrsm); 7175 } 7176 /* Now what about the previous? */ 7177 nrsm = TAILQ_PREV(rsm, bbr_head, r_next); 7178 if (nrsm && 7179 (nrsm->r_flags & BBR_ACKED)) { 7180 /* yep the previous and this can be merged */ 7181 rsm = bbr_merge_rsm(bbr, nrsm, rsm); 7182 } 7183 } 7184 if (used_ref == 0) { 7185 BBR_STAT_INC(bbr_sack_proc_all); 7186 } else { 7187 BBR_STAT_INC(bbr_sack_proc_short); 7188 } 7189 if (went_fwd && went_back) { 7190 BBR_STAT_INC(bbr_sack_search_both); 7191 } else if (went_fwd) { 7192 BBR_STAT_INC(bbr_sack_search_fwd); 7193 } else if (went_back) { 7194 BBR_STAT_INC(bbr_sack_search_back); 7195 } 7196 /* Save off where the next seq is */ 7197 if (rsm) 7198 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 7199 else 7200 bbr->r_ctl.rc_sacklast = NULL; 7201 *prsm = rsm; 7202 return (changed); 7203 } 7204 7205 static void inline 7206 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack) 7207 { 7208 struct bbr_sendmap *tmap; 7209 7210 BBR_STAT_INC(bbr_reneges_seen); 7211 tmap = NULL; 7212 while (rsm && (rsm->r_flags & BBR_ACKED)) { 7213 /* Its no longer sacked, mark it so */ 7214 uint32_t oflags; 7215 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7216 #ifdef BBR_INVARIANTS 7217 if (rsm->r_in_tmap) { 7218 panic("bbr:%p rsm:%p flags:0x%x in tmap?", 7219 bbr, rsm, rsm->r_flags); 7220 } 7221 #endif 7222 oflags = rsm->r_flags; 7223 if (rsm->r_flags & BBR_MARKED_LOST) { 7224 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7225 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7226 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7227 /* LT sampling also needs adjustment */ 7228 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7229 } 7230 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST); 7231 rsm->r_flags |= BBR_WAS_RENEGED; 7232 rsm->r_flags |= BBR_RXT_CLEARED; 7233 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__); 7234 /* Rebuild it into our tmap */ 7235 if (tmap == NULL) { 7236 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7237 tmap = rsm; 7238 } else { 7239 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext); 7240 tmap = rsm; 7241 } 7242 tmap->r_in_tmap = 1; 7243 /* 7244 * XXXrrs Delivered? Should we do anything here? 7245 * 7246 * Of course we don't on a rxt timeout so maybe its ok that 7247 * we don't? 7248 * 7249 * For now lets not. 7250 */ 7251 rsm = TAILQ_NEXT(rsm, r_next); 7252 } 7253 /* 7254 * Now lets possibly clear the sack filter so we start recognizing 7255 * sacks that cover this area. 7256 */ 7257 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack); 7258 } 7259 7260 static void 7261 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to) 7262 { 7263 struct tcp_bbr *bbr; 7264 struct bbr_sendmap *rsm; 7265 uint32_t cts; 7266 7267 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7268 cts = bbr->r_ctl.rc_rcvtime; 7269 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7270 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) { 7271 if ((rsm->r_end - rsm->r_start) <= 1) { 7272 /* Log out the SYN completely */ 7273 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7274 rsm->r_rtr_bytes = 0; 7275 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7276 if (rsm->r_in_tmap) { 7277 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7278 rsm->r_in_tmap = 0; 7279 } 7280 if (bbr->r_ctl.rc_next == rsm) { 7281 /* scoot along the marker */ 7282 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7283 } 7284 if (to != NULL) 7285 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0); 7286 bbr_free(bbr, rsm); 7287 } else { 7288 /* There is more (Fast open)? strip out SYN. */ 7289 rsm->r_flags &= ~BBR_HAS_SYN; 7290 rsm->r_start++; 7291 } 7292 } 7293 } 7294 7295 /* 7296 * Returns the number of bytes that were 7297 * acknowledged by SACK blocks. 7298 */ 7299 7300 static uint32_t 7301 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, 7302 uint32_t *prev_acked) 7303 { 7304 uint32_t changed, last_seq, entered_recovery = 0; 7305 struct tcp_bbr *bbr; 7306 struct bbr_sendmap *rsm; 7307 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 7308 register uint32_t th_ack; 7309 int32_t i, j, k, new_sb, num_sack_blks = 0; 7310 uint32_t cts, acked, ack_point, sack_changed = 0; 7311 uint32_t p_maxseg, maxseg, p_acked = 0; 7312 7313 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7314 if (tcp_get_flags(th) & TH_RST) { 7315 /* We don't log resets */ 7316 return (0); 7317 } 7318 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7319 cts = bbr->r_ctl.rc_rcvtime; 7320 7321 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7322 changed = 0; 7323 maxseg = tp->t_maxseg - bbr->rc_last_options; 7324 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg); 7325 th_ack = th->th_ack; 7326 if (SEQ_GT(th_ack, tp->snd_una)) { 7327 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__); 7328 bbr->rc_tp->t_acktime = ticks; 7329 } 7330 if (SEQ_LEQ(th_ack, tp->snd_una)) { 7331 /* Only sent here for sack processing */ 7332 goto proc_sack; 7333 } 7334 if (rsm && SEQ_GT(th_ack, rsm->r_start)) { 7335 changed = th_ack - rsm->r_start; 7336 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) { 7337 /* 7338 * For the SYN incoming case we will not have called 7339 * tcp_output for the sending of the SYN, so there will be 7340 * no map. All other cases should probably be a panic. 7341 */ 7342 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) { 7343 /* 7344 * We have a timestamp that can be used to generate 7345 * an initial RTT. 7346 */ 7347 uint32_t ts, now, rtt; 7348 7349 ts = bbr_ts_convert(to->to_tsecr); 7350 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv)); 7351 rtt = now - ts; 7352 if (rtt < 1) 7353 rtt = 1; 7354 bbr_log_type_bbrrttprop(bbr, rtt, 7355 tp->iss, 0, cts, 7356 BBR_RTT_BY_TIMESTAMP, tp->iss, 0); 7357 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 7358 changed = 1; 7359 bbr->r_wanted_output = 1; 7360 goto out; 7361 } 7362 goto proc_sack; 7363 } else if (rsm == NULL) { 7364 goto out; 7365 } 7366 if (changed) { 7367 /* 7368 * The ACK point is advancing to th_ack, we must drop off 7369 * the packets in the rack log and calculate any eligble 7370 * RTT's. 7371 */ 7372 bbr->r_wanted_output = 1; 7373 more: 7374 if (rsm == NULL) { 7375 if (tp->t_flags & TF_SENTFIN) { 7376 /* if we send a FIN we will not hav a map */ 7377 goto proc_sack; 7378 } 7379 #ifdef BBR_INVARIANTS 7380 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n", 7381 tp, 7382 th, tp->t_state, bbr, 7383 tp->snd_una, tp->snd_max, changed); 7384 #endif 7385 goto proc_sack; 7386 } 7387 } 7388 if (SEQ_LT(th_ack, rsm->r_start)) { 7389 /* Huh map is missing this */ 7390 #ifdef BBR_INVARIANTS 7391 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n", 7392 rsm->r_start, 7393 th_ack, tp->t_state, 7394 bbr->r_state, bbr); 7395 panic("th-ack is bad bbr:%p tp:%p", bbr, tp); 7396 #endif 7397 goto proc_sack; 7398 } else if (th_ack == rsm->r_start) { 7399 /* None here to ack */ 7400 goto proc_sack; 7401 } 7402 /* 7403 * Clear the dup ack counter, it will 7404 * either be freed or if there is some 7405 * remaining we need to start it at zero. 7406 */ 7407 rsm->r_dupack = 0; 7408 /* Now do we consume the whole thing? */ 7409 if (SEQ_GEQ(th_ack, rsm->r_end)) { 7410 /* Its all consumed. */ 7411 uint32_t left; 7412 7413 if (rsm->r_flags & BBR_ACKED) { 7414 /* 7415 * It was acked on the scoreboard -- remove it from 7416 * total 7417 */ 7418 p_acked += (rsm->r_end - rsm->r_start); 7419 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7420 if (bbr->r_ctl.rc_sacked == 0) 7421 bbr->r_ctl.rc_sacklast = NULL; 7422 } else { 7423 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack); 7424 if (rsm->r_flags & BBR_MARKED_LOST) { 7425 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7426 } 7427 if (rsm->r_flags & BBR_SACK_PASSED) { 7428 /* 7429 * There are acked segments ACKED on the 7430 * scoreboard further up. We are seeing 7431 * reordering. 7432 */ 7433 BBR_STAT_INC(bbr_reorder_seen); 7434 bbr->r_ctl.rc_reorder_ts = cts; 7435 if (rsm->r_flags & BBR_MARKED_LOST) { 7436 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7437 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7438 /* LT sampling also needs adjustment */ 7439 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7440 } 7441 } 7442 rsm->r_flags &= ~BBR_MARKED_LOST; 7443 } 7444 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7445 rsm->r_rtr_bytes = 0; 7446 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7447 if (rsm->r_in_tmap) { 7448 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7449 rsm->r_in_tmap = 0; 7450 } 7451 if (bbr->r_ctl.rc_next == rsm) { 7452 /* scoot along the marker */ 7453 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7454 } 7455 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7456 /* Adjust the packet counts */ 7457 left = th_ack - rsm->r_end; 7458 /* Free back to zone */ 7459 bbr_free(bbr, rsm); 7460 if (left) { 7461 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7462 goto more; 7463 } 7464 goto proc_sack; 7465 } 7466 if (rsm->r_flags & BBR_ACKED) { 7467 /* 7468 * It was acked on the scoreboard -- remove it from total 7469 * for the part being cum-acked. 7470 */ 7471 p_acked += (rsm->r_end - rsm->r_start); 7472 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 7473 if (bbr->r_ctl.rc_sacked == 0) 7474 bbr->r_ctl.rc_sacklast = NULL; 7475 } else { 7476 /* 7477 * It was acked up to th_ack point for the first time 7478 */ 7479 struct bbr_sendmap lrsm; 7480 7481 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap)); 7482 lrsm.r_end = th_ack; 7483 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack); 7484 } 7485 if ((rsm->r_flags & BBR_MARKED_LOST) && 7486 ((rsm->r_flags & BBR_ACKED) == 0)) { 7487 /* 7488 * It was marked lost and partly ack'd now 7489 * for the first time. We lower the rc_lost_bytes 7490 * and still leave it MARKED. 7491 */ 7492 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start; 7493 } 7494 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7495 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7496 rsm->r_rtr_bytes = 0; 7497 /* adjust packet count */ 7498 rsm->r_start = th_ack; 7499 proc_sack: 7500 /* Check for reneging */ 7501 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7502 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) { 7503 /* 7504 * The peer has moved snd_una up to the edge of this send, 7505 * i.e. one that it had previously acked. The only way that 7506 * can be true if the peer threw away data (space issues) 7507 * that it had previously sacked (else it would have given 7508 * us snd_una up to (rsm->r_end). We need to undo the acked 7509 * markings here. 7510 * 7511 * Note we have to look to make sure th_ack is our 7512 * rsm->r_start in case we get an old ack where th_ack is 7513 * behind snd_una. 7514 */ 7515 bbr_peer_reneges(bbr, rsm, th->th_ack); 7516 } 7517 if ((to->to_flags & TOF_SACK) == 0) { 7518 /* We are done nothing left to log */ 7519 goto out; 7520 } 7521 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7522 if (rsm) { 7523 last_seq = rsm->r_end; 7524 } else { 7525 last_seq = tp->snd_max; 7526 } 7527 /* Sack block processing */ 7528 if (SEQ_GT(th_ack, tp->snd_una)) 7529 ack_point = th_ack; 7530 else 7531 ack_point = tp->snd_una; 7532 for (i = 0; i < to->to_nsacks; i++) { 7533 bcopy((to->to_sacks + i * TCPOLEN_SACK), 7534 &sack, sizeof(sack)); 7535 sack.start = ntohl(sack.start); 7536 sack.end = ntohl(sack.end); 7537 if (SEQ_GT(sack.end, sack.start) && 7538 SEQ_GT(sack.start, ack_point) && 7539 SEQ_LT(sack.start, tp->snd_max) && 7540 SEQ_GT(sack.end, ack_point) && 7541 SEQ_LEQ(sack.end, tp->snd_max)) { 7542 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) && 7543 (SEQ_LT(sack.end, last_seq)) && 7544 ((sack.end - sack.start) < (p_maxseg / 8))) { 7545 /* 7546 * Not the last piece and its smaller than 7547 * 1/8th of a p_maxseg. We ignore this. 7548 */ 7549 BBR_STAT_INC(bbr_runt_sacks); 7550 continue; 7551 } 7552 sack_blocks[num_sack_blks] = sack; 7553 num_sack_blks++; 7554 } else if (SEQ_LEQ(sack.start, th_ack) && 7555 SEQ_LEQ(sack.end, th_ack)) { 7556 /* 7557 * Its a D-SACK block. 7558 */ 7559 tcp_record_dsack(tp, sack.start, sack.end, 0); 7560 } 7561 } 7562 if (num_sack_blks == 0) 7563 goto out; 7564 /* 7565 * Sort the SACK blocks so we can update the rack scoreboard with 7566 * just one pass. 7567 */ 7568 new_sb = sack_filter_blks(tp, &bbr->r_ctl.bbr_sf, sack_blocks, 7569 num_sack_blks, th->th_ack); 7570 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks); 7571 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks); 7572 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb)); 7573 num_sack_blks = new_sb; 7574 if (num_sack_blks < 2) { 7575 goto do_sack_work; 7576 } 7577 /* Sort the sacks */ 7578 for (i = 0; i < num_sack_blks; i++) { 7579 for (j = i + 1; j < num_sack_blks; j++) { 7580 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 7581 sack = sack_blocks[i]; 7582 sack_blocks[i] = sack_blocks[j]; 7583 sack_blocks[j] = sack; 7584 } 7585 } 7586 } 7587 /* 7588 * Now are any of the sack block ends the same (yes some 7589 * implememtations send these)? 7590 */ 7591 again: 7592 if (num_sack_blks > 1) { 7593 for (i = 0; i < num_sack_blks; i++) { 7594 for (j = i + 1; j < num_sack_blks; j++) { 7595 if (sack_blocks[i].end == sack_blocks[j].end) { 7596 /* 7597 * Ok these two have the same end we 7598 * want the smallest end and then 7599 * throw away the larger and start 7600 * again. 7601 */ 7602 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 7603 /* 7604 * The second block covers 7605 * more area use that 7606 */ 7607 sack_blocks[i].start = sack_blocks[j].start; 7608 } 7609 /* 7610 * Now collapse out the dup-sack and 7611 * lower the count 7612 */ 7613 for (k = (j + 1); k < num_sack_blks; k++) { 7614 sack_blocks[j].start = sack_blocks[k].start; 7615 sack_blocks[j].end = sack_blocks[k].end; 7616 j++; 7617 } 7618 num_sack_blks--; 7619 goto again; 7620 } 7621 } 7622 } 7623 } 7624 do_sack_work: 7625 rsm = bbr->r_ctl.rc_sacklast; 7626 for (i = 0; i < num_sack_blks; i++) { 7627 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts); 7628 if (acked) { 7629 bbr->r_wanted_output = 1; 7630 changed += acked; 7631 sack_changed += acked; 7632 } 7633 } 7634 out: 7635 *prev_acked = p_acked; 7636 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 7637 /* 7638 * Ok we have a high probability that we need to go in to 7639 * recovery since we have data sack'd 7640 */ 7641 struct bbr_sendmap *rsm; 7642 7643 rsm = bbr_check_recovery_mode(tp, bbr, cts); 7644 if (rsm) { 7645 /* Enter recovery */ 7646 entered_recovery = 1; 7647 bbr->r_wanted_output = 1; 7648 /* 7649 * When we enter recovery we need to assure we send 7650 * one packet. 7651 */ 7652 if (bbr->r_ctl.rc_resend == NULL) { 7653 bbr->r_ctl.rc_resend = rsm; 7654 } 7655 } 7656 } 7657 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 7658 /* 7659 * See if we need to rack-retransmit anything if so set it 7660 * up as the thing to resend assuming something else is not 7661 * already in that position. 7662 */ 7663 if (bbr->r_ctl.rc_resend == NULL) { 7664 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 7665 } 7666 } 7667 /* 7668 * We return the amount that changed via sack, this is used by the 7669 * ack-received code to augment what was changed between th_ack <-> 7670 * snd_una. 7671 */ 7672 return (sack_changed); 7673 } 7674 7675 static void 7676 bbr_strike_dupack(struct tcp_bbr *bbr) 7677 { 7678 struct bbr_sendmap *rsm; 7679 7680 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 7681 if (rsm && (rsm->r_dupack < 0xff)) { 7682 rsm->r_dupack++; 7683 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) 7684 bbr->r_wanted_output = 1; 7685 } 7686 } 7687 7688 /* 7689 * Return value of 1, we do not need to call bbr_process_data(). 7690 * return value of 0, bbr_process_data can be called. 7691 * For ret_val if its 0 the TCB is locked and valid, if its non-zero 7692 * its unlocked and probably unsafe to touch the TCB. 7693 */ 7694 static int 7695 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 7696 struct tcpcb *tp, struct tcpopt *to, 7697 uint32_t tiwin, int32_t tlen, 7698 int32_t * ofia, int32_t thflags, int32_t * ret_val) 7699 { 7700 int32_t ourfinisacked = 0; 7701 int32_t acked_amount; 7702 uint16_t nsegs; 7703 int32_t acked; 7704 uint32_t lost, sack_changed = 0; 7705 struct mbuf *mfree; 7706 struct tcp_bbr *bbr; 7707 uint32_t prev_acked = 0; 7708 7709 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7710 lost = bbr->r_ctl.rc_lost; 7711 nsegs = max(1, m->m_pkthdr.lro_nsegs); 7712 if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) { 7713 /* Checking SEG.ACK against ISS is definitely redundant. */ 7714 tp->t_flags2 |= TF2_NO_ISS_CHECK; 7715 } 7716 if (!V_tcp_insecure_ack) { 7717 tcp_seq seq_min; 7718 bool ghost_ack_check; 7719 7720 if (tp->t_flags2 & TF2_NO_ISS_CHECK) { 7721 /* Check for too old ACKs (RFC 5961, Section 5.2). */ 7722 seq_min = tp->snd_una - tp->max_sndwnd; 7723 ghost_ack_check = false; 7724 } else { 7725 if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) { 7726 /* Checking for ghost ACKs is stricter. */ 7727 seq_min = tp->iss + 1; 7728 ghost_ack_check = true; 7729 } else { 7730 /* 7731 * Checking for too old ACKs (RFC 5961, 7732 * Section 5.2) is stricter. 7733 */ 7734 seq_min = tp->snd_una - tp->max_sndwnd; 7735 ghost_ack_check = false; 7736 } 7737 } 7738 if (SEQ_LT(th->th_ack, seq_min)) { 7739 if (ghost_ack_check) 7740 TCPSTAT_INC(tcps_rcvghostack); 7741 else 7742 TCPSTAT_INC(tcps_rcvacktooold); 7743 /* Send challenge ACK. */ 7744 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7745 bbr->r_wanted_output = 1; 7746 return (1); 7747 } 7748 } 7749 if (SEQ_GT(th->th_ack, tp->snd_max)) { 7750 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7751 bbr->r_wanted_output = 1; 7752 return (1); 7753 } 7754 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 7755 /* Process the ack */ 7756 if (bbr->rc_in_persist) 7757 tp->t_rxtshift = 0; 7758 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd)) 7759 bbr_strike_dupack(bbr); 7760 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 7761 } 7762 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost)); 7763 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 7764 /* 7765 * Old ack, behind the last one rcv'd or a duplicate ack 7766 * with SACK info. 7767 */ 7768 if (th->th_ack == tp->snd_una) { 7769 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0); 7770 if (bbr->r_state == TCPS_SYN_SENT) { 7771 /* 7772 * Special case on where we sent SYN. When 7773 * the SYN-ACK is processed in syn_sent 7774 * state it bumps the snd_una. This causes 7775 * us to hit here even though we did ack 1 7776 * byte. 7777 * 7778 * Go through the nothing left case so we 7779 * send data. 7780 */ 7781 goto nothing_left; 7782 } 7783 } 7784 return (0); 7785 } 7786 /* 7787 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 7788 * something we sent. 7789 */ 7790 if (tp->t_flags & TF_NEEDSYN) { 7791 /* 7792 * T/TCP: Connection was half-synchronized, and our SYN has 7793 * been ACK'd (so connection is now fully synchronized). Go 7794 * to non-starred state, increment snd_una for ACK of SYN, 7795 * and check if we can do window scaling. 7796 */ 7797 tp->t_flags &= ~TF_NEEDSYN; 7798 tp->snd_una++; 7799 /* Do window scaling? */ 7800 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 7801 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 7802 tp->rcv_scale = tp->request_r_scale; 7803 /* Send window already scaled. */ 7804 } 7805 } 7806 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7807 7808 acked = BYTES_THIS_ACK(tp, th); 7809 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 7810 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 7811 7812 /* 7813 * If we just performed our first retransmit, and the ACK arrives 7814 * within our recovery window, then it was a mistake to do the 7815 * retransmit in the first place. Recover our original cwnd and 7816 * ssthresh, and proceed to transmit where we left off. 7817 */ 7818 if (tp->t_flags & TF_PREVVALID) { 7819 tp->t_flags &= ~TF_PREVVALID; 7820 if (tp->t_rxtshift == 1 && 7821 (int)(ticks - tp->t_badrxtwin) < 0) 7822 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 7823 } 7824 SOCK_SENDBUF_LOCK(so); 7825 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 7826 tp->snd_wnd -= acked_amount; 7827 mfree = sbcut_locked(&so->so_snd, acked_amount); 7828 /* NB: sowwakeup_locked() does an implicit unlock. */ 7829 sowwakeup_locked(so); 7830 m_freem(mfree); 7831 if (SEQ_GT(th->th_ack, tp->snd_una)) { 7832 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 7833 } 7834 tp->snd_una = th->th_ack; 7835 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost)); 7836 if (IN_RECOVERY(tp->t_flags)) { 7837 if (SEQ_LT(th->th_ack, tp->snd_recover) && 7838 (SEQ_LT(th->th_ack, tp->snd_max))) { 7839 tcp_bbr_partialack(tp); 7840 } else { 7841 bbr_post_recovery(tp); 7842 } 7843 } 7844 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 7845 tp->snd_recover = tp->snd_una; 7846 } 7847 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 7848 tp->snd_nxt = tp->snd_max; 7849 } 7850 if (tp->snd_una == tp->snd_max) { 7851 /* Nothing left outstanding */ 7852 nothing_left: 7853 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 7854 if (sbavail(&so->so_snd) == 0) 7855 bbr->rc_tp->t_acktime = 0; 7856 if ((sbused(&so->so_snd) == 0) && 7857 (tp->t_flags & TF_SENTFIN)) { 7858 ourfinisacked = 1; 7859 } 7860 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 7861 if (bbr->rc_in_persist == 0) { 7862 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 7863 } 7864 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 7865 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 7866 /* 7867 * We invalidate the last ack here since we 7868 * don't want to transfer forward the time 7869 * for our sum's calculations. 7870 */ 7871 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7872 (sbavail(&so->so_snd) == 0) && 7873 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 7874 /* 7875 * The socket was gone and the peer sent data, time 7876 * to reset him. 7877 */ 7878 *ret_val = 1; 7879 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 7880 /* tcp_close will kill the inp pre-log the Reset */ 7881 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 7882 tp = tcp_close(tp); 7883 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 7884 BBR_STAT_INC(bbr_dropped_af_data); 7885 return (1); 7886 } 7887 /* Set need output so persist might get set */ 7888 bbr->r_wanted_output = 1; 7889 } 7890 if (ofia) 7891 *ofia = ourfinisacked; 7892 return (0); 7893 } 7894 7895 static void 7896 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7897 { 7898 if (bbr->rc_in_persist == 0) { 7899 bbr_timer_cancel(bbr, __LINE__, cts); 7900 bbr->r_ctl.rc_last_delay_val = 0; 7901 tp->t_rxtshift = 0; 7902 bbr->rc_in_persist = 1; 7903 bbr->r_ctl.rc_went_idle_time = cts; 7904 /* We should be capped when rw went to 0 but just in case */ 7905 bbr_log_type_pesist(bbr, cts, 0, line, 1); 7906 /* Time freezes for the state, so do the accounting now */ 7907 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 7908 uint32_t time_in; 7909 7910 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 7911 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7912 int32_t idx; 7913 7914 idx = bbr_state_val(bbr); 7915 counter_u64_add(bbr_state_time[(idx + 5)], time_in); 7916 } else { 7917 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 7918 } 7919 } 7920 bbr->r_ctl.rc_bbr_state_time = cts; 7921 } 7922 } 7923 7924 static void 7925 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time) 7926 { 7927 /* 7928 * Note that if idle time does not exceed our 7929 * threshold, we do nothing continuing the state 7930 * transitions we were last walking through. 7931 */ 7932 if (idle_time >= bbr_idle_restart_threshold) { 7933 if (bbr->rc_use_idle_restart) { 7934 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT; 7935 /* 7936 * Set our target using BBR_UNIT, so 7937 * we increase at a dramatic rate but 7938 * we stop when we get the pipe 7939 * full again for our current b/w estimate. 7940 */ 7941 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 7942 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 7943 bbr_set_state_target(bbr, __LINE__); 7944 /* Now setup our gains to ramp up */ 7945 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 7946 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 7947 bbr_log_type_statechange(bbr, cts, __LINE__); 7948 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7949 bbr_substate_change(bbr, cts, __LINE__, 1); 7950 } 7951 } 7952 } 7953 7954 static void 7955 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7956 { 7957 uint32_t idle_time; 7958 7959 if (bbr->rc_in_persist == 0) 7960 return; 7961 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time); 7962 bbr->rc_in_persist = 0; 7963 bbr->rc_hit_state_1 = 0; 7964 bbr->r_ctl.rc_del_time = cts; 7965 /* 7966 * We invalidate the last ack here since we 7967 * don't want to transfer forward the time 7968 * for our sum's calculations. 7969 */ 7970 if (tcp_in_hpts(bbr->rc_tp)) { 7971 tcp_hpts_remove(bbr->rc_tp); 7972 bbr->rc_timer_first = 0; 7973 bbr->r_ctl.rc_hpts_flags = 0; 7974 bbr->r_ctl.rc_last_delay_val = 0; 7975 bbr->r_ctl.rc_hptsi_agg_delay = 0; 7976 bbr->r_agg_early_set = 0; 7977 bbr->r_ctl.rc_agg_early = 0; 7978 } 7979 bbr_log_type_pesist(bbr, cts, idle_time, line, 0); 7980 if (idle_time >= bbr_rtt_probe_time) { 7981 /* 7982 * This qualifies as a RTT_PROBE session since we drop the 7983 * data outstanding to nothing and waited more than 7984 * bbr_rtt_probe_time. 7985 */ 7986 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0); 7987 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts; 7988 } 7989 tp->t_rxtshift = 0; 7990 /* 7991 * If in probeBW and we have persisted more than an RTT lets do 7992 * special handling. 7993 */ 7994 /* Force a time based epoch */ 7995 bbr_set_epoch(bbr, cts, __LINE__); 7996 /* 7997 * Setup the lost so we don't count anything against the guy 7998 * we have been stuck with during persists. 7999 */ 8000 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 8001 /* Time un-freezes for the state */ 8002 bbr->r_ctl.rc_bbr_state_time = cts; 8003 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) || 8004 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) { 8005 /* 8006 * If we are going back to probe-bw 8007 * or probe_rtt, we may need to possibly 8008 * do a fast restart. 8009 */ 8010 bbr_restart_after_idle(bbr, cts, idle_time); 8011 } 8012 } 8013 8014 static void 8015 bbr_collapsed_window(struct tcp_bbr *bbr) 8016 { 8017 /* 8018 * Now we must walk the 8019 * send map and divide the 8020 * ones left stranded. These 8021 * guys can't cause us to abort 8022 * the connection and are really 8023 * "unsent". However if a buggy 8024 * client actually did keep some 8025 * of the data i.e. collapsed the win 8026 * and refused to ack and then opened 8027 * the win and acked that data. We would 8028 * get into an ack war, the simplier 8029 * method then of just pretending we 8030 * did not send those segments something 8031 * won't work. 8032 */ 8033 struct bbr_sendmap *rsm, *nrsm; 8034 tcp_seq max_seq; 8035 uint32_t maxseg; 8036 int can_split = 0; 8037 int fnd = 0; 8038 8039 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 8040 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd; 8041 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0); 8042 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 8043 /* Find the first seq past or at maxseq */ 8044 if (rsm->r_flags & BBR_RWND_COLLAPSED) 8045 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8046 if (SEQ_GEQ(max_seq, rsm->r_start) && 8047 SEQ_GEQ(rsm->r_end, max_seq)) { 8048 fnd = 1; 8049 break; 8050 } 8051 } 8052 bbr->rc_has_collapsed = 0; 8053 if (!fnd) { 8054 /* Nothing to do strange */ 8055 return; 8056 } 8057 /* 8058 * Now can we split? 8059 * 8060 * We don't want to split if splitting 8061 * would generate too many small segments 8062 * less we let an attacker fragment our 8063 * send_map and leave us out of memory. 8064 */ 8065 if ((max_seq != rsm->r_start) && 8066 (max_seq != rsm->r_end)){ 8067 /* can we split? */ 8068 int res1, res2; 8069 8070 res1 = max_seq - rsm->r_start; 8071 res2 = rsm->r_end - max_seq; 8072 if ((res1 >= (maxseg/8)) && 8073 (res2 >= (maxseg/8))) { 8074 /* No small pieces here */ 8075 can_split = 1; 8076 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) { 8077 /* We are under the limit */ 8078 can_split = 1; 8079 } 8080 } 8081 /* Ok do we need to split this rsm? */ 8082 if (max_seq == rsm->r_start) { 8083 /* It's this guy no split required */ 8084 nrsm = rsm; 8085 } else if (max_seq == rsm->r_end) { 8086 /* It's the next one no split required. */ 8087 nrsm = TAILQ_NEXT(rsm, r_next); 8088 if (nrsm == NULL) { 8089 /* Huh? */ 8090 return; 8091 } 8092 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) { 8093 /* yep we need to split it */ 8094 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 8095 if (nrsm == NULL) { 8096 /* failed XXXrrs what can we do mark the whole? */ 8097 nrsm = rsm; 8098 goto no_split; 8099 } 8100 /* Clone it */ 8101 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0); 8102 bbr_clone_rsm(bbr, nrsm, rsm, max_seq); 8103 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 8104 if (rsm->r_in_tmap) { 8105 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8106 nrsm->r_in_tmap = 1; 8107 } 8108 } else { 8109 /* 8110 * Split not allowed just start here just 8111 * use this guy. 8112 */ 8113 nrsm = rsm; 8114 } 8115 no_split: 8116 BBR_STAT_INC(bbr_collapsed_win); 8117 /* reuse fnd as a count */ 8118 fnd = 0; 8119 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) { 8120 nrsm->r_flags |= BBR_RWND_COLLAPSED; 8121 fnd++; 8122 bbr->rc_has_collapsed = 1; 8123 } 8124 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd); 8125 } 8126 8127 static void 8128 bbr_un_collapse_window(struct tcp_bbr *bbr) 8129 { 8130 struct bbr_sendmap *rsm; 8131 int cleared = 0; 8132 8133 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 8134 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 8135 /* Clear the flag */ 8136 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8137 cleared++; 8138 } else 8139 break; 8140 } 8141 bbr_log_type_rwnd_collapse(bbr, 8142 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared); 8143 bbr->rc_has_collapsed = 0; 8144 } 8145 8146 /* 8147 * Return value of 1, the TCB is unlocked and most 8148 * likely gone, return value of 0, the TCB is still 8149 * locked. 8150 */ 8151 static int 8152 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 8153 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 8154 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 8155 { 8156 /* 8157 * Update window information. Don't look at window if no ACK: TAC's 8158 * send garbage on first SYN. 8159 */ 8160 uint16_t nsegs; 8161 int32_t tfo_syn; 8162 struct tcp_bbr *bbr; 8163 8164 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8165 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8166 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8167 if ((thflags & TH_ACK) && 8168 (SEQ_LT(tp->snd_wl1, th->th_seq) || 8169 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 8170 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 8171 /* keep track of pure window updates */ 8172 if (tlen == 0 && 8173 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 8174 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 8175 tp->snd_wnd = tiwin; 8176 tp->snd_wl1 = th->th_seq; 8177 tp->snd_wl2 = th->th_ack; 8178 if (tp->snd_wnd > tp->max_sndwnd) 8179 tp->max_sndwnd = tp->snd_wnd; 8180 bbr->r_wanted_output = 1; 8181 } else if (thflags & TH_ACK) { 8182 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 8183 tp->snd_wnd = tiwin; 8184 tp->snd_wl1 = th->th_seq; 8185 tp->snd_wl2 = th->th_ack; 8186 } 8187 } 8188 if (tp->snd_wnd < ctf_outstanding(tp)) 8189 /* The peer collapsed its window on us */ 8190 bbr_collapsed_window(bbr); 8191 else if (bbr->rc_has_collapsed) 8192 bbr_un_collapse_window(bbr); 8193 /* Was persist timer active and now we have window space? */ 8194 if ((bbr->rc_in_persist != 0) && 8195 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8196 bbr_minseg(bbr)))) { 8197 /* 8198 * Make the rate persist at end of persist mode if idle long 8199 * enough 8200 */ 8201 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8202 8203 /* Make sure we output to start the timer */ 8204 bbr->r_wanted_output = 1; 8205 } 8206 /* Do we need to enter persist? */ 8207 if ((bbr->rc_in_persist == 0) && 8208 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8209 TCPS_HAVEESTABLISHED(tp->t_state) && 8210 (tp->snd_max == tp->snd_una) && 8211 sbavail(&so->so_snd) && 8212 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8213 /* No send window.. we must enter persist */ 8214 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8215 } 8216 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 8217 m_freem(m); 8218 return (0); 8219 } 8220 /* 8221 * We don't support urgent data but 8222 * drag along the up just to make sure 8223 * if there is a stack switch no one 8224 * is surprised. 8225 */ 8226 tp->rcv_up = tp->rcv_nxt; 8227 8228 /* 8229 * Process the segment text, merging it into the TCP sequencing 8230 * queue, and arranging for acknowledgment of receipt if necessary. 8231 * This process logically involves adjusting tp->rcv_wnd as data is 8232 * presented to the user (this happens in tcp_usrreq.c, case 8233 * PRU_RCVD). If a FIN has already been received on this connection 8234 * then we just ignore the text. 8235 */ 8236 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 8237 (tp->t_flags & TF_FASTOPEN)); 8238 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 8239 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8240 tcp_seq save_start = th->th_seq; 8241 tcp_seq save_rnxt = tp->rcv_nxt; 8242 int save_tlen = tlen; 8243 8244 m_adj(m, drop_hdrlen); /* delayed header drop */ 8245 /* 8246 * Insert segment which includes th into TCP reassembly 8247 * queue with control block tp. Set thflags to whether 8248 * reassembly now includes a segment with FIN. This handles 8249 * the common case inline (segment is the next to be 8250 * received on an established connection, and the queue is 8251 * empty), avoiding linkage into and removal from the queue 8252 * and repetition of various conversions. Set DELACK for 8253 * segments received in order, but ack immediately when 8254 * segments are out of order (so fast retransmit can work). 8255 */ 8256 if (th->th_seq == tp->rcv_nxt && 8257 SEGQ_EMPTY(tp) && 8258 (TCPS_HAVEESTABLISHED(tp->t_state) || 8259 tfo_syn)) { 8260 #ifdef NETFLIX_SB_LIMITS 8261 u_int mcnt, appended; 8262 8263 if (so->so_rcv.sb_shlim) { 8264 mcnt = m_memcnt(m); 8265 appended = 0; 8266 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8267 CFO_NOSLEEP, NULL) == false) { 8268 counter_u64_add(tcp_sb_shlim_fails, 1); 8269 m_freem(m); 8270 return (0); 8271 } 8272 } 8273 8274 #endif 8275 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) { 8276 bbr->bbr_segs_rcvd += max(1, nsegs); 8277 tp->t_flags |= TF_DELACK; 8278 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8279 } else { 8280 bbr->r_wanted_output = 1; 8281 tp->t_flags |= TF_ACKNOW; 8282 } 8283 tp->rcv_nxt += tlen; 8284 if (tlen && 8285 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8286 (tp->t_fbyte_in == 0)) { 8287 tp->t_fbyte_in = ticks; 8288 if (tp->t_fbyte_in == 0) 8289 tp->t_fbyte_in = 1; 8290 if (tp->t_fbyte_out && tp->t_fbyte_in) 8291 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8292 } 8293 thflags = tcp_get_flags(th) & TH_FIN; 8294 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8295 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8296 SOCK_RECVBUF_LOCK(so); 8297 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 8298 m_freem(m); 8299 else 8300 #ifdef NETFLIX_SB_LIMITS 8301 appended = 8302 #endif 8303 sbappendstream_locked(&so->so_rcv, m, 0); 8304 /* NB: sorwakeup_locked() does an implicit unlock. */ 8305 sorwakeup_locked(so); 8306 #ifdef NETFLIX_SB_LIMITS 8307 if (so->so_rcv.sb_shlim && appended != mcnt) 8308 counter_fo_release(so->so_rcv.sb_shlim, 8309 mcnt - appended); 8310 #endif 8311 8312 } else { 8313 /* 8314 * XXX: Due to the header drop above "th" is 8315 * theoretically invalid by now. Fortunately 8316 * m_adj() doesn't actually frees any mbufs when 8317 * trimming from the head. 8318 */ 8319 tcp_seq temp = save_start; 8320 8321 thflags = tcp_reass(tp, th, &temp, &tlen, m); 8322 tp->t_flags |= TF_ACKNOW; 8323 if (tp->t_flags & TF_WAKESOR) { 8324 tp->t_flags &= ~TF_WAKESOR; 8325 /* NB: sorwakeup_locked() does an implicit unlock. */ 8326 sorwakeup_locked(so); 8327 } 8328 } 8329 if ((tp->t_flags & TF_SACK_PERMIT) && 8330 (save_tlen > 0) && 8331 TCPS_HAVEESTABLISHED(tp->t_state)) { 8332 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 8333 /* 8334 * DSACK actually handled in the fastpath 8335 * above. 8336 */ 8337 tcp_update_sack_list(tp, save_start, 8338 save_start + save_tlen); 8339 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 8340 if ((tp->rcv_numsacks >= 1) && 8341 (tp->sackblks[0].end == save_start)) { 8342 /* 8343 * Partial overlap, recorded at todrop 8344 * above. 8345 */ 8346 tcp_update_sack_list(tp, 8347 tp->sackblks[0].start, 8348 tp->sackblks[0].end); 8349 } else { 8350 tcp_update_dsack_list(tp, save_start, 8351 save_start + save_tlen); 8352 } 8353 } else if (tlen >= save_tlen) { 8354 /* Update of sackblks. */ 8355 tcp_update_dsack_list(tp, save_start, 8356 save_start + save_tlen); 8357 } else if (tlen > 0) { 8358 tcp_update_dsack_list(tp, save_start, 8359 save_start + tlen); 8360 } 8361 } 8362 } else { 8363 m_freem(m); 8364 thflags &= ~TH_FIN; 8365 } 8366 8367 /* 8368 * If FIN is received ACK the FIN and let the user know that the 8369 * connection is closing. 8370 */ 8371 if (thflags & TH_FIN) { 8372 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8373 /* The socket upcall is handled by socantrcvmore. */ 8374 socantrcvmore(so); 8375 /* 8376 * If connection is half-synchronized (ie NEEDSYN 8377 * flag on) then delay ACK, so it may be piggybacked 8378 * when SYN is sent. Otherwise, since we received a 8379 * FIN then no more input can be expected, send ACK 8380 * now. 8381 */ 8382 if (tp->t_flags & TF_NEEDSYN) { 8383 tp->t_flags |= TF_DELACK; 8384 bbr_timer_cancel(bbr, 8385 __LINE__, bbr->r_ctl.rc_rcvtime); 8386 } else { 8387 tp->t_flags |= TF_ACKNOW; 8388 } 8389 tp->rcv_nxt++; 8390 } 8391 switch (tp->t_state) { 8392 /* 8393 * In SYN_RECEIVED and ESTABLISHED STATES enter the 8394 * CLOSE_WAIT state. 8395 */ 8396 case TCPS_SYN_RECEIVED: 8397 tp->t_starttime = ticks; 8398 /* FALLTHROUGH */ 8399 case TCPS_ESTABLISHED: 8400 tcp_state_change(tp, TCPS_CLOSE_WAIT); 8401 break; 8402 8403 /* 8404 * If still in FIN_WAIT_1 STATE FIN has not been 8405 * acked so enter the CLOSING state. 8406 */ 8407 case TCPS_FIN_WAIT_1: 8408 tcp_state_change(tp, TCPS_CLOSING); 8409 break; 8410 8411 /* 8412 * In FIN_WAIT_2 state enter the TIME_WAIT state, 8413 * starting the time-wait timer, turning off the 8414 * other standard timers. 8415 */ 8416 case TCPS_FIN_WAIT_2: 8417 bbr->rc_timer_first = 1; 8418 bbr_timer_cancel(bbr, 8419 __LINE__, bbr->r_ctl.rc_rcvtime); 8420 tcp_twstart(tp); 8421 return (1); 8422 } 8423 } 8424 /* 8425 * Return any desired output. 8426 */ 8427 if ((tp->t_flags & TF_ACKNOW) || 8428 (sbavail(&so->so_snd) > ctf_outstanding(tp))) { 8429 bbr->r_wanted_output = 1; 8430 } 8431 return (0); 8432 } 8433 8434 /* 8435 * Here nothing is really faster, its just that we 8436 * have broken out the fast-data path also just like 8437 * the fast-ack. Return 1 if we processed the packet 8438 * return 0 if you need to take the "slow-path". 8439 */ 8440 static int 8441 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 8442 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8443 uint32_t tiwin, int32_t nxt_pkt) 8444 { 8445 uint16_t nsegs; 8446 int32_t newsize = 0; /* automatic sockbuf scaling */ 8447 struct tcp_bbr *bbr; 8448 #ifdef NETFLIX_SB_LIMITS 8449 u_int mcnt, appended; 8450 #endif 8451 8452 /* On the hpts and we would have called output */ 8453 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8454 8455 /* 8456 * If last ACK falls within this segment's sequence numbers, record 8457 * the timestamp. NOTE that the test is modified according to the 8458 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8459 */ 8460 if (bbr->r_ctl.rc_resend != NULL) { 8461 return (0); 8462 } 8463 if (tiwin && tiwin != tp->snd_wnd) { 8464 return (0); 8465 } 8466 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 8467 return (0); 8468 } 8469 if (__predict_false((to->to_flags & TOF_TS) && 8470 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 8471 return (0); 8472 } 8473 if (__predict_false((th->th_ack != tp->snd_una))) { 8474 return (0); 8475 } 8476 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 8477 return (0); 8478 } 8479 if ((to->to_flags & TOF_TS) != 0 && 8480 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8481 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 8482 tp->ts_recent = to->to_tsval; 8483 } 8484 /* 8485 * This is a pure, in-sequence data packet with nothing on the 8486 * reassembly queue and we have enough buffer space to take it. 8487 */ 8488 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8489 8490 #ifdef NETFLIX_SB_LIMITS 8491 if (so->so_rcv.sb_shlim) { 8492 mcnt = m_memcnt(m); 8493 appended = 0; 8494 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8495 CFO_NOSLEEP, NULL) == false) { 8496 counter_u64_add(tcp_sb_shlim_fails, 1); 8497 m_freem(m); 8498 return (1); 8499 } 8500 } 8501 #endif 8502 /* Clean receiver SACK report if present */ 8503 if (tp->rcv_numsacks) 8504 tcp_clean_sackreport(tp); 8505 KMOD_TCPSTAT_INC(tcps_preddat); 8506 tp->rcv_nxt += tlen; 8507 if (tlen && 8508 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8509 (tp->t_fbyte_in == 0)) { 8510 tp->t_fbyte_in = ticks; 8511 if (tp->t_fbyte_in == 0) 8512 tp->t_fbyte_in = 1; 8513 if (tp->t_fbyte_out && tp->t_fbyte_in) 8514 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8515 } 8516 /* 8517 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 8518 */ 8519 tp->snd_wl1 = th->th_seq; 8520 /* 8521 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 8522 */ 8523 tp->rcv_up = tp->rcv_nxt; 8524 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8525 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8526 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 8527 8528 /* Add data to socket buffer. */ 8529 SOCK_RECVBUF_LOCK(so); 8530 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8531 m_freem(m); 8532 } else { 8533 /* 8534 * Set new socket buffer size. Give up when limit is 8535 * reached. 8536 */ 8537 if (newsize) 8538 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 8539 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 8540 m_adj(m, drop_hdrlen); /* delayed header drop */ 8541 8542 #ifdef NETFLIX_SB_LIMITS 8543 appended = 8544 #endif 8545 sbappendstream_locked(&so->so_rcv, m, 0); 8546 ctf_calc_rwin(so, tp); 8547 } 8548 /* NB: sorwakeup_locked() does an implicit unlock. */ 8549 sorwakeup_locked(so); 8550 #ifdef NETFLIX_SB_LIMITS 8551 if (so->so_rcv.sb_shlim && mcnt != appended) 8552 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 8553 #endif 8554 if (DELAY_ACK(tp, bbr, nsegs)) { 8555 bbr->bbr_segs_rcvd += max(1, nsegs); 8556 tp->t_flags |= TF_DELACK; 8557 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8558 } else { 8559 bbr->r_wanted_output = 1; 8560 tp->t_flags |= TF_ACKNOW; 8561 } 8562 return (1); 8563 } 8564 8565 /* 8566 * This subfunction is used to try to highly optimize the 8567 * fast path. We again allow window updates that are 8568 * in sequence to remain in the fast-path. We also add 8569 * in the __predict's to attempt to help the compiler. 8570 * Note that if we return a 0, then we can *not* process 8571 * it and the caller should push the packet into the 8572 * slow-path. If we return 1, then all is well and 8573 * the packet is fully processed. 8574 */ 8575 static int 8576 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 8577 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8578 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 8579 { 8580 int32_t acked; 8581 uint16_t nsegs; 8582 uint32_t sack_changed; 8583 uint32_t prev_acked = 0; 8584 struct tcp_bbr *bbr; 8585 8586 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 8587 /* Old ack, behind (or duplicate to) the last one rcv'd */ 8588 return (0); 8589 } 8590 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 8591 /* Above what we have sent? */ 8592 return (0); 8593 } 8594 if (__predict_false(tiwin == 0)) { 8595 /* zero window */ 8596 return (0); 8597 } 8598 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 8599 /* We need a SYN or a FIN, unlikely.. */ 8600 return (0); 8601 } 8602 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 8603 /* Timestamp is behind .. old ack with seq wrap? */ 8604 return (0); 8605 } 8606 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 8607 /* Still recovering */ 8608 return (0); 8609 } 8610 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8611 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) { 8612 /* We are retransmitting */ 8613 return (0); 8614 } 8615 if (__predict_false(bbr->rc_in_persist != 0)) { 8616 /* In persist mode */ 8617 return (0); 8618 } 8619 if (bbr->r_ctl.rc_sacked) { 8620 /* We have sack holes on our scoreboard */ 8621 return (0); 8622 } 8623 /* Ok if we reach here, we can process a fast-ack */ 8624 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8625 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 8626 /* 8627 * We never detect loss in fast ack [we can't 8628 * have a sack and can't be in recovery so 8629 * we always pass 0 (nothing detected)]. 8630 */ 8631 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0); 8632 /* Did the window get updated? */ 8633 if (tiwin != tp->snd_wnd) { 8634 tp->snd_wnd = tiwin; 8635 tp->snd_wl1 = th->th_seq; 8636 if (tp->snd_wnd > tp->max_sndwnd) 8637 tp->max_sndwnd = tp->snd_wnd; 8638 } 8639 /* Do we need to exit persists? */ 8640 if ((bbr->rc_in_persist != 0) && 8641 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8642 bbr_minseg(bbr)))) { 8643 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8644 bbr->r_wanted_output = 1; 8645 } 8646 /* Do we need to enter persists? */ 8647 if ((bbr->rc_in_persist == 0) && 8648 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8649 TCPS_HAVEESTABLISHED(tp->t_state) && 8650 (tp->snd_max == tp->snd_una) && 8651 sbavail(&so->so_snd) && 8652 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8653 /* No send window.. we must enter persist */ 8654 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8655 } 8656 /* 8657 * If last ACK falls within this segment's sequence numbers, record 8658 * the timestamp. NOTE that the test is modified according to the 8659 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8660 */ 8661 if ((to->to_flags & TOF_TS) != 0 && 8662 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8663 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime; 8664 tp->ts_recent = to->to_tsval; 8665 } 8666 /* 8667 * This is a pure ack for outstanding data. 8668 */ 8669 KMOD_TCPSTAT_INC(tcps_predack); 8670 8671 /* 8672 * "bad retransmit" recovery. 8673 */ 8674 if (tp->t_flags & TF_PREVVALID) { 8675 tp->t_flags &= ~TF_PREVVALID; 8676 if (tp->t_rxtshift == 1 && 8677 (int)(ticks - tp->t_badrxtwin) < 0) 8678 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 8679 } 8680 /* 8681 * Recalculate the transmit timer / rtt. 8682 * 8683 * Some boxes send broken timestamp replies during the SYN+ACK 8684 * phase, ignore timestamps of 0 or we could calculate a huge RTT 8685 * and blow up the retransmit timer. 8686 */ 8687 acked = BYTES_THIS_ACK(tp, th); 8688 8689 #ifdef TCP_HHOOK 8690 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 8691 hhook_run_tcp_est_in(tp, th, to); 8692 #endif 8693 8694 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 8695 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 8696 sbdrop(&so->so_snd, acked); 8697 8698 if (SEQ_GT(th->th_ack, tp->snd_una)) 8699 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 8700 tp->snd_una = th->th_ack; 8701 if (tp->snd_wnd < ctf_outstanding(tp)) 8702 /* The peer collapsed its window on us */ 8703 bbr_collapsed_window(bbr); 8704 else if (bbr->rc_has_collapsed) 8705 bbr_un_collapse_window(bbr); 8706 8707 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 8708 tp->snd_recover = tp->snd_una; 8709 } 8710 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0); 8711 /* 8712 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 8713 */ 8714 tp->snd_wl2 = th->th_ack; 8715 m_freem(m); 8716 /* 8717 * If all outstanding data are acked, stop retransmit timer, 8718 * otherwise restart timer using current (possibly backed-off) 8719 * value. If process is waiting for space, wakeup/selwakeup/signal. 8720 * If data are ready to send, let tcp_output decide between more 8721 * output or persist. 8722 * Wake up the socket if we have room to write more. 8723 */ 8724 sowwakeup(so); 8725 if (tp->snd_una == tp->snd_max) { 8726 /* Nothing left outstanding */ 8727 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 8728 if (sbavail(&so->so_snd) == 0) 8729 bbr->rc_tp->t_acktime = 0; 8730 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8731 if (bbr->rc_in_persist == 0) { 8732 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 8733 } 8734 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 8735 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 8736 /* 8737 * We invalidate the last ack here since we 8738 * don't want to transfer forward the time 8739 * for our sum's calculations. 8740 */ 8741 bbr->r_wanted_output = 1; 8742 } 8743 if (sbavail(&so->so_snd)) { 8744 bbr->r_wanted_output = 1; 8745 } 8746 return (1); 8747 } 8748 8749 /* 8750 * Return value of 1, the TCB is unlocked and most 8751 * likely gone, return value of 0, the TCB is still 8752 * locked. 8753 */ 8754 static int 8755 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 8756 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8757 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8758 { 8759 int32_t todrop; 8760 int32_t ourfinisacked = 0; 8761 struct tcp_bbr *bbr; 8762 int32_t ret_val = 0; 8763 8764 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8765 8766 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8767 ctf_calc_rwin(so, tp); 8768 /* 8769 * If the state is SYN_SENT: if seg contains an ACK, but not for our 8770 * SYN, drop the input. if seg contains a RST, then drop the 8771 * connection. if seg does not contain SYN, then drop it. Otherwise 8772 * this is an acceptable SYN segment initialize tp->rcv_nxt and 8773 * tp->irs if seg contains ack then advance tp->snd_una. BRR does 8774 * not support ECN so we will not say we are capable. if SYN has 8775 * been acked change to ESTABLISHED else SYN_RCVD state arrange for 8776 * segment to be acked (eventually) continue processing rest of 8777 * data/controls, beginning with URG 8778 */ 8779 if ((thflags & TH_ACK) && 8780 (SEQ_LEQ(th->th_ack, tp->iss) || 8781 SEQ_GT(th->th_ack, tp->snd_max))) { 8782 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8783 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8784 return (1); 8785 } 8786 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 8787 TCP_PROBE5(connect__refused, NULL, tp, 8788 mtod(m, const char *), tp, th); 8789 tp = tcp_drop(tp, ECONNREFUSED); 8790 ctf_do_drop(m, tp); 8791 return (1); 8792 } 8793 if (thflags & TH_RST) { 8794 ctf_do_drop(m, tp); 8795 return (1); 8796 } 8797 if (!(thflags & TH_SYN)) { 8798 ctf_do_drop(m, tp); 8799 return (1); 8800 } 8801 tp->irs = th->th_seq; 8802 tcp_rcvseqinit(tp); 8803 if (thflags & TH_ACK) { 8804 int tfo_partial = 0; 8805 8806 KMOD_TCPSTAT_INC(tcps_connects); 8807 soisconnected(so); 8808 #ifdef MAC 8809 mac_socketpeer_set_from_mbuf(m, so); 8810 #endif 8811 /* Do window scaling on this connection? */ 8812 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 8813 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 8814 tp->rcv_scale = tp->request_r_scale; 8815 } 8816 tp->rcv_adv += min(tp->rcv_wnd, 8817 TCP_MAXWIN << tp->rcv_scale); 8818 /* 8819 * If not all the data that was sent in the TFO SYN 8820 * has been acked, resend the remainder right away. 8821 */ 8822 if ((tp->t_flags & TF_FASTOPEN) && 8823 (tp->snd_una != tp->snd_max)) { 8824 tp->snd_nxt = th->th_ack; 8825 tfo_partial = 1; 8826 } 8827 /* 8828 * If there's data, delay ACK; if there's also a FIN ACKNOW 8829 * will be turned on later. 8830 */ 8831 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) { 8832 bbr->bbr_segs_rcvd += 1; 8833 tp->t_flags |= TF_DELACK; 8834 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8835 } else { 8836 bbr->r_wanted_output = 1; 8837 tp->t_flags |= TF_ACKNOW; 8838 } 8839 if (SEQ_GT(th->th_ack, tp->iss)) { 8840 /* 8841 * The SYN is acked 8842 * handle it specially. 8843 */ 8844 bbr_log_syn(tp, to); 8845 } 8846 if (SEQ_GT(th->th_ack, tp->snd_una)) { 8847 /* 8848 * We advance snd_una for the 8849 * fast open case. If th_ack is 8850 * acknowledging data beyond 8851 * snd_una we can't just call 8852 * ack-processing since the 8853 * data stream in our send-map 8854 * will start at snd_una + 1 (one 8855 * beyond the SYN). If its just 8856 * equal we don't need to do that 8857 * and there is no send_map. 8858 */ 8859 tp->snd_una++; 8860 } 8861 /* 8862 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 8863 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 8864 */ 8865 tp->t_starttime = ticks; 8866 if (tp->t_flags & TF_NEEDFIN) { 8867 tcp_state_change(tp, TCPS_FIN_WAIT_1); 8868 tp->t_flags &= ~TF_NEEDFIN; 8869 thflags &= ~TH_SYN; 8870 } else { 8871 tcp_state_change(tp, TCPS_ESTABLISHED); 8872 TCP_PROBE5(connect__established, NULL, tp, 8873 mtod(m, const char *), tp, th); 8874 cc_conn_init(tp); 8875 } 8876 } else { 8877 /* 8878 * Received initial SYN in SYN-SENT[*] state => simultaneous 8879 * open. If segment contains CC option and there is a 8880 * cached CC, apply TAO test. If it succeeds, connection is * 8881 * half-synchronized. Otherwise, do 3-way handshake: 8882 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 8883 * there was no CC option, clear cached CC value. 8884 */ 8885 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 8886 tcp_state_change(tp, TCPS_SYN_RECEIVED); 8887 } 8888 /* 8889 * Advance th->th_seq to correspond to first data byte. If data, 8890 * trim to stay within window, dropping FIN if necessary. 8891 */ 8892 th->th_seq++; 8893 if (tlen > tp->rcv_wnd) { 8894 todrop = tlen - tp->rcv_wnd; 8895 m_adj(m, -todrop); 8896 tlen = tp->rcv_wnd; 8897 thflags &= ~TH_FIN; 8898 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 8899 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 8900 } 8901 tp->snd_wl1 = th->th_seq - 1; 8902 tp->rcv_up = th->th_seq; 8903 /* 8904 * Client side of transaction: already sent SYN and data. If the 8905 * remote host used T/TCP to validate the SYN, our data will be 8906 * ACK'd; if so, enter normal data segment processing in the middle 8907 * of step 5, ack processing. Otherwise, goto step 6. 8908 */ 8909 if (thflags & TH_ACK) { 8910 if ((to->to_flags & TOF_TS) != 0) { 8911 uint32_t t, rtt; 8912 8913 t = tcp_tv_to_mssectick(&bbr->rc_tv); 8914 if (TSTMP_GEQ(t, to->to_tsecr)) { 8915 rtt = t - to->to_tsecr; 8916 if (rtt == 0) { 8917 rtt = 1; 8918 } 8919 rtt *= MS_IN_USEC; 8920 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 8921 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, 8922 rtt, bbr->r_ctl.rc_rcvtime); 8923 } 8924 } 8925 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 8926 return (ret_val); 8927 /* We may have changed to FIN_WAIT_1 above */ 8928 if (tp->t_state == TCPS_FIN_WAIT_1) { 8929 /* 8930 * In FIN_WAIT_1 STATE in addition to the processing 8931 * for the ESTABLISHED state if our FIN is now 8932 * acknowledged then enter FIN_WAIT_2. 8933 */ 8934 if (ourfinisacked) { 8935 /* 8936 * If we can't receive any more data, then 8937 * closing user can proceed. Starting the 8938 * timer is contrary to the specification, 8939 * but if we don't get a FIN we'll hang 8940 * forever. 8941 * 8942 * XXXjl: we should release the tp also, and 8943 * use a compressed state. 8944 */ 8945 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8946 soisdisconnected(so); 8947 tcp_timer_activate(tp, TT_2MSL, 8948 (tcp_fast_finwait2_recycle ? 8949 tcp_finwait2_timeout : 8950 TP_MAXIDLE(tp))); 8951 } 8952 tcp_state_change(tp, TCPS_FIN_WAIT_2); 8953 } 8954 } 8955 } 8956 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 8957 tiwin, thflags, nxt_pkt)); 8958 } 8959 8960 /* 8961 * Return value of 1, the TCB is unlocked and most 8962 * likely gone, return value of 0, the TCB is still 8963 * locked. 8964 */ 8965 static int 8966 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 8967 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8968 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8969 { 8970 int32_t ourfinisacked = 0; 8971 int32_t ret_val; 8972 struct tcp_bbr *bbr; 8973 8974 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8975 8976 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8977 ctf_calc_rwin(so, tp); 8978 if ((thflags & TH_RST) || 8979 (tp->t_fin_is_rst && (thflags & TH_FIN))) 8980 return (ctf_process_rst(m, th, so, tp)); 8981 if ((thflags & TH_ACK) && 8982 (SEQ_LEQ(th->th_ack, tp->snd_una) || 8983 SEQ_GT(th->th_ack, tp->snd_max))) { 8984 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8985 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8986 return (1); 8987 } 8988 if (tp->t_flags & TF_FASTOPEN) { 8989 /* 8990 * When a TFO connection is in SYN_RECEIVED, the only valid 8991 * packets are the initial SYN, a retransmit/copy of the 8992 * initial SYN (possibly with a subset of the original 8993 * data), a valid ACK, a FIN, or a RST. 8994 */ 8995 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 8996 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8997 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8998 return (1); 8999 } else if (thflags & TH_SYN) { 9000 /* non-initial SYN is ignored */ 9001 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 9002 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 9003 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 9004 ctf_do_drop(m, NULL); 9005 return (0); 9006 } 9007 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 9008 ctf_do_drop(m, NULL); 9009 return (0); 9010 } 9011 } 9012 /* 9013 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9014 * it's less than ts_recent, drop it. 9015 */ 9016 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9017 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9018 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9019 return (ret_val); 9020 } 9021 /* 9022 * In the SYN-RECEIVED state, validate that the packet belongs to 9023 * this connection before trimming the data to fit the receive 9024 * window. Check the sequence number versus IRS since we know the 9025 * sequence numbers haven't wrapped. This is a partial fix for the 9026 * "LAND" DoS attack. 9027 */ 9028 if (SEQ_LT(th->th_seq, tp->irs)) { 9029 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 9030 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9031 return (1); 9032 } 9033 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9034 return (ret_val); 9035 } 9036 /* 9037 * If last ACK falls within this segment's sequence numbers, record 9038 * its timestamp. NOTE: 1) That the test incorporates suggestions 9039 * from the latest proposal of the tcplw@cray.com list (Braden 9040 * 1993/04/26). 2) That updating only on newer timestamps interferes 9041 * with our earlier PAWS tests, so this check should be solely 9042 * predicated on the sequence space of this segment. 3) That we 9043 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9044 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9045 * SEG.Len, This modified check allows us to overcome RFC1323's 9046 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9047 * p.869. In such cases, we can still calculate the RTT correctly 9048 * when RCV.NXT == Last.ACK.Sent. 9049 */ 9050 if ((to->to_flags & TOF_TS) != 0 && 9051 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9052 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9053 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9054 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9055 tp->ts_recent = to->to_tsval; 9056 } 9057 tp->snd_wnd = tiwin; 9058 /* 9059 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9060 * is on (half-synchronized state), then queue data for later 9061 * processing; else drop segment and return. 9062 */ 9063 if ((thflags & TH_ACK) == 0) { 9064 if (tp->t_flags & TF_FASTOPEN) { 9065 cc_conn_init(tp); 9066 } 9067 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9068 tiwin, thflags, nxt_pkt)); 9069 } 9070 KMOD_TCPSTAT_INC(tcps_connects); 9071 if (tp->t_flags & TF_SONOTCONN) { 9072 tp->t_flags &= ~TF_SONOTCONN; 9073 soisconnected(so); 9074 } 9075 /* Do window scaling? */ 9076 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 9077 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 9078 tp->rcv_scale = tp->request_r_scale; 9079 } 9080 /* 9081 * ok for the first time in lets see if we can use the ts to figure 9082 * out what the initial RTT was. 9083 */ 9084 if ((to->to_flags & TOF_TS) != 0) { 9085 uint32_t t, rtt; 9086 9087 t = tcp_tv_to_mssectick(&bbr->rc_tv); 9088 if (TSTMP_GEQ(t, to->to_tsecr)) { 9089 rtt = t - to->to_tsecr; 9090 if (rtt == 0) { 9091 rtt = 1; 9092 } 9093 rtt *= MS_IN_USEC; 9094 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 9095 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime); 9096 } 9097 } 9098 /* Drop off any SYN in the send map (probably not there) */ 9099 if (thflags & TH_ACK) 9100 bbr_log_syn(tp, to); 9101 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 9102 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 9103 tp->t_tfo_pending = NULL; 9104 } 9105 /* 9106 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 9107 * FIN-WAIT-1 9108 */ 9109 tp->t_starttime = ticks; 9110 if (tp->t_flags & TF_NEEDFIN) { 9111 tcp_state_change(tp, TCPS_FIN_WAIT_1); 9112 tp->t_flags &= ~TF_NEEDFIN; 9113 } else { 9114 tcp_state_change(tp, TCPS_ESTABLISHED); 9115 TCP_PROBE5(accept__established, NULL, tp, 9116 mtod(m, const char *), tp, th); 9117 /* 9118 * TFO connections call cc_conn_init() during SYN 9119 * processing. Calling it again here for such connections 9120 * is not harmless as it would undo the snd_cwnd reduction 9121 * that occurs when a TFO SYN|ACK is retransmitted. 9122 */ 9123 if (!(tp->t_flags & TF_FASTOPEN)) 9124 cc_conn_init(tp); 9125 } 9126 /* 9127 * Account for the ACK of our SYN prior to 9128 * regular ACK processing below, except for 9129 * simultaneous SYN, which is handled later. 9130 */ 9131 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 9132 tp->snd_una++; 9133 /* 9134 * If segment contains data or ACK, will call tcp_reass() later; if 9135 * not, do so now to pass queued data to user. 9136 */ 9137 if (tlen == 0 && (thflags & TH_FIN) == 0) { 9138 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 9139 (struct mbuf *)0); 9140 if (tp->t_flags & TF_WAKESOR) { 9141 tp->t_flags &= ~TF_WAKESOR; 9142 /* NB: sorwakeup_locked() does an implicit unlock. */ 9143 sorwakeup_locked(so); 9144 } 9145 } 9146 tp->snd_wl1 = th->th_seq - 1; 9147 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9148 return (ret_val); 9149 } 9150 if (tp->t_state == TCPS_FIN_WAIT_1) { 9151 /* We could have went to FIN_WAIT_1 (or EST) above */ 9152 /* 9153 * In FIN_WAIT_1 STATE in addition to the processing for the 9154 * ESTABLISHED state if our FIN is now acknowledged then 9155 * enter FIN_WAIT_2. 9156 */ 9157 if (ourfinisacked) { 9158 /* 9159 * If we can't receive any more data, then closing 9160 * user can proceed. Starting the timer is contrary 9161 * to the specification, but if we don't get a FIN 9162 * we'll hang forever. 9163 * 9164 * XXXjl: we should release the tp also, and use a 9165 * compressed state. 9166 */ 9167 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9168 soisdisconnected(so); 9169 tcp_timer_activate(tp, TT_2MSL, 9170 (tcp_fast_finwait2_recycle ? 9171 tcp_finwait2_timeout : 9172 TP_MAXIDLE(tp))); 9173 } 9174 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9175 } 9176 } 9177 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9178 tiwin, thflags, nxt_pkt)); 9179 } 9180 9181 /* 9182 * Return value of 1, the TCB is unlocked and most 9183 * likely gone, return value of 0, the TCB is still 9184 * locked. 9185 */ 9186 static int 9187 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 9188 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9189 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9190 { 9191 struct tcp_bbr *bbr; 9192 int32_t ret_val; 9193 9194 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9195 9196 /* 9197 * Header prediction: check for the two common cases of a 9198 * uni-directional data xfer. If the packet has no control flags, 9199 * is in-sequence, the window didn't change and we're not 9200 * retransmitting, it's a candidate. If the length is zero and the 9201 * ack moved forward, we're the sender side of the xfer. Just free 9202 * the data acked & wake any higher level process that was blocked 9203 * waiting for space. If the length is non-zero and the ack didn't 9204 * move, we're the receiver side. If we're getting packets in-order 9205 * (the reassembly queue is empty), add the data toc The socket 9206 * buffer and note that we need a delayed ack. Make sure that the 9207 * hidden state-flags are also off. Since we check for 9208 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 9209 */ 9210 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9211 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) { 9212 /* 9213 * If we have delived under 4 segments increase the initial 9214 * window if raised by the peer. We use this to determine 9215 * dynamic and static rwnd's at the end of a connection. 9216 */ 9217 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd); 9218 } 9219 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 9220 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 9221 __predict_true(SEGQ_EMPTY(tp)) && 9222 __predict_true(th->th_seq == tp->rcv_nxt)) { 9223 if (tlen == 0) { 9224 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 9225 tiwin, nxt_pkt, iptos)) { 9226 return (0); 9227 } 9228 } else { 9229 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 9230 tiwin, nxt_pkt)) { 9231 return (0); 9232 } 9233 } 9234 } 9235 ctf_calc_rwin(so, tp); 9236 9237 if ((thflags & TH_RST) || 9238 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9239 return (ctf_process_rst(m, th, so, tp)); 9240 /* 9241 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9242 * synchronized state. 9243 */ 9244 if (thflags & TH_SYN) { 9245 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9246 return (ret_val); 9247 } 9248 /* 9249 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9250 * it's less than ts_recent, drop it. 9251 */ 9252 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9253 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9254 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9255 return (ret_val); 9256 } 9257 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9258 return (ret_val); 9259 } 9260 /* 9261 * If last ACK falls within this segment's sequence numbers, record 9262 * its timestamp. NOTE: 1) That the test incorporates suggestions 9263 * from the latest proposal of the tcplw@cray.com list (Braden 9264 * 1993/04/26). 2) That updating only on newer timestamps interferes 9265 * with our earlier PAWS tests, so this check should be solely 9266 * predicated on the sequence space of this segment. 3) That we 9267 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9268 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9269 * SEG.Len, This modified check allows us to overcome RFC1323's 9270 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9271 * p.869. In such cases, we can still calculate the RTT correctly 9272 * when RCV.NXT == Last.ACK.Sent. 9273 */ 9274 if ((to->to_flags & TOF_TS) != 0 && 9275 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9276 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9277 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9278 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9279 tp->ts_recent = to->to_tsval; 9280 } 9281 /* 9282 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9283 * is on (half-synchronized state), then queue data for later 9284 * processing; else drop segment and return. 9285 */ 9286 if ((thflags & TH_ACK) == 0) { 9287 if (tp->t_flags & TF_NEEDSYN) { 9288 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9289 tiwin, thflags, nxt_pkt)); 9290 } else if (tp->t_flags & TF_ACKNOW) { 9291 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9292 bbr->r_wanted_output = 1; 9293 return (ret_val); 9294 } else { 9295 ctf_do_drop(m, NULL); 9296 return (0); 9297 } 9298 } 9299 /* 9300 * Ack processing. 9301 */ 9302 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9303 return (ret_val); 9304 } 9305 if (sbavail(&so->so_snd)) { 9306 if (ctf_progress_timeout_check(tp, true)) { 9307 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9308 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9309 return (1); 9310 } 9311 } 9312 /* State changes only happen in bbr_process_data() */ 9313 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9314 tiwin, thflags, nxt_pkt)); 9315 } 9316 9317 /* 9318 * Return value of 1, the TCB is unlocked and most 9319 * likely gone, return value of 0, the TCB is still 9320 * locked. 9321 */ 9322 static int 9323 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 9324 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9325 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9326 { 9327 struct tcp_bbr *bbr; 9328 int32_t ret_val; 9329 9330 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9331 9332 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9333 ctf_calc_rwin(so, tp); 9334 if ((thflags & TH_RST) || 9335 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9336 return (ctf_process_rst(m, th, so, tp)); 9337 /* 9338 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9339 * synchronized state. 9340 */ 9341 if (thflags & TH_SYN) { 9342 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9343 return (ret_val); 9344 } 9345 /* 9346 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9347 * it's less than ts_recent, drop it. 9348 */ 9349 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9350 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9351 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9352 return (ret_val); 9353 } 9354 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9355 return (ret_val); 9356 } 9357 /* 9358 * If last ACK falls within this segment's sequence numbers, record 9359 * its timestamp. NOTE: 1) That the test incorporates suggestions 9360 * from the latest proposal of the tcplw@cray.com list (Braden 9361 * 1993/04/26). 2) That updating only on newer timestamps interferes 9362 * with our earlier PAWS tests, so this check should be solely 9363 * predicated on the sequence space of this segment. 3) That we 9364 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9365 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9366 * SEG.Len, This modified check allows us to overcome RFC1323's 9367 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9368 * p.869. In such cases, we can still calculate the RTT correctly 9369 * when RCV.NXT == Last.ACK.Sent. 9370 */ 9371 if ((to->to_flags & TOF_TS) != 0 && 9372 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9373 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9374 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9375 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9376 tp->ts_recent = to->to_tsval; 9377 } 9378 /* 9379 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9380 * is on (half-synchronized state), then queue data for later 9381 * processing; else drop segment and return. 9382 */ 9383 if ((thflags & TH_ACK) == 0) { 9384 if (tp->t_flags & TF_NEEDSYN) { 9385 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9386 tiwin, thflags, nxt_pkt)); 9387 } else if (tp->t_flags & TF_ACKNOW) { 9388 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9389 bbr->r_wanted_output = 1; 9390 return (ret_val); 9391 } else { 9392 ctf_do_drop(m, NULL); 9393 return (0); 9394 } 9395 } 9396 /* 9397 * Ack processing. 9398 */ 9399 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9400 return (ret_val); 9401 } 9402 if (sbavail(&so->so_snd)) { 9403 if (ctf_progress_timeout_check(tp, true)) { 9404 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9405 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9406 return (1); 9407 } 9408 } 9409 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9410 tiwin, thflags, nxt_pkt)); 9411 } 9412 9413 static int 9414 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr, 9415 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so) 9416 { 9417 9418 if (bbr->rc_allow_data_af_clo == 0) { 9419 close_now: 9420 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 9421 /* tcp_close will kill the inp pre-log the Reset */ 9422 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 9423 tp = tcp_close(tp); 9424 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 9425 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 9426 return (1); 9427 } 9428 if (sbavail(&so->so_snd) == 0) 9429 goto close_now; 9430 /* Ok we allow data that is ignored and a followup reset */ 9431 tp->rcv_nxt = th->th_seq + *tlen; 9432 tp->t_flags2 |= TF2_DROP_AF_DATA; 9433 bbr->r_wanted_output = 1; 9434 *tlen = 0; 9435 return (0); 9436 } 9437 9438 /* 9439 * Return value of 1, the TCB is unlocked and most 9440 * likely gone, return value of 0, the TCB is still 9441 * locked. 9442 */ 9443 static int 9444 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 9445 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9446 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9447 { 9448 int32_t ourfinisacked = 0; 9449 int32_t ret_val; 9450 struct tcp_bbr *bbr; 9451 9452 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9453 9454 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9455 ctf_calc_rwin(so, tp); 9456 if ((thflags & TH_RST) || 9457 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9458 return (ctf_process_rst(m, th, so, tp)); 9459 /* 9460 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9461 * synchronized state. 9462 */ 9463 if (thflags & TH_SYN) { 9464 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9465 return (ret_val); 9466 } 9467 /* 9468 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9469 * it's less than ts_recent, drop it. 9470 */ 9471 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9472 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9473 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9474 return (ret_val); 9475 } 9476 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9477 return (ret_val); 9478 } 9479 /* 9480 * If new data are received on a connection after the user processes 9481 * are gone, then RST the other end. 9482 * We call a new function now so we might continue and setup 9483 * to reset at all data being ack'd. 9484 */ 9485 if ((tp->t_flags & TF_CLOSED) && tlen && 9486 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9487 return (1); 9488 /* 9489 * If last ACK falls within this segment's sequence numbers, record 9490 * its timestamp. NOTE: 1) That the test incorporates suggestions 9491 * from the latest proposal of the tcplw@cray.com list (Braden 9492 * 1993/04/26). 2) That updating only on newer timestamps interferes 9493 * with our earlier PAWS tests, so this check should be solely 9494 * predicated on the sequence space of this segment. 3) That we 9495 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9496 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9497 * SEG.Len, This modified check allows us to overcome RFC1323's 9498 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9499 * p.869. In such cases, we can still calculate the RTT correctly 9500 * when RCV.NXT == Last.ACK.Sent. 9501 */ 9502 if ((to->to_flags & TOF_TS) != 0 && 9503 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9504 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9505 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9506 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9507 tp->ts_recent = to->to_tsval; 9508 } 9509 /* 9510 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9511 * is on (half-synchronized state), then queue data for later 9512 * processing; else drop segment and return. 9513 */ 9514 if ((thflags & TH_ACK) == 0) { 9515 if (tp->t_flags & TF_NEEDSYN) { 9516 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9517 tiwin, thflags, nxt_pkt)); 9518 } else if (tp->t_flags & TF_ACKNOW) { 9519 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9520 bbr->r_wanted_output = 1; 9521 return (ret_val); 9522 } else { 9523 ctf_do_drop(m, NULL); 9524 return (0); 9525 } 9526 } 9527 /* 9528 * Ack processing. 9529 */ 9530 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9531 return (ret_val); 9532 } 9533 if (ourfinisacked) { 9534 /* 9535 * If we can't receive any more data, then closing user can 9536 * proceed. Starting the timer is contrary to the 9537 * specification, but if we don't get a FIN we'll hang 9538 * forever. 9539 * 9540 * XXXjl: we should release the tp also, and use a 9541 * compressed state. 9542 */ 9543 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9544 soisdisconnected(so); 9545 tcp_timer_activate(tp, TT_2MSL, 9546 (tcp_fast_finwait2_recycle ? 9547 tcp_finwait2_timeout : 9548 TP_MAXIDLE(tp))); 9549 } 9550 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9551 } 9552 if (sbavail(&so->so_snd)) { 9553 if (ctf_progress_timeout_check(tp, true)) { 9554 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9555 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9556 return (1); 9557 } 9558 } 9559 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9560 tiwin, thflags, nxt_pkt)); 9561 } 9562 9563 /* 9564 * Return value of 1, the TCB is unlocked and most 9565 * likely gone, return value of 0, the TCB is still 9566 * locked. 9567 */ 9568 static int 9569 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 9570 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9571 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9572 { 9573 int32_t ourfinisacked = 0; 9574 int32_t ret_val; 9575 struct tcp_bbr *bbr; 9576 9577 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9578 9579 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9580 ctf_calc_rwin(so, tp); 9581 if ((thflags & TH_RST) || 9582 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9583 return (ctf_process_rst(m, th, so, tp)); 9584 /* 9585 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9586 * synchronized state. 9587 */ 9588 if (thflags & TH_SYN) { 9589 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9590 return (ret_val); 9591 } 9592 /* 9593 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9594 * it's less than ts_recent, drop it. 9595 */ 9596 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9597 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9598 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9599 return (ret_val); 9600 } 9601 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9602 return (ret_val); 9603 } 9604 /* 9605 * If last ACK falls within this segment's sequence numbers, record 9606 * its timestamp. NOTE: 1) That the test incorporates suggestions 9607 * from the latest proposal of the tcplw@cray.com list (Braden 9608 * 1993/04/26). 2) That updating only on newer timestamps interferes 9609 * with our earlier PAWS tests, so this check should be solely 9610 * predicated on the sequence space of this segment. 3) That we 9611 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9612 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9613 * SEG.Len, This modified check allows us to overcome RFC1323's 9614 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9615 * p.869. In such cases, we can still calculate the RTT correctly 9616 * when RCV.NXT == Last.ACK.Sent. 9617 */ 9618 if ((to->to_flags & TOF_TS) != 0 && 9619 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9620 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9621 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9622 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9623 tp->ts_recent = to->to_tsval; 9624 } 9625 /* 9626 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9627 * is on (half-synchronized state), then queue data for later 9628 * processing; else drop segment and return. 9629 */ 9630 if ((thflags & TH_ACK) == 0) { 9631 if (tp->t_flags & TF_NEEDSYN) { 9632 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9633 tiwin, thflags, nxt_pkt)); 9634 } else if (tp->t_flags & TF_ACKNOW) { 9635 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9636 bbr->r_wanted_output = 1; 9637 return (ret_val); 9638 } else { 9639 ctf_do_drop(m, NULL); 9640 return (0); 9641 } 9642 } 9643 /* 9644 * Ack processing. 9645 */ 9646 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9647 return (ret_val); 9648 } 9649 if (ourfinisacked) { 9650 tcp_twstart(tp); 9651 m_freem(m); 9652 return (1); 9653 } 9654 if (sbavail(&so->so_snd)) { 9655 if (ctf_progress_timeout_check(tp, true)) { 9656 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9657 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9658 return (1); 9659 } 9660 } 9661 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9662 tiwin, thflags, nxt_pkt)); 9663 } 9664 9665 /* 9666 * Return value of 1, the TCB is unlocked and most 9667 * likely gone, return value of 0, the TCB is still 9668 * locked. 9669 */ 9670 static int 9671 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 9672 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9673 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9674 { 9675 int32_t ourfinisacked = 0; 9676 int32_t ret_val; 9677 struct tcp_bbr *bbr; 9678 9679 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9680 9681 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9682 ctf_calc_rwin(so, tp); 9683 if ((thflags & TH_RST) || 9684 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9685 return (ctf_process_rst(m, th, so, tp)); 9686 /* 9687 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9688 * synchronized state. 9689 */ 9690 if (thflags & TH_SYN) { 9691 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9692 return (ret_val); 9693 } 9694 /* 9695 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9696 * it's less than ts_recent, drop it. 9697 */ 9698 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9699 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9700 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9701 return (ret_val); 9702 } 9703 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9704 return (ret_val); 9705 } 9706 /* 9707 * If last ACK falls within this segment's sequence numbers, record 9708 * its timestamp. NOTE: 1) That the test incorporates suggestions 9709 * from the latest proposal of the tcplw@cray.com list (Braden 9710 * 1993/04/26). 2) That updating only on newer timestamps interferes 9711 * with our earlier PAWS tests, so this check should be solely 9712 * predicated on the sequence space of this segment. 3) That we 9713 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9714 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9715 * SEG.Len, This modified check allows us to overcome RFC1323's 9716 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9717 * p.869. In such cases, we can still calculate the RTT correctly 9718 * when RCV.NXT == Last.ACK.Sent. 9719 */ 9720 if ((to->to_flags & TOF_TS) != 0 && 9721 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9722 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9723 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9724 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9725 tp->ts_recent = to->to_tsval; 9726 } 9727 /* 9728 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9729 * is on (half-synchronized state), then queue data for later 9730 * processing; else drop segment and return. 9731 */ 9732 if ((thflags & TH_ACK) == 0) { 9733 if (tp->t_flags & TF_NEEDSYN) { 9734 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9735 tiwin, thflags, nxt_pkt)); 9736 } else if (tp->t_flags & TF_ACKNOW) { 9737 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9738 bbr->r_wanted_output = 1; 9739 return (ret_val); 9740 } else { 9741 ctf_do_drop(m, NULL); 9742 return (0); 9743 } 9744 } 9745 /* 9746 * case TCPS_LAST_ACK: Ack processing. 9747 */ 9748 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9749 return (ret_val); 9750 } 9751 if (ourfinisacked) { 9752 tp = tcp_close(tp); 9753 ctf_do_drop(m, tp); 9754 return (1); 9755 } 9756 if (sbavail(&so->so_snd)) { 9757 if (ctf_progress_timeout_check(tp, true)) { 9758 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9759 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9760 return (1); 9761 } 9762 } 9763 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9764 tiwin, thflags, nxt_pkt)); 9765 } 9766 9767 /* 9768 * Return value of 1, the TCB is unlocked and most 9769 * likely gone, return value of 0, the TCB is still 9770 * locked. 9771 */ 9772 static int 9773 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 9774 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9775 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9776 { 9777 int32_t ourfinisacked = 0; 9778 int32_t ret_val; 9779 struct tcp_bbr *bbr; 9780 9781 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9782 9783 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9784 ctf_calc_rwin(so, tp); 9785 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 9786 if ((thflags & TH_RST) || 9787 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9788 return (ctf_process_rst(m, th, so, tp)); 9789 9790 /* 9791 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9792 * synchronized state. 9793 */ 9794 if (thflags & TH_SYN) { 9795 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9796 return (ret_val); 9797 } 9798 /* 9799 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9800 * it's less than ts_recent, drop it. 9801 */ 9802 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9803 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9804 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9805 return (ret_val); 9806 } 9807 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9808 return (ret_val); 9809 } 9810 /* 9811 * If new data are received on a connection after the user processes 9812 * are gone, then we may RST the other end depending on the outcome 9813 * of bbr_check_data_after_close. 9814 * We call a new function now so we might continue and setup 9815 * to reset at all data being ack'd. 9816 */ 9817 if ((tp->t_flags & TF_CLOSED) && tlen && 9818 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9819 return (1); 9820 /* 9821 * If last ACK falls within this segment's sequence numbers, record 9822 * its timestamp. NOTE: 1) That the test incorporates suggestions 9823 * from the latest proposal of the tcplw@cray.com list (Braden 9824 * 1993/04/26). 2) That updating only on newer timestamps interferes 9825 * with our earlier PAWS tests, so this check should be solely 9826 * predicated on the sequence space of this segment. 3) That we 9827 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9828 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9829 * SEG.Len, This modified check allows us to overcome RFC1323's 9830 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9831 * p.869. In such cases, we can still calculate the RTT correctly 9832 * when RCV.NXT == Last.ACK.Sent. 9833 */ 9834 if ((to->to_flags & TOF_TS) != 0 && 9835 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9836 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9837 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9838 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9839 tp->ts_recent = to->to_tsval; 9840 } 9841 /* 9842 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9843 * is on (half-synchronized state), then queue data for later 9844 * processing; else drop segment and return. 9845 */ 9846 if ((thflags & TH_ACK) == 0) { 9847 if (tp->t_flags & TF_NEEDSYN) { 9848 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9849 tiwin, thflags, nxt_pkt)); 9850 } else if (tp->t_flags & TF_ACKNOW) { 9851 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9852 bbr->r_wanted_output = 1; 9853 return (ret_val); 9854 } else { 9855 ctf_do_drop(m, NULL); 9856 return (0); 9857 } 9858 } 9859 /* 9860 * Ack processing. 9861 */ 9862 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9863 return (ret_val); 9864 } 9865 if (sbavail(&so->so_snd)) { 9866 if (ctf_progress_timeout_check(tp, true)) { 9867 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9868 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9869 return (1); 9870 } 9871 } 9872 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9873 tiwin, thflags, nxt_pkt)); 9874 } 9875 9876 static void 9877 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr) 9878 { 9879 /* 9880 * Assure no timers are running. 9881 */ 9882 if (tcp_timer_active(tp, TT_PERSIST)) { 9883 /* We enter in persists, set the flag appropriately */ 9884 bbr->rc_in_persist = 1; 9885 } 9886 if (tcp_in_hpts(bbr->rc_tp)) { 9887 tcp_hpts_remove(bbr->rc_tp); 9888 } 9889 } 9890 9891 static void 9892 bbr_google_mode_on(struct tcp_bbr *bbr) 9893 { 9894 bbr->rc_use_google = 1; 9895 bbr->rc_no_pacing = 0; 9896 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9897 bbr->r_use_policer = bbr_policer_detection_enabled; 9898 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 9899 bbr->bbr_use_rack_cheat = 0; 9900 bbr->r_ctl.rc_incr_tmrs = 0; 9901 bbr->r_ctl.rc_inc_tcp_oh = 0; 9902 bbr->r_ctl.rc_inc_ip_oh = 0; 9903 bbr->r_ctl.rc_inc_enet_oh = 0; 9904 reset_time(&bbr->r_ctl.rc_delrate, 9905 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 9906 reset_time_small(&bbr->r_ctl.rc_rttprop, 9907 (11 * USECS_IN_SECOND)); 9908 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9909 } 9910 9911 static void 9912 bbr_google_mode_off(struct tcp_bbr *bbr) 9913 { 9914 bbr->rc_use_google = 0; 9915 bbr->r_ctl.bbr_google_discount = 0; 9916 bbr->no_pacing_until = bbr_no_pacing_until; 9917 bbr->r_use_policer = 0; 9918 if (bbr->no_pacing_until) 9919 bbr->rc_no_pacing = 1; 9920 else 9921 bbr->rc_no_pacing = 0; 9922 if (bbr_use_rack_resend_cheat) 9923 bbr->bbr_use_rack_cheat = 1; 9924 else 9925 bbr->bbr_use_rack_cheat = 0; 9926 if (bbr_incr_timers) 9927 bbr->r_ctl.rc_incr_tmrs = 1; 9928 else 9929 bbr->r_ctl.rc_incr_tmrs = 0; 9930 if (bbr_include_tcp_oh) 9931 bbr->r_ctl.rc_inc_tcp_oh = 1; 9932 else 9933 bbr->r_ctl.rc_inc_tcp_oh = 0; 9934 if (bbr_include_ip_oh) 9935 bbr->r_ctl.rc_inc_ip_oh = 1; 9936 else 9937 bbr->r_ctl.rc_inc_ip_oh = 0; 9938 if (bbr_include_enet_oh) 9939 bbr->r_ctl.rc_inc_enet_oh = 1; 9940 else 9941 bbr->r_ctl.rc_inc_enet_oh = 0; 9942 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 9943 reset_time(&bbr->r_ctl.rc_delrate, 9944 bbr_num_pktepo_for_del_limit); 9945 reset_time_small(&bbr->r_ctl.rc_rttprop, 9946 (bbr_filter_len_sec * USECS_IN_SECOND)); 9947 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9948 } 9949 /* 9950 * Return 0 on success, non-zero on failure 9951 * which indicates the error (usually no memory). 9952 */ 9953 static int 9954 bbr_init(struct tcpcb *tp, void **ptr) 9955 { 9956 struct inpcb *inp = tptoinpcb(tp); 9957 struct tcp_bbr *bbr = NULL; 9958 uint32_t cts; 9959 9960 tcp_hpts_init(tp); 9961 9962 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO)); 9963 if (*ptr == NULL) { 9964 /* 9965 * We need to allocate memory but cant. The INP and INP_INFO 9966 * locks and they are recursive (happens during setup. So a 9967 * scheme to drop the locks fails :( 9968 * 9969 */ 9970 return (ENOMEM); 9971 } 9972 bbr = (struct tcp_bbr *)*ptr; 9973 bbr->rtt_valid = 0; 9974 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 9975 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 9976 /* Take off any undesired flags */ 9977 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 9978 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 9979 tp->t_flags2 &= ~TF2_MBUF_ACKCMP; 9980 tp->t_flags2 &= ~TF2_MBUF_L_ACKS; 9981 9982 TAILQ_INIT(&bbr->r_ctl.rc_map); 9983 TAILQ_INIT(&bbr->r_ctl.rc_free); 9984 TAILQ_INIT(&bbr->r_ctl.rc_tmap); 9985 bbr->rc_tp = tp; 9986 bbr->rc_inp = inp; 9987 cts = tcp_get_usecs(&bbr->rc_tv); 9988 tp->t_acktime = 0; 9989 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close; 9990 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade; 9991 bbr->rc_tlp_threshold = bbr_tlp_thresh; 9992 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh; 9993 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay; 9994 bbr->r_ctl.rc_min_to = bbr_min_to; 9995 bbr->rc_bbr_state = BBR_STATE_STARTUP; 9996 bbr->r_ctl.bbr_lost_at_state = 0; 9997 bbr->r_ctl.rc_lost_at_startup = 0; 9998 bbr->rc_all_timers_stopped = 0; 9999 bbr->r_ctl.rc_bbr_lastbtlbw = 0; 10000 bbr->r_ctl.rc_pkt_epoch_del = 0; 10001 bbr->r_ctl.rc_pkt_epoch = 0; 10002 bbr->r_ctl.rc_lowest_rtt = 0xffffffff; 10003 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain; 10004 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 10005 bbr->r_ctl.rc_went_idle_time = cts; 10006 bbr->rc_pacer_started = cts; 10007 bbr->r_ctl.rc_pkt_epoch_time = cts; 10008 bbr->r_ctl.rc_rcvtime = cts; 10009 bbr->r_ctl.rc_bbr_state_time = cts; 10010 bbr->r_ctl.rc_del_time = cts; 10011 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 10012 bbr->r_ctl.last_in_probertt = cts; 10013 bbr->skip_gain = 0; 10014 bbr->gain_is_limited = 0; 10015 bbr->no_pacing_until = bbr_no_pacing_until; 10016 if (bbr->no_pacing_until) 10017 bbr->rc_no_pacing = 1; 10018 if (bbr_use_google_algo) { 10019 bbr->rc_no_pacing = 0; 10020 bbr->rc_use_google = 1; 10021 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 10022 bbr->r_use_policer = bbr_policer_detection_enabled; 10023 } else { 10024 bbr->rc_use_google = 0; 10025 bbr->r_ctl.bbr_google_discount = 0; 10026 bbr->r_use_policer = 0; 10027 } 10028 if (bbr_ts_limiting) 10029 bbr->rc_use_ts_limit = 1; 10030 else 10031 bbr->rc_use_ts_limit = 0; 10032 if (bbr_ts_can_raise) 10033 bbr->ts_can_raise = 1; 10034 else 10035 bbr->ts_can_raise = 0; 10036 if (V_tcp_delack_enabled == 1) 10037 tp->t_delayed_ack = 2; 10038 else if (V_tcp_delack_enabled == 0) 10039 tp->t_delayed_ack = 0; 10040 else if (V_tcp_delack_enabled < 100) 10041 tp->t_delayed_ack = V_tcp_delack_enabled; 10042 else 10043 tp->t_delayed_ack = 2; 10044 if (bbr->rc_use_google == 0) 10045 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10046 else 10047 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 10048 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms; 10049 bbr->rc_max_rto_sec = bbr_rto_max_sec; 10050 bbr->rc_init_win = bbr_def_init_win; 10051 if (tp->t_flags & TF_REQ_TSTMP) 10052 bbr->rc_last_options = TCP_TS_OVERHEAD; 10053 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options; 10054 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd; 10055 bbr->r_init_rtt = 1; 10056 10057 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 10058 if (bbr_allow_hdwr_pacing) 10059 bbr->bbr_hdw_pace_ena = 1; 10060 else 10061 bbr->bbr_hdw_pace_ena = 0; 10062 if (bbr_sends_full_iwnd) 10063 bbr->bbr_init_win_cheat = 1; 10064 else 10065 bbr->bbr_init_win_cheat = 0; 10066 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max; 10067 bbr->r_ctl.rc_drain_pg = bbr_drain_gain; 10068 bbr->r_ctl.rc_startup_pg = bbr_high_gain; 10069 bbr->rc_loss_exit = bbr_exit_startup_at_loss; 10070 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain; 10071 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second; 10072 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar; 10073 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max; 10074 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor; 10075 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min; 10076 bbr->r_ctl.bbr_cross_over = bbr_cross_over; 10077 bbr->r_ctl.rc_rtt_shrinks = cts; 10078 if (bbr->rc_use_google) { 10079 setup_time_filter(&bbr->r_ctl.rc_delrate, 10080 FILTER_TYPE_MAX, 10081 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 10082 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10083 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND)); 10084 } else { 10085 setup_time_filter(&bbr->r_ctl.rc_delrate, 10086 FILTER_TYPE_MAX, 10087 bbr_num_pktepo_for_del_limit); 10088 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10089 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND)); 10090 } 10091 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0); 10092 if (bbr_uses_idle_restart) 10093 bbr->rc_use_idle_restart = 1; 10094 else 10095 bbr->rc_use_idle_restart = 0; 10096 bbr->r_ctl.rc_bbr_cur_del_rate = 0; 10097 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps; 10098 if (bbr_resends_use_tso) 10099 bbr->rc_resends_use_tso = 1; 10100 if (tp->snd_una != tp->snd_max) { 10101 /* Create a send map for the current outstanding data */ 10102 struct bbr_sendmap *rsm; 10103 10104 rsm = bbr_alloc(bbr); 10105 if (rsm == NULL) { 10106 uma_zfree(bbr_pcb_zone, *ptr); 10107 *ptr = NULL; 10108 return (ENOMEM); 10109 } 10110 rsm->r_rtt_not_allowed = 1; 10111 rsm->r_tim_lastsent[0] = cts; 10112 rsm->r_rtr_cnt = 1; 10113 rsm->r_rtr_bytes = 0; 10114 rsm->r_start = tp->snd_una; 10115 rsm->r_end = tp->snd_max; 10116 rsm->r_dupack = 0; 10117 rsm->r_delivered = bbr->r_ctl.rc_delivered; 10118 rsm->r_ts_valid = 0; 10119 rsm->r_del_ack_ts = tp->ts_recent; 10120 rsm->r_del_time = cts; 10121 if (bbr->r_ctl.r_app_limited_until) 10122 rsm->r_app_limited = 1; 10123 else 10124 rsm->r_app_limited = 0; 10125 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 10126 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 10127 rsm->r_in_tmap = 1; 10128 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 10129 rsm->r_bbr_state = bbr_state_val(bbr); 10130 else 10131 rsm->r_bbr_state = 8; 10132 } 10133 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0)) 10134 bbr->bbr_use_rack_cheat = 1; 10135 if (bbr_incr_timers && (bbr->rc_use_google == 0)) 10136 bbr->r_ctl.rc_incr_tmrs = 1; 10137 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0)) 10138 bbr->r_ctl.rc_inc_tcp_oh = 1; 10139 if (bbr_include_ip_oh && (bbr->rc_use_google == 0)) 10140 bbr->r_ctl.rc_inc_ip_oh = 1; 10141 if (bbr_include_enet_oh && (bbr->rc_use_google == 0)) 10142 bbr->r_ctl.rc_inc_enet_oh = 1; 10143 10144 bbr_log_type_statechange(bbr, cts, __LINE__); 10145 if (TCPS_HAVEESTABLISHED(tp->t_state) && 10146 (tp->t_srtt)) { 10147 uint32_t rtt; 10148 10149 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 10150 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 10151 } 10152 /* announce the settings and state */ 10153 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT); 10154 tcp_bbr_tso_size_check(bbr, cts); 10155 /* 10156 * Now call the generic function to start a timer. This will place 10157 * the TCB on the hptsi wheel if a timer is needed with appropriate 10158 * flags. 10159 */ 10160 bbr_stop_all_timers(tp, bbr); 10161 /* 10162 * Validate the timers are not in usec, if they are convert. 10163 * BBR should in theory move to USEC and get rid of a 10164 * lot of the TICKS_2 calls.. but for now we stay 10165 * with tick timers. 10166 */ 10167 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 10168 TCPT_RANGESET(tp->t_rxtcur, 10169 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 10170 tp->t_rttmin, TCPTV_REXMTMAX); 10171 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0); 10172 return (0); 10173 } 10174 10175 /* 10176 * Return 0 if we can accept the connection. Return 10177 * non-zero if we can't handle the connection. A EAGAIN 10178 * means you need to wait until the connection is up. 10179 * a EADDRNOTAVAIL means we can never handle the connection 10180 * (no SACK). 10181 */ 10182 static int 10183 bbr_handoff_ok(struct tcpcb *tp) 10184 { 10185 if ((tp->t_state == TCPS_CLOSED) || 10186 (tp->t_state == TCPS_LISTEN)) { 10187 /* Sure no problem though it may not stick */ 10188 return (0); 10189 } 10190 if ((tp->t_state == TCPS_SYN_SENT) || 10191 (tp->t_state == TCPS_SYN_RECEIVED)) { 10192 /* 10193 * We really don't know you have to get to ESTAB or beyond 10194 * to tell. 10195 */ 10196 return (EAGAIN); 10197 } 10198 if (tp->t_flags & TF_SENTFIN) 10199 return (EINVAL); 10200 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) { 10201 return (0); 10202 } 10203 /* 10204 * If we reach here we don't do SACK on this connection so we can 10205 * never do rack. 10206 */ 10207 return (EINVAL); 10208 } 10209 10210 static void 10211 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged) 10212 { 10213 if (tp->t_fb_ptr) { 10214 uint32_t calc; 10215 struct tcp_bbr *bbr; 10216 struct bbr_sendmap *rsm; 10217 10218 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 10219 if (bbr->r_ctl.crte) 10220 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 10221 bbr_log_flowend(bbr); 10222 bbr->rc_tp = NULL; 10223 if (bbr->bbr_hdrw_pacing) 10224 counter_u64_add(bbr_flows_whdwr_pacing, -1); 10225 else 10226 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 10227 if (bbr->r_ctl.crte != NULL) { 10228 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 10229 bbr->r_ctl.crte = NULL; 10230 } 10231 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10232 while (rsm) { 10233 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 10234 uma_zfree(bbr_zone, rsm); 10235 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10236 } 10237 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10238 while (rsm) { 10239 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 10240 uma_zfree(bbr_zone, rsm); 10241 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10242 } 10243 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd; 10244 if (calc > (bbr->r_ctl.rc_init_rwnd / 10)) 10245 BBR_STAT_INC(bbr_dynamic_rwnd); 10246 else 10247 BBR_STAT_INC(bbr_static_rwnd); 10248 bbr->r_ctl.rc_free_cnt = 0; 10249 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr); 10250 tp->t_fb_ptr = NULL; 10251 } 10252 /* Make sure snd_nxt is correctly set */ 10253 tp->snd_nxt = tp->snd_max; 10254 } 10255 10256 static void 10257 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win) 10258 { 10259 switch (tp->t_state) { 10260 case TCPS_SYN_SENT: 10261 bbr->r_state = TCPS_SYN_SENT; 10262 bbr->r_substate = bbr_do_syn_sent; 10263 break; 10264 case TCPS_SYN_RECEIVED: 10265 bbr->r_state = TCPS_SYN_RECEIVED; 10266 bbr->r_substate = bbr_do_syn_recv; 10267 break; 10268 case TCPS_ESTABLISHED: 10269 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd); 10270 bbr->r_state = TCPS_ESTABLISHED; 10271 bbr->r_substate = bbr_do_established; 10272 break; 10273 case TCPS_CLOSE_WAIT: 10274 bbr->r_state = TCPS_CLOSE_WAIT; 10275 bbr->r_substate = bbr_do_close_wait; 10276 break; 10277 case TCPS_FIN_WAIT_1: 10278 bbr->r_state = TCPS_FIN_WAIT_1; 10279 bbr->r_substate = bbr_do_fin_wait_1; 10280 break; 10281 case TCPS_CLOSING: 10282 bbr->r_state = TCPS_CLOSING; 10283 bbr->r_substate = bbr_do_closing; 10284 break; 10285 case TCPS_LAST_ACK: 10286 bbr->r_state = TCPS_LAST_ACK; 10287 bbr->r_substate = bbr_do_lastack; 10288 break; 10289 case TCPS_FIN_WAIT_2: 10290 bbr->r_state = TCPS_FIN_WAIT_2; 10291 bbr->r_substate = bbr_do_fin_wait_2; 10292 break; 10293 case TCPS_LISTEN: 10294 case TCPS_CLOSED: 10295 case TCPS_TIME_WAIT: 10296 default: 10297 break; 10298 }; 10299 } 10300 10301 static void 10302 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog) 10303 { 10304 /* 10305 * Now what state are we going into now? Is there adjustments 10306 * needed? 10307 */ 10308 int32_t old_state; 10309 10310 old_state = bbr_state_val(bbr); 10311 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) { 10312 /* Save the lowest srtt we saw in our end of the sub-state */ 10313 bbr->rc_hit_state_1 = 0; 10314 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff) 10315 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state; 10316 } 10317 bbr->rc_bbr_substate++; 10318 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) { 10319 /* Cycle back to first state-> gain */ 10320 bbr->rc_bbr_substate = 0; 10321 } 10322 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10323 /* 10324 * We enter the gain(5/4) cycle (possibly less if 10325 * shallow buffer detection is enabled) 10326 */ 10327 if (bbr->skip_gain) { 10328 /* 10329 * Hardware pacing has set our rate to 10330 * the max and limited our b/w just 10331 * do level i.e. no gain. 10332 */ 10333 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1]; 10334 } else if (bbr->gain_is_limited && 10335 bbr->bbr_hdrw_pacing && 10336 bbr->r_ctl.crte) { 10337 /* 10338 * We can't gain above the hardware pacing 10339 * rate which is less than our rate + the gain 10340 * calculate the gain needed to reach the hardware 10341 * pacing rate.. 10342 */ 10343 uint64_t bw, rate, gain_calc; 10344 10345 bw = bbr_get_bw(bbr); 10346 rate = bbr->r_ctl.crte->rate; 10347 if ((rate > bw) && 10348 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) { 10349 gain_calc = (rate * BBR_UNIT) / bw; 10350 if (gain_calc < BBR_UNIT) 10351 gain_calc = BBR_UNIT; 10352 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc; 10353 } else { 10354 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10355 } 10356 } else 10357 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10358 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) { 10359 bbr->r_ctl.rc_bbr_state_atflight = cts; 10360 } else 10361 bbr->r_ctl.rc_bbr_state_atflight = 0; 10362 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10363 bbr->rc_hit_state_1 = 1; 10364 bbr->r_ctl.rc_exta_time_gd = 0; 10365 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10366 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10367 if (bbr_state_drain_2_tar) { 10368 bbr->r_ctl.rc_bbr_state_atflight = 0; 10369 } else 10370 bbr->r_ctl.rc_bbr_state_atflight = cts; 10371 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN]; 10372 } else { 10373 /* All other cycles hit here 2-7 */ 10374 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) { 10375 if (bbr_sub_drain_slam_cwnd && 10376 (bbr->rc_use_google == 0) && 10377 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10378 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10379 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10380 } 10381 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP)) 10382 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) - 10383 bbr_get_rtt(bbr, BBR_RTT_PROP)); 10384 else 10385 bbr->r_ctl.rc_exta_time_gd = 0; 10386 if (bbr->r_ctl.rc_exta_time_gd) { 10387 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd; 10388 /* Now chop up the time for each state (div by 7) */ 10389 bbr->r_ctl.rc_level_state_extra /= 7; 10390 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) { 10391 /* Add a randomization */ 10392 bbr_randomize_extra_state_time(bbr); 10393 } 10394 } 10395 } 10396 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10397 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)]; 10398 } 10399 if (bbr->rc_use_google) { 10400 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10401 } 10402 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10403 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10404 if (dolog) 10405 bbr_log_type_statechange(bbr, cts, line); 10406 10407 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10408 uint32_t time_in; 10409 10410 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10411 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 10412 counter_u64_add(bbr_state_time[(old_state + 5)], time_in); 10413 } else { 10414 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10415 } 10416 } 10417 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 10418 bbr_set_state_target(bbr, __LINE__); 10419 if (bbr_sub_drain_slam_cwnd && 10420 (bbr->rc_use_google == 0) && 10421 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10422 /* Slam down the cwnd */ 10423 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10424 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10425 if (bbr_sub_drain_app_limit) { 10426 /* Go app limited if we are on a long drain */ 10427 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + 10428 ctf_flight_size(bbr->rc_tp, 10429 (bbr->r_ctl.rc_sacked + 10430 bbr->r_ctl.rc_lost_bytes))); 10431 } 10432 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10433 } 10434 if (bbr->rc_lt_use_bw) { 10435 /* In policed mode we clamp pacing_gain to BBR_UNIT */ 10436 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10437 } 10438 /* Google changes TSO size every cycle */ 10439 if (bbr->rc_use_google) 10440 tcp_bbr_tso_size_check(bbr, cts); 10441 bbr->r_ctl.gain_epoch = cts; 10442 bbr->r_ctl.rc_bbr_state_time = cts; 10443 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch; 10444 } 10445 10446 static void 10447 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10448 { 10449 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) && 10450 (google_allow_early_out == 1) && 10451 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) { 10452 /* We have reached out target flight size possibly early */ 10453 goto change_state; 10454 } 10455 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10456 return; 10457 } 10458 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) { 10459 /* 10460 * Must be a rttProp movement forward before 10461 * we can change states. 10462 */ 10463 return; 10464 } 10465 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10466 /* 10467 * The needed time has passed but for 10468 * the gain cycle extra rules apply: 10469 * 1) If we have seen loss, we exit 10470 * 2) If we have not reached the target 10471 * we stay in GAIN (gain-to-target). 10472 */ 10473 if (google_consider_lost && losses) 10474 goto change_state; 10475 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) { 10476 return; 10477 } 10478 } 10479 change_state: 10480 /* For gain we must reach our target, all others last 1 rttProp */ 10481 bbr_substate_change(bbr, cts, __LINE__, 1); 10482 } 10483 10484 static void 10485 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10486 { 10487 uint32_t flight, bbr_cur_cycle_time; 10488 10489 if (bbr->rc_use_google) { 10490 bbr_set_probebw_google_gains(bbr, cts, losses); 10491 return; 10492 } 10493 if (cts == 0) { 10494 /* 10495 * Never alow cts to be 0 we 10496 * do this so we can judge if 10497 * we have set a timestamp. 10498 */ 10499 cts = 1; 10500 } 10501 if (bbr_state_is_pkt_epoch) 10502 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 10503 else 10504 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP); 10505 10506 if (bbr->r_ctl.rc_bbr_state_atflight == 0) { 10507 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10508 flight = ctf_flight_size(bbr->rc_tp, 10509 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10510 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) { 10511 /* Keep it slam down */ 10512 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) { 10513 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10514 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10515 } 10516 if (bbr_sub_drain_app_limit) { 10517 /* Go app limited if we are on a long drain */ 10518 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight); 10519 } 10520 } 10521 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) && 10522 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) || 10523 (flight >= bbr->r_ctl.flightsize_at_drain))) { 10524 /* 10525 * Still here after the same time as 10526 * the gain. We need to drain harder 10527 * for the next srtt. Reduce by a set amount 10528 * the gain drop is capped at DRAIN states 10529 * value (88). 10530 */ 10531 bbr->r_ctl.flightsize_at_drain = flight; 10532 if (bbr_drain_drop_mul && 10533 bbr_drain_drop_div && 10534 (bbr_drain_drop_mul < bbr_drain_drop_div)) { 10535 /* Use your specific drop value (def 4/5 = 20%) */ 10536 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul; 10537 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div; 10538 } else { 10539 /* You get drop of 20% */ 10540 bbr->r_ctl.rc_bbr_hptsi_gain *= 4; 10541 bbr->r_ctl.rc_bbr_hptsi_gain /= 5; 10542 } 10543 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) { 10544 /* Reduce our gain again to the bottom */ 10545 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 10546 } 10547 bbr_log_exit_gain(bbr, cts, 4); 10548 /* 10549 * Extend out so we wait another 10550 * epoch before dropping again. 10551 */ 10552 bbr->r_ctl.gain_epoch = cts; 10553 } 10554 if (flight <= bbr->r_ctl.rc_target_at_state) { 10555 if (bbr_sub_drain_slam_cwnd && 10556 (bbr->rc_use_google == 0) && 10557 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10558 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10559 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10560 } 10561 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10562 bbr_log_exit_gain(bbr, cts, 3); 10563 } 10564 } else { 10565 /* Its a gain */ 10566 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) { 10567 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10568 goto change_state; 10569 } 10570 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) || 10571 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >= 10572 bbr->rc_tp->snd_wnd)) { 10573 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10574 bbr_log_exit_gain(bbr, cts, 2); 10575 } 10576 } 10577 /** 10578 * We fall through and return always one of two things has 10579 * occurred. 10580 * 1) We are still not at target 10581 * <or> 10582 * 2) We reached the target and set rc_bbr_state_atflight 10583 * which means we no longer hit this block 10584 * next time we are called. 10585 */ 10586 return; 10587 } 10588 change_state: 10589 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) 10590 return; 10591 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) { 10592 /* Less than a full time-period has passed */ 10593 return; 10594 } 10595 if (bbr->r_ctl.rc_level_state_extra && 10596 (bbr_state_val(bbr) > BBR_SUB_DRAIN) && 10597 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10598 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10599 /* Less than a full time-period + extra has passed */ 10600 return; 10601 } 10602 if (bbr_gain_gets_extra_too && 10603 bbr->r_ctl.rc_level_state_extra && 10604 (bbr_state_val(bbr) == BBR_SUB_GAIN) && 10605 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10606 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10607 /* Less than a full time-period + extra has passed */ 10608 return; 10609 } 10610 bbr_substate_change(bbr, cts, __LINE__, 1); 10611 } 10612 10613 static uint32_t 10614 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain) 10615 { 10616 uint32_t mss, tar; 10617 10618 if (bbr->rc_use_google) { 10619 /* Google just uses the cwnd target */ 10620 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain); 10621 } else { 10622 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 10623 bbr->r_ctl.rc_pace_max_segs); 10624 /* Get the base cwnd with gain rounded to a mss */ 10625 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr), 10626 gain), mss); 10627 /* Make sure it is within our min */ 10628 if (tar < get_min_cwnd(bbr)) 10629 return (get_min_cwnd(bbr)); 10630 } 10631 return (tar); 10632 } 10633 10634 static void 10635 bbr_set_state_target(struct tcp_bbr *bbr, int line) 10636 { 10637 uint32_t tar, meth; 10638 10639 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 10640 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 10641 /* Special case using old probe-rtt method */ 10642 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10643 meth = 1; 10644 } else { 10645 /* Non-probe-rtt case and reduced probe-rtt */ 10646 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 10647 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) { 10648 /* For gain cycle we use the hptsi gain */ 10649 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10650 meth = 2; 10651 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) { 10652 /* 10653 * If configured, or for google all other states 10654 * get BBR_UNIT. 10655 */ 10656 tar = bbr_get_a_state_target(bbr, BBR_UNIT); 10657 meth = 3; 10658 } else { 10659 /* 10660 * Or we set a target based on the pacing gain 10661 * for non-google mode and default (non-configured). 10662 * Note we don't set a target goal below drain (192). 10663 */ 10664 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) { 10665 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]); 10666 meth = 4; 10667 } else { 10668 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10669 meth = 5; 10670 } 10671 } 10672 } 10673 bbr_log_set_of_state_target(bbr, tar, line, meth); 10674 bbr->r_ctl.rc_target_at_state = tar; 10675 } 10676 10677 static void 10678 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 10679 { 10680 /* Change to probe_rtt */ 10681 uint32_t time_in; 10682 10683 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10684 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10685 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10686 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain 10687 + bbr->r_ctl.rc_delivered); 10688 /* Setup so we force feed the filter */ 10689 if (bbr->rc_use_google || bbr_probertt_sets_rtt) 10690 bbr->rc_prtt_set_ts = 1; 10691 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10692 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10693 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10694 } 10695 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0); 10696 bbr->r_ctl.rc_rtt_shrinks = cts; 10697 bbr->r_ctl.last_in_probertt = cts; 10698 bbr->r_ctl.rc_probertt_srttchktim = cts; 10699 bbr->r_ctl.rc_bbr_state_time = cts; 10700 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT; 10701 /* We need to force the filter to update */ 10702 10703 if ((bbr_sub_drain_slam_cwnd) && 10704 bbr->rc_hit_state_1 && 10705 (bbr->rc_use_google == 0) && 10706 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10707 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd) 10708 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10709 } else 10710 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10711 /* Update the lost */ 10712 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10713 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){ 10714 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */ 10715 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10716 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10717 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10718 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10719 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6); 10720 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd; 10721 } else { 10722 /* 10723 * We bring it down slowly by using a hptsi gain that is 10724 * probably 75%. This will slowly float down our outstanding 10725 * without tampering with the cwnd. 10726 */ 10727 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 10728 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10729 bbr_set_state_target(bbr, __LINE__); 10730 if (bbr_prtt_slam_cwnd && 10731 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 10732 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10733 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10734 } 10735 } 10736 if (ctf_flight_size(bbr->rc_tp, 10737 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 10738 bbr->r_ctl.rc_target_at_state) { 10739 /* We are at target */ 10740 bbr->r_ctl.rc_bbr_enters_probertt = cts; 10741 } else { 10742 /* We need to come down to reach target before our time begins */ 10743 bbr->r_ctl.rc_bbr_enters_probertt = 0; 10744 } 10745 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch; 10746 BBR_STAT_INC(bbr_enter_probertt); 10747 bbr_log_exit_gain(bbr, cts, 0); 10748 bbr_log_type_statechange(bbr, cts, line); 10749 } 10750 10751 static void 10752 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts) 10753 { 10754 /* 10755 * Sanity check on probe-rtt intervals. 10756 * In crazy situations where we are competing 10757 * against new-reno flows with huge buffers 10758 * our rtt-prop interval could come to dominate 10759 * things if we can't get through a full set 10760 * of cycles, we need to adjust it. 10761 */ 10762 if (bbr_can_adjust_probertt && 10763 (bbr->rc_use_google == 0)) { 10764 uint16_t val = 0; 10765 uint32_t cur_rttp, fval, newval, baseval; 10766 10767 /* Are we to small and go into probe-rtt to often? */ 10768 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1)); 10769 cur_rttp = roundup(baseval, USECS_IN_SECOND); 10770 fval = bbr_filter_len_sec * USECS_IN_SECOND; 10771 if (bbr_is_ratio == 0) { 10772 if (fval > bbr_rtt_probe_limit) 10773 newval = cur_rttp + (fval - bbr_rtt_probe_limit); 10774 else 10775 newval = cur_rttp; 10776 } else { 10777 int mul; 10778 10779 mul = fval / bbr_rtt_probe_limit; 10780 newval = cur_rttp * mul; 10781 } 10782 if (cur_rttp > bbr->r_ctl.rc_probertt_int) { 10783 bbr->r_ctl.rc_probertt_int = cur_rttp; 10784 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10785 val = 1; 10786 } else { 10787 /* 10788 * No adjustments were made 10789 * do we need to shrink it? 10790 */ 10791 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) { 10792 if (cur_rttp <= bbr_rtt_probe_limit) { 10793 /* 10794 * Things have calmed down lets 10795 * shrink all the way to default 10796 */ 10797 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10798 reset_time_small(&bbr->r_ctl.rc_rttprop, 10799 (bbr_filter_len_sec * USECS_IN_SECOND)); 10800 cur_rttp = bbr_rtt_probe_limit; 10801 newval = (bbr_filter_len_sec * USECS_IN_SECOND); 10802 val = 2; 10803 } else { 10804 /* 10805 * Well does some adjustment make sense? 10806 */ 10807 if (cur_rttp < bbr->r_ctl.rc_probertt_int) { 10808 /* We can reduce interval time some */ 10809 bbr->r_ctl.rc_probertt_int = cur_rttp; 10810 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10811 val = 3; 10812 } 10813 } 10814 } 10815 } 10816 if (val) 10817 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val); 10818 } 10819 } 10820 10821 static void 10822 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 10823 { 10824 /* Exit probe-rtt */ 10825 10826 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) { 10827 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10828 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10829 } 10830 bbr_log_exit_gain(bbr, cts, 1); 10831 bbr->rc_hit_state_1 = 0; 10832 bbr->r_ctl.rc_rtt_shrinks = cts; 10833 bbr->r_ctl.last_in_probertt = cts; 10834 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0); 10835 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10836 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, 10837 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 10838 bbr->r_ctl.rc_delivered); 10839 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10840 uint32_t time_in; 10841 10842 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10843 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10844 } 10845 if (bbr->rc_filled_pipe) { 10846 /* Switch to probe_bw */ 10847 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 10848 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 10849 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10850 bbr_substate_change(bbr, cts, __LINE__, 0); 10851 bbr_log_type_statechange(bbr, cts, __LINE__); 10852 } else { 10853 /* Back to startup */ 10854 bbr->rc_bbr_state = BBR_STATE_STARTUP; 10855 bbr->r_ctl.rc_bbr_state_time = cts; 10856 /* 10857 * We don't want to give a complete free 3 10858 * measurements until we exit, so we use 10859 * the number of pe's we were in probe-rtt 10860 * to add to the startup_epoch. That way 10861 * we will still retain the old state. 10862 */ 10863 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt); 10864 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10865 /* Make sure to use the lower pg when shifting back in */ 10866 if (bbr->r_ctl.rc_lost && 10867 bbr_use_lower_gain_in_startup && 10868 (bbr->rc_use_google == 0)) 10869 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10870 else 10871 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 10872 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 10873 /* Probably not needed but set it anyway */ 10874 bbr_set_state_target(bbr, __LINE__); 10875 bbr_log_type_statechange(bbr, cts, __LINE__); 10876 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10877 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0); 10878 } 10879 bbr_check_probe_rtt_limits(bbr, cts); 10880 } 10881 10882 static int32_t inline 10883 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts) 10884 { 10885 if ((bbr->rc_past_init_win == 1) && 10886 (bbr->rc_in_persist == 0) && 10887 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) { 10888 return (1); 10889 } 10890 if (bbr_can_force_probertt && 10891 (bbr->rc_in_persist == 0) && 10892 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 10893 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 10894 return (1); 10895 } 10896 return (0); 10897 } 10898 10899 static int32_t 10900 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch) 10901 { 10902 uint64_t btlbw, gain; 10903 if (pkt_epoch == 0) { 10904 /* 10905 * Need to be on a pkt-epoch to continue. 10906 */ 10907 return (0); 10908 } 10909 btlbw = bbr_get_full_bw(bbr); 10910 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10911 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10912 if (btlbw >= gain) { 10913 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 10914 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10915 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 10916 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10917 } 10918 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) 10919 return (1); 10920 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10921 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 10922 return(0); 10923 } 10924 10925 static int32_t inline 10926 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch) 10927 { 10928 /* Have we gained 25% in the last 3 packet based epoch's? */ 10929 uint64_t btlbw, gain; 10930 int do_exit; 10931 int delta, rtt_gain; 10932 10933 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 10934 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 10935 /* 10936 * This qualifies as a RTT_PROBE session since we drop the 10937 * data outstanding to nothing and waited more than 10938 * bbr_rtt_probe_time. 10939 */ 10940 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 10941 bbr_set_reduced_rtt(bbr, cts, __LINE__); 10942 } 10943 if (bbr_should_enter_probe_rtt(bbr, cts)) { 10944 bbr_enter_probe_rtt(bbr, cts, __LINE__); 10945 return (0); 10946 } 10947 if (bbr->rc_use_google) 10948 return (bbr_google_startup(bbr, cts, pkt_epoch)); 10949 10950 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 10951 (bbr_use_lower_gain_in_startup)) { 10952 /* Drop to a lower gain 1.5 x since we saw loss */ 10953 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10954 } 10955 if (pkt_epoch == 0) { 10956 /* 10957 * Need to be on a pkt-epoch to continue. 10958 */ 10959 return (0); 10960 } 10961 if (bbr_rtt_gain_thresh) { 10962 /* 10963 * Do we allow a flow to stay 10964 * in startup with no loss and no 10965 * gain in rtt over a set threshold? 10966 */ 10967 if (bbr->r_ctl.rc_pkt_epoch_rtt && 10968 bbr->r_ctl.startup_last_srtt && 10969 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) { 10970 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt; 10971 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt; 10972 } else 10973 rtt_gain = 0; 10974 if ((bbr->r_ctl.startup_last_srtt == 0) || 10975 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt)) 10976 /* First time or new lower value */ 10977 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 10978 10979 if ((bbr->r_ctl.rc_lost == 0) && 10980 (rtt_gain < bbr_rtt_gain_thresh)) { 10981 /* 10982 * No loss, and we are under 10983 * our gain threhold for 10984 * increasing RTT. 10985 */ 10986 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10987 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10988 bbr_log_startup_event(bbr, cts, rtt_gain, 10989 delta, bbr->r_ctl.startup_last_srtt, 10); 10990 return (0); 10991 } 10992 } 10993 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) && 10994 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) && 10995 (!IN_RECOVERY(bbr->rc_tp->t_flags))) { 10996 /* 10997 * We only assess if we have a new measurement when 10998 * we have no loss and are not in recovery. 10999 * Drag up by one our last_startup epoch so we will hold 11000 * the number of non-gain we have already accumulated. 11001 */ 11002 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 11003 bbr->r_ctl.rc_bbr_last_startup_epoch++; 11004 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11005 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9); 11006 return (0); 11007 } 11008 /* Case where we reduced the lost (bad retransmit) */ 11009 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost) 11010 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11011 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count; 11012 btlbw = bbr_get_full_bw(bbr); 11013 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower) 11014 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 11015 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 11016 else 11017 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 11018 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 11019 do_exit = 0; 11020 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw) 11021 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 11022 if (btlbw >= gain) { 11023 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 11024 /* Update the lost so we won't exit in next set of tests */ 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, 3); 11028 } 11029 if ((bbr->rc_loss_exit && 11030 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 11031 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) && 11032 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) { 11033 /* 11034 * If we had no gain, we had loss and that loss was above 11035 * our threshould, the rwnd is not constrained, and we have 11036 * had at least 3 packet epochs exit. Note that this is 11037 * switched off by sysctl. Google does not do this by the 11038 * way. 11039 */ 11040 if ((ctf_flight_size(bbr->rc_tp, 11041 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 11042 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) { 11043 do_exit = 1; 11044 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11045 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4); 11046 } else { 11047 /* Just record an updated loss value */ 11048 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11049 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11050 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5); 11051 } 11052 } else 11053 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11054 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) || 11055 do_exit) { 11056 /* Return 1 to exit the startup state. */ 11057 return (1); 11058 } 11059 /* Stay in startup */ 11060 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11061 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 11062 return (0); 11063 } 11064 11065 static void 11066 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses) 11067 { 11068 /* 11069 * A tick occurred in the rtt epoch do we need to do anything? 11070 */ 11071 #ifdef BBR_INVARIANTS 11072 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) && 11073 (bbr->rc_bbr_state != BBR_STATE_DRAIN) && 11074 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) && 11075 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 11076 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) { 11077 /* Debug code? */ 11078 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state); 11079 } 11080 #endif 11081 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 11082 /* Do we exit the startup state? */ 11083 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) { 11084 uint32_t time_in; 11085 11086 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11087 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6); 11088 bbr->rc_filled_pipe = 1; 11089 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11090 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11091 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11092 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11093 } else 11094 time_in = 0; 11095 if (bbr->rc_no_pacing) 11096 bbr->rc_no_pacing = 0; 11097 bbr->r_ctl.rc_bbr_state_time = cts; 11098 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg; 11099 bbr->rc_bbr_state = BBR_STATE_DRAIN; 11100 bbr_set_state_target(bbr, __LINE__); 11101 if ((bbr->rc_use_google == 0) && 11102 bbr_slam_cwnd_in_main_drain) { 11103 /* Here we don't have to worry about probe-rtt */ 11104 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 11105 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11106 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11107 } 11108 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 11109 bbr_log_type_statechange(bbr, cts, __LINE__); 11110 if (ctf_flight_size(bbr->rc_tp, 11111 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 11112 bbr->r_ctl.rc_target_at_state) { 11113 /* 11114 * Switch to probe_bw if we are already 11115 * there 11116 */ 11117 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11118 bbr_substate_change(bbr, cts, __LINE__, 0); 11119 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11120 bbr_log_type_statechange(bbr, cts, __LINE__); 11121 } 11122 } 11123 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) { 11124 uint32_t inflight; 11125 struct tcpcb *tp; 11126 11127 tp = bbr->rc_tp; 11128 inflight = ctf_flight_size(tp, 11129 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11130 if (inflight >= bbr->r_ctl.rc_target_at_state) { 11131 /* We have reached a flight of the cwnd target */ 11132 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11133 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11134 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 11135 bbr_set_state_target(bbr, __LINE__); 11136 /* 11137 * Rig it so we don't do anything crazy and 11138 * start fresh with a new randomization. 11139 */ 11140 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 11141 bbr->rc_bbr_substate = BBR_SUB_LEVEL6; 11142 bbr_substate_change(bbr, cts, __LINE__, 1); 11143 } 11144 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) { 11145 /* Has in-flight reached the bdp (or less)? */ 11146 uint32_t inflight; 11147 struct tcpcb *tp; 11148 11149 tp = bbr->rc_tp; 11150 inflight = ctf_flight_size(tp, 11151 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11152 if ((bbr->rc_use_google == 0) && 11153 bbr_slam_cwnd_in_main_drain && 11154 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11155 /* 11156 * Here we don't have to worry about probe-rtt 11157 * re-slam it, but keep it slammed down. 11158 */ 11159 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11160 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11161 } 11162 if (inflight <= bbr->r_ctl.rc_target_at_state) { 11163 /* We have drained */ 11164 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11165 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11166 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11167 uint32_t time_in; 11168 11169 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11170 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11171 } 11172 if ((bbr->rc_use_google == 0) && 11173 bbr_slam_cwnd_in_main_drain && 11174 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 11175 /* Restore the cwnd */ 11176 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 11177 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11178 } 11179 /* Setup probe-rtt has being done now RRS-HERE */ 11180 bbr->r_ctl.rc_rtt_shrinks = cts; 11181 bbr->r_ctl.last_in_probertt = cts; 11182 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0); 11183 /* Randomly pick a sub-state */ 11184 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11185 bbr_substate_change(bbr, cts, __LINE__, 0); 11186 bbr_log_type_statechange(bbr, cts, __LINE__); 11187 } 11188 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) { 11189 uint32_t flight; 11190 11191 flight = ctf_flight_size(bbr->rc_tp, 11192 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11193 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered); 11194 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) && 11195 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11196 /* 11197 * We must keep cwnd at the desired MSS. 11198 */ 11199 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 11200 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11201 } else if ((bbr_prtt_slam_cwnd) && 11202 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11203 /* Re-slam it */ 11204 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11205 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11206 } 11207 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) { 11208 /* Has outstanding reached our target? */ 11209 if (flight <= bbr->r_ctl.rc_target_at_state) { 11210 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0); 11211 bbr->r_ctl.rc_bbr_enters_probertt = cts; 11212 /* If time is exactly 0, be 1usec off */ 11213 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) 11214 bbr->r_ctl.rc_bbr_enters_probertt = 1; 11215 if (bbr->rc_use_google == 0) { 11216 /* 11217 * Restore any lowering that as occurred to 11218 * reach here 11219 */ 11220 if (bbr->r_ctl.bbr_rttprobe_gain_val) 11221 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 11222 else 11223 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11224 } 11225 } 11226 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) && 11227 (bbr->rc_use_google == 0) && 11228 bbr->r_ctl.bbr_rttprobe_gain_val && 11229 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) || 11230 (flight >= bbr->r_ctl.flightsize_at_drain))) { 11231 /* 11232 * We have doddled with our current hptsi 11233 * gain an srtt and have still not made it 11234 * to target, or we have increased our flight. 11235 * Lets reduce the gain by xx% 11236 * flooring the reduce at DRAIN (based on 11237 * mul/div) 11238 */ 11239 int red; 11240 11241 bbr->r_ctl.flightsize_at_drain = flight; 11242 bbr->r_ctl.rc_probertt_srttchktim = cts; 11243 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1); 11244 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) { 11245 /* Reduce our gain again */ 11246 bbr->r_ctl.rc_bbr_hptsi_gain -= red; 11247 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0); 11248 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) { 11249 /* one more chance before we give up */ 11250 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 11251 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0); 11252 } else { 11253 /* At the very bottom */ 11254 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1); 11255 } 11256 } 11257 } 11258 if (bbr->r_ctl.rc_bbr_enters_probertt && 11259 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) && 11260 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) { 11261 /* Time to exit probe RTT normally */ 11262 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts); 11263 } 11264 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 11265 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 11266 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 11267 /* 11268 * This qualifies as a RTT_PROBE session since we 11269 * drop the data outstanding to nothing and waited 11270 * more than bbr_rtt_probe_time. 11271 */ 11272 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 11273 bbr_set_reduced_rtt(bbr, cts, __LINE__); 11274 } 11275 if (bbr_should_enter_probe_rtt(bbr, cts)) { 11276 bbr_enter_probe_rtt(bbr, cts, __LINE__); 11277 } else { 11278 bbr_set_probebw_gains(bbr, cts, losses); 11279 } 11280 } 11281 } 11282 11283 static void 11284 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses) 11285 { 11286 int32_t epoch = 0; 11287 11288 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 11289 bbr_set_epoch(bbr, cts, line); 11290 /* At each epoch doe lt bw sampling */ 11291 epoch = 1; 11292 } 11293 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses); 11294 } 11295 11296 static int 11297 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11298 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt, 11299 struct timeval *tv) 11300 { 11301 struct inpcb *inp = tptoinpcb(tp); 11302 struct socket *so = tptosocket(tp); 11303 int32_t thflags, retval; 11304 uint32_t cts, lcts; 11305 uint32_t tiwin; 11306 struct tcpopt to; 11307 struct tcp_bbr *bbr; 11308 struct bbr_sendmap *rsm; 11309 struct timeval ltv; 11310 int32_t did_out = 0; 11311 uint16_t nsegs; 11312 int32_t prev_state; 11313 uint32_t lost; 11314 11315 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11316 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11317 /* add in our stats */ 11318 kern_prefetch(bbr, &prev_state); 11319 prev_state = 0; 11320 thflags = tcp_get_flags(th); 11321 /* 11322 * If this is either a state-changing packet or current state isn't 11323 * established, we require a write lock on tcbinfo. Otherwise, we 11324 * allow the tcbinfo to be in either alocked or unlocked, as the 11325 * caller may have unnecessarily acquired a write lock due to a 11326 * race. 11327 */ 11328 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11329 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 11330 __func__)); 11331 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 11332 __func__)); 11333 11334 tp->t_rcvtime = ticks; 11335 /* 11336 * Unscale the window into a 32-bit value. For the SYN_SENT state 11337 * the scale is zero. 11338 */ 11339 tiwin = th->th_win << tp->snd_scale; 11340 #ifdef STATS 11341 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 11342 #endif 11343 11344 if (m->m_flags & M_TSTMP) { 11345 /* Prefer the hardware timestamp if present */ 11346 struct timespec ts; 11347 11348 mbuf_tstmp2timespec(m, &ts); 11349 bbr->rc_tv.tv_sec = ts.tv_sec; 11350 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11351 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11352 } else if (m->m_flags & M_TSTMP_LRO) { 11353 /* Next the arrival timestamp */ 11354 struct timespec ts; 11355 11356 mbuf_tstmp2timespec(m, &ts); 11357 bbr->rc_tv.tv_sec = ts.tv_sec; 11358 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11359 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11360 } else { 11361 /* 11362 * Ok just get the current time. 11363 */ 11364 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv); 11365 } 11366 /* 11367 * Parse options on any incoming segment. 11368 */ 11369 tcp_dooptions(&to, (u_char *)(th + 1), 11370 (th->th_off << 2) - sizeof(struct tcphdr), 11371 (thflags & TH_SYN) ? TO_SYN : 0); 11372 if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) { 11373 /* 11374 * We don't look at sack's from the 11375 * peer because the MSS is too small which 11376 * can subject us to an attack. 11377 */ 11378 to.to_flags &= ~TOF_SACK; 11379 } 11380 /* 11381 * If timestamps were negotiated during SYN/ACK and a 11382 * segment without a timestamp is received, silently drop 11383 * the segment, unless it is a RST segment or missing timestamps are 11384 * tolerated. 11385 * See section 3.2 of RFC 7323. 11386 */ 11387 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 11388 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 11389 retval = 0; 11390 m_freem(m); 11391 goto done_with_input; 11392 } 11393 /* 11394 * If echoed timestamp is later than the current time, fall back to 11395 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 11396 * were used when this connection was established. 11397 */ 11398 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 11399 to.to_tsecr -= tp->ts_offset; 11400 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv))) 11401 to.to_tsecr = 0; 11402 } 11403 /* 11404 * If its the first time in we need to take care of options and 11405 * verify we can do SACK for rack! 11406 */ 11407 if (bbr->r_state == 0) { 11408 /* 11409 * Process options only when we get SYN/ACK back. The SYN 11410 * case for incoming connections is handled in tcp_syncache. 11411 * According to RFC1323 the window field in a SYN (i.e., a 11412 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 11413 * this is traditional behavior, may need to be cleaned up. 11414 */ 11415 if (bbr->rc_inp == NULL) { 11416 bbr->rc_inp = inp; 11417 } 11418 /* 11419 * We need to init rc_inp here since its not init'd when 11420 * bbr_init is called 11421 */ 11422 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 11423 if ((to.to_flags & TOF_SCALE) && 11424 (tp->t_flags & TF_REQ_SCALE)) { 11425 tp->t_flags |= TF_RCVD_SCALE; 11426 tp->snd_scale = to.to_wscale; 11427 } else 11428 tp->t_flags &= ~TF_REQ_SCALE; 11429 /* 11430 * Initial send window. It will be updated with the 11431 * next incoming segment to the scaled value. 11432 */ 11433 tp->snd_wnd = th->th_win; 11434 if ((to.to_flags & TOF_TS) && 11435 (tp->t_flags & TF_REQ_TSTMP)) { 11436 tp->t_flags |= TF_RCVD_TSTMP; 11437 tp->ts_recent = to.to_tsval; 11438 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 11439 } else 11440 tp->t_flags &= ~TF_REQ_TSTMP; 11441 if (to.to_flags & TOF_MSS) 11442 tcp_mss(tp, to.to_mss); 11443 if ((tp->t_flags & TF_SACK_PERMIT) && 11444 (to.to_flags & TOF_SACKPERM) == 0) 11445 tp->t_flags &= ~TF_SACK_PERMIT; 11446 if (tp->t_flags & TF_FASTOPEN) { 11447 if (to.to_flags & TOF_FASTOPEN) { 11448 uint16_t mss; 11449 11450 if (to.to_flags & TOF_MSS) 11451 mss = to.to_mss; 11452 else 11453 if ((inp->inp_vflag & INP_IPV6) != 0) 11454 mss = TCP6_MSS; 11455 else 11456 mss = TCP_MSS; 11457 tcp_fastopen_update_cache(tp, mss, 11458 to.to_tfo_len, to.to_tfo_cookie); 11459 } else 11460 tcp_fastopen_disable_path(tp); 11461 } 11462 } 11463 /* 11464 * At this point we are at the initial call. Here we decide 11465 * if we are doing RACK or not. We do this by seeing if 11466 * TF_SACK_PERMIT is set, if not rack is *not* possible and 11467 * we switch to the default code. 11468 */ 11469 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 11470 /* Bail */ 11471 tcp_switch_back_to_default(tp); 11472 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen, 11473 tlen, iptos); 11474 return (1); 11475 } 11476 /* Set the flag */ 11477 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 11478 tcp_set_hpts(tp); 11479 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack); 11480 } 11481 if (thflags & TH_ACK) { 11482 /* Track ack types */ 11483 if (to.to_flags & TOF_SACK) 11484 BBR_STAT_INC(bbr_acks_with_sacks); 11485 else 11486 BBR_STAT_INC(bbr_plain_acks); 11487 } 11488 /* 11489 * This is the one exception case where we set the rack state 11490 * always. All other times (timers etc) we must have a rack-state 11491 * set (so we assure we have done the checks above for SACK). 11492 */ 11493 if (thflags & TH_FIN) 11494 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 11495 if (bbr->r_state != tp->t_state) 11496 bbr_set_state(tp, bbr, tiwin); 11497 11498 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL) 11499 kern_prefetch(rsm, &prev_state); 11500 prev_state = bbr->r_state; 11501 bbr->rc_ack_was_delayed = 0; 11502 lost = bbr->r_ctl.rc_lost; 11503 bbr->rc_is_pkt_epoch_now = 0; 11504 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) { 11505 /* Get the real time into lcts and figure the real delay */ 11506 lcts = tcp_get_usecs(<v); 11507 if (TSTMP_GT(lcts, cts)) { 11508 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts; 11509 bbr->rc_ack_was_delayed = 1; 11510 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay, 11511 bbr->r_ctl.highest_hdwr_delay)) 11512 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay; 11513 } else { 11514 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11515 bbr->rc_ack_was_delayed = 0; 11516 } 11517 } else { 11518 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11519 bbr->rc_ack_was_delayed = 0; 11520 } 11521 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m); 11522 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 11523 retval = 0; 11524 m_freem(m); 11525 goto done_with_input; 11526 } 11527 /* 11528 * If a segment with the ACK-bit set arrives in the SYN-SENT state 11529 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 11530 */ 11531 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 11532 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 11533 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11534 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11535 return (1); 11536 } 11537 if (tiwin > bbr->r_ctl.rc_high_rwnd) 11538 bbr->r_ctl.rc_high_rwnd = tiwin; 11539 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp, 11540 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11541 bbr->rtt_valid = 0; 11542 if (to.to_flags & TOF_TS) { 11543 bbr->rc_ts_valid = 1; 11544 bbr->r_ctl.last_inbound_ts = to.to_tsval; 11545 } else { 11546 bbr->rc_ts_valid = 0; 11547 bbr->r_ctl.last_inbound_ts = 0; 11548 } 11549 retval = (*bbr->r_substate) (m, th, so, 11550 tp, &to, drop_hdrlen, 11551 tlen, tiwin, thflags, nxt_pkt, iptos); 11552 if (nxt_pkt == 0) 11553 BBR_STAT_INC(bbr_rlock_left_ret0); 11554 else 11555 BBR_STAT_INC(bbr_rlock_left_ret1); 11556 if (retval == 0) { 11557 /* 11558 * If retval is 1 the tcb is unlocked and most likely the tp 11559 * is gone. 11560 */ 11561 INP_WLOCK_ASSERT(inp); 11562 tcp_bbr_xmit_timer_commit(bbr, tp, cts); 11563 if (bbr->rc_is_pkt_epoch_now) 11564 bbr_set_pktepoch(bbr, cts, __LINE__); 11565 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost)); 11566 if (nxt_pkt == 0) { 11567 if ((bbr->r_wanted_output != 0) || 11568 (tp->t_flags & TF_ACKNOW)) { 11569 11570 bbr->rc_output_starts_timer = 0; 11571 did_out = 1; 11572 if (tcp_output(tp) < 0) 11573 return (1); 11574 } else 11575 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0); 11576 } 11577 if ((nxt_pkt == 0) && 11578 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 11579 (SEQ_GT(tp->snd_max, tp->snd_una) || 11580 (tp->t_flags & TF_DELACK) || 11581 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 11582 (tp->t_state <= TCPS_CLOSING)))) { 11583 /* 11584 * We could not send (probably in the hpts but 11585 * stopped the timer)? 11586 */ 11587 if ((tp->snd_max == tp->snd_una) && 11588 ((tp->t_flags & TF_DELACK) == 0) && 11589 (tcp_in_hpts(tp)) && 11590 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11591 /* 11592 * keep alive not needed if we are hptsi 11593 * output yet 11594 */ 11595 ; 11596 } else { 11597 if (tcp_in_hpts(tp)) { 11598 tcp_hpts_remove(tp); 11599 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11600 (TSTMP_GT(lcts, bbr->rc_pacer_started))) { 11601 uint32_t del; 11602 11603 del = lcts - bbr->rc_pacer_started; 11604 if (bbr->r_ctl.rc_last_delay_val > del) { 11605 BBR_STAT_INC(bbr_force_timer_start); 11606 bbr->r_ctl.rc_last_delay_val -= del; 11607 bbr->rc_pacer_started = lcts; 11608 } else { 11609 /* We are late */ 11610 bbr->r_ctl.rc_last_delay_val = 0; 11611 BBR_STAT_INC(bbr_force_output); 11612 if (tcp_output(tp) < 0) 11613 return (1); 11614 } 11615 } 11616 } 11617 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val, 11618 0); 11619 } 11620 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) { 11621 /* Do we have the correct timer running? */ 11622 bbr_timer_audit(tp, bbr, lcts, &so->so_snd); 11623 } 11624 /* Clear the flag, it may have been cleared by output but we may not have */ 11625 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS)) 11626 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11627 /* Do we have a new state */ 11628 if (bbr->r_state != tp->t_state) 11629 bbr_set_state(tp, bbr, tiwin); 11630 done_with_input: 11631 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out); 11632 if (did_out) 11633 bbr->r_wanted_output = 0; 11634 } 11635 return (retval); 11636 } 11637 11638 static void 11639 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11640 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 11641 { 11642 struct timeval tv; 11643 int retval; 11644 11645 /* First lets see if we have old packets */ 11646 if (!STAILQ_EMPTY(&tp->t_inqueue)) { 11647 if (ctf_do_queued_segments(tp, 1)) { 11648 m_freem(m); 11649 return; 11650 } 11651 } 11652 if (m->m_flags & M_TSTMP_LRO) { 11653 mbuf_tstmp2timeval(m, &tv); 11654 } else { 11655 /* Should not be should we kassert instead? */ 11656 tcp_get_usecs(&tv); 11657 } 11658 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 11659 0, &tv); 11660 if (retval == 0) { 11661 INP_WUNLOCK(tptoinpcb(tp)); 11662 } 11663 } 11664 11665 /* 11666 * Return how much data can be sent without violating the 11667 * cwnd or rwnd. 11668 */ 11669 11670 static inline uint32_t 11671 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin, 11672 uint32_t avail, int32_t sb_offset, uint32_t cts) 11673 { 11674 uint32_t len; 11675 11676 if (ctf_outstanding(tp) >= tp->snd_wnd) { 11677 /* We never want to go over our peers rcv-window */ 11678 len = 0; 11679 } else { 11680 uint32_t flight; 11681 11682 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11683 if (flight >= sendwin) { 11684 /* 11685 * We have in flight what we are allowed by cwnd (if 11686 * it was rwnd blocking it would have hit above out 11687 * >= tp->snd_wnd). 11688 */ 11689 return (0); 11690 } 11691 len = sendwin - flight; 11692 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 11693 /* We would send too much (beyond the rwnd) */ 11694 len = tp->snd_wnd - ctf_outstanding(tp); 11695 } 11696 if ((len + sb_offset) > avail) { 11697 /* 11698 * We don't have that much in the SB, how much is 11699 * there? 11700 */ 11701 len = avail - sb_offset; 11702 } 11703 } 11704 return (len); 11705 } 11706 11707 static inline void 11708 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11709 { 11710 if (error) { 11711 return; 11712 } 11713 if (rsm) { 11714 if (rsm->r_flags & BBR_TLP) { 11715 /* 11716 * TLP should not count in retran count, but in its 11717 * own bin 11718 */ 11719 KMOD_TCPSTAT_INC(tcps_tlpresends); 11720 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len); 11721 } else { 11722 /* Retransmit */ 11723 tp->t_sndrexmitpack++; 11724 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 11725 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 11726 #ifdef STATS 11727 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 11728 len); 11729 #endif 11730 } 11731 /* 11732 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is 11733 * sub-state 11734 */ 11735 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len); 11736 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) { 11737 /* Non probe_bw log in 1, 2, or 4. */ 11738 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len); 11739 } else { 11740 /* 11741 * Log our probe state 3, and log also 5-13 to show 11742 * us the recovery sub-state for the send. This 11743 * means that 3 == (5+6+7+8+9+10+11+12+13) 11744 */ 11745 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len); 11746 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len); 11747 } 11748 /* Place in both 16's the totals of retransmitted */ 11749 counter_u64_add(bbr_state_lost[16], len); 11750 counter_u64_add(bbr_state_resend[16], len); 11751 /* Place in 17's the total sent */ 11752 counter_u64_add(bbr_state_resend[17], len); 11753 counter_u64_add(bbr_state_lost[17], len); 11754 11755 } else { 11756 /* New sends */ 11757 KMOD_TCPSTAT_INC(tcps_sndpack); 11758 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 11759 /* Place in 17's the total sent */ 11760 counter_u64_add(bbr_state_resend[17], len); 11761 counter_u64_add(bbr_state_lost[17], len); 11762 #ifdef STATS 11763 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 11764 len); 11765 #endif 11766 } 11767 } 11768 11769 static void 11770 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level) 11771 { 11772 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) { 11773 /* 11774 * Limit the cwnd to not be above N x the target plus whats 11775 * is outstanding. The target is based on the current b/w 11776 * estimate. 11777 */ 11778 uint32_t target; 11779 11780 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT); 11781 target += ctf_outstanding(tp); 11782 target *= bbr_target_cwnd_mult_limit; 11783 if (tp->snd_cwnd > target) 11784 tp->snd_cwnd = target; 11785 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__); 11786 } 11787 } 11788 11789 static int 11790 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg) 11791 { 11792 /* 11793 * "adv" is the amount we could increase the window, taking into 11794 * account that we are limited by TCP_MAXWIN << tp->rcv_scale. 11795 */ 11796 int32_t adv; 11797 int32_t oldwin; 11798 11799 adv = recwin; 11800 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 11801 oldwin = (tp->rcv_adv - tp->rcv_nxt); 11802 if (adv > oldwin) 11803 adv -= oldwin; 11804 else { 11805 /* We can't increase the window */ 11806 adv = 0; 11807 } 11808 } else 11809 oldwin = 0; 11810 11811 /* 11812 * If the new window size ends up being the same as or less 11813 * than the old size when it is scaled, then don't force 11814 * a window update. 11815 */ 11816 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 11817 return (0); 11818 11819 if (adv >= (2 * maxseg) && 11820 (adv >= (so->so_rcv.sb_hiwat / 4) || 11821 recwin <= (so->so_rcv.sb_hiwat / 8) || 11822 so->so_rcv.sb_hiwat <= 8 * maxseg)) { 11823 return (1); 11824 } 11825 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 11826 return (1); 11827 return (0); 11828 } 11829 11830 /* 11831 * Return 0 on success and a errno on failure to send. 11832 * Note that a 0 return may not mean we sent anything 11833 * if the TCB was on the hpts. A non-zero return 11834 * does indicate the error we got from ip[6]_output. 11835 */ 11836 static int 11837 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv) 11838 { 11839 struct socket *so; 11840 int32_t len; 11841 uint32_t cts; 11842 uint32_t recwin, sendwin; 11843 int32_t sb_offset; 11844 int32_t flags, abandon, error = 0; 11845 struct tcp_log_buffer *lgb; 11846 struct mbuf *m; 11847 struct mbuf *mb; 11848 uint32_t if_hw_tsomaxsegcount = 0; 11849 uint32_t if_hw_tsomaxsegsize = 0; 11850 uint32_t if_hw_tsomax = 0; 11851 struct ip *ip = NULL; 11852 struct tcp_bbr *bbr; 11853 struct tcphdr *th; 11854 struct udphdr *udp = NULL; 11855 u_char opt[TCP_MAXOLEN]; 11856 unsigned ipoptlen, optlen, hdrlen; 11857 unsigned ulen; 11858 uint32_t bbr_seq; 11859 uint32_t delay_calc=0; 11860 uint8_t doing_tlp = 0; 11861 uint8_t local_options; 11862 #ifdef BBR_INVARIANTS 11863 uint8_t doing_retran_from = 0; 11864 uint8_t picked_up_retran = 0; 11865 #endif 11866 uint8_t wanted_cookie = 0; 11867 uint8_t more_to_rxt=0; 11868 int32_t prefetch_so_done = 0; 11869 int32_t prefetch_rsm = 0; 11870 uint32_t tot_len = 0; 11871 uint32_t maxseg, pace_max_segs, p_maxseg; 11872 int32_t csum_flags = 0; 11873 int32_t hw_tls; 11874 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 11875 unsigned ipsec_optlen = 0; 11876 11877 #endif 11878 volatile int32_t sack_rxmit; 11879 struct bbr_sendmap *rsm = NULL; 11880 int32_t tso, mtu; 11881 struct tcpopt to; 11882 int32_t slot = 0; 11883 struct inpcb *inp; 11884 struct sockbuf *sb; 11885 bool hpts_calling; 11886 #ifdef INET6 11887 struct ip6_hdr *ip6 = NULL; 11888 int32_t isipv6; 11889 #endif 11890 uint8_t app_limited = BBR_JR_SENT_DATA; 11891 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11892 /* We take a cache hit here */ 11893 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval)); 11894 cts = tcp_tv_to_usectick(&bbr->rc_tv); 11895 inp = bbr->rc_inp; 11896 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS); 11897 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11898 so = inp->inp_socket; 11899 sb = &so->so_snd; 11900 if (tp->t_nic_ktls_xmit) 11901 hw_tls = 1; 11902 else 11903 hw_tls = 0; 11904 kern_prefetch(sb, &maxseg); 11905 maxseg = tp->t_maxseg - bbr->rc_last_options; 11906 if (bbr_minseg(bbr) < maxseg) { 11907 tcp_bbr_tso_size_check(bbr, cts); 11908 } 11909 /* Remove any flags that indicate we are pacing on the inp */ 11910 pace_max_segs = bbr->r_ctl.rc_pace_max_segs; 11911 p_maxseg = min(maxseg, pace_max_segs); 11912 INP_WLOCK_ASSERT(inp); 11913 #ifdef TCP_OFFLOAD 11914 if (tp->t_flags & TF_TOE) 11915 return (tcp_offload_output(tp)); 11916 #endif 11917 11918 #ifdef INET6 11919 if (bbr->r_state) { 11920 /* Use the cache line loaded if possible */ 11921 isipv6 = bbr->r_is_v6; 11922 } else { 11923 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 11924 } 11925 #endif 11926 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 11927 tcp_in_hpts(tp)) { 11928 /* 11929 * We are on the hpts for some timer but not hptsi output. 11930 * Possibly remove from the hpts so we can send/recv etc. 11931 */ 11932 if ((tp->t_flags & TF_ACKNOW) == 0) { 11933 /* 11934 * No immediate demand right now to send an ack, but 11935 * the user may have read, making room for new data 11936 * (a window update). If so we may want to cancel 11937 * whatever timer is running (KEEP/DEL-ACK?) and 11938 * continue to send out a window update. Or we may 11939 * have gotten more data into the socket buffer to 11940 * send. 11941 */ 11942 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 11943 (long)TCP_MAXWIN << tp->rcv_scale); 11944 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) && 11945 ((tcp_outflags[tp->t_state] & TH_RST) == 0) && 11946 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <= 11947 (tp->snd_max - tp->snd_una))) { 11948 /* 11949 * Nothing new to send and no window update 11950 * is needed to send. Lets just return and 11951 * let the timer-run off. 11952 */ 11953 return (0); 11954 } 11955 } 11956 tcp_hpts_remove(tp); 11957 bbr_timer_cancel(bbr, __LINE__, cts); 11958 } 11959 if (bbr->r_ctl.rc_last_delay_val) { 11960 /* Calculate a rough delay for early escape to sending */ 11961 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11962 delay_calc = cts - bbr->rc_pacer_started; 11963 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11964 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11965 else 11966 delay_calc = 0; 11967 } 11968 /* Mark that we have called bbr_output(). */ 11969 if ((bbr->r_timer_override) || 11970 (tp->t_state < TCPS_ESTABLISHED)) { 11971 /* Timeouts or early states are exempt */ 11972 if (tcp_in_hpts(tp)) 11973 tcp_hpts_remove(tp); 11974 } else if (tcp_in_hpts(tp)) { 11975 if ((bbr->r_ctl.rc_last_delay_val) && 11976 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11977 delay_calc) { 11978 /* 11979 * We were being paced for output and the delay has 11980 * already exceeded when we were supposed to be 11981 * called, lets go ahead and pull out of the hpts 11982 * and call output. 11983 */ 11984 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1); 11985 bbr->r_ctl.rc_last_delay_val = 0; 11986 tcp_hpts_remove(tp); 11987 } else if (tp->t_state == TCPS_CLOSED) { 11988 bbr->r_ctl.rc_last_delay_val = 0; 11989 tcp_hpts_remove(tp); 11990 } else { 11991 /* 11992 * On the hpts, you shall not pass! even if ACKNOW 11993 * is on, we will when the hpts fires, unless of 11994 * course we are overdue. 11995 */ 11996 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1); 11997 return (0); 11998 } 11999 } 12000 bbr->rc_cwnd_limited = 0; 12001 if (bbr->r_ctl.rc_last_delay_val) { 12002 /* recalculate the real delay and deal with over/under */ 12003 if (SEQ_GT(cts, bbr->rc_pacer_started)) 12004 delay_calc = cts - bbr->rc_pacer_started; 12005 else 12006 delay_calc = 0; 12007 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 12008 /* Setup the delay which will be added in */ 12009 delay_calc -= bbr->r_ctl.rc_last_delay_val; 12010 else { 12011 /* 12012 * We are early setup to adjust 12013 * our slot time. 12014 */ 12015 uint64_t merged_val; 12016 12017 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc); 12018 bbr->r_agg_early_set = 1; 12019 if (bbr->r_ctl.rc_hptsi_agg_delay) { 12020 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) { 12021 /* Nope our previous late cancels out the early */ 12022 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early; 12023 bbr->r_agg_early_set = 0; 12024 bbr->r_ctl.rc_agg_early = 0; 12025 } else { 12026 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay; 12027 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12028 } 12029 } 12030 merged_val = bbr->rc_pacer_started; 12031 merged_val <<= 32; 12032 merged_val |= bbr->r_ctl.rc_last_delay_val; 12033 bbr_log_pacing_delay_calc(bbr, hpts_calling, 12034 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val, 12035 bbr->r_agg_early_set, 3); 12036 bbr->r_ctl.rc_last_delay_val = 0; 12037 BBR_STAT_INC(bbr_early); 12038 delay_calc = 0; 12039 } 12040 } else { 12041 /* We were not delayed due to hptsi */ 12042 if (bbr->r_agg_early_set) 12043 bbr->r_ctl.rc_agg_early = 0; 12044 bbr->r_agg_early_set = 0; 12045 delay_calc = 0; 12046 } 12047 if (delay_calc) { 12048 /* 12049 * We had a hptsi delay which means we are falling behind on 12050 * sending at the expected rate. Calculate an extra amount 12051 * of data we can send, if any, to put us back on track. 12052 */ 12053 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay) 12054 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff; 12055 else 12056 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc; 12057 } 12058 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12059 if ((tp->snd_una == tp->snd_max) && 12060 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 12061 (sbavail(sb))) { 12062 /* 12063 * Ok we have been idle with nothing outstanding 12064 * we possibly need to start fresh with either a new 12065 * suite of states or a fast-ramp up. 12066 */ 12067 bbr_restart_after_idle(bbr, 12068 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time)); 12069 } 12070 /* 12071 * Now was there a hptsi delay where we are behind? We only count 12072 * being behind if: a) We are not in recovery. b) There was a delay. 12073 * <and> c) We had room to send something. 12074 * 12075 */ 12076 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 12077 int retval; 12078 12079 retval = bbr_process_timers(tp, bbr, cts, hpts_calling); 12080 if (retval != 0) { 12081 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1); 12082 /* 12083 * If timers want tcp_drop(), then pass error out, 12084 * otherwise suppress it. 12085 */ 12086 return (retval < 0 ? retval : 0); 12087 } 12088 } 12089 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 12090 if (hpts_calling && 12091 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 12092 bbr->r_ctl.rc_last_delay_val = 0; 12093 } 12094 bbr->r_timer_override = 0; 12095 bbr->r_wanted_output = 0; 12096 /* 12097 * For TFO connections in SYN_RECEIVED, only allow the initial 12098 * SYN|ACK and those sent by the retransmit timer. 12099 */ 12100 if ((tp->t_flags & TF_FASTOPEN) && 12101 ((tp->t_state == TCPS_SYN_RECEIVED) || 12102 (tp->t_state == TCPS_SYN_SENT)) && 12103 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 12104 (tp->t_rxtshift == 0)) { /* not a retransmit */ 12105 len = 0; 12106 goto just_return_nolock; 12107 } 12108 /* 12109 * Before sending anything check for a state update. For hpts 12110 * calling without input this is important. If its input calling 12111 * then this was already done. 12112 */ 12113 if (bbr->rc_use_google == 0) 12114 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12115 again: 12116 /* 12117 * If we've recently taken a timeout, snd_max will be greater than 12118 * snd_max. BBR in general does not pay much attention to snd_nxt 12119 * for historic reasons the persist timer still uses it. This means 12120 * we have to look at it. All retransmissions that are not persits 12121 * use the rsm that needs to be sent so snd_nxt is ignored. At the 12122 * end of this routine we pull snd_nxt always up to snd_max. 12123 */ 12124 doing_tlp = 0; 12125 #ifdef BBR_INVARIANTS 12126 doing_retran_from = picked_up_retran = 0; 12127 #endif 12128 error = 0; 12129 tso = 0; 12130 slot = 0; 12131 mtu = 0; 12132 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12133 sb_offset = tp->snd_max - tp->snd_una; 12134 flags = tcp_outflags[tp->t_state]; 12135 sack_rxmit = 0; 12136 len = 0; 12137 rsm = NULL; 12138 if (flags & TH_RST) { 12139 SOCK_SENDBUF_LOCK(so); 12140 goto send; 12141 } 12142 recheck_resend: 12143 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 12144 /* We need to always have one in reserve */ 12145 rsm = bbr_alloc(bbr); 12146 if (rsm == NULL) { 12147 error = ENOMEM; 12148 /* Lie to get on the hpts */ 12149 tot_len = tp->t_maxseg; 12150 if (hpts_calling) 12151 /* Retry in a ms */ 12152 slot = 1001; 12153 goto just_return_nolock; 12154 } 12155 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 12156 bbr->r_ctl.rc_free_cnt++; 12157 rsm = NULL; 12158 } 12159 /* What do we send, a resend? */ 12160 if (bbr->r_ctl.rc_resend == NULL) { 12161 /* Check for rack timeout */ 12162 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 12163 if (bbr->r_ctl.rc_resend) { 12164 #ifdef BBR_INVARIANTS 12165 picked_up_retran = 1; 12166 #endif 12167 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend); 12168 } 12169 } 12170 if (bbr->r_ctl.rc_resend) { 12171 rsm = bbr->r_ctl.rc_resend; 12172 #ifdef BBR_INVARIANTS 12173 doing_retran_from = 1; 12174 #endif 12175 /* Remove any TLP flags its a RACK or T-O */ 12176 rsm->r_flags &= ~BBR_TLP; 12177 bbr->r_ctl.rc_resend = NULL; 12178 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 12179 #ifdef BBR_INVARIANTS 12180 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n", 12181 tp, bbr, rsm, rsm->r_start, tp->snd_una); 12182 goto recheck_resend; 12183 #else 12184 /* TSNH */ 12185 rsm = NULL; 12186 goto recheck_resend; 12187 #endif 12188 } 12189 if (rsm->r_flags & BBR_HAS_SYN) { 12190 /* Only retransmit a SYN by itself */ 12191 len = 0; 12192 if ((flags & TH_SYN) == 0) { 12193 /* Huh something is wrong */ 12194 rsm->r_start++; 12195 if (rsm->r_start == rsm->r_end) { 12196 /* Clean it up, somehow we missed the ack? */ 12197 bbr_log_syn(tp, NULL); 12198 } else { 12199 /* TFO with data? */ 12200 rsm->r_flags &= ~BBR_HAS_SYN; 12201 len = rsm->r_end - rsm->r_start; 12202 } 12203 } else { 12204 /* Retransmitting SYN */ 12205 rsm = NULL; 12206 SOCK_SENDBUF_LOCK(so); 12207 goto send; 12208 } 12209 } else 12210 len = rsm->r_end - rsm->r_start; 12211 if ((bbr->rc_resends_use_tso == 0) && 12212 (len > maxseg)) { 12213 len = maxseg; 12214 more_to_rxt = 1; 12215 } 12216 sb_offset = rsm->r_start - tp->snd_una; 12217 if (len > 0) { 12218 sack_rxmit = 1; 12219 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 12220 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 12221 min(len, maxseg)); 12222 } else { 12223 /* I dont think this can happen */ 12224 rsm = NULL; 12225 goto recheck_resend; 12226 } 12227 BBR_STAT_INC(bbr_resends_set); 12228 } else if (bbr->r_ctl.rc_tlp_send) { 12229 /* 12230 * Tail loss probe 12231 */ 12232 doing_tlp = 1; 12233 rsm = bbr->r_ctl.rc_tlp_send; 12234 bbr->r_ctl.rc_tlp_send = NULL; 12235 sack_rxmit = 1; 12236 len = rsm->r_end - rsm->r_start; 12237 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12238 len = maxseg; 12239 12240 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 12241 #ifdef BBR_INVARIANTS 12242 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u", 12243 tp, bbr, tp->snd_una, rsm, rsm->r_start); 12244 #else 12245 /* TSNH */ 12246 rsm = NULL; 12247 goto recheck_resend; 12248 #endif 12249 } 12250 sb_offset = rsm->r_start - tp->snd_una; 12251 BBR_STAT_INC(bbr_tlp_set); 12252 } 12253 /* 12254 * Enforce a connection sendmap count limit if set 12255 * as long as we are not retransmiting. 12256 */ 12257 if ((rsm == NULL) && 12258 (V_tcp_map_entries_limit > 0) && 12259 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 12260 BBR_STAT_INC(bbr_alloc_limited); 12261 if (!bbr->alloc_limit_reported) { 12262 bbr->alloc_limit_reported = 1; 12263 BBR_STAT_INC(bbr_alloc_limited_conns); 12264 } 12265 goto just_return_nolock; 12266 } 12267 #ifdef BBR_INVARIANTS 12268 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) { 12269 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u", 12270 tp, bbr, rsm, sb_offset, len); 12271 } 12272 #endif 12273 /* 12274 * Get standard flags, and add SYN or FIN if requested by 'hidden' 12275 * state flags. 12276 */ 12277 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL)) 12278 flags |= TH_FIN; 12279 if (tp->t_flags & TF_NEEDSYN) 12280 flags |= TH_SYN; 12281 12282 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) { 12283 /* we are retransmitting the fin */ 12284 len--; 12285 if (len) { 12286 /* 12287 * When retransmitting data do *not* include the 12288 * FIN. This could happen from a TLP probe if we 12289 * allowed data with a FIN. 12290 */ 12291 flags &= ~TH_FIN; 12292 } 12293 } else if (rsm) { 12294 if (flags & TH_FIN) 12295 flags &= ~TH_FIN; 12296 } 12297 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 12298 void *end_rsm; 12299 12300 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 12301 if (end_rsm) 12302 kern_prefetch(end_rsm, &prefetch_rsm); 12303 prefetch_rsm = 1; 12304 } 12305 SOCK_SENDBUF_LOCK(so); 12306 /* 12307 * If snd_nxt == snd_max and we have transmitted a FIN, the 12308 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 12309 * negative length. This can also occur when TCP opens up its 12310 * congestion window while receiving additional duplicate acks after 12311 * fast-retransmit because TCP will reset snd_nxt to snd_max after 12312 * the fast-retransmit. 12313 * 12314 * In the normal retransmit-FIN-only case, however, snd_nxt will be 12315 * set to snd_una, the sb_offset will be 0, and the length may wind 12316 * up 0. 12317 * 12318 * If sack_rxmit is true we are retransmitting from the scoreboard 12319 * in which case len is already set. 12320 */ 12321 if (sack_rxmit == 0) { 12322 uint32_t avail; 12323 12324 avail = sbavail(sb); 12325 if (SEQ_GT(tp->snd_max, tp->snd_una)) 12326 sb_offset = tp->snd_max - tp->snd_una; 12327 else 12328 sb_offset = 0; 12329 if (bbr->rc_tlp_new_data) { 12330 /* TLP is forcing out new data */ 12331 uint32_t tlplen; 12332 12333 doing_tlp = 1; 12334 tlplen = maxseg; 12335 12336 if (tlplen > (uint32_t)(avail - sb_offset)) { 12337 tlplen = (uint32_t)(avail - sb_offset); 12338 } 12339 if (tlplen > tp->snd_wnd) { 12340 len = tp->snd_wnd; 12341 } else { 12342 len = tlplen; 12343 } 12344 bbr->rc_tlp_new_data = 0; 12345 } else { 12346 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts); 12347 if ((len < p_maxseg) && 12348 (bbr->rc_in_persist == 0) && 12349 (ctf_outstanding(tp) >= (2 * p_maxseg)) && 12350 ((avail - sb_offset) >= p_maxseg)) { 12351 /* 12352 * We are not completing whats in the socket 12353 * buffer (i.e. there is at least a segment 12354 * waiting to send) and we have 2 or more 12355 * segments outstanding. There is no sense 12356 * of sending a little piece. Lets defer and 12357 * and wait until we can send a whole 12358 * segment. 12359 */ 12360 len = 0; 12361 } 12362 if (bbr->rc_in_persist) { 12363 /* 12364 * We are in persists, figure out if 12365 * a retransmit is available (maybe the previous 12366 * persists we sent) or if we have to send new 12367 * data. 12368 */ 12369 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 12370 if (rsm) { 12371 len = rsm->r_end - rsm->r_start; 12372 if (rsm->r_flags & BBR_HAS_FIN) 12373 len--; 12374 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12375 len = maxseg; 12376 if (len > 1) 12377 BBR_STAT_INC(bbr_persist_reneg); 12378 /* 12379 * XXXrrs we could force the len to 12380 * 1 byte here to cause the chunk to 12381 * split apart.. but that would then 12382 * mean we always retransmit it as 12383 * one byte even after the window 12384 * opens. 12385 */ 12386 sack_rxmit = 1; 12387 sb_offset = rsm->r_start - tp->snd_una; 12388 } else { 12389 /* 12390 * First time through in persists or peer 12391 * acked our one byte. Though we do have 12392 * to have something in the sb. 12393 */ 12394 len = 1; 12395 sb_offset = 0; 12396 if (avail == 0) 12397 len = 0; 12398 } 12399 } 12400 } 12401 } 12402 if (prefetch_so_done == 0) { 12403 kern_prefetch(so, &prefetch_so_done); 12404 prefetch_so_done = 1; 12405 } 12406 /* 12407 * Lop off SYN bit if it has already been sent. However, if this is 12408 * SYN-SENT state and if segment contains data and if we don't know 12409 * that foreign host supports TAO, suppress sending segment. 12410 */ 12411 if ((flags & TH_SYN) && (rsm == NULL) && 12412 SEQ_GT(tp->snd_max, tp->snd_una)) { 12413 if (tp->t_state != TCPS_SYN_RECEIVED) 12414 flags &= ~TH_SYN; 12415 /* 12416 * When sending additional segments following a TFO SYN|ACK, 12417 * do not include the SYN bit. 12418 */ 12419 if ((tp->t_flags & TF_FASTOPEN) && 12420 (tp->t_state == TCPS_SYN_RECEIVED)) 12421 flags &= ~TH_SYN; 12422 sb_offset--, len++; 12423 if (sbavail(sb) == 0) 12424 len = 0; 12425 } else if ((flags & TH_SYN) && rsm) { 12426 /* 12427 * Subtract one from the len for the SYN being 12428 * retransmitted. 12429 */ 12430 len--; 12431 } 12432 /* 12433 * Be careful not to send data and/or FIN on SYN segments. This 12434 * measure is needed to prevent interoperability problems with not 12435 * fully conformant TCP implementations. 12436 */ 12437 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 12438 len = 0; 12439 flags &= ~TH_FIN; 12440 } 12441 /* 12442 * On TFO sockets, ensure no data is sent in the following cases: 12443 * 12444 * - When retransmitting SYN|ACK on a passively-created socket 12445 * - When retransmitting SYN on an actively created socket 12446 * - When sending a zero-length cookie (cookie request) on an 12447 * actively created socket 12448 * - When the socket is in the CLOSED state (RST is being sent) 12449 */ 12450 if ((tp->t_flags & TF_FASTOPEN) && 12451 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 12452 ((tp->t_state == TCPS_SYN_SENT) && 12453 (tp->t_tfo_client_cookie_len == 0)) || 12454 (flags & TH_RST))) { 12455 len = 0; 12456 sack_rxmit = 0; 12457 rsm = NULL; 12458 } 12459 /* Without fast-open there should never be data sent on a SYN */ 12460 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) 12461 len = 0; 12462 if (len <= 0) { 12463 /* 12464 * If FIN has been sent but not acked, but we haven't been 12465 * called to retransmit, len will be < 0. Otherwise, window 12466 * shrank after we sent into it. If window shrank to 0, 12467 * cancel pending retransmit, pull snd_nxt back to (closed) 12468 * window, and set the persist timer if it isn't already 12469 * going. If the window didn't close completely, just wait 12470 * for an ACK. 12471 * 12472 * We also do a general check here to ensure that we will 12473 * set the persist timer when we have data to send, but a 12474 * 0-byte window. This makes sure the persist timer is set 12475 * even if the packet hits one of the "goto send" lines 12476 * below. 12477 */ 12478 len = 0; 12479 if ((tp->snd_wnd == 0) && 12480 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12481 (tp->snd_una == tp->snd_max) && 12482 (sb_offset < (int)sbavail(sb))) { 12483 /* 12484 * Not enough room in the rwnd to send 12485 * a paced segment out. 12486 */ 12487 bbr_enter_persist(tp, bbr, cts, __LINE__); 12488 } 12489 } else if ((rsm == NULL) && 12490 (doing_tlp == 0) && 12491 (len < bbr->r_ctl.rc_pace_max_segs)) { 12492 /* 12493 * We are not sending a full segment for 12494 * some reason. Should we not send anything (think 12495 * sws or persists)? 12496 */ 12497 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12498 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12499 (len < (int)(sbavail(sb) - sb_offset))) { 12500 /* 12501 * Here the rwnd is less than 12502 * the pacing size, this is not a retransmit, 12503 * we are established and 12504 * the send is not the last in the socket buffer 12505 * lets not send, and possibly enter persists. 12506 */ 12507 len = 0; 12508 if (tp->snd_max == tp->snd_una) 12509 bbr_enter_persist(tp, bbr, cts, __LINE__); 12510 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) && 12511 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12512 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12513 (len < (int)(sbavail(sb) - sb_offset)) && 12514 (len < bbr_minseg(bbr))) { 12515 /* 12516 * Here we are not retransmitting, and 12517 * the cwnd is not so small that we could 12518 * not send at least a min size (rxt timer 12519 * not having gone off), We have 2 segments or 12520 * more already in flight, its not the tail end 12521 * of the socket buffer and the cwnd is blocking 12522 * us from sending out minimum pacing segment size. 12523 * Lets not send anything. 12524 */ 12525 bbr->rc_cwnd_limited = 1; 12526 len = 0; 12527 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 12528 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12529 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12530 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12531 (len < (int)(sbavail(sb) - sb_offset)) && 12532 (TCPS_HAVEESTABLISHED(tp->t_state))) { 12533 /* 12534 * Here we have a send window but we have 12535 * filled it up and we can't send another pacing segment. 12536 * We also have in flight more than 2 segments 12537 * and we are not completing the sb i.e. we allow 12538 * the last bytes of the sb to go out even if 12539 * its not a full pacing segment. 12540 */ 12541 len = 0; 12542 } 12543 } 12544 /* len will be >= 0 after this point. */ 12545 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 12546 tcp_sndbuf_autoscale(tp, so, sendwin); 12547 /* 12548 * 12549 */ 12550 if (bbr->rc_in_persist && 12551 len && 12552 (rsm == NULL) && 12553 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) { 12554 /* 12555 * We are in persist, not doing a retransmit and don't have enough space 12556 * yet to send a full TSO. So is it at the end of the sb 12557 * if so we need to send else nuke to 0 and don't send. 12558 */ 12559 int sbleft; 12560 if (sbavail(sb) > sb_offset) 12561 sbleft = sbavail(sb) - sb_offset; 12562 else 12563 sbleft = 0; 12564 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) { 12565 /* not at end of sb lets not send */ 12566 len = 0; 12567 } 12568 } 12569 /* 12570 * Decide if we can use TCP Segmentation Offloading (if supported by 12571 * hardware). 12572 * 12573 * TSO may only be used if we are in a pure bulk sending state. The 12574 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 12575 * options prevent using TSO. With TSO the TCP header is the same 12576 * (except for the sequence number) for all generated packets. This 12577 * makes it impossible to transmit any options which vary per 12578 * generated segment or packet. 12579 * 12580 * IPv4 handling has a clear separation of ip options and ip header 12581 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() 12582 * does the right thing below to provide length of just ip options 12583 * and thus checking for ipoptlen is enough to decide if ip options 12584 * are present. 12585 */ 12586 #ifdef INET6 12587 if (isipv6) 12588 ipoptlen = ip6_optlen(inp); 12589 else 12590 #endif 12591 if (inp->inp_options) 12592 ipoptlen = inp->inp_options->m_len - 12593 offsetof(struct ipoption, ipopt_list); 12594 else 12595 ipoptlen = 0; 12596 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12597 /* 12598 * Pre-calculate here as we save another lookup into the darknesses 12599 * of IPsec that way and can actually decide if TSO is ok. 12600 */ 12601 #ifdef INET6 12602 if (isipv6 && IPSEC_ENABLED(ipv6)) 12603 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 12604 #ifdef INET 12605 else 12606 #endif 12607 #endif /* INET6 */ 12608 #ifdef INET 12609 if (IPSEC_ENABLED(ipv4)) 12610 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 12611 #endif /* INET */ 12612 #endif /* IPSEC */ 12613 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12614 ipoptlen += ipsec_optlen; 12615 #endif 12616 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && 12617 (len > maxseg) && 12618 (tp->t_port == 0) && 12619 ((tp->t_flags & TF_SIGNATURE) == 0) && 12620 ipoptlen == 0) 12621 tso = 1; 12622 12623 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 12624 (long)TCP_MAXWIN << tp->rcv_scale); 12625 /* 12626 * Sender silly window avoidance. We transmit under the following 12627 * conditions when len is non-zero: 12628 * 12629 * - We have a full segment (or more with TSO) - This is the last 12630 * buffer in a write()/send() and we are either idle or running 12631 * NODELAY - we've timed out (e.g. persist timer) - we have more 12632 * then 1/2 the maximum send window's worth of data (receiver may be 12633 * limited the window size) - we need to retransmit 12634 */ 12635 if (rsm) 12636 goto send; 12637 if (len) { 12638 if (sack_rxmit) 12639 goto send; 12640 if (len >= p_maxseg) 12641 goto send; 12642 /* 12643 * NOTE! on localhost connections an 'ack' from the remote 12644 * end may occur synchronously with the output and cause us 12645 * to flush a buffer queued with moretocome. XXX 12646 * 12647 */ 12648 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */ 12649 ((tp->t_flags & TF_NODELAY) || 12650 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) && 12651 (tp->t_flags & TF_NOPUSH) == 0) { 12652 goto send; 12653 } 12654 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 12655 goto send; 12656 } 12657 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 12658 goto send; 12659 } 12660 } 12661 /* 12662 * Sending of standalone window updates. 12663 * 12664 * Window updates are important when we close our window due to a 12665 * full socket buffer and are opening it again after the application 12666 * reads data from it. Once the window has opened again and the 12667 * remote end starts to send again the ACK clock takes over and 12668 * provides the most current window information. 12669 * 12670 * We must avoid the silly window syndrome whereas every read from 12671 * the receive buffer, no matter how small, causes a window update 12672 * to be sent. We also should avoid sending a flurry of window 12673 * updates when the socket buffer had queued a lot of data and the 12674 * application is doing small reads. 12675 * 12676 * Prevent a flurry of pointless window updates by only sending an 12677 * update when we can increase the advertized window by more than 12678 * 1/4th of the socket buffer capacity. When the buffer is getting 12679 * full or is very small be more aggressive and send an update 12680 * whenever we can increase by two mss sized segments. In all other 12681 * situations the ACK's to new incoming data will carry further 12682 * window increases. 12683 * 12684 * Don't send an independent window update if a delayed ACK is 12685 * pending (it will get piggy-backed on it) or the remote side 12686 * already has done a half-close and won't send more data. Skip 12687 * this if the connection is in T/TCP half-open state. 12688 */ 12689 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 12690 !(tp->t_flags & TF_DELACK) && 12691 !TCPS_HAVERCVDFIN(tp->t_state)) { 12692 /* Check to see if we should do a window update */ 12693 if (bbr_window_update_needed(tp, so, recwin, maxseg)) 12694 goto send; 12695 } 12696 /* 12697 * Send if we owe the peer an ACK, RST, SYN. ACKNOW 12698 * is also a catch-all for the retransmit timer timeout case. 12699 */ 12700 if (tp->t_flags & TF_ACKNOW) { 12701 goto send; 12702 } 12703 if (flags & TH_RST) { 12704 /* Always send a RST if one is due */ 12705 goto send; 12706 } 12707 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) { 12708 goto send; 12709 } 12710 /* 12711 * If our state indicates that FIN should be sent and we have not 12712 * yet done so, then we need to send. 12713 */ 12714 if (flags & TH_FIN && 12715 ((tp->t_flags & TF_SENTFIN) == 0)) { 12716 goto send; 12717 } 12718 /* 12719 * No reason to send a segment, just return. 12720 */ 12721 just_return: 12722 SOCK_SENDBUF_UNLOCK(so); 12723 just_return_nolock: 12724 if (tot_len) 12725 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 12726 if (bbr->rc_no_pacing) 12727 slot = 0; 12728 if (tot_len == 0) { 12729 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >= 12730 tp->snd_wnd) { 12731 BBR_STAT_INC(bbr_rwnd_limited); 12732 app_limited = BBR_JR_RWND_LIMITED; 12733 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12734 if ((bbr->rc_in_persist == 0) && 12735 TCPS_HAVEESTABLISHED(tp->t_state) && 12736 (tp->snd_max == tp->snd_una) && 12737 sbavail(&so->so_snd)) { 12738 /* No send window.. we must enter persist */ 12739 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 12740 } 12741 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 12742 BBR_STAT_INC(bbr_app_limited); 12743 app_limited = BBR_JR_APP_LIMITED; 12744 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12745 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12746 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) { 12747 BBR_STAT_INC(bbr_cwnd_limited); 12748 app_limited = BBR_JR_CWND_LIMITED; 12749 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12750 bbr->r_ctl.rc_lost_bytes))); 12751 bbr->rc_cwnd_limited = 1; 12752 } else { 12753 BBR_STAT_INC(bbr_app_limited); 12754 app_limited = BBR_JR_APP_LIMITED; 12755 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12756 } 12757 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12758 bbr->r_agg_early_set = 0; 12759 bbr->r_ctl.rc_agg_early = 0; 12760 bbr->r_ctl.rc_last_delay_val = 0; 12761 } else if (bbr->rc_use_google == 0) 12762 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12763 /* Are we app limited? */ 12764 if ((app_limited == BBR_JR_APP_LIMITED) || 12765 (app_limited == BBR_JR_RWND_LIMITED)) { 12766 /** 12767 * We are application limited. 12768 */ 12769 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12770 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered); 12771 } 12772 if (tot_len == 0) 12773 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1); 12774 /* Dont update the time if we did not send */ 12775 bbr->r_ctl.rc_last_delay_val = 0; 12776 bbr->rc_output_starts_timer = 1; 12777 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len); 12778 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len); 12779 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 12780 /* Make sure snd_nxt is drug up */ 12781 tp->snd_nxt = tp->snd_max; 12782 } 12783 return (error); 12784 12785 send: 12786 if (doing_tlp == 0) { 12787 /* 12788 * Data not a TLP, and its not the rxt firing. If it is the 12789 * rxt firing, we want to leave the tlp_in_progress flag on 12790 * so we don't send another TLP. It has to be a rack timer 12791 * or normal send (response to acked data) to clear the tlp 12792 * in progress flag. 12793 */ 12794 bbr->rc_tlp_in_progress = 0; 12795 bbr->rc_tlp_rtx_out = 0; 12796 } else { 12797 /* 12798 * Its a TLP. 12799 */ 12800 bbr->rc_tlp_in_progress = 1; 12801 } 12802 bbr_timer_cancel(bbr, __LINE__, cts); 12803 if (rsm == NULL) { 12804 if (sbused(sb) > 0) { 12805 /* 12806 * This is sub-optimal. We only send a stand alone 12807 * FIN on its own segment. 12808 */ 12809 if (flags & TH_FIN) { 12810 flags &= ~TH_FIN; 12811 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) { 12812 /* Lets not send this */ 12813 slot = 0; 12814 goto just_return; 12815 } 12816 } 12817 } 12818 } else { 12819 /* 12820 * We do *not* send a FIN on a retransmit if it has data. 12821 * The if clause here where len > 1 should never come true. 12822 */ 12823 if ((len > 0) && 12824 (((rsm->r_flags & BBR_HAS_FIN) == 0) && 12825 (flags & TH_FIN))) { 12826 flags &= ~TH_FIN; 12827 len--; 12828 } 12829 } 12830 SOCK_SENDBUF_LOCK_ASSERT(so); 12831 if (len > 0) { 12832 if ((tp->snd_una == tp->snd_max) && 12833 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 12834 /* 12835 * This qualifies as a RTT_PROBE session since we 12836 * drop the data outstanding to nothing and waited 12837 * more than bbr_rtt_probe_time. 12838 */ 12839 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 12840 bbr_set_reduced_rtt(bbr, cts, __LINE__); 12841 } 12842 if (len >= maxseg) 12843 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 12844 else 12845 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 12846 } 12847 /* 12848 * Before ESTABLISHED, force sending of initial options unless TCP 12849 * set not to do any options. NOTE: we assume that the IP/TCP header 12850 * plus TCP options always fit in a single mbuf, leaving room for a 12851 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 12852 * + optlen <= MCLBYTES 12853 */ 12854 optlen = 0; 12855 #ifdef INET6 12856 if (isipv6) 12857 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12858 else 12859 #endif 12860 hdrlen = sizeof(struct tcpiphdr); 12861 12862 /* 12863 * Compute options for segment. We only have to care about SYN and 12864 * established connection segments. Options for SYN-ACK segments 12865 * are handled in TCP syncache. 12866 */ 12867 to.to_flags = 0; 12868 local_options = 0; 12869 if ((tp->t_flags & TF_NOOPT) == 0) { 12870 /* Maximum segment size. */ 12871 if (flags & TH_SYN) { 12872 to.to_mss = tcp_mssopt(&inp->inp_inc); 12873 if (tp->t_port) 12874 to.to_mss -= V_tcp_udp_tunneling_overhead; 12875 to.to_flags |= TOF_MSS; 12876 /* 12877 * On SYN or SYN|ACK transmits on TFO connections, 12878 * only include the TFO option if it is not a 12879 * retransmit, as the presence of the TFO option may 12880 * have caused the original SYN or SYN|ACK to have 12881 * been dropped by a middlebox. 12882 */ 12883 if ((tp->t_flags & TF_FASTOPEN) && 12884 (tp->t_rxtshift == 0)) { 12885 if (tp->t_state == TCPS_SYN_RECEIVED) { 12886 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 12887 to.to_tfo_cookie = 12888 (u_int8_t *)&tp->t_tfo_cookie.server; 12889 to.to_flags |= TOF_FASTOPEN; 12890 wanted_cookie = 1; 12891 } else if (tp->t_state == TCPS_SYN_SENT) { 12892 to.to_tfo_len = 12893 tp->t_tfo_client_cookie_len; 12894 to.to_tfo_cookie = 12895 tp->t_tfo_cookie.client; 12896 to.to_flags |= TOF_FASTOPEN; 12897 wanted_cookie = 1; 12898 } 12899 } 12900 } 12901 /* Window scaling. */ 12902 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 12903 to.to_wscale = tp->request_r_scale; 12904 to.to_flags |= TOF_SCALE; 12905 } 12906 /* Timestamps. */ 12907 if ((tp->t_flags & TF_RCVD_TSTMP) || 12908 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 12909 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset; 12910 to.to_tsecr = tp->ts_recent; 12911 to.to_flags |= TOF_TS; 12912 local_options += TCPOLEN_TIMESTAMP + 2; 12913 } 12914 /* Set receive buffer autosizing timestamp. */ 12915 if (tp->rfbuf_ts == 0 && 12916 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 12917 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv); 12918 /* Selective ACK's. */ 12919 if (flags & TH_SYN) 12920 to.to_flags |= TOF_SACKPERM; 12921 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 12922 tp->rcv_numsacks > 0) { 12923 to.to_flags |= TOF_SACK; 12924 to.to_nsacks = tp->rcv_numsacks; 12925 to.to_sacks = (u_char *)tp->sackblks; 12926 } 12927 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 12928 /* TCP-MD5 (RFC2385). */ 12929 if (tp->t_flags & TF_SIGNATURE) 12930 to.to_flags |= TOF_SIGNATURE; 12931 #endif /* TCP_SIGNATURE */ 12932 12933 /* Processing the options. */ 12934 hdrlen += (optlen = tcp_addoptions(&to, opt)); 12935 /* 12936 * If we wanted a TFO option to be added, but it was unable 12937 * to fit, ensure no data is sent. 12938 */ 12939 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 12940 !(to.to_flags & TOF_FASTOPEN)) 12941 len = 0; 12942 } 12943 if (tp->t_port) { 12944 if (V_tcp_udp_tunneling_port == 0) { 12945 /* The port was removed?? */ 12946 SOCK_SENDBUF_UNLOCK(so); 12947 return (EHOSTUNREACH); 12948 } 12949 hdrlen += sizeof(struct udphdr); 12950 } 12951 #ifdef INET6 12952 if (isipv6) 12953 ipoptlen = ip6_optlen(inp); 12954 else 12955 #endif 12956 if (inp->inp_options) 12957 ipoptlen = inp->inp_options->m_len - 12958 offsetof(struct ipoption, ipopt_list); 12959 else 12960 ipoptlen = 0; 12961 ipoptlen = 0; 12962 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12963 ipoptlen += ipsec_optlen; 12964 #endif 12965 if (bbr->rc_last_options != local_options) { 12966 /* 12967 * Cache the options length this generally does not change 12968 * on a connection. We use this to calculate TSO. 12969 */ 12970 bbr->rc_last_options = local_options; 12971 } 12972 maxseg = tp->t_maxseg - (ipoptlen + optlen); 12973 p_maxseg = min(maxseg, pace_max_segs); 12974 /* 12975 * Adjust data length if insertion of options will bump the packet 12976 * length beyond the t_maxseg length. Clear the FIN bit because we 12977 * cut off the tail of the segment. 12978 */ 12979 if (len > maxseg) { 12980 if (len != 0 && (flags & TH_FIN)) { 12981 flags &= ~TH_FIN; 12982 } 12983 if (tso) { 12984 uint32_t moff; 12985 int32_t max_len; 12986 12987 /* extract TSO information */ 12988 if_hw_tsomax = tp->t_tsomax; 12989 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 12990 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 12991 KASSERT(ipoptlen == 0, 12992 ("%s: TSO can't do IP options", __func__)); 12993 12994 /* 12995 * Check if we should limit by maximum payload 12996 * length: 12997 */ 12998 if (if_hw_tsomax != 0) { 12999 /* compute maximum TSO length */ 13000 max_len = (if_hw_tsomax - hdrlen - 13001 max_linkhdr); 13002 if (max_len <= 0) { 13003 len = 0; 13004 } else if (len > max_len) { 13005 len = max_len; 13006 } 13007 } 13008 /* 13009 * Prevent the last segment from being fractional 13010 * unless the send sockbuf can be emptied: 13011 */ 13012 if ((sb_offset + len) < sbavail(sb)) { 13013 moff = len % (uint32_t)maxseg; 13014 if (moff != 0) { 13015 len -= moff; 13016 } 13017 } 13018 /* 13019 * In case there are too many small fragments don't 13020 * use TSO: 13021 */ 13022 if (len <= maxseg) { 13023 len = maxseg; 13024 tso = 0; 13025 } 13026 } else { 13027 /* Not doing TSO */ 13028 if (optlen + ipoptlen >= tp->t_maxseg) { 13029 /* 13030 * Since we don't have enough space to put 13031 * the IP header chain and the TCP header in 13032 * one packet as required by RFC 7112, don't 13033 * send it. Also ensure that at least one 13034 * byte of the payload can be put into the 13035 * TCP segment. 13036 */ 13037 SOCK_SENDBUF_UNLOCK(so); 13038 error = EMSGSIZE; 13039 sack_rxmit = 0; 13040 goto out; 13041 } 13042 len = maxseg; 13043 } 13044 } else { 13045 /* Not doing TSO */ 13046 if_hw_tsomaxsegcount = 0; 13047 tso = 0; 13048 } 13049 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 13050 ("%s: len > IP_MAXPACKET", __func__)); 13051 #ifdef DIAGNOSTIC 13052 #ifdef INET6 13053 if (max_linkhdr + hdrlen > MCLBYTES) 13054 #else 13055 if (max_linkhdr + hdrlen > MHLEN) 13056 #endif 13057 panic("tcphdr too big"); 13058 #endif 13059 /* 13060 * This KASSERT is here to catch edge cases at a well defined place. 13061 * Before, those had triggered (random) panic conditions further 13062 * down. 13063 */ 13064 #ifdef BBR_INVARIANTS 13065 if (sack_rxmit) { 13066 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 13067 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u", 13068 rsm, tp, bbr, rsm->r_start, tp->snd_una); 13069 } 13070 } 13071 #endif 13072 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 13073 if ((len == 0) && 13074 (flags & TH_FIN) && 13075 (sbused(sb))) { 13076 /* 13077 * We have outstanding data, don't send a fin by itself!. 13078 */ 13079 slot = 0; 13080 goto just_return; 13081 } 13082 /* 13083 * Grab a header mbuf, attaching a copy of data to be transmitted, 13084 * and initialize the header from the template for sends on this 13085 * connection. 13086 */ 13087 if (len) { 13088 uint32_t moff; 13089 13090 /* 13091 * We place a limit on sending with hptsi. 13092 */ 13093 if ((rsm == NULL) && len > pace_max_segs) 13094 len = pace_max_segs; 13095 if (len <= maxseg) 13096 tso = 0; 13097 #ifdef INET6 13098 if (MHLEN < hdrlen + max_linkhdr) 13099 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13100 else 13101 #endif 13102 m = m_gethdr(M_NOWAIT, MT_DATA); 13103 13104 if (m == NULL) { 13105 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13106 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13107 SOCK_SENDBUF_UNLOCK(so); 13108 error = ENOBUFS; 13109 sack_rxmit = 0; 13110 goto out; 13111 } 13112 m->m_data += max_linkhdr; 13113 m->m_len = hdrlen; 13114 /* 13115 * Start the m_copy functions from the closest mbuf to the 13116 * sb_offset in the socket buffer chain. 13117 */ 13118 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) { 13119 #ifdef BBR_INVARIANTS 13120 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) 13121 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u", 13122 tp, bbr, len, sb_offset, sbavail(sb), rsm, 13123 doing_retran_from, 13124 picked_up_retran, 13125 doing_tlp); 13126 13127 #endif 13128 /* 13129 * In this messed up situation we have two choices, 13130 * a) pretend the send worked, and just start timers 13131 * and what not (not good since that may lead us 13132 * back here a lot). <or> b) Send the lowest segment 13133 * in the map. <or> c) Drop the connection. Lets do 13134 * <b> which if it continues to happen will lead to 13135 * <c> via timeouts. 13136 */ 13137 BBR_STAT_INC(bbr_offset_recovery); 13138 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 13139 sb_offset = 0; 13140 if (rsm == NULL) { 13141 sack_rxmit = 0; 13142 len = sbavail(sb); 13143 } else { 13144 sack_rxmit = 1; 13145 if (rsm->r_start != tp->snd_una) { 13146 /* 13147 * Things are really messed up, <c> 13148 * is the only thing to do. 13149 */ 13150 BBR_STAT_INC(bbr_offset_drop); 13151 SOCK_SENDBUF_UNLOCK(so); 13152 (void)m_free(m); 13153 return (-EFAULT); /* tcp_drop() */ 13154 } 13155 len = rsm->r_end - rsm->r_start; 13156 } 13157 if (len > sbavail(sb)) 13158 len = sbavail(sb); 13159 if (len > maxseg) 13160 len = maxseg; 13161 } 13162 mb = sbsndptr_noadv(sb, sb_offset, &moff); 13163 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 13164 m_copydata(mb, moff, (int)len, 13165 mtod(m, caddr_t)+hdrlen); 13166 if (rsm == NULL) 13167 sbsndptr_adv(sb, mb, len); 13168 m->m_len += len; 13169 } else { 13170 struct sockbuf *msb; 13171 13172 if (rsm) 13173 msb = NULL; 13174 else 13175 msb = sb; 13176 #ifdef BBR_INVARIANTS 13177 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) { 13178 if (rsm) { 13179 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 ", 13180 tp, bbr, len, moff, 13181 sbavail(sb), rsm, 13182 tp->snd_una, rsm->r_flags, rsm->r_start, 13183 doing_retran_from, 13184 picked_up_retran, 13185 doing_tlp, sack_rxmit); 13186 } else { 13187 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u", 13188 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una); 13189 } 13190 } 13191 #endif 13192 m->m_next = tcp_m_copym( 13193 mb, moff, &len, 13194 if_hw_tsomaxsegcount, 13195 if_hw_tsomaxsegsize, msb, 13196 ((rsm == NULL) ? hw_tls : 0) 13197 #ifdef NETFLIX_COPY_ARGS 13198 , NULL, NULL 13199 #endif 13200 ); 13201 if (len <= maxseg) { 13202 /* 13203 * Must have ran out of mbufs for the copy 13204 * shorten it to no longer need tso. Lets 13205 * not put on sendalot since we are low on 13206 * mbufs. 13207 */ 13208 tso = 0; 13209 } 13210 if (m->m_next == NULL) { 13211 SOCK_SENDBUF_UNLOCK(so); 13212 (void)m_free(m); 13213 error = ENOBUFS; 13214 sack_rxmit = 0; 13215 goto out; 13216 } 13217 } 13218 #ifdef BBR_INVARIANTS 13219 if (tso && len < maxseg) { 13220 panic("tp:%p tso on, but len:%d < maxseg:%d", 13221 tp, len, maxseg); 13222 } 13223 if (tso && if_hw_tsomaxsegcount) { 13224 int32_t seg_cnt = 0; 13225 struct mbuf *foo; 13226 13227 foo = m; 13228 while (foo) { 13229 seg_cnt++; 13230 foo = foo->m_next; 13231 } 13232 if (seg_cnt > if_hw_tsomaxsegcount) { 13233 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount); 13234 } 13235 } 13236 #endif 13237 /* 13238 * If we're sending everything we've got, set PUSH. (This 13239 * will keep happy those implementations which only give 13240 * data to the user when a buffer fills or a PUSH comes in.) 13241 */ 13242 if (sb_offset + len == sbused(sb) && 13243 sbused(sb) && 13244 !(flags & TH_SYN)) { 13245 flags |= TH_PUSH; 13246 } 13247 SOCK_SENDBUF_UNLOCK(so); 13248 } else { 13249 SOCK_SENDBUF_UNLOCK(so); 13250 if (tp->t_flags & TF_ACKNOW) 13251 KMOD_TCPSTAT_INC(tcps_sndacks); 13252 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 13253 KMOD_TCPSTAT_INC(tcps_sndctrl); 13254 else 13255 KMOD_TCPSTAT_INC(tcps_sndwinup); 13256 13257 m = m_gethdr(M_NOWAIT, MT_DATA); 13258 if (m == NULL) { 13259 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13260 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13261 error = ENOBUFS; 13262 /* Fudge the send time since we could not send */ 13263 sack_rxmit = 0; 13264 goto out; 13265 } 13266 #ifdef INET6 13267 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 13268 MHLEN >= hdrlen) { 13269 M_ALIGN(m, hdrlen); 13270 } else 13271 #endif 13272 m->m_data += max_linkhdr; 13273 m->m_len = hdrlen; 13274 } 13275 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13276 m->m_pkthdr.rcvif = (struct ifnet *)0; 13277 #ifdef MAC 13278 mac_inpcb_create_mbuf(inp, m); 13279 #endif 13280 #ifdef INET6 13281 if (isipv6) { 13282 ip6 = mtod(m, struct ip6_hdr *); 13283 if (tp->t_port) { 13284 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 13285 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13286 udp->uh_dport = tp->t_port; 13287 ulen = hdrlen + len - sizeof(struct ip6_hdr); 13288 udp->uh_ulen = htons(ulen); 13289 th = (struct tcphdr *)(udp + 1); 13290 } else { 13291 th = (struct tcphdr *)(ip6 + 1); 13292 } 13293 tcpip_fillheaders(inp, tp->t_port, ip6, th); 13294 } else 13295 #endif /* INET6 */ 13296 { 13297 ip = mtod(m, struct ip *); 13298 if (tp->t_port) { 13299 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 13300 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13301 udp->uh_dport = tp->t_port; 13302 ulen = hdrlen + len - sizeof(struct ip); 13303 udp->uh_ulen = htons(ulen); 13304 th = (struct tcphdr *)(udp + 1); 13305 } else { 13306 th = (struct tcphdr *)(ip + 1); 13307 } 13308 tcpip_fillheaders(inp, tp->t_port, ip, th); 13309 } 13310 /* 13311 * If we are doing retransmissions, then snd_nxt will not reflect 13312 * the first unsent octet. For ACK only packets, we do not want the 13313 * sequence number of the retransmitted packet, we want the sequence 13314 * number of the next unsent octet. So, if there is no data (and no 13315 * SYN or FIN), use snd_max instead of snd_nxt when filling in 13316 * ti_seq. But if we are in persist state, snd_max might reflect 13317 * one byte beyond the right edge of the window, so use snd_nxt in 13318 * that case, since we know we aren't doing a retransmission. 13319 * (retransmit and persist are mutually exclusive...) 13320 */ 13321 if (sack_rxmit == 0) { 13322 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) { 13323 /* New data (including new persists) */ 13324 th->th_seq = htonl(tp->snd_max); 13325 bbr_seq = tp->snd_max; 13326 } else if (flags & TH_SYN) { 13327 /* Syn's always send from iss */ 13328 th->th_seq = htonl(tp->iss); 13329 bbr_seq = tp->iss; 13330 } else if (flags & TH_FIN) { 13331 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) { 13332 /* 13333 * If we sent the fin already its 1 minus 13334 * snd_max 13335 */ 13336 th->th_seq = (htonl(tp->snd_max - 1)); 13337 bbr_seq = (tp->snd_max - 1); 13338 } else { 13339 /* First time FIN use snd_max */ 13340 th->th_seq = htonl(tp->snd_max); 13341 bbr_seq = tp->snd_max; 13342 } 13343 } else { 13344 /* 13345 * len == 0 and not persist we use snd_max, sending 13346 * an ack unless we have sent the fin then its 1 13347 * minus. 13348 */ 13349 /* 13350 * XXXRRS Question if we are in persists and we have 13351 * nothing outstanding to send and we have not sent 13352 * a FIN, we will send an ACK. In such a case it 13353 * might be better to send (tp->snd_una - 1) which 13354 * would force the peer to ack. 13355 */ 13356 if (tp->t_flags & TF_SENTFIN) { 13357 th->th_seq = htonl(tp->snd_max - 1); 13358 bbr_seq = (tp->snd_max - 1); 13359 } else { 13360 th->th_seq = htonl(tp->snd_max); 13361 bbr_seq = tp->snd_max; 13362 } 13363 } 13364 } else { 13365 /* All retransmits use the rsm to guide the send */ 13366 th->th_seq = htonl(rsm->r_start); 13367 bbr_seq = rsm->r_start; 13368 } 13369 th->th_ack = htonl(tp->rcv_nxt); 13370 if (optlen) { 13371 bcopy(opt, th + 1, optlen); 13372 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 13373 } 13374 tcp_set_flags(th, flags); 13375 /* 13376 * Calculate receive window. Don't shrink window, but avoid silly 13377 * window syndrome. 13378 */ 13379 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) && 13380 recwin < maxseg))) 13381 recwin = 0; 13382 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 13383 recwin < (tp->rcv_adv - tp->rcv_nxt)) 13384 recwin = (tp->rcv_adv - tp->rcv_nxt); 13385 if (recwin > TCP_MAXWIN << tp->rcv_scale) 13386 recwin = TCP_MAXWIN << tp->rcv_scale; 13387 13388 /* 13389 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 13390 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 13391 * handled in syncache. 13392 */ 13393 if (flags & TH_SYN) 13394 th->th_win = htons((u_short) 13395 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 13396 else { 13397 /* Avoid shrinking window with window scaling. */ 13398 recwin = roundup2(recwin, 1 << tp->rcv_scale); 13399 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13400 } 13401 /* 13402 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 13403 * window. This may cause the remote transmitter to stall. This 13404 * flag tells soreceive() to disable delayed acknowledgements when 13405 * draining the buffer. This can occur if the receiver is 13406 * attempting to read more data than can be buffered prior to 13407 * transmitting on the connection. 13408 */ 13409 if (th->th_win == 0) { 13410 tp->t_sndzerowin++; 13411 tp->t_flags |= TF_RXWIN0SENT; 13412 } else 13413 tp->t_flags &= ~TF_RXWIN0SENT; 13414 /* 13415 * We don't support urgent data, but drag along 13416 * the pointer in case of a stack switch. 13417 */ 13418 tp->snd_up = tp->snd_una; 13419 /* 13420 * Put TCP length in extended header, and then checksum extended 13421 * header and data. 13422 */ 13423 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 13424 13425 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 13426 if (to.to_flags & TOF_SIGNATURE) { 13427 /* 13428 * Calculate MD5 signature and put it into the place 13429 * determined before. NOTE: since TCP options buffer doesn't 13430 * point into mbuf's data, calculate offset and use it. 13431 */ 13432 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 13433 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 13434 /* 13435 * Do not send segment if the calculation of MD5 13436 * digest has failed. 13437 */ 13438 goto out; 13439 } 13440 } 13441 #endif 13442 13443 #ifdef INET6 13444 if (isipv6) { 13445 /* 13446 * ip6_plen is not need to be filled now, and will be filled 13447 * in ip6_output. 13448 */ 13449 if (tp->t_port) { 13450 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13451 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13452 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13453 th->th_sum = htons(0); 13454 UDPSTAT_INC(udps_opackets); 13455 } else { 13456 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13457 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13458 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 13459 optlen + len, IPPROTO_TCP, 0); 13460 } 13461 } 13462 #endif 13463 #if defined(INET6) && defined(INET) 13464 else 13465 #endif 13466 #ifdef INET 13467 { 13468 if (tp->t_port) { 13469 m->m_pkthdr.csum_flags = CSUM_UDP; 13470 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13471 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13472 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13473 th->th_sum = htons(0); 13474 UDPSTAT_INC(udps_opackets); 13475 } else { 13476 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP; 13477 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13478 th->th_sum = in_pseudo(ip->ip_src.s_addr, 13479 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13480 IPPROTO_TCP + len + optlen)); 13481 } 13482 /* IP version must be set here for ipv4/ipv6 checking later */ 13483 KASSERT(ip->ip_v == IPVERSION, 13484 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 13485 } 13486 #endif 13487 13488 /* 13489 * Enable TSO and specify the size of the segments. The TCP pseudo 13490 * header checksum is always provided. XXX: Fixme: This is currently 13491 * not the case for IPv6. 13492 */ 13493 if (tso) { 13494 KASSERT(len > maxseg, 13495 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg)); 13496 m->m_pkthdr.csum_flags |= CSUM_TSO; 13497 csum_flags |= CSUM_TSO; 13498 m->m_pkthdr.tso_segsz = maxseg; 13499 } 13500 KASSERT(len + hdrlen == m_length(m, NULL), 13501 ("%s: mbuf chain different than expected: %d + %u != %u", 13502 __func__, len, hdrlen, m_length(m, NULL))); 13503 13504 #ifdef TCP_HHOOK 13505 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */ 13506 hhook_run_tcp_est_out(tp, th, &to, len, tso); 13507 #endif 13508 13509 /* Log to the black box */ 13510 if (tcp_bblogging_on(tp)) { 13511 union tcp_log_stackspecific log; 13512 13513 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 13514 /* Record info on type of transmission */ 13515 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay; 13516 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3); 13517 log.u_bbr.flex3 = maxseg; 13518 log.u_bbr.flex4 = delay_calc; 13519 log.u_bbr.flex5 = bbr->rc_past_init_win; 13520 log.u_bbr.flex5 <<= 1; 13521 log.u_bbr.flex5 |= bbr->rc_no_pacing; 13522 log.u_bbr.flex5 <<= 29; 13523 log.u_bbr.flex5 |= tp->t_maxseg; 13524 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs; 13525 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr); 13526 /* lets poke in the low and the high here for debugging */ 13527 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 13528 if (rsm || sack_rxmit) { 13529 if (doing_tlp) 13530 log.u_bbr.flex8 = 2; 13531 else 13532 log.u_bbr.flex8 = 1; 13533 } else { 13534 log.u_bbr.flex8 = 0; 13535 } 13536 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 13537 len, &log, false, NULL, NULL, 0, tv); 13538 } else { 13539 lgb = NULL; 13540 } 13541 /* 13542 * Fill in IP length and desired time to live and send to IP level. 13543 * There should be a better way to handle ttl and tos; we could keep 13544 * them in the template, but need a way to checksum without them. 13545 */ 13546 /* 13547 * m->m_pkthdr.len should have been set before cksum calcuration, 13548 * because in6_cksum() need it. 13549 */ 13550 #ifdef INET6 13551 if (isipv6) { 13552 /* 13553 * we separately set hoplimit for every segment, since the 13554 * user might want to change the value via setsockopt. Also, 13555 * desired default hop limit might be changed via Neighbor 13556 * Discovery. 13557 */ 13558 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 13559 13560 /* 13561 * Set the packet size here for the benefit of DTrace 13562 * probes. ip6_output() will set it properly; it's supposed 13563 * to include the option header lengths as well. 13564 */ 13565 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 13566 13567 if (V_path_mtu_discovery && maxseg > V_tcp_minmss) 13568 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13569 else 13570 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13571 13572 if (tp->t_state == TCPS_SYN_SENT) 13573 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 13574 13575 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 13576 /* TODO: IPv6 IP6TOS_ECT bit on */ 13577 error = ip6_output(m, inp->in6p_outputopts, 13578 &inp->inp_route6, 13579 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 13580 NULL, NULL, inp); 13581 13582 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 13583 mtu = inp->inp_route6.ro_nh->nh_mtu; 13584 } 13585 #endif /* INET6 */ 13586 #if defined(INET) && defined(INET6) 13587 else 13588 #endif 13589 #ifdef INET 13590 { 13591 ip->ip_len = htons(m->m_pkthdr.len); 13592 #ifdef INET6 13593 if (isipv6) 13594 ip->ip_ttl = in6_selecthlim(inp, NULL); 13595 #endif /* INET6 */ 13596 /* 13597 * If we do path MTU discovery, then we set DF on every 13598 * packet. This might not be the best thing to do according 13599 * to RFC3390 Section 2. However the tcp hostcache migitates 13600 * the problem so it affects only the first tcp connection 13601 * with a host. 13602 * 13603 * NB: Don't set DF on small MTU/MSS to have a safe 13604 * fallback. 13605 */ 13606 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 13607 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13608 if (tp->t_port == 0 || len < V_tcp_minmss) { 13609 ip->ip_off |= htons(IP_DF); 13610 } 13611 } else { 13612 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13613 } 13614 13615 if (tp->t_state == TCPS_SYN_SENT) 13616 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 13617 13618 TCP_PROBE5(send, NULL, tp, ip, tp, th); 13619 13620 error = ip_output(m, inp->inp_options, &inp->inp_route, 13621 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 13622 inp); 13623 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 13624 mtu = inp->inp_route.ro_nh->nh_mtu; 13625 } 13626 #endif /* INET */ 13627 if (lgb) { 13628 lgb->tlb_errno = error; 13629 lgb = NULL; 13630 } 13631 13632 out: 13633 /* 13634 * In transmit state, time the transmission and arrange for the 13635 * retransmit. In persist state, just set snd_max. 13636 */ 13637 if (error == 0) { 13638 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 13639 if (TCPS_HAVEESTABLISHED(tp->t_state) && 13640 (tp->t_flags & TF_SACK_PERMIT) && 13641 tp->rcv_numsacks > 0) 13642 tcp_clean_dsack_blocks(tp); 13643 /* We sent an ack clear the bbr_segs_rcvd count */ 13644 bbr->output_error_seen = 0; 13645 bbr->oerror_cnt = 0; 13646 bbr->bbr_segs_rcvd = 0; 13647 if (len == 0) 13648 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1); 13649 /* Do accounting for new sends */ 13650 if ((len > 0) && (rsm == NULL)) { 13651 int idx; 13652 if (tp->snd_una == tp->snd_max) { 13653 /* 13654 * Special case to match google, when 13655 * nothing is in flight the delivered 13656 * time does get updated to the current 13657 * time (see tcp_rate_bsd.c). 13658 */ 13659 bbr->r_ctl.rc_del_time = cts; 13660 } 13661 if (len >= maxseg) { 13662 idx = (len / maxseg) + 3; 13663 if (idx >= TCP_MSS_ACCT_ATIMER) 13664 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1); 13665 else 13666 counter_u64_add(bbr_out_size[idx], 1); 13667 } else { 13668 /* smaller than a MSS */ 13669 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options); 13670 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV) 13671 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1); 13672 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1); 13673 } 13674 } 13675 } 13676 abandon = 0; 13677 /* 13678 * We must do the send accounting before we log the output, 13679 * otherwise the state of the rsm could change and we account to the 13680 * wrong bucket. 13681 */ 13682 if (len > 0) { 13683 bbr_do_send_accounting(tp, bbr, rsm, len, error); 13684 if (error == 0) { 13685 if (tp->snd_una == tp->snd_max) 13686 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 13687 } 13688 } 13689 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error, 13690 cts, mb, &abandon, rsm, 0, sb); 13691 if (abandon) { 13692 /* 13693 * If bbr_log_output destroys the TCB or sees a TH_RST being 13694 * sent we should hit this condition. 13695 */ 13696 return (0); 13697 } 13698 if (bbr->rc_in_persist == 0) { 13699 /* 13700 * Advance snd_nxt over sequence space of this segment. 13701 */ 13702 if (error) 13703 /* We don't log or do anything with errors */ 13704 goto skip_upd; 13705 13706 if (tp->snd_una == tp->snd_max && 13707 (len || (flags & (TH_SYN | TH_FIN)))) { 13708 /* 13709 * Update the time we just added data since none was 13710 * outstanding. 13711 */ 13712 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13713 bbr->rc_tp->t_acktime = ticks; 13714 } 13715 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) { 13716 if (flags & TH_SYN) { 13717 /* 13718 * Smack the snd_max to iss + 1 13719 * if its a FO we will add len below. 13720 */ 13721 tp->snd_max = tp->iss + 1; 13722 } 13723 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13724 tp->snd_max++; 13725 tp->t_flags |= TF_SENTFIN; 13726 } 13727 } 13728 if (sack_rxmit == 0) 13729 tp->snd_max += len; 13730 skip_upd: 13731 if ((error == 0) && len) 13732 tot_len += len; 13733 } else { 13734 /* Persists case */ 13735 int32_t xlen = len; 13736 13737 if (error) 13738 goto nomore; 13739 13740 if (flags & TH_SYN) 13741 ++xlen; 13742 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13743 ++xlen; 13744 tp->t_flags |= TF_SENTFIN; 13745 } 13746 if (xlen && (tp->snd_una == tp->snd_max)) { 13747 /* 13748 * Update the time we just added data since none was 13749 * outstanding. 13750 */ 13751 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13752 bbr->rc_tp->t_acktime = ticks; 13753 } 13754 if (sack_rxmit == 0) 13755 tp->snd_max += xlen; 13756 tot_len += (len + optlen + ipoptlen); 13757 } 13758 nomore: 13759 if (error) { 13760 /* 13761 * Failures do not advance the seq counter above. For the 13762 * case of ENOBUFS we will fall out and become ack-clocked. 13763 * capping the cwnd at the current flight. 13764 * Everything else will just have to retransmit with the timer 13765 * (no pacer). 13766 */ 13767 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13768 BBR_STAT_INC(bbr_saw_oerr); 13769 /* Clear all delay/early tracks */ 13770 bbr->r_ctl.rc_hptsi_agg_delay = 0; 13771 bbr->r_ctl.rc_agg_early = 0; 13772 bbr->r_agg_early_set = 0; 13773 bbr->output_error_seen = 1; 13774 if (bbr->oerror_cnt < 0xf) 13775 bbr->oerror_cnt++; 13776 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) { 13777 /* drop the session */ 13778 return (-ENETDOWN); 13779 } 13780 switch (error) { 13781 case ENOBUFS: 13782 /* 13783 * Make this guy have to get ack's to send 13784 * more but lets make sure we don't 13785 * slam him below a T-O (1MSS). 13786 */ 13787 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 13788 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13789 bbr->r_ctl.rc_lost_bytes)) - maxseg; 13790 if (tp->snd_cwnd < maxseg) 13791 tp->snd_cwnd = maxseg; 13792 } 13793 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt; 13794 BBR_STAT_INC(bbr_saw_enobuf); 13795 if (bbr->bbr_hdrw_pacing) 13796 counter_u64_add(bbr_hdwr_pacing_enobuf, 1); 13797 else 13798 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1); 13799 /* 13800 * Here even in the enobuf's case we want to do our 13801 * state update. The reason being we may have been 13802 * called by the input function. If so we have had 13803 * things change. 13804 */ 13805 error = 0; 13806 goto enobufs; 13807 case EMSGSIZE: 13808 /* 13809 * For some reason the interface we used initially 13810 * to send segments changed to another or lowered 13811 * its MTU. If TSO was active we either got an 13812 * interface without TSO capabilits or TSO was 13813 * turned off. If we obtained mtu from ip_output() 13814 * then update it and try again. 13815 */ 13816 /* Turn on tracing (or try to) */ 13817 { 13818 int old_maxseg; 13819 13820 old_maxseg = tp->t_maxseg; 13821 BBR_STAT_INC(bbr_saw_emsgsiz); 13822 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts); 13823 if (mtu != 0) 13824 tcp_mss_update(tp, -1, mtu, NULL, NULL); 13825 if (old_maxseg <= tp->t_maxseg) { 13826 /* Huh it did not shrink? */ 13827 tp->t_maxseg = old_maxseg - 40; 13828 if (tp->t_maxseg < V_tcp_mssdflt) { 13829 /* 13830 * The MSS is so small we should not 13831 * process incoming SACK's since we are 13832 * subject to attack in such a case. 13833 */ 13834 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 13835 } else { 13836 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 13837 } 13838 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts); 13839 } 13840 /* 13841 * Nuke all other things that can interfere 13842 * with slot 13843 */ 13844 if ((tot_len + len) && (len >= tp->t_maxseg)) { 13845 slot = bbr_get_pacing_delay(bbr, 13846 bbr->r_ctl.rc_bbr_hptsi_gain, 13847 (tot_len + len), cts, 0); 13848 if (slot < bbr_error_base_paceout) 13849 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13850 } else 13851 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13852 bbr->rc_output_starts_timer = 1; 13853 bbr_start_hpts_timer(bbr, tp, cts, 10, slot, 13854 tot_len); 13855 return (error); 13856 } 13857 case EPERM: 13858 case EACCES: 13859 tp->t_softerror = error; 13860 /* FALLTHROUGH */ 13861 case EHOSTDOWN: 13862 case EHOSTUNREACH: 13863 case ENETDOWN: 13864 case ENETUNREACH: 13865 if (TCPS_HAVERCVDSYN(tp->t_state)) { 13866 tp->t_softerror = error; 13867 error = 0; 13868 } 13869 /* FALLTHROUGH */ 13870 default: 13871 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt; 13872 bbr->rc_output_starts_timer = 1; 13873 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0); 13874 return (error); 13875 } 13876 #ifdef STATS 13877 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) && 13878 len && 13879 (rsm == NULL) && 13880 (bbr->rc_in_persist == 0)) { 13881 tp->gput_seq = bbr_seq; 13882 tp->gput_ack = bbr_seq + 13883 min(sbavail(&so->so_snd) - sb_offset, sendwin); 13884 tp->gput_ts = cts; 13885 tp->t_flags |= TF_GPUTINPROG; 13886 #endif 13887 } 13888 KMOD_TCPSTAT_INC(tcps_sndtotal); 13889 if ((bbr->bbr_hdw_pace_ena) && 13890 (bbr->bbr_attempt_hdwr_pace == 0) && 13891 (bbr->rc_past_init_win) && 13892 (bbr->rc_bbr_state != BBR_STATE_STARTUP) && 13893 (get_filter_value(&bbr->r_ctl.rc_delrate)) && 13894 (inp->inp_route.ro_nh && 13895 inp->inp_route.ro_nh->nh_ifp)) { 13896 /* 13897 * We are past the initial window and 13898 * have at least one measurement so we 13899 * could use hardware pacing if its available. 13900 * We have an interface and we have not attempted 13901 * to setup hardware pacing, lets try to now. 13902 */ 13903 uint64_t rate_wanted; 13904 int err = 0; 13905 13906 rate_wanted = bbr_get_hardware_rate(bbr); 13907 bbr->bbr_attempt_hdwr_pace = 1; 13908 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp, 13909 inp->inp_route.ro_nh->nh_ifp, 13910 rate_wanted, 13911 (RS_PACING_GEQ|RS_PACING_SUB_OK), 13912 &err, NULL); 13913 if (bbr->r_ctl.crte) { 13914 bbr_type_log_hdwr_pacing(bbr, 13915 bbr->r_ctl.crte->ptbl->rs_ifp, 13916 rate_wanted, 13917 bbr->r_ctl.crte->rate, 13918 __LINE__, cts, err); 13919 BBR_STAT_INC(bbr_hdwr_rl_add_ok); 13920 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 13921 counter_u64_add(bbr_flows_whdwr_pacing, 1); 13922 bbr->bbr_hdrw_pacing = 1; 13923 /* Now what is our gain status? */ 13924 if (bbr->r_ctl.crte->rate < rate_wanted) { 13925 /* We have a problem */ 13926 bbr_setup_less_of_rate(bbr, cts, 13927 bbr->r_ctl.crte->rate, rate_wanted); 13928 } else { 13929 /* We are good */ 13930 bbr->gain_is_limited = 0; 13931 bbr->skip_gain = 0; 13932 } 13933 tcp_bbr_tso_size_check(bbr, cts); 13934 } else { 13935 bbr_type_log_hdwr_pacing(bbr, 13936 inp->inp_route.ro_nh->nh_ifp, 13937 rate_wanted, 13938 0, 13939 __LINE__, cts, err); 13940 BBR_STAT_INC(bbr_hdwr_rl_add_fail); 13941 } 13942 } 13943 if (bbr->bbr_hdrw_pacing) { 13944 /* 13945 * Worry about cases where the route 13946 * changes or something happened that we 13947 * lost our hardware pacing possibly during 13948 * the last ip_output call. 13949 */ 13950 if (inp->inp_snd_tag == NULL) { 13951 /* A change during ip output disabled hw pacing? */ 13952 bbr->bbr_hdrw_pacing = 0; 13953 } else if ((inp->inp_route.ro_nh == NULL) || 13954 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) { 13955 /* 13956 * We had an interface or route change, 13957 * detach from the current hdwr pacing 13958 * and setup to re-attempt next go 13959 * round. 13960 */ 13961 bbr->bbr_hdrw_pacing = 0; 13962 bbr->bbr_attempt_hdwr_pace = 0; 13963 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 13964 tcp_bbr_tso_size_check(bbr, cts); 13965 } 13966 } 13967 /* 13968 * Data sent (as far as we can tell). If this advertises a larger 13969 * window than any other segment, then remember the size of the 13970 * advertised window. Any pending ACK has now been sent. 13971 */ 13972 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 13973 tp->rcv_adv = tp->rcv_nxt + recwin; 13974 13975 tp->last_ack_sent = tp->rcv_nxt; 13976 if ((error == 0) && 13977 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) && 13978 (doing_tlp == 0) && 13979 (tso == 0) && 13980 (len > 0) && 13981 ((flags & TH_RST) == 0) && 13982 ((flags & TH_SYN) == 0) && 13983 (IN_RECOVERY(tp->t_flags) == 0) && 13984 (bbr->rc_in_persist == 0) && 13985 (tot_len < bbr->r_ctl.rc_pace_max_segs)) { 13986 /* 13987 * For non-tso we need to goto again until we have sent out 13988 * enough data to match what we are hptsi out every hptsi 13989 * interval. 13990 */ 13991 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13992 /* Make sure snd_nxt is drug up */ 13993 tp->snd_nxt = tp->snd_max; 13994 } 13995 if (rsm != NULL) { 13996 rsm = NULL; 13997 goto skip_again; 13998 } 13999 rsm = NULL; 14000 sack_rxmit = 0; 14001 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 14002 goto again; 14003 } 14004 skip_again: 14005 if ((error == 0) && (flags & TH_FIN)) 14006 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 14007 if ((error == 0) && (flags & TH_RST)) 14008 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 14009 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) { 14010 /* 14011 * Calculate/Re-Calculate the hptsi slot in usecs based on 14012 * what we have sent so far 14013 */ 14014 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 14015 if (bbr->rc_no_pacing) 14016 slot = 0; 14017 } 14018 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 14019 enobufs: 14020 if (bbr->rc_use_google == 0) 14021 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 14022 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 14023 bbr->r_ctl.rc_lost_bytes))); 14024 bbr->rc_output_starts_timer = 1; 14025 if (bbr->bbr_use_rack_cheat && 14026 (more_to_rxt || 14027 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) { 14028 /* Rack cheats and shotguns out all rxt's 1ms apart */ 14029 if (slot > 1000) 14030 slot = 1000; 14031 } 14032 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) { 14033 /* 14034 * We don't change the tso size until some number of sends 14035 * to give the hardware commands time to get down 14036 * to the interface. 14037 */ 14038 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++; 14039 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) { 14040 bbr->hw_pacing_set = 1; 14041 tcp_bbr_tso_size_check(bbr, cts); 14042 } 14043 } 14044 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len); 14045 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 14046 /* Make sure snd_nxt is drug up */ 14047 tp->snd_nxt = tp->snd_max; 14048 } 14049 return (error); 14050 14051 } 14052 14053 /* 14054 * See bbr_output_wtime() for return values. 14055 */ 14056 static int 14057 bbr_output(struct tcpcb *tp) 14058 { 14059 int32_t ret; 14060 struct timeval tv; 14061 14062 NET_EPOCH_ASSERT(); 14063 14064 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14065 (void)tcp_get_usecs(&tv); 14066 ret = bbr_output_wtime(tp, &tv); 14067 return (ret); 14068 } 14069 14070 static void 14071 bbr_mtu_chg(struct tcpcb *tp) 14072 { 14073 struct tcp_bbr *bbr; 14074 struct bbr_sendmap *rsm, *frsm = NULL; 14075 uint32_t maxseg; 14076 14077 /* 14078 * The MTU has changed. a) Clear the sack filter. b) Mark everything 14079 * over the current size as SACK_PASS so a retransmit will occur. 14080 */ 14081 14082 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14083 maxseg = tp->t_maxseg - bbr->rc_last_options; 14084 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 14085 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 14086 /* Don't mess with ones acked (by sack?) */ 14087 if (rsm->r_flags & BBR_ACKED) 14088 continue; 14089 if ((rsm->r_end - rsm->r_start) > maxseg) { 14090 /* 14091 * We mark sack-passed on all the previous large 14092 * sends we did. This will force them to retransmit. 14093 */ 14094 rsm->r_flags |= BBR_SACK_PASSED; 14095 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) && 14096 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) { 14097 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 14098 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 14099 rsm->r_flags |= BBR_MARKED_LOST; 14100 } 14101 if (frsm == NULL) 14102 frsm = rsm; 14103 } 14104 } 14105 if (frsm) { 14106 bbr->r_ctl.rc_resend = frsm; 14107 } 14108 } 14109 14110 static int 14111 bbr_pru_options(struct tcpcb *tp, int flags) 14112 { 14113 if (flags & PRUS_OOB) 14114 return (EOPNOTSUPP); 14115 return (0); 14116 } 14117 14118 static void 14119 bbr_switch_failed(struct tcpcb *tp) 14120 { 14121 /* 14122 * If a switch fails we only need to 14123 * make sure mbuf_queuing is still in place. 14124 * We also need to make sure we are still in 14125 * ticks granularity (though we should probably 14126 * change bbr to go to USECs). 14127 * 14128 * For timers we need to see if we are still in the 14129 * pacer (if our flags are up) if so we are good, if 14130 * not we need to get back into the pacer. 14131 */ 14132 struct timeval tv; 14133 uint32_t cts; 14134 uint32_t toval; 14135 struct tcp_bbr *bbr; 14136 struct hpts_diag diag; 14137 14138 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 14139 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 14140 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 14141 if (tp->t_in_hpts > IHPTS_NONE) { 14142 return; 14143 } 14144 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14145 cts = tcp_get_usecs(&tv); 14146 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14147 if (TSTMP_GT(bbr->rc_pacer_started, cts)) { 14148 toval = bbr->rc_pacer_started - cts; 14149 } else { 14150 /* one slot please */ 14151 toval = HPTS_TICKS_PER_SLOT; 14152 } 14153 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 14154 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 14155 toval = bbr->r_ctl.rc_timer_exp - cts; 14156 } else { 14157 /* one slot please */ 14158 toval = HPTS_TICKS_PER_SLOT; 14159 } 14160 } else 14161 toval = HPTS_TICKS_PER_SLOT; 14162 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval), 14163 __LINE__, &diag); 14164 bbr_log_hpts_diag(bbr, cts, &diag); 14165 } 14166 14167 struct tcp_function_block __tcp_bbr = { 14168 .tfb_tcp_block_name = __XSTRING(STACKNAME), 14169 .tfb_tcp_output = bbr_output, 14170 .tfb_do_queued_segments = ctf_do_queued_segments, 14171 .tfb_do_segment_nounlock = bbr_do_segment_nounlock, 14172 .tfb_tcp_do_segment = bbr_do_segment, 14173 .tfb_tcp_ctloutput = bbr_ctloutput, 14174 .tfb_tcp_fb_init = bbr_init, 14175 .tfb_tcp_fb_fini = bbr_fini, 14176 .tfb_tcp_timer_stop_all = bbr_stopall, 14177 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr, 14178 .tfb_tcp_handoff_ok = bbr_handoff_ok, 14179 .tfb_tcp_mtu_chg = bbr_mtu_chg, 14180 .tfb_pru_options = bbr_pru_options, 14181 .tfb_switch_failed = bbr_switch_failed, 14182 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK, 14183 }; 14184 14185 /* 14186 * bbr_ctloutput() must drop the inpcb lock before performing copyin on 14187 * socket option arguments. When it re-acquires the lock after the copy, it 14188 * has to revalidate that the connection is still valid for the socket 14189 * option. 14190 */ 14191 static int 14192 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14193 { 14194 struct epoch_tracker et; 14195 struct inpcb *inp = tptoinpcb(tp); 14196 struct tcp_bbr *bbr; 14197 int32_t error = 0, optval; 14198 14199 switch (sopt->sopt_level) { 14200 case IPPROTO_IPV6: 14201 case IPPROTO_IP: 14202 return (tcp_default_ctloutput(tp, sopt)); 14203 } 14204 14205 switch (sopt->sopt_name) { 14206 case TCP_RACK_PACE_MAX_SEG: 14207 case TCP_RACK_MIN_TO: 14208 case TCP_RACK_REORD_THRESH: 14209 case TCP_RACK_REORD_FADE: 14210 case TCP_RACK_TLP_THRESH: 14211 case TCP_RACK_PKT_DELAY: 14212 case TCP_BBR_ALGORITHM: 14213 case TCP_BBR_TSLIMITS: 14214 case TCP_BBR_IWINTSO: 14215 case TCP_BBR_STARTUP_PG: 14216 case TCP_BBR_DRAIN_PG: 14217 case TCP_BBR_PROBE_RTT_INT: 14218 case TCP_BBR_PROBE_RTT_GAIN: 14219 case TCP_BBR_PROBE_RTT_LEN: 14220 case TCP_BBR_STARTUP_LOSS_EXIT: 14221 case TCP_BBR_USEDEL_RATE: 14222 case TCP_BBR_MIN_RTO: 14223 case TCP_BBR_MAX_RTO: 14224 case TCP_BBR_PACE_PER_SEC: 14225 case TCP_DELACK: 14226 case TCP_BBR_PACE_DEL_TAR: 14227 case TCP_BBR_SEND_IWND_IN_TSO: 14228 case TCP_BBR_EXTRA_STATE: 14229 case TCP_BBR_UTTER_MAX_TSO: 14230 case TCP_BBR_MIN_TOPACEOUT: 14231 case TCP_BBR_FLOOR_MIN_TSO: 14232 case TCP_BBR_TSTMP_RAISES: 14233 case TCP_BBR_POLICER_DETECT: 14234 case TCP_BBR_USE_RACK_CHEAT: 14235 case TCP_DATA_AFTER_CLOSE: 14236 case TCP_BBR_HDWR_PACE: 14237 case TCP_BBR_PACE_SEG_MAX: 14238 case TCP_BBR_PACE_SEG_MIN: 14239 case TCP_BBR_PACE_CROSS: 14240 case TCP_BBR_PACE_OH: 14241 case TCP_BBR_TMR_PACE_OH: 14242 case TCP_BBR_RACK_RTT_USE: 14243 case TCP_BBR_RETRAN_WTSO: 14244 break; 14245 default: 14246 return (tcp_default_ctloutput(tp, sopt)); 14247 break; 14248 } 14249 INP_WUNLOCK(inp); 14250 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 14251 if (error) 14252 return (error); 14253 INP_WLOCK(inp); 14254 if (inp->inp_flags & INP_DROPPED) { 14255 INP_WUNLOCK(inp); 14256 return (ECONNRESET); 14257 } 14258 if (tp->t_fb != &__tcp_bbr) { 14259 INP_WUNLOCK(inp); 14260 return (ENOPROTOOPT); 14261 } 14262 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14263 switch (sopt->sopt_name) { 14264 case TCP_BBR_PACE_PER_SEC: 14265 BBR_OPTS_INC(tcp_bbr_pace_per_sec); 14266 bbr->r_ctl.bbr_hptsi_per_second = optval; 14267 break; 14268 case TCP_BBR_PACE_DEL_TAR: 14269 BBR_OPTS_INC(tcp_bbr_pace_del_tar); 14270 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval; 14271 break; 14272 case TCP_BBR_PACE_SEG_MAX: 14273 BBR_OPTS_INC(tcp_bbr_pace_seg_max); 14274 bbr->r_ctl.bbr_hptsi_segments_max = optval; 14275 break; 14276 case TCP_BBR_PACE_SEG_MIN: 14277 BBR_OPTS_INC(tcp_bbr_pace_seg_min); 14278 bbr->r_ctl.bbr_hptsi_bytes_min = optval; 14279 break; 14280 case TCP_BBR_PACE_CROSS: 14281 BBR_OPTS_INC(tcp_bbr_pace_cross); 14282 bbr->r_ctl.bbr_cross_over = optval; 14283 break; 14284 case TCP_BBR_ALGORITHM: 14285 BBR_OPTS_INC(tcp_bbr_algorithm); 14286 if (optval && (bbr->rc_use_google == 0)) { 14287 /* Turn on the google mode */ 14288 bbr_google_mode_on(bbr); 14289 if ((optval > 3) && (optval < 500)) { 14290 /* 14291 * Must be at least greater than .3% 14292 * and must be less than 50.0%. 14293 */ 14294 bbr->r_ctl.bbr_google_discount = optval; 14295 } 14296 } else if ((optval == 0) && (bbr->rc_use_google == 1)) { 14297 /* Turn off the google mode */ 14298 bbr_google_mode_off(bbr); 14299 } 14300 break; 14301 case TCP_BBR_TSLIMITS: 14302 BBR_OPTS_INC(tcp_bbr_tslimits); 14303 if (optval == 1) 14304 bbr->rc_use_ts_limit = 1; 14305 else if (optval == 0) 14306 bbr->rc_use_ts_limit = 0; 14307 else 14308 error = EINVAL; 14309 break; 14310 14311 case TCP_BBR_IWINTSO: 14312 BBR_OPTS_INC(tcp_bbr_iwintso); 14313 if ((optval >= 0) && (optval < 128)) { 14314 uint32_t twin; 14315 14316 bbr->rc_init_win = optval; 14317 twin = bbr_initial_cwnd(bbr, tp); 14318 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd)) 14319 tp->snd_cwnd = twin; 14320 else 14321 error = EBUSY; 14322 } else 14323 error = EINVAL; 14324 break; 14325 case TCP_BBR_STARTUP_PG: 14326 BBR_OPTS_INC(tcp_bbr_startup_pg); 14327 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) { 14328 bbr->r_ctl.rc_startup_pg = optval; 14329 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 14330 bbr->r_ctl.rc_bbr_hptsi_gain = optval; 14331 } 14332 } else 14333 error = EINVAL; 14334 break; 14335 case TCP_BBR_DRAIN_PG: 14336 BBR_OPTS_INC(tcp_bbr_drain_pg); 14337 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) 14338 bbr->r_ctl.rc_drain_pg = optval; 14339 else 14340 error = EINVAL; 14341 break; 14342 case TCP_BBR_PROBE_RTT_LEN: 14343 BBR_OPTS_INC(tcp_bbr_probertt_len); 14344 if (optval <= 1) 14345 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND)); 14346 else 14347 error = EINVAL; 14348 break; 14349 case TCP_BBR_PROBE_RTT_GAIN: 14350 BBR_OPTS_INC(tcp_bbr_probertt_gain); 14351 if (optval <= BBR_UNIT) 14352 bbr->r_ctl.bbr_rttprobe_gain_val = optval; 14353 else 14354 error = EINVAL; 14355 break; 14356 case TCP_BBR_PROBE_RTT_INT: 14357 BBR_OPTS_INC(tcp_bbr_probe_rtt_int); 14358 if (optval > 1000) 14359 bbr->r_ctl.rc_probertt_int = optval; 14360 else 14361 error = EINVAL; 14362 break; 14363 case TCP_BBR_MIN_TOPACEOUT: 14364 BBR_OPTS_INC(tcp_bbr_topaceout); 14365 if (optval == 0) { 14366 bbr->no_pacing_until = 0; 14367 bbr->rc_no_pacing = 0; 14368 } else if (optval <= 0x00ff) { 14369 bbr->no_pacing_until = optval; 14370 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) && 14371 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){ 14372 /* Turn on no pacing */ 14373 bbr->rc_no_pacing = 1; 14374 } 14375 } else 14376 error = EINVAL; 14377 break; 14378 case TCP_BBR_STARTUP_LOSS_EXIT: 14379 BBR_OPTS_INC(tcp_bbr_startup_loss_exit); 14380 bbr->rc_loss_exit = optval; 14381 break; 14382 case TCP_BBR_USEDEL_RATE: 14383 error = EINVAL; 14384 break; 14385 case TCP_BBR_MIN_RTO: 14386 BBR_OPTS_INC(tcp_bbr_min_rto); 14387 bbr->r_ctl.rc_min_rto_ms = optval; 14388 break; 14389 case TCP_BBR_MAX_RTO: 14390 BBR_OPTS_INC(tcp_bbr_max_rto); 14391 bbr->rc_max_rto_sec = optval; 14392 break; 14393 case TCP_RACK_MIN_TO: 14394 /* Minimum time between rack t-o's in ms */ 14395 BBR_OPTS_INC(tcp_rack_min_to); 14396 bbr->r_ctl.rc_min_to = optval; 14397 break; 14398 case TCP_RACK_REORD_THRESH: 14399 /* RACK reorder threshold (shift amount) */ 14400 BBR_OPTS_INC(tcp_rack_reord_thresh); 14401 if ((optval > 0) && (optval < 31)) 14402 bbr->r_ctl.rc_reorder_shift = optval; 14403 else 14404 error = EINVAL; 14405 break; 14406 case TCP_RACK_REORD_FADE: 14407 /* Does reordering fade after ms time */ 14408 BBR_OPTS_INC(tcp_rack_reord_fade); 14409 bbr->r_ctl.rc_reorder_fade = optval; 14410 break; 14411 case TCP_RACK_TLP_THRESH: 14412 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14413 BBR_OPTS_INC(tcp_rack_tlp_thresh); 14414 if (optval) 14415 bbr->rc_tlp_threshold = optval; 14416 else 14417 error = EINVAL; 14418 break; 14419 case TCP_BBR_USE_RACK_CHEAT: 14420 BBR_OPTS_INC(tcp_use_rackcheat); 14421 if (bbr->rc_use_google) { 14422 error = EINVAL; 14423 break; 14424 } 14425 BBR_OPTS_INC(tcp_rack_cheat); 14426 if (optval) 14427 bbr->bbr_use_rack_cheat = 1; 14428 else 14429 bbr->bbr_use_rack_cheat = 0; 14430 break; 14431 case TCP_BBR_FLOOR_MIN_TSO: 14432 BBR_OPTS_INC(tcp_utter_max_tso); 14433 if ((optval >= 0) && (optval < 40)) 14434 bbr->r_ctl.bbr_hptsi_segments_floor = optval; 14435 else 14436 error = EINVAL; 14437 break; 14438 case TCP_BBR_UTTER_MAX_TSO: 14439 BBR_OPTS_INC(tcp_utter_max_tso); 14440 if ((optval >= 0) && (optval < 0xffff)) 14441 bbr->r_ctl.bbr_utter_max = optval; 14442 else 14443 error = EINVAL; 14444 break; 14445 14446 case TCP_BBR_EXTRA_STATE: 14447 BBR_OPTS_INC(tcp_extra_state); 14448 if (optval) 14449 bbr->rc_use_idle_restart = 1; 14450 else 14451 bbr->rc_use_idle_restart = 0; 14452 break; 14453 case TCP_BBR_SEND_IWND_IN_TSO: 14454 BBR_OPTS_INC(tcp_iwnd_tso); 14455 if (optval) { 14456 bbr->bbr_init_win_cheat = 1; 14457 if (bbr->rc_past_init_win == 0) { 14458 uint32_t cts; 14459 cts = tcp_get_usecs(&bbr->rc_tv); 14460 tcp_bbr_tso_size_check(bbr, cts); 14461 } 14462 } else 14463 bbr->bbr_init_win_cheat = 0; 14464 break; 14465 case TCP_BBR_HDWR_PACE: 14466 BBR_OPTS_INC(tcp_hdwr_pacing); 14467 if (optval){ 14468 bbr->bbr_hdw_pace_ena = 1; 14469 bbr->bbr_attempt_hdwr_pace = 0; 14470 } else { 14471 bbr->bbr_hdw_pace_ena = 0; 14472 #ifdef RATELIMIT 14473 if (bbr->r_ctl.crte != NULL) { 14474 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 14475 bbr->r_ctl.crte = NULL; 14476 } 14477 #endif 14478 } 14479 break; 14480 14481 case TCP_DELACK: 14482 BBR_OPTS_INC(tcp_delack); 14483 if (optval < 100) { 14484 if (optval == 0) /* off */ 14485 tp->t_delayed_ack = 0; 14486 else if (optval == 1) /* on which is 2 */ 14487 tp->t_delayed_ack = 2; 14488 else /* higher than 2 and less than 100 */ 14489 tp->t_delayed_ack = optval; 14490 if (tp->t_flags & TF_DELACK) { 14491 tp->t_flags &= ~TF_DELACK; 14492 tp->t_flags |= TF_ACKNOW; 14493 NET_EPOCH_ENTER(et); 14494 bbr_output(tp); 14495 NET_EPOCH_EXIT(et); 14496 } 14497 } else 14498 error = EINVAL; 14499 break; 14500 case TCP_RACK_PKT_DELAY: 14501 /* RACK added ms i.e. rack-rtt + reord + N */ 14502 BBR_OPTS_INC(tcp_rack_pkt_delay); 14503 bbr->r_ctl.rc_pkt_delay = optval; 14504 break; 14505 14506 case TCP_BBR_RETRAN_WTSO: 14507 BBR_OPTS_INC(tcp_retran_wtso); 14508 if (optval) 14509 bbr->rc_resends_use_tso = 1; 14510 else 14511 bbr->rc_resends_use_tso = 0; 14512 break; 14513 case TCP_DATA_AFTER_CLOSE: 14514 BBR_OPTS_INC(tcp_data_ac); 14515 if (optval) 14516 bbr->rc_allow_data_af_clo = 1; 14517 else 14518 bbr->rc_allow_data_af_clo = 0; 14519 break; 14520 case TCP_BBR_POLICER_DETECT: 14521 BBR_OPTS_INC(tcp_policer_det); 14522 if (bbr->rc_use_google == 0) 14523 error = EINVAL; 14524 else if (optval) 14525 bbr->r_use_policer = 1; 14526 else 14527 bbr->r_use_policer = 0; 14528 break; 14529 14530 case TCP_BBR_TSTMP_RAISES: 14531 BBR_OPTS_INC(tcp_ts_raises); 14532 if (optval) 14533 bbr->ts_can_raise = 1; 14534 else 14535 bbr->ts_can_raise = 0; 14536 break; 14537 case TCP_BBR_TMR_PACE_OH: 14538 BBR_OPTS_INC(tcp_pacing_oh_tmr); 14539 if (bbr->rc_use_google) { 14540 error = EINVAL; 14541 } else { 14542 if (optval) 14543 bbr->r_ctl.rc_incr_tmrs = 1; 14544 else 14545 bbr->r_ctl.rc_incr_tmrs = 0; 14546 } 14547 break; 14548 case TCP_BBR_PACE_OH: 14549 BBR_OPTS_INC(tcp_pacing_oh); 14550 if (bbr->rc_use_google) { 14551 error = EINVAL; 14552 } else { 14553 if (optval > (BBR_INCL_TCP_OH| 14554 BBR_INCL_IP_OH| 14555 BBR_INCL_ENET_OH)) { 14556 error = EINVAL; 14557 break; 14558 } 14559 if (optval & BBR_INCL_TCP_OH) 14560 bbr->r_ctl.rc_inc_tcp_oh = 1; 14561 else 14562 bbr->r_ctl.rc_inc_tcp_oh = 0; 14563 if (optval & BBR_INCL_IP_OH) 14564 bbr->r_ctl.rc_inc_ip_oh = 1; 14565 else 14566 bbr->r_ctl.rc_inc_ip_oh = 0; 14567 if (optval & BBR_INCL_ENET_OH) 14568 bbr->r_ctl.rc_inc_enet_oh = 1; 14569 else 14570 bbr->r_ctl.rc_inc_enet_oh = 0; 14571 } 14572 break; 14573 default: 14574 return (tcp_default_ctloutput(tp, sopt)); 14575 break; 14576 } 14577 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 14578 INP_WUNLOCK(inp); 14579 return (error); 14580 } 14581 14582 /* 14583 * return 0 on success, error-num on failure 14584 */ 14585 static int 14586 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14587 { 14588 struct inpcb *inp = tptoinpcb(tp); 14589 struct tcp_bbr *bbr; 14590 uint64_t loptval; 14591 int32_t error, optval; 14592 14593 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14594 if (bbr == NULL) { 14595 INP_WUNLOCK(inp); 14596 return (EINVAL); 14597 } 14598 /* 14599 * Because all our options are either boolean or an int, we can just 14600 * pull everything into optval and then unlock and copy. If we ever 14601 * add a option that is not a int, then this will have quite an 14602 * impact to this routine. 14603 */ 14604 switch (sopt->sopt_name) { 14605 case TCP_BBR_PACE_PER_SEC: 14606 optval = bbr->r_ctl.bbr_hptsi_per_second; 14607 break; 14608 case TCP_BBR_PACE_DEL_TAR: 14609 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar; 14610 break; 14611 case TCP_BBR_PACE_SEG_MAX: 14612 optval = bbr->r_ctl.bbr_hptsi_segments_max; 14613 break; 14614 case TCP_BBR_MIN_TOPACEOUT: 14615 optval = bbr->no_pacing_until; 14616 break; 14617 case TCP_BBR_PACE_SEG_MIN: 14618 optval = bbr->r_ctl.bbr_hptsi_bytes_min; 14619 break; 14620 case TCP_BBR_PACE_CROSS: 14621 optval = bbr->r_ctl.bbr_cross_over; 14622 break; 14623 case TCP_BBR_ALGORITHM: 14624 optval = bbr->rc_use_google; 14625 break; 14626 case TCP_BBR_TSLIMITS: 14627 optval = bbr->rc_use_ts_limit; 14628 break; 14629 case TCP_BBR_IWINTSO: 14630 optval = bbr->rc_init_win; 14631 break; 14632 case TCP_BBR_STARTUP_PG: 14633 optval = bbr->r_ctl.rc_startup_pg; 14634 break; 14635 case TCP_BBR_DRAIN_PG: 14636 optval = bbr->r_ctl.rc_drain_pg; 14637 break; 14638 case TCP_BBR_PROBE_RTT_INT: 14639 optval = bbr->r_ctl.rc_probertt_int; 14640 break; 14641 case TCP_BBR_PROBE_RTT_LEN: 14642 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND); 14643 break; 14644 case TCP_BBR_PROBE_RTT_GAIN: 14645 optval = bbr->r_ctl.bbr_rttprobe_gain_val; 14646 break; 14647 case TCP_BBR_STARTUP_LOSS_EXIT: 14648 optval = bbr->rc_loss_exit; 14649 break; 14650 case TCP_BBR_USEDEL_RATE: 14651 loptval = get_filter_value(&bbr->r_ctl.rc_delrate); 14652 break; 14653 case TCP_BBR_MIN_RTO: 14654 optval = bbr->r_ctl.rc_min_rto_ms; 14655 break; 14656 case TCP_BBR_MAX_RTO: 14657 optval = bbr->rc_max_rto_sec; 14658 break; 14659 case TCP_RACK_PACE_MAX_SEG: 14660 /* Max segments in a pace */ 14661 optval = bbr->r_ctl.rc_pace_max_segs; 14662 break; 14663 case TCP_RACK_MIN_TO: 14664 /* Minimum time between rack t-o's in ms */ 14665 optval = bbr->r_ctl.rc_min_to; 14666 break; 14667 case TCP_RACK_REORD_THRESH: 14668 /* RACK reorder threshold (shift amount) */ 14669 optval = bbr->r_ctl.rc_reorder_shift; 14670 break; 14671 case TCP_RACK_REORD_FADE: 14672 /* Does reordering fade after ms time */ 14673 optval = bbr->r_ctl.rc_reorder_fade; 14674 break; 14675 case TCP_BBR_USE_RACK_CHEAT: 14676 /* Do we use the rack cheat for rxt */ 14677 optval = bbr->bbr_use_rack_cheat; 14678 break; 14679 case TCP_BBR_FLOOR_MIN_TSO: 14680 optval = bbr->r_ctl.bbr_hptsi_segments_floor; 14681 break; 14682 case TCP_BBR_UTTER_MAX_TSO: 14683 optval = bbr->r_ctl.bbr_utter_max; 14684 break; 14685 case TCP_BBR_SEND_IWND_IN_TSO: 14686 /* Do we send TSO size segments initially */ 14687 optval = bbr->bbr_init_win_cheat; 14688 break; 14689 case TCP_BBR_EXTRA_STATE: 14690 optval = bbr->rc_use_idle_restart; 14691 break; 14692 case TCP_RACK_TLP_THRESH: 14693 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14694 optval = bbr->rc_tlp_threshold; 14695 break; 14696 case TCP_RACK_PKT_DELAY: 14697 /* RACK added ms i.e. rack-rtt + reord + N */ 14698 optval = bbr->r_ctl.rc_pkt_delay; 14699 break; 14700 case TCP_BBR_RETRAN_WTSO: 14701 optval = bbr->rc_resends_use_tso; 14702 break; 14703 case TCP_DATA_AFTER_CLOSE: 14704 optval = bbr->rc_allow_data_af_clo; 14705 break; 14706 case TCP_DELACK: 14707 optval = tp->t_delayed_ack; 14708 break; 14709 case TCP_BBR_HDWR_PACE: 14710 optval = bbr->bbr_hdw_pace_ena; 14711 break; 14712 case TCP_BBR_POLICER_DETECT: 14713 optval = bbr->r_use_policer; 14714 break; 14715 case TCP_BBR_TSTMP_RAISES: 14716 optval = bbr->ts_can_raise; 14717 break; 14718 case TCP_BBR_TMR_PACE_OH: 14719 optval = bbr->r_ctl.rc_incr_tmrs; 14720 break; 14721 case TCP_BBR_PACE_OH: 14722 optval = 0; 14723 if (bbr->r_ctl.rc_inc_tcp_oh) 14724 optval |= BBR_INCL_TCP_OH; 14725 if (bbr->r_ctl.rc_inc_ip_oh) 14726 optval |= BBR_INCL_IP_OH; 14727 if (bbr->r_ctl.rc_inc_enet_oh) 14728 optval |= BBR_INCL_ENET_OH; 14729 break; 14730 default: 14731 return (tcp_default_ctloutput(tp, sopt)); 14732 break; 14733 } 14734 INP_WUNLOCK(inp); 14735 if (sopt->sopt_name == TCP_BBR_USEDEL_RATE) 14736 error = sooptcopyout(sopt, &loptval, sizeof loptval); 14737 else 14738 error = sooptcopyout(sopt, &optval, sizeof optval); 14739 return (error); 14740 } 14741 14742 /* 14743 * return 0 on success, error-num on failure 14744 */ 14745 static int 14746 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 14747 { 14748 if (sopt->sopt_dir == SOPT_SET) { 14749 return (bbr_set_sockopt(tp, sopt)); 14750 } else if (sopt->sopt_dir == SOPT_GET) { 14751 return (bbr_get_sockopt(tp, sopt)); 14752 } else { 14753 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 14754 } 14755 } 14756 14757 static const char *bbr_stack_names[] = { 14758 __XSTRING(STACKNAME), 14759 #ifdef STACKALIAS 14760 __XSTRING(STACKALIAS), 14761 #endif 14762 }; 14763 14764 static bool bbr_mod_inited = false; 14765 14766 static int 14767 tcp_addbbr(module_t mod, int32_t type, void *data) 14768 { 14769 int32_t err = 0; 14770 int num_stacks; 14771 14772 switch (type) { 14773 case MOD_LOAD: 14774 printf("Attempting to load " __XSTRING(MODNAME) "\n"); 14775 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 14776 sizeof(struct bbr_sendmap), 14777 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 14778 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 14779 sizeof(struct tcp_bbr), 14780 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 14781 sysctl_ctx_init(&bbr_sysctl_ctx); 14782 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 14783 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 14784 OID_AUTO, 14785 #ifdef STACKALIAS 14786 __XSTRING(STACKALIAS), 14787 #else 14788 __XSTRING(STACKNAME), 14789 #endif 14790 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 14791 ""); 14792 if (bbr_sysctl_root == NULL) { 14793 printf("Failed to add sysctl node\n"); 14794 err = EFAULT; 14795 goto free_uma; 14796 } 14797 bbr_init_sysctls(); 14798 num_stacks = nitems(bbr_stack_names); 14799 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK, 14800 bbr_stack_names, &num_stacks); 14801 if (err) { 14802 printf("Failed to register %s stack name for " 14803 "%s module\n", bbr_stack_names[num_stacks], 14804 __XSTRING(MODNAME)); 14805 sysctl_ctx_free(&bbr_sysctl_ctx); 14806 free_uma: 14807 uma_zdestroy(bbr_zone); 14808 uma_zdestroy(bbr_pcb_zone); 14809 bbr_counter_destroy(); 14810 printf("Failed to register " __XSTRING(MODNAME) 14811 " module err:%d\n", err); 14812 return (err); 14813 } 14814 tcp_lro_reg_mbufq(); 14815 bbr_mod_inited = true; 14816 printf(__XSTRING(MODNAME) " is now available\n"); 14817 break; 14818 case MOD_QUIESCE: 14819 err = deregister_tcp_functions(&__tcp_bbr, true, false); 14820 break; 14821 case MOD_UNLOAD: 14822 err = deregister_tcp_functions(&__tcp_bbr, false, true); 14823 if (err == EBUSY) 14824 break; 14825 if (bbr_mod_inited) { 14826 uma_zdestroy(bbr_zone); 14827 uma_zdestroy(bbr_pcb_zone); 14828 sysctl_ctx_free(&bbr_sysctl_ctx); 14829 bbr_counter_destroy(); 14830 printf(__XSTRING(MODNAME) 14831 " is now no longer available\n"); 14832 bbr_mod_inited = false; 14833 } 14834 tcp_lro_dereg_mbufq(); 14835 err = 0; 14836 break; 14837 default: 14838 return (EOPNOTSUPP); 14839 } 14840 return (err); 14841 } 14842 14843 static moduledata_t tcp_bbr = { 14844 .name = __XSTRING(MODNAME), 14845 .evhand = tcp_addbbr, 14846 .priv = 0 14847 }; 14848 14849 MODULE_VERSION(MODNAME, 1); 14850 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 14851 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 14852