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 <[email protected]>
28 * This work is based on the ACM Queue paper
29 * BBR - Congestion Based Congestion Control
30 * and also numerous discussions with Neal, Yuchung and Van.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_tcpdebug.h"
40 #include "opt_ratelimit.h"
41 #include <sys/param.h>
42 #include <sys/arb.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #ifdef TCP_HHOOK
47 #include <sys/hhook.h>
48 #endif
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #ifdef STATS
57 #include <sys/qmath.h>
58 #include <sys/tree.h>
59 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
60 #endif
61 #include <sys/refcount.h>
62 #include <sys/queue.h>
63 #include <sys/eventhandler.h>
64 #include <sys/smp.h>
65 #include <sys/kthread.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <sys/tim_filter.h>
69 #include <sys/time.h>
70 #include <sys/protosw.h>
71 #include <vm/uma.h>
72 #include <sys/kern_prefetch.h>
73
74 #include <net/route.h>
75 #include <net/route/nhop.h>
76 #include <net/vnet.h>
77
78 #define TCPSTATES /* for logging */
79
80 #include <netinet/in.h>
81 #include <netinet/in_kdtrace.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/ip.h>
84 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
85 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
86 #include <netinet/ip_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet6/in6_pcb.h>
89 #include <netinet6/ip6_var.h>
90 #define TCPOUTFLAGS
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_fsm.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcpip.h>
97 #include <netinet/tcp_hpts.h>
98 #include <netinet/cc/cc.h>
99 #include <netinet/tcp_log_buf.h>
100 #include <netinet/tcp_ratelimit.h>
101 #include <netinet/tcp_lro.h>
102 #ifdef TCPDEBUG
103 #include <netinet/tcp_debug.h>
104 #endif /* TCPDEBUG */
105 #ifdef TCP_OFFLOAD
106 #include <netinet/tcp_offload.h>
107 #endif
108 #ifdef INET6
109 #include <netinet6/tcp6_var.h>
110 #endif
111 #include <netinet/tcp_fastopen.h>
112
113 #include <netipsec/ipsec_support.h>
114 #include <net/if.h>
115 #include <net/if_var.h>
116 #include <net/ethernet.h>
117
118 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
119 #include <netipsec/ipsec.h>
120 #include <netipsec/ipsec6.h>
121 #endif /* IPSEC */
122
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
125 #include <machine/in_cksum.h>
126
127 #ifdef MAC
128 #include <security/mac/mac_framework.h>
129 #endif
130
131 #include "sack_filter.h"
132 #include "tcp_bbr.h"
133 #include "rack_bbr_common.h"
134 uma_zone_t bbr_zone;
135 uma_zone_t bbr_pcb_zone;
136
137 struct sysctl_ctx_list bbr_sysctl_ctx;
138 struct sysctl_oid *bbr_sysctl_root;
139
140 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
141 (tv) = (value); \
142 if ((u_long)(tv) < (u_long)(tvmin)) \
143 (tv) = (tvmin); \
144 if ((u_long)(tv) > (u_long)(tvmax)) \
145 (tv) = (tvmax); \
146 } while(0)
147
148 /*#define BBR_INVARIANT 1*/
149
150 #ifdef FSTACK
151 #define MODNAME tcp_bbr
152 #define STACKNAME bbr
153 #endif
154
155 /*
156 * initial window
157 */
158 static uint32_t bbr_def_init_win = 10;
159 static int32_t bbr_persist_min = 250000; /* 250ms */
160 static int32_t bbr_persist_max = 1000000; /* 1 Second */
161 static int32_t bbr_cwnd_may_shrink = 0;
162 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
163 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
164 static int32_t bbr_hardware_pacing_limit = 8000;
165 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
166 static int32_t bbr_no_retran = 0;
167
168 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
169 static int32_t bbr_max_net_error_cnt = 10;
170 /* Should the following be dynamic too -- loss wise */
171 static int32_t bbr_rtt_gain_thresh = 0;
172 /* Measurement controls */
173 static int32_t bbr_use_google_algo = 1;
174 static int32_t bbr_ts_limiting = 1;
175 static int32_t bbr_ts_can_raise = 0;
176 static int32_t bbr_do_red = 600;
177 static int32_t bbr_red_scale = 20000;
178 static int32_t bbr_red_mul = 1;
179 static int32_t bbr_red_div = 2;
180 static int32_t bbr_red_growth_restrict = 1;
181 static int32_t bbr_target_is_bbunit = 0;
182 static int32_t bbr_drop_limit = 0;
183 /*
184 * How much gain do we need to see to
185 * stay in startup?
186 */
187 static int32_t bbr_marks_rxt_sack_passed = 0;
188 static int32_t bbr_start_exit = 25;
189 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
190 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
191 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
192 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
193 * if we go back ever to where the pacer
194 * has priority over timers.
195 */
196 static int32_t bbr_policer_call_from_rack_to = 0;
197 static int32_t bbr_policer_detection_enabled = 1;
198 static int32_t bbr_min_measurements_req = 1; /* We need at least 2
199 * measurments before we are
200 * "good" note that 2 == 1.
201 * This is because we use a >
202 * comparison. This means if
203 * min_measure was 0, it takes
204 * num-measures > min(0) and
205 * you get 1 measurement and
206 * you are good. Set to 1, you
207 * have to have two
208 * measurements (this is done
209 * to prevent it from being ok
210 * to have no measurements). */
211 static int32_t bbr_no_pacing_until = 4;
212
213 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
214 static int32_t bbr_min_peer_delta = 20; /* 20 units */
215 static int32_t bbr_delta_percent = 150; /* 15.0 % */
216
217 static int32_t bbr_target_cwnd_mult_limit = 8;
218 /*
219 * bbr_cwnd_min_val is the number of
220 * segments we hold to in the RTT probe
221 * state typically 4.
222 */
223 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
224
225 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
226
227 static int32_t bbr_gain_to_target = 1;
228 static int32_t bbr_gain_gets_extra_too = 1;
229 /*
230 * bbr_high_gain is the 2/ln(2) value we need
231 * to double the sending rate in startup. This
232 * is used for both cwnd and hptsi gain's.
233 */
234 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
235 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
236 static int32_t bbr_use_lower_gain_in_startup = 1;
237
238 /* thresholds for reduction on drain in sub-states/drain */
239 static int32_t bbr_drain_rtt = BBR_SRTT;
240 static int32_t bbr_drain_floor = 88;
241 static int32_t google_allow_early_out = 1;
242 static int32_t google_consider_lost = 1;
243 static int32_t bbr_drain_drop_mul = 4;
244 static int32_t bbr_drain_drop_div = 5;
245 static int32_t bbr_rand_ot = 50;
246 static int32_t bbr_can_force_probertt = 0;
247 static int32_t bbr_can_adjust_probertt = 1;
248 static int32_t bbr_probertt_sets_rtt = 0;
249 static int32_t bbr_can_use_ts_for_rtt = 1;
250 static int32_t bbr_is_ratio = 0;
251 static int32_t bbr_sub_drain_app_limit = 1;
252 static int32_t bbr_prtt_slam_cwnd = 1;
253 static int32_t bbr_sub_drain_slam_cwnd = 1;
254 static int32_t bbr_slam_cwnd_in_main_drain = 1;
255 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
256 * hold */
257 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
258 /*
259 * bbr_drain_gain is the reverse of the high_gain
260 * designed to drain back out the standing queue
261 * that is formed in startup by causing a larger
262 * hptsi gain and thus drainging the packets
263 * in flight.
264 */
265 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
266 static int32_t bbr_rttprobe_gain = 192;
267
268 /*
269 * The cwnd_gain is the default cwnd gain applied when
270 * calculating a target cwnd. Note that the cwnd is
271 * a secondary factor in the way BBR works (see the
272 * paper and think about it, it will take some time).
273 * Basically the hptsi_gain spreads the packets out
274 * so you never get more than BDP to the peer even
275 * if the cwnd is high. In our implemenation that
276 * means in non-recovery/retransmission scenarios
277 * cwnd will never be reached by the flight-size.
278 */
279 static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
280 static int32_t bbr_tlp_type_to_use = BBR_SRTT;
281 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
282 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
283 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
284 static int32_t bbr_ignore_data_after_close = 1;
285 static int16_t bbr_hptsi_gain[] = {
286 (BBR_UNIT *5 / 4),
287 (BBR_UNIT * 3 / 4),
288 BBR_UNIT,
289 BBR_UNIT,
290 BBR_UNIT,
291 BBR_UNIT,
292 BBR_UNIT,
293 BBR_UNIT
294 };
295 int32_t bbr_use_rack_resend_cheat = 1;
296 int32_t bbr_sends_full_iwnd = 1;
297
298 #define BBR_HPTSI_GAIN_MAX 8
299 /*
300 * The BBR module incorporates a number of
301 * TCP ideas that have been put out into the IETF
302 * over the last few years:
303 * - Yuchung Cheng's RACK TCP (for which its named) that
304 * will stop us using the number of dup acks and instead
305 * use time as the gage of when we retransmit.
306 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
307 * of Dukkipati et.al.
308 * - Van Jacobson's et.al BBR.
309 *
310 * RACK depends on SACK, so if an endpoint arrives that
311 * cannot do SACK the state machine below will shuttle the
312 * connection back to using the "default" TCP stack that is
313 * in FreeBSD.
314 *
315 * To implement BBR and RACK the original TCP stack was first decomposed
316 * into a functional state machine with individual states
317 * for each of the possible TCP connection states. The do_segement
318 * functions role in life is to mandate the connection supports SACK
319 * initially and then assure that the RACK state matches the conenction
320 * state before calling the states do_segment function. Data processing
321 * of inbound segments also now happens in the hpts_do_segment in general
322 * with only one exception. This is so we can keep the connection on
323 * a single CPU.
324 *
325 * Each state is simplified due to the fact that the original do_segment
326 * has been decomposed and we *know* what state we are in (no
327 * switches on the state) and all tests for SACK are gone. This
328 * greatly simplifies what each state does.
329 *
330 * TCP output is also over-written with a new version since it
331 * must maintain the new rack scoreboard and has had hptsi
332 * integrated as a requirment. Still todo is to eliminate the
333 * use of the callout_() system and use the hpts for all
334 * timers as well.
335 */
336 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
337 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
338 static const int32_t bbr_min_req_free = 2; /* The min we must have on the
339 * free list */
340 static int32_t bbr_tlp_thresh = 1;
341 static int32_t bbr_reorder_thresh = 2;
342 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
343 * 60,000,000 - 60 seconds */
344 static int32_t bbr_pkt_delay = 1000;
345 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
346 static int32_t bbr_incr_timers = 1;
347
348 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
349 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
350 static int32_t bbr_exit_startup_at_loss = 1;
351
352 /*
353 * bbr_lt_bw_ratio is 1/8th
354 * bbr_lt_bw_diff is < 4 Kbit/sec
355 */
356 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
357 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
358 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
359 * the lt_bw for */
360 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
361 * lt_bw */
362 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
363 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
364 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
365
366 static int32_t bbr_verbose_logging = 0;
367 /*
368 * Currently regular tcp has a rto_min of 30ms
369 * the backoff goes 12 times so that ends up
370 * being a total of 122.850 seconds before a
371 * connection is killed.
372 */
373 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
374 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
375
376 /****************************************************/
377 /* DEFAULT TSO SIZING (cpu performance impacting) */
378 /****************************************************/
379 /* What amount is our formula using to get TSO size */
380 static int32_t bbr_hptsi_per_second = 1000;
381
382 /*
383 * For hptsi under bbr_cross_over connections what is delay
384 * target 7ms (in usec) combined with a seg_max of 2
385 * gets us close to identical google behavior in
386 * TSO size selection (possibly more 1MSS sends).
387 */
388 static int32_t bbr_hptsi_segments_delay_tar = 7000;
389
390 /* Does pacing delay include overhead's in its time calculations? */
391 static int32_t bbr_include_enet_oh = 0;
392 static int32_t bbr_include_ip_oh = 1;
393 static int32_t bbr_include_tcp_oh = 1;
394 static int32_t bbr_google_discount = 10;
395
396 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
397 static int32_t bbr_state_is_pkt_epoch = 0;
398 static int32_t bbr_state_drain_2_tar = 1;
399 /* What is the max the 0 - bbr_cross_over MBPS TSO target
400 * can reach using our delay target. Note that this
401 * value becomes the floor for the cross over
402 * algorithm.
403 */
404 static int32_t bbr_hptsi_segments_max = 2;
405 static int32_t bbr_hptsi_segments_floor = 1;
406 static int32_t bbr_hptsi_utter_max = 0;
407
408 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
409 static int32_t bbr_hptsi_bytes_min = 1460;
410 static int32_t bbr_all_get_min = 0;
411
412 /* Cross over point from algo-a to algo-b */
413 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
414
415 /* Do we deal with our restart state? */
416 static int32_t bbr_uses_idle_restart = 0;
417 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
418
419 /* Do we allow hardware pacing? */
420 static int32_t bbr_allow_hdwr_pacing = 0;
421 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
422 static int32_t bbr_hdwr_pace_floor = 1;
423 static int32_t bbr_hdwr_pacing_delay_cnt = 10;
424
425 /****************************************************/
426 static int32_t bbr_resends_use_tso = 0;
427 static int32_t bbr_tlp_max_resend = 2;
428 static int32_t bbr_sack_block_limit = 128;
429
430 #define BBR_MAX_STAT 19
431 counter_u64_t bbr_state_time[BBR_MAX_STAT];
432 counter_u64_t bbr_state_lost[BBR_MAX_STAT];
433 counter_u64_t bbr_state_resend[BBR_MAX_STAT];
434 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
435 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
436 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
437 counter_u64_t bbr_flows_whdwr_pacing;
438 counter_u64_t bbr_flows_nohdwr_pacing;
439
440 counter_u64_t bbr_nohdwr_pacing_enobuf;
441 counter_u64_t bbr_hdwr_pacing_enobuf;
442
443 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
444
445 /*
446 * Static defintions we need for forward declarations.
447 */
448 static uint32_t
449 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
450 uint32_t useconds_time, uint64_t bw);
451 static uint32_t
452 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
453 static void
454 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
455 static void
456 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
457 static void
458 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
459 int dolog);
460 static uint32_t
461 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
462 static void
463 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
464 int32_t pkt_epoch, uint32_t losses);
465 static uint32_t
466 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm);
467 static uint32_t bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
468 static uint32_t
469 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
470 struct bbr_sendmap *rsm, uint32_t srtt,
471 uint32_t cts);
472 static void
473 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
474 int32_t line);
475 static void
476 bbr_set_state_target(struct tcp_bbr *bbr, int line);
477 static void
478 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
479
480 static void
481 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line);
482
483 static void
484 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
485
486 static void
487 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
488
489 static void
490 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, uint32_t rtt,
491 uint32_t line, uint8_t is_start, uint16_t set);
492
493 static struct bbr_sendmap *
494 bbr_find_lowest_rsm(struct tcp_bbr *bbr);
495 static __inline uint32_t
496 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
497 static void
498 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which);
499
500 static void
501 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
502 uint32_t thresh, uint32_t to);
503 static void
504 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
505
506 static void
507 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
508 uint32_t del_by, uint32_t cts, uint32_t sloton, uint32_t prev_delay);
509
510 static void
511 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr,
512 uint32_t cts, int32_t line);
513 static void
514 bbr_stop_all_timers(struct tcpcb *tp);
515 static void
516 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
517 static void
518 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
519 static void
520 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
521
522 static void
523 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
524 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod);
525
526 static inline uint8_t
bbr_state_val(struct tcp_bbr * bbr)527 bbr_state_val(struct tcp_bbr *bbr)
528 {
529 return(bbr->rc_bbr_substate);
530 }
531
532 static inline uint32_t
get_min_cwnd(struct tcp_bbr * bbr)533 get_min_cwnd(struct tcp_bbr *bbr)
534 {
535 int mss;
536
537 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
538 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED)
539 return (bbr_cwnd_min_val_hs * mss);
540 else
541 return (bbr_cwnd_min_val * mss);
542 }
543
544 static uint32_t
bbr_get_persists_timer_val(struct tcpcb * tp,struct tcp_bbr * bbr)545 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr)
546 {
547 uint64_t srtt, var;
548 uint64_t ret_val;
549
550 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
551 if (tp->t_srtt == 0) {
552 srtt = (uint64_t)BBR_INITIAL_RTO;
553 var = 0;
554 } else {
555 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
556 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT);
557 }
558 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]),
559 bbr_persist_min, bbr_persist_max);
560 return ((uint32_t)ret_val);
561 }
562
563 static uint32_t
bbr_timer_start(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)564 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
565 {
566 /*
567 * Start the FR timer, we do this based on getting the first one in
568 * the rc_tmap. Note that if its NULL we must stop the timer. in all
569 * events we need to stop the running timer (if its running) before
570 * starting the new one.
571 */
572 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
573 int32_t idx;
574 int32_t is_tlp_timer = 0;
575 struct bbr_sendmap *rsm;
576
577 if (bbr->rc_all_timers_stopped) {
578 /* All timers have been stopped none are to run */
579 return (0);
580 }
581 if (bbr->rc_in_persist) {
582 /* We can't start any timer in persists */
583 return (bbr_get_persists_timer_val(tp, bbr));
584 }
585 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
586 if ((rsm == NULL) ||
587 ((tp->t_flags & TF_SACK_PERMIT) == 0) ||
588 (tp->t_state < TCPS_ESTABLISHED)) {
589 /* Nothing on the send map */
590 activate_rxt:
591 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
592 uint64_t tov;
593
594 time_since_sent = 0;
595 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
596 if (rsm) {
597 idx = rsm->r_rtr_cnt - 1;
598 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
599 tstmp_touse = rsm->r_tim_lastsent[idx];
600 else
601 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
602 if (TSTMP_GT(tstmp_touse, cts))
603 time_since_sent = cts - tstmp_touse;
604 }
605 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
606 if (tp->t_srtt == 0)
607 tov = BBR_INITIAL_RTO;
608 else
609 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) +
610 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT);
611 if (tp->t_rxtshift)
612 tov *= tcp_backoff[tp->t_rxtshift];
613 if (tov > time_since_sent)
614 tov -= time_since_sent;
615 else
616 tov = bbr->r_ctl.rc_min_to;
617 TCPT_RANGESET_NOSLOP(to, tov,
618 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC),
619 (bbr->rc_max_rto_sec * USECS_IN_SECOND));
620 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to);
621 return (to);
622 }
623 return (0);
624 }
625 if (rsm->r_flags & BBR_ACKED) {
626 rsm = bbr_find_lowest_rsm(bbr);
627 if (rsm == NULL) {
628 /* No lowest? */
629 goto activate_rxt;
630 }
631 }
632 /* Convert from ms to usecs */
633 if (rsm->r_flags & BBR_SACK_PASSED) {
634 if ((tp->t_flags & TF_SENTFIN) &&
635 ((tp->snd_max - tp->snd_una) == 1) &&
636 (rsm->r_flags & BBR_HAS_FIN)) {
637 /*
638 * We don't start a bbr rack timer if all we have is
639 * a FIN outstanding.
640 */
641 goto activate_rxt;
642 }
643 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK);
644 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm);
645 idx = rsm->r_rtr_cnt - 1;
646 exp = rsm->r_tim_lastsent[idx] + thresh;
647 if (SEQ_GEQ(exp, cts)) {
648 to = exp - cts;
649 if (to < bbr->r_ctl.rc_min_to) {
650 to = bbr->r_ctl.rc_min_to;
651 }
652 } else {
653 to = bbr->r_ctl.rc_min_to;
654 }
655 } else {
656 /* Ok we need to do a TLP not RACK */
657 if (bbr->rc_tlp_in_progress != 0) {
658 /*
659 * The previous send was a TLP.
660 */
661 goto activate_rxt;
662 }
663 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
664 if (rsm == NULL) {
665 /* We found no rsm to TLP with. */
666 goto activate_rxt;
667 }
668 if (rsm->r_flags & BBR_HAS_FIN) {
669 /* If its a FIN we don't do TLP */
670 rsm = NULL;
671 goto activate_rxt;
672 }
673 time_since_sent = 0;
674 idx = rsm->r_rtr_cnt - 1;
675 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
676 tstmp_touse = rsm->r_tim_lastsent[idx];
677 else
678 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
679 if (TSTMP_GT(tstmp_touse, cts))
680 time_since_sent = cts - tstmp_touse;
681 is_tlp_timer = 1;
682 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use);
683 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts);
684 if (thresh > time_since_sent)
685 to = thresh - time_since_sent;
686 else
687 to = bbr->r_ctl.rc_min_to;
688 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
689 /*
690 * If the TLP time works out to larger than the max
691 * RTO lets not do TLP.. just RTO.
692 */
693 goto activate_rxt;
694 }
695 if ((bbr->rc_tlp_rtx_out == 1) &&
696 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) {
697 /*
698 * Second retransmit of the same TLP
699 * lets not.
700 */
701 bbr->rc_tlp_rtx_out = 0;
702 goto activate_rxt;
703 }
704 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) {
705 /*
706 * The tail is no longer the last one I did a probe
707 * on
708 */
709 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
710 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
711 }
712 }
713 if (is_tlp_timer == 0) {
714 BBR_STAT_INC(bbr_to_arm_rack);
715 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
716 } else {
717 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to);
718 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
719 /*
720 * We have exceeded how many times we can retran the
721 * current TLP timer, switch to the RTO timer.
722 */
723 goto activate_rxt;
724 } else {
725 BBR_STAT_INC(bbr_to_arm_tlp);
726 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
727 }
728 }
729 return (to);
730 }
731
732 static inline int32_t
bbr_minseg(struct tcp_bbr * bbr)733 bbr_minseg(struct tcp_bbr *bbr)
734 {
735 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options);
736 }
737
738 static void
bbr_start_hpts_timer(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t cts,int32_t frm,int32_t slot,uint32_t tot_len)739 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len)
740 {
741 struct inpcb *inp;
742 struct hpts_diag diag;
743 uint32_t delayed_ack = 0;
744 uint32_t left = 0;
745 uint32_t hpts_timeout;
746 uint8_t stopped;
747 int32_t delay_calc = 0;
748 uint32_t prev_delay = 0;
749
750 inp = tp->t_inpcb;
751 if (inp->inp_in_hpts) {
752 /* A previous call is already set up */
753 return;
754 }
755 if ((tp->t_state == TCPS_CLOSED) ||
756 (tp->t_state == TCPS_LISTEN)) {
757 return;
758 }
759 stopped = bbr->rc_tmr_stopped;
760 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
761 left = bbr->r_ctl.rc_timer_exp - cts;
762 }
763 bbr->r_ctl.rc_hpts_flags = 0;
764 bbr->r_ctl.rc_timer_exp = 0;
765 prev_delay = bbr->r_ctl.rc_last_delay_val;
766 if (bbr->r_ctl.rc_last_delay_val &&
767 (slot == 0)) {
768 /*
769 * If a previous pacer delay was in place we
770 * are not coming from the output side (where
771 * we calculate a delay, more likely a timer).
772 */
773 slot = bbr->r_ctl.rc_last_delay_val;
774 if (TSTMP_GT(cts, bbr->rc_pacer_started)) {
775 /* Compensate for time passed */
776 delay_calc = cts - bbr->rc_pacer_started;
777 if (delay_calc <= slot)
778 slot -= delay_calc;
779 }
780 }
781 /* Do we have early to make up for by pushing out the pacing time? */
782 if (bbr->r_agg_early_set) {
783 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2);
784 slot += bbr->r_ctl.rc_agg_early;
785 bbr->r_ctl.rc_agg_early = 0;
786 bbr->r_agg_early_set = 0;
787 }
788 /* Are we running a total debt that needs to be compensated for? */
789 if (bbr->r_ctl.rc_hptsi_agg_delay) {
790 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) {
791 /* We nuke the delay */
792 slot -= bbr->r_ctl.rc_hptsi_agg_delay;
793 bbr->r_ctl.rc_hptsi_agg_delay = 0;
794 } else {
795 /* We nuke some of the delay, put in a minimal 100usecs */
796 bbr->r_ctl.rc_hptsi_agg_delay -= slot;
797 bbr->r_ctl.rc_last_delay_val = slot = 100;
798 }
799 }
800 bbr->r_ctl.rc_last_delay_val = slot;
801 hpts_timeout = bbr_timer_start(tp, bbr, cts);
802 if (tp->t_flags & TF_DELACK) {
803 if (bbr->rc_in_persist == 0) {
804 delayed_ack = bbr_delack_time;
805 } else {
806 /*
807 * We are in persists and have
808 * gotten a new data element.
809 */
810 if (hpts_timeout > bbr_delack_time) {
811 /*
812 * Lets make the persists timer (which acks)
813 * be the smaller of hpts_timeout and bbr_delack_time.
814 */
815 hpts_timeout = bbr_delack_time;
816 }
817 }
818 }
819 if (delayed_ack &&
820 ((hpts_timeout == 0) ||
821 (delayed_ack < hpts_timeout))) {
822 /* We need a Delayed ack timer */
823 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
824 hpts_timeout = delayed_ack;
825 }
826 if (slot) {
827 /* Mark that we have a pacing timer up */
828 BBR_STAT_INC(bbr_paced_segments);
829 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
830 }
831 /*
832 * If no timers are going to run and we will fall off thfe hptsi
833 * wheel, we resort to a keep-alive timer if its configured.
834 */
835 if ((hpts_timeout == 0) &&
836 (slot == 0)) {
837 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
838 (tp->t_state <= TCPS_CLOSING)) {
839 /*
840 * Ok we have no timer (persists, rack, tlp, rxt or
841 * del-ack), we don't have segments being paced. So
842 * all that is left is the keepalive timer.
843 */
844 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
845 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
846 } else {
847 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
848 }
849 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
850 }
851 }
852 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
853 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
854 /*
855 * RACK, TLP, persists and RXT timers all are restartable
856 * based on actions input .. i.e we received a packet (ack
857 * or sack) and that changes things (rw, or snd_una etc).
858 * Thus we can restart them with a new value. For
859 * keep-alive, delayed_ack we keep track of what was left
860 * and restart the timer with a smaller value.
861 */
862 if (left < hpts_timeout)
863 hpts_timeout = left;
864 }
865 if (bbr->r_ctl.rc_incr_tmrs && slot &&
866 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
867 /*
868 * If configured to do so, and the timer is either
869 * the TLP or RXT timer, we need to increase the timeout
870 * by the pacing time. Consider the bottleneck at my
871 * machine as an example, we are sending something
872 * to start a TLP on. The last packet won't be emitted
873 * fully until the pacing time (the bottleneck will hold
874 * the data in place). Once the packet is emitted that
875 * is when we want to start waiting for the TLP. This
876 * is most evident with hardware pacing (where the nic
877 * is holding the packet(s) before emitting). But it
878 * can also show up in the network so we do it for all
879 * cases. Technically we would take off one packet from
880 * this extra delay but this is easier and being more
881 * conservative is probably better.
882 */
883 hpts_timeout += slot;
884 }
885 if (hpts_timeout) {
886 /*
887 * Hack alert for now we can't time-out over 2147 seconds (a
888 * bit more than 35min)
889 */
890 if (hpts_timeout > 0x7ffffffe)
891 hpts_timeout = 0x7ffffffe;
892 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
893 } else
894 bbr->r_ctl.rc_timer_exp = 0;
895 if ((slot) &&
896 (bbr->rc_use_google ||
897 bbr->output_error_seen ||
898 (slot <= hpts_timeout)) ) {
899 /*
900 * Tell LRO that it can queue packets while
901 * we pace.
902 */
903 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
904 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
905 (bbr->rc_cwnd_limited == 0)) {
906 /*
907 * If we are not cwnd limited and we
908 * are running a rack timer we put on
909 * the do not disturbe even for sack.
910 */
911 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
912 } else
913 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
914 bbr->rc_pacer_started = cts;
915
916 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
917 __LINE__, &diag);
918 bbr->rc_timer_first = 0;
919 bbr->bbr_timer_src = frm;
920 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1);
921 bbr_log_hpts_diag(bbr, cts, &diag);
922 } else if (hpts_timeout) {
923 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
924 __LINE__, &diag);
925 /*
926 * We add the flag here as well if the slot is set,
927 * since hpts will call in to clear the queue first before
928 * calling the output routine (which does our timers).
929 * We don't want to set the flag if its just a timer
930 * else the arrival of data might (that causes us
931 * to send more) might get delayed. Imagine being
932 * on a keep-alive timer and a request comes in for
933 * more data.
934 */
935 if (slot)
936 bbr->rc_pacer_started = cts;
937 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
938 (bbr->rc_cwnd_limited == 0)) {
939 /*
940 * For a rack timer, don't wake us even
941 * if a sack arrives as long as we are
942 * not cwnd limited.
943 */
944 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
945 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
946 } else {
947 /* All other timers wake us up */
948 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
949 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
950 }
951 bbr->bbr_timer_src = frm;
952 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0);
953 bbr_log_hpts_diag(bbr, cts, &diag);
954 bbr->rc_timer_first = 1;
955 }
956 bbr->rc_tmr_stopped = 0;
957 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay);
958 }
959
960 static void
bbr_timer_audit(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,struct sockbuf * sb)961 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb)
962 {
963 /*
964 * We received an ack, and then did not call send or were bounced
965 * out due to the hpts was running. Now a timer is up as well, is it
966 * the right timer?
967 */
968 struct inpcb *inp;
969 struct bbr_sendmap *rsm;
970 uint32_t hpts_timeout;
971 int tmr_up;
972
973 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
974 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
975 return;
976 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
977 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
978 (tmr_up == PACE_TMR_RXT)) {
979 /* Should be an RXT */
980 return;
981 }
982 inp = bbr->rc_inp;
983 if (rsm == NULL) {
984 /* Nothing outstanding? */
985 if (tp->t_flags & TF_DELACK) {
986 if (tmr_up == PACE_TMR_DELACK)
987 /*
988 * We are supposed to have delayed ack up
989 * and we do
990 */
991 return;
992 } else if (sbavail(&inp->inp_socket->so_snd) &&
993 (tmr_up == PACE_TMR_RXT)) {
994 /*
995 * if we hit enobufs then we would expect the
996 * possiblity of nothing outstanding and the RXT up
997 * (and the hptsi timer).
998 */
999 return;
1000 } else if (((V_tcp_always_keepalive ||
1001 inp->inp_socket->so_options & SO_KEEPALIVE) &&
1002 (tp->t_state <= TCPS_CLOSING)) &&
1003 (tmr_up == PACE_TMR_KEEP) &&
1004 (tp->snd_max == tp->snd_una)) {
1005 /* We should have keep alive up and we do */
1006 return;
1007 }
1008 }
1009 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) {
1010 if ((tp->t_flags & TF_SENTFIN) &&
1011 ((tp->snd_max - tp->snd_una) == 1) &&
1012 (rsm->r_flags & BBR_HAS_FIN)) {
1013 /* needs to be a RXT */
1014 if (tmr_up == PACE_TMR_RXT)
1015 return;
1016 else
1017 goto wrong_timer;
1018 } else if (tmr_up == PACE_TMR_RACK)
1019 return;
1020 else
1021 goto wrong_timer;
1022 } else if (rsm && (tmr_up == PACE_TMR_RACK)) {
1023 /* Rack timer has priority if we have data out */
1024 return;
1025 } else if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1026 ((tmr_up == PACE_TMR_TLP) ||
1027 (tmr_up == PACE_TMR_RXT))) {
1028 /*
1029 * Either a TLP or RXT is fine if no sack-passed is in place
1030 * and data is outstanding.
1031 */
1032 return;
1033 } else if (tmr_up == PACE_TMR_DELACK) {
1034 /*
1035 * If the delayed ack was going to go off before the
1036 * rtx/tlp/rack timer were going to expire, then that would
1037 * be the timer in control. Note we don't check the time
1038 * here trusting the code is correct.
1039 */
1040 return;
1041 }
1042 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1043 ((tmr_up == PACE_TMR_RXT) ||
1044 (tmr_up == PACE_TMR_TLP) ||
1045 (tmr_up == PACE_TMR_RACK))) {
1046 /*
1047 * We have outstanding data and
1048 * we *do* have a RACK, TLP or RXT
1049 * timer running. We won't restart
1050 * anything here since thats probably ok we
1051 * will get called with some timer here shortly.
1052 */
1053 return;
1054 }
1055 /*
1056 * Ok the timer originally started is not what we want now. We will
1057 * force the hpts to be stopped if any, and restart with the slot
1058 * set to what was in the saved slot.
1059 */
1060 wrong_timer:
1061 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) {
1062 if (inp->inp_in_hpts)
1063 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
1064 bbr_timer_cancel(bbr, __LINE__, cts);
1065 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val,
1066 0);
1067 } else {
1068 /*
1069 * Output is hptsi so we just need to switch the type of
1070 * timer. We don't bother with keep-alive, since when we
1071 * jump through the output, it will start the keep-alive if
1072 * nothing is sent.
1073 *
1074 * We only need a delayed-ack added and or the hpts_timeout.
1075 */
1076 hpts_timeout = bbr_timer_start(tp, bbr, cts);
1077 if (tp->t_flags & TF_DELACK) {
1078 if (hpts_timeout == 0) {
1079 hpts_timeout = bbr_delack_time;
1080 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1081 }
1082 else if (hpts_timeout > bbr_delack_time) {
1083 hpts_timeout = bbr_delack_time;
1084 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1085 }
1086 }
1087 if (hpts_timeout) {
1088 if (hpts_timeout > 0x7ffffffe)
1089 hpts_timeout = 0x7ffffffe;
1090 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
1091 }
1092 }
1093 }
1094
1095 int32_t bbr_clear_lost = 0;
1096
1097 /*
1098 * Considers the two time values now (cts) and earlier.
1099 * If cts is smaller than earlier, we could have
1100 * had a sequence wrap (our counter wraps every
1101 * 70 min or so) or it could be just clock skew
1102 * getting us two differnt time values. Clock skew
1103 * will show up within 10ms or so. So in such
1104 * a case (where cts is behind earlier time by
1105 * less than 10ms) we return 0. Otherwise we
1106 * return the true difference between them.
1107 */
1108 static inline uint32_t
bbr_calc_time(uint32_t cts,uint32_t earlier_time)1109 bbr_calc_time(uint32_t cts, uint32_t earlier_time) {
1110 /*
1111 * Given two timestamps, the current time stamp cts, and some other
1112 * time-stamp taken in theory earlier return the difference. The
1113 * trick is here sometimes locking will get the other timestamp
1114 * after the cts. If this occurs we need to return 0.
1115 */
1116 if (TSTMP_GEQ(cts, earlier_time))
1117 return (cts - earlier_time);
1118 /*
1119 * cts is behind earlier_time if its less than 10ms consider it 0.
1120 * If its more than 10ms difference then we had a time wrap. Else
1121 * its just the normal locking foo. I wonder if we should not go to
1122 * 64bit TS and get rid of this issue.
1123 */
1124 if (TSTMP_GEQ((cts + 10000), earlier_time))
1125 return (0);
1126 /*
1127 * Ok the time must have wrapped. So we need to answer a large
1128 * amount of time, which the normal subtraction should do.
1129 */
1130 return (cts - earlier_time);
1131 }
1132
1133 static int
sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)1134 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)
1135 {
1136 uint32_t stat;
1137 int32_t error;
1138
1139 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t));
1140 if (error || req->newptr == NULL)
1141 return error;
1142
1143 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
1144 if (error)
1145 return (error);
1146 if (stat == 1) {
1147 #ifdef BBR_INVARIANTS
1148 printf("Clearing BBR lost counters\n");
1149 #endif
1150 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT);
1151 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT);
1152 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT);
1153 } else if (stat == 2) {
1154 #ifdef BBR_INVARIANTS
1155 printf("Clearing BBR option counters\n");
1156 #endif
1157 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE);
1158 } else if (stat == 3) {
1159 #ifdef BBR_INVARIANTS
1160 printf("Clearing BBR stats counters\n");
1161 #endif
1162 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE);
1163 } else if (stat == 4) {
1164 #ifdef BBR_INVARIANTS
1165 printf("Clearing BBR out-size counters\n");
1166 #endif
1167 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE);
1168 }
1169 bbr_clear_lost = 0;
1170 return (0);
1171 }
1172
1173 static void
bbr_init_sysctls(void)1174 bbr_init_sysctls(void)
1175 {
1176 struct sysctl_oid *bbr_probertt;
1177 struct sysctl_oid *bbr_hptsi;
1178 struct sysctl_oid *bbr_measure;
1179 struct sysctl_oid *bbr_cwnd;
1180 struct sysctl_oid *bbr_timeout;
1181 struct sysctl_oid *bbr_states;
1182 struct sysctl_oid *bbr_startup;
1183 struct sysctl_oid *bbr_policer;
1184
1185 /* Probe rtt controls */
1186 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1187 SYSCTL_CHILDREN(bbr_sysctl_root),
1188 OID_AUTO,
1189 "probertt",
1190 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1191 "");
1192 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1193 SYSCTL_CHILDREN(bbr_probertt),
1194 OID_AUTO, "gain", CTLFLAG_RW,
1195 &bbr_rttprobe_gain, 192,
1196 "What is the filter gain drop in probe_rtt (0=disable)?");
1197 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1198 SYSCTL_CHILDREN(bbr_probertt),
1199 OID_AUTO, "cwnd", CTLFLAG_RW,
1200 &bbr_rtt_probe_cwndtarg, 4,
1201 "How many mss's are outstanding during probe-rtt");
1202 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1203 SYSCTL_CHILDREN(bbr_probertt),
1204 OID_AUTO, "int", CTLFLAG_RW,
1205 &bbr_rtt_probe_limit, 4000000,
1206 "If RTT has not shrank in this many micro-seconds enter probe-rtt");
1207 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1208 SYSCTL_CHILDREN(bbr_probertt),
1209 OID_AUTO, "mintime", CTLFLAG_RW,
1210 &bbr_rtt_probe_time, 200000,
1211 "How many microseconds in probe-rtt");
1212 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1213 SYSCTL_CHILDREN(bbr_probertt),
1214 OID_AUTO, "filter_len_sec", CTLFLAG_RW,
1215 &bbr_filter_len_sec, 6,
1216 "How long in seconds does the rttProp filter run?");
1217 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1218 SYSCTL_CHILDREN(bbr_probertt),
1219 OID_AUTO, "drain_rtt", CTLFLAG_RW,
1220 &bbr_drain_rtt, BBR_SRTT,
1221 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?");
1222 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1223 SYSCTL_CHILDREN(bbr_probertt),
1224 OID_AUTO, "can_force", CTLFLAG_RW,
1225 &bbr_can_force_probertt, 0,
1226 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??");
1227 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1228 SYSCTL_CHILDREN(bbr_probertt),
1229 OID_AUTO, "enter_sets_force", CTLFLAG_RW,
1230 &bbr_probertt_sets_rtt, 0,
1231 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?");
1232 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1233 SYSCTL_CHILDREN(bbr_probertt),
1234 OID_AUTO, "can_adjust", CTLFLAG_RW,
1235 &bbr_can_adjust_probertt, 1,
1236 "Can we dynamically adjust the probe-rtt limits and times?");
1237 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1238 SYSCTL_CHILDREN(bbr_probertt),
1239 OID_AUTO, "is_ratio", CTLFLAG_RW,
1240 &bbr_is_ratio, 0,
1241 "is the limit to filter a ratio?");
1242 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1243 SYSCTL_CHILDREN(bbr_probertt),
1244 OID_AUTO, "use_cwnd", CTLFLAG_RW,
1245 &bbr_prtt_slam_cwnd, 0,
1246 "Should we set/recover cwnd?");
1247 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1248 SYSCTL_CHILDREN(bbr_probertt),
1249 OID_AUTO, "can_use_ts", CTLFLAG_RW,
1250 &bbr_can_use_ts_for_rtt, 1,
1251 "Can we use the ms timestamp if available for retransmistted rtt calculations?");
1252
1253 /* Pacing controls */
1254 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1255 SYSCTL_CHILDREN(bbr_sysctl_root),
1256 OID_AUTO,
1257 "pacing",
1258 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1259 "");
1260 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1261 SYSCTL_CHILDREN(bbr_hptsi),
1262 OID_AUTO, "hw_pacing", CTLFLAG_RW,
1263 &bbr_allow_hdwr_pacing, 1,
1264 "Do we allow hardware pacing?");
1265 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1266 SYSCTL_CHILDREN(bbr_hptsi),
1267 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW,
1268 &bbr_hardware_pacing_limit, 4000,
1269 "Do we have a limited number of connections for pacing chelsio (0=no limit)?");
1270 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1271 SYSCTL_CHILDREN(bbr_hptsi),
1272 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW,
1273 &bbr_hdwr_pace_adjust, 2,
1274 "Multiplier to calculated tso size?");
1275 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1276 SYSCTL_CHILDREN(bbr_hptsi),
1277 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW,
1278 &bbr_hdwr_pace_floor, 1,
1279 "Do we invoke the hardware pacing floor?");
1280 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1281 SYSCTL_CHILDREN(bbr_hptsi),
1282 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW,
1283 &bbr_hdwr_pacing_delay_cnt, 10,
1284 "How many packets must be sent after hdwr pacing is enabled");
1285 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1286 SYSCTL_CHILDREN(bbr_hptsi),
1287 OID_AUTO, "bw_cross", CTLFLAG_RW,
1288 &bbr_cross_over, 3000000,
1289 "What is the point where we cross over to linux like TSO size set");
1290 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1291 SYSCTL_CHILDREN(bbr_hptsi),
1292 OID_AUTO, "seg_deltarg", CTLFLAG_RW,
1293 &bbr_hptsi_segments_delay_tar, 7000,
1294 "What is the worse case delay target for hptsi < 48Mbp connections");
1295 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1296 SYSCTL_CHILDREN(bbr_hptsi),
1297 OID_AUTO, "enet_oh", CTLFLAG_RW,
1298 &bbr_include_enet_oh, 0,
1299 "Do we include the ethernet overhead in calculating pacing delay?");
1300 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1301 SYSCTL_CHILDREN(bbr_hptsi),
1302 OID_AUTO, "ip_oh", CTLFLAG_RW,
1303 &bbr_include_ip_oh, 1,
1304 "Do we include the IP overhead in calculating pacing delay?");
1305 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1306 SYSCTL_CHILDREN(bbr_hptsi),
1307 OID_AUTO, "tcp_oh", CTLFLAG_RW,
1308 &bbr_include_tcp_oh, 0,
1309 "Do we include the TCP overhead in calculating pacing delay?");
1310 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1311 SYSCTL_CHILDREN(bbr_hptsi),
1312 OID_AUTO, "google_discount", CTLFLAG_RW,
1313 &bbr_google_discount, 10,
1314 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?");
1315 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1316 SYSCTL_CHILDREN(bbr_hptsi),
1317 OID_AUTO, "all_get_min", CTLFLAG_RW,
1318 &bbr_all_get_min, 0,
1319 "If you are less than a MSS do you just get the min?");
1320 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1321 SYSCTL_CHILDREN(bbr_hptsi),
1322 OID_AUTO, "tso_min", CTLFLAG_RW,
1323 &bbr_hptsi_bytes_min, 1460,
1324 "For 0 -> 24Mbps what is floor number of segments for TSO");
1325 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1326 SYSCTL_CHILDREN(bbr_hptsi),
1327 OID_AUTO, "seg_tso_max", CTLFLAG_RW,
1328 &bbr_hptsi_segments_max, 6,
1329 "For 0 -> 24Mbps what is top number of segments for TSO");
1330 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1331 SYSCTL_CHILDREN(bbr_hptsi),
1332 OID_AUTO, "seg_floor", CTLFLAG_RW,
1333 &bbr_hptsi_segments_floor, 1,
1334 "Minimum TSO size we will fall too in segments");
1335 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1336 SYSCTL_CHILDREN(bbr_hptsi),
1337 OID_AUTO, "utter_max", CTLFLAG_RW,
1338 &bbr_hptsi_utter_max, 0,
1339 "The absolute maximum that any pacing (outside of hardware) can be");
1340 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1341 SYSCTL_CHILDREN(bbr_hptsi),
1342 OID_AUTO, "seg_divisor", CTLFLAG_RW,
1343 &bbr_hptsi_per_second, 100,
1344 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps ");
1345 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1346 SYSCTL_CHILDREN(bbr_hptsi),
1347 OID_AUTO, "srtt_mul", CTLFLAG_RW,
1348 &bbr_hptsi_max_mul, 1,
1349 "The multiplier for pace len max");
1350 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1351 SYSCTL_CHILDREN(bbr_hptsi),
1352 OID_AUTO, "srtt_div", CTLFLAG_RW,
1353 &bbr_hptsi_max_div, 2,
1354 "The divisor for pace len max");
1355 /* Measurement controls */
1356 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1357 SYSCTL_CHILDREN(bbr_sysctl_root),
1358 OID_AUTO,
1359 "measure",
1360 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1361 "Measurement controls");
1362 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1363 SYSCTL_CHILDREN(bbr_measure),
1364 OID_AUTO, "min_i_bw", CTLFLAG_RW,
1365 &bbr_initial_bw_bps, 62500,
1366 "Minimum initial b/w in bytes per second");
1367 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1368 SYSCTL_CHILDREN(bbr_measure),
1369 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1370 &bbr_sack_not_required, 0,
1371 "Do we allow bbr to run on connections not supporting SACK?");
1372 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1373 SYSCTL_CHILDREN(bbr_measure),
1374 OID_AUTO, "use_google", CTLFLAG_RW,
1375 &bbr_use_google_algo, 0,
1376 "Use has close to google V1.0 has possible?");
1377 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1378 SYSCTL_CHILDREN(bbr_measure),
1379 OID_AUTO, "ts_limiting", CTLFLAG_RW,
1380 &bbr_ts_limiting, 1,
1381 "Do we attempt to use the peers timestamp to limit b/w caculations?");
1382 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1383 SYSCTL_CHILDREN(bbr_measure),
1384 OID_AUTO, "ts_can_raise", CTLFLAG_RW,
1385 &bbr_ts_can_raise, 0,
1386 "Can we raise the b/w via timestamp b/w calculation?");
1387 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1388 SYSCTL_CHILDREN(bbr_measure),
1389 OID_AUTO, "ts_delta", CTLFLAG_RW,
1390 &bbr_min_usec_delta, 20000,
1391 "How long in usec between ts of our sends in ts validation code?");
1392 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1393 SYSCTL_CHILDREN(bbr_measure),
1394 OID_AUTO, "ts_peer_delta", CTLFLAG_RW,
1395 &bbr_min_peer_delta, 20,
1396 "What min numerical value should be between the peer deltas?");
1397 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1398 SYSCTL_CHILDREN(bbr_measure),
1399 OID_AUTO, "ts_delta_percent", CTLFLAG_RW,
1400 &bbr_delta_percent, 150,
1401 "What percentage (150 = 15.0) do we allow variance for?");
1402 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1403 SYSCTL_CHILDREN(bbr_measure),
1404 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW,
1405 &bbr_min_measurements_req, 1,
1406 "What is the minimum measurment count we need before we switch to our b/w estimate");
1407 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1408 SYSCTL_CHILDREN(bbr_measure),
1409 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW,
1410 &bbr_no_pacing_until, 4,
1411 "How many pkt-epoch's (0 is off) do we need before pacing is on?");
1412 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1413 SYSCTL_CHILDREN(bbr_measure),
1414 OID_AUTO, "quanta", CTLFLAG_RW,
1415 &bbr_quanta, 2,
1416 "Extra quanta to add when calculating the target (ID section 4.2.3.2).");
1417 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1418 SYSCTL_CHILDREN(bbr_measure),
1419 OID_AUTO, "noretran", CTLFLAG_RW,
1420 &bbr_no_retran, 0,
1421 "Should google mode not use retransmission measurements for the b/w estimation?");
1422 /* State controls */
1423 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1424 SYSCTL_CHILDREN(bbr_sysctl_root),
1425 OID_AUTO,
1426 "states",
1427 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1428 "State controls");
1429 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1430 SYSCTL_CHILDREN(bbr_states),
1431 OID_AUTO, "idle_restart", CTLFLAG_RW,
1432 &bbr_uses_idle_restart, 0,
1433 "Do we use a new special idle_restart state to ramp back up quickly?");
1434 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1435 SYSCTL_CHILDREN(bbr_states),
1436 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW,
1437 &bbr_idle_restart_threshold, 100000,
1438 "How long must we be idle before we restart??");
1439 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1440 SYSCTL_CHILDREN(bbr_states),
1441 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW,
1442 &bbr_state_is_pkt_epoch, 0,
1443 "Do we use a pkt-epoch for substate if 0 rttProp?");
1444 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1445 SYSCTL_CHILDREN(bbr_states),
1446 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW,
1447 &bbr_rtt_gain_thresh, 0,
1448 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?");
1449 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1450 SYSCTL_CHILDREN(bbr_states),
1451 OID_AUTO, "drain_floor", CTLFLAG_RW,
1452 &bbr_drain_floor, 88,
1453 "What is the lowest we can drain (pg) too?");
1454 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1455 SYSCTL_CHILDREN(bbr_states),
1456 OID_AUTO, "drain_2_target", CTLFLAG_RW,
1457 &bbr_state_drain_2_tar, 1,
1458 "Do we drain to target in drain substate?");
1459 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1460 SYSCTL_CHILDREN(bbr_states),
1461 OID_AUTO, "gain_2_target", CTLFLAG_RW,
1462 &bbr_gain_to_target, 1,
1463 "Does probe bw gain to target??");
1464 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1465 SYSCTL_CHILDREN(bbr_states),
1466 OID_AUTO, "gain_extra_time", CTLFLAG_RW,
1467 &bbr_gain_gets_extra_too, 1,
1468 "Does probe bw gain get the extra time too?");
1469 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1470 SYSCTL_CHILDREN(bbr_states),
1471 OID_AUTO, "ld_div", CTLFLAG_RW,
1472 &bbr_drain_drop_div, 5,
1473 "Long drain drop divider?");
1474 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1475 SYSCTL_CHILDREN(bbr_states),
1476 OID_AUTO, "ld_mul", CTLFLAG_RW,
1477 &bbr_drain_drop_mul, 4,
1478 "Long drain drop multiplier?");
1479 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1480 SYSCTL_CHILDREN(bbr_states),
1481 OID_AUTO, "rand_ot_disc", CTLFLAG_RW,
1482 &bbr_rand_ot, 50,
1483 "Random discount of the ot?");
1484 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1485 SYSCTL_CHILDREN(bbr_states),
1486 OID_AUTO, "dr_filter_life", CTLFLAG_RW,
1487 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT,
1488 "How many packet-epochs does the b/w delivery rate last?");
1489 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1490 SYSCTL_CHILDREN(bbr_states),
1491 OID_AUTO, "subdrain_applimited", CTLFLAG_RW,
1492 &bbr_sub_drain_app_limit, 0,
1493 "Does our sub-state drain invoke app limited if its long?");
1494 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1495 SYSCTL_CHILDREN(bbr_states),
1496 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW,
1497 &bbr_sub_drain_slam_cwnd, 0,
1498 "Should we set/recover cwnd for sub-state drain?");
1499 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1500 SYSCTL_CHILDREN(bbr_states),
1501 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW,
1502 &bbr_slam_cwnd_in_main_drain, 0,
1503 "Should we set/recover cwnd for main-state drain?");
1504 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1505 SYSCTL_CHILDREN(bbr_states),
1506 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW,
1507 &google_allow_early_out, 1,
1508 "Should we allow google probe-bw/drain to exit early at flight target?");
1509 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1510 SYSCTL_CHILDREN(bbr_states),
1511 OID_AUTO, "google_exit_loss", CTLFLAG_RW,
1512 &google_consider_lost, 1,
1513 "Should we have losses exit gain of probebw in google mode??");
1514 /* Startup controls */
1515 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1516 SYSCTL_CHILDREN(bbr_sysctl_root),
1517 OID_AUTO,
1518 "startup",
1519 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1520 "Startup controls");
1521 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1522 SYSCTL_CHILDREN(bbr_startup),
1523 OID_AUTO, "cheat_iwnd", CTLFLAG_RW,
1524 &bbr_sends_full_iwnd, 1,
1525 "Do we not pace but burst out initial windows has our TSO size?");
1526 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1527 SYSCTL_CHILDREN(bbr_startup),
1528 OID_AUTO, "loss_threshold", CTLFLAG_RW,
1529 &bbr_startup_loss_thresh, 2000,
1530 "In startup what is the loss threshold in a pe that will exit us from startup?");
1531 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1532 SYSCTL_CHILDREN(bbr_startup),
1533 OID_AUTO, "use_lowerpg", CTLFLAG_RW,
1534 &bbr_use_lower_gain_in_startup, 1,
1535 "Should we use a lower hptsi gain if we see loss in startup?");
1536 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1537 SYSCTL_CHILDREN(bbr_startup),
1538 OID_AUTO, "gain", CTLFLAG_RW,
1539 &bbr_start_exit, 25,
1540 "What gain percent do we need to see to stay in startup??");
1541 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1542 SYSCTL_CHILDREN(bbr_startup),
1543 OID_AUTO, "low_gain", CTLFLAG_RW,
1544 &bbr_low_start_exit, 15,
1545 "What gain percent do we need to see to stay in the lower gain startup??");
1546 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1547 SYSCTL_CHILDREN(bbr_startup),
1548 OID_AUTO, "loss_exit", CTLFLAG_RW,
1549 &bbr_exit_startup_at_loss, 1,
1550 "Should we exit startup at loss in an epoch if we are not gaining?");
1551 /* CWND controls */
1552 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1553 SYSCTL_CHILDREN(bbr_sysctl_root),
1554 OID_AUTO,
1555 "cwnd",
1556 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1557 "Cwnd controls");
1558 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1559 SYSCTL_CHILDREN(bbr_cwnd),
1560 OID_AUTO, "tar_rtt", CTLFLAG_RW,
1561 &bbr_cwndtarget_rtt_touse, 0,
1562 "Target cwnd rtt measurment to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?");
1563 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1564 SYSCTL_CHILDREN(bbr_cwnd),
1565 OID_AUTO, "may_shrink", CTLFLAG_RW,
1566 &bbr_cwnd_may_shrink, 0,
1567 "Can the cwnd shrink if it would grow to more than the target?");
1568 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1569 SYSCTL_CHILDREN(bbr_cwnd),
1570 OID_AUTO, "max_target_limit", CTLFLAG_RW,
1571 &bbr_target_cwnd_mult_limit, 8,
1572 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?");
1573 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1574 SYSCTL_CHILDREN(bbr_cwnd),
1575 OID_AUTO, "highspeed_min", CTLFLAG_RW,
1576 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS,
1577 "What is the high-speed min cwnd (rttProp under 1ms)");
1578 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1579 SYSCTL_CHILDREN(bbr_cwnd),
1580 OID_AUTO, "lowspeed_min", CTLFLAG_RW,
1581 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS,
1582 "What is the min cwnd (rttProp > 1ms)");
1583 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1584 SYSCTL_CHILDREN(bbr_cwnd),
1585 OID_AUTO, "initwin", CTLFLAG_RW,
1586 &bbr_def_init_win, 10,
1587 "What is the BBR initial window, if 0 use tcp version");
1588 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1589 SYSCTL_CHILDREN(bbr_cwnd),
1590 OID_AUTO, "do_loss_red", CTLFLAG_RW,
1591 &bbr_do_red, 600,
1592 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?");
1593 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1594 SYSCTL_CHILDREN(bbr_cwnd),
1595 OID_AUTO, "red_scale", CTLFLAG_RW,
1596 &bbr_red_scale, 20000,
1597 "What RTT do we scale with?");
1598 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1599 SYSCTL_CHILDREN(bbr_cwnd),
1600 OID_AUTO, "red_growslow", CTLFLAG_RW,
1601 &bbr_red_growth_restrict, 1,
1602 "Do we restrict cwnd growth for whats in flight?");
1603 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1604 SYSCTL_CHILDREN(bbr_cwnd),
1605 OID_AUTO, "red_div", CTLFLAG_RW,
1606 &bbr_red_div, 2,
1607 "If we reduce whats the divisor?");
1608 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1609 SYSCTL_CHILDREN(bbr_cwnd),
1610 OID_AUTO, "red_mul", CTLFLAG_RW,
1611 &bbr_red_mul, 1,
1612 "If we reduce whats the mulitiplier?");
1613 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1614 SYSCTL_CHILDREN(bbr_cwnd),
1615 OID_AUTO, "target_is_unit", CTLFLAG_RW,
1616 &bbr_target_is_bbunit, 0,
1617 "Is the state target the pacing_gain or BBR_UNIT?");
1618 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1619 SYSCTL_CHILDREN(bbr_cwnd),
1620 OID_AUTO, "drop_limit", CTLFLAG_RW,
1621 &bbr_drop_limit, 0,
1622 "Number of segments limit for drop (0=use min_cwnd w/flight)?");
1623
1624 /* Timeout controls */
1625 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1626 SYSCTL_CHILDREN(bbr_sysctl_root),
1627 OID_AUTO,
1628 "timeout",
1629 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1630 "Time out controls");
1631 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1632 SYSCTL_CHILDREN(bbr_timeout),
1633 OID_AUTO, "delack", CTLFLAG_RW,
1634 &bbr_delack_time, 100000,
1635 "BBR's delayed ack time");
1636 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1637 SYSCTL_CHILDREN(bbr_timeout),
1638 OID_AUTO, "tlp_uses", CTLFLAG_RW,
1639 &bbr_tlp_type_to_use, 3,
1640 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt");
1641 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1642 SYSCTL_CHILDREN(bbr_timeout),
1643 OID_AUTO, "persmin", CTLFLAG_RW,
1644 &bbr_persist_min, 250000,
1645 "What is the minimum time in microseconds between persists");
1646 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1647 SYSCTL_CHILDREN(bbr_timeout),
1648 OID_AUTO, "persmax", CTLFLAG_RW,
1649 &bbr_persist_max, 1000000,
1650 "What is the largest delay in microseconds between persists");
1651 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1652 SYSCTL_CHILDREN(bbr_timeout),
1653 OID_AUTO, "tlp_minto", CTLFLAG_RW,
1654 &bbr_tlp_min, 10000,
1655 "TLP Min timeout in usecs");
1656 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1657 SYSCTL_CHILDREN(bbr_timeout),
1658 OID_AUTO, "tlp_dack_time", CTLFLAG_RW,
1659 &bbr_delayed_ack_time, 200000,
1660 "TLP delayed ack compensation value");
1661 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1662 SYSCTL_CHILDREN(bbr_sysctl_root),
1663 OID_AUTO, "minrto", CTLFLAG_RW,
1664 &bbr_rto_min_ms, 30,
1665 "Minimum RTO in ms");
1666 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1667 SYSCTL_CHILDREN(bbr_timeout),
1668 OID_AUTO, "maxrto", CTLFLAG_RW,
1669 &bbr_rto_max_sec, 4,
1670 "Maxiumum RTO in seconds -- should be at least as large as min_rto");
1671 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1672 SYSCTL_CHILDREN(bbr_timeout),
1673 OID_AUTO, "tlp_retry", CTLFLAG_RW,
1674 &bbr_tlp_max_resend, 2,
1675 "How many times does TLP retry a single segment or multiple with no ACK");
1676 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1677 SYSCTL_CHILDREN(bbr_timeout),
1678 OID_AUTO, "minto", CTLFLAG_RW,
1679 &bbr_min_to, 1000,
1680 "Minimum rack timeout in useconds");
1681 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1682 SYSCTL_CHILDREN(bbr_timeout),
1683 OID_AUTO, "pktdelay", CTLFLAG_RW,
1684 &bbr_pkt_delay, 1000,
1685 "Extra RACK time (in useconds) besides reordering thresh");
1686 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1687 SYSCTL_CHILDREN(bbr_timeout),
1688 OID_AUTO, "incr_tmrs", CTLFLAG_RW,
1689 &bbr_incr_timers, 1,
1690 "Increase the RXT/TLP timer by the pacing time used?");
1691 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1692 SYSCTL_CHILDREN(bbr_timeout),
1693 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW,
1694 &bbr_marks_rxt_sack_passed, 0,
1695 "Mark sack passed on all those not ack'd when a RXT hits?");
1696 /* Policer controls */
1697 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1698 SYSCTL_CHILDREN(bbr_sysctl_root),
1699 OID_AUTO,
1700 "policer",
1701 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1702 "Policer controls");
1703 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1704 SYSCTL_CHILDREN(bbr_policer),
1705 OID_AUTO, "detect_enable", CTLFLAG_RW,
1706 &bbr_policer_detection_enabled, 1,
1707 "Is policer detection enabled??");
1708 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1709 SYSCTL_CHILDREN(bbr_policer),
1710 OID_AUTO, "min_pes", CTLFLAG_RW,
1711 &bbr_lt_intvl_min_rtts, 4,
1712 "Minimum number of PE's?");
1713 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1714 SYSCTL_CHILDREN(bbr_policer),
1715 OID_AUTO, "bwdiff", CTLFLAG_RW,
1716 &bbr_lt_bw_diff, (4000/8),
1717 "Minimal bw diff?");
1718 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1719 SYSCTL_CHILDREN(bbr_policer),
1720 OID_AUTO, "bwratio", CTLFLAG_RW,
1721 &bbr_lt_bw_ratio, 8,
1722 "Minimal bw diff?");
1723 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1724 SYSCTL_CHILDREN(bbr_policer),
1725 OID_AUTO, "from_rack_rxt", CTLFLAG_RW,
1726 &bbr_policer_call_from_rack_to, 0,
1727 "Do we call the policer detection code from a rack-timeout?");
1728 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1729 SYSCTL_CHILDREN(bbr_policer),
1730 OID_AUTO, "false_postive", CTLFLAG_RW,
1731 &bbr_lt_intvl_fp, 0,
1732 "What packet epoch do we do false-postive detection at (0=no)?");
1733 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1734 SYSCTL_CHILDREN(bbr_policer),
1735 OID_AUTO, "loss_thresh", CTLFLAG_RW,
1736 &bbr_lt_loss_thresh, 196,
1737 "Loss threshold 196 = 19.6%?");
1738 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1739 SYSCTL_CHILDREN(bbr_policer),
1740 OID_AUTO, "false_postive_thresh", CTLFLAG_RW,
1741 &bbr_lt_fd_thresh, 100,
1742 "What percentage is the false detection threshold (150=15.0)?");
1743 /* All the rest */
1744 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1745 SYSCTL_CHILDREN(bbr_sysctl_root),
1746 OID_AUTO, "cheat_rxt", CTLFLAG_RW,
1747 &bbr_use_rack_resend_cheat, 0,
1748 "Do we burst 1ms between sends on retransmissions (like rack)?");
1749 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1750 SYSCTL_CHILDREN(bbr_sysctl_root),
1751 OID_AUTO, "error_paceout", CTLFLAG_RW,
1752 &bbr_error_base_paceout, 10000,
1753 "When we hit an error what is the min to pace out in usec's?");
1754 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1755 SYSCTL_CHILDREN(bbr_sysctl_root),
1756 OID_AUTO, "kill_paceout", CTLFLAG_RW,
1757 &bbr_max_net_error_cnt, 10,
1758 "When we hit this many errors in a row, kill the session?");
1759 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1760 SYSCTL_CHILDREN(bbr_sysctl_root),
1761 OID_AUTO, "data_after_close", CTLFLAG_RW,
1762 &bbr_ignore_data_after_close, 1,
1763 "Do we hold off sending a RST until all pending data is ack'd");
1764 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1765 SYSCTL_CHILDREN(bbr_sysctl_root),
1766 OID_AUTO, "resend_use_tso", CTLFLAG_RW,
1767 &bbr_resends_use_tso, 0,
1768 "Can resends use TSO?");
1769 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1770 SYSCTL_CHILDREN(bbr_sysctl_root),
1771 OID_AUTO, "sblklimit", CTLFLAG_RW,
1772 &bbr_sack_block_limit, 128,
1773 "When do we start ignoring small sack blocks");
1774 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1775 SYSCTL_CHILDREN(bbr_sysctl_root),
1776 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1777 &bbr_verbose_logging, 0,
1778 "Should BBR black box logging be verbose");
1779 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1780 SYSCTL_CHILDREN(bbr_sysctl_root),
1781 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1782 &bbr_reorder_thresh, 2,
1783 "What factor for rack will be added when seeing reordering (shift right)");
1784 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1785 SYSCTL_CHILDREN(bbr_sysctl_root),
1786 OID_AUTO, "reorder_fade", CTLFLAG_RW,
1787 &bbr_reorder_fade, 0,
1788 "Does reorder detection fade, if so how many ms (0 means never)");
1789 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1790 SYSCTL_CHILDREN(bbr_sysctl_root),
1791 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1792 &bbr_tlp_thresh, 1,
1793 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1794 /* Stats and counters */
1795 /* The pacing counters for hdwr/software can't be in the array */
1796 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1797 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1798 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1799 SYSCTL_CHILDREN(bbr_sysctl_root),
1800 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD,
1801 &bbr_hdwr_pacing_enobuf,
1802 "Total number of enobufs for hardware paced flows");
1803 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1804 SYSCTL_CHILDREN(bbr_sysctl_root),
1805 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD,
1806 &bbr_nohdwr_pacing_enobuf,
1807 "Total number of enobufs for non-hardware paced flows");
1808
1809 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK);
1810 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1811 SYSCTL_CHILDREN(bbr_sysctl_root),
1812 OID_AUTO, "hdwr_pacing", CTLFLAG_RD,
1813 &bbr_flows_whdwr_pacing,
1814 "Total number of hardware paced flows");
1815 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK);
1816 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1817 SYSCTL_CHILDREN(bbr_sysctl_root),
1818 OID_AUTO, "software_pacing", CTLFLAG_RD,
1819 &bbr_flows_nohdwr_pacing,
1820 "Total number of software paced flows");
1821 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK);
1822 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1823 OID_AUTO, "stats", CTLFLAG_RD,
1824 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats");
1825 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK);
1826 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1827 OID_AUTO, "opts", CTLFLAG_RD,
1828 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats");
1829 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK);
1830 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1831 OID_AUTO, "lost", CTLFLAG_RD,
1832 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur");
1833 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK);
1834 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1835 OID_AUTO, "stateresend", CTLFLAG_RD,
1836 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend");
1837 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK);
1838 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1839 OID_AUTO, "statetime", CTLFLAG_RD,
1840 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states");
1841 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1842 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1843 OID_AUTO, "outsize", CTLFLAG_RD,
1844 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls");
1845 SYSCTL_ADD_PROC(&bbr_sysctl_ctx,
1846 SYSCTL_CHILDREN(bbr_sysctl_root),
1847 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1848 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters");
1849 }
1850
1851 static void
bbr_counter_destroy(void)1852 bbr_counter_destroy(void)
1853 {
1854 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE);
1855 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE);
1856 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE);
1857 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT);
1858 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT);
1859 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT);
1860 counter_u64_free(bbr_nohdwr_pacing_enobuf);
1861 counter_u64_free(bbr_hdwr_pacing_enobuf);
1862 counter_u64_free(bbr_flows_whdwr_pacing);
1863 counter_u64_free(bbr_flows_nohdwr_pacing);
1864
1865 }
1866
1867 static __inline void
bbr_fill_in_logging_data(struct tcp_bbr * bbr,struct tcp_log_bbr * l,uint32_t cts)1868 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts)
1869 {
1870 memset(l, 0, sizeof(union tcp_log_stackspecific));
1871 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate;
1872 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate);
1873 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
1874 l->bw_inuse = bbr_get_bw(bbr);
1875 l->inflight = ctf_flight_size(bbr->rc_tp,
1876 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
1877 l->applimited = bbr->r_ctl.r_app_limited_until;
1878 l->delivered = bbr->r_ctl.rc_delivered;
1879 l->timeStamp = cts;
1880 l->lost = bbr->r_ctl.rc_lost;
1881 l->bbr_state = bbr->rc_bbr_state;
1882 l->bbr_substate = bbr_state_val(bbr);
1883 l->epoch = bbr->r_ctl.rc_rtt_epoch;
1884 l->lt_epoch = bbr->r_ctl.rc_lt_epoch;
1885 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
1886 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain;
1887 l->inhpts = bbr->rc_inp->inp_in_hpts;
1888 l->ininput = bbr->rc_inp->inp_in_input;
1889 l->use_lt_bw = bbr->rc_lt_use_bw;
1890 l->pkts_out = bbr->r_ctl.rc_flight_at_input;
1891 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch;
1892 }
1893
1894 static void
bbr_log_type_bw_reduce(struct tcp_bbr * bbr,int reason)1895 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason)
1896 {
1897 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1898 union tcp_log_stackspecific log;
1899
1900 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1901 log.u_bbr.flex1 = 0;
1902 log.u_bbr.flex2 = 0;
1903 log.u_bbr.flex5 = 0;
1904 log.u_bbr.flex3 = 0;
1905 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate;
1906 log.u_bbr.flex7 = reason;
1907 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt;
1908 log.u_bbr.flex8 = 0;
1909 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1910 &bbr->rc_inp->inp_socket->so_rcv,
1911 &bbr->rc_inp->inp_socket->so_snd,
1912 BBR_LOG_BW_RED_EV, 0,
1913 0, &log, false, &bbr->rc_tv);
1914 }
1915 }
1916
1917 static void
bbr_log_type_rwnd_collapse(struct tcp_bbr * bbr,int seq,int mode,uint32_t count)1918 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count)
1919 {
1920 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1921 union tcp_log_stackspecific log;
1922
1923 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1924 log.u_bbr.flex1 = seq;
1925 log.u_bbr.flex2 = count;
1926 log.u_bbr.flex8 = mode;
1927 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1928 &bbr->rc_inp->inp_socket->so_rcv,
1929 &bbr->rc_inp->inp_socket->so_snd,
1930 BBR_LOG_LOWGAIN, 0,
1931 0, &log, false, &bbr->rc_tv);
1932 }
1933 }
1934
1935 static void
bbr_log_type_just_return(struct tcp_bbr * bbr,uint32_t cts,uint32_t tlen,uint8_t hpts_calling,uint8_t reason,uint32_t p_maxseg,int len)1936 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling,
1937 uint8_t reason, uint32_t p_maxseg, int len)
1938 {
1939 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1940 union tcp_log_stackspecific log;
1941
1942 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1943 log.u_bbr.flex1 = p_maxseg;
1944 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags;
1945 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
1946 log.u_bbr.flex4 = reason;
1947 log.u_bbr.flex5 = bbr->rc_in_persist;
1948 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val;
1949 log.u_bbr.flex7 = p_maxseg;
1950 log.u_bbr.flex8 = bbr->rc_in_persist;
1951 log.u_bbr.pkts_out = 0;
1952 log.u_bbr.applimited = len;
1953 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1954 &bbr->rc_inp->inp_socket->so_rcv,
1955 &bbr->rc_inp->inp_socket->so_snd,
1956 BBR_LOG_JUSTRET, 0,
1957 tlen, &log, false, &bbr->rc_tv);
1958 }
1959 }
1960
1961 static void
bbr_log_type_enter_rec(struct tcp_bbr * bbr,uint32_t seq)1962 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq)
1963 {
1964 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1965 union tcp_log_stackspecific log;
1966
1967 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1968 log.u_bbr.flex1 = seq;
1969 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
1970 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start;
1971 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1972 &bbr->rc_inp->inp_socket->so_rcv,
1973 &bbr->rc_inp->inp_socket->so_snd,
1974 BBR_LOG_ENTREC, 0,
1975 0, &log, false, &bbr->rc_tv);
1976 }
1977 }
1978
1979 static void
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)1980 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)
1981 {
1982 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1983 union tcp_log_stackspecific log;
1984
1985 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1986 log.u_bbr.flex1 = tso;
1987 log.u_bbr.flex2 = maxseg;
1988 log.u_bbr.flex3 = mtu;
1989 log.u_bbr.flex4 = csum_flags;
1990 TCP_LOG_EVENTP(tp, NULL,
1991 &bbr->rc_inp->inp_socket->so_rcv,
1992 &bbr->rc_inp->inp_socket->so_snd,
1993 BBR_LOG_MSGSIZE, 0,
1994 0, &log, false, &bbr->rc_tv);
1995 }
1996 }
1997
1998 static void
bbr_log_flowend(struct tcp_bbr * bbr)1999 bbr_log_flowend(struct tcp_bbr *bbr)
2000 {
2001 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2002 union tcp_log_stackspecific log;
2003 struct sockbuf *r, *s;
2004 struct timeval tv;
2005
2006 if (bbr->rc_inp->inp_socket) {
2007 r = &bbr->rc_inp->inp_socket->so_rcv;
2008 s = &bbr->rc_inp->inp_socket->so_snd;
2009 } else {
2010 r = s = NULL;
2011 }
2012 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv));
2013 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2014 r, s,
2015 TCP_LOG_FLOWEND, 0,
2016 0, &log, false, &tv);
2017 }
2018 }
2019
2020 static void
bbr_log_pkt_epoch(struct tcp_bbr * bbr,uint32_t cts,uint32_t line,uint32_t lost,uint32_t del)2021 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line,
2022 uint32_t lost, uint32_t del)
2023 {
2024 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2025 union tcp_log_stackspecific log;
2026
2027 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2028 log.u_bbr.flex1 = lost;
2029 log.u_bbr.flex2 = del;
2030 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw;
2031 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt;
2032 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2033 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2034 log.u_bbr.flex7 = line;
2035 log.u_bbr.flex8 = 0;
2036 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count;
2037 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2038 &bbr->rc_inp->inp_socket->so_rcv,
2039 &bbr->rc_inp->inp_socket->so_snd,
2040 BBR_LOG_PKT_EPOCH, 0,
2041 0, &log, false, &bbr->rc_tv);
2042 }
2043 }
2044
2045 static void
bbr_log_time_epoch(struct tcp_bbr * bbr,uint32_t cts,uint32_t line,uint32_t epoch_time)2046 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time)
2047 {
2048 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2049 union tcp_log_stackspecific log;
2050
2051 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2052 log.u_bbr.flex1 = bbr->r_ctl.rc_lost;
2053 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat;
2054 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat;
2055 log.u_bbr.flex7 = line;
2056 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2057 &bbr->rc_inp->inp_socket->so_rcv,
2058 &bbr->rc_inp->inp_socket->so_snd,
2059 BBR_LOG_TIME_EPOCH, 0,
2060 0, &log, false, &bbr->rc_tv);
2061 }
2062 }
2063
2064 static void
bbr_log_set_of_state_target(struct tcp_bbr * bbr,uint32_t new_tar,int line,int meth)2065 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth)
2066 {
2067 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2068 union tcp_log_stackspecific log;
2069
2070 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2071 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2072 log.u_bbr.flex2 = new_tar;
2073 log.u_bbr.flex3 = line;
2074 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2075 log.u_bbr.flex5 = bbr_quanta;
2076 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs;
2077 log.u_bbr.flex7 = bbr->rc_last_options;
2078 log.u_bbr.flex8 = meth;
2079 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2080 &bbr->rc_inp->inp_socket->so_rcv,
2081 &bbr->rc_inp->inp_socket->so_snd,
2082 BBR_LOG_STATE_TARGET, 0,
2083 0, &log, false, &bbr->rc_tv);
2084 }
2085
2086 }
2087
2088 static void
bbr_log_type_statechange(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2089 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2090 {
2091 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2092 union tcp_log_stackspecific log;
2093
2094 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2095 log.u_bbr.flex1 = line;
2096 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2097 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2098 if (bbr_state_is_pkt_epoch)
2099 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
2100 else
2101 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP);
2102 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2103 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2104 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000);
2105 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra;
2106 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state;
2107 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2108 &bbr->rc_inp->inp_socket->so_rcv,
2109 &bbr->rc_inp->inp_socket->so_snd,
2110 BBR_LOG_STATE, 0,
2111 0, &log, false, &bbr->rc_tv);
2112 }
2113 }
2114
2115 static void
bbr_log_rtt_shrinks(struct tcp_bbr * bbr,uint32_t cts,uint32_t applied,uint32_t rtt,uint32_t line,uint8_t reas,uint16_t cond)2116 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
2117 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond)
2118 {
2119 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2120 union tcp_log_stackspecific log;
2121
2122 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2123 log.u_bbr.flex1 = line;
2124 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2125 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt;
2126 log.u_bbr.flex4 = applied;
2127 log.u_bbr.flex5 = rtt;
2128 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2129 log.u_bbr.flex7 = cond;
2130 log.u_bbr.flex8 = reas;
2131 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2132 &bbr->rc_inp->inp_socket->so_rcv,
2133 &bbr->rc_inp->inp_socket->so_snd,
2134 BBR_LOG_RTT_SHRINKS, 0,
2135 0, &log, false, &bbr->rc_tv);
2136 }
2137 }
2138
2139 static void
bbr_log_type_exit_rec(struct tcp_bbr * bbr)2140 bbr_log_type_exit_rec(struct tcp_bbr *bbr)
2141 {
2142 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2143 union tcp_log_stackspecific log;
2144
2145 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2146 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start;
2147 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
2148 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2149 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2150 &bbr->rc_inp->inp_socket->so_rcv,
2151 &bbr->rc_inp->inp_socket->so_snd,
2152 BBR_LOG_EXITREC, 0,
2153 0, &log, false, &bbr->rc_tv);
2154 }
2155 }
2156
2157 static void
bbr_log_type_cwndupd(struct tcp_bbr * bbr,uint32_t bytes_this_ack,uint32_t chg,uint32_t prev_acked,int32_t meth,uint32_t target,uint32_t th_ack,int32_t line)2158 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg,
2159 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line)
2160 {
2161 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2162 union tcp_log_stackspecific log;
2163
2164 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2165 log.u_bbr.flex1 = line;
2166 log.u_bbr.flex2 = prev_acked;
2167 log.u_bbr.flex3 = bytes_this_ack;
2168 log.u_bbr.flex4 = chg;
2169 log.u_bbr.flex5 = th_ack;
2170 log.u_bbr.flex6 = target;
2171 log.u_bbr.flex8 = meth;
2172 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2173 &bbr->rc_inp->inp_socket->so_rcv,
2174 &bbr->rc_inp->inp_socket->so_snd,
2175 BBR_LOG_CWND, 0,
2176 0, &log, false, &bbr->rc_tv);
2177 }
2178 }
2179
2180 static void
bbr_log_rtt_sample(struct tcp_bbr * bbr,uint32_t rtt,uint32_t tsin)2181 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin)
2182 {
2183 /*
2184 * Log the rtt sample we are applying to the srtt algorithm in
2185 * useconds.
2186 */
2187 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2188 union tcp_log_stackspecific log;
2189
2190 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2191 log.u_bbr.flex1 = rtt;
2192 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time;
2193 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay;
2194 log.u_bbr.flex4 = bbr->rc_tp->ts_offset;
2195 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2196 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv);
2197 log.u_bbr.flex6 = tsin;
2198 log.u_bbr.flex7 = 0;
2199 log.u_bbr.flex8 = bbr->rc_ack_was_delayed;
2200 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2201 &bbr->rc_inp->inp_socket->so_rcv,
2202 &bbr->rc_inp->inp_socket->so_snd,
2203 TCP_LOG_RTT, 0,
2204 0, &log, false, &bbr->rc_tv);
2205 }
2206 }
2207
2208 static void
bbr_log_type_pesist(struct tcp_bbr * bbr,uint32_t cts,uint32_t time_in,int32_t line,uint8_t enter_exit)2209 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit)
2210 {
2211 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2212 union tcp_log_stackspecific log;
2213
2214 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2215 log.u_bbr.flex1 = time_in;
2216 log.u_bbr.flex2 = line;
2217 log.u_bbr.flex8 = enter_exit;
2218 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2219 &bbr->rc_inp->inp_socket->so_rcv,
2220 &bbr->rc_inp->inp_socket->so_snd,
2221 BBR_LOG_PERSIST, 0,
2222 0, &log, false, &bbr->rc_tv);
2223 }
2224 }
2225 static void
bbr_log_ack_clear(struct tcp_bbr * bbr,uint32_t cts)2226 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts)
2227 {
2228 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2229 union tcp_log_stackspecific log;
2230
2231 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2232 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age;
2233 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2234 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2235 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time;
2236 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2237 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2238 &bbr->rc_inp->inp_socket->so_rcv,
2239 &bbr->rc_inp->inp_socket->so_snd,
2240 BBR_LOG_ACKCLEAR, 0,
2241 0, &log, false, &bbr->rc_tv);
2242 }
2243 }
2244
2245 static void
bbr_log_ack_event(struct tcp_bbr * bbr,struct tcphdr * th,struct tcpopt * to,uint32_t tlen,uint16_t nsegs,uint32_t cts,int32_t nxt_pkt,struct mbuf * m)2246 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen,
2247 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m)
2248 {
2249 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2250 union tcp_log_stackspecific log;
2251 struct timeval tv;
2252
2253 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2254 log.u_bbr.flex1 = nsegs;
2255 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes;
2256 if (m) {
2257 struct timespec ts;
2258
2259 log.u_bbr.flex3 = m->m_flags;
2260 if (m->m_flags & M_TSTMP) {
2261 mbuf_tstmp2timespec(m, &ts);
2262 tv.tv_sec = ts.tv_sec;
2263 tv.tv_usec = ts.tv_nsec / 1000;
2264 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv);
2265 } else {
2266 log.u_bbr.lt_epoch = 0;
2267 }
2268 if (m->m_flags & M_TSTMP_LRO) {
2269 tv.tv_sec = m->m_pkthdr.rcv_tstmp / 1000000000;
2270 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000) / 1000;
2271 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv);
2272 } else {
2273 /* No arrival timestamp */
2274 log.u_bbr.flex5 = 0;
2275 }
2276
2277 log.u_bbr.pkts_out = tcp_get_usecs(&tv);
2278 } else {
2279 log.u_bbr.flex3 = 0;
2280 log.u_bbr.flex5 = 0;
2281 log.u_bbr.flex6 = 0;
2282 log.u_bbr.pkts_out = 0;
2283 }
2284 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state;
2285 log.u_bbr.flex7 = bbr->r_wanted_output;
2286 log.u_bbr.flex8 = bbr->rc_in_persist;
2287 TCP_LOG_EVENTP(bbr->rc_tp, th,
2288 &bbr->rc_inp->inp_socket->so_rcv,
2289 &bbr->rc_inp->inp_socket->so_snd,
2290 TCP_LOG_IN, 0,
2291 tlen, &log, true, &bbr->rc_tv);
2292 }
2293 }
2294
2295 static void
bbr_log_doseg_done(struct tcp_bbr * bbr,uint32_t cts,int32_t nxt_pkt,int32_t did_out)2296 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out)
2297 {
2298 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2299 union tcp_log_stackspecific log;
2300
2301 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2302 log.u_bbr.flex1 = did_out;
2303 log.u_bbr.flex2 = nxt_pkt;
2304 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val;
2305 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2306 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp;
2307 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes;
2308 log.u_bbr.flex7 = bbr->r_wanted_output;
2309 log.u_bbr.flex8 = bbr->rc_in_persist;
2310 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay;
2311 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2312 &bbr->rc_inp->inp_socket->so_rcv,
2313 &bbr->rc_inp->inp_socket->so_snd,
2314 BBR_LOG_DOSEG_DONE, 0,
2315 0, &log, true, &bbr->rc_tv);
2316 }
2317 }
2318
2319 static void
bbr_log_enobuf_jmp(struct tcp_bbr * bbr,uint32_t len,uint32_t cts,int32_t line,uint32_t o_len,uint32_t segcnt,uint32_t segsiz)2320 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts,
2321 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz)
2322 {
2323 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2324 union tcp_log_stackspecific log;
2325
2326 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2327 log.u_bbr.flex1 = line;
2328 log.u_bbr.flex2 = o_len;
2329 log.u_bbr.flex3 = segcnt;
2330 log.u_bbr.flex4 = segsiz;
2331 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2332 &bbr->rc_inp->inp_socket->so_rcv,
2333 &bbr->rc_inp->inp_socket->so_snd,
2334 BBR_LOG_ENOBUF_JMP, ENOBUFS,
2335 len, &log, true, &bbr->rc_tv);
2336 }
2337 }
2338
2339 static void
bbr_log_to_processing(struct tcp_bbr * bbr,uint32_t cts,int32_t ret,int32_t timers,uint8_t hpts_calling)2340 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling)
2341 {
2342 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2343 union tcp_log_stackspecific log;
2344
2345 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2346 log.u_bbr.flex1 = timers;
2347 log.u_bbr.flex2 = ret;
2348 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
2349 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2350 log.u_bbr.flex5 = cts;
2351 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2352 log.u_bbr.flex8 = hpts_calling;
2353 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2354 &bbr->rc_inp->inp_socket->so_rcv,
2355 &bbr->rc_inp->inp_socket->so_snd,
2356 BBR_LOG_TO_PROCESS, 0,
2357 0, &log, false, &bbr->rc_tv);
2358 }
2359 }
2360
2361 static void
bbr_log_to_event(struct tcp_bbr * bbr,uint32_t cts,int32_t to_num)2362 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num)
2363 {
2364 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2365 union tcp_log_stackspecific log;
2366 uint64_t ar;
2367
2368 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2369 log.u_bbr.flex1 = bbr->bbr_timer_src;
2370 log.u_bbr.flex2 = 0;
2371 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2372 ar = (uint64_t)(bbr->r_ctl.rc_resend);
2373 ar >>= 32;
2374 ar &= 0x00000000ffffffff;
2375 log.u_bbr.flex4 = (uint32_t)ar;
2376 ar = (uint64_t)bbr->r_ctl.rc_resend;
2377 ar &= 0x00000000ffffffff;
2378 log.u_bbr.flex5 = (uint32_t)ar;
2379 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2380 log.u_bbr.flex8 = to_num;
2381 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2382 &bbr->rc_inp->inp_socket->so_rcv,
2383 &bbr->rc_inp->inp_socket->so_snd,
2384 BBR_LOG_RTO, 0,
2385 0, &log, false, &bbr->rc_tv);
2386 }
2387 }
2388
2389 static void
bbr_log_startup_event(struct tcp_bbr * bbr,uint32_t cts,uint32_t flex1,uint32_t flex2,uint32_t flex3,uint8_t reason)2390 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason)
2391 {
2392 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2393 union tcp_log_stackspecific log;
2394
2395 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2396 log.u_bbr.flex1 = flex1;
2397 log.u_bbr.flex2 = flex2;
2398 log.u_bbr.flex3 = flex3;
2399 log.u_bbr.flex4 = 0;
2400 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2401 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2402 log.u_bbr.flex8 = reason;
2403 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw;
2404 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2405 &bbr->rc_inp->inp_socket->so_rcv,
2406 &bbr->rc_inp->inp_socket->so_snd,
2407 BBR_LOG_REDUCE, 0,
2408 0, &log, false, &bbr->rc_tv);
2409 }
2410 }
2411
2412 static void
bbr_log_hpts_diag(struct tcp_bbr * bbr,uint32_t cts,struct hpts_diag * diag)2413 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag)
2414 {
2415 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2416 union tcp_log_stackspecific log;
2417
2418 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2419 log.u_bbr.flex1 = diag->p_nxt_slot;
2420 log.u_bbr.flex2 = diag->p_cur_slot;
2421 log.u_bbr.flex3 = diag->slot_req;
2422 log.u_bbr.flex4 = diag->inp_hptsslot;
2423 log.u_bbr.flex5 = diag->slot_remaining;
2424 log.u_bbr.flex6 = diag->need_new_to;
2425 log.u_bbr.flex7 = diag->p_hpts_active;
2426 log.u_bbr.flex8 = diag->p_on_min_sleep;
2427 /* Hijack other fields as needed */
2428 log.u_bbr.epoch = diag->have_slept;
2429 log.u_bbr.lt_epoch = diag->yet_to_sleep;
2430 log.u_bbr.pkts_out = diag->co_ret;
2431 log.u_bbr.applimited = diag->hpts_sleep_time;
2432 log.u_bbr.delivered = diag->p_prev_slot;
2433 log.u_bbr.inflight = diag->p_runningtick;
2434 log.u_bbr.bw_inuse = diag->wheel_tick;
2435 log.u_bbr.rttProp = diag->wheel_cts;
2436 log.u_bbr.delRate = diag->maxticks;
2437 log.u_bbr.cur_del_rate = diag->p_curtick;
2438 log.u_bbr.cur_del_rate <<= 32;
2439 log.u_bbr.cur_del_rate |= diag->p_lasttick;
2440 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2441 &bbr->rc_inp->inp_socket->so_rcv,
2442 &bbr->rc_inp->inp_socket->so_snd,
2443 BBR_LOG_HPTSDIAG, 0,
2444 0, &log, false, &bbr->rc_tv);
2445 }
2446 }
2447
2448 static void
bbr_log_timer_var(struct tcp_bbr * bbr,int mode,uint32_t cts,uint32_t time_since_sent,uint32_t srtt,uint32_t thresh,uint32_t to)2449 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
2450 uint32_t thresh, uint32_t to)
2451 {
2452 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2453 union tcp_log_stackspecific log;
2454
2455 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2456 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar;
2457 log.u_bbr.flex2 = time_since_sent;
2458 log.u_bbr.flex3 = srtt;
2459 log.u_bbr.flex4 = thresh;
2460 log.u_bbr.flex5 = to;
2461 log.u_bbr.flex6 = bbr->rc_tp->t_srtt;
2462 log.u_bbr.flex8 = mode;
2463 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2464 &bbr->rc_inp->inp_socket->so_rcv,
2465 &bbr->rc_inp->inp_socket->so_snd,
2466 BBR_LOG_TIMERPREP, 0,
2467 0, &log, false, &bbr->rc_tv);
2468 }
2469 }
2470
2471 static void
bbr_log_pacing_delay_calc(struct tcp_bbr * bbr,uint16_t gain,uint32_t len,uint32_t cts,uint32_t usecs,uint64_t bw,uint32_t override,int mod)2472 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
2473 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod)
2474 {
2475 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2476 union tcp_log_stackspecific log;
2477
2478 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2479 log.u_bbr.flex1 = usecs;
2480 log.u_bbr.flex2 = len;
2481 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff);
2482 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff);
2483 if (override)
2484 log.u_bbr.flex5 = (1 << 2);
2485 else
2486 log.u_bbr.flex5 = 0;
2487 log.u_bbr.flex6 = override;
2488 log.u_bbr.flex7 = gain;
2489 log.u_bbr.flex8 = mod;
2490 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2491 &bbr->rc_inp->inp_socket->so_rcv,
2492 &bbr->rc_inp->inp_socket->so_snd,
2493 BBR_LOG_HPTSI_CALC, 0,
2494 len, &log, false, &bbr->rc_tv);
2495 }
2496 }
2497
2498 static void
bbr_log_to_start(struct tcp_bbr * bbr,uint32_t cts,uint32_t to,int32_t slot,uint8_t which)2499 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2500 {
2501 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2502 union tcp_log_stackspecific log;
2503
2504 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2505
2506 log.u_bbr.flex1 = bbr->bbr_timer_src;
2507 log.u_bbr.flex2 = to;
2508 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2509 log.u_bbr.flex4 = slot;
2510 log.u_bbr.flex5 = bbr->rc_inp->inp_hptsslot;
2511 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2512 log.u_bbr.pkts_out = bbr->rc_inp->inp_flags2;
2513 log.u_bbr.flex8 = which;
2514 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2515 &bbr->rc_inp->inp_socket->so_rcv,
2516 &bbr->rc_inp->inp_socket->so_snd,
2517 BBR_LOG_TIMERSTAR, 0,
2518 0, &log, false, &bbr->rc_tv);
2519 }
2520 }
2521
2522 static void
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)2523 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)
2524 {
2525 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2526 union tcp_log_stackspecific log;
2527
2528 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2529 log.u_bbr.flex1 = thresh;
2530 log.u_bbr.flex2 = lro;
2531 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts;
2532 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
2533 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2534 log.u_bbr.flex6 = srtt;
2535 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift;
2536 log.u_bbr.flex8 = frm;
2537 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2538 &bbr->rc_inp->inp_socket->so_rcv,
2539 &bbr->rc_inp->inp_socket->so_snd,
2540 BBR_LOG_THRESH_CALC, 0,
2541 0, &log, false, &bbr->rc_tv);
2542 }
2543 }
2544
2545 static void
bbr_log_to_cancel(struct tcp_bbr * bbr,int32_t line,uint32_t cts,uint8_t hpts_removed)2546 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed)
2547 {
2548 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2549 union tcp_log_stackspecific log;
2550
2551 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2552 log.u_bbr.flex1 = line;
2553 log.u_bbr.flex2 = bbr->bbr_timer_src;
2554 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2555 log.u_bbr.flex4 = bbr->rc_in_persist;
2556 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2557 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2558 log.u_bbr.flex8 = hpts_removed;
2559 log.u_bbr.pkts_out = bbr->rc_pacer_started;
2560 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2561 &bbr->rc_inp->inp_socket->so_rcv,
2562 &bbr->rc_inp->inp_socket->so_snd,
2563 BBR_LOG_TIMERCANC, 0,
2564 0, &log, false, &bbr->rc_tv);
2565 }
2566 }
2567
2568 static void
bbr_log_tstmp_validation(struct tcp_bbr * bbr,uint64_t peer_delta,uint64_t delta)2569 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta)
2570 {
2571 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2572 union tcp_log_stackspecific log;
2573
2574 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2575 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio;
2576 log.u_bbr.flex2 = (peer_delta >> 32);
2577 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff);
2578 log.u_bbr.flex4 = (delta >> 32);
2579 log.u_bbr.flex5 = (delta & 0x00000000ffffffff);
2580 log.u_bbr.flex7 = bbr->rc_ts_clock_set;
2581 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used;
2582 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2583 &bbr->rc_inp->inp_socket->so_rcv,
2584 &bbr->rc_inp->inp_socket->so_snd,
2585 BBR_LOG_TSTMP_VAL, 0,
2586 0, &log, false, &bbr->rc_tv);
2587 }
2588 }
2589
2590 static void
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)2591 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)
2592 {
2593 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2594 union tcp_log_stackspecific log;
2595
2596 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2597 log.u_bbr.flex1 = tsosz;
2598 log.u_bbr.flex2 = tls;
2599 log.u_bbr.flex3 = tcp_min_hptsi_time;
2600 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min;
2601 log.u_bbr.flex5 = old_val;
2602 log.u_bbr.flex6 = maxseg;
2603 log.u_bbr.flex7 = bbr->rc_no_pacing;
2604 log.u_bbr.flex7 <<= 1;
2605 log.u_bbr.flex7 |= bbr->rc_past_init_win;
2606 if (hdwr)
2607 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google;
2608 else
2609 log.u_bbr.flex8 = bbr->rc_use_google;
2610 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2611 &bbr->rc_inp->inp_socket->so_rcv,
2612 &bbr->rc_inp->inp_socket->so_snd,
2613 BBR_LOG_BBRTSO, 0,
2614 0, &log, false, &bbr->rc_tv);
2615 }
2616 }
2617
2618 static void
bbr_log_type_rsmclear(struct tcp_bbr * bbr,uint32_t cts,struct bbr_sendmap * rsm,uint32_t flags,uint32_t line)2619 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm,
2620 uint32_t flags, uint32_t line)
2621 {
2622 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2623 union tcp_log_stackspecific log;
2624
2625 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2626 log.u_bbr.flex1 = line;
2627 log.u_bbr.flex2 = rsm->r_start;
2628 log.u_bbr.flex3 = rsm->r_end;
2629 log.u_bbr.flex4 = rsm->r_delivered;
2630 log.u_bbr.flex5 = rsm->r_rtr_cnt;
2631 log.u_bbr.flex6 = rsm->r_dupack;
2632 log.u_bbr.flex7 = rsm->r_tim_lastsent[0];
2633 log.u_bbr.flex8 = rsm->r_flags;
2634 /* Hijack the pkts_out fids */
2635 log.u_bbr.applimited = flags;
2636 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2637 &bbr->rc_inp->inp_socket->so_rcv,
2638 &bbr->rc_inp->inp_socket->so_snd,
2639 BBR_RSM_CLEARED, 0,
2640 0, &log, false, &bbr->rc_tv);
2641 }
2642 }
2643
2644 static void
bbr_log_type_bbrupd(struct tcp_bbr * bbr,uint8_t flex8,uint32_t cts,uint32_t flex3,uint32_t flex2,uint32_t flex5,uint32_t flex6,uint32_t pkts_out,int flex7,uint32_t flex4,uint32_t flex1)2645 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts,
2646 uint32_t flex3, uint32_t flex2, uint32_t flex5,
2647 uint32_t flex6, uint32_t pkts_out, int flex7,
2648 uint32_t flex4, uint32_t flex1)
2649 {
2650
2651 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2652 union tcp_log_stackspecific log;
2653
2654 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2655 log.u_bbr.flex1 = flex1;
2656 log.u_bbr.flex2 = flex2;
2657 log.u_bbr.flex3 = flex3;
2658 log.u_bbr.flex4 = flex4;
2659 log.u_bbr.flex5 = flex5;
2660 log.u_bbr.flex6 = flex6;
2661 log.u_bbr.flex7 = flex7;
2662 /* Hijack the pkts_out fids */
2663 log.u_bbr.pkts_out = pkts_out;
2664 log.u_bbr.flex8 = flex8;
2665 if (bbr->rc_ack_was_delayed)
2666 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay;
2667 else
2668 log.u_bbr.epoch = 0;
2669 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2670 &bbr->rc_inp->inp_socket->so_rcv,
2671 &bbr->rc_inp->inp_socket->so_snd,
2672 BBR_LOG_BBRUPD, 0,
2673 flex2, &log, false, &bbr->rc_tv);
2674 }
2675 }
2676
2677 static void
bbr_log_type_ltbw(struct tcp_bbr * bbr,uint32_t cts,int32_t reason,uint32_t newbw,uint32_t obw,uint32_t diff,uint32_t tim)2678 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason,
2679 uint32_t newbw, uint32_t obw, uint32_t diff,
2680 uint32_t tim)
2681 {
2682 if (/*bbr_verbose_logging && */(bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2683 union tcp_log_stackspecific log;
2684
2685 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2686 log.u_bbr.flex1 = reason;
2687 log.u_bbr.flex2 = newbw;
2688 log.u_bbr.flex3 = obw;
2689 log.u_bbr.flex4 = diff;
2690 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost;
2691 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del;
2692 log.u_bbr.flex7 = bbr->rc_lt_is_sampling;
2693 log.u_bbr.pkts_out = tim;
2694 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw;
2695 if (bbr->rc_lt_use_bw == 0)
2696 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
2697 else
2698 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
2699 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2700 &bbr->rc_inp->inp_socket->so_rcv,
2701 &bbr->rc_inp->inp_socket->so_snd,
2702 BBR_LOG_BWSAMP, 0,
2703 0, &log, false, &bbr->rc_tv);
2704 }
2705 }
2706
2707 static inline void
bbr_log_progress_event(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t tick,int event,int line)2708 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line)
2709 {
2710 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2711 union tcp_log_stackspecific log;
2712
2713 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2714 log.u_bbr.flex1 = line;
2715 log.u_bbr.flex2 = tick;
2716 log.u_bbr.flex3 = tp->t_maxunacktime;
2717 log.u_bbr.flex4 = tp->t_acktime;
2718 log.u_bbr.flex8 = event;
2719 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2720 &bbr->rc_inp->inp_socket->so_rcv,
2721 &bbr->rc_inp->inp_socket->so_snd,
2722 BBR_LOG_PROGRESS, 0,
2723 0, &log, false, &bbr->rc_tv);
2724 }
2725 }
2726
2727 static void
bbr_type_log_hdwr_pacing(struct tcp_bbr * bbr,const struct ifnet * ifp,uint64_t rate,uint64_t hw_rate,int line,uint32_t cts,int error)2728 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp,
2729 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts,
2730 int error)
2731 {
2732 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2733 union tcp_log_stackspecific log;
2734
2735 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2736 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2737 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2738 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2739 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2740 log.u_bbr.bw_inuse = rate;
2741 log.u_bbr.flex5 = line;
2742 log.u_bbr.flex6 = error;
2743 log.u_bbr.flex8 = bbr->skip_gain;
2744 log.u_bbr.flex8 <<= 1;
2745 log.u_bbr.flex8 |= bbr->gain_is_limited;
2746 log.u_bbr.flex8 <<= 1;
2747 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing;
2748 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
2749 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2750 &bbr->rc_inp->inp_socket->so_rcv,
2751 &bbr->rc_inp->inp_socket->so_snd,
2752 BBR_LOG_HDWR_PACE, 0,
2753 0, &log, false, &bbr->rc_tv);
2754 }
2755 }
2756
2757 static void
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)2758 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)
2759 {
2760 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2761 union tcp_log_stackspecific log;
2762
2763 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2764 log.u_bbr.flex1 = slot;
2765 log.u_bbr.flex2 = del_by;
2766 log.u_bbr.flex3 = prev_delay;
2767 log.u_bbr.flex4 = line;
2768 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val;
2769 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay;
2770 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags);
2771 log.u_bbr.flex8 = bbr->rc_in_persist;
2772 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2773 &bbr->rc_inp->inp_socket->so_rcv,
2774 &bbr->rc_inp->inp_socket->so_snd,
2775 BBR_LOG_BBRSND, 0,
2776 len, &log, false, &bbr->rc_tv);
2777 }
2778 }
2779
2780 static void
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)2781 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)
2782 {
2783 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2784 union tcp_log_stackspecific log;
2785
2786 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2787 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered;
2788 log.u_bbr.flex2 = 0;
2789 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt;
2790 log.u_bbr.flex4 = end;
2791 log.u_bbr.flex5 = seq;
2792 log.u_bbr.flex6 = t;
2793 log.u_bbr.flex7 = match;
2794 log.u_bbr.flex8 = flags;
2795 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2796 &bbr->rc_inp->inp_socket->so_rcv,
2797 &bbr->rc_inp->inp_socket->so_snd,
2798 BBR_LOG_BBRRTT, 0,
2799 0, &log, false, &bbr->rc_tv);
2800 }
2801 }
2802
2803 static void
bbr_log_exit_gain(struct tcp_bbr * bbr,uint32_t cts,int32_t entry_method)2804 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method)
2805 {
2806 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2807 union tcp_log_stackspecific log;
2808
2809 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2810 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2811 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
2812 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch;
2813 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2814 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs;
2815 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight;
2816 log.u_bbr.flex7 = 0;
2817 log.u_bbr.flex8 = entry_method;
2818 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2819 &bbr->rc_inp->inp_socket->so_rcv,
2820 &bbr->rc_inp->inp_socket->so_snd,
2821 BBR_LOG_EXIT_GAIN, 0,
2822 0, &log, false, &bbr->rc_tv);
2823 }
2824 }
2825
2826 static void
bbr_log_settings_change(struct tcp_bbr * bbr,int settings_desired)2827 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired)
2828 {
2829 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2830 union tcp_log_stackspecific log;
2831
2832 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2833 /* R-HU */
2834 log.u_bbr.flex1 = 0;
2835 log.u_bbr.flex2 = 0;
2836 log.u_bbr.flex3 = 0;
2837 log.u_bbr.flex4 = 0;
2838 log.u_bbr.flex7 = 0;
2839 log.u_bbr.flex8 = settings_desired;
2840
2841 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2842 &bbr->rc_inp->inp_socket->so_rcv,
2843 &bbr->rc_inp->inp_socket->so_snd,
2844 BBR_LOG_SETTINGS_CHG, 0,
2845 0, &log, false, &bbr->rc_tv);
2846 }
2847 }
2848
2849 /*
2850 * Returns the bw from the our filter.
2851 */
2852 static inline uint64_t
bbr_get_full_bw(struct tcp_bbr * bbr)2853 bbr_get_full_bw(struct tcp_bbr *bbr)
2854 {
2855 uint64_t bw;
2856
2857 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
2858
2859 return (bw);
2860 }
2861
2862 static inline void
bbr_set_pktepoch(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2863 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2864 {
2865 uint64_t calclr;
2866 uint32_t lost, del;
2867
2868 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch)
2869 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch;
2870 else
2871 lost = 0;
2872 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del;
2873 if (lost == 0) {
2874 calclr = 0;
2875 } else if (del) {
2876 calclr = lost;
2877 calclr *= (uint64_t)1000;
2878 calclr /= (uint64_t)del;
2879 } else {
2880 /* Nothing delivered? 100.0% loss */
2881 calclr = 1000;
2882 }
2883 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr;
2884 if (IN_RECOVERY(bbr->rc_tp->t_flags))
2885 bbr->r_ctl.recovery_lr += (uint32_t)calclr;
2886 bbr->r_ctl.rc_pkt_epoch++;
2887 if (bbr->rc_no_pacing &&
2888 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) {
2889 bbr->rc_no_pacing = 0;
2890 tcp_bbr_tso_size_check(bbr, cts);
2891 }
2892 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time);
2893 bbr->r_ctl.rc_pkt_epoch_time = cts;
2894 /* What was our loss rate */
2895 bbr_log_pkt_epoch(bbr, cts, line, lost, del);
2896 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered;
2897 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost;
2898 }
2899
2900 static inline void
bbr_set_epoch(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2901 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2902 {
2903 uint32_t epoch_time;
2904
2905 /* Tick the RTT clock */
2906 bbr->r_ctl.rc_rtt_epoch++;
2907 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start;
2908 bbr_log_time_epoch(bbr, cts, line, epoch_time);
2909 bbr->r_ctl.rc_rcv_epoch_start = cts;
2910 }
2911
2912 static inline void
bbr_isit_a_pkt_epoch(struct tcp_bbr * bbr,uint32_t cts,struct bbr_sendmap * rsm,int32_t line,int32_t cum_acked)2913 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked)
2914 {
2915 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) {
2916 bbr->rc_is_pkt_epoch_now = 1;
2917 }
2918 }
2919
2920 /*
2921 * Returns the bw from either the b/w filter
2922 * or from the lt_bw (if the connection is being
2923 * policed).
2924 */
2925 static inline uint64_t
__bbr_get_bw(struct tcp_bbr * bbr)2926 __bbr_get_bw(struct tcp_bbr *bbr)
2927 {
2928 uint64_t bw, min_bw;
2929 uint64_t rtt;
2930 int gm_measure_cnt = 1;
2931
2932 /*
2933 * For startup we make, like google, a
2934 * minimum b/w. This is generated from the
2935 * IW and the rttProp. We do fall back to srtt
2936 * if for some reason (initial handshake) we don't
2937 * have a rttProp. We, in the worst case, fall back
2938 * to the configured min_bw (rc_initial_hptsi_bw).
2939 */
2940 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
2941 /* Attempt first to use rttProp */
2942 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2943 if (rtt && (rtt < 0xffffffff)) {
2944 measure:
2945 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2946 ((uint64_t)1000000);
2947 min_bw /= rtt;
2948 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2949 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2950 }
2951
2952 } else if (bbr->rc_tp->t_srtt != 0) {
2953 /* No rttProp, use srtt? */
2954 rtt = bbr_get_rtt(bbr, BBR_SRTT);
2955 goto measure;
2956 } else {
2957 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2958 }
2959 } else
2960 min_bw = 0;
2961
2962 if ((bbr->rc_past_init_win == 0) &&
2963 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp)))
2964 bbr->rc_past_init_win = 1;
2965 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1))
2966 gm_measure_cnt = 0;
2967 if (gm_measure_cnt &&
2968 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) ||
2969 (bbr->rc_past_init_win == 0))) {
2970 /* For google we use our guess rate until we get 1 measurement */
2971
2972 use_initial_window:
2973 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2974 if (rtt && (rtt < 0xffffffff)) {
2975 /*
2976 * We have an RTT measurment. Use that in
2977 * combination with our initial window to calculate
2978 * a b/w.
2979 */
2980 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2981 ((uint64_t)1000000);
2982 bw /= rtt;
2983 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2984 bw = bbr->r_ctl.rc_initial_hptsi_bw;
2985 }
2986 } else {
2987 /* Drop back to the 40 and punt to a default */
2988 bw = bbr->r_ctl.rc_initial_hptsi_bw;
2989 }
2990 if (bw < 1)
2991 /* Probably should panic */
2992 bw = 1;
2993 if (bw > min_bw)
2994 return (bw);
2995 else
2996 return (min_bw);
2997 }
2998 if (bbr->rc_lt_use_bw)
2999 bw = bbr->r_ctl.rc_lt_bw;
3000 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0))
3001 bw = bbr->r_ctl.red_bw;
3002 else
3003 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3004 if (bbr->rc_tp->t_peakrate_thr && (bbr->rc_use_google == 0)) {
3005 /*
3006 * Enforce user set rate limit, keep in mind that
3007 * t_peakrate_thr is in B/s already
3008 */
3009 bw = uqmin((uint64_t)bbr->rc_tp->t_peakrate_thr, bw);
3010 }
3011 if (bw == 0) {
3012 /* We should not be at 0, go to the initial window then */
3013 goto use_initial_window;
3014 }
3015 if (bw < 1)
3016 /* Probably should panic */
3017 bw = 1;
3018 if (bw < min_bw)
3019 bw = min_bw;
3020 return (bw);
3021 }
3022
3023 static inline uint64_t
bbr_get_bw(struct tcp_bbr * bbr)3024 bbr_get_bw(struct tcp_bbr *bbr)
3025 {
3026 uint64_t bw;
3027
3028 bw = __bbr_get_bw(bbr);
3029 return (bw);
3030 }
3031
3032 static inline void
bbr_reset_lt_bw_interval(struct tcp_bbr * bbr,uint32_t cts)3033 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts)
3034 {
3035 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch;
3036 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time;
3037 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3038 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3039 }
3040
3041 static inline void
bbr_reset_lt_bw_sampling(struct tcp_bbr * bbr,uint32_t cts)3042 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts)
3043 {
3044 bbr->rc_lt_is_sampling = 0;
3045 bbr->rc_lt_use_bw = 0;
3046 bbr->r_ctl.rc_lt_bw = 0;
3047 bbr_reset_lt_bw_interval(bbr, cts);
3048 }
3049
3050 static inline void
bbr_lt_bw_samp_done(struct tcp_bbr * bbr,uint64_t bw,uint32_t cts,uint32_t timin)3051 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin)
3052 {
3053 uint64_t diff;
3054
3055 /* Do we have a previous sample? */
3056 if (bbr->r_ctl.rc_lt_bw) {
3057 /* Get the diff in bytes per second */
3058 if (bbr->r_ctl.rc_lt_bw > bw)
3059 diff = bbr->r_ctl.rc_lt_bw - bw;
3060 else
3061 diff = bw - bbr->r_ctl.rc_lt_bw;
3062 if ((diff <= bbr_lt_bw_diff) ||
3063 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) {
3064 /* Consider us policed */
3065 uint32_t saved_bw;
3066
3067 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw;
3068 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */
3069 bbr->rc_lt_use_bw = 1;
3070 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
3071 /*
3072 * Use pkt based epoch for measuring length of
3073 * policer up
3074 */
3075 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch;
3076 /*
3077 * reason 4 is we need to start consider being
3078 * policed
3079 */
3080 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin);
3081 return;
3082 }
3083 }
3084 bbr->r_ctl.rc_lt_bw = bw;
3085 bbr_reset_lt_bw_interval(bbr, cts);
3086 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin);
3087 }
3088
3089 static void
bbr_randomize_extra_state_time(struct tcp_bbr * bbr)3090 bbr_randomize_extra_state_time(struct tcp_bbr *bbr)
3091 {
3092 uint32_t ran, deduct;
3093
3094 ran = arc4random_uniform(bbr_rand_ot);
3095 if (ran) {
3096 deduct = bbr->r_ctl.rc_level_state_extra / ran;
3097 bbr->r_ctl.rc_level_state_extra -= deduct;
3098 }
3099 }
3100 /*
3101 * Return randomly the starting state
3102 * to use in probebw.
3103 */
3104 static uint8_t
bbr_pick_probebw_substate(struct tcp_bbr * bbr,uint32_t cts)3105 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts)
3106 {
3107 uint32_t ran;
3108 uint8_t ret_val;
3109
3110 /* Initialize the offset to 0 */
3111 bbr->r_ctl.rc_exta_time_gd = 0;
3112 bbr->rc_hit_state_1 = 0;
3113 bbr->r_ctl.rc_level_state_extra = 0;
3114 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1));
3115 /*
3116 * The math works funny here :) the return value is used to set the
3117 * substate and then the state change is called which increments by
3118 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when
3119 * we fully enter the state. Note that the (8 - 1 - ran) assures that
3120 * we return 1 - 7, so we dont return 0 and end up starting in
3121 * state 1 (DRAIN).
3122 */
3123 ret_val = BBR_SUBSTATE_COUNT - 1 - ran;
3124 /* Set an epoch */
3125 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP))
3126 bbr_set_epoch(bbr, cts, __LINE__);
3127
3128 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
3129 return (ret_val);
3130 }
3131
3132 static void
bbr_lt_bw_sampling(struct tcp_bbr * bbr,uint32_t cts,int32_t loss_detected)3133 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected)
3134 {
3135 uint32_t diff, d_time;
3136 uint64_t del_time, bw, lost, delivered;
3137
3138 if (bbr->r_use_policer == 0)
3139 return;
3140 if (bbr->rc_lt_use_bw) {
3141 /* We are using lt bw do we stop yet? */
3142 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
3143 if (diff > bbr_lt_bw_max_rtts) {
3144 /* Reset it all */
3145 reset_all:
3146 bbr_reset_lt_bw_sampling(bbr, cts);
3147 if (bbr->rc_filled_pipe) {
3148 bbr_set_epoch(bbr, cts, __LINE__);
3149 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
3150 bbr_substate_change(bbr, cts, __LINE__, 0);
3151 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
3152 bbr_log_type_statechange(bbr, cts, __LINE__);
3153 } else {
3154 /*
3155 * This should not happen really
3156 * unless we remove the startup/drain
3157 * restrictions above.
3158 */
3159 bbr->rc_bbr_state = BBR_STATE_STARTUP;
3160 bbr_set_epoch(bbr, cts, __LINE__);
3161 bbr->r_ctl.rc_bbr_state_time = cts;
3162 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
3163 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
3164 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
3165 bbr_set_state_target(bbr, __LINE__);
3166 bbr_log_type_statechange(bbr, cts, __LINE__);
3167 }
3168 /* reason 0 is to stop using lt-bw */
3169 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0);
3170 return;
3171 }
3172 if (bbr_lt_intvl_fp == 0) {
3173 /* Not doing false-postive detection */
3174 return;
3175 }
3176 /* False positive detection */
3177 if (diff == bbr_lt_intvl_fp) {
3178 /* At bbr_lt_intvl_fp we record the lost */
3179 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3180 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3181 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) {
3182 /* Now is our loss rate still high? */
3183 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3184 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3185 if ((delivered == 0) ||
3186 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) {
3187 /* No still below our threshold */
3188 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0);
3189 } else {
3190 /* Yikes its still high, it must be a false positive */
3191 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0);
3192 goto reset_all;
3193 }
3194 }
3195 return;
3196 }
3197 /*
3198 * Wait for the first loss before sampling, to let the policer
3199 * exhaust its tokens and estimate the steady-state rate allowed by
3200 * the policer. Starting samples earlier includes bursts that
3201 * over-estimate the bw.
3202 */
3203 if (bbr->rc_lt_is_sampling == 0) {
3204 /* reason 1 is to begin doing the sampling */
3205 if (loss_detected == 0)
3206 return;
3207 bbr_reset_lt_bw_interval(bbr, cts);
3208 bbr->rc_lt_is_sampling = 1;
3209 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0);
3210 return;
3211 }
3212 /* Now how long were we delivering long term last> */
3213 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time))
3214 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time;
3215 else
3216 d_time = 0;
3217
3218 /* To avoid underestimates, reset sampling if we run out of data. */
3219 if (bbr->r_ctl.r_app_limited_until) {
3220 /* Can not measure in app-limited state */
3221 bbr_reset_lt_bw_sampling(bbr, cts);
3222 /* reason 2 is to reset sampling due to app limits */
3223 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time);
3224 return;
3225 }
3226 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
3227 if (diff < bbr_lt_intvl_min_rtts) {
3228 /*
3229 * need more samples (we don't
3230 * start on a round like linux so
3231 * we need 1 more).
3232 */
3233 /* 6 is not_enough time or no-loss */
3234 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3235 return;
3236 }
3237 if (diff > (4 * bbr_lt_intvl_min_rtts)) {
3238 /*
3239 * For now if we wait too long, reset all sampling. We need
3240 * to do some research here, its possible that we should
3241 * base this on how much loss as occurred.. something like
3242 * if its under 10% (or some thresh) reset all otherwise
3243 * don't. Thats for phase II I guess.
3244 */
3245 bbr_reset_lt_bw_sampling(bbr, cts);
3246 /* reason 3 is to reset sampling due too long of sampling */
3247 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3248 return;
3249 }
3250 /*
3251 * End sampling interval when a packet is lost, so we estimate the
3252 * policer tokens were exhausted. Stopping the sampling before the
3253 * tokens are exhausted under-estimates the policed rate.
3254 */
3255 if (loss_detected == 0) {
3256 /* 6 is not_enough time or no-loss */
3257 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3258 return;
3259 }
3260 /* Calculate packets lost and delivered in sampling interval. */
3261 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3262 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3263 if ((delivered == 0) ||
3264 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) {
3265 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time);
3266 return;
3267 }
3268 if (d_time < 1000) {
3269 /* Not enough time. wait */
3270 /* 6 is not_enough time or no-loss */
3271 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3272 return;
3273 }
3274 if (d_time >= (0xffffffff / USECS_IN_MSEC)) {
3275 /* Too long */
3276 bbr_reset_lt_bw_sampling(bbr, cts);
3277 /* reason 3 is to reset sampling due too long of sampling */
3278 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3279 return;
3280 }
3281 del_time = d_time;
3282 bw = delivered;
3283 bw *= (uint64_t)USECS_IN_SECOND;
3284 bw /= del_time;
3285 bbr_lt_bw_samp_done(bbr, bw, cts, d_time);
3286 }
3287
3288 /*
3289 * Allocate a sendmap from our zone.
3290 */
3291 static struct bbr_sendmap *
bbr_alloc(struct tcp_bbr * bbr)3292 bbr_alloc(struct tcp_bbr *bbr)
3293 {
3294 struct bbr_sendmap *rsm;
3295
3296 BBR_STAT_INC(bbr_to_alloc);
3297 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO));
3298 if (rsm) {
3299 bbr->r_ctl.rc_num_maps_alloced++;
3300 return (rsm);
3301 }
3302 if (bbr->r_ctl.rc_free_cnt) {
3303 BBR_STAT_INC(bbr_to_alloc_emerg);
3304 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
3305 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
3306 bbr->r_ctl.rc_free_cnt--;
3307 return (rsm);
3308 }
3309 BBR_STAT_INC(bbr_to_alloc_failed);
3310 return (NULL);
3311 }
3312
3313 static struct bbr_sendmap *
bbr_alloc_full_limit(struct tcp_bbr * bbr)3314 bbr_alloc_full_limit(struct tcp_bbr *bbr)
3315 {
3316 if ((V_tcp_map_entries_limit > 0) &&
3317 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3318 BBR_STAT_INC(bbr_alloc_limited);
3319 if (!bbr->alloc_limit_reported) {
3320 bbr->alloc_limit_reported = 1;
3321 BBR_STAT_INC(bbr_alloc_limited_conns);
3322 }
3323 return (NULL);
3324 }
3325 return (bbr_alloc(bbr));
3326 }
3327
3328 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3329 static struct bbr_sendmap *
bbr_alloc_limit(struct tcp_bbr * bbr,uint8_t limit_type)3330 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type)
3331 {
3332 struct bbr_sendmap *rsm;
3333
3334 if (limit_type) {
3335 /* currently there is only one limit type */
3336 if (V_tcp_map_split_limit > 0 &&
3337 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3338 BBR_STAT_INC(bbr_split_limited);
3339 if (!bbr->alloc_limit_reported) {
3340 bbr->alloc_limit_reported = 1;
3341 BBR_STAT_INC(bbr_alloc_limited_conns);
3342 }
3343 return (NULL);
3344 }
3345 }
3346
3347 /* allocate and mark in the limit type, if set */
3348 rsm = bbr_alloc(bbr);
3349 if (rsm != NULL && limit_type) {
3350 rsm->r_limit_type = limit_type;
3351 bbr->r_ctl.rc_num_split_allocs++;
3352 }
3353 return (rsm);
3354 }
3355
3356 static void
bbr_free(struct tcp_bbr * bbr,struct bbr_sendmap * rsm)3357 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
3358 {
3359 if (rsm->r_limit_type) {
3360 /* currently there is only one limit type */
3361 bbr->r_ctl.rc_num_split_allocs--;
3362 }
3363 if (rsm->r_is_smallmap)
3364 bbr->r_ctl.rc_num_small_maps_alloced--;
3365 if (bbr->r_ctl.rc_tlp_send == rsm)
3366 bbr->r_ctl.rc_tlp_send = NULL;
3367 if (bbr->r_ctl.rc_resend == rsm) {
3368 bbr->r_ctl.rc_resend = NULL;
3369 }
3370 if (bbr->r_ctl.rc_next == rsm)
3371 bbr->r_ctl.rc_next = NULL;
3372 if (bbr->r_ctl.rc_sacklast == rsm)
3373 bbr->r_ctl.rc_sacklast = NULL;
3374 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
3375 memset(rsm, 0, sizeof(struct bbr_sendmap));
3376 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
3377 rsm->r_limit_type = 0;
3378 bbr->r_ctl.rc_free_cnt++;
3379 return;
3380 }
3381 bbr->r_ctl.rc_num_maps_alloced--;
3382 uma_zfree(bbr_zone, rsm);
3383 }
3384
3385 /*
3386 * Returns the BDP.
3387 */
3388 static uint64_t
bbr_get_bw_delay_prod(uint64_t rtt,uint64_t bw)3389 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) {
3390 /*
3391 * Calculate the bytes in flight needed given the bw (in bytes per
3392 * second) and the specifyed rtt in useconds. We need to put out the
3393 * returned value per RTT to match that rate. Gain will normaly
3394 * raise it up from there.
3395 *
3396 * This should not overflow as long as the bandwidth is below 1
3397 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller
3398 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30).
3399 */
3400 uint64_t usec_per_sec;
3401
3402 usec_per_sec = USECS_IN_SECOND;
3403 return ((rtt * bw) / usec_per_sec);
3404 }
3405
3406 /*
3407 * Return the initial cwnd.
3408 */
3409 static uint32_t
bbr_initial_cwnd(struct tcp_bbr * bbr,struct tcpcb * tp)3410 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp)
3411 {
3412 uint32_t i_cwnd;
3413
3414 if (bbr->rc_init_win) {
3415 i_cwnd = bbr->rc_init_win * tp->t_maxseg;
3416 } else if (V_tcp_initcwnd_segments)
3417 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg),
3418 max(2 * tp->t_maxseg, 14600));
3419 else if (V_tcp_do_rfc3390)
3420 i_cwnd = min(4 * tp->t_maxseg,
3421 max(2 * tp->t_maxseg, 4380));
3422 else {
3423 /* Per RFC5681 Section 3.1 */
3424 if (tp->t_maxseg > 2190)
3425 i_cwnd = 2 * tp->t_maxseg;
3426 else if (tp->t_maxseg > 1095)
3427 i_cwnd = 3 * tp->t_maxseg;
3428 else
3429 i_cwnd = 4 * tp->t_maxseg;
3430 }
3431 return (i_cwnd);
3432 }
3433
3434 /*
3435 * Given a specified gain, return the target
3436 * cwnd based on that gain.
3437 */
3438 static uint32_t
bbr_get_raw_target_cwnd(struct tcp_bbr * bbr,uint32_t gain,uint64_t bw)3439 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw)
3440 {
3441 uint64_t bdp, rtt;
3442 uint32_t cwnd;
3443
3444 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) ||
3445 (bbr_get_full_bw(bbr) == 0)) {
3446 /* No measurements yet */
3447 return (bbr_initial_cwnd(bbr, bbr->rc_tp));
3448 }
3449 /*
3450 * Get bytes per RTT needed (rttProp is normally in
3451 * bbr_cwndtarget_rtt_touse)
3452 */
3453 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse);
3454 /* Get the bdp from the two values */
3455 bdp = bbr_get_bw_delay_prod(rtt, bw);
3456 /* Now apply the gain */
3457 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT));
3458
3459 return (cwnd);
3460 }
3461
3462 static uint32_t
bbr_get_target_cwnd(struct tcp_bbr * bbr,uint64_t bw,uint32_t gain)3463 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain)
3464 {
3465 uint32_t cwnd, mss;
3466
3467 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
3468 /* Get the base cwnd with gain rounded to a mss */
3469 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss);
3470 /*
3471 * Add in N (2 default since we do not have a
3472 * fq layer to trap packets in) quanta's per the I-D
3473 * section 4.2.3.2 quanta adjust.
3474 */
3475 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs);
3476 if (bbr->rc_use_google) {
3477 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3478 (bbr_state_val(bbr) == BBR_SUB_GAIN)) {
3479 /*
3480 * The linux implementation adds
3481 * an extra 2 x mss in gain cycle which
3482 * is documented no-where except in the code.
3483 * so we add more for Neal undocumented feature
3484 */
3485 cwnd += 2 * mss;
3486 }
3487 if ((cwnd / mss) & 0x1) {
3488 /* Round up for odd num mss */
3489 cwnd += mss;
3490 }
3491 }
3492 /* Are we below the min cwnd? */
3493 if (cwnd < get_min_cwnd(bbr))
3494 return (get_min_cwnd(bbr));
3495 return (cwnd);
3496 }
3497
3498 static uint16_t
bbr_gain_adjust(struct tcp_bbr * bbr,uint16_t gain)3499 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain)
3500 {
3501 if (gain < 1)
3502 gain = 1;
3503 return (gain);
3504 }
3505
3506 static uint32_t
bbr_get_header_oh(struct tcp_bbr * bbr)3507 bbr_get_header_oh(struct tcp_bbr *bbr)
3508 {
3509 int seg_oh;
3510
3511 seg_oh = 0;
3512 if (bbr->r_ctl.rc_inc_tcp_oh) {
3513 /* Do we include TCP overhead? */
3514 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr));
3515 }
3516 if (bbr->r_ctl.rc_inc_ip_oh) {
3517 /* Do we include IP overhead? */
3518 #ifdef INET6
3519 if (bbr->r_is_v6)
3520 seg_oh += sizeof(struct ip6_hdr);
3521 else
3522 #endif
3523 #ifdef INET
3524 seg_oh += sizeof(struct ip);
3525 #endif
3526 }
3527 if (bbr->r_ctl.rc_inc_enet_oh) {
3528 /* Do we include the ethernet overhead? */
3529 seg_oh += sizeof(struct ether_header);
3530 }
3531 return(seg_oh);
3532 }
3533
3534 static uint32_t
bbr_get_pacing_length(struct tcp_bbr * bbr,uint16_t gain,uint32_t useconds_time,uint64_t bw)3535 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw)
3536 {
3537 uint64_t divor, res, tim;
3538
3539 if (useconds_time == 0)
3540 return (0);
3541 gain = bbr_gain_adjust(bbr, gain);
3542 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT;
3543 tim = useconds_time;
3544 res = (tim * bw * gain) / divor;
3545 if (res == 0)
3546 res = 1;
3547 return ((uint32_t)res);
3548 }
3549
3550 /*
3551 * Given a gain and a length return the delay in useconds that
3552 * should be used to evenly space out packets
3553 * on the connection (based on the gain factor).
3554 */
3555 static uint32_t
bbr_get_pacing_delay(struct tcp_bbr * bbr,uint16_t gain,int32_t len,uint32_t cts,int nolog)3556 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog)
3557 {
3558 uint64_t bw, lentim, res;
3559 uint32_t usecs, srtt, over = 0;
3560 uint32_t seg_oh, num_segs, maxseg;
3561
3562 if (len == 0)
3563 return (0);
3564
3565 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
3566 num_segs = (len + maxseg - 1) / maxseg;
3567 if (bbr->rc_use_google == 0) {
3568 seg_oh = bbr_get_header_oh(bbr);
3569 len += (num_segs * seg_oh);
3570 }
3571 gain = bbr_gain_adjust(bbr, gain);
3572 bw = bbr_get_bw(bbr);
3573 if (bbr->rc_use_google) {
3574 uint64_t cbw;
3575
3576 /*
3577 * Reduce the b/w by the google discount
3578 * factor 10 = 1%.
3579 */
3580 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount);
3581 cbw /= (uint64_t)1000;
3582 /* We don't apply a discount if it results in 0 */
3583 if (cbw > 0)
3584 bw = cbw;
3585 }
3586 lentim = ((uint64_t)len *
3587 (uint64_t)USECS_IN_SECOND *
3588 (uint64_t)BBR_UNIT);
3589 res = lentim / ((uint64_t)gain * bw);
3590 if (res == 0)
3591 res = 1;
3592 usecs = (uint32_t)res;
3593 srtt = bbr_get_rtt(bbr, BBR_SRTT);
3594 if (bbr_hptsi_max_mul && bbr_hptsi_max_div &&
3595 (bbr->rc_use_google == 0) &&
3596 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) {
3597 /*
3598 * We cannot let the delay be more than 1/2 the srtt time.
3599 * Otherwise we cannot pace out or send properly.
3600 */
3601 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div;
3602 BBR_STAT_INC(bbr_hpts_min_time);
3603 }
3604 if (!nolog)
3605 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1);
3606 return (usecs);
3607 }
3608
3609 static void
bbr_ack_received(struct tcpcb * tp,struct tcp_bbr * bbr,struct tcphdr * th,uint32_t bytes_this_ack,uint32_t sack_changed,uint32_t prev_acked,int32_t line,uint32_t losses)3610 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack,
3611 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses)
3612 {
3613 INP_WLOCK_ASSERT(tp->t_inpcb);
3614 uint64_t bw;
3615 uint32_t cwnd, target_cwnd, saved_bytes, maxseg;
3616 int32_t meth;
3617
3618 #ifdef STATS
3619 if ((tp->t_flags & TF_GPUTINPROG) &&
3620 SEQ_GEQ(th->th_ack, tp->gput_ack)) {
3621 /*
3622 * Strech acks and compressed acks will cause this to
3623 * oscillate but we are doing it the same way as the main
3624 * stack so it will be compariable (though possibly not
3625 * ideal).
3626 */
3627 int32_t cgput;
3628 int64_t gput, time_stamp;
3629
3630 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8;
3631 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000));
3632 cgput = gput / time_stamp;
3633 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
3634 cgput);
3635 if (tp->t_stats_gput_prev > 0)
3636 stats_voi_update_abs_s32(tp->t_stats,
3637 VOI_TCP_GPUT_ND,
3638 ((gput - tp->t_stats_gput_prev) * 100) /
3639 tp->t_stats_gput_prev);
3640 tp->t_flags &= ~TF_GPUTINPROG;
3641 tp->t_stats_gput_prev = cgput;
3642 }
3643 #endif
3644 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3645 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
3646 /* We don't change anything in probe-rtt */
3647 return;
3648 }
3649 maxseg = tp->t_maxseg - bbr->rc_last_options;
3650 saved_bytes = bytes_this_ack;
3651 bytes_this_ack += sack_changed;
3652 if (bytes_this_ack > prev_acked) {
3653 bytes_this_ack -= prev_acked;
3654 /*
3655 * A byte ack'd gives us a full mss
3656 * to be like linux i.e. they count packets.
3657 */
3658 if ((bytes_this_ack < maxseg) && bbr->rc_use_google)
3659 bytes_this_ack = maxseg;
3660 } else {
3661 /* Unlikely */
3662 bytes_this_ack = 0;
3663 }
3664 cwnd = tp->snd_cwnd;
3665 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3666 if (bw)
3667 target_cwnd = bbr_get_target_cwnd(bbr,
3668 bw,
3669 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain);
3670 else
3671 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp);
3672 if (IN_RECOVERY(tp->t_flags) &&
3673 (bbr->bbr_prev_in_rec == 0)) {
3674 /*
3675 * We are entering recovery and
3676 * thus packet conservation.
3677 */
3678 bbr->pkt_conservation = 1;
3679 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime;
3680 cwnd = ctf_flight_size(tp,
3681 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3682 bytes_this_ack;
3683 }
3684 if (IN_RECOVERY(tp->t_flags)) {
3685 uint32_t flight;
3686
3687 bbr->bbr_prev_in_rec = 1;
3688 if (cwnd > losses) {
3689 cwnd -= losses;
3690 if (cwnd < maxseg)
3691 cwnd = maxseg;
3692 } else
3693 cwnd = maxseg;
3694 flight = ctf_flight_size(tp,
3695 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3696 bbr_log_type_cwndupd(bbr, flight, 0,
3697 losses, 10, 0, 0, line);
3698 if (bbr->pkt_conservation) {
3699 uint32_t time_in;
3700
3701 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start))
3702 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start;
3703 else
3704 time_in = 0;
3705
3706 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
3707 /* Clear packet conservation after an rttProp */
3708 bbr->pkt_conservation = 0;
3709 } else {
3710 if ((flight + bytes_this_ack) > cwnd)
3711 cwnd = flight + bytes_this_ack;
3712 if (cwnd < get_min_cwnd(bbr))
3713 cwnd = get_min_cwnd(bbr);
3714 tp->snd_cwnd = cwnd;
3715 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed,
3716 prev_acked, 1, target_cwnd, th->th_ack, line);
3717 return;
3718 }
3719 }
3720 } else
3721 bbr->bbr_prev_in_rec = 0;
3722 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) {
3723 bbr->r_ctl.restrict_growth--;
3724 if (bytes_this_ack > maxseg)
3725 bytes_this_ack = maxseg;
3726 }
3727 if (bbr->rc_filled_pipe) {
3728 /*
3729 * Here we have exited startup and filled the pipe. We will
3730 * thus allow the cwnd to shrink to the target. We hit here
3731 * mostly.
3732 */
3733 uint32_t s_cwnd;
3734
3735 meth = 2;
3736 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd);
3737 if (s_cwnd > cwnd)
3738 cwnd = s_cwnd;
3739 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing)
3740 cwnd = s_cwnd;
3741 } else {
3742 /*
3743 * Here we are still in startup, we increase cwnd by what
3744 * has been acked.
3745 */
3746 if ((cwnd < target_cwnd) ||
3747 (bbr->rc_past_init_win == 0)) {
3748 meth = 3;
3749 cwnd += bytes_this_ack;
3750 } else {
3751 /*
3752 * Method 4 means we are at target so no gain in
3753 * startup and past the initial window.
3754 */
3755 meth = 4;
3756 }
3757 }
3758 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr));
3759 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line);
3760 }
3761
3762 static void
tcp_bbr_partialack(struct tcpcb * tp)3763 tcp_bbr_partialack(struct tcpcb *tp)
3764 {
3765 struct tcp_bbr *bbr;
3766
3767 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3768 INP_WLOCK_ASSERT(tp->t_inpcb);
3769 if (ctf_flight_size(tp,
3770 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
3771 tp->snd_cwnd) {
3772 bbr->r_wanted_output = 1;
3773 }
3774 }
3775
3776 static void
bbr_post_recovery(struct tcpcb * tp)3777 bbr_post_recovery(struct tcpcb *tp)
3778 {
3779 struct tcp_bbr *bbr;
3780 uint32_t flight;
3781
3782 INP_WLOCK_ASSERT(tp->t_inpcb);
3783 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3784 /*
3785 * Here we just exit recovery.
3786 */
3787 EXIT_RECOVERY(tp->t_flags);
3788 /* Lock in our b/w reduction for the specified number of pkt-epochs */
3789 bbr->r_recovery_bw = 0;
3790 tp->snd_recover = tp->snd_una;
3791 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3792 bbr->pkt_conservation = 0;
3793 if (bbr->rc_use_google == 0) {
3794 /*
3795 * For non-google mode lets
3796 * go ahead and make sure we clear
3797 * the recovery state so if we
3798 * bounce back in to recovery we
3799 * will do PC.
3800 */
3801 bbr->bbr_prev_in_rec = 0;
3802 }
3803 bbr_log_type_exit_rec(bbr);
3804 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
3805 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
3806 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__);
3807 } else {
3808 /* For probe-rtt case lets fix up its saved_cwnd */
3809 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) {
3810 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent;
3811 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__);
3812 }
3813 }
3814 flight = ctf_flight_size(tp,
3815 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3816 if ((bbr->rc_use_google == 0) &&
3817 bbr_do_red) {
3818 uint64_t val, lr2use;
3819 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd;
3820 uint32_t *cwnd_p;
3821
3822 if (bbr_get_rtt(bbr, BBR_SRTT)) {
3823 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000);
3824 val /= bbr_get_rtt(bbr, BBR_SRTT);
3825 ratio = (uint32_t)val;
3826 } else
3827 ratio = 1000;
3828
3829 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div,
3830 bbr->r_ctl.recovery_lr, 21,
3831 ratio,
3832 bbr->r_ctl.rc_red_cwnd_pe,
3833 __LINE__);
3834 if ((ratio < bbr_do_red) || (bbr_do_red == 0))
3835 goto done;
3836 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3837 bbr_prtt_slam_cwnd) ||
3838 (bbr_sub_drain_slam_cwnd &&
3839 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3840 bbr->rc_hit_state_1 &&
3841 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) ||
3842 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) &&
3843 bbr_slam_cwnd_in_main_drain)) {
3844 /*
3845 * Here we must poke at the saved cwnd
3846 * as well as the cwnd.
3847 */
3848 cwnd = bbr->r_ctl.rc_saved_cwnd;
3849 cwnd_p = &bbr->r_ctl.rc_saved_cwnd;
3850 } else {
3851 cwnd = tp->snd_cwnd;
3852 cwnd_p = &tp->snd_cwnd;
3853 }
3854 maxseg = tp->t_maxseg - bbr->rc_last_options;
3855 /* Add the overall lr with the recovery lr */
3856 if (bbr->r_ctl.rc_lost == 0)
3857 lr2use = 0;
3858 else if (bbr->r_ctl.rc_delivered == 0)
3859 lr2use = 1000;
3860 else {
3861 lr2use = bbr->r_ctl.rc_lost * 1000;
3862 lr2use /= bbr->r_ctl.rc_delivered;
3863 }
3864 lr2use += bbr->r_ctl.recovery_lr;
3865 acks_inflight = (flight / (maxseg * 2));
3866 if (bbr_red_scale) {
3867 lr2use *= bbr_get_rtt(bbr, BBR_SRTT);
3868 lr2use /= bbr_red_scale;
3869 if ((bbr_red_growth_restrict) &&
3870 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1))
3871 bbr->r_ctl.restrict_growth += acks_inflight;
3872 }
3873 if (lr2use) {
3874 val = (uint64_t)cwnd * lr2use;
3875 val /= 1000;
3876 if (cwnd > val)
3877 newcwnd = roundup((cwnd - val), maxseg);
3878 else
3879 newcwnd = maxseg;
3880 } else {
3881 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul;
3882 val /= (uint64_t)bbr_red_div;
3883 newcwnd = roundup((uint32_t)val, maxseg);
3884 }
3885 /* with standard delayed acks how many acks can I expect? */
3886 if (bbr_drop_limit == 0) {
3887 /*
3888 * Anticpate how much we will
3889 * raise the cwnd based on the acks.
3890 */
3891 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) {
3892 /* We do enforce the min (with the acks) */
3893 newcwnd = (get_min_cwnd(bbr) - acks_inflight);
3894 }
3895 } else {
3896 /*
3897 * A strict drop limit of N is is inplace
3898 */
3899 if (newcwnd < (bbr_drop_limit * maxseg)) {
3900 newcwnd = bbr_drop_limit * maxseg;
3901 }
3902 }
3903 /* For the next N acks do we restrict the growth */
3904 *cwnd_p = newcwnd;
3905 if (tp->snd_cwnd > newcwnd)
3906 tp->snd_cwnd = newcwnd;
3907 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22,
3908 (uint32_t)lr2use,
3909 bbr_get_rtt(bbr, BBR_SRTT), __LINE__);
3910 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch;
3911 }
3912 done:
3913 bbr->r_ctl.recovery_lr = 0;
3914 if (flight <= tp->snd_cwnd) {
3915 bbr->r_wanted_output = 1;
3916 }
3917 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3918 }
3919
3920 static void
bbr_setup_red_bw(struct tcp_bbr * bbr,uint32_t cts)3921 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts)
3922 {
3923 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3924 /* Limit the drop in b/w to 1/2 our current filter. */
3925 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate)
3926 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate;
3927 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2))
3928 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2;
3929 tcp_bbr_tso_size_check(bbr, cts);
3930 }
3931
3932 static void
bbr_cong_signal(struct tcpcb * tp,struct tcphdr * th,uint32_t type,struct bbr_sendmap * rsm)3933 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm)
3934 {
3935 struct tcp_bbr *bbr;
3936
3937 INP_WLOCK_ASSERT(tp->t_inpcb);
3938 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3939 switch (type) {
3940 case CC_NDUPACK:
3941 if (!IN_RECOVERY(tp->t_flags)) {
3942 tp->snd_recover = tp->snd_max;
3943 /* Start a new epoch */
3944 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
3945 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) {
3946 /*
3947 * Move forward the lt epoch
3948 * so it won't count the truncated
3949 * epoch.
3950 */
3951 bbr->r_ctl.rc_lt_epoch++;
3952 }
3953 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
3954 /*
3955 * Just like the policer detection code
3956 * if we are in startup we must push
3957 * forward the last startup epoch
3958 * to hide the truncated PE.
3959 */
3960 bbr->r_ctl.rc_bbr_last_startup_epoch++;
3961 }
3962 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd;
3963 ENTER_RECOVERY(tp->t_flags);
3964 bbr->rc_tlp_rtx_out = 0;
3965 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate;
3966 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3967 if (bbr->rc_inp->inp_in_hpts &&
3968 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) {
3969 /*
3970 * When we enter recovery, we need to restart
3971 * any timers. This may mean we gain an agg
3972 * early, which will be made up for at the last
3973 * rxt out.
3974 */
3975 bbr->rc_timer_first = 1;
3976 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
3977 }
3978 /*
3979 * Calculate a new cwnd based on to the current
3980 * delivery rate with no gain. We get the bdp
3981 * without gaining it up like we normally would and
3982 * we use the last cur_del_rate.
3983 */
3984 if ((bbr->rc_use_google == 0) &&
3985 (bbr->r_ctl.bbr_rttprobe_gain_val ||
3986 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) {
3987 tp->snd_cwnd = ctf_flight_size(tp,
3988 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3989 (tp->t_maxseg - bbr->rc_last_options);
3990 if (tp->snd_cwnd < get_min_cwnd(bbr)) {
3991 /* We always gate to min cwnd */
3992 tp->snd_cwnd = get_min_cwnd(bbr);
3993 }
3994 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__);
3995 }
3996 bbr_log_type_enter_rec(bbr, rsm->r_start);
3997 }
3998 break;
3999 case CC_RTO_ERR:
4000 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4001 /* RTO was unnecessary, so reset everything. */
4002 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime);
4003 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
4004 tp->snd_cwnd = tp->snd_cwnd_prev;
4005 tp->snd_ssthresh = tp->snd_ssthresh_prev;
4006 tp->snd_recover = tp->snd_recover_prev;
4007 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
4008 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__);
4009 }
4010 tp->t_badrxtwin = 0;
4011 break;
4012 }
4013 }
4014
4015 /*
4016 * Indicate whether this ack should be delayed. We can delay the ack if
4017 * following conditions are met:
4018 * - There is no delayed ack timer in progress.
4019 * - Our last ack wasn't a 0-sized window. We never want to delay
4020 * the ack that opens up a 0-sized window.
4021 * - LRO wasn't used for this segment. We make sure by checking that the
4022 * segment size is not larger than the MSS.
4023 * - Delayed acks are enabled or this is a half-synchronized T/TCP
4024 * connection.
4025 * - The data being acked is less than a full segment (a stretch ack
4026 * of more than a segment we should ack.
4027 * - nsegs is 1 (if its more than that we received more than 1 ack).
4028 */
4029 #define DELAY_ACK(tp, bbr, nsegs) \
4030 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4031 ((tp->t_flags & TF_DELACK) == 0) && \
4032 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \
4033 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4034
4035 /*
4036 * Return the lowest RSM in the map of
4037 * packets still in flight that is not acked.
4038 * This should normally find on the first one
4039 * since we remove packets from the send
4040 * map after they are marked ACKED.
4041 */
4042 static struct bbr_sendmap *
bbr_find_lowest_rsm(struct tcp_bbr * bbr)4043 bbr_find_lowest_rsm(struct tcp_bbr *bbr)
4044 {
4045 struct bbr_sendmap *rsm;
4046
4047 /*
4048 * Walk the time-order transmitted list looking for an rsm that is
4049 * not acked. This will be the one that was sent the longest time
4050 * ago that is still outstanding.
4051 */
4052 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) {
4053 if (rsm->r_flags & BBR_ACKED) {
4054 continue;
4055 }
4056 goto finish;
4057 }
4058 finish:
4059 return (rsm);
4060 }
4061
4062 static struct bbr_sendmap *
bbr_find_high_nonack(struct tcp_bbr * bbr,struct bbr_sendmap * rsm)4063 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
4064 {
4065 struct bbr_sendmap *prsm;
4066
4067 /*
4068 * Walk the sequence order list backward until we hit and arrive at
4069 * the highest seq not acked. In theory when this is called it
4070 * should be the last segment (which it was not).
4071 */
4072 prsm = rsm;
4073 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4074 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) {
4075 continue;
4076 }
4077 return (prsm);
4078 }
4079 return (NULL);
4080 }
4081
4082 /*
4083 * Returns to the caller the number of microseconds that
4084 * the packet can be outstanding before we think we
4085 * should have had an ack returned.
4086 */
4087 static uint32_t
bbr_calc_thresh_rack(struct tcp_bbr * bbr,uint32_t srtt,uint32_t cts,struct bbr_sendmap * rsm)4088 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm)
4089 {
4090 /*
4091 * lro is the flag we use to determine if we have seen reordering.
4092 * If it gets set we have seen reordering. The reorder logic either
4093 * works in one of two ways:
4094 *
4095 * If reorder-fade is configured, then we track the last time we saw
4096 * re-ordering occur. If we reach the point where enough time as
4097 * passed we no longer consider reordering has occuring.
4098 *
4099 * Or if reorder-face is 0, then once we see reordering we consider
4100 * the connection to alway be subject to reordering and just set lro
4101 * to 1.
4102 *
4103 * In the end if lro is non-zero we add the extra time for
4104 * reordering in.
4105 */
4106 int32_t lro;
4107 uint32_t thresh, t_rxtcur;
4108
4109 if (srtt == 0)
4110 srtt = 1;
4111 if (bbr->r_ctl.rc_reorder_ts) {
4112 if (bbr->r_ctl.rc_reorder_fade) {
4113 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) {
4114 lro = cts - bbr->r_ctl.rc_reorder_ts;
4115 if (lro == 0) {
4116 /*
4117 * No time as passed since the last
4118 * reorder, mark it as reordering.
4119 */
4120 lro = 1;
4121 }
4122 } else {
4123 /* Negative time? */
4124 lro = 0;
4125 }
4126 if (lro > bbr->r_ctl.rc_reorder_fade) {
4127 /* Turn off reordering seen too */
4128 bbr->r_ctl.rc_reorder_ts = 0;
4129 lro = 0;
4130 }
4131 } else {
4132 /* Reodering does not fade */
4133 lro = 1;
4134 }
4135 } else {
4136 lro = 0;
4137 }
4138 thresh = srtt + bbr->r_ctl.rc_pkt_delay;
4139 if (lro) {
4140 /* It must be set, if not you get 1/4 rtt */
4141 if (bbr->r_ctl.rc_reorder_shift)
4142 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift);
4143 else
4144 thresh += (srtt >> 2);
4145 } else {
4146 thresh += 1000;
4147 }
4148 /* We don't let the rack timeout be above a RTO */
4149 if ((bbr->rc_tp)->t_srtt == 0)
4150 t_rxtcur = BBR_INITIAL_RTO;
4151 else
4152 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
4153 if (thresh > t_rxtcur) {
4154 thresh = t_rxtcur;
4155 }
4156 /* And we don't want it above the RTO max either */
4157 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4158 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4159 }
4160 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK);
4161 return (thresh);
4162 }
4163
4164 /*
4165 * Return to the caller the amount of time in mico-seconds
4166 * that should be used for the TLP timer from the last
4167 * send time of this packet.
4168 */
4169 static uint32_t
bbr_calc_thresh_tlp(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t srtt,uint32_t cts)4170 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
4171 struct bbr_sendmap *rsm, uint32_t srtt,
4172 uint32_t cts)
4173 {
4174 uint32_t thresh, len, maxseg, t_rxtcur;
4175 struct bbr_sendmap *prsm;
4176
4177 if (srtt == 0)
4178 srtt = 1;
4179 if (bbr->rc_tlp_threshold)
4180 thresh = srtt + (srtt / bbr->rc_tlp_threshold);
4181 else
4182 thresh = (srtt * 2);
4183 maxseg = tp->t_maxseg - bbr->rc_last_options;
4184 /* Get the previous sent packet, if any */
4185 len = rsm->r_end - rsm->r_start;
4186
4187 /* 2.1 behavior */
4188 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext);
4189 if (prsm && (len <= maxseg)) {
4190 /*
4191 * Two packets outstanding, thresh should be (2*srtt) +
4192 * possible inter-packet delay (if any).
4193 */
4194 uint32_t inter_gap = 0;
4195 int idx, nidx;
4196
4197 idx = rsm->r_rtr_cnt - 1;
4198 nidx = prsm->r_rtr_cnt - 1;
4199 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
4200 /* Yes it was sent later (or at the same time) */
4201 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
4202 }
4203 thresh += inter_gap;
4204 } else if (len <= maxseg) {
4205 /*
4206 * Possibly compensate for delayed-ack.
4207 */
4208 uint32_t alt_thresh;
4209
4210 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time;
4211 if (alt_thresh > thresh)
4212 thresh = alt_thresh;
4213 }
4214 /* Not above the current RTO */
4215 if (tp->t_srtt == 0)
4216 t_rxtcur = BBR_INITIAL_RTO;
4217 else
4218 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur);
4219
4220 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP);
4221 /* Not above an RTO */
4222 if (thresh > t_rxtcur) {
4223 thresh = t_rxtcur;
4224 }
4225 /* Not above a RTO max */
4226 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4227 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4228 }
4229 /* And now apply the user TLP min */
4230 if (thresh < bbr_tlp_min) {
4231 thresh = bbr_tlp_min;
4232 }
4233 return (thresh);
4234 }
4235
4236 /*
4237 * Return one of three RTTs to use (in microseconds).
4238 */
4239 static __inline uint32_t
bbr_get_rtt(struct tcp_bbr * bbr,int32_t rtt_type)4240 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type)
4241 {
4242 uint32_t f_rtt;
4243 uint32_t srtt;
4244
4245 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
4246 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) {
4247 /* We have no rtt at all */
4248 if (bbr->rc_tp->t_srtt == 0)
4249 f_rtt = BBR_INITIAL_RTO;
4250 else
4251 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4252 /*
4253 * Since we don't know how good the rtt is apply a
4254 * delayed-ack min
4255 */
4256 if (f_rtt < bbr_delayed_ack_time) {
4257 f_rtt = bbr_delayed_ack_time;
4258 }
4259 }
4260 /* Take the filter version or last measured pkt-rtt */
4261 if (rtt_type == BBR_RTT_PROP) {
4262 srtt = f_rtt;
4263 } else if (rtt_type == BBR_RTT_PKTRTT) {
4264 if (bbr->r_ctl.rc_pkt_epoch_rtt) {
4265 srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
4266 } else {
4267 /* No pkt rtt yet */
4268 srtt = f_rtt;
4269 }
4270 } else if (rtt_type == BBR_RTT_RACK) {
4271 srtt = bbr->r_ctl.rc_last_rtt;
4272 /* We need to add in any internal delay for our timer */
4273 if (bbr->rc_ack_was_delayed)
4274 srtt += bbr->r_ctl.rc_ack_hdwr_delay;
4275 } else if (rtt_type == BBR_SRTT) {
4276 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4277 } else {
4278 /* TSNH */
4279 srtt = f_rtt;
4280 #ifdef BBR_INVARIANTS
4281 panic("Unknown rtt request type %d", rtt_type);
4282 #endif
4283 }
4284 return (srtt);
4285 }
4286
4287 static int
bbr_is_lost(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts)4288 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts)
4289 {
4290 uint32_t thresh;
4291
4292 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK),
4293 cts, rsm);
4294 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) {
4295 /* It is lost (past time) */
4296 return (1);
4297 }
4298 return (0);
4299 }
4300
4301 /*
4302 * Return a sendmap if we need to retransmit something.
4303 */
4304 static struct bbr_sendmap *
bbr_check_recovery_mode(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4305 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4306 {
4307 /*
4308 * Check to see that we don't need to fall into recovery. We will
4309 * need to do so if our oldest transmit is past the time we should
4310 * have had an ack.
4311 */
4312
4313 struct bbr_sendmap *rsm;
4314 int32_t idx;
4315
4316 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) {
4317 /* Nothing outstanding that we know of */
4318 return (NULL);
4319 }
4320 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
4321 if (rsm == NULL) {
4322 /* Nothing in the transmit map */
4323 return (NULL);
4324 }
4325 if (tp->t_flags & TF_SENTFIN) {
4326 /* Fin restricted, don't find anything once a fin is sent */
4327 return (NULL);
4328 }
4329 if (rsm->r_flags & BBR_ACKED) {
4330 /*
4331 * Ok the first one is acked (this really should not happen
4332 * since we remove the from the tmap once they are acked)
4333 */
4334 rsm = bbr_find_lowest_rsm(bbr);
4335 if (rsm == NULL)
4336 return (NULL);
4337 }
4338 idx = rsm->r_rtr_cnt - 1;
4339 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) {
4340 /* Send timestamp is the same or less? can't be ready */
4341 return (NULL);
4342 }
4343 /* Get our RTT time */
4344 if (bbr_is_lost(bbr, rsm, cts) &&
4345 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
4346 (rsm->r_flags & BBR_SACK_PASSED))) {
4347 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4348 rsm->r_flags |= BBR_MARKED_LOST;
4349 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4350 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4351 }
4352 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm);
4353 #ifdef BBR_INVARIANTS
4354 if ((rsm->r_end - rsm->r_start) == 0)
4355 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm);
4356 #endif
4357 return (rsm);
4358 }
4359 return (NULL);
4360 }
4361
4362 /*
4363 * RACK Timer, here we simply do logging and house keeping.
4364 * the normal bbr_output_wtime() function will call the
4365 * appropriate thing to check if we need to do a RACK retransmit.
4366 * We return 1, saying don't proceed with bbr_output_wtime only
4367 * when all timers have been stopped (destroyed PCB?).
4368 */
4369 static int
bbr_timeout_rack(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4370 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4371 {
4372 /*
4373 * This timer simply provides an internal trigger to send out data.
4374 * The check_recovery_mode call will see if there are needed
4375 * retransmissions, if so we will enter fast-recovery. The output
4376 * call may or may not do the same thing depending on sysctl
4377 * settings.
4378 */
4379 uint32_t lost;
4380
4381 if (bbr->rc_all_timers_stopped) {
4382 return (1);
4383 }
4384 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4385 /* Its not time yet */
4386 return (0);
4387 }
4388 BBR_STAT_INC(bbr_to_tot);
4389 lost = bbr->r_ctl.rc_lost;
4390 if (bbr->r_state && (bbr->r_state != tp->t_state))
4391 bbr_set_state(tp, bbr, 0);
4392 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK);
4393 if (bbr->r_ctl.rc_resend == NULL) {
4394 /* Lets do the check here */
4395 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
4396 }
4397 if (bbr_policer_call_from_rack_to)
4398 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4399 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
4400 return (0);
4401 }
4402
4403 static __inline void
bbr_clone_rsm(struct tcp_bbr * bbr,struct bbr_sendmap * nrsm,struct bbr_sendmap * rsm,uint32_t start)4404 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start)
4405 {
4406 int idx;
4407
4408 nrsm->r_start = start;
4409 nrsm->r_end = rsm->r_end;
4410 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
4411 nrsm->r_flags = rsm->r_flags;
4412 /* We don't transfer forward the SYN flag */
4413 nrsm->r_flags &= ~BBR_HAS_SYN;
4414 /* We move forward the FIN flag, not that this should happen */
4415 rsm->r_flags &= ~BBR_HAS_FIN;
4416 nrsm->r_dupack = rsm->r_dupack;
4417 nrsm->r_rtr_bytes = 0;
4418 nrsm->r_is_gain = rsm->r_is_gain;
4419 nrsm->r_is_drain = rsm->r_is_drain;
4420 nrsm->r_delivered = rsm->r_delivered;
4421 nrsm->r_ts_valid = rsm->r_ts_valid;
4422 nrsm->r_del_ack_ts = rsm->r_del_ack_ts;
4423 nrsm->r_del_time = rsm->r_del_time;
4424 nrsm->r_app_limited = rsm->r_app_limited;
4425 nrsm->r_first_sent_time = rsm->r_first_sent_time;
4426 nrsm->r_flight_at_send = rsm->r_flight_at_send;
4427 /* We split a piece the lower section looses any just_ret flag. */
4428 nrsm->r_bbr_state = rsm->r_bbr_state;
4429 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
4430 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
4431 }
4432 rsm->r_end = nrsm->r_start;
4433 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
4434 idx /= 8;
4435 /* Check if we got too small */
4436 if ((rsm->r_is_smallmap == 0) &&
4437 ((rsm->r_end - rsm->r_start) <= idx)) {
4438 bbr->r_ctl.rc_num_small_maps_alloced++;
4439 rsm->r_is_smallmap = 1;
4440 }
4441 /* Check the new one as well */
4442 if ((nrsm->r_end - nrsm->r_start) <= idx) {
4443 bbr->r_ctl.rc_num_small_maps_alloced++;
4444 nrsm->r_is_smallmap = 1;
4445 }
4446 }
4447
4448 static int
bbr_sack_mergable(struct bbr_sendmap * at,uint32_t start,uint32_t end)4449 bbr_sack_mergable(struct bbr_sendmap *at,
4450 uint32_t start, uint32_t end)
4451 {
4452 /*
4453 * Given a sack block defined by
4454 * start and end, and a current postion
4455 * at. Return 1 if either side of at
4456 * would show that the block is mergable
4457 * to that side. A block to be mergable
4458 * must have overlap with the start/end
4459 * and be in the SACK'd state.
4460 */
4461 struct bbr_sendmap *l_rsm;
4462 struct bbr_sendmap *r_rsm;
4463
4464 /* first get the either side blocks */
4465 l_rsm = TAILQ_PREV(at, bbr_head, r_next);
4466 r_rsm = TAILQ_NEXT(at, r_next);
4467 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) {
4468 /* Potentially mergeable */
4469 if ((l_rsm->r_end == start) ||
4470 (SEQ_LT(start, l_rsm->r_end) &&
4471 SEQ_GT(end, l_rsm->r_end))) {
4472 /*
4473 * map blk |------|
4474 * sack blk |------|
4475 * <or>
4476 * map blk |------|
4477 * sack blk |------|
4478 */
4479 return (1);
4480 }
4481 }
4482 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) {
4483 /* Potentially mergeable */
4484 if ((r_rsm->r_start == end) ||
4485 (SEQ_LT(start, r_rsm->r_start) &&
4486 SEQ_GT(end, r_rsm->r_start))) {
4487 /*
4488 * map blk |---------|
4489 * sack blk |----|
4490 * <or>
4491 * map blk |---------|
4492 * sack blk |-------|
4493 */
4494 return (1);
4495 }
4496 }
4497 return (0);
4498 }
4499
4500 static struct bbr_sendmap *
bbr_merge_rsm(struct tcp_bbr * bbr,struct bbr_sendmap * l_rsm,struct bbr_sendmap * r_rsm)4501 bbr_merge_rsm(struct tcp_bbr *bbr,
4502 struct bbr_sendmap *l_rsm,
4503 struct bbr_sendmap *r_rsm)
4504 {
4505 /*
4506 * We are merging two ack'd RSM's,
4507 * the l_rsm is on the left (lower seq
4508 * values) and the r_rsm is on the right
4509 * (higher seq value). The simplest way
4510 * to merge these is to move the right
4511 * one into the left. I don't think there
4512 * is any reason we need to try to find
4513 * the oldest (or last oldest retransmitted).
4514 */
4515 l_rsm->r_end = r_rsm->r_end;
4516 if (l_rsm->r_dupack < r_rsm->r_dupack)
4517 l_rsm->r_dupack = r_rsm->r_dupack;
4518 if (r_rsm->r_rtr_bytes)
4519 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
4520 if (r_rsm->r_in_tmap) {
4521 /* This really should not happen */
4522 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext);
4523 }
4524 if (r_rsm->r_app_limited)
4525 l_rsm->r_app_limited = r_rsm->r_app_limited;
4526 /* Now the flags */
4527 if (r_rsm->r_flags & BBR_HAS_FIN)
4528 l_rsm->r_flags |= BBR_HAS_FIN;
4529 if (r_rsm->r_flags & BBR_TLP)
4530 l_rsm->r_flags |= BBR_TLP;
4531 if (r_rsm->r_flags & BBR_RWND_COLLAPSED)
4532 l_rsm->r_flags |= BBR_RWND_COLLAPSED;
4533 if (r_rsm->r_flags & BBR_MARKED_LOST) {
4534 /* This really should not happen */
4535 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start;
4536 }
4537 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next);
4538 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
4539 /* Transfer the split limit to the map we free */
4540 r_rsm->r_limit_type = l_rsm->r_limit_type;
4541 l_rsm->r_limit_type = 0;
4542 }
4543 bbr_free(bbr, r_rsm);
4544 return(l_rsm);
4545 }
4546
4547 /*
4548 * TLP Timer, here we simply setup what segment we want to
4549 * have the TLP expire on, the normal bbr_output_wtime() will then
4550 * send it out.
4551 *
4552 * We return 1, saying don't proceed with bbr_output_wtime only
4553 * when all timers have been stopped (destroyed PCB?).
4554 */
4555 static int
bbr_timeout_tlp(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4556 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4557 {
4558 /*
4559 * Tail Loss Probe.
4560 */
4561 struct bbr_sendmap *rsm = NULL;
4562 struct socket *so;
4563 uint32_t amm;
4564 uint32_t out, avail;
4565 uint32_t maxseg;
4566 int collapsed_win = 0;
4567
4568 if (bbr->rc_all_timers_stopped) {
4569 return (1);
4570 }
4571 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4572 /* Its not time yet */
4573 return (0);
4574 }
4575 if (ctf_progress_timeout_check(tp, true)) {
4576 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4577 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4578 return (1);
4579 }
4580 /* Did we somehow get into persists? */
4581 if (bbr->rc_in_persist) {
4582 return (0);
4583 }
4584 if (bbr->r_state && (bbr->r_state != tp->t_state))
4585 bbr_set_state(tp, bbr, 0);
4586 BBR_STAT_INC(bbr_tlp_tot);
4587 maxseg = tp->t_maxseg - bbr->rc_last_options;
4588 /*
4589 * A TLP timer has expired. We have been idle for 2 rtts. So we now
4590 * need to figure out how to force a full MSS segment out.
4591 */
4592 so = tp->t_inpcb->inp_socket;
4593 avail = sbavail(&so->so_snd);
4594 out = ctf_outstanding(tp);
4595 if (out > tp->snd_wnd) {
4596 /* special case, we need a retransmission */
4597 collapsed_win = 1;
4598 goto need_retran;
4599 }
4600 if (avail > out) {
4601 /* New data is available */
4602 amm = avail - out;
4603 if (amm > maxseg) {
4604 amm = maxseg;
4605 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) {
4606 /* not enough to fill a MTU and no-delay is off */
4607 goto need_retran;
4608 }
4609 /* Set the send-new override */
4610 if ((out + amm) <= tp->snd_wnd) {
4611 bbr->rc_tlp_new_data = 1;
4612 } else {
4613 goto need_retran;
4614 }
4615 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4616 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max;
4617 bbr->r_ctl.rc_tlp_send = NULL;
4618 /* cap any slots */
4619 BBR_STAT_INC(bbr_tlp_newdata);
4620 goto send;
4621 }
4622 need_retran:
4623 /*
4624 * Ok we need to arrange the last un-acked segment to be re-sent, or
4625 * optionally the first un-acked segment.
4626 */
4627 if (collapsed_win == 0) {
4628 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
4629 if (rsm && (BBR_ACKED | BBR_HAS_FIN)) {
4630 rsm = bbr_find_high_nonack(bbr, rsm);
4631 }
4632 if (rsm == NULL) {
4633 goto restore;
4634 }
4635 } else {
4636 /*
4637 * We must find the last segment
4638 * that was acceptable by the client.
4639 */
4640 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4641 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) {
4642 /* Found one */
4643 break;
4644 }
4645 }
4646 if (rsm == NULL) {
4647 /* None? if so send the first */
4648 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4649 if (rsm == NULL)
4650 goto restore;
4651 }
4652 }
4653 if ((rsm->r_end - rsm->r_start) > maxseg) {
4654 /*
4655 * We need to split this the last segment in two.
4656 */
4657 struct bbr_sendmap *nrsm;
4658
4659 nrsm = bbr_alloc_full_limit(bbr);
4660 if (nrsm == NULL) {
4661 /*
4662 * We can't get memory to split, we can either just
4663 * not split it. Or retransmit the whole piece, lets
4664 * do the large send (BTLP :-) ).
4665 */
4666 goto go_for_it;
4667 }
4668 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg));
4669 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
4670 if (rsm->r_in_tmap) {
4671 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
4672 nrsm->r_in_tmap = 1;
4673 }
4674 rsm->r_flags &= (~BBR_HAS_FIN);
4675 rsm = nrsm;
4676 }
4677 go_for_it:
4678 bbr->r_ctl.rc_tlp_send = rsm;
4679 bbr->rc_tlp_rtx_out = 1;
4680 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) {
4681 bbr->r_ctl.rc_tlp_seg_send_cnt++;
4682 tp->t_rxtshift++;
4683 } else {
4684 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
4685 bbr->r_ctl.rc_tlp_seg_send_cnt = 1;
4686 }
4687 send:
4688 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
4689 /*
4690 * Can't [re]/transmit a segment we have retranmitted the
4691 * max times. We need the retransmit timer to take over.
4692 */
4693 restore:
4694 bbr->rc_tlp_new_data = 0;
4695 bbr->r_ctl.rc_tlp_send = NULL;
4696 if (rsm)
4697 rsm->r_flags &= ~BBR_TLP;
4698 BBR_STAT_INC(bbr_tlp_retran_fail);
4699 return (0);
4700 } else if (rsm) {
4701 rsm->r_flags |= BBR_TLP;
4702 }
4703 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) &&
4704 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) {
4705 /*
4706 * We have retransmitted to many times for TLP. Switch to
4707 * the regular RTO timer
4708 */
4709 goto restore;
4710 }
4711 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP);
4712 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
4713 return (0);
4714 }
4715
4716 /*
4717 * Delayed ack Timer, here we simply need to setup the
4718 * ACK_NOW flag and remove the DELACK flag. From there
4719 * the output routine will send the ack out.
4720 *
4721 * We only return 1, saying don't proceed, if all timers
4722 * are stopped (destroyed PCB?).
4723 */
4724 static int
bbr_timeout_delack(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4725 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4726 {
4727 if (bbr->rc_all_timers_stopped) {
4728 return (1);
4729 }
4730 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK);
4731 tp->t_flags &= ~TF_DELACK;
4732 tp->t_flags |= TF_ACKNOW;
4733 KMOD_TCPSTAT_INC(tcps_delack);
4734 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
4735 return (0);
4736 }
4737
4738 /*
4739 * Here we send a KEEP-ALIVE like probe to the
4740 * peer, we do not send data.
4741 *
4742 * We only return 1, saying don't proceed, if all timers
4743 * are stopped (destroyed PCB?).
4744 */
4745 static int
bbr_timeout_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4746 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4747 {
4748 struct tcptemp *t_template;
4749 int32_t retval = 1;
4750
4751 if (bbr->rc_all_timers_stopped) {
4752 return (1);
4753 }
4754 if (bbr->rc_in_persist == 0)
4755 return (0);
4756 KASSERT(tp->t_inpcb != NULL,
4757 ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
4758 /*
4759 * Persistence timer into zero window. Force a byte to be output, if
4760 * possible.
4761 */
4762 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST);
4763 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
4764 KMOD_TCPSTAT_INC(tcps_persisttimeo);
4765 /*
4766 * Have we exceeded the user specified progress time?
4767 */
4768 if (ctf_progress_timeout_check(tp, true)) {
4769 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4770 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4771 goto out;
4772 }
4773 /*
4774 * Hack: if the peer is dead/unreachable, we do not time out if the
4775 * window is closed. After a full backoff, drop the connection if
4776 * the idle time (no responses to probes) reaches the maximum
4777 * backoff that we would use if retransmitting.
4778 */
4779 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
4780 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4781 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
4782 KMOD_TCPSTAT_INC(tcps_persistdrop);
4783 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4784 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4785 goto out;
4786 }
4787 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) &&
4788 tp->snd_una == tp->snd_max) {
4789 bbr_exit_persist(tp, bbr, cts, __LINE__);
4790 retval = 0;
4791 goto out;
4792 }
4793 /*
4794 * If the user has closed the socket then drop a persisting
4795 * connection after a much reduced timeout.
4796 */
4797 if (tp->t_state > TCPS_CLOSE_WAIT &&
4798 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
4799 KMOD_TCPSTAT_INC(tcps_persistdrop);
4800 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4801 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4802 goto out;
4803 }
4804 t_template = tcpip_maketemplate(bbr->rc_inp);
4805 if (t_template) {
4806 tcp_respond(tp, t_template->tt_ipgen,
4807 &t_template->tt_t, (struct mbuf *)NULL,
4808 tp->rcv_nxt, tp->snd_una - 1, 0);
4809 /* This sends an ack */
4810 if (tp->t_flags & TF_DELACK)
4811 tp->t_flags &= ~TF_DELACK;
4812 free(t_template, M_TEMP);
4813 }
4814 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
4815 tp->t_rxtshift++;
4816 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0);
4817 out:
4818 return (retval);
4819 }
4820
4821 /*
4822 * If a keepalive goes off, we had no other timers
4823 * happening. We always return 1 here since this
4824 * routine either drops the connection or sends
4825 * out a segment with respond.
4826 */
4827 static int
bbr_timeout_keepalive(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4828 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4829 {
4830 struct tcptemp *t_template;
4831 struct inpcb *inp;
4832
4833 if (bbr->rc_all_timers_stopped) {
4834 return (1);
4835 }
4836 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
4837 inp = tp->t_inpcb;
4838 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP);
4839 /*
4840 * Keep-alive timer went off; send something or drop connection if
4841 * idle for too long.
4842 */
4843 KMOD_TCPSTAT_INC(tcps_keeptimeo);
4844 if (tp->t_state < TCPS_ESTABLISHED)
4845 goto dropit;
4846 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4847 tp->t_state <= TCPS_CLOSING) {
4848 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4849 goto dropit;
4850 /*
4851 * Send a packet designed to force a response if the peer is
4852 * up and reachable: either an ACK if the connection is
4853 * still alive, or an RST if the peer has closed the
4854 * connection due to timeout or reboot. Using sequence
4855 * number tp->snd_una-1 causes the transmitted zero-length
4856 * segment to lie outside the receive window; by the
4857 * protocol spec, this requires the correspondent TCP to
4858 * respond.
4859 */
4860 KMOD_TCPSTAT_INC(tcps_keepprobe);
4861 t_template = tcpip_maketemplate(inp);
4862 if (t_template) {
4863 tcp_respond(tp, t_template->tt_ipgen,
4864 &t_template->tt_t, (struct mbuf *)NULL,
4865 tp->rcv_nxt, tp->snd_una - 1, 0);
4866 free(t_template, M_TEMP);
4867 }
4868 }
4869 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0);
4870 return (1);
4871 dropit:
4872 KMOD_TCPSTAT_INC(tcps_keepdrops);
4873 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
4874 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4875 return (1);
4876 }
4877
4878 /*
4879 * Retransmit helper function, clear up all the ack
4880 * flags and take care of important book keeping.
4881 */
4882 static void
bbr_remxt_tmr(struct tcpcb * tp)4883 bbr_remxt_tmr(struct tcpcb *tp)
4884 {
4885 /*
4886 * The retransmit timer went off, all sack'd blocks must be
4887 * un-acked.
4888 */
4889 struct bbr_sendmap *rsm, *trsm = NULL;
4890 struct tcp_bbr *bbr;
4891 uint32_t cts, lost;
4892
4893 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
4894 cts = tcp_get_usecs(&bbr->rc_tv);
4895 lost = bbr->r_ctl.rc_lost;
4896 if (bbr->r_state && (bbr->r_state != tp->t_state))
4897 bbr_set_state(tp, bbr, 0);
4898
4899 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
4900 if (rsm->r_flags & BBR_ACKED) {
4901 uint32_t old_flags;
4902
4903 rsm->r_dupack = 0;
4904 if (rsm->r_in_tmap == 0) {
4905 /* We must re-add it back to the tlist */
4906 if (trsm == NULL) {
4907 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
4908 } else {
4909 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext);
4910 }
4911 rsm->r_in_tmap = 1;
4912 }
4913 old_flags = rsm->r_flags;
4914 rsm->r_flags |= BBR_RXT_CLEARED;
4915 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS);
4916 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
4917 } else {
4918 if ((tp->t_state < TCPS_ESTABLISHED) &&
4919 (rsm->r_start == tp->snd_una)) {
4920 /*
4921 * Special case for TCP FO. Where
4922 * we sent more data beyond the snd_max.
4923 * We don't mark that as lost and stop here.
4924 */
4925 break;
4926 }
4927 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4928 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4929 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4930 }
4931 if (bbr_marks_rxt_sack_passed) {
4932 /*
4933 * With this option, we will rack out
4934 * in 1ms increments the rest of the packets.
4935 */
4936 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST;
4937 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4938 } else {
4939 /*
4940 * With this option we only mark them lost
4941 * and remove all sack'd markings. We will run
4942 * another RXT or a TLP. This will cause
4943 * us to eventually send more based on what
4944 * ack's come in.
4945 */
4946 rsm->r_flags |= BBR_MARKED_LOST;
4947 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4948 rsm->r_flags &= ~BBR_SACK_PASSED;
4949 }
4950 }
4951 trsm = rsm;
4952 }
4953 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4954 /* Clear the count (we just un-acked them) */
4955 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR);
4956 bbr->rc_tlp_new_data = 0;
4957 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4958 /* zap the behindness on a rxt */
4959 bbr->r_ctl.rc_hptsi_agg_delay = 0;
4960 bbr->r_agg_early_set = 0;
4961 bbr->r_ctl.rc_agg_early = 0;
4962 bbr->rc_tlp_rtx_out = 0;
4963 bbr->r_ctl.rc_sacked = 0;
4964 bbr->r_ctl.rc_sacklast = NULL;
4965 bbr->r_timer_override = 1;
4966 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4967 }
4968
4969 /*
4970 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
4971 * we will setup to retransmit the lowest seq number outstanding.
4972 */
4973 static int
bbr_timeout_rxt(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4974 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4975 {
4976 int32_t rexmt;
4977 int32_t retval = 0;
4978 bool isipv6;
4979
4980 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
4981 if (bbr->rc_all_timers_stopped) {
4982 return (1);
4983 }
4984 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
4985 (tp->snd_una == tp->snd_max)) {
4986 /* Nothing outstanding .. nothing to do */
4987 return (0);
4988 }
4989 /*
4990 * Retransmission timer went off. Message has not been acked within
4991 * retransmit interval. Back off to a longer retransmit interval
4992 * and retransmit one segment.
4993 */
4994 if (ctf_progress_timeout_check(tp, true)) {
4995 retval = 1;
4996 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4997 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4998 goto out;
4999 }
5000 bbr_remxt_tmr(tp);
5001 if ((bbr->r_ctl.rc_resend == NULL) ||
5002 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) {
5003 /*
5004 * If the rwnd collapsed on
5005 * the one we are retransmitting
5006 * it does not count against the
5007 * rxt count.
5008 */
5009 tp->t_rxtshift++;
5010 }
5011 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
5012 tp->t_rxtshift = TCP_MAXRXTSHIFT;
5013 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
5014 retval = 1;
5015 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
5016 tcp_set_inp_to_drop(bbr->rc_inp,
5017 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
5018 goto out;
5019 }
5020 if (tp->t_state == TCPS_SYN_SENT) {
5021 /*
5022 * If the SYN was retransmitted, indicate CWND to be limited
5023 * to 1 segment in cc_conn_init().
5024 */
5025 tp->snd_cwnd = 1;
5026 } else if (tp->t_rxtshift == 1) {
5027 /*
5028 * first retransmit; record ssthresh and cwnd so they can be
5029 * recovered if this turns out to be a "bad" retransmit. A
5030 * retransmit is considered "bad" if an ACK for this segment
5031 * is received within RTT/2 interval; the assumption here is
5032 * that the ACK was already in flight. See "On Estimating
5033 * End-to-End Network Path Properties" by Allman and Paxson
5034 * for more details.
5035 */
5036 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5037 if (!IN_RECOVERY(tp->t_flags)) {
5038 tp->snd_cwnd_prev = tp->snd_cwnd;
5039 tp->snd_ssthresh_prev = tp->snd_ssthresh;
5040 tp->snd_recover_prev = tp->snd_recover;
5041 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
5042 tp->t_flags |= TF_PREVVALID;
5043 } else {
5044 tp->t_flags &= ~TF_PREVVALID;
5045 }
5046 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5047 } else {
5048 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5049 tp->t_flags &= ~TF_PREVVALID;
5050 }
5051 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
5052 if ((tp->t_state == TCPS_SYN_SENT) ||
5053 (tp->t_state == TCPS_SYN_RECEIVED))
5054 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift];
5055 else
5056 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
5057 TCPT_RANGESET(tp->t_rxtcur, rexmt,
5058 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms),
5059 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
5060 /*
5061 * We enter the path for PLMTUD if connection is established or, if
5062 * connection is FIN_WAIT_1 status, reason for the last is that if
5063 * amount of data we send is very small, we could send it in couple
5064 * of packets and process straight to FIN. In that case we won't
5065 * catch ESTABLISHED state.
5066 */
5067 #ifdef INET6
5068 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
5069 #else
5070 isipv6 = false;
5071 #endif
5072 if (((V_tcp_pmtud_blackhole_detect == 1) ||
5073 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
5074 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
5075 ((tp->t_state == TCPS_ESTABLISHED) ||
5076 (tp->t_state == TCPS_FIN_WAIT_1))) {
5077 /*
5078 * Idea here is that at each stage of mtu probe (usually,
5079 * 1448 -> 1188 -> 524) should be given 2 chances to recover
5080 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
5081 * should take care of that.
5082 */
5083 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
5084 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
5085 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
5086 tp->t_rxtshift % 2 == 0)) {
5087 /*
5088 * Enter Path MTU Black-hole Detection mechanism: -
5089 * Disable Path MTU Discovery (IP "DF" bit). -
5090 * Reduce MTU to lower value than what we negotiated
5091 * with peer.
5092 */
5093 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
5094 /*
5095 * Record that we may have found a black
5096 * hole.
5097 */
5098 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
5099 /* Keep track of previous MSS. */
5100 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
5101 }
5102 /*
5103 * Reduce the MSS to blackhole value or to the
5104 * default in an attempt to retransmit.
5105 */
5106 #ifdef INET6
5107 isipv6 = bbr->r_is_v6;
5108 if (isipv6 &&
5109 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
5110 /* Use the sysctl tuneable blackhole MSS. */
5111 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
5112 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5113 } else if (isipv6) {
5114 /* Use the default MSS. */
5115 tp->t_maxseg = V_tcp_v6mssdflt;
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 #if defined(INET6) && defined(INET)
5125 else
5126 #endif
5127 #ifdef INET
5128 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
5129 /* Use the sysctl tuneable blackhole MSS. */
5130 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
5131 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5132 } else {
5133 /* Use the default MSS. */
5134 tp->t_maxseg = V_tcp_mssdflt;
5135 /*
5136 * Disable Path MTU Discovery when we switch
5137 * to minmss.
5138 */
5139 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5140 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5141 }
5142 #endif
5143 } else {
5144 /*
5145 * If further retransmissions are still unsuccessful
5146 * with a lowered MTU, maybe this isn't a blackhole
5147 * and we restore the previous MSS and blackhole
5148 * detection flags. The limit '6' is determined by
5149 * giving each probe stage (1448, 1188, 524) 2
5150 * chances to recover.
5151 */
5152 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
5153 (tp->t_rxtshift >= 6)) {
5154 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
5155 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
5156 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
5157 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5158 }
5159 }
5160 }
5161 /*
5162 * Disable RFC1323 and SACK if we haven't got any response to our
5163 * third SYN to work-around some broken terminal servers (most of
5164 * which have hopefully been retired) that have bad VJ header
5165 * compression code which trashes TCP segments containing
5166 * unknown-to-them TCP options.
5167 */
5168 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
5169 (tp->t_rxtshift == 3))
5170 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
5171 /*
5172 * If we backed off this far, our srtt estimate is probably bogus.
5173 * Clobber it so we'll take the next rtt measurement as our srtt;
5174 * move the current srtt into rttvar to keep the current retransmit
5175 * times until then.
5176 */
5177 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5178 #ifdef INET6
5179 if (bbr->r_is_v6)
5180 in6_losing(tp->t_inpcb);
5181 else
5182 #endif
5183 in_losing(tp->t_inpcb);
5184 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5185 tp->t_srtt = 0;
5186 }
5187 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
5188 tp->snd_recover = tp->snd_max;
5189 tp->t_flags |= TF_ACKNOW;
5190 tp->t_rtttime = 0;
5191 out:
5192 return (retval);
5193 }
5194
5195 static int
bbr_process_timers(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,uint8_t hpts_calling)5196 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling)
5197 {
5198 int32_t ret = 0;
5199 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5200
5201 if (timers == 0) {
5202 return (0);
5203 }
5204 if (tp->t_state == TCPS_LISTEN) {
5205 /* no timers on listen sockets */
5206 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5207 return (0);
5208 return (1);
5209 }
5210 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
5211 uint32_t left;
5212
5213 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5214 ret = -1;
5215 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5216 return (0);
5217 }
5218 if (hpts_calling == 0) {
5219 ret = -2;
5220 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5221 return (0);
5222 }
5223 /*
5224 * Ok our timer went off early and we are not paced false
5225 * alarm, go back to sleep.
5226 */
5227 left = bbr->r_ctl.rc_timer_exp - cts;
5228 ret = -3;
5229 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling);
5230 tcp_hpts_insert(tp->t_inpcb, HPTS_USEC_TO_SLOTS(left));
5231 return (1);
5232 }
5233 bbr->rc_tmr_stopped = 0;
5234 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5235 if (timers & PACE_TMR_DELACK) {
5236 ret = bbr_timeout_delack(tp, bbr, cts);
5237 } else if (timers & PACE_TMR_PERSIT) {
5238 ret = bbr_timeout_persist(tp, bbr, cts);
5239 } else if (timers & PACE_TMR_RACK) {
5240 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5241 ret = bbr_timeout_rack(tp, bbr, cts);
5242 } else if (timers & PACE_TMR_TLP) {
5243 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5244 ret = bbr_timeout_tlp(tp, bbr, cts);
5245 } else if (timers & PACE_TMR_RXT) {
5246 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5247 ret = bbr_timeout_rxt(tp, bbr, cts);
5248 } else if (timers & PACE_TMR_KEEP) {
5249 ret = bbr_timeout_keepalive(tp, bbr, cts);
5250 }
5251 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling);
5252 return (ret);
5253 }
5254
5255 static void
bbr_timer_cancel(struct tcp_bbr * bbr,int32_t line,uint32_t cts)5256 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts)
5257 {
5258 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5259 uint8_t hpts_removed = 0;
5260
5261 if (bbr->rc_inp->inp_in_hpts &&
5262 (bbr->rc_timer_first == 1)) {
5263 /*
5264 * If we are canceling timer's when we have the
5265 * timer ahead of the output being paced. We also
5266 * must remove ourselves from the hpts.
5267 */
5268 hpts_removed = 1;
5269 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
5270 if (bbr->r_ctl.rc_last_delay_val) {
5271 /* Update the last hptsi delay too */
5272 uint32_t time_since_send;
5273
5274 if (TSTMP_GT(cts, bbr->rc_pacer_started))
5275 time_since_send = cts - bbr->rc_pacer_started;
5276 else
5277 time_since_send = 0;
5278 if (bbr->r_ctl.rc_last_delay_val > time_since_send) {
5279 /* Cut down our slot time */
5280 bbr->r_ctl.rc_last_delay_val -= time_since_send;
5281 } else {
5282 bbr->r_ctl.rc_last_delay_val = 0;
5283 }
5284 bbr->rc_pacer_started = cts;
5285 }
5286 }
5287 bbr->rc_timer_first = 0;
5288 bbr_log_to_cancel(bbr, line, cts, hpts_removed);
5289 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5290 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5291 }
5292 }
5293
5294 static void
bbr_timer_stop(struct tcpcb * tp,uint32_t timer_type)5295 bbr_timer_stop(struct tcpcb *tp, uint32_t timer_type)
5296 {
5297 struct tcp_bbr *bbr;
5298
5299 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
5300 bbr->rc_all_timers_stopped = 1;
5301 return;
5302 }
5303
5304 /*
5305 * stop all timers always returning 0.
5306 */
5307 static int
bbr_stopall(struct tcpcb * tp)5308 bbr_stopall(struct tcpcb *tp)
5309 {
5310 return (0);
5311 }
5312
5313 static void
bbr_timer_activate(struct tcpcb * tp,uint32_t timer_type,uint32_t delta)5314 bbr_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
5315 {
5316 return;
5317 }
5318
5319 /*
5320 * return true if a bbr timer (rack or tlp) is active.
5321 */
5322 static int
bbr_timer_active(struct tcpcb * tp,uint32_t timer_type)5323 bbr_timer_active(struct tcpcb *tp, uint32_t timer_type)
5324 {
5325 return (0);
5326 }
5327
5328 static uint32_t
bbr_get_earliest_send_outstanding(struct tcp_bbr * bbr,struct bbr_sendmap * u_rsm,uint32_t cts)5329 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts)
5330 {
5331 struct bbr_sendmap *rsm;
5332
5333 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
5334 if ((rsm == NULL) || (u_rsm == rsm))
5335 return (cts);
5336 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
5337 }
5338
5339 static void
bbr_update_rsm(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts,uint32_t pacing_time)5340 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr,
5341 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time)
5342 {
5343 int32_t idx;
5344
5345 rsm->r_rtr_cnt++;
5346 rsm->r_dupack = 0;
5347 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) {
5348 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS;
5349 rsm->r_flags |= BBR_OVERMAX;
5350 }
5351 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
5352 /* Take off the collapsed flag at rxt */
5353 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
5354 }
5355 if (rsm->r_flags & BBR_MARKED_LOST) {
5356 /* We have retransmitted, its no longer lost */
5357 rsm->r_flags &= ~BBR_MARKED_LOST;
5358 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
5359 }
5360 if (rsm->r_flags & BBR_RXT_CLEARED) {
5361 /*
5362 * We hit a RXT timer on it and
5363 * we cleared the "acked" flag.
5364 * We now have it going back into
5365 * flight, we can remove the cleared
5366 * flag and possibly do accounting on
5367 * this piece.
5368 */
5369 rsm->r_flags &= ~BBR_RXT_CLEARED;
5370 }
5371 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) {
5372 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5373 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5374 }
5375 idx = rsm->r_rtr_cnt - 1;
5376 rsm->r_tim_lastsent[idx] = cts;
5377 rsm->r_pacing_delay = pacing_time;
5378 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5379 rsm->r_ts_valid = bbr->rc_ts_valid;
5380 if (bbr->rc_ts_valid)
5381 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5382 if (bbr->r_ctl.r_app_limited_until)
5383 rsm->r_app_limited = 1;
5384 else
5385 rsm->r_app_limited = 0;
5386 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5387 rsm->r_bbr_state = bbr_state_val(bbr);
5388 else
5389 rsm->r_bbr_state = 8;
5390 if (rsm->r_flags & BBR_ACKED) {
5391 /* Problably MTU discovery messing with us */
5392 uint32_t old_flags;
5393
5394 old_flags = rsm->r_flags;
5395 rsm->r_flags &= ~BBR_ACKED;
5396 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
5397 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5398 if (bbr->r_ctl.rc_sacked == 0)
5399 bbr->r_ctl.rc_sacklast = NULL;
5400 }
5401 if (rsm->r_in_tmap) {
5402 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5403 }
5404 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5405 rsm->r_in_tmap = 1;
5406 if (rsm->r_flags & BBR_SACK_PASSED) {
5407 /* We have retransmitted due to the SACK pass */
5408 rsm->r_flags &= ~BBR_SACK_PASSED;
5409 rsm->r_flags |= BBR_WAS_SACKPASS;
5410 }
5411 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5412 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5413 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5414 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
5415 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5416 rsm->r_is_gain = 1;
5417 rsm->r_is_drain = 0;
5418 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5419 rsm->r_is_drain = 1;
5420 rsm->r_is_gain = 0;
5421 } else {
5422 rsm->r_is_drain = 0;
5423 rsm->r_is_gain = 0;
5424 }
5425 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */
5426 }
5427
5428 /*
5429 * Returns 0, or the sequence where we stopped
5430 * updating. We also update the lenp to be the amount
5431 * of data left.
5432 */
5433
5434 static uint32_t
bbr_update_entry(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts,int32_t * lenp,uint32_t pacing_time)5435 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr,
5436 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time)
5437 {
5438 /*
5439 * We (re-)transmitted starting at rsm->r_start for some length
5440 * (possibly less than r_end.
5441 */
5442 struct bbr_sendmap *nrsm;
5443 uint32_t c_end;
5444 int32_t len;
5445
5446 len = *lenp;
5447 c_end = rsm->r_start + len;
5448 if (SEQ_GEQ(c_end, rsm->r_end)) {
5449 /*
5450 * We retransmitted the whole piece or more than the whole
5451 * slopping into the next rsm.
5452 */
5453 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5454 if (c_end == rsm->r_end) {
5455 *lenp = 0;
5456 return (0);
5457 } else {
5458 int32_t act_len;
5459
5460 /* Hangs over the end return whats left */
5461 act_len = rsm->r_end - rsm->r_start;
5462 *lenp = (len - act_len);
5463 return (rsm->r_end);
5464 }
5465 /* We don't get out of this block. */
5466 }
5467 /*
5468 * Here we retransmitted less than the whole thing which means we
5469 * have to split this into what was transmitted and what was not.
5470 */
5471 nrsm = bbr_alloc_full_limit(bbr);
5472 if (nrsm == NULL) {
5473 *lenp = 0;
5474 return (0);
5475 }
5476 /*
5477 * So here we are going to take the original rsm and make it what we
5478 * retransmitted. nrsm will be the tail portion we did not
5479 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
5480 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
5481 * 1, 6 and the new piece will be 6, 11.
5482 */
5483 bbr_clone_rsm(bbr, nrsm, rsm, c_end);
5484 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
5485 nrsm->r_dupack = 0;
5486 if (rsm->r_in_tmap) {
5487 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5488 nrsm->r_in_tmap = 1;
5489 }
5490 rsm->r_flags &= (~BBR_HAS_FIN);
5491 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5492 *lenp = 0;
5493 return (0);
5494 }
5495
5496 static uint64_t
bbr_get_hardware_rate(struct tcp_bbr * bbr)5497 bbr_get_hardware_rate(struct tcp_bbr *bbr)
5498 {
5499 uint64_t bw;
5500
5501 bw = bbr_get_bw(bbr);
5502 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN];
5503 bw /= (uint64_t)BBR_UNIT;
5504 return(bw);
5505 }
5506
5507 static void
bbr_setup_less_of_rate(struct tcp_bbr * bbr,uint32_t cts,uint64_t act_rate,uint64_t rate_wanted)5508 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts,
5509 uint64_t act_rate, uint64_t rate_wanted)
5510 {
5511 /*
5512 * We could not get a full gains worth
5513 * of rate.
5514 */
5515 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) {
5516 /* we can't even get the real rate */
5517 uint64_t red;
5518
5519 bbr->skip_gain = 1;
5520 bbr->gain_is_limited = 0;
5521 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate;
5522 if (red)
5523 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts);
5524 } else {
5525 /* We can use a lower gain */
5526 bbr->skip_gain = 0;
5527 bbr->gain_is_limited = 1;
5528 }
5529 }
5530
5531 static void
bbr_update_hardware_pacing_rate(struct tcp_bbr * bbr,uint32_t cts)5532 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts)
5533 {
5534 const struct tcp_hwrate_limit_table *nrte;
5535 int error, rate = -1;
5536
5537 if (bbr->r_ctl.crte == NULL)
5538 return;
5539 if ((bbr->rc_inp->inp_route.ro_nh == NULL) ||
5540 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) {
5541 /* Lost our routes? */
5542 /* Clear the way for a re-attempt */
5543 bbr->bbr_attempt_hdwr_pace = 0;
5544 lost_rate:
5545 bbr->gain_is_limited = 0;
5546 bbr->skip_gain = 0;
5547 bbr->bbr_hdrw_pacing = 0;
5548 counter_u64_add(bbr_flows_whdwr_pacing, -1);
5549 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
5550 tcp_bbr_tso_size_check(bbr, cts);
5551 return;
5552 }
5553 rate = bbr_get_hardware_rate(bbr);
5554 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte,
5555 bbr->rc_tp,
5556 bbr->rc_inp->inp_route.ro_nh->nh_ifp,
5557 rate,
5558 (RS_PACING_GEQ|RS_PACING_SUB_OK),
5559 &error);
5560 if (nrte == NULL) {
5561 goto lost_rate;
5562 }
5563 if (nrte != bbr->r_ctl.crte) {
5564 bbr->r_ctl.crte = nrte;
5565 if (error == 0) {
5566 BBR_STAT_INC(bbr_hdwr_rl_mod_ok);
5567 if (bbr->r_ctl.crte->rate < rate) {
5568 /* We have a problem */
5569 bbr_setup_less_of_rate(bbr, cts,
5570 bbr->r_ctl.crte->rate, rate);
5571 } else {
5572 /* We are good */
5573 bbr->gain_is_limited = 0;
5574 bbr->skip_gain = 0;
5575 }
5576 } else {
5577 /* A failure should release the tag */
5578 BBR_STAT_INC(bbr_hdwr_rl_mod_fail);
5579 bbr->gain_is_limited = 0;
5580 bbr->skip_gain = 0;
5581 bbr->bbr_hdrw_pacing = 0;
5582 }
5583 bbr_type_log_hdwr_pacing(bbr,
5584 bbr->r_ctl.crte->ptbl->rs_ifp,
5585 rate,
5586 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate),
5587 __LINE__,
5588 cts,
5589 error);
5590 }
5591 }
5592
5593 static void
bbr_adjust_for_hw_pacing(struct tcp_bbr * bbr,uint32_t cts)5594 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts)
5595 {
5596 /*
5597 * If we have hardware pacing support
5598 * we need to factor that in for our
5599 * TSO size.
5600 */
5601 const struct tcp_hwrate_limit_table *rlp;
5602 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay;
5603
5604 if ((bbr->bbr_hdrw_pacing == 0) ||
5605 (IN_RECOVERY(bbr->rc_tp->t_flags)) ||
5606 (bbr->r_ctl.crte == NULL))
5607 return;
5608 if (bbr->hw_pacing_set == 0) {
5609 /* Not yet by the hdwr pacing count delay */
5610 return;
5611 }
5612 if (bbr_hdwr_pace_adjust == 0) {
5613 /* No adjustment */
5614 return;
5615 }
5616 rlp = bbr->r_ctl.crte;
5617 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options)
5618 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5619 else
5620 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5621 /*
5622 * So lets first get the
5623 * time we will take between
5624 * TSO sized sends currently without
5625 * hardware help.
5626 */
5627 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT,
5628 bbr->r_ctl.rc_pace_max_segs, cts, 1);
5629 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg;
5630 hdwr_delay *= rlp->time_between;
5631 if (cur_delay > hdwr_delay)
5632 delta = cur_delay - hdwr_delay;
5633 else
5634 delta = 0;
5635 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay,
5636 (bbr->r_ctl.rc_pace_max_segs / maxseg),
5637 1);
5638 if (delta &&
5639 (delta < (max(rlp->time_between,
5640 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) {
5641 /*
5642 * Now lets divide by the pacing
5643 * time between each segment the
5644 * hardware sends rounding up and
5645 * derive a bytes from that. We multiply
5646 * that by bbr_hdwr_pace_adjust to get
5647 * more bang for our buck.
5648 *
5649 * The goal is to have the software pacer
5650 * waiting no more than an additional
5651 * pacing delay if we can (without the
5652 * compensation i.e. x bbr_hdwr_pace_adjust).
5653 */
5654 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between),
5655 (bbr->r_ctl.rc_pace_max_segs/maxseg));
5656 seg_sz *= bbr_hdwr_pace_adjust;
5657 if (bbr_hdwr_pace_floor &&
5658 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5659 /* Currently hardware paces
5660 * out rs_min_seg segments at a time.
5661 * We need to make sure we always send at least
5662 * a full burst of bbr_hdwr_pace_floor down.
5663 */
5664 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5665 }
5666 seg_sz *= maxseg;
5667 } else if (delta == 0) {
5668 /*
5669 * The highest pacing rate is
5670 * above our b/w gained. This means
5671 * we probably are going quite fast at
5672 * the hardware highest rate. Lets just multiply
5673 * the calculated TSO size by the
5674 * multiplier factor (its probably
5675 * 4 segments in the default config for
5676 * mlx).
5677 */
5678 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust;
5679 if (bbr_hdwr_pace_floor &&
5680 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5681 /* Currently hardware paces
5682 * out rs_min_seg segments at a time.
5683 * We need to make sure we always send at least
5684 * a full burst of bbr_hdwr_pace_floor down.
5685 */
5686 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5687 }
5688 } else {
5689 /*
5690 * The pacing time difference is so
5691 * big that the hardware will
5692 * pace out more rapidly then we
5693 * really want and then we
5694 * will have a long delay. Lets just keep
5695 * the same TSO size so its as if
5696 * we were not using hdwr pacing (we
5697 * just gain a bit of spacing from the
5698 * hardware if seg_sz > 1).
5699 */
5700 seg_sz = bbr->r_ctl.rc_pace_max_segs;
5701 }
5702 if (seg_sz > bbr->r_ctl.rc_pace_max_segs)
5703 new_tso = seg_sz;
5704 else
5705 new_tso = bbr->r_ctl.rc_pace_max_segs;
5706 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg))
5707 new_tso = PACE_MAX_IP_BYTES - maxseg;
5708
5709 if (new_tso != bbr->r_ctl.rc_pace_max_segs) {
5710 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0);
5711 bbr->r_ctl.rc_pace_max_segs = new_tso;
5712 }
5713 }
5714
5715 static void
tcp_bbr_tso_size_check(struct tcp_bbr * bbr,uint32_t cts)5716 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts)
5717 {
5718 uint64_t bw;
5719 uint32_t old_tso = 0, new_tso;
5720 uint32_t maxseg, bytes;
5721 uint32_t tls_seg=0;
5722 /*
5723 * Google/linux uses the following algorithm to determine
5724 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18):
5725 *
5726 * bytes = bw_in_bytes_per_second / 1000
5727 * bytes = min(bytes, 64k)
5728 * tso_segs = bytes / MSS
5729 * if (bw < 1.2Mbs)
5730 * min_tso_segs = 1
5731 * else
5732 * min_tso_segs = 2
5733 * tso_segs = max(tso_segs, min_tso_segs)
5734 *
5735 * * Note apply a device specific limit (we apply this in the
5736 * tcp_m_copym).
5737 * Note that before the initial measurement is made google bursts out
5738 * a full iwnd just like new-reno/cubic.
5739 *
5740 * We do not use this algorithm. Instead we
5741 * use a two phased approach:
5742 *
5743 * if ( bw <= per-tcb-cross-over)
5744 * goal_tso = calculate how much with this bw we
5745 * can send in goal-time seconds.
5746 * if (goal_tso > mss)
5747 * seg = goal_tso / mss
5748 * tso = seg * mss
5749 * else
5750 * tso = mss
5751 * if (tso > per-tcb-max)
5752 * tso = per-tcb-max
5753 * else if ( bw > 512Mbps)
5754 * tso = max-tso (64k/mss)
5755 * else
5756 * goal_tso = bw / per-tcb-divsor
5757 * seg = (goal_tso + mss-1)/mss
5758 * tso = seg * mss
5759 *
5760 * if (tso < per-tcb-floor)
5761 * tso = per-tcb-floor
5762 * if (tso > per-tcb-utter_max)
5763 * tso = per-tcb-utter_max
5764 *
5765 * Note the default per-tcb-divisor is 1000 (same as google).
5766 * the goal cross over is 30Mbps however. To recreate googles
5767 * algorithm you need to set:
5768 *
5769 * cross-over = 23,168,000 bps
5770 * goal-time = 18000
5771 * per-tcb-max = 2
5772 * per-tcb-divisor = 1000
5773 * per-tcb-floor = 1
5774 *
5775 * This will get you "google bbr" behavior with respect to tso size.
5776 *
5777 * Note we do set anything TSO size until we are past the initial
5778 * window. Before that we gnerally use either a single MSS
5779 * or we use the full IW size (so we burst a IW at a time)
5780 */
5781
5782 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) {
5783 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5784 } else {
5785 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5786 }
5787 old_tso = bbr->r_ctl.rc_pace_max_segs;
5788 if (bbr->rc_past_init_win == 0) {
5789 /*
5790 * Not enough data has been acknowledged to make a
5791 * judgement. Set up the initial TSO based on if we
5792 * are sending a full IW at once or not.
5793 */
5794 if (bbr->rc_use_google)
5795 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2);
5796 else if (bbr->bbr_init_win_cheat)
5797 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp);
5798 else
5799 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5800 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg)
5801 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg;
5802 if (bbr->r_ctl.rc_pace_max_segs == 0) {
5803 bbr->r_ctl.rc_pace_max_segs = maxseg;
5804 }
5805 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0);
5806 bbr_adjust_for_hw_pacing(bbr, cts);
5807 return;
5808 }
5809 /**
5810 * Now lets set the TSO goal based on our delivery rate in
5811 * bytes per second. Note we only do this if
5812 * we have acked at least the initial cwnd worth of data.
5813 */
5814 bw = bbr_get_bw(bbr);
5815 if (IN_RECOVERY(bbr->rc_tp->t_flags) &&
5816 (bbr->rc_use_google == 0)) {
5817 /* We clamp to one MSS in recovery */
5818 new_tso = maxseg;
5819 } else if (bbr->rc_use_google) {
5820 int min_tso_segs;
5821
5822 /* Google considers the gain too */
5823 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) {
5824 bw *= bbr->r_ctl.rc_bbr_hptsi_gain;
5825 bw /= BBR_UNIT;
5826 }
5827 bytes = bw / 1024;
5828 if (bytes > (64 * 1024))
5829 bytes = 64 * 1024;
5830 new_tso = bytes / maxseg;
5831 if (bw < ONE_POINT_TWO_MEG)
5832 min_tso_segs = 1;
5833 else
5834 min_tso_segs = 2;
5835 if (new_tso < min_tso_segs)
5836 new_tso = min_tso_segs;
5837 new_tso *= maxseg;
5838 } else if (bbr->rc_no_pacing) {
5839 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg;
5840 } else if (bw <= bbr->r_ctl.bbr_cross_over) {
5841 /*
5842 * Calculate the worse case b/w TSO if we are inserting no
5843 * more than a delay_target number of TSO's.
5844 */
5845 uint32_t tso_len, min_tso;
5846
5847 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw);
5848 if (tso_len > maxseg) {
5849 new_tso = tso_len / maxseg;
5850 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max)
5851 new_tso = bbr->r_ctl.bbr_hptsi_segments_max;
5852 new_tso *= maxseg;
5853 } else {
5854 /*
5855 * less than a full sized frame yikes.. long rtt or
5856 * low bw?
5857 */
5858 min_tso = bbr_minseg(bbr);
5859 if ((tso_len > min_tso) && (bbr_all_get_min == 0))
5860 new_tso = rounddown(tso_len, min_tso);
5861 else
5862 new_tso = min_tso;
5863 }
5864 } else if (bw > FIVETWELVE_MBPS) {
5865 /*
5866 * This guy is so fast b/w wise that we can TSO as large as
5867 * possible of segments that the NIC will allow.
5868 */
5869 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5870 } else {
5871 /*
5872 * This formula is based on attempting to send a segment or
5873 * more every bbr_hptsi_per_second. The default is 1000
5874 * which means you are targeting what you can send every 1ms
5875 * based on the peers bw.
5876 *
5877 * If the number drops to say 500, then you are looking more
5878 * at 2ms and you will raise how much we send in a single
5879 * TSO thus saving CPU (less bbr_output_wtime() calls). The
5880 * trade off of course is you will send more at once and
5881 * thus tend to clump up the sends into larger "bursts"
5882 * building a queue.
5883 */
5884 bw /= bbr->r_ctl.bbr_hptsi_per_second;
5885 new_tso = roundup(bw, (uint64_t)maxseg);
5886 /*
5887 * Gate the floor to match what our lower than 48Mbps
5888 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus
5889 * becomes the floor for this calculation.
5890 */
5891 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg))
5892 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg);
5893 }
5894 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor)))
5895 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor;
5896 if (new_tso > PACE_MAX_IP_BYTES)
5897 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5898 /* Enforce an utter maximum. */
5899 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) {
5900 new_tso = bbr->r_ctl.bbr_utter_max * maxseg;
5901 }
5902 if (old_tso != new_tso) {
5903 /* Only log changes */
5904 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0);
5905 bbr->r_ctl.rc_pace_max_segs = new_tso;
5906 }
5907 /* We have hardware pacing! */
5908 bbr_adjust_for_hw_pacing(bbr, cts);
5909 }
5910
5911 static void
bbr_log_output(struct tcp_bbr * bbr,struct tcpcb * tp,struct tcpopt * to,int32_t len,uint32_t seq_out,uint8_t th_flags,int32_t err,uint32_t cts,struct mbuf * mb,int32_t * abandon,struct bbr_sendmap * hintrsm,uint32_t delay_calc,struct sockbuf * sb)5912 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len,
5913 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t cts,
5914 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc,
5915 struct sockbuf *sb)
5916 {
5917
5918 struct bbr_sendmap *rsm, *nrsm;
5919 register uint32_t snd_max, snd_una;
5920 uint32_t pacing_time;
5921 /*
5922 * Add to the RACK log of packets in flight or retransmitted. If
5923 * there is a TS option we will use the TS echoed, if not we will
5924 * grab a TS.
5925 *
5926 * Retransmissions will increment the count and move the ts to its
5927 * proper place. Note that if options do not include TS's then we
5928 * won't be able to effectively use the ACK for an RTT on a retran.
5929 *
5930 * Notes about r_start and r_end. Lets consider a send starting at
5931 * sequence 1 for 10 bytes. In such an example the r_start would be
5932 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
5933 * This means that r_end is actually the first sequence for the next
5934 * slot (11).
5935 *
5936 */
5937 INP_WLOCK_ASSERT(tp->t_inpcb);
5938 if (err) {
5939 /*
5940 * We don't log errors -- we could but snd_max does not
5941 * advance in this case either.
5942 */
5943 return;
5944 }
5945 if (th_flags & TH_RST) {
5946 /*
5947 * We don't log resets and we return immediately from
5948 * sending
5949 */
5950 *abandon = 1;
5951 return;
5952 }
5953 snd_una = tp->snd_una;
5954 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) {
5955 /*
5956 * The call to bbr_log_output is made before bumping
5957 * snd_max. This means we can record one extra byte on a SYN
5958 * or FIN if seq_out is adding more on and a FIN is present
5959 * (and we are not resending).
5960 */
5961 if ((th_flags & TH_SYN) && (tp->iss == seq_out))
5962 len++;
5963 if (th_flags & TH_FIN)
5964 len++;
5965 }
5966 if (SEQ_LEQ((seq_out + len), snd_una)) {
5967 /* Are sending an old segment to induce an ack (keep-alive)? */
5968 return;
5969 }
5970 if (SEQ_LT(seq_out, snd_una)) {
5971 /* huh? should we panic? */
5972 uint32_t end;
5973
5974 end = seq_out + len;
5975 seq_out = snd_una;
5976 len = end - seq_out;
5977 }
5978 snd_max = tp->snd_max;
5979 if (len == 0) {
5980 /* We don't log zero window probes */
5981 return;
5982 }
5983 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1);
5984 /* First question is it a retransmission? */
5985 if (seq_out == snd_max) {
5986 again:
5987 rsm = bbr_alloc(bbr);
5988 if (rsm == NULL) {
5989 return;
5990 }
5991 rsm->r_flags = 0;
5992 if (th_flags & TH_SYN)
5993 rsm->r_flags |= BBR_HAS_SYN;
5994 if (th_flags & TH_FIN)
5995 rsm->r_flags |= BBR_HAS_FIN;
5996 rsm->r_tim_lastsent[0] = cts;
5997 rsm->r_rtr_cnt = 1;
5998 rsm->r_rtr_bytes = 0;
5999 rsm->r_start = seq_out;
6000 rsm->r_end = rsm->r_start + len;
6001 rsm->r_dupack = 0;
6002 rsm->r_delivered = bbr->r_ctl.rc_delivered;
6003 rsm->r_pacing_delay = pacing_time;
6004 rsm->r_ts_valid = bbr->rc_ts_valid;
6005 if (bbr->rc_ts_valid)
6006 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
6007 rsm->r_del_time = bbr->r_ctl.rc_del_time;
6008 if (bbr->r_ctl.r_app_limited_until)
6009 rsm->r_app_limited = 1;
6010 else
6011 rsm->r_app_limited = 0;
6012 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
6013 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
6014 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
6015 /*
6016 * Here we must also add in this rsm since snd_max
6017 * is updated after we return from a new send.
6018 */
6019 rsm->r_flight_at_send += len;
6020 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
6021 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
6022 rsm->r_in_tmap = 1;
6023 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
6024 rsm->r_bbr_state = bbr_state_val(bbr);
6025 else
6026 rsm->r_bbr_state = 8;
6027 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
6028 rsm->r_is_gain = 1;
6029 rsm->r_is_drain = 0;
6030 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
6031 rsm->r_is_drain = 1;
6032 rsm->r_is_gain = 0;
6033 } else {
6034 rsm->r_is_drain = 0;
6035 rsm->r_is_gain = 0;
6036 }
6037 return;
6038 }
6039 /*
6040 * If we reach here its a retransmission and we need to find it.
6041 */
6042 more:
6043 if (hintrsm && (hintrsm->r_start == seq_out)) {
6044 rsm = hintrsm;
6045 hintrsm = NULL;
6046 } else if (bbr->r_ctl.rc_next) {
6047 /* We have a hint from a previous run */
6048 rsm = bbr->r_ctl.rc_next;
6049 } else {
6050 /* No hints sorry */
6051 rsm = NULL;
6052 }
6053 if ((rsm) && (rsm->r_start == seq_out)) {
6054 /*
6055 * We used rc_next or hintrsm to retransmit, hopefully the
6056 * likely case.
6057 */
6058 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6059 if (len == 0) {
6060 return;
6061 } else {
6062 goto more;
6063 }
6064 }
6065 /* Ok it was not the last pointer go through it the hard way. */
6066 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6067 if (rsm->r_start == seq_out) {
6068 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6069 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
6070 if (len == 0) {
6071 return;
6072 } else {
6073 continue;
6074 }
6075 }
6076 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6077 /* Transmitted within this piece */
6078 /*
6079 * Ok we must split off the front and then let the
6080 * update do the rest
6081 */
6082 nrsm = bbr_alloc_full_limit(bbr);
6083 if (nrsm == NULL) {
6084 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
6085 return;
6086 }
6087 /*
6088 * copy rsm to nrsm and then trim the front of rsm
6089 * to not include this part.
6090 */
6091 bbr_clone_rsm(bbr, nrsm, rsm, seq_out);
6092 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
6093 if (rsm->r_in_tmap) {
6094 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6095 nrsm->r_in_tmap = 1;
6096 }
6097 rsm->r_flags &= (~BBR_HAS_FIN);
6098 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time);
6099 if (len == 0) {
6100 return;
6101 }
6102 }
6103 }
6104 /*
6105 * Hmm not found in map did they retransmit both old and on into the
6106 * new?
6107 */
6108 if (seq_out == tp->snd_max) {
6109 goto again;
6110 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6111 #ifdef BBR_INVARIANTS
6112 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6113 seq_out, len, tp->snd_una, tp->snd_max);
6114 printf("Starting Dump of all rack entries\n");
6115 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6116 printf("rsm:%p start:%u end:%u\n",
6117 rsm, rsm->r_start, rsm->r_end);
6118 }
6119 printf("Dump complete\n");
6120 panic("seq_out not found rack:%p tp:%p",
6121 bbr, tp);
6122 #endif
6123 } else {
6124 #ifdef BBR_INVARIANTS
6125 /*
6126 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6127 * flag)
6128 */
6129 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6130 seq_out, len, tp->snd_max, tp);
6131 #endif
6132 }
6133 }
6134
6135 static void
bbr_collapse_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,int32_t rtt)6136 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt)
6137 {
6138 /*
6139 * Collapse timeout back the cum-ack moved.
6140 */
6141 tp->t_rxtshift = 0;
6142 tp->t_softerror = 0;
6143 }
6144
6145 static void
tcp_bbr_xmit_timer(struct tcp_bbr * bbr,uint32_t rtt_usecs,uint32_t rsm_send_time,uint32_t r_start,uint32_t tsin)6146 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin)
6147 {
6148 bbr->rtt_valid = 1;
6149 bbr->r_ctl.cur_rtt = rtt_usecs;
6150 bbr->r_ctl.ts_in = tsin;
6151 if (rsm_send_time)
6152 bbr->r_ctl.cur_rtt_send_time = rsm_send_time;
6153 }
6154
6155 static void
bbr_make_timestamp_determination(struct tcp_bbr * bbr)6156 bbr_make_timestamp_determination(struct tcp_bbr *bbr)
6157 {
6158 /**
6159 * We have in our bbr control:
6160 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp).
6161 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts).
6162 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts)
6163 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time)
6164 *
6165 * Now we can calculate the time between the sends by doing:
6166 *
6167 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts
6168 *
6169 * And the peer's time between receiving them by doing:
6170 *
6171 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp
6172 *
6173 * We want to figure out if the timestamp values are in msec, 10msec or usec.
6174 * We also may find that we can't use the timestamps if say we see
6175 * that the peer_delta indicates that though we may have taken 10ms to
6176 * pace out the data, it only saw 1ms between the two packets. This would
6177 * indicate that somewhere on the path is a batching entity that is giving
6178 * out time-slices of the actual b/w. This would mean we could not use
6179 * reliably the peers timestamps.
6180 *
6181 * We expect delta > peer_delta initially. Until we figure out the
6182 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio.
6183 * If we place 1000 there then its a ms vs our usec. If we place 10000 there
6184 * then its 10ms vs our usec. If the peer is running a usec clock we would
6185 * put a 1 there. If the value is faster then ours, we will disable the
6186 * use of timestamps (though we could revist this later if we find it to be not
6187 * just an isolated one or two flows)).
6188 *
6189 * To detect the batching middle boxes we will come up with our compensation and
6190 * if with it in place, we find the peer is drastically off (by some margin) in
6191 * the smaller direction, then we will assume the worst case and disable use of timestamps.
6192 *
6193 */
6194 uint64_t delta, peer_delta, delta_up;
6195
6196 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts;
6197 if (delta < bbr_min_usec_delta) {
6198 /*
6199 * Have not seen a min amount of time
6200 * between our send times so we can
6201 * make a determination of the timestamp
6202 * yet.
6203 */
6204 return;
6205 }
6206 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp;
6207 if (peer_delta < bbr_min_peer_delta) {
6208 /*
6209 * We may have enough in the form of
6210 * our delta but the peers number
6211 * has not changed that much. It could
6212 * be its clock ratio is such that
6213 * we need more data (10ms tick) or
6214 * there may be other compression scenarios
6215 * going on. In any event we need the
6216 * spread to be larger.
6217 */
6218 return;
6219 }
6220 /* Ok lets first see which way our delta is going */
6221 if (peer_delta > delta) {
6222 /* Very unlikely, the peer without
6223 * compensation shows that it saw
6224 * the two sends arrive further apart
6225 * then we saw then in micro-seconds.
6226 */
6227 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) {
6228 /* well it looks like the peer is a micro-second clock. */
6229 bbr->rc_ts_clock_set = 1;
6230 bbr->r_ctl.bbr_peer_tsratio = 1;
6231 } else {
6232 bbr->rc_ts_cant_be_used = 1;
6233 bbr->rc_ts_clock_set = 1;
6234 }
6235 return;
6236 }
6237 /* Ok we know that the peer_delta is smaller than our send distance */
6238 bbr->rc_ts_clock_set = 1;
6239 /* First question is it within the percentage that they are using usec time? */
6240 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent;
6241 if ((peer_delta + delta_up) >= delta) {
6242 /* Its a usec clock */
6243 bbr->r_ctl.bbr_peer_tsratio = 1;
6244 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6245 return;
6246 }
6247 /* Ok if not usec, what about 10usec (though unlikely)? */
6248 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent;
6249 if (((peer_delta * 10) + delta_up) >= delta) {
6250 bbr->r_ctl.bbr_peer_tsratio = 10;
6251 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6252 return;
6253 }
6254 /* And what about 100usec (though again unlikely)? */
6255 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent;
6256 if (((peer_delta * 100) + delta_up) >= delta) {
6257 bbr->r_ctl.bbr_peer_tsratio = 100;
6258 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6259 return;
6260 }
6261 /* And how about 1 msec (the most likely one)? */
6262 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent;
6263 if (((peer_delta * 1000) + delta_up) >= delta) {
6264 bbr->r_ctl.bbr_peer_tsratio = 1000;
6265 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6266 return;
6267 }
6268 /* Ok if not msec could it be 10 msec? */
6269 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent;
6270 if (((peer_delta * 10000) + delta_up) >= delta) {
6271 bbr->r_ctl.bbr_peer_tsratio = 10000;
6272 return;
6273 }
6274 /* If we fall down here the clock tick so slowly we can't use it */
6275 bbr->rc_ts_cant_be_used = 1;
6276 bbr->r_ctl.bbr_peer_tsratio = 0;
6277 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6278 }
6279
6280 /*
6281 * Collect new round-trip time estimate
6282 * and update averages and current timeout.
6283 */
6284 static void
tcp_bbr_xmit_timer_commit(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t cts)6285 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts)
6286 {
6287 int32_t delta;
6288 uint32_t rtt, tsin;
6289 int32_t rtt_ticks;
6290
6291 if (bbr->rtt_valid == 0)
6292 /* No valid sample */
6293 return;
6294
6295 rtt = bbr->r_ctl.cur_rtt;
6296 tsin = bbr->r_ctl.ts_in;
6297 if (bbr->rc_prtt_set_ts) {
6298 /*
6299 * We are to force feed the rttProp filter due
6300 * to an entry into PROBE_RTT. This assures
6301 * that the times are sync'd between when we
6302 * go into PROBE_RTT and the filter expiration.
6303 *
6304 * Google does not use a true filter, so they do
6305 * this implicitly since they only keep one value
6306 * and when they enter probe-rtt they update the
6307 * value to the newest rtt.
6308 */
6309 uint32_t rtt_prop;
6310
6311 bbr->rc_prtt_set_ts = 0;
6312 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
6313 if (rtt > rtt_prop)
6314 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts);
6315 else
6316 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6317 }
6318 if (bbr->rc_ack_was_delayed)
6319 rtt += bbr->r_ctl.rc_ack_hdwr_delay;
6320
6321 if (rtt < bbr->r_ctl.rc_lowest_rtt)
6322 bbr->r_ctl.rc_lowest_rtt = rtt;
6323 bbr_log_rtt_sample(bbr, rtt, tsin);
6324 if (bbr->r_init_rtt) {
6325 /*
6326 * The initial rtt is not-trusted, nuke it and lets get
6327 * our first valid measurement in.
6328 */
6329 bbr->r_init_rtt = 0;
6330 tp->t_srtt = 0;
6331 }
6332 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) {
6333 /*
6334 * So we have not yet figured out
6335 * what the peers TSTMP value is
6336 * in (most likely ms). We need a
6337 * series of cum-ack's to determine
6338 * this reliably.
6339 */
6340 if (bbr->rc_ack_is_cumack) {
6341 if (bbr->rc_ts_data_set) {
6342 /* Lets attempt to determine the timestamp granularity. */
6343 bbr_make_timestamp_determination(bbr);
6344 } else {
6345 bbr->rc_ts_data_set = 1;
6346 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts;
6347 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time;
6348 }
6349 } else {
6350 /*
6351 * We have to have consecutive acks
6352 * reset any "filled" state to none.
6353 */
6354 bbr->rc_ts_data_set = 0;
6355 }
6356 }
6357 /* Round it up */
6358 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1)));
6359 if (rtt_ticks == 0)
6360 rtt_ticks = 1;
6361 if (tp->t_srtt != 0) {
6362 /*
6363 * srtt is stored as fixed point with 5 bits after the
6364 * binary point (i.e., scaled by 8). The following magic is
6365 * equivalent to the smoothing algorithm in rfc793 with an
6366 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6367 * Adjust rtt to origin 0.
6368 */
6369
6370 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT)
6371 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6372
6373 tp->t_srtt += delta;
6374 if (tp->t_srtt <= 0)
6375 tp->t_srtt = 1;
6376
6377 /*
6378 * We accumulate a smoothed rtt variance (actually, a
6379 * smoothed mean difference), then set the retransmit timer
6380 * to smoothed rtt + 4 times the smoothed variance. rttvar
6381 * is stored as fixed point with 4 bits after the binary
6382 * point (scaled by 16). The following is equivalent to
6383 * rfc793 smoothing with an alpha of .75 (rttvar =
6384 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6385 * wired-in beta.
6386 */
6387 if (delta < 0)
6388 delta = -delta;
6389 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6390 tp->t_rttvar += delta;
6391 if (tp->t_rttvar <= 0)
6392 tp->t_rttvar = 1;
6393 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
6394 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6395 } else {
6396 /*
6397 * No rtt measurement yet - use the unsmoothed rtt. Set the
6398 * variance to half the rtt (so our first retransmit happens
6399 * at 3*rtt).
6400 */
6401 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT;
6402 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1);
6403 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6404 }
6405 KMOD_TCPSTAT_INC(tcps_rttupdated);
6406 tp->t_rttupdated++;
6407 #ifdef STATS
6408 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks));
6409 #endif
6410 /*
6411 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6412 * way we do the smoothing, srtt and rttvar will each average +1/2
6413 * tick of bias. When we compute the retransmit timer, we want 1/2
6414 * tick of rounding and 1 extra tick because of +-1/2 tick
6415 * uncertainty in the firing of the timer. The bias will give us
6416 * exactly the 1.5 tick we need. But, because the bias is
6417 * statistical, we have to test that we don't drop below the minimum
6418 * feasible timer (which is 2 ticks).
6419 */
6420 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6421 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2),
6422 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
6423
6424 /*
6425 * We received an ack for a packet that wasn't retransmitted; it is
6426 * probably safe to discard any error indications we've received
6427 * recently. This isn't quite right, but close enough for now (a
6428 * route might have failed after we sent a segment, and the return
6429 * path might not be symmetrical).
6430 */
6431 tp->t_softerror = 0;
6432 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
6433 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt)
6434 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt;
6435 }
6436
6437 static void
bbr_earlier_retran(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t t,uint32_t cts,int ack_type)6438 bbr_earlier_retran(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm,
6439 uint32_t t, uint32_t cts, int ack_type)
6440 {
6441 /*
6442 * For this RSM, we acknowledged the data from a previous
6443 * transmission, not the last one we made. This means we did a false
6444 * retransmit.
6445 */
6446 if (rsm->r_flags & BBR_HAS_FIN) {
6447 /*
6448 * The sending of the FIN often is multiple sent when we
6449 * have everything outstanding ack'd. We ignore this case
6450 * since its over now.
6451 */
6452 return;
6453 }
6454 if (rsm->r_flags & BBR_TLP) {
6455 /*
6456 * We expect TLP's to have this occur often
6457 */
6458 bbr->rc_tlp_rtx_out = 0;
6459 return;
6460 }
6461 if (ack_type != BBR_CUM_ACKED) {
6462 /*
6463 * If it was not a cum-ack we
6464 * don't really know for sure since
6465 * the timestamp could be from some
6466 * other transmission.
6467 */
6468 return;
6469 }
6470
6471 if (rsm->r_flags & BBR_WAS_SACKPASS) {
6472 /*
6473 * We retransmitted based on a sack and the earlier
6474 * retransmission ack'd it - re-ordering is occuring.
6475 */
6476 BBR_STAT_INC(bbr_reorder_seen);
6477 bbr->r_ctl.rc_reorder_ts = cts;
6478 }
6479 /* Back down the loss count */
6480 if (rsm->r_flags & BBR_MARKED_LOST) {
6481 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
6482 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
6483 rsm->r_flags &= ~BBR_MARKED_LOST;
6484 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
6485 /* LT sampling also needs adjustment */
6486 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
6487 }
6488 /***** RRS HERE ************************/
6489 /* Do we need to do this??? */
6490 /* bbr_reset_lt_bw_sampling(bbr, cts); */
6491 /***** RRS HERE ************************/
6492 BBR_STAT_INC(bbr_badfr);
6493 BBR_STAT_ADD(bbr_badfr_bytes, (rsm->r_end - rsm->r_start));
6494 }
6495
6496 static void
bbr_set_reduced_rtt(struct tcp_bbr * bbr,uint32_t cts,uint32_t line)6497 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line)
6498 {
6499 bbr->r_ctl.rc_rtt_shrinks = cts;
6500 if (bbr_can_force_probertt &&
6501 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
6502 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
6503 /*
6504 * We should enter probe-rtt its been too long
6505 * since we have been there.
6506 */
6507 bbr_enter_probe_rtt(bbr, cts, __LINE__);
6508 } else
6509 bbr_check_probe_rtt_limits(bbr, cts);
6510 }
6511
6512 static void
tcp_bbr_commit_bw(struct tcp_bbr * bbr,uint32_t cts)6513 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts)
6514 {
6515 uint64_t orig_bw;
6516
6517 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) {
6518 /* We never apply a zero measurment */
6519 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0,
6520 0, 0, 0, 0, 0, 0);
6521 return;
6522 }
6523 if (bbr->r_ctl.r_measurement_count < 0xffffffff)
6524 bbr->r_ctl.r_measurement_count++;
6525 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
6526 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch);
6527 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw,
6528 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate),
6529 0, 0, 0, 0, 0, 0);
6530 if (orig_bw &&
6531 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) {
6532 if (bbr->bbr_hdrw_pacing) {
6533 /*
6534 * Apply a new rate to the hardware
6535 * possibly.
6536 */
6537 bbr_update_hardware_pacing_rate(bbr, cts);
6538 }
6539 bbr_set_state_target(bbr, __LINE__);
6540 tcp_bbr_tso_size_check(bbr, cts);
6541 if (bbr->r_recovery_bw) {
6542 bbr_setup_red_bw(bbr, cts);
6543 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW);
6544 }
6545 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate))
6546 tcp_bbr_tso_size_check(bbr, cts);
6547 }
6548
6549 static void
bbr_nf_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6550 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6551 {
6552 if (bbr->rc_in_persist == 0) {
6553 /* We log only when not in persist */
6554 /* Translate to a Bytes Per Second */
6555 uint64_t tim, bw, ts_diff, ts_bw;
6556 uint32_t upper, lower, delivered;
6557
6558 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6559 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6560 else
6561 tim = 1;
6562 /*
6563 * Now that we have processed the tim (skipping the sample
6564 * or possibly updating the time, go ahead and
6565 * calculate the cdr.
6566 */
6567 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6568 bw = (uint64_t)delivered;
6569 bw *= (uint64_t)USECS_IN_SECOND;
6570 bw /= tim;
6571 if (bw == 0) {
6572 /* We must have a calculatable amount */
6573 return;
6574 }
6575 upper = (bw >> 32) & 0x00000000ffffffff;
6576 lower = bw & 0x00000000ffffffff;
6577 /*
6578 * If we are using this b/w shove it in now so we
6579 * can see in the trace viewer if it gets over-ridden.
6580 */
6581 if (rsm->r_ts_valid &&
6582 bbr->rc_ts_valid &&
6583 bbr->rc_ts_clock_set &&
6584 (bbr->rc_ts_cant_be_used == 0) &&
6585 bbr->rc_use_ts_limit) {
6586 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1);
6587 ts_diff *= bbr->r_ctl.bbr_peer_tsratio;
6588 if ((delivered == 0) ||
6589 (rtt < 1000)) {
6590 /* Can't use the ts */
6591 bbr_log_type_bbrupd(bbr, 61, cts,
6592 ts_diff,
6593 bbr->r_ctl.last_inbound_ts,
6594 rsm->r_del_ack_ts, 0,
6595 0, 0, 0, delivered);
6596 } else {
6597 ts_bw = (uint64_t)delivered;
6598 ts_bw *= (uint64_t)USECS_IN_SECOND;
6599 ts_bw /= ts_diff;
6600 bbr_log_type_bbrupd(bbr, 62, cts,
6601 (ts_bw >> 32),
6602 (ts_bw & 0xffffffff), 0, 0,
6603 0, 0, ts_diff, delivered);
6604 if ((bbr->ts_can_raise) &&
6605 (ts_bw > bw)) {
6606 bbr_log_type_bbrupd(bbr, 8, cts,
6607 delivered,
6608 ts_diff,
6609 (bw >> 32),
6610 (bw & 0x00000000ffffffff),
6611 0, 0, 0, 0);
6612 bw = ts_bw;
6613 } else if (ts_bw && (ts_bw < bw)) {
6614 bbr_log_type_bbrupd(bbr, 7, cts,
6615 delivered,
6616 ts_diff,
6617 (bw >> 32),
6618 (bw & 0x00000000ffffffff),
6619 0, 0, 0, 0);
6620 bw = ts_bw;
6621 }
6622 }
6623 }
6624 if (rsm->r_first_sent_time &&
6625 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6626 uint64_t sbw, sti;
6627 /*
6628 * We use what was in flight at the time of our
6629 * send and the size of this send to figure
6630 * out what we have been sending at (amount).
6631 * For the time we take from the time of
6632 * the send of the first send outstanding
6633 * until this send plus this sends pacing
6634 * time. This gives us a good calculation
6635 * as to the rate we have been sending at.
6636 */
6637
6638 sbw = (uint64_t)(rsm->r_flight_at_send);
6639 sbw *= (uint64_t)USECS_IN_SECOND;
6640 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6641 sti += rsm->r_pacing_delay;
6642 sbw /= sti;
6643 if (sbw < bw) {
6644 bbr_log_type_bbrupd(bbr, 6, cts,
6645 delivered,
6646 (uint32_t)sti,
6647 (bw >> 32),
6648 (uint32_t)bw,
6649 rsm->r_first_sent_time, 0, (sbw >> 32),
6650 (uint32_t)sbw);
6651 bw = sbw;
6652 }
6653 }
6654 /* Use the google algorithm for b/w measurements */
6655 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6656 if ((rsm->r_app_limited == 0) ||
6657 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) {
6658 tcp_bbr_commit_bw(bbr, cts);
6659 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6660 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6661 }
6662 }
6663 }
6664
6665 static void
bbr_google_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6666 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6667 {
6668 if (bbr->rc_in_persist == 0) {
6669 /* We log only when not in persist */
6670 /* Translate to a Bytes Per Second */
6671 uint64_t tim, bw;
6672 uint32_t upper, lower, delivered;
6673 int no_apply = 0;
6674
6675 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6676 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6677 else
6678 tim = 1;
6679 /*
6680 * Now that we have processed the tim (skipping the sample
6681 * or possibly updating the time, go ahead and
6682 * calculate the cdr.
6683 */
6684 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6685 bw = (uint64_t)delivered;
6686 bw *= (uint64_t)USECS_IN_SECOND;
6687 bw /= tim;
6688 if (tim < bbr->r_ctl.rc_lowest_rtt) {
6689 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6690 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6691
6692 no_apply = 1;
6693 }
6694 upper = (bw >> 32) & 0x00000000ffffffff;
6695 lower = bw & 0x00000000ffffffff;
6696 /*
6697 * If we are using this b/w shove it in now so we
6698 * can see in the trace viewer if it gets over-ridden.
6699 */
6700 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6701 /* Gate by the sending rate */
6702 if (rsm->r_first_sent_time &&
6703 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6704 uint64_t sbw, sti;
6705 /*
6706 * We use what was in flight at the time of our
6707 * send and the size of this send to figure
6708 * out what we have been sending at (amount).
6709 * For the time we take from the time of
6710 * the send of the first send outstanding
6711 * until this send plus this sends pacing
6712 * time. This gives us a good calculation
6713 * as to the rate we have been sending at.
6714 */
6715
6716 sbw = (uint64_t)(rsm->r_flight_at_send);
6717 sbw *= (uint64_t)USECS_IN_SECOND;
6718 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6719 sti += rsm->r_pacing_delay;
6720 sbw /= sti;
6721 if (sbw < bw) {
6722 bbr_log_type_bbrupd(bbr, 6, cts,
6723 delivered,
6724 (uint32_t)sti,
6725 (bw >> 32),
6726 (uint32_t)bw,
6727 rsm->r_first_sent_time, 0, (sbw >> 32),
6728 (uint32_t)sbw);
6729 bw = sbw;
6730 }
6731 if ((sti > tim) &&
6732 (sti < bbr->r_ctl.rc_lowest_rtt)) {
6733 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6734 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6735 no_apply = 1;
6736 } else
6737 no_apply = 0;
6738 }
6739 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6740 if ((no_apply == 0) &&
6741 ((rsm->r_app_limited == 0) ||
6742 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) {
6743 tcp_bbr_commit_bw(bbr, cts);
6744 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6745 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6746 }
6747 }
6748 }
6749
6750 static void
bbr_update_bbr_info(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts,uint32_t tsin,uint32_t uts,int32_t match,uint32_t rsm_send_time,int32_t ack_type,struct tcpopt * to)6751 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin,
6752 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to)
6753 {
6754 uint64_t old_rttprop;
6755
6756 /* Update our delivery time and amount */
6757 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start);
6758 bbr->r_ctl.rc_del_time = cts;
6759 if (rtt == 0) {
6760 /*
6761 * 0 means its a retransmit, for now we don't use these for
6762 * the rest of BBR.
6763 */
6764 return;
6765 }
6766 if ((bbr->rc_use_google == 0) &&
6767 (match != BBR_RTT_BY_EXACTMATCH) &&
6768 (match != BBR_RTT_BY_TIMESTAMP)){
6769 /*
6770 * We get a lot of rtt updates, lets not pay attention to
6771 * any that are not an exact match. That way we don't have
6772 * to worry about timestamps and the whole nonsense of
6773 * unsure if its a retransmission etc (if we ever had the
6774 * timestamp fixed to always have the last thing sent this
6775 * would not be a issue).
6776 */
6777 return;
6778 }
6779 if ((bbr_no_retran && bbr->rc_use_google) &&
6780 (match != BBR_RTT_BY_EXACTMATCH) &&
6781 (match != BBR_RTT_BY_TIMESTAMP)){
6782 /*
6783 * We only do measurements in google mode
6784 * with bbr_no_retran on for sure things.
6785 */
6786 return;
6787 }
6788 /* Only update srtt if we know by exact match */
6789 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin);
6790 if (ack_type == BBR_CUM_ACKED)
6791 bbr->rc_ack_is_cumack = 1;
6792 else
6793 bbr->rc_ack_is_cumack = 0;
6794 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP);
6795 /*
6796 * Note the following code differs to the original
6797 * BBR spec. It calls for <= not <. However after a
6798 * long discussion in email with Neal, he acknowledged
6799 * that it should be < than so that we will have flows
6800 * going into probe-rtt (we were seeing cases where that
6801 * did not happen and caused ugly things to occur). We
6802 * have added this agreed upon fix to our code base.
6803 */
6804 if (rtt < old_rttprop) {
6805 /* Update when we last saw a rtt drop */
6806 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0);
6807 bbr_set_reduced_rtt(bbr, cts, __LINE__);
6808 }
6809 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts,
6810 match, rsm->r_start, rsm->r_flags);
6811 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6812 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) {
6813 /*
6814 * The RTT-prop moved, reset the target (may be a
6815 * nop for some states).
6816 */
6817 bbr_set_state_target(bbr, __LINE__);
6818 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)
6819 bbr_log_rtt_shrinks(bbr, cts, 0, 0,
6820 __LINE__, BBR_RTTS_NEW_TARGET, 0);
6821 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP))
6822 /* It went up */
6823 bbr_check_probe_rtt_limits(bbr, cts);
6824 }
6825 if ((bbr->rc_use_google == 0) &&
6826 (match == BBR_RTT_BY_TIMESTAMP)) {
6827 /*
6828 * We don't do b/w update with
6829 * these since they are not really
6830 * reliable.
6831 */
6832 return;
6833 }
6834 if (bbr->r_ctl.r_app_limited_until &&
6835 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) {
6836 /* We are no longer app-limited */
6837 bbr->r_ctl.r_app_limited_until = 0;
6838 }
6839 if (bbr->rc_use_google) {
6840 bbr_google_measurement(bbr, rsm, rtt, cts);
6841 } else {
6842 bbr_nf_measurement(bbr, rsm, rtt, cts);
6843 }
6844 }
6845
6846 /*
6847 * Convert a timestamp that the main stack
6848 * uses (milliseconds) into one that bbr uses
6849 * (microseconds). Return that converted timestamp.
6850 */
6851 static uint32_t
bbr_ts_convert(uint32_t cts)6852 bbr_ts_convert(uint32_t cts) {
6853 uint32_t sec, msec;
6854
6855 sec = cts / MS_IN_USEC;
6856 msec = cts - (MS_IN_USEC * sec);
6857 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC));
6858 }
6859
6860 /*
6861 * Return 0 if we did not update the RTT time, return
6862 * 1 if we did.
6863 */
6864 static int
bbr_update_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,struct tcpopt * to,uint32_t cts,int32_t ack_type,uint32_t th_ack)6865 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr,
6866 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack)
6867 {
6868 int32_t i;
6869 uint32_t t, uts = 0;
6870
6871 if ((rsm->r_flags & BBR_ACKED) ||
6872 (rsm->r_flags & BBR_WAS_RENEGED) ||
6873 (rsm->r_flags & BBR_RXT_CLEARED)) {
6874 /* Already done */
6875 return (0);
6876 }
6877 if (rsm->r_rtr_cnt == 1) {
6878 /*
6879 * Only one transmit. Hopefully the normal case.
6880 */
6881 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0]))
6882 t = cts - rsm->r_tim_lastsent[0];
6883 else
6884 t = 1;
6885 if ((int)t <= 0)
6886 t = 1;
6887 bbr->r_ctl.rc_last_rtt = t;
6888 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6889 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to);
6890 return (1);
6891 }
6892 /* Convert to usecs */
6893 if ((bbr_can_use_ts_for_rtt == 1) &&
6894 (bbr->rc_use_google == 1) &&
6895 (ack_type == BBR_CUM_ACKED) &&
6896 (to->to_flags & TOF_TS) &&
6897 (to->to_tsecr != 0)) {
6898 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr;
6899 if (t < 1)
6900 t = 1;
6901 t *= MS_IN_USEC;
6902 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6903 BBR_RTT_BY_TIMESTAMP,
6904 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)],
6905 ack_type, to);
6906 return (1);
6907 }
6908 uts = bbr_ts_convert(to->to_tsecr);
6909 if ((to->to_flags & TOF_TS) &&
6910 (to->to_tsecr != 0) &&
6911 (ack_type == BBR_CUM_ACKED) &&
6912 ((rsm->r_flags & BBR_OVERMAX) == 0)) {
6913 /*
6914 * Now which timestamp does it match? In this block the ACK
6915 * may be coming from a previous transmission.
6916 */
6917 uint32_t fudge;
6918
6919 fudge = BBR_TIMER_FUDGE;
6920 for (i = 0; i < rsm->r_rtr_cnt; i++) {
6921 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) &&
6922 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) {
6923 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6924 t = cts - rsm->r_tim_lastsent[i];
6925 else
6926 t = 1;
6927 if ((int)t <= 0)
6928 t = 1;
6929 bbr->r_ctl.rc_last_rtt = t;
6930 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING,
6931 rsm->r_tim_lastsent[i], ack_type, to);
6932 if ((i + 1) < rsm->r_rtr_cnt) {
6933 /* Likely */
6934 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
6935 } else if (rsm->r_flags & BBR_TLP) {
6936 bbr->rc_tlp_rtx_out = 0;
6937 }
6938 return (1);
6939 }
6940 }
6941 /* Fall through if we can't find a matching timestamp */
6942 }
6943 /*
6944 * Ok its a SACK block that we retransmitted. or a windows
6945 * machine without timestamps. We can tell nothing from the
6946 * time-stamp since its not there or the time the peer last
6947 * recieved a segment that moved forward its cum-ack point.
6948 *
6949 * Lets look at the last retransmit and see what we can tell
6950 * (with BBR for space we only keep 2 note we have to keep
6951 * at least 2 so the map can not be condensed more).
6952 */
6953 i = rsm->r_rtr_cnt - 1;
6954 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6955 t = cts - rsm->r_tim_lastsent[i];
6956 else
6957 goto not_sure;
6958 if (t < bbr->r_ctl.rc_lowest_rtt) {
6959 /*
6960 * We retransmitted and the ack came back in less
6961 * than the smallest rtt we have observed in the
6962 * windowed rtt. We most likey did an improper
6963 * retransmit as outlined in 4.2 Step 3 point 2 in
6964 * the rack-draft.
6965 *
6966 * Use the prior transmission to update all the
6967 * information as long as there is only one prior
6968 * transmission.
6969 */
6970 if ((rsm->r_flags & BBR_OVERMAX) == 0) {
6971 #ifdef BBR_INVARIANTS
6972 if (rsm->r_rtr_cnt == 1)
6973 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags);
6974 #endif
6975 i = rsm->r_rtr_cnt - 2;
6976 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6977 t = cts - rsm->r_tim_lastsent[i];
6978 else
6979 t = 1;
6980 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET,
6981 rsm->r_tim_lastsent[i], ack_type, to);
6982 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
6983 } else {
6984 /*
6985 * Too many prior transmissions, just
6986 * updated BBR delivered
6987 */
6988 not_sure:
6989 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
6990 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
6991 }
6992 } else {
6993 /*
6994 * We retransmitted it and the retransmit did the
6995 * job.
6996 */
6997 if (rsm->r_flags & BBR_TLP)
6998 bbr->rc_tlp_rtx_out = 0;
6999 if ((rsm->r_flags & BBR_OVERMAX) == 0)
7000 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts,
7001 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to);
7002 else
7003 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
7004 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
7005 return (1);
7006 }
7007 return (0);
7008 }
7009
7010 /*
7011 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
7012 */
7013 static void
bbr_log_sack_passed(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm)7014 bbr_log_sack_passed(struct tcpcb *tp,
7015 struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
7016 {
7017 struct bbr_sendmap *nrsm;
7018
7019 nrsm = rsm;
7020 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap,
7021 bbr_head, r_tnext) {
7022 if (nrsm == rsm) {
7023 /* Skip orginal segment he is acked */
7024 continue;
7025 }
7026 if (nrsm->r_flags & BBR_ACKED) {
7027 /* Skip ack'd segments */
7028 continue;
7029 }
7030 if (nrsm->r_flags & BBR_SACK_PASSED) {
7031 /*
7032 * We found one that is already marked
7033 * passed, we have been here before and
7034 * so all others below this are marked.
7035 */
7036 break;
7037 }
7038 BBR_STAT_INC(bbr_sack_passed);
7039 nrsm->r_flags |= BBR_SACK_PASSED;
7040 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) &&
7041 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) {
7042 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start;
7043 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start;
7044 nrsm->r_flags |= BBR_MARKED_LOST;
7045 }
7046 nrsm->r_flags &= ~BBR_WAS_SACKPASS;
7047 }
7048 }
7049
7050 /*
7051 * Returns the number of bytes that were
7052 * newly ack'd by sack blocks.
7053 */
7054 static uint32_t
bbr_proc_sack_blk(struct tcpcb * tp,struct tcp_bbr * bbr,struct sackblk * sack,struct tcpopt * to,struct bbr_sendmap ** prsm,uint32_t cts)7055 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack,
7056 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts)
7057 {
7058 int32_t times = 0;
7059 uint32_t start, end, maxseg, changed = 0;
7060 struct bbr_sendmap *rsm, *nrsm;
7061 int32_t used_ref = 1;
7062 uint8_t went_back = 0, went_fwd = 0;
7063
7064 maxseg = tp->t_maxseg - bbr->rc_last_options;
7065 start = sack->start;
7066 end = sack->end;
7067 rsm = *prsm;
7068 if (rsm == NULL)
7069 used_ref = 0;
7070
7071 /* Do we locate the block behind where we last were? */
7072 if (rsm && SEQ_LT(start, rsm->r_start)) {
7073 went_back = 1;
7074 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
7075 if (SEQ_GEQ(start, rsm->r_start) &&
7076 SEQ_LT(start, rsm->r_end)) {
7077 goto do_rest_ofb;
7078 }
7079 }
7080 }
7081 start_at_beginning:
7082 went_fwd = 1;
7083 /*
7084 * Ok lets locate the block where this guy is fwd from rsm (if its
7085 * set)
7086 */
7087 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) {
7088 if (SEQ_GEQ(start, rsm->r_start) &&
7089 SEQ_LT(start, rsm->r_end)) {
7090 break;
7091 }
7092 }
7093 do_rest_ofb:
7094 if (rsm == NULL) {
7095 /*
7096 * This happens when we get duplicate sack blocks with the
7097 * same end. For example SACK 4: 100 SACK 3: 100 The sort
7098 * will not change there location so we would just start at
7099 * the end of the first one and get lost.
7100 */
7101 if (tp->t_flags & TF_SENTFIN) {
7102 /*
7103 * Check to see if we have not logged the FIN that
7104 * went out.
7105 */
7106 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7107 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
7108 /*
7109 * Ok we did not get the FIN logged.
7110 */
7111 nrsm->r_end++;
7112 rsm = nrsm;
7113 goto do_rest_ofb;
7114 }
7115 }
7116 if (times == 1) {
7117 #ifdef BBR_INVARIANTS
7118 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p",
7119 tp, bbr, sack, to, prsm);
7120 #else
7121 goto out;
7122 #endif
7123 }
7124 times++;
7125 BBR_STAT_INC(bbr_sack_proc_restart);
7126 rsm = NULL;
7127 goto start_at_beginning;
7128 }
7129 /* Ok we have an ACK for some piece of rsm */
7130 if (rsm->r_start != start) {
7131 /*
7132 * Need to split this in two pieces the before and after.
7133 */
7134 if (bbr_sack_mergable(rsm, start, end))
7135 nrsm = bbr_alloc_full_limit(bbr);
7136 else
7137 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7138 if (nrsm == NULL) {
7139 /* We could not allocate ignore the sack */
7140 struct sackblk blk;
7141
7142 blk.start = start;
7143 blk.end = end;
7144 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7145 goto out;
7146 }
7147 bbr_clone_rsm(bbr, nrsm, rsm, start);
7148 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7149 if (rsm->r_in_tmap) {
7150 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7151 nrsm->r_in_tmap = 1;
7152 }
7153 rsm->r_flags &= (~BBR_HAS_FIN);
7154 rsm = nrsm;
7155 }
7156 if (SEQ_GEQ(end, rsm->r_end)) {
7157 /*
7158 * The end of this block is either beyond this guy or right
7159 * at this guy.
7160 */
7161 if ((rsm->r_flags & BBR_ACKED) == 0) {
7162 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7163 changed += (rsm->r_end - rsm->r_start);
7164 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7165 bbr_log_sack_passed(tp, bbr, rsm);
7166 if (rsm->r_flags & BBR_MARKED_LOST) {
7167 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7168 }
7169 /* Is Reordering occuring? */
7170 if (rsm->r_flags & BBR_SACK_PASSED) {
7171 BBR_STAT_INC(bbr_reorder_seen);
7172 bbr->r_ctl.rc_reorder_ts = cts;
7173 if (rsm->r_flags & BBR_MARKED_LOST) {
7174 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7175 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7176 /* LT sampling also needs adjustment */
7177 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7178 }
7179 }
7180 rsm->r_flags |= BBR_ACKED;
7181 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7182 if (rsm->r_in_tmap) {
7183 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7184 rsm->r_in_tmap = 0;
7185 }
7186 }
7187 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7188 if (end == rsm->r_end) {
7189 /* This block only - done */
7190 goto out;
7191 }
7192 /* There is more not coverend by this rsm move on */
7193 start = rsm->r_end;
7194 nrsm = TAILQ_NEXT(rsm, r_next);
7195 rsm = nrsm;
7196 times = 0;
7197 goto do_rest_ofb;
7198 }
7199 if (rsm->r_flags & BBR_ACKED) {
7200 /* Been here done that */
7201 goto out;
7202 }
7203 /* Ok we need to split off this one at the tail */
7204 if (bbr_sack_mergable(rsm, start, end))
7205 nrsm = bbr_alloc_full_limit(bbr);
7206 else
7207 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7208 if (nrsm == NULL) {
7209 /* failed XXXrrs what can we do but loose the sack info? */
7210 struct sackblk blk;
7211
7212 blk.start = start;
7213 blk.end = end;
7214 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7215 goto out;
7216 }
7217 /* Clone it */
7218 bbr_clone_rsm(bbr, nrsm, rsm, end);
7219 /* The sack block does not cover this guy fully */
7220 rsm->r_flags &= (~BBR_HAS_FIN);
7221 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7222 if (rsm->r_in_tmap) {
7223 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7224 nrsm->r_in_tmap = 1;
7225 }
7226 nrsm->r_dupack = 0;
7227 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7228 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7229 changed += (rsm->r_end - rsm->r_start);
7230 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7231 bbr_log_sack_passed(tp, bbr, rsm);
7232 /* Is Reordering occuring? */
7233 if (rsm->r_flags & BBR_MARKED_LOST) {
7234 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7235 }
7236 if (rsm->r_flags & BBR_SACK_PASSED) {
7237 BBR_STAT_INC(bbr_reorder_seen);
7238 bbr->r_ctl.rc_reorder_ts = cts;
7239 if (rsm->r_flags & BBR_MARKED_LOST) {
7240 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7241 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7242 /* LT sampling also needs adjustment */
7243 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7244 }
7245 }
7246 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7247 rsm->r_flags |= BBR_ACKED;
7248 if (rsm->r_in_tmap) {
7249 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7250 rsm->r_in_tmap = 0;
7251 }
7252 out:
7253 if (rsm && (rsm->r_flags & BBR_ACKED)) {
7254 /*
7255 * Now can we merge this newly acked
7256 * block with either the previous or
7257 * next block?
7258 */
7259 nrsm = TAILQ_NEXT(rsm, r_next);
7260 if (nrsm &&
7261 (nrsm->r_flags & BBR_ACKED)) {
7262 /* yep this and next can be merged */
7263 rsm = bbr_merge_rsm(bbr, rsm, nrsm);
7264 }
7265 /* Now what about the previous? */
7266 nrsm = TAILQ_PREV(rsm, bbr_head, r_next);
7267 if (nrsm &&
7268 (nrsm->r_flags & BBR_ACKED)) {
7269 /* yep the previous and this can be merged */
7270 rsm = bbr_merge_rsm(bbr, nrsm, rsm);
7271 }
7272 }
7273 if (used_ref == 0) {
7274 BBR_STAT_INC(bbr_sack_proc_all);
7275 } else {
7276 BBR_STAT_INC(bbr_sack_proc_short);
7277 }
7278 if (went_fwd && went_back) {
7279 BBR_STAT_INC(bbr_sack_search_both);
7280 } else if (went_fwd) {
7281 BBR_STAT_INC(bbr_sack_search_fwd);
7282 } else if (went_back) {
7283 BBR_STAT_INC(bbr_sack_search_back);
7284 }
7285 /* Save off where the next seq is */
7286 if (rsm)
7287 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
7288 else
7289 bbr->r_ctl.rc_sacklast = NULL;
7290 *prsm = rsm;
7291 return (changed);
7292 }
7293
7294 static void inline
bbr_peer_reneges(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,tcp_seq th_ack)7295 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack)
7296 {
7297 struct bbr_sendmap *tmap;
7298
7299 BBR_STAT_INC(bbr_reneges_seen);
7300 tmap = NULL;
7301 while (rsm && (rsm->r_flags & BBR_ACKED)) {
7302 /* Its no longer sacked, mark it so */
7303 uint32_t oflags;
7304 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7305 #ifdef BBR_INVARIANTS
7306 if (rsm->r_in_tmap) {
7307 panic("bbr:%p rsm:%p flags:0x%x in tmap?",
7308 bbr, rsm, rsm->r_flags);
7309 }
7310 #endif
7311 oflags = rsm->r_flags;
7312 if (rsm->r_flags & BBR_MARKED_LOST) {
7313 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7314 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7315 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7316 /* LT sampling also needs adjustment */
7317 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7318 }
7319 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST);
7320 rsm->r_flags |= BBR_WAS_RENEGED;
7321 rsm->r_flags |= BBR_RXT_CLEARED;
7322 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__);
7323 /* Rebuild it into our tmap */
7324 if (tmap == NULL) {
7325 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7326 tmap = rsm;
7327 } else {
7328 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7329 tmap = rsm;
7330 }
7331 tmap->r_in_tmap = 1;
7332 /*
7333 * XXXrrs Delivered? Should we do anything here?
7334 *
7335 * Of course we don't on a rxt timeout so maybe its ok that
7336 * we don't?
7337 *
7338 * For now lets not.
7339 */
7340 rsm = TAILQ_NEXT(rsm, r_next);
7341 }
7342 /*
7343 * Now lets possibly clear the sack filter so we start recognizing
7344 * sacks that cover this area.
7345 */
7346 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack);
7347 }
7348
7349 static void
bbr_log_syn(struct tcpcb * tp,struct tcpopt * to)7350 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to)
7351 {
7352 struct tcp_bbr *bbr;
7353 struct bbr_sendmap *rsm;
7354 uint32_t cts;
7355
7356 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7357 cts = bbr->r_ctl.rc_rcvtime;
7358 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7359 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) {
7360 if ((rsm->r_end - rsm->r_start) <= 1) {
7361 /* Log out the SYN completely */
7362 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7363 rsm->r_rtr_bytes = 0;
7364 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7365 if (rsm->r_in_tmap) {
7366 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7367 rsm->r_in_tmap = 0;
7368 }
7369 if (bbr->r_ctl.rc_next == rsm) {
7370 /* scoot along the marker */
7371 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7372 }
7373 if (to != NULL)
7374 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0);
7375 bbr_free(bbr, rsm);
7376 } else {
7377 /* There is more (Fast open)? strip out SYN. */
7378 rsm->r_flags &= ~BBR_HAS_SYN;
7379 rsm->r_start++;
7380 }
7381 }
7382 }
7383
7384 /*
7385 * Returns the number of bytes that were
7386 * acknowledged by SACK blocks.
7387 */
7388
7389 static uint32_t
bbr_log_ack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th,uint32_t * prev_acked)7390 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
7391 uint32_t *prev_acked)
7392 {
7393 uint32_t changed, last_seq, entered_recovery = 0;
7394 struct tcp_bbr *bbr;
7395 struct bbr_sendmap *rsm;
7396 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7397 register uint32_t th_ack;
7398 int32_t i, j, k, new_sb, num_sack_blks = 0;
7399 uint32_t cts, acked, ack_point, sack_changed = 0;
7400 uint32_t p_maxseg, maxseg, p_acked = 0;
7401
7402 INP_WLOCK_ASSERT(tp->t_inpcb);
7403 if (th->th_flags & TH_RST) {
7404 /* We don't log resets */
7405 return (0);
7406 }
7407 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7408 cts = bbr->r_ctl.rc_rcvtime;
7409
7410 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7411 changed = 0;
7412 maxseg = tp->t_maxseg - bbr->rc_last_options;
7413 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg);
7414 th_ack = th->th_ack;
7415 if (SEQ_GT(th_ack, tp->snd_una)) {
7416 acked = th_ack - tp->snd_una;
7417 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__);
7418 bbr->rc_tp->t_acktime = ticks;
7419 } else
7420 acked = 0;
7421 if (SEQ_LEQ(th_ack, tp->snd_una)) {
7422 /* Only sent here for sack processing */
7423 goto proc_sack;
7424 }
7425 if (rsm && SEQ_GT(th_ack, rsm->r_start)) {
7426 changed = th_ack - rsm->r_start;
7427 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) {
7428 /*
7429 * For the SYN incoming case we will not have called
7430 * tcp_output for the sending of the SYN, so there will be
7431 * no map. All other cases should probably be a panic.
7432 */
7433 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) {
7434 /*
7435 * We have a timestamp that can be used to generate
7436 * an initial RTT.
7437 */
7438 uint32_t ts, now, rtt;
7439
7440 ts = bbr_ts_convert(to->to_tsecr);
7441 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv));
7442 rtt = now - ts;
7443 if (rtt < 1)
7444 rtt = 1;
7445 bbr_log_type_bbrrttprop(bbr, rtt,
7446 tp->iss, 0, cts,
7447 BBR_RTT_BY_TIMESTAMP, tp->iss, 0);
7448 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
7449 changed = 1;
7450 bbr->r_wanted_output = 1;
7451 goto out;
7452 }
7453 goto proc_sack;
7454 } else if (rsm == NULL) {
7455 goto out;
7456 }
7457 if (changed) {
7458 /*
7459 * The ACK point is advancing to th_ack, we must drop off
7460 * the packets in the rack log and calculate any eligble
7461 * RTT's.
7462 */
7463 bbr->r_wanted_output = 1;
7464 more:
7465 if (rsm == NULL) {
7466 if (tp->t_flags & TF_SENTFIN) {
7467 /* if we send a FIN we will not hav a map */
7468 goto proc_sack;
7469 }
7470 #ifdef BBR_INVARIANTS
7471 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n",
7472 tp,
7473 th, tp->t_state, bbr,
7474 tp->snd_una, tp->snd_max, changed);
7475 #endif
7476 goto proc_sack;
7477 }
7478 }
7479 if (SEQ_LT(th_ack, rsm->r_start)) {
7480 /* Huh map is missing this */
7481 #ifdef BBR_INVARIANTS
7482 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n",
7483 rsm->r_start,
7484 th_ack, tp->t_state,
7485 bbr->r_state, bbr);
7486 panic("th-ack is bad bbr:%p tp:%p", bbr, tp);
7487 #endif
7488 goto proc_sack;
7489 } else if (th_ack == rsm->r_start) {
7490 /* None here to ack */
7491 goto proc_sack;
7492 }
7493 /*
7494 * Clear the dup ack counter, it will
7495 * either be freed or if there is some
7496 * remaining we need to start it at zero.
7497 */
7498 rsm->r_dupack = 0;
7499 /* Now do we consume the whole thing? */
7500 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7501 /* Its all consumed. */
7502 uint32_t left;
7503
7504 if (rsm->r_flags & BBR_ACKED) {
7505 /*
7506 * It was acked on the scoreboard -- remove it from
7507 * total
7508 */
7509 p_acked += (rsm->r_end - rsm->r_start);
7510 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7511 if (bbr->r_ctl.rc_sacked == 0)
7512 bbr->r_ctl.rc_sacklast = NULL;
7513 } else {
7514 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack);
7515 if (rsm->r_flags & BBR_MARKED_LOST) {
7516 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7517 }
7518 if (rsm->r_flags & BBR_SACK_PASSED) {
7519 /*
7520 * There are acked segments ACKED on the
7521 * scoreboard further up. We are seeing
7522 * reordering.
7523 */
7524 BBR_STAT_INC(bbr_reorder_seen);
7525 bbr->r_ctl.rc_reorder_ts = cts;
7526 if (rsm->r_flags & BBR_MARKED_LOST) {
7527 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7528 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7529 /* LT sampling also needs adjustment */
7530 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7531 }
7532 }
7533 rsm->r_flags &= ~BBR_MARKED_LOST;
7534 }
7535 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7536 rsm->r_rtr_bytes = 0;
7537 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7538 if (rsm->r_in_tmap) {
7539 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7540 rsm->r_in_tmap = 0;
7541 }
7542 if (bbr->r_ctl.rc_next == rsm) {
7543 /* scoot along the marker */
7544 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7545 }
7546 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7547 /* Adjust the packet counts */
7548 left = th_ack - rsm->r_end;
7549 /* Free back to zone */
7550 bbr_free(bbr, rsm);
7551 if (left) {
7552 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7553 goto more;
7554 }
7555 goto proc_sack;
7556 }
7557 if (rsm->r_flags & BBR_ACKED) {
7558 /*
7559 * It was acked on the scoreboard -- remove it from total
7560 * for the part being cum-acked.
7561 */
7562 p_acked += (rsm->r_end - rsm->r_start);
7563 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7564 if (bbr->r_ctl.rc_sacked == 0)
7565 bbr->r_ctl.rc_sacklast = NULL;
7566 } else {
7567 /*
7568 * It was acked up to th_ack point for the first time
7569 */
7570 struct bbr_sendmap lrsm;
7571
7572 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap));
7573 lrsm.r_end = th_ack;
7574 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack);
7575 }
7576 if ((rsm->r_flags & BBR_MARKED_LOST) &&
7577 ((rsm->r_flags & BBR_ACKED) == 0)) {
7578 /*
7579 * It was marked lost and partly ack'd now
7580 * for the first time. We lower the rc_lost_bytes
7581 * and still leave it MARKED.
7582 */
7583 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start;
7584 }
7585 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7586 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7587 rsm->r_rtr_bytes = 0;
7588 /* adjust packet count */
7589 rsm->r_start = th_ack;
7590 proc_sack:
7591 /* Check for reneging */
7592 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7593 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) {
7594 /*
7595 * The peer has moved snd_una up to the edge of this send,
7596 * i.e. one that it had previously acked. The only way that
7597 * can be true if the peer threw away data (space issues)
7598 * that it had previously sacked (else it would have given
7599 * us snd_una up to (rsm->r_end). We need to undo the acked
7600 * markings here.
7601 *
7602 * Note we have to look to make sure th_ack is our
7603 * rsm->r_start in case we get an old ack where th_ack is
7604 * behind snd_una.
7605 */
7606 bbr_peer_reneges(bbr, rsm, th->th_ack);
7607 }
7608 if ((to->to_flags & TOF_SACK) == 0) {
7609 /* We are done nothing left to log */
7610 goto out;
7611 }
7612 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7613 if (rsm) {
7614 last_seq = rsm->r_end;
7615 } else {
7616 last_seq = tp->snd_max;
7617 }
7618 /* Sack block processing */
7619 if (SEQ_GT(th_ack, tp->snd_una))
7620 ack_point = th_ack;
7621 else
7622 ack_point = tp->snd_una;
7623 for (i = 0; i < to->to_nsacks; i++) {
7624 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7625 &sack, sizeof(sack));
7626 sack.start = ntohl(sack.start);
7627 sack.end = ntohl(sack.end);
7628 if (SEQ_GT(sack.end, sack.start) &&
7629 SEQ_GT(sack.start, ack_point) &&
7630 SEQ_LT(sack.start, tp->snd_max) &&
7631 SEQ_GT(sack.end, ack_point) &&
7632 SEQ_LEQ(sack.end, tp->snd_max)) {
7633 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) &&
7634 (SEQ_LT(sack.end, last_seq)) &&
7635 ((sack.end - sack.start) < (p_maxseg / 8))) {
7636 /*
7637 * Not the last piece and its smaller than
7638 * 1/8th of a p_maxseg. We ignore this.
7639 */
7640 BBR_STAT_INC(bbr_runt_sacks);
7641 continue;
7642 }
7643 sack_blocks[num_sack_blks] = sack;
7644 num_sack_blks++;
7645 #ifdef NETFLIX_STATS
7646 } else if (SEQ_LEQ(sack.start, th_ack) &&
7647 SEQ_LEQ(sack.end, th_ack)) {
7648 /*
7649 * Its a D-SACK block.
7650 */
7651 tcp_record_dsack(sack.start, sack.end);
7652 #endif
7653 }
7654 }
7655 if (num_sack_blks == 0)
7656 goto out;
7657 /*
7658 * Sort the SACK blocks so we can update the rack scoreboard with
7659 * just one pass.
7660 */
7661 new_sb = sack_filter_blks(&bbr->r_ctl.bbr_sf, sack_blocks,
7662 num_sack_blks, th->th_ack);
7663 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks);
7664 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks);
7665 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb));
7666 num_sack_blks = new_sb;
7667 if (num_sack_blks < 2) {
7668 goto do_sack_work;
7669 }
7670 /* Sort the sacks */
7671 for (i = 0; i < num_sack_blks; i++) {
7672 for (j = i + 1; j < num_sack_blks; j++) {
7673 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7674 sack = sack_blocks[i];
7675 sack_blocks[i] = sack_blocks[j];
7676 sack_blocks[j] = sack;
7677 }
7678 }
7679 }
7680 /*
7681 * Now are any of the sack block ends the same (yes some
7682 * implememtations send these)?
7683 */
7684 again:
7685 if (num_sack_blks > 1) {
7686 for (i = 0; i < num_sack_blks; i++) {
7687 for (j = i + 1; j < num_sack_blks; j++) {
7688 if (sack_blocks[i].end == sack_blocks[j].end) {
7689 /*
7690 * Ok these two have the same end we
7691 * want the smallest end and then
7692 * throw away the larger and start
7693 * again.
7694 */
7695 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7696 /*
7697 * The second block covers
7698 * more area use that
7699 */
7700 sack_blocks[i].start = sack_blocks[j].start;
7701 }
7702 /*
7703 * Now collapse out the dup-sack and
7704 * lower the count
7705 */
7706 for (k = (j + 1); k < num_sack_blks; k++) {
7707 sack_blocks[j].start = sack_blocks[k].start;
7708 sack_blocks[j].end = sack_blocks[k].end;
7709 j++;
7710 }
7711 num_sack_blks--;
7712 goto again;
7713 }
7714 }
7715 }
7716 }
7717 do_sack_work:
7718 rsm = bbr->r_ctl.rc_sacklast;
7719 for (i = 0; i < num_sack_blks; i++) {
7720 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts);
7721 if (acked) {
7722 bbr->r_wanted_output = 1;
7723 changed += acked;
7724 sack_changed += acked;
7725 }
7726 }
7727 out:
7728 *prev_acked = p_acked;
7729 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
7730 /*
7731 * Ok we have a high probability that we need to go in to
7732 * recovery since we have data sack'd
7733 */
7734 struct bbr_sendmap *rsm;
7735
7736 rsm = bbr_check_recovery_mode(tp, bbr, cts);
7737 if (rsm) {
7738 /* Enter recovery */
7739 entered_recovery = 1;
7740 bbr->r_wanted_output = 1;
7741 /*
7742 * When we enter recovery we need to assure we send
7743 * one packet.
7744 */
7745 if (bbr->r_ctl.rc_resend == NULL) {
7746 bbr->r_ctl.rc_resend = rsm;
7747 }
7748 }
7749 }
7750 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
7751 /*
7752 * See if we need to rack-retransmit anything if so set it
7753 * up as the thing to resend assuming something else is not
7754 * already in that position.
7755 */
7756 if (bbr->r_ctl.rc_resend == NULL) {
7757 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
7758 }
7759 }
7760 /*
7761 * We return the amount that changed via sack, this is used by the
7762 * ack-received code to augment what was changed between th_ack <->
7763 * snd_una.
7764 */
7765 return (sack_changed);
7766 }
7767
7768 static void
bbr_strike_dupack(struct tcp_bbr * bbr)7769 bbr_strike_dupack(struct tcp_bbr *bbr)
7770 {
7771 struct bbr_sendmap *rsm;
7772
7773 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
7774 if (rsm && (rsm->r_dupack < 0xff)) {
7775 rsm->r_dupack++;
7776 if (rsm->r_dupack >= DUP_ACK_THRESHOLD)
7777 bbr->r_wanted_output = 1;
7778 }
7779 }
7780
7781 /*
7782 * Return value of 1, we do not need to call bbr_process_data().
7783 * return value of 0, bbr_process_data can be called.
7784 * For ret_val if its 0 the TCB is locked and valid, if its non-zero
7785 * its unlocked and probably unsafe to touch the TCB.
7786 */
7787 static int
bbr_process_ack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,uint32_t tiwin,int32_t tlen,int32_t * ofia,int32_t thflags,int32_t * ret_val)7788 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
7789 struct tcpcb *tp, struct tcpopt *to,
7790 uint32_t tiwin, int32_t tlen,
7791 int32_t * ofia, int32_t thflags, int32_t * ret_val)
7792 {
7793 int32_t ourfinisacked = 0;
7794 int32_t acked_amount;
7795 uint16_t nsegs;
7796 int32_t acked;
7797 uint32_t lost, sack_changed = 0;
7798 struct mbuf *mfree;
7799 struct tcp_bbr *bbr;
7800 uint32_t prev_acked = 0;
7801
7802 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7803 lost = bbr->r_ctl.rc_lost;
7804 nsegs = max(1, m->m_pkthdr.lro_nsegs);
7805 if (SEQ_GT(th->th_ack, tp->snd_max)) {
7806 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7807 bbr->r_wanted_output = 1;
7808 return (1);
7809 }
7810 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
7811 /* Process the ack */
7812 if (bbr->rc_in_persist)
7813 tp->t_rxtshift = 0;
7814 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
7815 bbr_strike_dupack(bbr);
7816 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
7817 }
7818 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost));
7819 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
7820 /*
7821 * Old ack, behind the last one rcv'd or a duplicate ack
7822 * with SACK info.
7823 */
7824 if (th->th_ack == tp->snd_una) {
7825 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0);
7826 if (bbr->r_state == TCPS_SYN_SENT) {
7827 /*
7828 * Special case on where we sent SYN. When
7829 * the SYN-ACK is processed in syn_sent
7830 * state it bumps the snd_una. This causes
7831 * us to hit here even though we did ack 1
7832 * byte.
7833 *
7834 * Go through the nothing left case so we
7835 * send data.
7836 */
7837 goto nothing_left;
7838 }
7839 }
7840 return (0);
7841 }
7842 /*
7843 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
7844 * something we sent.
7845 */
7846 if (tp->t_flags & TF_NEEDSYN) {
7847 /*
7848 * T/TCP: Connection was half-synchronized, and our SYN has
7849 * been ACK'd (so connection is now fully synchronized). Go
7850 * to non-starred state, increment snd_una for ACK of SYN,
7851 * and check if we can do window scaling.
7852 */
7853 tp->t_flags &= ~TF_NEEDSYN;
7854 tp->snd_una++;
7855 /* Do window scaling? */
7856 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
7857 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
7858 tp->rcv_scale = tp->request_r_scale;
7859 /* Send window already scaled. */
7860 }
7861 }
7862 INP_WLOCK_ASSERT(tp->t_inpcb);
7863
7864 acked = BYTES_THIS_ACK(tp, th);
7865 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
7866 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
7867
7868 /*
7869 * If we just performed our first retransmit, and the ACK arrives
7870 * within our recovery window, then it was a mistake to do the
7871 * retransmit in the first place. Recover our original cwnd and
7872 * ssthresh, and proceed to transmit where we left off.
7873 */
7874 if (tp->t_flags & TF_PREVVALID) {
7875 tp->t_flags &= ~TF_PREVVALID;
7876 if (tp->t_rxtshift == 1 &&
7877 (int)(ticks - tp->t_badrxtwin) < 0)
7878 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
7879 }
7880 SOCKBUF_LOCK(&so->so_snd);
7881 acked_amount = min(acked, (int)sbavail(&so->so_snd));
7882 tp->snd_wnd -= acked_amount;
7883 mfree = sbcut_locked(&so->so_snd, acked_amount);
7884 SOCKBUF_UNLOCK(&so->so_snd);
7885 tp->t_flags |= TF_WAKESOW;
7886 m_freem(mfree);
7887 if (SEQ_GT(th->th_ack, tp->snd_una)) {
7888 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
7889 }
7890 tp->snd_una = th->th_ack;
7891 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost));
7892 if (IN_RECOVERY(tp->t_flags)) {
7893 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
7894 (SEQ_LT(th->th_ack, tp->snd_max))) {
7895 tcp_bbr_partialack(tp);
7896 } else {
7897 bbr_post_recovery(tp);
7898 }
7899 }
7900 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
7901 tp->snd_recover = tp->snd_una;
7902 }
7903 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
7904 tp->snd_nxt = tp->snd_max;
7905 }
7906 if (tp->snd_una == tp->snd_max) {
7907 /* Nothing left outstanding */
7908 nothing_left:
7909 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
7910 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
7911 bbr->rc_tp->t_acktime = 0;
7912 if ((sbused(&so->so_snd) == 0) &&
7913 (tp->t_flags & TF_SENTFIN)) {
7914 ourfinisacked = 1;
7915 }
7916 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
7917 if (bbr->rc_in_persist == 0) {
7918 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
7919 }
7920 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
7921 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
7922 /*
7923 * We invalidate the last ack here since we
7924 * don't want to transfer forward the time
7925 * for our sum's calculations.
7926 */
7927 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7928 (sbavail(&so->so_snd) == 0) &&
7929 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
7930 /*
7931 * The socket was gone and the peer sent data, time
7932 * to reset him.
7933 */
7934 *ret_val = 1;
7935 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
7936 /* tcp_close will kill the inp pre-log the Reset */
7937 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
7938 tp = tcp_close(tp);
7939 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
7940 BBR_STAT_INC(bbr_dropped_af_data);
7941 return (1);
7942 }
7943 /* Set need output so persist might get set */
7944 bbr->r_wanted_output = 1;
7945 }
7946 if (ofia)
7947 *ofia = ourfinisacked;
7948 return (0);
7949 }
7950
7951 static void
bbr_enter_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)7952 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
7953 {
7954 if (bbr->rc_in_persist == 0) {
7955 bbr_timer_cancel(bbr, __LINE__, cts);
7956 bbr->r_ctl.rc_last_delay_val = 0;
7957 tp->t_rxtshift = 0;
7958 bbr->rc_in_persist = 1;
7959 bbr->r_ctl.rc_went_idle_time = cts;
7960 /* We should be capped when rw went to 0 but just in case */
7961 bbr_log_type_pesist(bbr, cts, 0, line, 1);
7962 /* Time freezes for the state, so do the accounting now */
7963 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
7964 uint32_t time_in;
7965
7966 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
7967 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
7968 int32_t idx;
7969
7970 idx = bbr_state_val(bbr);
7971 counter_u64_add(bbr_state_time[(idx + 5)], time_in);
7972 } else {
7973 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
7974 }
7975 }
7976 bbr->r_ctl.rc_bbr_state_time = cts;
7977 }
7978 }
7979
7980 static void
bbr_restart_after_idle(struct tcp_bbr * bbr,uint32_t cts,uint32_t idle_time)7981 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time)
7982 {
7983 /*
7984 * Note that if idle time does not exceed our
7985 * threshold, we do nothing continuing the state
7986 * transitions we were last walking through.
7987 */
7988 if (idle_time >= bbr_idle_restart_threshold) {
7989 if (bbr->rc_use_idle_restart) {
7990 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT;
7991 /*
7992 * Set our target using BBR_UNIT, so
7993 * we increase at a dramatic rate but
7994 * we stop when we get the pipe
7995 * full again for our current b/w estimate.
7996 */
7997 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
7998 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
7999 bbr_set_state_target(bbr, __LINE__);
8000 /* Now setup our gains to ramp up */
8001 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
8002 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
8003 bbr_log_type_statechange(bbr, cts, __LINE__);
8004 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
8005 bbr_substate_change(bbr, cts, __LINE__, 1);
8006 }
8007 }
8008 }
8009
8010 static void
bbr_exit_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)8011 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
8012 {
8013 uint32_t idle_time;
8014
8015 if (bbr->rc_in_persist == 0)
8016 return;
8017 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time);
8018 bbr->rc_in_persist = 0;
8019 bbr->rc_hit_state_1 = 0;
8020 bbr->r_ctl.rc_del_time = cts;
8021 /*
8022 * We invalidate the last ack here since we
8023 * don't want to transfer forward the time
8024 * for our sum's calculations.
8025 */
8026 if (bbr->rc_inp->inp_in_hpts) {
8027 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
8028 bbr->rc_timer_first = 0;
8029 bbr->r_ctl.rc_hpts_flags = 0;
8030 bbr->r_ctl.rc_last_delay_val = 0;
8031 bbr->r_ctl.rc_hptsi_agg_delay = 0;
8032 bbr->r_agg_early_set = 0;
8033 bbr->r_ctl.rc_agg_early = 0;
8034 }
8035 bbr_log_type_pesist(bbr, cts, idle_time, line, 0);
8036 if (idle_time >= bbr_rtt_probe_time) {
8037 /*
8038 * This qualifies as a RTT_PROBE session since we drop the
8039 * data outstanding to nothing and waited more than
8040 * bbr_rtt_probe_time.
8041 */
8042 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0);
8043 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts;
8044 }
8045 tp->t_rxtshift = 0;
8046 /*
8047 * If in probeBW and we have persisted more than an RTT lets do
8048 * special handling.
8049 */
8050 /* Force a time based epoch */
8051 bbr_set_epoch(bbr, cts, __LINE__);
8052 /*
8053 * Setup the lost so we don't count anything against the guy
8054 * we have been stuck with during persists.
8055 */
8056 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
8057 /* Time un-freezes for the state */
8058 bbr->r_ctl.rc_bbr_state_time = cts;
8059 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) ||
8060 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) {
8061 /*
8062 * If we are going back to probe-bw
8063 * or probe_rtt, we may need to possibly
8064 * do a fast restart.
8065 */
8066 bbr_restart_after_idle(bbr, cts, idle_time);
8067 }
8068 }
8069
8070 static void
bbr_collapsed_window(struct tcp_bbr * bbr)8071 bbr_collapsed_window(struct tcp_bbr *bbr)
8072 {
8073 /*
8074 * Now we must walk the
8075 * send map and divide the
8076 * ones left stranded. These
8077 * guys can't cause us to abort
8078 * the connection and are really
8079 * "unsent". However if a buggy
8080 * client actually did keep some
8081 * of the data i.e. collapsed the win
8082 * and refused to ack and then opened
8083 * the win and acked that data. We would
8084 * get into an ack war, the simplier
8085 * method then of just pretending we
8086 * did not send those segments something
8087 * won't work.
8088 */
8089 struct bbr_sendmap *rsm, *nrsm;
8090 tcp_seq max_seq;
8091 uint32_t maxseg;
8092 int can_split = 0;
8093 int fnd = 0;
8094
8095 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
8096 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd;
8097 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0);
8098 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
8099 /* Find the first seq past or at maxseq */
8100 if (rsm->r_flags & BBR_RWND_COLLAPSED)
8101 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8102 if (SEQ_GEQ(max_seq, rsm->r_start) &&
8103 SEQ_GEQ(rsm->r_end, max_seq)) {
8104 fnd = 1;
8105 break;
8106 }
8107 }
8108 bbr->rc_has_collapsed = 0;
8109 if (!fnd) {
8110 /* Nothing to do strange */
8111 return;
8112 }
8113 /*
8114 * Now can we split?
8115 *
8116 * We don't want to split if splitting
8117 * would generate too many small segments
8118 * less we let an attacker fragment our
8119 * send_map and leave us out of memory.
8120 */
8121 if ((max_seq != rsm->r_start) &&
8122 (max_seq != rsm->r_end)){
8123 /* can we split? */
8124 int res1, res2;
8125
8126 res1 = max_seq - rsm->r_start;
8127 res2 = rsm->r_end - max_seq;
8128 if ((res1 >= (maxseg/8)) &&
8129 (res2 >= (maxseg/8))) {
8130 /* No small pieces here */
8131 can_split = 1;
8132 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) {
8133 /* We are under the limit */
8134 can_split = 1;
8135 }
8136 }
8137 /* Ok do we need to split this rsm? */
8138 if (max_seq == rsm->r_start) {
8139 /* It's this guy no split required */
8140 nrsm = rsm;
8141 } else if (max_seq == rsm->r_end) {
8142 /* It's the next one no split required. */
8143 nrsm = TAILQ_NEXT(rsm, r_next);
8144 if (nrsm == NULL) {
8145 /* Huh? */
8146 return;
8147 }
8148 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) {
8149 /* yep we need to split it */
8150 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
8151 if (nrsm == NULL) {
8152 /* failed XXXrrs what can we do mark the whole? */
8153 nrsm = rsm;
8154 goto no_split;
8155 }
8156 /* Clone it */
8157 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0);
8158 bbr_clone_rsm(bbr, nrsm, rsm, max_seq);
8159 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
8160 if (rsm->r_in_tmap) {
8161 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8162 nrsm->r_in_tmap = 1;
8163 }
8164 } else {
8165 /*
8166 * Split not allowed just start here just
8167 * use this guy.
8168 */
8169 nrsm = rsm;
8170 }
8171 no_split:
8172 BBR_STAT_INC(bbr_collapsed_win);
8173 /* reuse fnd as a count */
8174 fnd = 0;
8175 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) {
8176 nrsm->r_flags |= BBR_RWND_COLLAPSED;
8177 fnd++;
8178 bbr->rc_has_collapsed = 1;
8179 }
8180 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd);
8181 }
8182
8183 static void
bbr_un_collapse_window(struct tcp_bbr * bbr)8184 bbr_un_collapse_window(struct tcp_bbr *bbr)
8185 {
8186 struct bbr_sendmap *rsm;
8187 int cleared = 0;
8188
8189 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
8190 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
8191 /* Clear the flag */
8192 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8193 cleared++;
8194 } else
8195 break;
8196 }
8197 bbr_log_type_rwnd_collapse(bbr,
8198 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared);
8199 bbr->rc_has_collapsed = 0;
8200 }
8201
8202 /*
8203 * Return value of 1, the TCB is unlocked and most
8204 * likely gone, return value of 0, the TCB is still
8205 * locked.
8206 */
8207 static int
bbr_process_data(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt)8208 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8209 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8210 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8211 {
8212 /*
8213 * Update window information. Don't look at window if no ACK: TAC's
8214 * send garbage on first SYN.
8215 */
8216 uint16_t nsegs;
8217 int32_t tfo_syn;
8218 struct tcp_bbr *bbr;
8219
8220 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8221 INP_WLOCK_ASSERT(tp->t_inpcb);
8222 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8223 if ((thflags & TH_ACK) &&
8224 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8225 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8226 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8227 /* keep track of pure window updates */
8228 if (tlen == 0 &&
8229 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8230 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8231 tp->snd_wnd = tiwin;
8232 tp->snd_wl1 = th->th_seq;
8233 tp->snd_wl2 = th->th_ack;
8234 if (tp->snd_wnd > tp->max_sndwnd)
8235 tp->max_sndwnd = tp->snd_wnd;
8236 bbr->r_wanted_output = 1;
8237 } else if (thflags & TH_ACK) {
8238 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8239 tp->snd_wnd = tiwin;
8240 tp->snd_wl1 = th->th_seq;
8241 tp->snd_wl2 = th->th_ack;
8242 }
8243 }
8244 if (tp->snd_wnd < ctf_outstanding(tp))
8245 /* The peer collapsed its window on us */
8246 bbr_collapsed_window(bbr);
8247 else if (bbr->rc_has_collapsed)
8248 bbr_un_collapse_window(bbr);
8249 /* Was persist timer active and now we have window space? */
8250 if ((bbr->rc_in_persist != 0) &&
8251 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8252 bbr_minseg(bbr)))) {
8253 /*
8254 * Make the rate persist at end of persist mode if idle long
8255 * enough
8256 */
8257 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8258
8259 /* Make sure we output to start the timer */
8260 bbr->r_wanted_output = 1;
8261 }
8262 /* Do we need to enter persist? */
8263 if ((bbr->rc_in_persist == 0) &&
8264 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8265 TCPS_HAVEESTABLISHED(tp->t_state) &&
8266 (tp->snd_max == tp->snd_una) &&
8267 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8268 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8269 /* No send window.. we must enter persist */
8270 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8271 }
8272 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8273 m_freem(m);
8274 return (0);
8275 }
8276 /*
8277 * We don't support urgent data but
8278 * drag along the up just to make sure
8279 * if there is a stack switch no one
8280 * is surprised.
8281 */
8282 tp->rcv_up = tp->rcv_nxt;
8283 INP_WLOCK_ASSERT(tp->t_inpcb);
8284
8285 /*
8286 * Process the segment text, merging it into the TCP sequencing
8287 * queue, and arranging for acknowledgment of receipt if necessary.
8288 * This process logically involves adjusting tp->rcv_wnd as data is
8289 * presented to the user (this happens in tcp_usrreq.c, case
8290 * PRU_RCVD). If a FIN has already been received on this connection
8291 * then we just ignore the text.
8292 */
8293 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8294 IS_FASTOPEN(tp->t_flags));
8295 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
8296 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8297 tcp_seq save_start = th->th_seq;
8298 tcp_seq save_rnxt = tp->rcv_nxt;
8299 int save_tlen = tlen;
8300
8301 m_adj(m, drop_hdrlen); /* delayed header drop */
8302 /*
8303 * Insert segment which includes th into TCP reassembly
8304 * queue with control block tp. Set thflags to whether
8305 * reassembly now includes a segment with FIN. This handles
8306 * the common case inline (segment is the next to be
8307 * received on an established connection, and the queue is
8308 * empty), avoiding linkage into and removal from the queue
8309 * and repetition of various conversions. Set DELACK for
8310 * segments received in order, but ack immediately when
8311 * segments are out of order (so fast retransmit can work).
8312 */
8313 if (th->th_seq == tp->rcv_nxt &&
8314 SEGQ_EMPTY(tp) &&
8315 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8316 tfo_syn)) {
8317 #ifdef NETFLIX_SB_LIMITS
8318 u_int mcnt, appended;
8319
8320 if (so->so_rcv.sb_shlim) {
8321 mcnt = m_memcnt(m);
8322 appended = 0;
8323 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8324 CFO_NOSLEEP, NULL) == false) {
8325 counter_u64_add(tcp_sb_shlim_fails, 1);
8326 m_freem(m);
8327 return (0);
8328 }
8329 }
8330
8331 #endif
8332 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) {
8333 bbr->bbr_segs_rcvd += max(1, nsegs);
8334 tp->t_flags |= TF_DELACK;
8335 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8336 } else {
8337 bbr->r_wanted_output = 1;
8338 tp->t_flags |= TF_ACKNOW;
8339 }
8340 tp->rcv_nxt += tlen;
8341 if (tlen &&
8342 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8343 (tp->t_fbyte_in == 0)) {
8344 tp->t_fbyte_in = ticks;
8345 if (tp->t_fbyte_in == 0)
8346 tp->t_fbyte_in = 1;
8347 if (tp->t_fbyte_out && tp->t_fbyte_in)
8348 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8349 }
8350 thflags = th->th_flags & TH_FIN;
8351 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8352 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8353 SOCKBUF_LOCK(&so->so_rcv);
8354 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
8355 m_freem(m);
8356 else
8357 #ifdef NETFLIX_SB_LIMITS
8358 appended =
8359 #endif
8360 sbappendstream_locked(&so->so_rcv, m, 0);
8361 SOCKBUF_UNLOCK(&so->so_rcv);
8362 tp->t_flags |= TF_WAKESOR;
8363 #ifdef NETFLIX_SB_LIMITS
8364 if (so->so_rcv.sb_shlim && appended != mcnt)
8365 counter_fo_release(so->so_rcv.sb_shlim,
8366 mcnt - appended);
8367 #endif
8368 } else {
8369 /*
8370 * XXX: Due to the header drop above "th" is
8371 * theoretically invalid by now. Fortunately
8372 * m_adj() doesn't actually frees any mbufs when
8373 * trimming from the head.
8374 */
8375 tcp_seq temp = save_start;
8376 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8377 tp->t_flags |= TF_ACKNOW;
8378 }
8379 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
8380 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8381 /*
8382 * DSACK actually handled in the fastpath
8383 * above.
8384 */
8385 tcp_update_sack_list(tp, save_start,
8386 save_start + save_tlen);
8387 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8388 if ((tp->rcv_numsacks >= 1) &&
8389 (tp->sackblks[0].end == save_start)) {
8390 /*
8391 * Partial overlap, recorded at todrop
8392 * above.
8393 */
8394 tcp_update_sack_list(tp,
8395 tp->sackblks[0].start,
8396 tp->sackblks[0].end);
8397 } else {
8398 tcp_update_dsack_list(tp, save_start,
8399 save_start + save_tlen);
8400 }
8401 } else if (tlen >= save_tlen) {
8402 /* Update of sackblks. */
8403 tcp_update_dsack_list(tp, save_start,
8404 save_start + save_tlen);
8405 } else if (tlen > 0) {
8406 tcp_update_dsack_list(tp, save_start,
8407 save_start + tlen);
8408 }
8409 }
8410 } else {
8411 m_freem(m);
8412 thflags &= ~TH_FIN;
8413 }
8414
8415 /*
8416 * If FIN is received ACK the FIN and let the user know that the
8417 * connection is closing.
8418 */
8419 if (thflags & TH_FIN) {
8420 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8421 socantrcvmore(so);
8422 /* The socket upcall is handled by socantrcvmore. */
8423 tp->t_flags &= ~TF_WAKESOR;
8424 /*
8425 * If connection is half-synchronized (ie NEEDSYN
8426 * flag on) then delay ACK, so it may be piggybacked
8427 * when SYN is sent. Otherwise, since we received a
8428 * FIN then no more input can be expected, send ACK
8429 * now.
8430 */
8431 if (tp->t_flags & TF_NEEDSYN) {
8432 tp->t_flags |= TF_DELACK;
8433 bbr_timer_cancel(bbr,
8434 __LINE__, bbr->r_ctl.rc_rcvtime);
8435 } else {
8436 tp->t_flags |= TF_ACKNOW;
8437 }
8438 tp->rcv_nxt++;
8439 }
8440 switch (tp->t_state) {
8441 /*
8442 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8443 * CLOSE_WAIT state.
8444 */
8445 case TCPS_SYN_RECEIVED:
8446 tp->t_starttime = ticks;
8447 /* FALLTHROUGH */
8448 case TCPS_ESTABLISHED:
8449 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8450 break;
8451
8452 /*
8453 * If still in FIN_WAIT_1 STATE FIN has not been
8454 * acked so enter the CLOSING state.
8455 */
8456 case TCPS_FIN_WAIT_1:
8457 tcp_state_change(tp, TCPS_CLOSING);
8458 break;
8459
8460 /*
8461 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8462 * starting the time-wait timer, turning off the
8463 * other standard timers.
8464 */
8465 case TCPS_FIN_WAIT_2:
8466 bbr->rc_timer_first = 1;
8467 bbr_timer_cancel(bbr,
8468 __LINE__, bbr->r_ctl.rc_rcvtime);
8469 INP_WLOCK_ASSERT(tp->t_inpcb);
8470 tcp_twstart(tp);
8471 return (1);
8472 }
8473 }
8474 /*
8475 * Return any desired output.
8476 */
8477 if ((tp->t_flags & TF_ACKNOW) ||
8478 (sbavail(&so->so_snd) > ctf_outstanding(tp))) {
8479 bbr->r_wanted_output = 1;
8480 }
8481 INP_WLOCK_ASSERT(tp->t_inpcb);
8482 return (0);
8483 }
8484
8485 /*
8486 * Here nothing is really faster, its just that we
8487 * have broken out the fast-data path also just like
8488 * the fast-ack. Return 1 if we processed the packet
8489 * return 0 if you need to take the "slow-path".
8490 */
8491 static int
bbr_do_fastnewdata(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt)8492 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8493 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8494 uint32_t tiwin, int32_t nxt_pkt)
8495 {
8496 uint16_t nsegs;
8497 int32_t newsize = 0; /* automatic sockbuf scaling */
8498 struct tcp_bbr *bbr;
8499 #ifdef NETFLIX_SB_LIMITS
8500 u_int mcnt, appended;
8501 #endif
8502 #ifdef TCPDEBUG
8503 /*
8504 * The size of tcp_saveipgen must be the size of the max ip header,
8505 * now IPv6.
8506 */
8507 u_char tcp_saveipgen[IP6_HDR_LEN];
8508 struct tcphdr tcp_savetcp;
8509 short ostate = 0;
8510
8511 #endif
8512 /* On the hpts and we would have called output */
8513 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8514
8515 /*
8516 * If last ACK falls within this segment's sequence numbers, record
8517 * the timestamp. NOTE that the test is modified according to the
8518 * latest proposal of the [email protected] list (Braden 1993/04/26).
8519 */
8520 if (bbr->r_ctl.rc_resend != NULL) {
8521 return (0);
8522 }
8523 if (tiwin && tiwin != tp->snd_wnd) {
8524 return (0);
8525 }
8526 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8527 return (0);
8528 }
8529 if (__predict_false((to->to_flags & TOF_TS) &&
8530 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8531 return (0);
8532 }
8533 if (__predict_false((th->th_ack != tp->snd_una))) {
8534 return (0);
8535 }
8536 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8537 return (0);
8538 }
8539 if ((to->to_flags & TOF_TS) != 0 &&
8540 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8541 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
8542 tp->ts_recent = to->to_tsval;
8543 }
8544 /*
8545 * This is a pure, in-sequence data packet with nothing on the
8546 * reassembly queue and we have enough buffer space to take it.
8547 */
8548 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8549
8550 #ifdef NETFLIX_SB_LIMITS
8551 if (so->so_rcv.sb_shlim) {
8552 mcnt = m_memcnt(m);
8553 appended = 0;
8554 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8555 CFO_NOSLEEP, NULL) == false) {
8556 counter_u64_add(tcp_sb_shlim_fails, 1);
8557 m_freem(m);
8558 return (1);
8559 }
8560 }
8561 #endif
8562 /* Clean receiver SACK report if present */
8563 if (tp->rcv_numsacks)
8564 tcp_clean_sackreport(tp);
8565 KMOD_TCPSTAT_INC(tcps_preddat);
8566 tp->rcv_nxt += tlen;
8567 if (tlen &&
8568 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8569 (tp->t_fbyte_in == 0)) {
8570 tp->t_fbyte_in = ticks;
8571 if (tp->t_fbyte_in == 0)
8572 tp->t_fbyte_in = 1;
8573 if (tp->t_fbyte_out && tp->t_fbyte_in)
8574 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8575 }
8576 /*
8577 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8578 */
8579 tp->snd_wl1 = th->th_seq;
8580 /*
8581 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8582 */
8583 tp->rcv_up = tp->rcv_nxt;
8584 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8585 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8586 #ifdef TCPDEBUG
8587 if (so->so_options & SO_DEBUG)
8588 tcp_trace(TA_INPUT, ostate, tp,
8589 (void *)tcp_saveipgen, &tcp_savetcp, 0);
8590 #endif
8591 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8592
8593 /* Add data to socket buffer. */
8594 SOCKBUF_LOCK(&so->so_rcv);
8595 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8596 m_freem(m);
8597 } else {
8598 /*
8599 * Set new socket buffer size. Give up when limit is
8600 * reached.
8601 */
8602 if (newsize)
8603 if (!sbreserve_locked(&so->so_rcv,
8604 newsize, so, NULL))
8605 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8606 m_adj(m, drop_hdrlen); /* delayed header drop */
8607
8608 #ifdef NETFLIX_SB_LIMITS
8609 appended =
8610 #endif
8611 sbappendstream_locked(&so->so_rcv, m, 0);
8612 ctf_calc_rwin(so, tp);
8613 }
8614 SOCKBUF_UNLOCK(&so->so_rcv);
8615 tp->t_flags |= TF_WAKESOR;
8616 #ifdef NETFLIX_SB_LIMITS
8617 if (so->so_rcv.sb_shlim && mcnt != appended)
8618 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8619 #endif
8620 if (DELAY_ACK(tp, bbr, nsegs)) {
8621 bbr->bbr_segs_rcvd += max(1, nsegs);
8622 tp->t_flags |= TF_DELACK;
8623 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8624 } else {
8625 bbr->r_wanted_output = 1;
8626 tp->t_flags |= TF_ACKNOW;
8627 }
8628 return (1);
8629 }
8630
8631 /*
8632 * This subfunction is used to try to highly optimize the
8633 * fast path. We again allow window updates that are
8634 * in sequence to remain in the fast-path. We also add
8635 * in the __predict's to attempt to help the compiler.
8636 * Note that if we return a 0, then we can *not* process
8637 * it and the caller should push the packet into the
8638 * slow-path. If we return 1, then all is well and
8639 * the packet is fully processed.
8640 */
8641 static int
bbr_fastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt,uint8_t iptos)8642 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8643 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8644 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
8645 {
8646 int32_t acked;
8647 uint16_t nsegs;
8648 uint32_t sack_changed;
8649 #ifdef TCPDEBUG
8650 /*
8651 * The size of tcp_saveipgen must be the size of the max ip header,
8652 * now IPv6.
8653 */
8654 u_char tcp_saveipgen[IP6_HDR_LEN];
8655 struct tcphdr tcp_savetcp;
8656 short ostate = 0;
8657
8658 #endif
8659 uint32_t prev_acked = 0;
8660 struct tcp_bbr *bbr;
8661
8662 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8663 /* Old ack, behind (or duplicate to) the last one rcv'd */
8664 return (0);
8665 }
8666 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8667 /* Above what we have sent? */
8668 return (0);
8669 }
8670 if (__predict_false(tiwin == 0)) {
8671 /* zero window */
8672 return (0);
8673 }
8674 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8675 /* We need a SYN or a FIN, unlikely.. */
8676 return (0);
8677 }
8678 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8679 /* Timestamp is behind .. old ack with seq wrap? */
8680 return (0);
8681 }
8682 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8683 /* Still recovering */
8684 return (0);
8685 }
8686 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8687 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) {
8688 /* We are retransmitting */
8689 return (0);
8690 }
8691 if (__predict_false(bbr->rc_in_persist != 0)) {
8692 /* In persist mode */
8693 return (0);
8694 }
8695 if (bbr->r_ctl.rc_sacked) {
8696 /* We have sack holes on our scoreboard */
8697 return (0);
8698 }
8699 /* Ok if we reach here, we can process a fast-ack */
8700 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8701 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
8702 /*
8703 * We never detect loss in fast ack [we can't
8704 * have a sack and can't be in recovery so
8705 * we always pass 0 (nothing detected)].
8706 */
8707 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0);
8708 /* Did the window get updated? */
8709 if (tiwin != tp->snd_wnd) {
8710 tp->snd_wnd = tiwin;
8711 tp->snd_wl1 = th->th_seq;
8712 if (tp->snd_wnd > tp->max_sndwnd)
8713 tp->max_sndwnd = tp->snd_wnd;
8714 }
8715 /* Do we need to exit persists? */
8716 if ((bbr->rc_in_persist != 0) &&
8717 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8718 bbr_minseg(bbr)))) {
8719 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8720 bbr->r_wanted_output = 1;
8721 }
8722 /* Do we need to enter persists? */
8723 if ((bbr->rc_in_persist == 0) &&
8724 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8725 TCPS_HAVEESTABLISHED(tp->t_state) &&
8726 (tp->snd_max == tp->snd_una) &&
8727 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8728 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8729 /* No send window.. we must enter persist */
8730 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8731 }
8732 /*
8733 * If last ACK falls within this segment's sequence numbers, record
8734 * the timestamp. NOTE that the test is modified according to the
8735 * latest proposal of the [email protected] list (Braden 1993/04/26).
8736 */
8737 if ((to->to_flags & TOF_TS) != 0 &&
8738 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8739 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime;
8740 tp->ts_recent = to->to_tsval;
8741 }
8742 /*
8743 * This is a pure ack for outstanding data.
8744 */
8745 KMOD_TCPSTAT_INC(tcps_predack);
8746
8747 /*
8748 * "bad retransmit" recovery.
8749 */
8750 if (tp->t_flags & TF_PREVVALID) {
8751 tp->t_flags &= ~TF_PREVVALID;
8752 if (tp->t_rxtshift == 1 &&
8753 (int)(ticks - tp->t_badrxtwin) < 0)
8754 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
8755 }
8756 /*
8757 * Recalculate the transmit timer / rtt.
8758 *
8759 * Some boxes send broken timestamp replies during the SYN+ACK
8760 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8761 * and blow up the retransmit timer.
8762 */
8763 acked = BYTES_THIS_ACK(tp, th);
8764
8765 #ifdef TCP_HHOOK
8766 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
8767 hhook_run_tcp_est_in(tp, th, to);
8768 #endif
8769
8770 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
8771 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8772 sbdrop(&so->so_snd, acked);
8773
8774 if (SEQ_GT(th->th_ack, tp->snd_una))
8775 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8776 tp->snd_una = th->th_ack;
8777 if (tp->snd_wnd < ctf_outstanding(tp))
8778 /* The peer collapsed its window on us */
8779 bbr_collapsed_window(bbr);
8780 else if (bbr->rc_has_collapsed)
8781 bbr_un_collapse_window(bbr);
8782
8783 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8784 tp->snd_recover = tp->snd_una;
8785 }
8786 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0);
8787 /*
8788 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
8789 */
8790 tp->snd_wl2 = th->th_ack;
8791 m_freem(m);
8792 /*
8793 * If all outstanding data are acked, stop retransmit timer,
8794 * otherwise restart timer using current (possibly backed-off)
8795 * value. If process is waiting for space, wakeup/selwakeup/signal.
8796 * If data are ready to send, let tcp_output decide between more
8797 * output or persist.
8798 */
8799 #ifdef TCPDEBUG
8800 if (so->so_options & SO_DEBUG)
8801 tcp_trace(TA_INPUT, ostate, tp,
8802 (void *)tcp_saveipgen,
8803 &tcp_savetcp, 0);
8804 #endif
8805 /* Wake up the socket if we have room to write more */
8806 tp->t_flags |= TF_WAKESOW;
8807 if (tp->snd_una == tp->snd_max) {
8808 /* Nothing left outstanding */
8809 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8810 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
8811 bbr->rc_tp->t_acktime = 0;
8812 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8813 if (bbr->rc_in_persist == 0) {
8814 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8815 }
8816 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8817 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8818 /*
8819 * We invalidate the last ack here since we
8820 * don't want to transfer forward the time
8821 * for our sum's calculations.
8822 */
8823 bbr->r_wanted_output = 1;
8824 }
8825 if (sbavail(&so->so_snd)) {
8826 bbr->r_wanted_output = 1;
8827 }
8828 return (1);
8829 }
8830
8831 /*
8832 * Return value of 1, the TCB is unlocked and most
8833 * likely gone, return value of 0, the TCB is still
8834 * locked.
8835 */
8836 static int
bbr_do_syn_sent(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)8837 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
8838 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8839 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8840 {
8841 int32_t todrop;
8842 int32_t ourfinisacked = 0;
8843 struct tcp_bbr *bbr;
8844 int32_t ret_val = 0;
8845
8846 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8847 ctf_calc_rwin(so, tp);
8848 /*
8849 * If the state is SYN_SENT: if seg contains an ACK, but not for our
8850 * SYN, drop the input. if seg contains a RST, then drop the
8851 * connection. if seg does not contain SYN, then drop it. Otherwise
8852 * this is an acceptable SYN segment initialize tp->rcv_nxt and
8853 * tp->irs if seg contains ack then advance tp->snd_una. BRR does
8854 * not support ECN so we will not say we are capable. if SYN has
8855 * been acked change to ESTABLISHED else SYN_RCVD state arrange for
8856 * segment to be acked (eventually) continue processing rest of
8857 * data/controls, beginning with URG
8858 */
8859 if ((thflags & TH_ACK) &&
8860 (SEQ_LEQ(th->th_ack, tp->iss) ||
8861 SEQ_GT(th->th_ack, tp->snd_max))) {
8862 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8863 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8864 return (1);
8865 }
8866 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
8867 TCP_PROBE5(connect__refused, NULL, tp,
8868 mtod(m, const char *), tp, th);
8869 tp = tcp_drop(tp, ECONNREFUSED);
8870 ctf_do_drop(m, tp);
8871 return (1);
8872 }
8873 if (thflags & TH_RST) {
8874 ctf_do_drop(m, tp);
8875 return (1);
8876 }
8877 if (!(thflags & TH_SYN)) {
8878 ctf_do_drop(m, tp);
8879 return (1);
8880 }
8881 tp->irs = th->th_seq;
8882 tcp_rcvseqinit(tp);
8883 if (thflags & TH_ACK) {
8884 int tfo_partial = 0;
8885
8886 KMOD_TCPSTAT_INC(tcps_connects);
8887 soisconnected(so);
8888 #ifdef MAC
8889 mac_socketpeer_set_from_mbuf(m, so);
8890 #endif
8891 /* Do window scaling on this connection? */
8892 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
8893 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
8894 tp->rcv_scale = tp->request_r_scale;
8895 }
8896 tp->rcv_adv += min(tp->rcv_wnd,
8897 TCP_MAXWIN << tp->rcv_scale);
8898 /*
8899 * If not all the data that was sent in the TFO SYN
8900 * has been acked, resend the remainder right away.
8901 */
8902 if (IS_FASTOPEN(tp->t_flags) &&
8903 (tp->snd_una != tp->snd_max)) {
8904 tp->snd_nxt = th->th_ack;
8905 tfo_partial = 1;
8906 }
8907 /*
8908 * If there's data, delay ACK; if there's also a FIN ACKNOW
8909 * will be turned on later.
8910 */
8911 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) {
8912 bbr->bbr_segs_rcvd += 1;
8913 tp->t_flags |= TF_DELACK;
8914 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8915 } else {
8916 bbr->r_wanted_output = 1;
8917 tp->t_flags |= TF_ACKNOW;
8918 }
8919 if (SEQ_GT(th->th_ack, tp->iss)) {
8920 /*
8921 * The SYN is acked
8922 * handle it specially.
8923 */
8924 bbr_log_syn(tp, to);
8925 }
8926 if (SEQ_GT(th->th_ack, tp->snd_una)) {
8927 /*
8928 * We advance snd_una for the
8929 * fast open case. If th_ack is
8930 * acknowledging data beyond
8931 * snd_una we can't just call
8932 * ack-processing since the
8933 * data stream in our send-map
8934 * will start at snd_una + 1 (one
8935 * beyond the SYN). If its just
8936 * equal we don't need to do that
8937 * and there is no send_map.
8938 */
8939 tp->snd_una++;
8940 }
8941 /*
8942 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
8943 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
8944 */
8945 tp->t_starttime = ticks;
8946 if (tp->t_flags & TF_NEEDFIN) {
8947 tcp_state_change(tp, TCPS_FIN_WAIT_1);
8948 tp->t_flags &= ~TF_NEEDFIN;
8949 thflags &= ~TH_SYN;
8950 } else {
8951 tcp_state_change(tp, TCPS_ESTABLISHED);
8952 TCP_PROBE5(connect__established, NULL, tp,
8953 mtod(m, const char *), tp, th);
8954 cc_conn_init(tp);
8955 }
8956 } else {
8957 /*
8958 * Received initial SYN in SYN-SENT[*] state => simultaneous
8959 * open. If segment contains CC option and there is a
8960 * cached CC, apply TAO test. If it succeeds, connection is *
8961 * half-synchronized. Otherwise, do 3-way handshake:
8962 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
8963 * there was no CC option, clear cached CC value.
8964 */
8965 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
8966 tcp_state_change(tp, TCPS_SYN_RECEIVED);
8967 }
8968 INP_WLOCK_ASSERT(tp->t_inpcb);
8969 /*
8970 * Advance th->th_seq to correspond to first data byte. If data,
8971 * trim to stay within window, dropping FIN if necessary.
8972 */
8973 th->th_seq++;
8974 if (tlen > tp->rcv_wnd) {
8975 todrop = tlen - tp->rcv_wnd;
8976 m_adj(m, -todrop);
8977 tlen = tp->rcv_wnd;
8978 thflags &= ~TH_FIN;
8979 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
8980 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
8981 }
8982 tp->snd_wl1 = th->th_seq - 1;
8983 tp->rcv_up = th->th_seq;
8984 /*
8985 * Client side of transaction: already sent SYN and data. If the
8986 * remote host used T/TCP to validate the SYN, our data will be
8987 * ACK'd; if so, enter normal data segment processing in the middle
8988 * of step 5, ack processing. Otherwise, goto step 6.
8989 */
8990 if (thflags & TH_ACK) {
8991 if ((to->to_flags & TOF_TS) != 0) {
8992 uint32_t t, rtt;
8993
8994 t = tcp_tv_to_mssectick(&bbr->rc_tv);
8995 if (TSTMP_GEQ(t, to->to_tsecr)) {
8996 rtt = t - to->to_tsecr;
8997 if (rtt == 0) {
8998 rtt = 1;
8999 }
9000 rtt *= MS_IN_USEC;
9001 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9002 apply_filter_min_small(&bbr->r_ctl.rc_rttprop,
9003 rtt, bbr->r_ctl.rc_rcvtime);
9004 }
9005 }
9006 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
9007 return (ret_val);
9008 /* We may have changed to FIN_WAIT_1 above */
9009 if (tp->t_state == TCPS_FIN_WAIT_1) {
9010 /*
9011 * In FIN_WAIT_1 STATE in addition to the processing
9012 * for the ESTABLISHED state if our FIN is now
9013 * acknowledged then enter FIN_WAIT_2.
9014 */
9015 if (ourfinisacked) {
9016 /*
9017 * If we can't receive any more data, then
9018 * closing user can proceed. Starting the
9019 * timer is contrary to the specification,
9020 * but if we don't get a FIN we'll hang
9021 * forever.
9022 *
9023 * XXXjl: we should release the tp also, and
9024 * use a compressed state.
9025 */
9026 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9027 soisdisconnected(so);
9028 tcp_timer_activate(tp, TT_2MSL,
9029 (tcp_fast_finwait2_recycle ?
9030 tcp_finwait2_timeout :
9031 TP_MAXIDLE(tp)));
9032 }
9033 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9034 }
9035 }
9036 }
9037 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9038 tiwin, thflags, nxt_pkt));
9039 }
9040
9041 /*
9042 * Return value of 1, the TCB is unlocked and most
9043 * likely gone, return value of 0, the TCB is still
9044 * locked.
9045 */
9046 static int
bbr_do_syn_recv(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9047 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
9048 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9049 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9050 {
9051 int32_t ourfinisacked = 0;
9052 int32_t ret_val;
9053 struct tcp_bbr *bbr;
9054
9055 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9056 ctf_calc_rwin(so, tp);
9057 if ((thflags & TH_ACK) &&
9058 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
9059 SEQ_GT(th->th_ack, tp->snd_max))) {
9060 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9061 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9062 return (1);
9063 }
9064 if (IS_FASTOPEN(tp->t_flags)) {
9065 /*
9066 * When a TFO connection is in SYN_RECEIVED, the only valid
9067 * packets are the initial SYN, a retransmit/copy of the
9068 * initial SYN (possibly with a subset of the original
9069 * data), a valid ACK, a FIN, or a RST.
9070 */
9071 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
9072 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9073 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9074 return (1);
9075 } else if (thflags & TH_SYN) {
9076 /* non-initial SYN is ignored */
9077 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
9078 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
9079 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
9080 ctf_do_drop(m, NULL);
9081 return (0);
9082 }
9083 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
9084 ctf_do_drop(m, NULL);
9085 return (0);
9086 }
9087 }
9088 if ((thflags & TH_RST) ||
9089 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9090 return (ctf_process_rst(m, th, so, tp));
9091 /*
9092 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9093 * it's less than ts_recent, drop it.
9094 */
9095 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9096 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9097 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9098 return (ret_val);
9099 }
9100 /*
9101 * In the SYN-RECEIVED state, validate that the packet belongs to
9102 * this connection before trimming the data to fit the receive
9103 * window. Check the sequence number versus IRS since we know the
9104 * sequence numbers haven't wrapped. This is a partial fix for the
9105 * "LAND" DoS attack.
9106 */
9107 if (SEQ_LT(th->th_seq, tp->irs)) {
9108 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9109 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9110 return (1);
9111 }
9112 INP_WLOCK_ASSERT(tp->t_inpcb);
9113 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9114 return (ret_val);
9115 }
9116 /*
9117 * If last ACK falls within this segment's sequence numbers, record
9118 * its timestamp. NOTE: 1) That the test incorporates suggestions
9119 * from the latest proposal of the [email protected] list (Braden
9120 * 1993/04/26). 2) That updating only on newer timestamps interferes
9121 * with our earlier PAWS tests, so this check should be solely
9122 * predicated on the sequence space of this segment. 3) That we
9123 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9124 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9125 * SEG.Len, This modified check allows us to overcome RFC1323's
9126 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9127 * p.869. In such cases, we can still calculate the RTT correctly
9128 * when RCV.NXT == Last.ACK.Sent.
9129 */
9130 if ((to->to_flags & TOF_TS) != 0 &&
9131 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9132 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9133 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9134 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9135 tp->ts_recent = to->to_tsval;
9136 }
9137 tp->snd_wnd = tiwin;
9138 /*
9139 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9140 * is on (half-synchronized state), then queue data for later
9141 * processing; else drop segment and return.
9142 */
9143 if ((thflags & TH_ACK) == 0) {
9144 if (IS_FASTOPEN(tp->t_flags)) {
9145 cc_conn_init(tp);
9146 }
9147 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9148 tiwin, thflags, nxt_pkt));
9149 }
9150 KMOD_TCPSTAT_INC(tcps_connects);
9151 soisconnected(so);
9152 /* Do window scaling? */
9153 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9154 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9155 tp->rcv_scale = tp->request_r_scale;
9156 }
9157 /*
9158 * ok for the first time in lets see if we can use the ts to figure
9159 * out what the initial RTT was.
9160 */
9161 if ((to->to_flags & TOF_TS) != 0) {
9162 uint32_t t, rtt;
9163
9164 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9165 if (TSTMP_GEQ(t, to->to_tsecr)) {
9166 rtt = t - to->to_tsecr;
9167 if (rtt == 0) {
9168 rtt = 1;
9169 }
9170 rtt *= MS_IN_USEC;
9171 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9172 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime);
9173 }
9174 }
9175 /* Drop off any SYN in the send map (probably not there) */
9176 if (thflags & TH_ACK)
9177 bbr_log_syn(tp, to);
9178 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
9179 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9180 tp->t_tfo_pending = NULL;
9181 }
9182 /*
9183 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9184 * FIN-WAIT-1
9185 */
9186 tp->t_starttime = ticks;
9187 if (tp->t_flags & TF_NEEDFIN) {
9188 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9189 tp->t_flags &= ~TF_NEEDFIN;
9190 } else {
9191 tcp_state_change(tp, TCPS_ESTABLISHED);
9192 TCP_PROBE5(accept__established, NULL, tp,
9193 mtod(m, const char *), tp, th);
9194 /*
9195 * TFO connections call cc_conn_init() during SYN
9196 * processing. Calling it again here for such connections
9197 * is not harmless as it would undo the snd_cwnd reduction
9198 * that occurs when a TFO SYN|ACK is retransmitted.
9199 */
9200 if (!IS_FASTOPEN(tp->t_flags))
9201 cc_conn_init(tp);
9202 }
9203 /*
9204 * Account for the ACK of our SYN prior to
9205 * regular ACK processing below, except for
9206 * simultaneous SYN, which is handled later.
9207 */
9208 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
9209 tp->snd_una++;
9210 /*
9211 * If segment contains data or ACK, will call tcp_reass() later; if
9212 * not, do so now to pass queued data to user.
9213 */
9214 if (tlen == 0 && (thflags & TH_FIN) == 0)
9215 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9216 (struct mbuf *)0);
9217 tp->snd_wl1 = th->th_seq - 1;
9218 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9219 return (ret_val);
9220 }
9221 if (tp->t_state == TCPS_FIN_WAIT_1) {
9222 /* We could have went to FIN_WAIT_1 (or EST) above */
9223 /*
9224 * In FIN_WAIT_1 STATE in addition to the processing for the
9225 * ESTABLISHED state if our FIN is now acknowledged then
9226 * enter FIN_WAIT_2.
9227 */
9228 if (ourfinisacked) {
9229 /*
9230 * If we can't receive any more data, then closing
9231 * user can proceed. Starting the timer is contrary
9232 * to the specification, but if we don't get a FIN
9233 * we'll hang forever.
9234 *
9235 * XXXjl: we should release the tp also, and use a
9236 * compressed state.
9237 */
9238 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9239 soisdisconnected(so);
9240 tcp_timer_activate(tp, TT_2MSL,
9241 (tcp_fast_finwait2_recycle ?
9242 tcp_finwait2_timeout :
9243 TP_MAXIDLE(tp)));
9244 }
9245 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9246 }
9247 }
9248 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9249 tiwin, thflags, nxt_pkt));
9250 }
9251
9252 /*
9253 * Return value of 1, the TCB is unlocked and most
9254 * likely gone, return value of 0, the TCB is still
9255 * locked.
9256 */
9257 static int
bbr_do_established(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9258 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9259 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9260 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9261 {
9262 struct tcp_bbr *bbr;
9263 int32_t ret_val;
9264
9265 /*
9266 * Header prediction: check for the two common cases of a
9267 * uni-directional data xfer. If the packet has no control flags,
9268 * is in-sequence, the window didn't change and we're not
9269 * retransmitting, it's a candidate. If the length is zero and the
9270 * ack moved forward, we're the sender side of the xfer. Just free
9271 * the data acked & wake any higher level process that was blocked
9272 * waiting for space. If the length is non-zero and the ack didn't
9273 * move, we're the receiver side. If we're getting packets in-order
9274 * (the reassembly queue is empty), add the data toc The socket
9275 * buffer and note that we need a delayed ack. Make sure that the
9276 * hidden state-flags are also off. Since we check for
9277 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9278 */
9279 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9280 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) {
9281 /*
9282 * If we have delived under 4 segments increase the initial
9283 * window if raised by the peer. We use this to determine
9284 * dynamic and static rwnd's at the end of a connection.
9285 */
9286 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd);
9287 }
9288 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9289 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
9290 __predict_true(SEGQ_EMPTY(tp)) &&
9291 __predict_true(th->th_seq == tp->rcv_nxt)) {
9292 if (tlen == 0) {
9293 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9294 tiwin, nxt_pkt, iptos)) {
9295 return (0);
9296 }
9297 } else {
9298 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9299 tiwin, nxt_pkt)) {
9300 return (0);
9301 }
9302 }
9303 }
9304 ctf_calc_rwin(so, tp);
9305
9306 if ((thflags & TH_RST) ||
9307 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9308 return (ctf_process_rst(m, th, so, tp));
9309 /*
9310 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9311 * synchronized state.
9312 */
9313 if (thflags & TH_SYN) {
9314 ctf_challenge_ack(m, th, tp, &ret_val);
9315 return (ret_val);
9316 }
9317 /*
9318 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9319 * it's less than ts_recent, drop it.
9320 */
9321 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9322 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9323 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9324 return (ret_val);
9325 }
9326 INP_WLOCK_ASSERT(tp->t_inpcb);
9327 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9328 return (ret_val);
9329 }
9330 /*
9331 * If last ACK falls within this segment's sequence numbers, record
9332 * its timestamp. NOTE: 1) That the test incorporates suggestions
9333 * from the latest proposal of the [email protected] list (Braden
9334 * 1993/04/26). 2) That updating only on newer timestamps interferes
9335 * with our earlier PAWS tests, so this check should be solely
9336 * predicated on the sequence space of this segment. 3) That we
9337 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9338 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9339 * SEG.Len, This modified check allows us to overcome RFC1323's
9340 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9341 * p.869. In such cases, we can still calculate the RTT correctly
9342 * when RCV.NXT == Last.ACK.Sent.
9343 */
9344 if ((to->to_flags & TOF_TS) != 0 &&
9345 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9346 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9347 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9348 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9349 tp->ts_recent = to->to_tsval;
9350 }
9351 /*
9352 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9353 * is on (half-synchronized state), then queue data for later
9354 * processing; else drop segment and return.
9355 */
9356 if ((thflags & TH_ACK) == 0) {
9357 if (tp->t_flags & TF_NEEDSYN) {
9358 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9359 tiwin, thflags, nxt_pkt));
9360 } else if (tp->t_flags & TF_ACKNOW) {
9361 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9362 bbr->r_wanted_output = 1;
9363 return (ret_val);
9364 } else {
9365 ctf_do_drop(m, NULL);
9366 return (0);
9367 }
9368 }
9369 /*
9370 * Ack processing.
9371 */
9372 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9373 return (ret_val);
9374 }
9375 if (sbavail(&so->so_snd)) {
9376 if (ctf_progress_timeout_check(tp, true)) {
9377 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9378 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9379 return (1);
9380 }
9381 }
9382 /* State changes only happen in bbr_process_data() */
9383 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9384 tiwin, thflags, nxt_pkt));
9385 }
9386
9387 /*
9388 * Return value of 1, the TCB is unlocked and most
9389 * likely gone, return value of 0, the TCB is still
9390 * locked.
9391 */
9392 static int
bbr_do_close_wait(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9393 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9394 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9395 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9396 {
9397 struct tcp_bbr *bbr;
9398 int32_t ret_val;
9399
9400 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9401 ctf_calc_rwin(so, tp);
9402 if ((thflags & TH_RST) ||
9403 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9404 return (ctf_process_rst(m, th, so, tp));
9405 /*
9406 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9407 * synchronized state.
9408 */
9409 if (thflags & TH_SYN) {
9410 ctf_challenge_ack(m, th, tp, &ret_val);
9411 return (ret_val);
9412 }
9413 /*
9414 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9415 * it's less than ts_recent, drop it.
9416 */
9417 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9418 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9419 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9420 return (ret_val);
9421 }
9422 INP_WLOCK_ASSERT(tp->t_inpcb);
9423 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9424 return (ret_val);
9425 }
9426 /*
9427 * If last ACK falls within this segment's sequence numbers, record
9428 * its timestamp. NOTE: 1) That the test incorporates suggestions
9429 * from the latest proposal of the [email protected] list (Braden
9430 * 1993/04/26). 2) That updating only on newer timestamps interferes
9431 * with our earlier PAWS tests, so this check should be solely
9432 * predicated on the sequence space of this segment. 3) That we
9433 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9434 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9435 * SEG.Len, This modified check allows us to overcome RFC1323's
9436 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9437 * p.869. In such cases, we can still calculate the RTT correctly
9438 * when RCV.NXT == Last.ACK.Sent.
9439 */
9440 if ((to->to_flags & TOF_TS) != 0 &&
9441 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9442 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9443 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9444 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9445 tp->ts_recent = to->to_tsval;
9446 }
9447 /*
9448 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9449 * is on (half-synchronized state), then queue data for later
9450 * processing; else drop segment and return.
9451 */
9452 if ((thflags & TH_ACK) == 0) {
9453 if (tp->t_flags & TF_NEEDSYN) {
9454 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9455 tiwin, thflags, nxt_pkt));
9456 } else if (tp->t_flags & TF_ACKNOW) {
9457 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9458 bbr->r_wanted_output = 1;
9459 return (ret_val);
9460 } else {
9461 ctf_do_drop(m, NULL);
9462 return (0);
9463 }
9464 }
9465 /*
9466 * Ack processing.
9467 */
9468 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9469 return (ret_val);
9470 }
9471 if (sbavail(&so->so_snd)) {
9472 if (ctf_progress_timeout_check(tp, true)) {
9473 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9474 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9475 return (1);
9476 }
9477 }
9478 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9479 tiwin, thflags, nxt_pkt));
9480 }
9481
9482 static int
bbr_check_data_after_close(struct mbuf * m,struct tcp_bbr * bbr,struct tcpcb * tp,int32_t * tlen,struct tcphdr * th,struct socket * so)9483 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr,
9484 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so)
9485 {
9486
9487 if (bbr->rc_allow_data_af_clo == 0) {
9488 close_now:
9489 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9490 /* tcp_close will kill the inp pre-log the Reset */
9491 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
9492 tp = tcp_close(tp);
9493 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9494 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9495 return (1);
9496 }
9497 if (sbavail(&so->so_snd) == 0)
9498 goto close_now;
9499 /* Ok we allow data that is ignored and a followup reset */
9500 tp->rcv_nxt = th->th_seq + *tlen;
9501 tp->t_flags2 |= TF2_DROP_AF_DATA;
9502 bbr->r_wanted_output = 1;
9503 *tlen = 0;
9504 return (0);
9505 }
9506
9507 /*
9508 * Return value of 1, the TCB is unlocked and most
9509 * likely gone, return value of 0, the TCB is still
9510 * locked.
9511 */
9512 static int
bbr_do_fin_wait_1(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9513 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9514 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9515 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9516 {
9517 int32_t ourfinisacked = 0;
9518 int32_t ret_val;
9519 struct tcp_bbr *bbr;
9520
9521 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9522 ctf_calc_rwin(so, tp);
9523 if ((thflags & TH_RST) ||
9524 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9525 return (ctf_process_rst(m, th, so, tp));
9526 /*
9527 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9528 * synchronized state.
9529 */
9530 if (thflags & TH_SYN) {
9531 ctf_challenge_ack(m, th, tp, &ret_val);
9532 return (ret_val);
9533 }
9534 /*
9535 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9536 * it's less than ts_recent, drop it.
9537 */
9538 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9539 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9540 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9541 return (ret_val);
9542 }
9543 INP_WLOCK_ASSERT(tp->t_inpcb);
9544 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9545 return (ret_val);
9546 }
9547 /*
9548 * If new data are received on a connection after the user processes
9549 * are gone, then RST the other end.
9550 */
9551 if ((so->so_state & SS_NOFDREF) && tlen) {
9552 /*
9553 * We call a new function now so we might continue and setup
9554 * to reset at all data being ack'd.
9555 */
9556 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9557 return (1);
9558 }
9559 /*
9560 * If last ACK falls within this segment's sequence numbers, record
9561 * its timestamp. NOTE: 1) That the test incorporates suggestions
9562 * from the latest proposal of the [email protected] list (Braden
9563 * 1993/04/26). 2) That updating only on newer timestamps interferes
9564 * with our earlier PAWS tests, so this check should be solely
9565 * predicated on the sequence space of this segment. 3) That we
9566 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9567 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9568 * SEG.Len, This modified check allows us to overcome RFC1323's
9569 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9570 * p.869. In such cases, we can still calculate the RTT correctly
9571 * when RCV.NXT == Last.ACK.Sent.
9572 */
9573 if ((to->to_flags & TOF_TS) != 0 &&
9574 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9575 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9576 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9577 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9578 tp->ts_recent = to->to_tsval;
9579 }
9580 /*
9581 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9582 * is on (half-synchronized state), then queue data for later
9583 * processing; else drop segment and return.
9584 */
9585 if ((thflags & TH_ACK) == 0) {
9586 if (tp->t_flags & TF_NEEDSYN) {
9587 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9588 tiwin, thflags, nxt_pkt));
9589 } else if (tp->t_flags & TF_ACKNOW) {
9590 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9591 bbr->r_wanted_output = 1;
9592 return (ret_val);
9593 } else {
9594 ctf_do_drop(m, NULL);
9595 return (0);
9596 }
9597 }
9598 /*
9599 * Ack processing.
9600 */
9601 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9602 return (ret_val);
9603 }
9604 if (ourfinisacked) {
9605 /*
9606 * If we can't receive any more data, then closing user can
9607 * proceed. Starting the timer is contrary to the
9608 * specification, but if we don't get a FIN we'll hang
9609 * forever.
9610 *
9611 * XXXjl: we should release the tp also, and use a
9612 * compressed state.
9613 */
9614 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9615 soisdisconnected(so);
9616 tcp_timer_activate(tp, TT_2MSL,
9617 (tcp_fast_finwait2_recycle ?
9618 tcp_finwait2_timeout :
9619 TP_MAXIDLE(tp)));
9620 }
9621 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9622 }
9623 if (sbavail(&so->so_snd)) {
9624 if (ctf_progress_timeout_check(tp, true)) {
9625 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9626 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9627 return (1);
9628 }
9629 }
9630 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9631 tiwin, thflags, nxt_pkt));
9632 }
9633
9634 /*
9635 * Return value of 1, the TCB is unlocked and most
9636 * likely gone, return value of 0, the TCB is still
9637 * locked.
9638 */
9639 static int
bbr_do_closing(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9640 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9641 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9642 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9643 {
9644 int32_t ourfinisacked = 0;
9645 int32_t ret_val;
9646 struct tcp_bbr *bbr;
9647
9648 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9649 ctf_calc_rwin(so, tp);
9650 if ((thflags & TH_RST) ||
9651 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9652 return (ctf_process_rst(m, th, so, tp));
9653 /*
9654 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9655 * synchronized state.
9656 */
9657 if (thflags & TH_SYN) {
9658 ctf_challenge_ack(m, th, tp, &ret_val);
9659 return (ret_val);
9660 }
9661 /*
9662 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9663 * it's less than ts_recent, drop it.
9664 */
9665 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9666 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9667 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9668 return (ret_val);
9669 }
9670 INP_WLOCK_ASSERT(tp->t_inpcb);
9671 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9672 return (ret_val);
9673 }
9674 /*
9675 * If new data are received on a connection after the user processes
9676 * are gone, then RST the other end.
9677 */
9678 if ((so->so_state & SS_NOFDREF) && tlen) {
9679 /*
9680 * We call a new function now so we might continue and setup
9681 * to reset at all data being ack'd.
9682 */
9683 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9684 return (1);
9685 }
9686 /*
9687 * If last ACK falls within this segment's sequence numbers, record
9688 * its timestamp. NOTE: 1) That the test incorporates suggestions
9689 * from the latest proposal of the [email protected] list (Braden
9690 * 1993/04/26). 2) That updating only on newer timestamps interferes
9691 * with our earlier PAWS tests, so this check should be solely
9692 * predicated on the sequence space of this segment. 3) That we
9693 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9694 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9695 * SEG.Len, This modified check allows us to overcome RFC1323's
9696 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9697 * p.869. In such cases, we can still calculate the RTT correctly
9698 * when RCV.NXT == Last.ACK.Sent.
9699 */
9700 if ((to->to_flags & TOF_TS) != 0 &&
9701 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9702 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9703 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9704 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9705 tp->ts_recent = to->to_tsval;
9706 }
9707 /*
9708 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9709 * is on (half-synchronized state), then queue data for later
9710 * processing; else drop segment and return.
9711 */
9712 if ((thflags & TH_ACK) == 0) {
9713 if (tp->t_flags & TF_NEEDSYN) {
9714 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9715 tiwin, thflags, nxt_pkt));
9716 } else if (tp->t_flags & TF_ACKNOW) {
9717 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9718 bbr->r_wanted_output = 1;
9719 return (ret_val);
9720 } else {
9721 ctf_do_drop(m, NULL);
9722 return (0);
9723 }
9724 }
9725 /*
9726 * Ack processing.
9727 */
9728 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9729 return (ret_val);
9730 }
9731 if (ourfinisacked) {
9732 tcp_twstart(tp);
9733 m_freem(m);
9734 return (1);
9735 }
9736 if (sbavail(&so->so_snd)) {
9737 if (ctf_progress_timeout_check(tp, true)) {
9738 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9739 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9740 return (1);
9741 }
9742 }
9743 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9744 tiwin, thflags, nxt_pkt));
9745 }
9746
9747 /*
9748 * Return value of 1, the TCB is unlocked and most
9749 * likely gone, return value of 0, the TCB is still
9750 * locked.
9751 */
9752 static int
bbr_do_lastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9753 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
9754 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9755 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9756 {
9757 int32_t ourfinisacked = 0;
9758 int32_t ret_val;
9759 struct tcp_bbr *bbr;
9760
9761 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9762 ctf_calc_rwin(so, tp);
9763 if ((thflags & TH_RST) ||
9764 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9765 return (ctf_process_rst(m, th, so, tp));
9766 /*
9767 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9768 * synchronized state.
9769 */
9770 if (thflags & TH_SYN) {
9771 ctf_challenge_ack(m, th, tp, &ret_val);
9772 return (ret_val);
9773 }
9774 /*
9775 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9776 * it's less than ts_recent, drop it.
9777 */
9778 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9779 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9780 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9781 return (ret_val);
9782 }
9783 INP_WLOCK_ASSERT(tp->t_inpcb);
9784 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9785 return (ret_val);
9786 }
9787 /*
9788 * If new data are received on a connection after the user processes
9789 * are gone, then RST the other end.
9790 */
9791 if ((so->so_state & SS_NOFDREF) && tlen) {
9792 /*
9793 * We call a new function now so we might continue and setup
9794 * to reset at all data being ack'd.
9795 */
9796 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9797 return (1);
9798 }
9799 /*
9800 * If last ACK falls within this segment's sequence numbers, record
9801 * its timestamp. NOTE: 1) That the test incorporates suggestions
9802 * from the latest proposal of the [email protected] list (Braden
9803 * 1993/04/26). 2) That updating only on newer timestamps interferes
9804 * with our earlier PAWS tests, so this check should be solely
9805 * predicated on the sequence space of this segment. 3) That we
9806 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9807 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9808 * SEG.Len, This modified check allows us to overcome RFC1323's
9809 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9810 * p.869. In such cases, we can still calculate the RTT correctly
9811 * when RCV.NXT == Last.ACK.Sent.
9812 */
9813 if ((to->to_flags & TOF_TS) != 0 &&
9814 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9815 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9816 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9817 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9818 tp->ts_recent = to->to_tsval;
9819 }
9820 /*
9821 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9822 * is on (half-synchronized state), then queue data for later
9823 * processing; else drop segment and return.
9824 */
9825 if ((thflags & TH_ACK) == 0) {
9826 if (tp->t_flags & TF_NEEDSYN) {
9827 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9828 tiwin, thflags, nxt_pkt));
9829 } else if (tp->t_flags & TF_ACKNOW) {
9830 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9831 bbr->r_wanted_output = 1;
9832 return (ret_val);
9833 } else {
9834 ctf_do_drop(m, NULL);
9835 return (0);
9836 }
9837 }
9838 /*
9839 * case TCPS_LAST_ACK: Ack processing.
9840 */
9841 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9842 return (ret_val);
9843 }
9844 if (ourfinisacked) {
9845 tp = tcp_close(tp);
9846 ctf_do_drop(m, tp);
9847 return (1);
9848 }
9849 if (sbavail(&so->so_snd)) {
9850 if (ctf_progress_timeout_check(tp, true)) {
9851 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9852 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9853 return (1);
9854 }
9855 }
9856 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9857 tiwin, thflags, nxt_pkt));
9858 }
9859
9860 /*
9861 * Return value of 1, the TCB is unlocked and most
9862 * likely gone, return value of 0, the TCB is still
9863 * locked.
9864 */
9865 static int
bbr_do_fin_wait_2(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9866 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
9867 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9868 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9869 {
9870 int32_t ourfinisacked = 0;
9871 int32_t ret_val;
9872 struct tcp_bbr *bbr;
9873
9874 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9875 ctf_calc_rwin(so, tp);
9876 /* Reset receive buffer auto scaling when not in bulk receive mode. */
9877 if ((thflags & TH_RST) ||
9878 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9879 return (ctf_process_rst(m, th, so, tp));
9880
9881 /*
9882 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9883 * synchronized state.
9884 */
9885 if (thflags & TH_SYN) {
9886 ctf_challenge_ack(m, th, tp, &ret_val);
9887 return (ret_val);
9888 }
9889 INP_WLOCK_ASSERT(tp->t_inpcb);
9890 /*
9891 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9892 * it's less than ts_recent, drop it.
9893 */
9894 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9895 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9896 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9897 return (ret_val);
9898 }
9899 INP_WLOCK_ASSERT(tp->t_inpcb);
9900 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9901 return (ret_val);
9902 }
9903 /*
9904 * If new data are received on a connection after the user processes
9905 * are gone, then we may RST the other end depending on the outcome
9906 * of bbr_check_data_after_close.
9907 */
9908 if ((so->so_state & SS_NOFDREF) &&
9909 tlen) {
9910 /*
9911 * We call a new function now so we might continue and setup
9912 * to reset at all data being ack'd.
9913 */
9914 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9915 return (1);
9916 }
9917 INP_WLOCK_ASSERT(tp->t_inpcb);
9918 /*
9919 * If last ACK falls within this segment's sequence numbers, record
9920 * its timestamp. NOTE: 1) That the test incorporates suggestions
9921 * from the latest proposal of the [email protected] list (Braden
9922 * 1993/04/26). 2) That updating only on newer timestamps interferes
9923 * with our earlier PAWS tests, so this check should be solely
9924 * predicated on the sequence space of this segment. 3) That we
9925 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9926 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9927 * SEG.Len, This modified check allows us to overcome RFC1323's
9928 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9929 * p.869. In such cases, we can still calculate the RTT correctly
9930 * when RCV.NXT == Last.ACK.Sent.
9931 */
9932 INP_WLOCK_ASSERT(tp->t_inpcb);
9933 if ((to->to_flags & TOF_TS) != 0 &&
9934 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9935 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9936 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9937 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9938 tp->ts_recent = to->to_tsval;
9939 }
9940 /*
9941 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9942 * is on (half-synchronized state), then queue data for later
9943 * processing; else drop segment and return.
9944 */
9945 if ((thflags & TH_ACK) == 0) {
9946 if (tp->t_flags & TF_NEEDSYN) {
9947 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9948 tiwin, thflags, nxt_pkt));
9949 } else if (tp->t_flags & TF_ACKNOW) {
9950 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9951 bbr->r_wanted_output = 1;
9952 return (ret_val);
9953 } else {
9954 ctf_do_drop(m, NULL);
9955 return (0);
9956 }
9957 }
9958 /*
9959 * Ack processing.
9960 */
9961 INP_WLOCK_ASSERT(tp->t_inpcb);
9962 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9963 return (ret_val);
9964 }
9965 if (sbavail(&so->so_snd)) {
9966 if (ctf_progress_timeout_check(tp, true)) {
9967 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9968 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9969 return (1);
9970 }
9971 }
9972 INP_WLOCK_ASSERT(tp->t_inpcb);
9973 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9974 tiwin, thflags, nxt_pkt));
9975 }
9976
9977 static void
bbr_stop_all_timers(struct tcpcb * tp)9978 bbr_stop_all_timers(struct tcpcb *tp)
9979 {
9980 struct tcp_bbr *bbr;
9981
9982 /*
9983 * Assure no timers are running.
9984 */
9985 if (tcp_timer_active(tp, TT_PERSIST)) {
9986 /* We enter in persists, set the flag appropriately */
9987 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9988 bbr->rc_in_persist = 1;
9989 }
9990 tcp_timer_suspend(tp, TT_PERSIST);
9991 tcp_timer_suspend(tp, TT_REXMT);
9992 tcp_timer_suspend(tp, TT_KEEP);
9993 tcp_timer_suspend(tp, TT_DELACK);
9994 }
9995
9996 static void
bbr_google_mode_on(struct tcp_bbr * bbr)9997 bbr_google_mode_on(struct tcp_bbr *bbr)
9998 {
9999 bbr->rc_use_google = 1;
10000 bbr->rc_no_pacing = 0;
10001 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10002 bbr->r_use_policer = bbr_policer_detection_enabled;
10003 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10004 bbr->bbr_use_rack_cheat = 0;
10005 bbr->r_ctl.rc_incr_tmrs = 0;
10006 bbr->r_ctl.rc_inc_tcp_oh = 0;
10007 bbr->r_ctl.rc_inc_ip_oh = 0;
10008 bbr->r_ctl.rc_inc_enet_oh = 0;
10009 reset_time(&bbr->r_ctl.rc_delrate,
10010 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10011 reset_time_small(&bbr->r_ctl.rc_rttprop,
10012 (11 * USECS_IN_SECOND));
10013 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10014 }
10015
10016 static void
bbr_google_mode_off(struct tcp_bbr * bbr)10017 bbr_google_mode_off(struct tcp_bbr *bbr)
10018 {
10019 bbr->rc_use_google = 0;
10020 bbr->r_ctl.bbr_google_discount = 0;
10021 bbr->no_pacing_until = bbr_no_pacing_until;
10022 bbr->r_use_policer = 0;
10023 if (bbr->no_pacing_until)
10024 bbr->rc_no_pacing = 1;
10025 else
10026 bbr->rc_no_pacing = 0;
10027 if (bbr_use_rack_resend_cheat)
10028 bbr->bbr_use_rack_cheat = 1;
10029 else
10030 bbr->bbr_use_rack_cheat = 0;
10031 if (bbr_incr_timers)
10032 bbr->r_ctl.rc_incr_tmrs = 1;
10033 else
10034 bbr->r_ctl.rc_incr_tmrs = 0;
10035 if (bbr_include_tcp_oh)
10036 bbr->r_ctl.rc_inc_tcp_oh = 1;
10037 else
10038 bbr->r_ctl.rc_inc_tcp_oh = 0;
10039 if (bbr_include_ip_oh)
10040 bbr->r_ctl.rc_inc_ip_oh = 1;
10041 else
10042 bbr->r_ctl.rc_inc_ip_oh = 0;
10043 if (bbr_include_enet_oh)
10044 bbr->r_ctl.rc_inc_enet_oh = 1;
10045 else
10046 bbr->r_ctl.rc_inc_enet_oh = 0;
10047 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10048 reset_time(&bbr->r_ctl.rc_delrate,
10049 bbr_num_pktepo_for_del_limit);
10050 reset_time_small(&bbr->r_ctl.rc_rttprop,
10051 (bbr_filter_len_sec * USECS_IN_SECOND));
10052 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10053 }
10054 /*
10055 * Return 0 on success, non-zero on failure
10056 * which indicates the error (usually no memory).
10057 */
10058 static int
bbr_init(struct tcpcb * tp)10059 bbr_init(struct tcpcb *tp)
10060 {
10061 struct tcp_bbr *bbr = NULL;
10062 struct inpcb *inp;
10063 uint32_t cts;
10064
10065 tp->t_fb_ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO));
10066 if (tp->t_fb_ptr == NULL) {
10067 /*
10068 * We need to allocate memory but cant. The INP and INP_INFO
10069 * locks and they are recusive (happens during setup. So a
10070 * scheme to drop the locks fails :(
10071 *
10072 */
10073 return (ENOMEM);
10074 }
10075 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10076 bbr->rtt_valid = 0;
10077 inp = tp->t_inpcb;
10078 inp->inp_flags2 |= INP_CANNOT_DO_ECN;
10079 inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
10080 TAILQ_INIT(&bbr->r_ctl.rc_map);
10081 TAILQ_INIT(&bbr->r_ctl.rc_free);
10082 TAILQ_INIT(&bbr->r_ctl.rc_tmap);
10083 bbr->rc_tp = tp;
10084 if (tp->t_inpcb) {
10085 bbr->rc_inp = tp->t_inpcb;
10086 }
10087 cts = tcp_get_usecs(&bbr->rc_tv);
10088 tp->t_acktime = 0;
10089 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close;
10090 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade;
10091 bbr->rc_tlp_threshold = bbr_tlp_thresh;
10092 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh;
10093 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay;
10094 bbr->r_ctl.rc_min_to = bbr_min_to;
10095 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10096 bbr->r_ctl.bbr_lost_at_state = 0;
10097 bbr->r_ctl.rc_lost_at_startup = 0;
10098 bbr->rc_all_timers_stopped = 0;
10099 bbr->r_ctl.rc_bbr_lastbtlbw = 0;
10100 bbr->r_ctl.rc_pkt_epoch_del = 0;
10101 bbr->r_ctl.rc_pkt_epoch = 0;
10102 bbr->r_ctl.rc_lowest_rtt = 0xffffffff;
10103 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain;
10104 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
10105 bbr->r_ctl.rc_went_idle_time = cts;
10106 bbr->rc_pacer_started = cts;
10107 bbr->r_ctl.rc_pkt_epoch_time = cts;
10108 bbr->r_ctl.rc_rcvtime = cts;
10109 bbr->r_ctl.rc_bbr_state_time = cts;
10110 bbr->r_ctl.rc_del_time = cts;
10111 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
10112 bbr->r_ctl.last_in_probertt = cts;
10113 bbr->skip_gain = 0;
10114 bbr->gain_is_limited = 0;
10115 bbr->no_pacing_until = bbr_no_pacing_until;
10116 if (bbr->no_pacing_until)
10117 bbr->rc_no_pacing = 1;
10118 if (bbr_use_google_algo) {
10119 bbr->rc_no_pacing = 0;
10120 bbr->rc_use_google = 1;
10121 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10122 bbr->r_use_policer = bbr_policer_detection_enabled;
10123 } else {
10124 bbr->rc_use_google = 0;
10125 bbr->r_ctl.bbr_google_discount = 0;
10126 bbr->r_use_policer = 0;
10127 }
10128 if (bbr_ts_limiting)
10129 bbr->rc_use_ts_limit = 1;
10130 else
10131 bbr->rc_use_ts_limit = 0;
10132 if (bbr_ts_can_raise)
10133 bbr->ts_can_raise = 1;
10134 else
10135 bbr->ts_can_raise = 0;
10136 if (V_tcp_delack_enabled == 1)
10137 tp->t_delayed_ack = 2;
10138 else if (V_tcp_delack_enabled == 0)
10139 tp->t_delayed_ack = 0;
10140 else if (V_tcp_delack_enabled < 100)
10141 tp->t_delayed_ack = V_tcp_delack_enabled;
10142 else
10143 tp->t_delayed_ack = 2;
10144 if (bbr->rc_use_google == 0)
10145 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10146 else
10147 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10148 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms;
10149 bbr->rc_max_rto_sec = bbr_rto_max_sec;
10150 bbr->rc_init_win = bbr_def_init_win;
10151 if (tp->t_flags & TF_REQ_TSTMP)
10152 bbr->rc_last_options = TCP_TS_OVERHEAD;
10153 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options;
10154 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd;
10155 bbr->r_init_rtt = 1;
10156
10157 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
10158 if (bbr_allow_hdwr_pacing)
10159 bbr->bbr_hdw_pace_ena = 1;
10160 else
10161 bbr->bbr_hdw_pace_ena = 0;
10162 if (bbr_sends_full_iwnd)
10163 bbr->bbr_init_win_cheat = 1;
10164 else
10165 bbr->bbr_init_win_cheat = 0;
10166 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max;
10167 bbr->r_ctl.rc_drain_pg = bbr_drain_gain;
10168 bbr->r_ctl.rc_startup_pg = bbr_high_gain;
10169 bbr->rc_loss_exit = bbr_exit_startup_at_loss;
10170 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain;
10171 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second;
10172 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar;
10173 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max;
10174 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor;
10175 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min;
10176 bbr->r_ctl.bbr_cross_over = bbr_cross_over;
10177 bbr->r_ctl.rc_rtt_shrinks = cts;
10178 if (bbr->rc_use_google) {
10179 setup_time_filter(&bbr->r_ctl.rc_delrate,
10180 FILTER_TYPE_MAX,
10181 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10182 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10183 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND));
10184 } else {
10185 setup_time_filter(&bbr->r_ctl.rc_delrate,
10186 FILTER_TYPE_MAX,
10187 bbr_num_pktepo_for_del_limit);
10188 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10189 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND));
10190 }
10191 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0);
10192 if (bbr_uses_idle_restart)
10193 bbr->rc_use_idle_restart = 1;
10194 else
10195 bbr->rc_use_idle_restart = 0;
10196 bbr->r_ctl.rc_bbr_cur_del_rate = 0;
10197 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps;
10198 if (bbr_resends_use_tso)
10199 bbr->rc_resends_use_tso = 1;
10200 #ifdef NETFLIX_PEAKRATE
10201 tp->t_peakrate_thr = tp->t_maxpeakrate;
10202 #endif
10203 if (tp->snd_una != tp->snd_max) {
10204 /* Create a send map for the current outstanding data */
10205 struct bbr_sendmap *rsm;
10206
10207 rsm = bbr_alloc(bbr);
10208 if (rsm == NULL) {
10209 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10210 tp->t_fb_ptr = NULL;
10211 return (ENOMEM);
10212 }
10213 rsm->r_flags = BBR_OVERMAX;
10214 rsm->r_tim_lastsent[0] = cts;
10215 rsm->r_rtr_cnt = 1;
10216 rsm->r_rtr_bytes = 0;
10217 rsm->r_start = tp->snd_una;
10218 rsm->r_end = tp->snd_max;
10219 rsm->r_dupack = 0;
10220 rsm->r_delivered = bbr->r_ctl.rc_delivered;
10221 rsm->r_ts_valid = 0;
10222 rsm->r_del_ack_ts = tp->ts_recent;
10223 rsm->r_del_time = cts;
10224 if (bbr->r_ctl.r_app_limited_until)
10225 rsm->r_app_limited = 1;
10226 else
10227 rsm->r_app_limited = 0;
10228 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
10229 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
10230 rsm->r_in_tmap = 1;
10231 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
10232 rsm->r_bbr_state = bbr_state_val(bbr);
10233 else
10234 rsm->r_bbr_state = 8;
10235 }
10236 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0))
10237 bbr->bbr_use_rack_cheat = 1;
10238 if (bbr_incr_timers && (bbr->rc_use_google == 0))
10239 bbr->r_ctl.rc_incr_tmrs = 1;
10240 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0))
10241 bbr->r_ctl.rc_inc_tcp_oh = 1;
10242 if (bbr_include_ip_oh && (bbr->rc_use_google == 0))
10243 bbr->r_ctl.rc_inc_ip_oh = 1;
10244 if (bbr_include_enet_oh && (bbr->rc_use_google == 0))
10245 bbr->r_ctl.rc_inc_enet_oh = 1;
10246
10247 bbr_log_type_statechange(bbr, cts, __LINE__);
10248 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
10249 (tp->t_srtt)) {
10250 uint32_t rtt;
10251
10252 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
10253 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
10254 }
10255 /* announce the settings and state */
10256 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT);
10257 tcp_bbr_tso_size_check(bbr, cts);
10258 /*
10259 * Now call the generic function to start a timer. This will place
10260 * the TCB on the hptsi wheel if a timer is needed with appropriate
10261 * flags.
10262 */
10263 bbr_stop_all_timers(tp);
10264 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0);
10265 return (0);
10266 }
10267
10268 /*
10269 * Return 0 if we can accept the connection. Return
10270 * non-zero if we can't handle the connection. A EAGAIN
10271 * means you need to wait until the connection is up.
10272 * a EADDRNOTAVAIL means we can never handle the connection
10273 * (no SACK).
10274 */
10275 static int
bbr_handoff_ok(struct tcpcb * tp)10276 bbr_handoff_ok(struct tcpcb *tp)
10277 {
10278 if ((tp->t_state == TCPS_CLOSED) ||
10279 (tp->t_state == TCPS_LISTEN)) {
10280 /* Sure no problem though it may not stick */
10281 return (0);
10282 }
10283 if ((tp->t_state == TCPS_SYN_SENT) ||
10284 (tp->t_state == TCPS_SYN_RECEIVED)) {
10285 /*
10286 * We really don't know you have to get to ESTAB or beyond
10287 * to tell.
10288 */
10289 return (EAGAIN);
10290 }
10291 if (tp->t_flags & TF_SENTFIN)
10292 return (EINVAL);
10293 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) {
10294 return (0);
10295 }
10296 /*
10297 * If we reach here we don't do SACK on this connection so we can
10298 * never do rack.
10299 */
10300 return (EINVAL);
10301 }
10302
10303 static void
bbr_fini(struct tcpcb * tp,int32_t tcb_is_purged)10304 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10305 {
10306 if (tp->t_fb_ptr) {
10307 uint32_t calc;
10308 struct tcp_bbr *bbr;
10309 struct bbr_sendmap *rsm;
10310
10311 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10312 if (bbr->r_ctl.crte)
10313 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
10314 bbr_log_flowend(bbr);
10315 bbr->rc_tp = NULL;
10316 if (tp->t_inpcb) {
10317 /* Backout any flags2 we applied */
10318 tp->t_inpcb->inp_flags2 &= ~INP_CANNOT_DO_ECN;
10319 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
10320 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
10321 }
10322 if (bbr->bbr_hdrw_pacing)
10323 counter_u64_add(bbr_flows_whdwr_pacing, -1);
10324 else
10325 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
10326 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10327 while (rsm) {
10328 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
10329 uma_zfree(bbr_zone, rsm);
10330 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10331 }
10332 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10333 while (rsm) {
10334 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
10335 uma_zfree(bbr_zone, rsm);
10336 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10337 }
10338 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd;
10339 if (calc > (bbr->r_ctl.rc_init_rwnd / 10))
10340 BBR_STAT_INC(bbr_dynamic_rwnd);
10341 else
10342 BBR_STAT_INC(bbr_static_rwnd);
10343 bbr->r_ctl.rc_free_cnt = 0;
10344 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10345 tp->t_fb_ptr = NULL;
10346 }
10347 /* Make sure snd_nxt is correctly set */
10348 tp->snd_nxt = tp->snd_max;
10349 }
10350
10351 static void
bbr_set_state(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t win)10352 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win)
10353 {
10354 switch (tp->t_state) {
10355 case TCPS_SYN_SENT:
10356 bbr->r_state = TCPS_SYN_SENT;
10357 bbr->r_substate = bbr_do_syn_sent;
10358 break;
10359 case TCPS_SYN_RECEIVED:
10360 bbr->r_state = TCPS_SYN_RECEIVED;
10361 bbr->r_substate = bbr_do_syn_recv;
10362 break;
10363 case TCPS_ESTABLISHED:
10364 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd);
10365 bbr->r_state = TCPS_ESTABLISHED;
10366 bbr->r_substate = bbr_do_established;
10367 break;
10368 case TCPS_CLOSE_WAIT:
10369 bbr->r_state = TCPS_CLOSE_WAIT;
10370 bbr->r_substate = bbr_do_close_wait;
10371 break;
10372 case TCPS_FIN_WAIT_1:
10373 bbr->r_state = TCPS_FIN_WAIT_1;
10374 bbr->r_substate = bbr_do_fin_wait_1;
10375 break;
10376 case TCPS_CLOSING:
10377 bbr->r_state = TCPS_CLOSING;
10378 bbr->r_substate = bbr_do_closing;
10379 break;
10380 case TCPS_LAST_ACK:
10381 bbr->r_state = TCPS_LAST_ACK;
10382 bbr->r_substate = bbr_do_lastack;
10383 break;
10384 case TCPS_FIN_WAIT_2:
10385 bbr->r_state = TCPS_FIN_WAIT_2;
10386 bbr->r_substate = bbr_do_fin_wait_2;
10387 break;
10388 case TCPS_LISTEN:
10389 case TCPS_CLOSED:
10390 case TCPS_TIME_WAIT:
10391 default:
10392 break;
10393 };
10394 }
10395
10396 static void
bbr_substate_change(struct tcp_bbr * bbr,uint32_t cts,int32_t line,int dolog)10397 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog)
10398 {
10399 /*
10400 * Now what state are we going into now? Is there adjustments
10401 * needed?
10402 */
10403 int32_t old_state, old_gain;
10404
10405 old_state = bbr_state_val(bbr);
10406 old_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
10407 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) {
10408 /* Save the lowest srtt we saw in our end of the sub-state */
10409 bbr->rc_hit_state_1 = 0;
10410 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff)
10411 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state;
10412 }
10413 bbr->rc_bbr_substate++;
10414 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) {
10415 /* Cycle back to first state-> gain */
10416 bbr->rc_bbr_substate = 0;
10417 }
10418 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10419 /*
10420 * We enter the gain(5/4) cycle (possibly less if
10421 * shallow buffer detection is enabled)
10422 */
10423 if (bbr->skip_gain) {
10424 /*
10425 * Hardware pacing has set our rate to
10426 * the max and limited our b/w just
10427 * do level i.e. no gain.
10428 */
10429 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1];
10430 } else if (bbr->gain_is_limited &&
10431 bbr->bbr_hdrw_pacing &&
10432 bbr->r_ctl.crte) {
10433 /*
10434 * We can't gain above the hardware pacing
10435 * rate which is less than our rate + the gain
10436 * calculate the gain needed to reach the hardware
10437 * pacing rate..
10438 */
10439 uint64_t bw, rate, gain_calc;
10440
10441 bw = bbr_get_bw(bbr);
10442 rate = bbr->r_ctl.crte->rate;
10443 if ((rate > bw) &&
10444 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) {
10445 gain_calc = (rate * BBR_UNIT) / bw;
10446 if (gain_calc < BBR_UNIT)
10447 gain_calc = BBR_UNIT;
10448 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc;
10449 } else {
10450 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10451 }
10452 } else
10453 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10454 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) {
10455 bbr->r_ctl.rc_bbr_state_atflight = cts;
10456 } else
10457 bbr->r_ctl.rc_bbr_state_atflight = 0;
10458 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10459 bbr->rc_hit_state_1 = 1;
10460 bbr->r_ctl.rc_exta_time_gd = 0;
10461 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10462 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10463 if (bbr_state_drain_2_tar) {
10464 bbr->r_ctl.rc_bbr_state_atflight = 0;
10465 } else
10466 bbr->r_ctl.rc_bbr_state_atflight = cts;
10467 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN];
10468 } else {
10469 /* All other cycles hit here 2-7 */
10470 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) {
10471 if (bbr_sub_drain_slam_cwnd &&
10472 (bbr->rc_use_google == 0) &&
10473 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10474 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10475 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10476 }
10477 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP))
10478 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) -
10479 bbr_get_rtt(bbr, BBR_RTT_PROP));
10480 else
10481 bbr->r_ctl.rc_exta_time_gd = 0;
10482 if (bbr->r_ctl.rc_exta_time_gd) {
10483 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd;
10484 /* Now chop up the time for each state (div by 7) */
10485 bbr->r_ctl.rc_level_state_extra /= 7;
10486 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) {
10487 /* Add a randomization */
10488 bbr_randomize_extra_state_time(bbr);
10489 }
10490 }
10491 }
10492 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10493 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)];
10494 }
10495 if (bbr->rc_use_google) {
10496 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10497 }
10498 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10499 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10500 if (dolog)
10501 bbr_log_type_statechange(bbr, cts, line);
10502
10503 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10504 uint32_t time_in;
10505
10506 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10507 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
10508 counter_u64_add(bbr_state_time[(old_state + 5)], time_in);
10509 } else {
10510 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10511 }
10512 }
10513 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
10514 bbr_set_state_target(bbr, __LINE__);
10515 if (bbr_sub_drain_slam_cwnd &&
10516 (bbr->rc_use_google == 0) &&
10517 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10518 /* Slam down the cwnd */
10519 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10520 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10521 if (bbr_sub_drain_app_limit) {
10522 /* Go app limited if we are on a long drain */
10523 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered +
10524 ctf_flight_size(bbr->rc_tp,
10525 (bbr->r_ctl.rc_sacked +
10526 bbr->r_ctl.rc_lost_bytes)));
10527 }
10528 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10529 }
10530 if (bbr->rc_lt_use_bw) {
10531 /* In policed mode we clamp pacing_gain to BBR_UNIT */
10532 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10533 }
10534 /* Google changes TSO size every cycle */
10535 if (bbr->rc_use_google)
10536 tcp_bbr_tso_size_check(bbr, cts);
10537 bbr->r_ctl.gain_epoch = cts;
10538 bbr->r_ctl.rc_bbr_state_time = cts;
10539 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch;
10540 }
10541
10542 static void
bbr_set_probebw_google_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10543 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10544 {
10545 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) &&
10546 (google_allow_early_out == 1) &&
10547 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) {
10548 /* We have reached out target flight size possibly early */
10549 goto change_state;
10550 }
10551 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10552 return;
10553 }
10554 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) {
10555 /*
10556 * Must be a rttProp movement forward before
10557 * we can change states.
10558 */
10559 return;
10560 }
10561 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10562 /*
10563 * The needed time has passed but for
10564 * the gain cycle extra rules apply:
10565 * 1) If we have seen loss, we exit
10566 * 2) If we have not reached the target
10567 * we stay in GAIN (gain-to-target).
10568 */
10569 if (google_consider_lost && losses)
10570 goto change_state;
10571 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) {
10572 return;
10573 }
10574 }
10575 change_state:
10576 /* For gain we must reach our target, all others last 1 rttProp */
10577 bbr_substate_change(bbr, cts, __LINE__, 1);
10578 }
10579
10580 static void
bbr_set_probebw_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10581 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10582 {
10583 uint32_t flight, bbr_cur_cycle_time;
10584
10585 if (bbr->rc_use_google) {
10586 bbr_set_probebw_google_gains(bbr, cts, losses);
10587 return;
10588 }
10589 if (cts == 0) {
10590 /*
10591 * Never alow cts to be 0 we
10592 * do this so we can judge if
10593 * we have set a timestamp.
10594 */
10595 cts = 1;
10596 }
10597 if (bbr_state_is_pkt_epoch)
10598 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
10599 else
10600 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP);
10601
10602 if (bbr->r_ctl.rc_bbr_state_atflight == 0) {
10603 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10604 flight = ctf_flight_size(bbr->rc_tp,
10605 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10606 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) {
10607 /* Keep it slam down */
10608 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) {
10609 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10610 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10611 }
10612 if (bbr_sub_drain_app_limit) {
10613 /* Go app limited if we are on a long drain */
10614 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight);
10615 }
10616 }
10617 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) &&
10618 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) ||
10619 (flight >= bbr->r_ctl.flightsize_at_drain))) {
10620 /*
10621 * Still here after the same time as
10622 * the gain. We need to drain harder
10623 * for the next srtt. Reduce by a set amount
10624 * the gain drop is capped at DRAIN states
10625 * value (88).
10626 */
10627 bbr->r_ctl.flightsize_at_drain = flight;
10628 if (bbr_drain_drop_mul &&
10629 bbr_drain_drop_div &&
10630 (bbr_drain_drop_mul < bbr_drain_drop_div)) {
10631 /* Use your specific drop value (def 4/5 = 20%) */
10632 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul;
10633 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div;
10634 } else {
10635 /* You get drop of 20% */
10636 bbr->r_ctl.rc_bbr_hptsi_gain *= 4;
10637 bbr->r_ctl.rc_bbr_hptsi_gain /= 5;
10638 }
10639 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) {
10640 /* Reduce our gain again to the bottom */
10641 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
10642 }
10643 bbr_log_exit_gain(bbr, cts, 4);
10644 /*
10645 * Extend out so we wait another
10646 * epoch before dropping again.
10647 */
10648 bbr->r_ctl.gain_epoch = cts;
10649 }
10650 if (flight <= bbr->r_ctl.rc_target_at_state) {
10651 if (bbr_sub_drain_slam_cwnd &&
10652 (bbr->rc_use_google == 0) &&
10653 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10654 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10655 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10656 }
10657 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10658 bbr_log_exit_gain(bbr, cts, 3);
10659 }
10660 } else {
10661 /* Its a gain */
10662 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) {
10663 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10664 goto change_state;
10665 }
10666 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) ||
10667 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >=
10668 bbr->rc_tp->snd_wnd)) {
10669 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10670 bbr_log_exit_gain(bbr, cts, 2);
10671 }
10672 }
10673 /**
10674 * We fall through and return always one of two things has
10675 * occured.
10676 * 1) We are still not at target
10677 * <or>
10678 * 2) We reached the target and set rc_bbr_state_atflight
10679 * which means we no longer hit this block
10680 * next time we are called.
10681 */
10682 return;
10683 }
10684 change_state:
10685 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time))
10686 return;
10687 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) {
10688 /* Less than a full time-period has passed */
10689 return;
10690 }
10691 if (bbr->r_ctl.rc_level_state_extra &&
10692 (bbr_state_val(bbr) > BBR_SUB_DRAIN) &&
10693 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10694 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10695 /* Less than a full time-period + extra has passed */
10696 return;
10697 }
10698 if (bbr_gain_gets_extra_too &&
10699 bbr->r_ctl.rc_level_state_extra &&
10700 (bbr_state_val(bbr) == BBR_SUB_GAIN) &&
10701 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10702 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10703 /* Less than a full time-period + extra has passed */
10704 return;
10705 }
10706 bbr_substate_change(bbr, cts, __LINE__, 1);
10707 }
10708
10709 static uint32_t
bbr_get_a_state_target(struct tcp_bbr * bbr,uint32_t gain)10710 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain)
10711 {
10712 uint32_t mss, tar;
10713
10714 if (bbr->rc_use_google) {
10715 /* Google just uses the cwnd target */
10716 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain);
10717 } else {
10718 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
10719 bbr->r_ctl.rc_pace_max_segs);
10720 /* Get the base cwnd with gain rounded to a mss */
10721 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr),
10722 gain), mss);
10723 /* Make sure it is within our min */
10724 if (tar < get_min_cwnd(bbr))
10725 return (get_min_cwnd(bbr));
10726 }
10727 return (tar);
10728 }
10729
10730 static void
bbr_set_state_target(struct tcp_bbr * bbr,int line)10731 bbr_set_state_target(struct tcp_bbr *bbr, int line)
10732 {
10733 uint32_t tar, meth;
10734
10735 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
10736 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
10737 /* Special case using old probe-rtt method */
10738 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10739 meth = 1;
10740 } else {
10741 /* Non-probe-rtt case and reduced probe-rtt */
10742 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
10743 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) {
10744 /* For gain cycle we use the hptsi gain */
10745 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10746 meth = 2;
10747 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) {
10748 /*
10749 * If configured, or for google all other states
10750 * get BBR_UNIT.
10751 */
10752 tar = bbr_get_a_state_target(bbr, BBR_UNIT);
10753 meth = 3;
10754 } else {
10755 /*
10756 * Or we set a target based on the pacing gain
10757 * for non-google mode and default (non-configured).
10758 * Note we don't set a target goal below drain (192).
10759 */
10760 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) {
10761 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]);
10762 meth = 4;
10763 } else {
10764 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10765 meth = 5;
10766 }
10767 }
10768 }
10769 bbr_log_set_of_state_target(bbr, tar, line, meth);
10770 bbr->r_ctl.rc_target_at_state = tar;
10771 }
10772
10773 static void
bbr_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts,int32_t line)10774 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
10775 {
10776 /* Change to probe_rtt */
10777 uint32_t time_in;
10778
10779 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10780 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10781 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10782 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain
10783 + bbr->r_ctl.rc_delivered);
10784 /* Setup so we force feed the filter */
10785 if (bbr->rc_use_google || bbr_probertt_sets_rtt)
10786 bbr->rc_prtt_set_ts = 1;
10787 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10788 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10789 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10790 }
10791 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0);
10792 bbr->r_ctl.rc_rtt_shrinks = cts;
10793 bbr->r_ctl.last_in_probertt = cts;
10794 bbr->r_ctl.rc_probertt_srttchktim = cts;
10795 bbr->r_ctl.rc_bbr_state_time = cts;
10796 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT;
10797 /* We need to force the filter to update */
10798
10799 if ((bbr_sub_drain_slam_cwnd) &&
10800 bbr->rc_hit_state_1 &&
10801 (bbr->rc_use_google == 0) &&
10802 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10803 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd)
10804 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10805 } else
10806 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10807 /* Update the lost */
10808 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10809 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){
10810 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */
10811 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10812 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10813 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10814 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10815 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6);
10816 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd;
10817 } else {
10818 /*
10819 * We bring it down slowly by using a hptsi gain that is
10820 * probably 75%. This will slowly float down our outstanding
10821 * without tampering with the cwnd.
10822 */
10823 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
10824 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10825 bbr_set_state_target(bbr, __LINE__);
10826 if (bbr_prtt_slam_cwnd &&
10827 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
10828 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10829 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10830 }
10831 }
10832 if (ctf_flight_size(bbr->rc_tp,
10833 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
10834 bbr->r_ctl.rc_target_at_state) {
10835 /* We are at target */
10836 bbr->r_ctl.rc_bbr_enters_probertt = cts;
10837 } else {
10838 /* We need to come down to reach target before our time begins */
10839 bbr->r_ctl.rc_bbr_enters_probertt = 0;
10840 }
10841 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch;
10842 BBR_STAT_INC(bbr_enter_probertt);
10843 bbr_log_exit_gain(bbr, cts, 0);
10844 bbr_log_type_statechange(bbr, cts, line);
10845 }
10846
10847 static void
bbr_check_probe_rtt_limits(struct tcp_bbr * bbr,uint32_t cts)10848 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts)
10849 {
10850 /*
10851 * Sanity check on probe-rtt intervals.
10852 * In crazy situations where we are competing
10853 * against new-reno flows with huge buffers
10854 * our rtt-prop interval could come to dominate
10855 * things if we can't get through a full set
10856 * of cycles, we need to adjust it.
10857 */
10858 if (bbr_can_adjust_probertt &&
10859 (bbr->rc_use_google == 0)) {
10860 uint16_t val = 0;
10861 uint32_t cur_rttp, fval, newval, baseval;
10862
10863 /* Are we to small and go into probe-rtt to often? */
10864 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1));
10865 cur_rttp = roundup(baseval, USECS_IN_SECOND);
10866 fval = bbr_filter_len_sec * USECS_IN_SECOND;
10867 if (bbr_is_ratio == 0) {
10868 if (fval > bbr_rtt_probe_limit)
10869 newval = cur_rttp + (fval - bbr_rtt_probe_limit);
10870 else
10871 newval = cur_rttp;
10872 } else {
10873 int mul;
10874
10875 mul = fval / bbr_rtt_probe_limit;
10876 newval = cur_rttp * mul;
10877 }
10878 if (cur_rttp > bbr->r_ctl.rc_probertt_int) {
10879 bbr->r_ctl.rc_probertt_int = cur_rttp;
10880 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10881 val = 1;
10882 } else {
10883 /*
10884 * No adjustments were made
10885 * do we need to shrink it?
10886 */
10887 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) {
10888 if (cur_rttp <= bbr_rtt_probe_limit) {
10889 /*
10890 * Things have calmed down lets
10891 * shrink all the way to default
10892 */
10893 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10894 reset_time_small(&bbr->r_ctl.rc_rttprop,
10895 (bbr_filter_len_sec * USECS_IN_SECOND));
10896 cur_rttp = bbr_rtt_probe_limit;
10897 newval = (bbr_filter_len_sec * USECS_IN_SECOND);
10898 val = 2;
10899 } else {
10900 /*
10901 * Well does some adjustment make sense?
10902 */
10903 if (cur_rttp < bbr->r_ctl.rc_probertt_int) {
10904 /* We can reduce interval time some */
10905 bbr->r_ctl.rc_probertt_int = cur_rttp;
10906 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10907 val = 3;
10908 }
10909 }
10910 }
10911 }
10912 if (val)
10913 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val);
10914 }
10915 }
10916
10917 static void
bbr_exit_probe_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)10918 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
10919 {
10920 /* Exit probe-rtt */
10921
10922 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) {
10923 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10924 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10925 }
10926 bbr_log_exit_gain(bbr, cts, 1);
10927 bbr->rc_hit_state_1 = 0;
10928 bbr->r_ctl.rc_rtt_shrinks = cts;
10929 bbr->r_ctl.last_in_probertt = cts;
10930 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0);
10931 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10932 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp,
10933 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
10934 bbr->r_ctl.rc_delivered);
10935 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10936 uint32_t time_in;
10937
10938 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10939 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10940 }
10941 if (bbr->rc_filled_pipe) {
10942 /* Switch to probe_bw */
10943 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
10944 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
10945 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10946 bbr_substate_change(bbr, cts, __LINE__, 0);
10947 bbr_log_type_statechange(bbr, cts, __LINE__);
10948 } else {
10949 /* Back to startup */
10950 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10951 bbr->r_ctl.rc_bbr_state_time = cts;
10952 /*
10953 * We don't want to give a complete free 3
10954 * measurements until we exit, so we use
10955 * the number of pe's we were in probe-rtt
10956 * to add to the startup_epoch. That way
10957 * we will still retain the old state.
10958 */
10959 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt);
10960 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10961 /* Make sure to use the lower pg when shifting back in */
10962 if (bbr->r_ctl.rc_lost &&
10963 bbr_use_lower_gain_in_startup &&
10964 (bbr->rc_use_google == 0))
10965 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
10966 else
10967 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
10968 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
10969 /* Probably not needed but set it anyway */
10970 bbr_set_state_target(bbr, __LINE__);
10971 bbr_log_type_statechange(bbr, cts, __LINE__);
10972 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10973 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0);
10974 }
10975 bbr_check_probe_rtt_limits(bbr, cts);
10976 }
10977
10978 static int32_t inline
bbr_should_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts)10979 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts)
10980 {
10981 if ((bbr->rc_past_init_win == 1) &&
10982 (bbr->rc_in_persist == 0) &&
10983 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) {
10984 return (1);
10985 }
10986 if (bbr_can_force_probertt &&
10987 (bbr->rc_in_persist == 0) &&
10988 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
10989 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
10990 return (1);
10991 }
10992 return (0);
10993 }
10994
10995 static int32_t
bbr_google_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t pkt_epoch)10996 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch)
10997 {
10998 uint64_t btlbw, gain;
10999 if (pkt_epoch == 0) {
11000 /*
11001 * Need to be on a pkt-epoch to continue.
11002 */
11003 return (0);
11004 }
11005 btlbw = bbr_get_full_bw(bbr);
11006 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11007 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11008 if (btlbw >= gain) {
11009 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11010 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11011 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11012 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11013 }
11014 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)
11015 return (1);
11016 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11017 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11018 return(0);
11019 }
11020
11021 static int32_t inline
bbr_state_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch)11022 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch)
11023 {
11024 /* Have we gained 25% in the last 3 packet based epoch's? */
11025 uint64_t btlbw, gain;
11026 int do_exit;
11027 int delta, rtt_gain;
11028
11029 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11030 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11031 /*
11032 * This qualifies as a RTT_PROBE session since we drop the
11033 * data outstanding to nothing and waited more than
11034 * bbr_rtt_probe_time.
11035 */
11036 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11037 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11038 }
11039 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11040 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11041 return (0);
11042 }
11043 if (bbr->rc_use_google)
11044 return (bbr_google_startup(bbr, cts, pkt_epoch));
11045
11046 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11047 (bbr_use_lower_gain_in_startup)) {
11048 /* Drop to a lower gain 1.5 x since we saw loss */
11049 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
11050 }
11051 if (pkt_epoch == 0) {
11052 /*
11053 * Need to be on a pkt-epoch to continue.
11054 */
11055 return (0);
11056 }
11057 if (bbr_rtt_gain_thresh) {
11058 /*
11059 * Do we allow a flow to stay
11060 * in startup with no loss and no
11061 * gain in rtt over a set threshold?
11062 */
11063 if (bbr->r_ctl.rc_pkt_epoch_rtt &&
11064 bbr->r_ctl.startup_last_srtt &&
11065 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) {
11066 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt;
11067 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt;
11068 } else
11069 rtt_gain = 0;
11070 if ((bbr->r_ctl.startup_last_srtt == 0) ||
11071 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt))
11072 /* First time or new lower value */
11073 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
11074
11075 if ((bbr->r_ctl.rc_lost == 0) &&
11076 (rtt_gain < bbr_rtt_gain_thresh)) {
11077 /*
11078 * No loss, and we are under
11079 * our gain threhold for
11080 * increasing RTT.
11081 */
11082 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11083 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11084 bbr_log_startup_event(bbr, cts, rtt_gain,
11085 delta, bbr->r_ctl.startup_last_srtt, 10);
11086 return (0);
11087 }
11088 }
11089 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) &&
11090 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) &&
11091 (!IN_RECOVERY(bbr->rc_tp->t_flags))) {
11092 /*
11093 * We only assess if we have a new measurment when
11094 * we have no loss and are not in recovery.
11095 * Drag up by one our last_startup epoch so we will hold
11096 * the number of non-gain we have already accumulated.
11097 */
11098 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11099 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11100 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11101 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9);
11102 return (0);
11103 }
11104 /* Case where we reduced the lost (bad retransmit) */
11105 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost)
11106 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11107 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count;
11108 btlbw = bbr_get_full_bw(bbr);
11109 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower)
11110 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11111 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11112 else
11113 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11114 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11115 do_exit = 0;
11116 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw)
11117 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11118 if (btlbw >= gain) {
11119 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11120 /* Update the lost so we won't exit in next set of tests */
11121 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11122 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11123 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11124 }
11125 if ((bbr->rc_loss_exit &&
11126 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11127 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) &&
11128 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) {
11129 /*
11130 * If we had no gain, we had loss and that loss was above
11131 * our threshould, the rwnd is not constrained, and we have
11132 * had at least 3 packet epochs exit. Note that this is
11133 * switched off by sysctl. Google does not do this by the
11134 * way.
11135 */
11136 if ((ctf_flight_size(bbr->rc_tp,
11137 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11138 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) {
11139 do_exit = 1;
11140 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11141 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4);
11142 } else {
11143 /* Just record an updated loss value */
11144 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11145 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11146 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5);
11147 }
11148 } else
11149 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11150 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) ||
11151 do_exit) {
11152 /* Return 1 to exit the startup state. */
11153 return (1);
11154 }
11155 /* Stay in startup */
11156 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11157 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11158 return (0);
11159 }
11160
11161 static void
bbr_state_change(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch,uint32_t losses)11162 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses)
11163 {
11164 /*
11165 * A tick occured in the rtt epoch do we need to do anything?
11166 */
11167 #ifdef BBR_INVARIANTS
11168 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
11169 (bbr->rc_bbr_state != BBR_STATE_DRAIN) &&
11170 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) &&
11171 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
11172 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) {
11173 /* Debug code? */
11174 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state);
11175 }
11176 #endif
11177 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
11178 /* Do we exit the startup state? */
11179 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) {
11180 uint32_t time_in;
11181
11182 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11183 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6);
11184 bbr->rc_filled_pipe = 1;
11185 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11186 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11187 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11188 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11189 } else
11190 time_in = 0;
11191 if (bbr->rc_no_pacing)
11192 bbr->rc_no_pacing = 0;
11193 bbr->r_ctl.rc_bbr_state_time = cts;
11194 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg;
11195 bbr->rc_bbr_state = BBR_STATE_DRAIN;
11196 bbr_set_state_target(bbr, __LINE__);
11197 if ((bbr->rc_use_google == 0) &&
11198 bbr_slam_cwnd_in_main_drain) {
11199 /* Here we don't have to worry about probe-rtt */
11200 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
11201 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11202 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11203 }
11204 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
11205 bbr_log_type_statechange(bbr, cts, __LINE__);
11206 if (ctf_flight_size(bbr->rc_tp,
11207 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
11208 bbr->r_ctl.rc_target_at_state) {
11209 /*
11210 * Switch to probe_bw if we are already
11211 * there
11212 */
11213 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11214 bbr_substate_change(bbr, cts, __LINE__, 0);
11215 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11216 bbr_log_type_statechange(bbr, cts, __LINE__);
11217 }
11218 }
11219 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) {
11220 uint32_t inflight;
11221 struct tcpcb *tp;
11222
11223 tp = bbr->rc_tp;
11224 inflight = ctf_flight_size(tp,
11225 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11226 if (inflight >= bbr->r_ctl.rc_target_at_state) {
11227 /* We have reached a flight of the cwnd target */
11228 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11229 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11230 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
11231 bbr_set_state_target(bbr, __LINE__);
11232 /*
11233 * Rig it so we don't do anything crazy and
11234 * start fresh with a new randomization.
11235 */
11236 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
11237 bbr->rc_bbr_substate = BBR_SUB_LEVEL6;
11238 bbr_substate_change(bbr, cts, __LINE__, 1);
11239 }
11240 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) {
11241 /* Has in-flight reached the bdp (or less)? */
11242 uint32_t inflight;
11243 struct tcpcb *tp;
11244
11245 tp = bbr->rc_tp;
11246 inflight = ctf_flight_size(tp,
11247 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11248 if ((bbr->rc_use_google == 0) &&
11249 bbr_slam_cwnd_in_main_drain &&
11250 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11251 /*
11252 * Here we don't have to worry about probe-rtt
11253 * re-slam it, but keep it slammed down.
11254 */
11255 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11256 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11257 }
11258 if (inflight <= bbr->r_ctl.rc_target_at_state) {
11259 /* We have drained */
11260 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11261 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11262 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11263 uint32_t time_in;
11264
11265 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11266 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11267 }
11268 if ((bbr->rc_use_google == 0) &&
11269 bbr_slam_cwnd_in_main_drain &&
11270 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
11271 /* Restore the cwnd */
11272 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11273 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11274 }
11275 /* Setup probe-rtt has being done now RRS-HERE */
11276 bbr->r_ctl.rc_rtt_shrinks = cts;
11277 bbr->r_ctl.last_in_probertt = cts;
11278 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0);
11279 /* Randomly pick a sub-state */
11280 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11281 bbr_substate_change(bbr, cts, __LINE__, 0);
11282 bbr_log_type_statechange(bbr, cts, __LINE__);
11283 }
11284 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) {
11285 uint32_t flight;
11286
11287 flight = ctf_flight_size(bbr->rc_tp,
11288 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11289 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered);
11290 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) &&
11291 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11292 /*
11293 * We must keep cwnd at the desired MSS.
11294 */
11295 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
11296 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11297 } else if ((bbr_prtt_slam_cwnd) &&
11298 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11299 /* Re-slam it */
11300 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11301 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11302 }
11303 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) {
11304 /* Has outstanding reached our target? */
11305 if (flight <= bbr->r_ctl.rc_target_at_state) {
11306 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0);
11307 bbr->r_ctl.rc_bbr_enters_probertt = cts;
11308 /* If time is exactly 0, be 1usec off */
11309 if (bbr->r_ctl.rc_bbr_enters_probertt == 0)
11310 bbr->r_ctl.rc_bbr_enters_probertt = 1;
11311 if (bbr->rc_use_google == 0) {
11312 /*
11313 * Restore any lowering that as occured to
11314 * reach here
11315 */
11316 if (bbr->r_ctl.bbr_rttprobe_gain_val)
11317 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
11318 else
11319 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11320 }
11321 }
11322 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) &&
11323 (bbr->rc_use_google == 0) &&
11324 bbr->r_ctl.bbr_rttprobe_gain_val &&
11325 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) ||
11326 (flight >= bbr->r_ctl.flightsize_at_drain))) {
11327 /*
11328 * We have doddled with our current hptsi
11329 * gain an srtt and have still not made it
11330 * to target, or we have increased our flight.
11331 * Lets reduce the gain by xx%
11332 * flooring the reduce at DRAIN (based on
11333 * mul/div)
11334 */
11335 int red;
11336
11337 bbr->r_ctl.flightsize_at_drain = flight;
11338 bbr->r_ctl.rc_probertt_srttchktim = cts;
11339 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1);
11340 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) {
11341 /* Reduce our gain again */
11342 bbr->r_ctl.rc_bbr_hptsi_gain -= red;
11343 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0);
11344 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) {
11345 /* one more chance before we give up */
11346 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
11347 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0);
11348 } else {
11349 /* At the very bottom */
11350 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1);
11351 }
11352 }
11353 }
11354 if (bbr->r_ctl.rc_bbr_enters_probertt &&
11355 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) &&
11356 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) {
11357 /* Time to exit probe RTT normally */
11358 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts);
11359 }
11360 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
11361 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11362 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11363 /*
11364 * This qualifies as a RTT_PROBE session since we
11365 * drop the data outstanding to nothing and waited
11366 * more than bbr_rtt_probe_time.
11367 */
11368 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11369 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11370 }
11371 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11372 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11373 } else {
11374 bbr_set_probebw_gains(bbr, cts, losses);
11375 }
11376 }
11377 }
11378
11379 static void
bbr_check_bbr_for_state(struct tcp_bbr * bbr,uint32_t cts,int32_t line,uint32_t losses)11380 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses)
11381 {
11382 int32_t epoch = 0;
11383
11384 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
11385 bbr_set_epoch(bbr, cts, line);
11386 /* At each epoch doe lt bw sampling */
11387 epoch = 1;
11388 }
11389 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses);
11390 }
11391
11392 static int
bbr_do_segment_nounlock(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos,int32_t nxt_pkt,struct timeval * tv)11393 bbr_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
11394 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
11395 int32_t nxt_pkt, struct timeval *tv)
11396 {
11397 int32_t thflags, retval;
11398 uint32_t cts, lcts;
11399 uint32_t tiwin;
11400 struct tcpopt to;
11401 struct tcp_bbr *bbr;
11402 struct bbr_sendmap *rsm;
11403 struct timeval ltv;
11404 int32_t did_out = 0;
11405 int32_t in_recovery;
11406 uint16_t nsegs;
11407 int32_t prev_state;
11408 uint32_t lost;
11409
11410 nsegs = max(1, m->m_pkthdr.lro_nsegs);
11411 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11412 /* add in our stats */
11413 kern_prefetch(bbr, &prev_state);
11414 prev_state = 0;
11415 thflags = th->th_flags;
11416 /*
11417 * If this is either a state-changing packet or current state isn't
11418 * established, we require a write lock on tcbinfo. Otherwise, we
11419 * allow the tcbinfo to be in either alocked or unlocked, as the
11420 * caller may have unnecessarily acquired a write lock due to a
11421 * race.
11422 */
11423 INP_WLOCK_ASSERT(tp->t_inpcb);
11424 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
11425 __func__));
11426 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
11427 __func__));
11428
11429 tp->t_rcvtime = ticks;
11430 /*
11431 * Unscale the window into a 32-bit value. For the SYN_SENT state
11432 * the scale is zero.
11433 */
11434 tiwin = th->th_win << tp->snd_scale;
11435 #ifdef STATS
11436 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
11437 #endif
11438
11439 if (m->m_flags & M_TSTMP) {
11440 /* Prefer the hardware timestamp if present */
11441 struct timespec ts;
11442
11443 mbuf_tstmp2timespec(m, &ts);
11444 bbr->rc_tv.tv_sec = ts.tv_sec;
11445 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11446 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11447 } else if (m->m_flags & M_TSTMP_LRO) {
11448 /* Next the arrival timestamp */
11449 struct timespec ts;
11450
11451 mbuf_tstmp2timespec(m, &ts);
11452 bbr->rc_tv.tv_sec = ts.tv_sec;
11453 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11454 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11455 } else {
11456 /*
11457 * Ok just get the current time.
11458 */
11459 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv);
11460 }
11461 /*
11462 * Parse options on any incoming segment.
11463 */
11464 tcp_dooptions(&to, (u_char *)(th + 1),
11465 (th->th_off << 2) - sizeof(struct tcphdr),
11466 (thflags & TH_SYN) ? TO_SYN : 0);
11467
11468 /*
11469 * If timestamps were negotiated during SYN/ACK and a
11470 * segment without a timestamp is received, silently drop
11471 * the segment, unless it is a RST segment or missing timestamps are
11472 * tolerated.
11473 * See section 3.2 of RFC 7323.
11474 */
11475 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
11476 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
11477 retval = 0;
11478 goto done_with_input;
11479 }
11480 /*
11481 * If echoed timestamp is later than the current time, fall back to
11482 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
11483 * were used when this connection was established.
11484 */
11485 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
11486 to.to_tsecr -= tp->ts_offset;
11487 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv)))
11488 to.to_tsecr = 0;
11489 }
11490 /*
11491 * If its the first time in we need to take care of options and
11492 * verify we can do SACK for rack!
11493 */
11494 if (bbr->r_state == 0) {
11495 /*
11496 * Process options only when we get SYN/ACK back. The SYN
11497 * case for incoming connections is handled in tcp_syncache.
11498 * According to RFC1323 the window field in a SYN (i.e., a
11499 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
11500 * this is traditional behavior, may need to be cleaned up.
11501 */
11502 if (bbr->rc_inp == NULL) {
11503 bbr->rc_inp = tp->t_inpcb;
11504 }
11505 /*
11506 * We need to init rc_inp here since its not init'd when
11507 * bbr_init is called
11508 */
11509 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
11510 if ((to.to_flags & TOF_SCALE) &&
11511 (tp->t_flags & TF_REQ_SCALE)) {
11512 tp->t_flags |= TF_RCVD_SCALE;
11513 tp->snd_scale = to.to_wscale;
11514 } else
11515 tp->t_flags &= ~TF_REQ_SCALE;
11516 /*
11517 * Initial send window. It will be updated with the
11518 * next incoming segment to the scaled value.
11519 */
11520 tp->snd_wnd = th->th_win;
11521 if ((to.to_flags & TOF_TS) &&
11522 (tp->t_flags & TF_REQ_TSTMP)) {
11523 tp->t_flags |= TF_RCVD_TSTMP;
11524 tp->ts_recent = to.to_tsval;
11525 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
11526 } else
11527 tp->t_flags &= ~TF_REQ_TSTMP;
11528 if (to.to_flags & TOF_MSS)
11529 tcp_mss(tp, to.to_mss);
11530 if ((tp->t_flags & TF_SACK_PERMIT) &&
11531 (to.to_flags & TOF_SACKPERM) == 0)
11532 tp->t_flags &= ~TF_SACK_PERMIT;
11533 if (IS_FASTOPEN(tp->t_flags)) {
11534 if (to.to_flags & TOF_FASTOPEN) {
11535 uint16_t mss;
11536
11537 if (to.to_flags & TOF_MSS)
11538 mss = to.to_mss;
11539 else
11540 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
11541 mss = TCP6_MSS;
11542 else
11543 mss = TCP_MSS;
11544 tcp_fastopen_update_cache(tp, mss,
11545 to.to_tfo_len, to.to_tfo_cookie);
11546 } else
11547 tcp_fastopen_disable_path(tp);
11548 }
11549 }
11550 /*
11551 * At this point we are at the initial call. Here we decide
11552 * if we are doing RACK or not. We do this by seeing if
11553 * TF_SACK_PERMIT is set, if not rack is *not* possible and
11554 * we switch to the default code.
11555 */
11556 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
11557 /* Bail */
11558 tcp_switch_back_to_default(tp);
11559 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
11560 tlen, iptos);
11561 return (1);
11562 }
11563 /* Set the flag */
11564 bbr->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
11565 tcp_set_hpts(tp->t_inpcb);
11566 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack);
11567 }
11568 if (thflags & TH_ACK) {
11569 /* Track ack types */
11570 if (to.to_flags & TOF_SACK)
11571 BBR_STAT_INC(bbr_acks_with_sacks);
11572 else
11573 BBR_STAT_INC(bbr_plain_acks);
11574 }
11575 /*
11576 * This is the one exception case where we set the rack state
11577 * always. All other times (timers etc) we must have a rack-state
11578 * set (so we assure we have done the checks above for SACK).
11579 */
11580 if (thflags & TH_FIN)
11581 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
11582 if (bbr->r_state != tp->t_state)
11583 bbr_set_state(tp, bbr, tiwin);
11584
11585 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL)
11586 kern_prefetch(rsm, &prev_state);
11587 prev_state = bbr->r_state;
11588 bbr->rc_ack_was_delayed = 0;
11589 lost = bbr->r_ctl.rc_lost;
11590 bbr->rc_is_pkt_epoch_now = 0;
11591 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) {
11592 /* Get the real time into lcts and figure the real delay */
11593 lcts = tcp_get_usecs(<v);
11594 if (TSTMP_GT(lcts, cts)) {
11595 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts;
11596 bbr->rc_ack_was_delayed = 1;
11597 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay,
11598 bbr->r_ctl.highest_hdwr_delay))
11599 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay;
11600 } else {
11601 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11602 bbr->rc_ack_was_delayed = 0;
11603 }
11604 } else {
11605 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11606 bbr->rc_ack_was_delayed = 0;
11607 }
11608 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m);
11609 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
11610 retval = 0;
11611 m_freem(m);
11612 goto done_with_input;
11613 }
11614 /*
11615 * If a segment with the ACK-bit set arrives in the SYN-SENT state
11616 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
11617 */
11618 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
11619 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
11620 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11621 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11622 return (1);
11623 }
11624 in_recovery = IN_RECOVERY(tp->t_flags);
11625 if (tiwin > bbr->r_ctl.rc_high_rwnd)
11626 bbr->r_ctl.rc_high_rwnd = tiwin;
11627 #ifdef BBR_INVARIANTS
11628 if ((tp->t_inpcb->inp_flags & INP_DROPPED) ||
11629 (tp->t_inpcb->inp_flags2 & INP_FREED)) {
11630 panic("tp:%p bbr:%p given a dropped inp:%p",
11631 tp, bbr, tp->t_inpcb);
11632 }
11633 #endif
11634 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp,
11635 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11636 bbr->rtt_valid = 0;
11637 if (to.to_flags & TOF_TS) {
11638 bbr->rc_ts_valid = 1;
11639 bbr->r_ctl.last_inbound_ts = to.to_tsval;
11640 } else {
11641 bbr->rc_ts_valid = 0;
11642 bbr->r_ctl.last_inbound_ts = 0;
11643 }
11644 retval = (*bbr->r_substate) (m, th, so,
11645 tp, &to, drop_hdrlen,
11646 tlen, tiwin, thflags, nxt_pkt, iptos);
11647 #ifdef BBR_INVARIANTS
11648 if ((retval == 0) &&
11649 (tp->t_inpcb == NULL)) {
11650 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
11651 retval, tp, prev_state);
11652 }
11653 #endif
11654 if (nxt_pkt == 0)
11655 BBR_STAT_INC(bbr_rlock_left_ret0);
11656 else
11657 BBR_STAT_INC(bbr_rlock_left_ret1);
11658 if (retval == 0) {
11659 /*
11660 * If retval is 1 the tcb is unlocked and most likely the tp
11661 * is gone.
11662 */
11663 INP_WLOCK_ASSERT(tp->t_inpcb);
11664 tcp_bbr_xmit_timer_commit(bbr, tp, cts);
11665 if (bbr->rc_is_pkt_epoch_now)
11666 bbr_set_pktepoch(bbr, cts, __LINE__);
11667 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost));
11668 if (nxt_pkt == 0) {
11669 if (bbr->r_wanted_output != 0) {
11670 bbr->rc_output_starts_timer = 0;
11671 did_out = 1;
11672 (void)tp->t_fb->tfb_tcp_output(tp);
11673 } else
11674 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0);
11675 }
11676 if ((nxt_pkt == 0) &&
11677 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11678 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11679 (tp->t_flags & TF_DELACK) ||
11680 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11681 (tp->t_state <= TCPS_CLOSING)))) {
11682 /*
11683 * We could not send (probably in the hpts but
11684 * stopped the timer)?
11685 */
11686 if ((tp->snd_max == tp->snd_una) &&
11687 ((tp->t_flags & TF_DELACK) == 0) &&
11688 (bbr->rc_inp->inp_in_hpts) &&
11689 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11690 /*
11691 * keep alive not needed if we are hptsi
11692 * output yet
11693 */
11694 ;
11695 } else {
11696 if (bbr->rc_inp->inp_in_hpts) {
11697 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
11698 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11699 (TSTMP_GT(lcts, bbr->rc_pacer_started))) {
11700 uint32_t del;
11701
11702 del = lcts - bbr->rc_pacer_started;
11703 if (bbr->r_ctl.rc_last_delay_val > del) {
11704 BBR_STAT_INC(bbr_force_timer_start);
11705 bbr->r_ctl.rc_last_delay_val -= del;
11706 bbr->rc_pacer_started = lcts;
11707 } else {
11708 /* We are late */
11709 bbr->r_ctl.rc_last_delay_val = 0;
11710 BBR_STAT_INC(bbr_force_output);
11711 (void)tp->t_fb->tfb_tcp_output(tp);
11712 }
11713 }
11714 }
11715 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val,
11716 0);
11717 }
11718 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) {
11719 /* Do we have the correct timer running? */
11720 bbr_timer_audit(tp, bbr, lcts, &so->so_snd);
11721 }
11722 /* Do we have a new state */
11723 if (bbr->r_state != tp->t_state)
11724 bbr_set_state(tp, bbr, tiwin);
11725 done_with_input:
11726 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out);
11727 if (did_out)
11728 bbr->r_wanted_output = 0;
11729 #ifdef BBR_INVARIANTS
11730 if (tp->t_inpcb == NULL) {
11731 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
11732 did_out,
11733 retval, tp, prev_state);
11734 }
11735 #endif
11736 }
11737 return (retval);
11738 }
11739
11740 static void
bbr_do_segment(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos)11741 bbr_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
11742 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11743 {
11744 struct timeval tv;
11745 int retval;
11746
11747 /* First lets see if we have old packets */
11748 if (tp->t_in_pkt) {
11749 if (ctf_do_queued_segments(so, tp, 1)) {
11750 m_freem(m);
11751 return;
11752 }
11753 }
11754 if (m->m_flags & M_TSTMP_LRO) {
11755 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
11756 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
11757 } else {
11758 /* Should not be should we kassert instead? */
11759 tcp_get_usecs(&tv);
11760 }
11761 retval = bbr_do_segment_nounlock(m, th, so, tp,
11762 drop_hdrlen, tlen, iptos, 0, &tv);
11763 if (retval == 0) {
11764 tcp_handle_wakeup(tp, so);
11765 INP_WUNLOCK(tp->t_inpcb);
11766 }
11767 }
11768
11769 /*
11770 * Return how much data can be sent without violating the
11771 * cwnd or rwnd.
11772 */
11773
11774 static inline uint32_t
bbr_what_can_we_send(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t sendwin,uint32_t avail,int32_t sb_offset,uint32_t cts)11775 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin,
11776 uint32_t avail, int32_t sb_offset, uint32_t cts)
11777 {
11778 uint32_t len;
11779
11780 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11781 /* We never want to go over our peers rcv-window */
11782 len = 0;
11783 } else {
11784 uint32_t flight;
11785
11786 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11787 if (flight >= sendwin) {
11788 /*
11789 * We have in flight what we are allowed by cwnd (if
11790 * it was rwnd blocking it would have hit above out
11791 * >= tp->snd_wnd).
11792 */
11793 return (0);
11794 }
11795 len = sendwin - flight;
11796 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11797 /* We would send too much (beyond the rwnd) */
11798 len = tp->snd_wnd - ctf_outstanding(tp);
11799 }
11800 if ((len + sb_offset) > avail) {
11801 /*
11802 * We don't have that much in the SB, how much is
11803 * there?
11804 */
11805 len = avail - sb_offset;
11806 }
11807 }
11808 return (len);
11809 }
11810
11811 static inline void
bbr_do_error_accounting(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,int32_t len,int32_t error)11812 bbr_do_error_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11813 {
11814 #ifdef NETFLIX_STATS
11815 KMOD_TCPSTAT_INC(tcps_sndpack_error);
11816 KMOD_TCPSTAT_ADD(tcps_sndbyte_error, len);
11817 #endif
11818 }
11819
11820 static inline void
bbr_do_send_accounting(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,int32_t len,int32_t error)11821 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11822 {
11823 if (error) {
11824 bbr_do_error_accounting(tp, bbr, rsm, len, error);
11825 return;
11826 }
11827 if (rsm) {
11828 if (rsm->r_flags & BBR_TLP) {
11829 /*
11830 * TLP should not count in retran count, but in its
11831 * own bin
11832 */
11833 #ifdef NETFLIX_STATS
11834 tp->t_sndtlppack++;
11835 tp->t_sndtlpbyte += len;
11836 KMOD_TCPSTAT_INC(tcps_tlpresends);
11837 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len);
11838 #endif
11839 } else {
11840 /* Retransmit */
11841 tp->t_sndrexmitpack++;
11842 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
11843 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
11844 #ifdef STATS
11845 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
11846 len);
11847 #endif
11848 }
11849 /*
11850 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is
11851 * sub-state
11852 */
11853 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len);
11854 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) {
11855 /* Non probe_bw log in 1, 2, or 4. */
11856 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len);
11857 } else {
11858 /*
11859 * Log our probe state 3, and log also 5-13 to show
11860 * us the recovery sub-state for the send. This
11861 * means that 3 == (5+6+7+8+9+10+11+12+13)
11862 */
11863 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len);
11864 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len);
11865 }
11866 /* Place in both 16's the totals of retransmitted */
11867 counter_u64_add(bbr_state_lost[16], len);
11868 counter_u64_add(bbr_state_resend[16], len);
11869 /* Place in 17's the total sent */
11870 counter_u64_add(bbr_state_resend[17], len);
11871 counter_u64_add(bbr_state_lost[17], len);
11872
11873 } else {
11874 /* New sends */
11875 KMOD_TCPSTAT_INC(tcps_sndpack);
11876 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
11877 /* Place in 17's the total sent */
11878 counter_u64_add(bbr_state_resend[17], len);
11879 counter_u64_add(bbr_state_lost[17], len);
11880 #ifdef STATS
11881 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
11882 len);
11883 #endif
11884 }
11885 }
11886
11887 static void
bbr_cwnd_limiting(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t in_level)11888 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level)
11889 {
11890 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) {
11891 /*
11892 * Limit the cwnd to not be above N x the target plus whats
11893 * is outstanding. The target is based on the current b/w
11894 * estimate.
11895 */
11896 uint32_t target;
11897
11898 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT);
11899 target += ctf_outstanding(tp);
11900 target *= bbr_target_cwnd_mult_limit;
11901 if (tp->snd_cwnd > target)
11902 tp->snd_cwnd = target;
11903 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__);
11904 }
11905 }
11906
11907 static int
bbr_window_update_needed(struct tcpcb * tp,struct socket * so,uint32_t recwin,int32_t maxseg)11908 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg)
11909 {
11910 /*
11911 * "adv" is the amount we could increase the window, taking into
11912 * account that we are limited by TCP_MAXWIN << tp->rcv_scale.
11913 */
11914 int32_t adv;
11915 int32_t oldwin;
11916
11917 adv = recwin;
11918 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
11919 oldwin = (tp->rcv_adv - tp->rcv_nxt);
11920 if (adv > oldwin)
11921 adv -= oldwin;
11922 else {
11923 /* We can't increase the window */
11924 adv = 0;
11925 }
11926 } else
11927 oldwin = 0;
11928
11929 /*
11930 * If the new window size ends up being the same as or less
11931 * than the old size when it is scaled, then don't force
11932 * a window update.
11933 */
11934 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
11935 return (0);
11936
11937 if (adv >= (2 * maxseg) &&
11938 (adv >= (so->so_rcv.sb_hiwat / 4) ||
11939 recwin <= (so->so_rcv.sb_hiwat / 8) ||
11940 so->so_rcv.sb_hiwat <= 8 * maxseg)) {
11941 return (1);
11942 }
11943 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
11944 return (1);
11945 return (0);
11946 }
11947
11948 /*
11949 * Return 0 on success and a errno on failure to send.
11950 * Note that a 0 return may not mean we sent anything
11951 * if the TCB was on the hpts. A non-zero return
11952 * does indicate the error we got from ip[6]_output.
11953 */
11954 static int
bbr_output_wtime(struct tcpcb * tp,const struct timeval * tv)11955 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv)
11956 {
11957 struct socket *so;
11958 int32_t len;
11959 uint32_t cts;
11960 uint32_t recwin, sendwin;
11961 int32_t sb_offset;
11962 int32_t flags, abandon, error = 0;
11963 struct tcp_log_buffer *lgb = NULL;
11964 struct mbuf *m;
11965 struct mbuf *mb;
11966 uint32_t if_hw_tsomaxsegcount = 0;
11967 uint32_t if_hw_tsomaxsegsize = 0;
11968 uint32_t if_hw_tsomax = 0;
11969 struct ip *ip = NULL;
11970 #ifdef TCPDEBUG
11971 struct ipovly *ipov = NULL;
11972 #endif
11973 struct tcp_bbr *bbr;
11974 struct tcphdr *th;
11975 #ifdef NETFLIX_TCPOUDP
11976 struct udphdr *udp = NULL;
11977 #endif
11978 u_char opt[TCP_MAXOLEN];
11979 unsigned ipoptlen, optlen, hdrlen;
11980 #ifdef NETFLIX_TCPOUDP
11981 unsigned ulen;
11982 #endif
11983 uint32_t bbr_seq;
11984 uint32_t delay_calc=0;
11985 uint8_t doing_tlp = 0;
11986 uint8_t local_options;
11987 #ifdef BBR_INVARIANTS
11988 uint8_t doing_retran_from = 0;
11989 uint8_t picked_up_retran = 0;
11990 #endif
11991 uint8_t wanted_cookie = 0;
11992 uint8_t more_to_rxt=0;
11993 int32_t prefetch_so_done = 0;
11994 int32_t prefetch_rsm = 0;
11995 uint32_t what_we_can = 0;
11996 uint32_t tot_len = 0;
11997 uint32_t rtr_cnt = 0;
11998 uint32_t maxseg, pace_max_segs, p_maxseg;
11999 int32_t csum_flags;
12000 int32_t hw_tls;
12001 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12002 unsigned ipsec_optlen = 0;
12003
12004 #endif
12005 volatile int32_t sack_rxmit;
12006 struct bbr_sendmap *rsm = NULL;
12007 int32_t tso, mtu;
12008 struct tcpopt to;
12009 int32_t slot = 0;
12010 struct inpcb *inp;
12011 struct sockbuf *sb;
12012 uint32_t hpts_calling;
12013 #ifdef INET6
12014 struct ip6_hdr *ip6 = NULL;
12015 int32_t isipv6;
12016 #endif
12017 uint8_t app_limited = BBR_JR_SENT_DATA;
12018 uint8_t filled_all = 0;
12019 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
12020 /* We take a cache hit here */
12021 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval));
12022 cts = tcp_tv_to_usectick(&bbr->rc_tv);
12023 inp = bbr->rc_inp;
12024 so = inp->inp_socket;
12025 sb = &so->so_snd;
12026 if (sb->sb_flags & SB_TLS_IFNET)
12027 hw_tls = 1;
12028 else
12029 hw_tls = 0;
12030 kern_prefetch(sb, &maxseg);
12031 maxseg = tp->t_maxseg - bbr->rc_last_options;
12032 if (bbr_minseg(bbr) < maxseg) {
12033 tcp_bbr_tso_size_check(bbr, cts);
12034 }
12035 /* Remove any flags that indicate we are pacing on the inp */
12036 pace_max_segs = bbr->r_ctl.rc_pace_max_segs;
12037 p_maxseg = min(maxseg, pace_max_segs);
12038 INP_WLOCK_ASSERT(inp);
12039 #ifdef TCP_OFFLOAD
12040 if (tp->t_flags & TF_TOE)
12041 return (tcp_offload_output(tp));
12042 #endif
12043
12044 #ifdef INET6
12045 if (bbr->r_state) {
12046 /* Use the cache line loaded if possible */
12047 isipv6 = bbr->r_is_v6;
12048 } else {
12049 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
12050 }
12051 #endif
12052 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
12053 inp->inp_in_hpts) {
12054 /*
12055 * We are on the hpts for some timer but not hptsi output.
12056 * Possibly remove from the hpts so we can send/recv etc.
12057 */
12058 if ((tp->t_flags & TF_ACKNOW) == 0) {
12059 /*
12060 * No immediate demand right now to send an ack, but
12061 * the user may have read, making room for new data
12062 * (a window update). If so we may want to cancel
12063 * whatever timer is running (KEEP/DEL-ACK?) and
12064 * continue to send out a window update. Or we may
12065 * have gotten more data into the socket buffer to
12066 * send.
12067 */
12068 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12069 (long)TCP_MAXWIN << tp->rcv_scale);
12070 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) &&
12071 ((tcp_outflags[tp->t_state] & TH_RST) == 0) &&
12072 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <=
12073 (tp->snd_max - tp->snd_una))) {
12074 /*
12075 * Nothing new to send and no window update
12076 * is needed to send. Lets just return and
12077 * let the timer-run off.
12078 */
12079 return (0);
12080 }
12081 }
12082 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12083 bbr_timer_cancel(bbr, __LINE__, cts);
12084 }
12085 if (bbr->r_ctl.rc_last_delay_val) {
12086 /* Calculate a rough delay for early escape to sending */
12087 if (SEQ_GT(cts, bbr->rc_pacer_started))
12088 delay_calc = cts - bbr->rc_pacer_started;
12089 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12090 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12091 else
12092 delay_calc = 0;
12093 }
12094 /* Mark that we have called bbr_output(). */
12095 if ((bbr->r_timer_override) ||
12096 (tp->t_state < TCPS_ESTABLISHED)) {
12097 /* Timeouts or early states are exempt */
12098 if (inp->inp_in_hpts)
12099 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12100 } else if (inp->inp_in_hpts) {
12101 if ((bbr->r_ctl.rc_last_delay_val) &&
12102 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
12103 delay_calc) {
12104 /*
12105 * We were being paced for output and the delay has
12106 * already exceeded when we were supposed to be
12107 * called, lets go ahead and pull out of the hpts
12108 * and call output.
12109 */
12110 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1);
12111 bbr->r_ctl.rc_last_delay_val = 0;
12112 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12113 } else if (tp->t_state == TCPS_CLOSED) {
12114 bbr->r_ctl.rc_last_delay_val = 0;
12115 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12116 } else {
12117 /*
12118 * On the hpts, you shall not pass! even if ACKNOW
12119 * is on, we will when the hpts fires, unless of
12120 * course we are overdue.
12121 */
12122 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1);
12123 return (0);
12124 }
12125 }
12126 bbr->rc_cwnd_limited = 0;
12127 if (bbr->r_ctl.rc_last_delay_val) {
12128 /* recalculate the real delay and deal with over/under */
12129 if (SEQ_GT(cts, bbr->rc_pacer_started))
12130 delay_calc = cts - bbr->rc_pacer_started;
12131 else
12132 delay_calc = 0;
12133 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12134 /* Setup the delay which will be added in */
12135 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12136 else {
12137 /*
12138 * We are early setup to adjust
12139 * our slot time.
12140 */
12141 uint64_t merged_val;
12142
12143 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc);
12144 bbr->r_agg_early_set = 1;
12145 if (bbr->r_ctl.rc_hptsi_agg_delay) {
12146 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) {
12147 /* Nope our previous late cancels out the early */
12148 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early;
12149 bbr->r_agg_early_set = 0;
12150 bbr->r_ctl.rc_agg_early = 0;
12151 } else {
12152 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay;
12153 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12154 }
12155 }
12156 merged_val = bbr->rc_pacer_started;
12157 merged_val <<= 32;
12158 merged_val |= bbr->r_ctl.rc_last_delay_val;
12159 bbr_log_pacing_delay_calc(bbr, inp->inp_hpts_calls,
12160 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val,
12161 bbr->r_agg_early_set, 3);
12162 bbr->r_ctl.rc_last_delay_val = 0;
12163 BBR_STAT_INC(bbr_early);
12164 delay_calc = 0;
12165 }
12166 } else {
12167 /* We were not delayed due to hptsi */
12168 if (bbr->r_agg_early_set)
12169 bbr->r_ctl.rc_agg_early = 0;
12170 bbr->r_agg_early_set = 0;
12171 delay_calc = 0;
12172 }
12173 if (delay_calc) {
12174 /*
12175 * We had a hptsi delay which means we are falling behind on
12176 * sending at the expected rate. Calculate an extra amount
12177 * of data we can send, if any, to put us back on track.
12178 */
12179 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay)
12180 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff;
12181 else
12182 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc;
12183 }
12184 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12185 if ((tp->snd_una == tp->snd_max) &&
12186 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
12187 (sbavail(sb))) {
12188 /*
12189 * Ok we have been idle with nothing outstanding
12190 * we possibly need to start fresh with either a new
12191 * suite of states or a fast-ramp up.
12192 */
12193 bbr_restart_after_idle(bbr,
12194 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time));
12195 }
12196 /*
12197 * Now was there a hptsi delay where we are behind? We only count
12198 * being behind if: a) We are not in recovery. b) There was a delay.
12199 * <and> c) We had room to send something.
12200 *
12201 */
12202 hpts_calling = inp->inp_hpts_calls;
12203 inp->inp_hpts_calls = 0;
12204 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
12205 if (bbr_process_timers(tp, bbr, cts, hpts_calling)) {
12206 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1);
12207 return (0);
12208 }
12209 }
12210 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
12211 if (hpts_calling &&
12212 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12213 bbr->r_ctl.rc_last_delay_val = 0;
12214 }
12215 bbr->r_timer_override = 0;
12216 bbr->r_wanted_output = 0;
12217 /*
12218 * For TFO connections in SYN_RECEIVED, only allow the initial
12219 * SYN|ACK and those sent by the retransmit timer.
12220 */
12221 if (IS_FASTOPEN(tp->t_flags) &&
12222 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12223 (tp->t_state == TCPS_SYN_SENT)) &&
12224 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12225 (tp->t_rxtshift == 0)) { /* not a retransmit */
12226 len = 0;
12227 goto just_return_nolock;
12228 }
12229 /*
12230 * Before sending anything check for a state update. For hpts
12231 * calling without input this is important. If its input calling
12232 * then this was already done.
12233 */
12234 if (bbr->rc_use_google == 0)
12235 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12236 again:
12237 /*
12238 * If we've recently taken a timeout, snd_max will be greater than
12239 * snd_max. BBR in general does not pay much attention to snd_nxt
12240 * for historic reasons the persist timer still uses it. This means
12241 * we have to look at it. All retransmissions that are not persits
12242 * use the rsm that needs to be sent so snd_nxt is ignored. At the
12243 * end of this routine we pull snd_nxt always up to snd_max.
12244 */
12245 doing_tlp = 0;
12246 #ifdef BBR_INVARIANTS
12247 doing_retran_from = picked_up_retran = 0;
12248 #endif
12249 error = 0;
12250 tso = 0;
12251 slot = 0;
12252 mtu = 0;
12253 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12254 sb_offset = tp->snd_max - tp->snd_una;
12255 flags = tcp_outflags[tp->t_state];
12256 sack_rxmit = 0;
12257 len = 0;
12258 rsm = NULL;
12259 if (flags & TH_RST) {
12260 SOCKBUF_LOCK(sb);
12261 goto send;
12262 }
12263 recheck_resend:
12264 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
12265 /* We need to always have one in reserve */
12266 rsm = bbr_alloc(bbr);
12267 if (rsm == NULL) {
12268 error = ENOMEM;
12269 /* Lie to get on the hpts */
12270 tot_len = tp->t_maxseg;
12271 if (hpts_calling)
12272 /* Retry in a ms */
12273 slot = 1001;
12274 goto just_return_nolock;
12275 }
12276 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
12277 bbr->r_ctl.rc_free_cnt++;
12278 rsm = NULL;
12279 }
12280 /* What do we send, a resend? */
12281 if (bbr->r_ctl.rc_resend == NULL) {
12282 /* Check for rack timeout */
12283 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
12284 if (bbr->r_ctl.rc_resend) {
12285 #ifdef BBR_INVARIANTS
12286 picked_up_retran = 1;
12287 #endif
12288 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend);
12289 }
12290 }
12291 if (bbr->r_ctl.rc_resend) {
12292 rsm = bbr->r_ctl.rc_resend;
12293 #ifdef BBR_INVARIANTS
12294 doing_retran_from = 1;
12295 #endif
12296 /* Remove any TLP flags its a RACK or T-O */
12297 rsm->r_flags &= ~BBR_TLP;
12298 bbr->r_ctl.rc_resend = NULL;
12299 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12300 #ifdef BBR_INVARIANTS
12301 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n",
12302 tp, bbr, rsm, rsm->r_start, tp->snd_una);
12303 goto recheck_resend;
12304 #else
12305 /* TSNH */
12306 rsm = NULL;
12307 goto recheck_resend;
12308 #endif
12309 }
12310 rtr_cnt++;
12311 if (rsm->r_flags & BBR_HAS_SYN) {
12312 /* Only retransmit a SYN by itself */
12313 len = 0;
12314 if ((flags & TH_SYN) == 0) {
12315 /* Huh something is wrong */
12316 rsm->r_start++;
12317 if (rsm->r_start == rsm->r_end) {
12318 /* Clean it up, somehow we missed the ack? */
12319 bbr_log_syn(tp, NULL);
12320 } else {
12321 /* TFO with data? */
12322 rsm->r_flags &= ~BBR_HAS_SYN;
12323 len = rsm->r_end - rsm->r_start;
12324 }
12325 } else {
12326 /* Retransmitting SYN */
12327 rsm = NULL;
12328 SOCKBUF_LOCK(sb);
12329 goto send;
12330 }
12331 } else
12332 len = rsm->r_end - rsm->r_start;
12333 if ((bbr->rc_resends_use_tso == 0) &&
12334 (len > maxseg)) {
12335 len = maxseg;
12336 more_to_rxt = 1;
12337 }
12338 sb_offset = rsm->r_start - tp->snd_una;
12339 if (len > 0) {
12340 sack_rxmit = 1;
12341 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12342 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12343 min(len, maxseg));
12344 } else {
12345 /* I dont think this can happen */
12346 rsm = NULL;
12347 goto recheck_resend;
12348 }
12349 BBR_STAT_INC(bbr_resends_set);
12350 } else if (bbr->r_ctl.rc_tlp_send) {
12351 /*
12352 * Tail loss probe
12353 */
12354 doing_tlp = 1;
12355 rsm = bbr->r_ctl.rc_tlp_send;
12356 bbr->r_ctl.rc_tlp_send = NULL;
12357 sack_rxmit = 1;
12358 len = rsm->r_end - rsm->r_start;
12359 rtr_cnt++;
12360 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12361 len = maxseg;
12362
12363 if (SEQ_GT(tp->snd_una, rsm->r_start)) {
12364 #ifdef BBR_INVARIANTS
12365 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u",
12366 tp, bbr, tp->snd_una, rsm, rsm->r_start);
12367 #else
12368 /* TSNH */
12369 rsm = NULL;
12370 goto recheck_resend;
12371 #endif
12372 }
12373 sb_offset = rsm->r_start - tp->snd_una;
12374 BBR_STAT_INC(bbr_tlp_set);
12375 }
12376 /*
12377 * Enforce a connection sendmap count limit if set
12378 * as long as we are not retransmiting.
12379 */
12380 if ((rsm == NULL) &&
12381 (V_tcp_map_entries_limit > 0) &&
12382 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12383 BBR_STAT_INC(bbr_alloc_limited);
12384 if (!bbr->alloc_limit_reported) {
12385 bbr->alloc_limit_reported = 1;
12386 BBR_STAT_INC(bbr_alloc_limited_conns);
12387 }
12388 goto just_return_nolock;
12389 }
12390 #ifdef BBR_INVARIANTS
12391 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) {
12392 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u",
12393 tp, bbr, rsm, sb_offset, len);
12394 }
12395 #endif
12396 /*
12397 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12398 * state flags.
12399 */
12400 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL))
12401 flags |= TH_FIN;
12402 if (tp->t_flags & TF_NEEDSYN)
12403 flags |= TH_SYN;
12404
12405 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) {
12406 /* we are retransmitting the fin */
12407 len--;
12408 if (len) {
12409 /*
12410 * When retransmitting data do *not* include the
12411 * FIN. This could happen from a TLP probe if we
12412 * allowed data with a FIN.
12413 */
12414 flags &= ~TH_FIN;
12415 }
12416 } else if (rsm) {
12417 if (flags & TH_FIN)
12418 flags &= ~TH_FIN;
12419 }
12420 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12421 void *end_rsm;
12422
12423 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
12424 if (end_rsm)
12425 kern_prefetch(end_rsm, &prefetch_rsm);
12426 prefetch_rsm = 1;
12427 }
12428 SOCKBUF_LOCK(sb);
12429 /*
12430 * If snd_nxt == snd_max and we have transmitted a FIN, the
12431 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12432 * negative length. This can also occur when TCP opens up its
12433 * congestion window while receiving additional duplicate acks after
12434 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12435 * the fast-retransmit.
12436 *
12437 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12438 * set to snd_una, the sb_offset will be 0, and the length may wind
12439 * up 0.
12440 *
12441 * If sack_rxmit is true we are retransmitting from the scoreboard
12442 * in which case len is already set.
12443 */
12444 if (sack_rxmit == 0) {
12445 uint32_t avail;
12446
12447 avail = sbavail(sb);
12448 if (SEQ_GT(tp->snd_max, tp->snd_una))
12449 sb_offset = tp->snd_max - tp->snd_una;
12450 else
12451 sb_offset = 0;
12452 if (bbr->rc_tlp_new_data) {
12453 /* TLP is forcing out new data */
12454 uint32_t tlplen;
12455
12456 doing_tlp = 1;
12457 tlplen = maxseg;
12458
12459 if (tlplen > (uint32_t)(avail - sb_offset)) {
12460 tlplen = (uint32_t)(avail - sb_offset);
12461 }
12462 if (tlplen > tp->snd_wnd) {
12463 len = tp->snd_wnd;
12464 } else {
12465 len = tlplen;
12466 }
12467 bbr->rc_tlp_new_data = 0;
12468 } else {
12469 what_we_can = len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts);
12470 if ((len < p_maxseg) &&
12471 (bbr->rc_in_persist == 0) &&
12472 (ctf_outstanding(tp) >= (2 * p_maxseg)) &&
12473 ((avail - sb_offset) >= p_maxseg)) {
12474 /*
12475 * We are not completing whats in the socket
12476 * buffer (i.e. there is at least a segment
12477 * waiting to send) and we have 2 or more
12478 * segments outstanding. There is no sense
12479 * of sending a little piece. Lets defer and
12480 * and wait until we can send a whole
12481 * segment.
12482 */
12483 len = 0;
12484 }
12485 if (bbr->rc_in_persist) {
12486 /*
12487 * We are in persists, figure out if
12488 * a retransmit is available (maybe the previous
12489 * persists we sent) or if we have to send new
12490 * data.
12491 */
12492 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
12493 if (rsm) {
12494 len = rsm->r_end - rsm->r_start;
12495 if (rsm->r_flags & BBR_HAS_FIN)
12496 len--;
12497 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12498 len = maxseg;
12499 if (len > 1)
12500 BBR_STAT_INC(bbr_persist_reneg);
12501 /*
12502 * XXXrrs we could force the len to
12503 * 1 byte here to cause the chunk to
12504 * split apart.. but that would then
12505 * mean we always retransmit it as
12506 * one byte even after the window
12507 * opens.
12508 */
12509 sack_rxmit = 1;
12510 sb_offset = rsm->r_start - tp->snd_una;
12511 } else {
12512 /*
12513 * First time through in persists or peer
12514 * acked our one byte. Though we do have
12515 * to have something in the sb.
12516 */
12517 len = 1;
12518 sb_offset = 0;
12519 if (avail == 0)
12520 len = 0;
12521 }
12522 }
12523 }
12524 }
12525 if (prefetch_so_done == 0) {
12526 kern_prefetch(so, &prefetch_so_done);
12527 prefetch_so_done = 1;
12528 }
12529 /*
12530 * Lop off SYN bit if it has already been sent. However, if this is
12531 * SYN-SENT state and if segment contains data and if we don't know
12532 * that foreign host supports TAO, suppress sending segment.
12533 */
12534 if ((flags & TH_SYN) && (rsm == NULL) &&
12535 SEQ_GT(tp->snd_max, tp->snd_una)) {
12536 if (tp->t_state != TCPS_SYN_RECEIVED)
12537 flags &= ~TH_SYN;
12538 /*
12539 * When sending additional segments following a TFO SYN|ACK,
12540 * do not include the SYN bit.
12541 */
12542 if (IS_FASTOPEN(tp->t_flags) &&
12543 (tp->t_state == TCPS_SYN_RECEIVED))
12544 flags &= ~TH_SYN;
12545 sb_offset--, len++;
12546 if (sbavail(sb) == 0)
12547 len = 0;
12548 } else if ((flags & TH_SYN) && rsm) {
12549 /*
12550 * Subtract one from the len for the SYN being
12551 * retransmitted.
12552 */
12553 len--;
12554 }
12555 /*
12556 * Be careful not to send data and/or FIN on SYN segments. This
12557 * measure is needed to prevent interoperability problems with not
12558 * fully conformant TCP implementations.
12559 */
12560 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12561 len = 0;
12562 flags &= ~TH_FIN;
12563 }
12564 /*
12565 * On TFO sockets, ensure no data is sent in the following cases:
12566 *
12567 * - When retransmitting SYN|ACK on a passively-created socket
12568 * - When retransmitting SYN on an actively created socket
12569 * - When sending a zero-length cookie (cookie request) on an
12570 * actively created socket
12571 * - When the socket is in the CLOSED state (RST is being sent)
12572 */
12573 if (IS_FASTOPEN(tp->t_flags) &&
12574 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12575 ((tp->t_state == TCPS_SYN_SENT) &&
12576 (tp->t_tfo_client_cookie_len == 0)) ||
12577 (flags & TH_RST))) {
12578 len = 0;
12579 sack_rxmit = 0;
12580 rsm = NULL;
12581 }
12582 /* Without fast-open there should never be data sent on a SYN */
12583 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags)))
12584 len = 0;
12585 if (len <= 0) {
12586 /*
12587 * If FIN has been sent but not acked, but we haven't been
12588 * called to retransmit, len will be < 0. Otherwise, window
12589 * shrank after we sent into it. If window shrank to 0,
12590 * cancel pending retransmit, pull snd_nxt back to (closed)
12591 * window, and set the persist timer if it isn't already
12592 * going. If the window didn't close completely, just wait
12593 * for an ACK.
12594 *
12595 * We also do a general check here to ensure that we will
12596 * set the persist timer when we have data to send, but a
12597 * 0-byte window. This makes sure the persist timer is set
12598 * even if the packet hits one of the "goto send" lines
12599 * below.
12600 */
12601 len = 0;
12602 if ((tp->snd_wnd == 0) &&
12603 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12604 (tp->snd_una == tp->snd_max) &&
12605 (sb_offset < (int)sbavail(sb))) {
12606 /*
12607 * Not enough room in the rwnd to send
12608 * a paced segment out.
12609 */
12610 bbr_enter_persist(tp, bbr, cts, __LINE__);
12611 }
12612 } else if ((rsm == NULL) &&
12613 (doing_tlp == 0) &&
12614 (len < bbr->r_ctl.rc_pace_max_segs)) {
12615 /*
12616 * We are not sending a full segment for
12617 * some reason. Should we not send anything (think
12618 * sws or persists)?
12619 */
12620 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12621 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12622 (len < (int)(sbavail(sb) - sb_offset))) {
12623 /*
12624 * Here the rwnd is less than
12625 * the pacing size, this is not a retransmit,
12626 * we are established and
12627 * the send is not the last in the socket buffer
12628 * lets not send, and possibly enter persists.
12629 */
12630 len = 0;
12631 if (tp->snd_max == tp->snd_una)
12632 bbr_enter_persist(tp, bbr, cts, __LINE__);
12633 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) &&
12634 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12635 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12636 (len < (int)(sbavail(sb) - sb_offset)) &&
12637 (len < bbr_minseg(bbr))) {
12638 /*
12639 * Here we are not retransmitting, and
12640 * the cwnd is not so small that we could
12641 * not send at least a min size (rxt timer
12642 * not having gone off), We have 2 segments or
12643 * more already in flight, its not the tail end
12644 * of the socket buffer and the cwnd is blocking
12645 * us from sending out minimum pacing segment size.
12646 * Lets not send anything.
12647 */
12648 bbr->rc_cwnd_limited = 1;
12649 len = 0;
12650 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12651 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12652 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12653 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12654 (len < (int)(sbavail(sb) - sb_offset)) &&
12655 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12656 /*
12657 * Here we have a send window but we have
12658 * filled it up and we can't send another pacing segment.
12659 * We also have in flight more than 2 segments
12660 * and we are not completing the sb i.e. we allow
12661 * the last bytes of the sb to go out even if
12662 * its not a full pacing segment.
12663 */
12664 len = 0;
12665 }
12666 }
12667 /* len will be >= 0 after this point. */
12668 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12669 tcp_sndbuf_autoscale(tp, so, sendwin);
12670 /*
12671 *
12672 */
12673 if (bbr->rc_in_persist &&
12674 len &&
12675 (rsm == NULL) &&
12676 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) {
12677 /*
12678 * We are in persist, not doing a retransmit and don't have enough space
12679 * yet to send a full TSO. So is it at the end of the sb
12680 * if so we need to send else nuke to 0 and don't send.
12681 */
12682 int sbleft;
12683 if (sbavail(sb) > sb_offset)
12684 sbleft = sbavail(sb) - sb_offset;
12685 else
12686 sbleft = 0;
12687 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) {
12688 /* not at end of sb lets not send */
12689 len = 0;
12690 }
12691 }
12692 /*
12693 * Decide if we can use TCP Segmentation Offloading (if supported by
12694 * hardware).
12695 *
12696 * TSO may only be used if we are in a pure bulk sending state. The
12697 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12698 * options prevent using TSO. With TSO the TCP header is the same
12699 * (except for the sequence number) for all generated packets. This
12700 * makes it impossible to transmit any options which vary per
12701 * generated segment or packet.
12702 *
12703 * IPv4 handling has a clear separation of ip options and ip header
12704 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen()
12705 * does the right thing below to provide length of just ip options
12706 * and thus checking for ipoptlen is enough to decide if ip options
12707 * are present.
12708 */
12709 #ifdef INET6
12710 if (isipv6)
12711 ipoptlen = ip6_optlen(inp);
12712 else
12713 #endif
12714 if (inp->inp_options)
12715 ipoptlen = inp->inp_options->m_len -
12716 offsetof(struct ipoption, ipopt_list);
12717 else
12718 ipoptlen = 0;
12719 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12720 /*
12721 * Pre-calculate here as we save another lookup into the darknesses
12722 * of IPsec that way and can actually decide if TSO is ok.
12723 */
12724 #ifdef INET6
12725 if (isipv6 && IPSEC_ENABLED(ipv6))
12726 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
12727 #ifdef INET
12728 else
12729 #endif
12730 #endif /* INET6 */
12731 #ifdef INET
12732 if (IPSEC_ENABLED(ipv4))
12733 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
12734 #endif /* INET */
12735 #endif /* IPSEC */
12736 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12737 ipoptlen += ipsec_optlen;
12738 #endif
12739 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
12740 (len > maxseg) &&
12741 (tp->t_port == 0) &&
12742 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12743 tp->rcv_numsacks == 0 &&
12744 ipoptlen == 0)
12745 tso = 1;
12746
12747 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12748 (long)TCP_MAXWIN << tp->rcv_scale);
12749 /*
12750 * Sender silly window avoidance. We transmit under the following
12751 * conditions when len is non-zero:
12752 *
12753 * - We have a full segment (or more with TSO) - This is the last
12754 * buffer in a write()/send() and we are either idle or running
12755 * NODELAY - we've timed out (e.g. persist timer) - we have more
12756 * then 1/2 the maximum send window's worth of data (receiver may be
12757 * limited the window size) - we need to retransmit
12758 */
12759 if (rsm)
12760 goto send;
12761 if (len) {
12762 if (sack_rxmit)
12763 goto send;
12764 if (len >= p_maxseg)
12765 goto send;
12766 /*
12767 * NOTE! on localhost connections an 'ack' from the remote
12768 * end may occur synchronously with the output and cause us
12769 * to flush a buffer queued with moretocome. XXX
12770 *
12771 */
12772 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */
12773 ((tp->t_flags & TF_NODELAY) ||
12774 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) &&
12775 (tp->t_flags & TF_NOPUSH) == 0) {
12776 goto send;
12777 }
12778 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12779 goto send;
12780 }
12781 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12782 goto send;
12783 }
12784 }
12785 /*
12786 * Sending of standalone window updates.
12787 *
12788 * Window updates are important when we close our window due to a
12789 * full socket buffer and are opening it again after the application
12790 * reads data from it. Once the window has opened again and the
12791 * remote end starts to send again the ACK clock takes over and
12792 * provides the most current window information.
12793 *
12794 * We must avoid the silly window syndrome whereas every read from
12795 * the receive buffer, no matter how small, causes a window update
12796 * to be sent. We also should avoid sending a flurry of window
12797 * updates when the socket buffer had queued a lot of data and the
12798 * application is doing small reads.
12799 *
12800 * Prevent a flurry of pointless window updates by only sending an
12801 * update when we can increase the advertized window by more than
12802 * 1/4th of the socket buffer capacity. When the buffer is getting
12803 * full or is very small be more aggressive and send an update
12804 * whenever we can increase by two mss sized segments. In all other
12805 * situations the ACK's to new incoming data will carry further
12806 * window increases.
12807 *
12808 * Don't send an independent window update if a delayed ACK is
12809 * pending (it will get piggy-backed on it) or the remote side
12810 * already has done a half-close and won't send more data. Skip
12811 * this if the connection is in T/TCP half-open state.
12812 */
12813 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
12814 !(tp->t_flags & TF_DELACK) &&
12815 !TCPS_HAVERCVDFIN(tp->t_state)) {
12816 /* Check to see if we should do a window update */
12817 if (bbr_window_update_needed(tp, so, recwin, maxseg))
12818 goto send;
12819 }
12820 /*
12821 * Send if we owe the peer an ACK, RST, SYN. ACKNOW
12822 * is also a catch-all for the retransmit timer timeout case.
12823 */
12824 if (tp->t_flags & TF_ACKNOW) {
12825 goto send;
12826 }
12827 if (flags & TH_RST) {
12828 /* Always send a RST if one is due */
12829 goto send;
12830 }
12831 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) {
12832 goto send;
12833 }
12834 /*
12835 * If our state indicates that FIN should be sent and we have not
12836 * yet done so, then we need to send.
12837 */
12838 if (flags & TH_FIN &&
12839 ((tp->t_flags & TF_SENTFIN) == 0)) {
12840 goto send;
12841 }
12842 /*
12843 * No reason to send a segment, just return.
12844 */
12845 just_return:
12846 SOCKBUF_UNLOCK(sb);
12847 just_return_nolock:
12848 if (tot_len)
12849 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
12850 if (bbr->rc_no_pacing)
12851 slot = 0;
12852 if (tot_len == 0) {
12853 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >=
12854 tp->snd_wnd) {
12855 BBR_STAT_INC(bbr_rwnd_limited);
12856 app_limited = BBR_JR_RWND_LIMITED;
12857 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12858 if ((bbr->rc_in_persist == 0) &&
12859 TCPS_HAVEESTABLISHED(tp->t_state) &&
12860 (tp->snd_max == tp->snd_una) &&
12861 sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
12862 /* No send window.. we must enter persist */
12863 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
12864 }
12865 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
12866 BBR_STAT_INC(bbr_app_limited);
12867 app_limited = BBR_JR_APP_LIMITED;
12868 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12869 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12870 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) {
12871 BBR_STAT_INC(bbr_cwnd_limited);
12872 app_limited = BBR_JR_CWND_LIMITED;
12873 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12874 bbr->r_ctl.rc_lost_bytes)));
12875 bbr->rc_cwnd_limited = 1;
12876 } else {
12877 BBR_STAT_INC(bbr_app_limited);
12878 app_limited = BBR_JR_APP_LIMITED;
12879 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12880 }
12881 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12882 bbr->r_agg_early_set = 0;
12883 bbr->r_ctl.rc_agg_early = 0;
12884 bbr->r_ctl.rc_last_delay_val = 0;
12885 } else if (bbr->rc_use_google == 0)
12886 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12887 /* Are we app limited? */
12888 if ((app_limited == BBR_JR_APP_LIMITED) ||
12889 (app_limited == BBR_JR_RWND_LIMITED)) {
12890 /**
12891 * We are application limited.
12892 */
12893 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12894 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered);
12895 }
12896 if (tot_len == 0)
12897 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1);
12898 /* Dont update the time if we did not send */
12899 bbr->r_ctl.rc_last_delay_val = 0;
12900 bbr->rc_output_starts_timer = 1;
12901 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len);
12902 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len);
12903 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12904 /* Make sure snd_nxt is drug up */
12905 tp->snd_nxt = tp->snd_max;
12906 }
12907 return (error);
12908
12909 send:
12910 if (doing_tlp == 0) {
12911 /*
12912 * Data not a TLP, and its not the rxt firing. If it is the
12913 * rxt firing, we want to leave the tlp_in_progress flag on
12914 * so we don't send another TLP. It has to be a rack timer
12915 * or normal send (response to acked data) to clear the tlp
12916 * in progress flag.
12917 */
12918 bbr->rc_tlp_in_progress = 0;
12919 bbr->rc_tlp_rtx_out = 0;
12920 } else {
12921 /*
12922 * Its a TLP.
12923 */
12924 bbr->rc_tlp_in_progress = 1;
12925 }
12926 bbr_timer_cancel(bbr, __LINE__, cts);
12927 if (rsm == NULL) {
12928 if (sbused(sb) > 0) {
12929 /*
12930 * This is sub-optimal. We only send a stand alone
12931 * FIN on its own segment.
12932 */
12933 if (flags & TH_FIN) {
12934 flags &= ~TH_FIN;
12935 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) {
12936 /* Lets not send this */
12937 slot = 0;
12938 goto just_return;
12939 }
12940 }
12941 }
12942 } else {
12943 /*
12944 * We do *not* send a FIN on a retransmit if it has data.
12945 * The if clause here where len > 1 should never come true.
12946 */
12947 if ((len > 0) &&
12948 (((rsm->r_flags & BBR_HAS_FIN) == 0) &&
12949 (flags & TH_FIN))) {
12950 flags &= ~TH_FIN;
12951 len--;
12952 }
12953 }
12954 SOCKBUF_LOCK_ASSERT(sb);
12955 if (len > 0) {
12956 if ((tp->snd_una == tp->snd_max) &&
12957 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
12958 /*
12959 * This qualifies as a RTT_PROBE session since we
12960 * drop the data outstanding to nothing and waited
12961 * more than bbr_rtt_probe_time.
12962 */
12963 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
12964 bbr_set_reduced_rtt(bbr, cts, __LINE__);
12965 }
12966 if (len >= maxseg)
12967 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
12968 else
12969 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
12970 }
12971 /*
12972 * Before ESTABLISHED, force sending of initial options unless TCP
12973 * set not to do any options. NOTE: we assume that the IP/TCP header
12974 * plus TCP options always fit in a single mbuf, leaving room for a
12975 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
12976 * + optlen <= MCLBYTES
12977 */
12978 optlen = 0;
12979 #ifdef INET6
12980 if (isipv6)
12981 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12982 else
12983 #endif
12984 hdrlen = sizeof(struct tcpiphdr);
12985
12986 /*
12987 * Compute options for segment. We only have to care about SYN and
12988 * established connection segments. Options for SYN-ACK segments
12989 * are handled in TCP syncache.
12990 */
12991 to.to_flags = 0;
12992 local_options = 0;
12993 if ((tp->t_flags & TF_NOOPT) == 0) {
12994 /* Maximum segment size. */
12995 if (flags & TH_SYN) {
12996 to.to_mss = tcp_mssopt(&inp->inp_inc);
12997 #ifdef NETFLIX_TCPOUDP
12998 if (tp->t_port)
12999 to.to_mss -= V_tcp_udp_tunneling_overhead;
13000 #endif
13001 to.to_flags |= TOF_MSS;
13002 /*
13003 * On SYN or SYN|ACK transmits on TFO connections,
13004 * only include the TFO option if it is not a
13005 * retransmit, as the presence of the TFO option may
13006 * have caused the original SYN or SYN|ACK to have
13007 * been dropped by a middlebox.
13008 */
13009 if (IS_FASTOPEN(tp->t_flags) &&
13010 (tp->t_rxtshift == 0)) {
13011 if (tp->t_state == TCPS_SYN_RECEIVED) {
13012 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
13013 to.to_tfo_cookie =
13014 (u_int8_t *)&tp->t_tfo_cookie.server;
13015 to.to_flags |= TOF_FASTOPEN;
13016 wanted_cookie = 1;
13017 } else if (tp->t_state == TCPS_SYN_SENT) {
13018 to.to_tfo_len =
13019 tp->t_tfo_client_cookie_len;
13020 to.to_tfo_cookie =
13021 tp->t_tfo_cookie.client;
13022 to.to_flags |= TOF_FASTOPEN;
13023 wanted_cookie = 1;
13024 }
13025 }
13026 }
13027 /* Window scaling. */
13028 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
13029 to.to_wscale = tp->request_r_scale;
13030 to.to_flags |= TOF_SCALE;
13031 }
13032 /* Timestamps. */
13033 if ((tp->t_flags & TF_RCVD_TSTMP) ||
13034 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
13035 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset;
13036 to.to_tsecr = tp->ts_recent;
13037 to.to_flags |= TOF_TS;
13038 local_options += TCPOLEN_TIMESTAMP + 2;
13039 }
13040 /* Set receive buffer autosizing timestamp. */
13041 if (tp->rfbuf_ts == 0 &&
13042 (so->so_rcv.sb_flags & SB_AUTOSIZE))
13043 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv);
13044 /* Selective ACK's. */
13045 if (flags & TH_SYN)
13046 to.to_flags |= TOF_SACKPERM;
13047 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13048 tp->rcv_numsacks > 0) {
13049 to.to_flags |= TOF_SACK;
13050 to.to_nsacks = tp->rcv_numsacks;
13051 to.to_sacks = (u_char *)tp->sackblks;
13052 }
13053 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13054 /* TCP-MD5 (RFC2385). */
13055 if (tp->t_flags & TF_SIGNATURE)
13056 to.to_flags |= TOF_SIGNATURE;
13057 #endif /* TCP_SIGNATURE */
13058
13059 /* Processing the options. */
13060 hdrlen += (optlen = tcp_addoptions(&to, opt));
13061 /*
13062 * If we wanted a TFO option to be added, but it was unable
13063 * to fit, ensure no data is sent.
13064 */
13065 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
13066 !(to.to_flags & TOF_FASTOPEN))
13067 len = 0;
13068 }
13069 #ifdef NETFLIX_TCPOUDP
13070 if (tp->t_port) {
13071 if (V_tcp_udp_tunneling_port == 0) {
13072 /* The port was removed?? */
13073 SOCKBUF_UNLOCK(&so->so_snd);
13074 return (EHOSTUNREACH);
13075 }
13076 hdrlen += sizeof(struct udphdr);
13077 }
13078 #endif
13079 #ifdef INET6
13080 if (isipv6)
13081 ipoptlen = ip6_optlen(tp->t_inpcb);
13082 else
13083 #endif
13084 if (tp->t_inpcb->inp_options)
13085 ipoptlen = tp->t_inpcb->inp_options->m_len -
13086 offsetof(struct ipoption, ipopt_list);
13087 else
13088 ipoptlen = 0;
13089 ipoptlen = 0;
13090 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13091 ipoptlen += ipsec_optlen;
13092 #endif
13093 if (bbr->rc_last_options != local_options) {
13094 /*
13095 * Cache the options length this generally does not change
13096 * on a connection. We use this to calculate TSO.
13097 */
13098 bbr->rc_last_options = local_options;
13099 }
13100 maxseg = tp->t_maxseg - (ipoptlen + optlen);
13101 p_maxseg = min(maxseg, pace_max_segs);
13102 /*
13103 * Adjust data length if insertion of options will bump the packet
13104 * length beyond the t_maxseg length. Clear the FIN bit because we
13105 * cut off the tail of the segment.
13106 */
13107 if (len > maxseg) {
13108 if (len != 0 && (flags & TH_FIN)) {
13109 flags &= ~TH_FIN;
13110 }
13111 if (tso) {
13112 uint32_t moff;
13113 int32_t max_len;
13114
13115 /* extract TSO information */
13116 if_hw_tsomax = tp->t_tsomax;
13117 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
13118 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
13119 KASSERT(ipoptlen == 0,
13120 ("%s: TSO can't do IP options", __func__));
13121
13122 /*
13123 * Check if we should limit by maximum payload
13124 * length:
13125 */
13126 if (if_hw_tsomax != 0) {
13127 /* compute maximum TSO length */
13128 max_len = (if_hw_tsomax - hdrlen -
13129 max_linkhdr);
13130 if (max_len <= 0) {
13131 len = 0;
13132 } else if (len > max_len) {
13133 len = max_len;
13134 }
13135 }
13136 /*
13137 * Prevent the last segment from being fractional
13138 * unless the send sockbuf can be emptied:
13139 */
13140 if ((sb_offset + len) < sbavail(sb)) {
13141 moff = len % (uint32_t)maxseg;
13142 if (moff != 0) {
13143 len -= moff;
13144 }
13145 }
13146 /*
13147 * In case there are too many small fragments don't
13148 * use TSO:
13149 */
13150 if (len <= maxseg) {
13151 len = maxseg;
13152 tso = 0;
13153 }
13154 } else {
13155 /* Not doing TSO */
13156 if (optlen + ipoptlen >= tp->t_maxseg) {
13157 /*
13158 * Since we don't have enough space to put
13159 * the IP header chain and the TCP header in
13160 * one packet as required by RFC 7112, don't
13161 * send it. Also ensure that at least one
13162 * byte of the payload can be put into the
13163 * TCP segment.
13164 */
13165 SOCKBUF_UNLOCK(&so->so_snd);
13166 error = EMSGSIZE;
13167 sack_rxmit = 0;
13168 goto out;
13169 }
13170 len = maxseg;
13171 }
13172 } else {
13173 /* Not doing TSO */
13174 if_hw_tsomaxsegcount = 0;
13175 tso = 0;
13176 }
13177 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13178 ("%s: len > IP_MAXPACKET", __func__));
13179 #ifdef DIAGNOSTIC
13180 #ifdef INET6
13181 if (max_linkhdr + hdrlen > MCLBYTES)
13182 #else
13183 if (max_linkhdr + hdrlen > MHLEN)
13184 #endif
13185 panic("tcphdr too big");
13186 #endif
13187 /*
13188 * This KASSERT is here to catch edge cases at a well defined place.
13189 * Before, those had triggered (random) panic conditions further
13190 * down.
13191 */
13192 #ifdef BBR_INVARIANTS
13193 if (sack_rxmit) {
13194 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
13195 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u",
13196 rsm, tp, bbr, rsm->r_start, tp->snd_una);
13197 }
13198 }
13199 #endif
13200 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13201 if ((len == 0) &&
13202 (flags & TH_FIN) &&
13203 (sbused(sb))) {
13204 /*
13205 * We have outstanding data, don't send a fin by itself!.
13206 */
13207 slot = 0;
13208 goto just_return;
13209 }
13210 /*
13211 * Grab a header mbuf, attaching a copy of data to be transmitted,
13212 * and initialize the header from the template for sends on this
13213 * connection.
13214 */
13215 if (len) {
13216 uint32_t moff;
13217 uint32_t orig_len;
13218
13219 /*
13220 * We place a limit on sending with hptsi.
13221 */
13222 if ((rsm == NULL) && len > pace_max_segs)
13223 len = pace_max_segs;
13224 if (len <= maxseg)
13225 tso = 0;
13226 #ifdef INET6
13227 if (MHLEN < hdrlen + max_linkhdr)
13228 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13229 else
13230 #endif
13231 m = m_gethdr(M_NOWAIT, MT_DATA);
13232
13233 if (m == NULL) {
13234 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13235 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13236 SOCKBUF_UNLOCK(sb);
13237 error = ENOBUFS;
13238 sack_rxmit = 0;
13239 goto out;
13240 }
13241 m->m_data += max_linkhdr;
13242 m->m_len = hdrlen;
13243 /*
13244 * Start the m_copy functions from the closest mbuf to the
13245 * sb_offset in the socket buffer chain.
13246 */
13247 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) {
13248 #ifdef BBR_INVARIANTS
13249 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0)))
13250 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u",
13251 tp, bbr, len, sb_offset, sbavail(sb), rsm,
13252 doing_retran_from,
13253 picked_up_retran,
13254 doing_tlp);
13255
13256 #endif
13257 /*
13258 * In this messed up situation we have two choices,
13259 * a) pretend the send worked, and just start timers
13260 * and what not (not good since that may lead us
13261 * back here a lot). <or> b) Send the lowest segment
13262 * in the map. <or> c) Drop the connection. Lets do
13263 * <b> which if it continues to happen will lead to
13264 * <c> via timeouts.
13265 */
13266 BBR_STAT_INC(bbr_offset_recovery);
13267 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
13268 sb_offset = 0;
13269 if (rsm == NULL) {
13270 sack_rxmit = 0;
13271 len = sbavail(sb);
13272 } else {
13273 sack_rxmit = 1;
13274 if (rsm->r_start != tp->snd_una) {
13275 /*
13276 * Things are really messed up, <c>
13277 * is the only thing to do.
13278 */
13279 BBR_STAT_INC(bbr_offset_drop);
13280 tcp_set_inp_to_drop(inp, EFAULT);
13281 SOCKBUF_UNLOCK(sb);
13282 (void)m_free(m);
13283 return (0);
13284 }
13285 len = rsm->r_end - rsm->r_start;
13286 }
13287 if (len > sbavail(sb))
13288 len = sbavail(sb);
13289 if (len > maxseg)
13290 len = maxseg;
13291 }
13292 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13293 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13294 m_copydata(mb, moff, (int)len,
13295 mtod(m, caddr_t)+hdrlen);
13296 if (rsm == NULL)
13297 sbsndptr_adv(sb, mb, len);
13298 m->m_len += len;
13299 } else {
13300 struct sockbuf *msb;
13301
13302 if (rsm)
13303 msb = NULL;
13304 else
13305 msb = sb;
13306 #ifdef BBR_INVARIANTS
13307 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) {
13308 if (rsm) {
13309 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 ",
13310 tp, bbr, len, moff,
13311 sbavail(sb), rsm,
13312 tp->snd_una, rsm->r_flags, rsm->r_start,
13313 doing_retran_from,
13314 picked_up_retran,
13315 doing_tlp, sack_rxmit);
13316 } else {
13317 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u",
13318 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una);
13319 }
13320 }
13321 #endif
13322 orig_len = len;
13323 m->m_next = tcp_m_copym(
13324 mb, moff, &len,
13325 if_hw_tsomaxsegcount,
13326 if_hw_tsomaxsegsize, msb,
13327 ((rsm == NULL) ? hw_tls : 0)
13328 #ifdef NETFLIX_COPY_ARGS
13329 , &filled_all
13330 #endif
13331 );
13332 if (len <= maxseg) {
13333 /*
13334 * Must have ran out of mbufs for the copy
13335 * shorten it to no longer need tso. Lets
13336 * not put on sendalot since we are low on
13337 * mbufs.
13338 */
13339 tso = 0;
13340 }
13341 if (m->m_next == NULL) {
13342 SOCKBUF_UNLOCK(sb);
13343 (void)m_free(m);
13344 error = ENOBUFS;
13345 sack_rxmit = 0;
13346 goto out;
13347 }
13348 }
13349 #ifdef BBR_INVARIANTS
13350 if (tso && len < maxseg) {
13351 panic("tp:%p tso on, but len:%d < maxseg:%d",
13352 tp, len, maxseg);
13353 }
13354 if (tso && if_hw_tsomaxsegcount) {
13355 int32_t seg_cnt = 0;
13356 struct mbuf *foo;
13357
13358 foo = m;
13359 while (foo) {
13360 seg_cnt++;
13361 foo = foo->m_next;
13362 }
13363 if (seg_cnt > if_hw_tsomaxsegcount) {
13364 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount);
13365 }
13366 }
13367 #endif
13368 /*
13369 * If we're sending everything we've got, set PUSH. (This
13370 * will keep happy those implementations which only give
13371 * data to the user when a buffer fills or a PUSH comes in.)
13372 */
13373 if (sb_offset + len == sbused(sb) &&
13374 sbused(sb) &&
13375 !(flags & TH_SYN)) {
13376 flags |= TH_PUSH;
13377 }
13378 SOCKBUF_UNLOCK(sb);
13379 } else {
13380 SOCKBUF_UNLOCK(sb);
13381 if (tp->t_flags & TF_ACKNOW)
13382 KMOD_TCPSTAT_INC(tcps_sndacks);
13383 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13384 KMOD_TCPSTAT_INC(tcps_sndctrl);
13385 else
13386 KMOD_TCPSTAT_INC(tcps_sndwinup);
13387
13388 m = m_gethdr(M_NOWAIT, MT_DATA);
13389 if (m == NULL) {
13390 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13391 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13392 error = ENOBUFS;
13393 /* Fudge the send time since we could not send */
13394 sack_rxmit = 0;
13395 goto out;
13396 }
13397 #ifdef INET6
13398 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13399 MHLEN >= hdrlen) {
13400 M_ALIGN(m, hdrlen);
13401 } else
13402 #endif
13403 m->m_data += max_linkhdr;
13404 m->m_len = hdrlen;
13405 }
13406 SOCKBUF_UNLOCK_ASSERT(sb);
13407 m->m_pkthdr.rcvif = (struct ifnet *)0;
13408 #ifdef MAC
13409 mac_inpcb_create_mbuf(inp, m);
13410 #endif
13411 #ifdef INET6
13412 if (isipv6) {
13413 ip6 = mtod(m, struct ip6_hdr *);
13414 #ifdef NETFLIX_TCPOUDP
13415 if (tp->t_port) {
13416 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr));
13417 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13418 udp->uh_dport = tp->t_port;
13419 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13420 udp->uh_ulen = htons(ulen);
13421 th = (struct tcphdr *)(udp + 1);
13422 } else {
13423 #endif
13424 th = (struct tcphdr *)(ip6 + 1);
13425
13426 #ifdef NETFLIX_TCPOUDP
13427 }
13428 #endif
13429 tcpip_fillheaders(inp,
13430 #ifdef NETFLIX_TCPOUDP
13431 tp->t_port,
13432 #endif
13433 ip6, th);
13434 } else
13435 #endif /* INET6 */
13436 {
13437 ip = mtod(m, struct ip *);
13438 #ifdef TCPDEBUG
13439 ipov = (struct ipovly *)ip;
13440 #endif
13441 #ifdef NETFLIX_TCPOUDP
13442 if (tp->t_port) {
13443 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip));
13444 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13445 udp->uh_dport = tp->t_port;
13446 ulen = hdrlen + len - sizeof(struct ip);
13447 udp->uh_ulen = htons(ulen);
13448 th = (struct tcphdr *)(udp + 1);
13449 } else
13450 #endif
13451 th = (struct tcphdr *)(ip + 1);
13452 tcpip_fillheaders(inp,
13453 #ifdef NETFLIX_TCPOUDP
13454 tp->t_port,
13455 #endif
13456 ip, th);
13457 }
13458 /*
13459 * If we are doing retransmissions, then snd_nxt will not reflect
13460 * the first unsent octet. For ACK only packets, we do not want the
13461 * sequence number of the retransmitted packet, we want the sequence
13462 * number of the next unsent octet. So, if there is no data (and no
13463 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13464 * ti_seq. But if we are in persist state, snd_max might reflect
13465 * one byte beyond the right edge of the window, so use snd_nxt in
13466 * that case, since we know we aren't doing a retransmission.
13467 * (retransmit and persist are mutually exclusive...)
13468 */
13469 if (sack_rxmit == 0) {
13470 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) {
13471 /* New data (including new persists) */
13472 th->th_seq = htonl(tp->snd_max);
13473 bbr_seq = tp->snd_max;
13474 } else if (flags & TH_SYN) {
13475 /* Syn's always send from iss */
13476 th->th_seq = htonl(tp->iss);
13477 bbr_seq = tp->iss;
13478 } else if (flags & TH_FIN) {
13479 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) {
13480 /*
13481 * If we sent the fin already its 1 minus
13482 * snd_max
13483 */
13484 th->th_seq = (htonl(tp->snd_max - 1));
13485 bbr_seq = (tp->snd_max - 1);
13486 } else {
13487 /* First time FIN use snd_max */
13488 th->th_seq = htonl(tp->snd_max);
13489 bbr_seq = tp->snd_max;
13490 }
13491 } else if (flags & TH_RST) {
13492 /*
13493 * For a Reset send the last cum ack in sequence
13494 * (this like any other choice may still generate a
13495 * challenge ack, if a ack-update packet is in
13496 * flight).
13497 */
13498 th->th_seq = htonl(tp->snd_una);
13499 bbr_seq = tp->snd_una;
13500 } else {
13501 /*
13502 * len == 0 and not persist we use snd_max, sending
13503 * an ack unless we have sent the fin then its 1
13504 * minus.
13505 */
13506 /*
13507 * XXXRRS Question if we are in persists and we have
13508 * nothing outstanding to send and we have not sent
13509 * a FIN, we will send an ACK. In such a case it
13510 * might be better to send (tp->snd_una - 1) which
13511 * would force the peer to ack.
13512 */
13513 if (tp->t_flags & TF_SENTFIN) {
13514 th->th_seq = htonl(tp->snd_max - 1);
13515 bbr_seq = (tp->snd_max - 1);
13516 } else {
13517 th->th_seq = htonl(tp->snd_max);
13518 bbr_seq = tp->snd_max;
13519 }
13520 }
13521 } else {
13522 /* All retransmits use the rsm to guide the send */
13523 th->th_seq = htonl(rsm->r_start);
13524 bbr_seq = rsm->r_start;
13525 }
13526 th->th_ack = htonl(tp->rcv_nxt);
13527 if (optlen) {
13528 bcopy(opt, th + 1, optlen);
13529 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13530 }
13531 th->th_flags = flags;
13532 /*
13533 * Calculate receive window. Don't shrink window, but avoid silly
13534 * window syndrome.
13535 */
13536 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) &&
13537 recwin < maxseg)))
13538 recwin = 0;
13539 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13540 recwin < (tp->rcv_adv - tp->rcv_nxt))
13541 recwin = (tp->rcv_adv - tp->rcv_nxt);
13542 if (recwin > TCP_MAXWIN << tp->rcv_scale)
13543 recwin = TCP_MAXWIN << tp->rcv_scale;
13544
13545 /*
13546 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13547 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13548 * handled in syncache.
13549 */
13550 if (flags & TH_SYN)
13551 th->th_win = htons((u_short)
13552 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13553 else {
13554 /* Avoid shrinking window with window scaling. */
13555 recwin = roundup2(recwin, 1 << tp->rcv_scale);
13556 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13557 }
13558 /*
13559 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13560 * window. This may cause the remote transmitter to stall. This
13561 * flag tells soreceive() to disable delayed acknowledgements when
13562 * draining the buffer. This can occur if the receiver is
13563 * attempting to read more data than can be buffered prior to
13564 * transmitting on the connection.
13565 */
13566 if (th->th_win == 0) {
13567 tp->t_sndzerowin++;
13568 tp->t_flags |= TF_RXWIN0SENT;
13569 } else
13570 tp->t_flags &= ~TF_RXWIN0SENT;
13571 /*
13572 * We don't support urgent data, but drag along
13573 * the pointer in case of a stack switch.
13574 */
13575 tp->snd_up = tp->snd_una;
13576
13577 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13578 if (to.to_flags & TOF_SIGNATURE) {
13579 /*
13580 * Calculate MD5 signature and put it into the place
13581 * determined before. NOTE: since TCP options buffer doesn't
13582 * point into mbuf's data, calculate offset and use it.
13583 */
13584 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13585 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13586 /*
13587 * Do not send segment if the calculation of MD5
13588 * digest has failed.
13589 */
13590 goto out;
13591 }
13592 }
13593 #endif
13594
13595 /*
13596 * Put TCP length in extended header, and then checksum extended
13597 * header and data.
13598 */
13599 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13600 #ifdef INET6
13601 if (isipv6) {
13602 /*
13603 * ip6_plen is not need to be filled now, and will be filled
13604 * in ip6_output.
13605 */
13606 #ifdef NETFLIX_TCPOUDP
13607 if (tp->t_port) {
13608 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13609 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13610 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13611 th->th_sum = htons(0);
13612 UDPSTAT_INC(udps_opackets);
13613 } else {
13614 #endif
13615 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13616 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13617 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
13618 optlen + len, IPPROTO_TCP, 0);
13619 #ifdef NETFLIX_TCPOUDP
13620 }
13621 #endif
13622 }
13623 #endif
13624 #if defined(INET6) && defined(INET)
13625 else
13626 #endif
13627 #ifdef INET
13628 {
13629 #ifdef NETFLIX_TCPOUDP
13630 if (tp->t_port) {
13631 m->m_pkthdr.csum_flags = CSUM_UDP;
13632 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13633 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13634 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13635 th->th_sum = htons(0);
13636 UDPSTAT_INC(udps_opackets);
13637 } else {
13638 #endif
13639 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP;
13640 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13641 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13642 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13643 IPPROTO_TCP + len + optlen));
13644 #ifdef NETFLIX_TCPOUDP
13645 }
13646 #endif
13647 /* IP version must be set here for ipv4/ipv6 checking later */
13648 KASSERT(ip->ip_v == IPVERSION,
13649 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13650 }
13651 #endif
13652
13653 /*
13654 * Enable TSO and specify the size of the segments. The TCP pseudo
13655 * header checksum is always provided. XXX: Fixme: This is currently
13656 * not the case for IPv6.
13657 */
13658 if (tso) {
13659 KASSERT(len > maxseg,
13660 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg));
13661 m->m_pkthdr.csum_flags |= CSUM_TSO;
13662 csum_flags |= CSUM_TSO;
13663 m->m_pkthdr.tso_segsz = maxseg;
13664 }
13665 KASSERT(len + hdrlen == m_length(m, NULL),
13666 ("%s: mbuf chain different than expected: %d + %u != %u",
13667 __func__, len, hdrlen, m_length(m, NULL)));
13668
13669 #ifdef TCP_HHOOK
13670 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */
13671 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13672 #endif
13673 #ifdef TCPDEBUG
13674 /*
13675 * Trace.
13676 */
13677 if (so->so_options & SO_DEBUG) {
13678 u_short save = 0;
13679
13680 #ifdef INET6
13681 if (!isipv6)
13682 #endif
13683 {
13684 save = ipov->ih_len;
13685 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen +
13686 * (th->th_off << 2) */ );
13687 }
13688 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13689 #ifdef INET6
13690 if (!isipv6)
13691 #endif
13692 ipov->ih_len = save;
13693 }
13694 #endif /* TCPDEBUG */
13695
13696 /* Log to the black box */
13697 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13698 union tcp_log_stackspecific log;
13699
13700 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
13701 /* Record info on type of transmission */
13702 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay;
13703 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3);
13704 log.u_bbr.flex3 = maxseg;
13705 log.u_bbr.flex4 = delay_calc;
13706 /* Encode filled_all into the upper flex5 bit */
13707 log.u_bbr.flex5 = bbr->rc_past_init_win;
13708 log.u_bbr.flex5 <<= 1;
13709 log.u_bbr.flex5 |= bbr->rc_no_pacing;
13710 log.u_bbr.flex5 <<= 29;
13711 if (filled_all)
13712 log.u_bbr.flex5 |= 0x80000000;
13713 log.u_bbr.flex5 |= tp->t_maxseg;
13714 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs;
13715 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr);
13716 /* lets poke in the low and the high here for debugging */
13717 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
13718 if (rsm || sack_rxmit) {
13719 if (doing_tlp)
13720 log.u_bbr.flex8 = 2;
13721 else
13722 log.u_bbr.flex8 = 1;
13723 } else {
13724 log.u_bbr.flex8 = 0;
13725 }
13726 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13727 len, &log, false, NULL, NULL, 0, tv);
13728 } else {
13729 lgb = NULL;
13730 }
13731 /*
13732 * Fill in IP length and desired time to live and send to IP level.
13733 * There should be a better way to handle ttl and tos; we could keep
13734 * them in the template, but need a way to checksum without them.
13735 */
13736 /*
13737 * m->m_pkthdr.len should have been set before cksum calcuration,
13738 * because in6_cksum() need it.
13739 */
13740 #ifdef INET6
13741 if (isipv6) {
13742 /*
13743 * we separately set hoplimit for every segment, since the
13744 * user might want to change the value via setsockopt. Also,
13745 * desired default hop limit might be changed via Neighbor
13746 * Discovery.
13747 */
13748 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13749
13750 /*
13751 * Set the packet size here for the benefit of DTrace
13752 * probes. ip6_output() will set it properly; it's supposed
13753 * to include the option header lengths as well.
13754 */
13755 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13756
13757 if (V_path_mtu_discovery && maxseg > V_tcp_minmss)
13758 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13759 else
13760 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13761
13762 if (tp->t_state == TCPS_SYN_SENT)
13763 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13764
13765 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13766 /* TODO: IPv6 IP6TOS_ECT bit on */
13767 error = ip6_output(m, inp->in6p_outputopts,
13768 &inp->inp_route6,
13769 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13770 NULL, NULL, inp);
13771
13772 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
13773 mtu = inp->inp_route6.ro_nh->nh_mtu;
13774 }
13775 #endif /* INET6 */
13776 #if defined(INET) && defined(INET6)
13777 else
13778 #endif
13779 #ifdef INET
13780 {
13781 ip->ip_len = htons(m->m_pkthdr.len);
13782 #ifdef INET6
13783 if (isipv6)
13784 ip->ip_ttl = in6_selecthlim(inp, NULL);
13785 #endif /* INET6 */
13786 /*
13787 * If we do path MTU discovery, then we set DF on every
13788 * packet. This might not be the best thing to do according
13789 * to RFC3390 Section 2. However the tcp hostcache migitates
13790 * the problem so it affects only the first tcp connection
13791 * with a host.
13792 *
13793 * NB: Don't set DF on small MTU/MSS to have a safe
13794 * fallback.
13795 */
13796 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13797 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13798 if (tp->t_port == 0 || len < V_tcp_minmss) {
13799 ip->ip_off |= htons(IP_DF);
13800 }
13801 } else {
13802 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13803 }
13804
13805 if (tp->t_state == TCPS_SYN_SENT)
13806 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
13807
13808 TCP_PROBE5(send, NULL, tp, ip, tp, th);
13809
13810 error = ip_output(m, inp->inp_options, &inp->inp_route,
13811 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
13812 inp);
13813 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
13814 mtu = inp->inp_route.ro_nh->nh_mtu;
13815 }
13816 #endif /* INET */
13817 out:
13818
13819 if (lgb) {
13820 lgb->tlb_errno = error;
13821 lgb = NULL;
13822 }
13823 /*
13824 * In transmit state, time the transmission and arrange for the
13825 * retransmit. In persist state, just set snd_max.
13826 */
13827 if (error == 0) {
13828 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13829 (tp->t_flags & TF_SACK_PERMIT) &&
13830 tp->rcv_numsacks > 0)
13831 tcp_clean_dsack_blocks(tp);
13832 /* We sent an ack clear the bbr_segs_rcvd count */
13833 bbr->output_error_seen = 0;
13834 bbr->oerror_cnt = 0;
13835 bbr->bbr_segs_rcvd = 0;
13836 if (len == 0)
13837 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1);
13838 /* Do accounting for new sends */
13839 if ((len > 0) && (rsm == NULL)) {
13840 int idx;
13841 if (tp->snd_una == tp->snd_max) {
13842 /*
13843 * Special case to match google, when
13844 * nothing is in flight the delivered
13845 * time does get updated to the current
13846 * time (see tcp_rate_bsd.c).
13847 */
13848 bbr->r_ctl.rc_del_time = cts;
13849 }
13850 if (len >= maxseg) {
13851 idx = (len / maxseg) + 3;
13852 if (idx >= TCP_MSS_ACCT_ATIMER)
13853 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1);
13854 else
13855 counter_u64_add(bbr_out_size[idx], 1);
13856 } else {
13857 /* smaller than a MSS */
13858 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options);
13859 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV)
13860 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1);
13861 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1);
13862 }
13863 }
13864 }
13865 abandon = 0;
13866 /*
13867 * We must do the send accounting before we log the output,
13868 * otherwise the state of the rsm could change and we account to the
13869 * wrong bucket.
13870 */
13871 if (len > 0) {
13872 bbr_do_send_accounting(tp, bbr, rsm, len, error);
13873 if (error == 0) {
13874 if (tp->snd_una == tp->snd_max)
13875 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
13876 }
13877 }
13878 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error,
13879 cts, mb, &abandon, rsm, 0, sb);
13880 if (abandon) {
13881 /*
13882 * If bbr_log_output destroys the TCB or sees a TH_RST being
13883 * sent we should hit this condition.
13884 */
13885 return (0);
13886 }
13887 if (bbr->rc_in_persist == 0) {
13888 /*
13889 * Advance snd_nxt over sequence space of this segment.
13890 */
13891 if (error)
13892 /* We don't log or do anything with errors */
13893 goto skip_upd;
13894
13895 if (tp->snd_una == tp->snd_max &&
13896 (len || (flags & (TH_SYN | TH_FIN)))) {
13897 /*
13898 * Update the time we just added data since none was
13899 * outstanding.
13900 */
13901 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13902 bbr->rc_tp->t_acktime = ticks;
13903 }
13904 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) {
13905 if (flags & TH_SYN) {
13906 /*
13907 * Smack the snd_max to iss + 1
13908 * if its a FO we will add len below.
13909 */
13910 tp->snd_max = tp->iss + 1;
13911 }
13912 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13913 tp->snd_max++;
13914 tp->t_flags |= TF_SENTFIN;
13915 }
13916 }
13917 if (sack_rxmit == 0)
13918 tp->snd_max += len;
13919 skip_upd:
13920 if ((error == 0) && len)
13921 tot_len += len;
13922 } else {
13923 /* Persists case */
13924 int32_t xlen = len;
13925
13926 if (error)
13927 goto nomore;
13928
13929 if (flags & TH_SYN)
13930 ++xlen;
13931 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13932 ++xlen;
13933 tp->t_flags |= TF_SENTFIN;
13934 }
13935 if (xlen && (tp->snd_una == tp->snd_max)) {
13936 /*
13937 * Update the time we just added data since none was
13938 * outstanding.
13939 */
13940 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13941 bbr->rc_tp->t_acktime = ticks;
13942 }
13943 if (sack_rxmit == 0)
13944 tp->snd_max += xlen;
13945 tot_len += (len + optlen + ipoptlen);
13946 }
13947 nomore:
13948 if (error) {
13949 /*
13950 * Failures do not advance the seq counter above. For the
13951 * case of ENOBUFS we will fall out and become ack-clocked.
13952 * capping the cwnd at the current flight.
13953 * Everything else will just have to retransmit with the timer
13954 * (no pacer).
13955 */
13956 SOCKBUF_UNLOCK_ASSERT(sb);
13957 BBR_STAT_INC(bbr_saw_oerr);
13958 /* Clear all delay/early tracks */
13959 bbr->r_ctl.rc_hptsi_agg_delay = 0;
13960 bbr->r_ctl.rc_agg_early = 0;
13961 bbr->r_agg_early_set = 0;
13962 bbr->output_error_seen = 1;
13963 if (bbr->oerror_cnt < 0xf)
13964 bbr->oerror_cnt++;
13965 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) {
13966 /* drop the session */
13967 tcp_set_inp_to_drop(inp, ENETDOWN);
13968 }
13969 switch (error) {
13970 case ENOBUFS:
13971 /*
13972 * Make this guy have to get ack's to send
13973 * more but lets make sure we don't
13974 * slam him below a T-O (1MSS).
13975 */
13976 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
13977 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13978 bbr->r_ctl.rc_lost_bytes)) - maxseg;
13979 if (tp->snd_cwnd < maxseg)
13980 tp->snd_cwnd = maxseg;
13981 }
13982 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt;
13983 BBR_STAT_INC(bbr_saw_enobuf);
13984 if (bbr->bbr_hdrw_pacing)
13985 counter_u64_add(bbr_hdwr_pacing_enobuf, 1);
13986 else
13987 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1);
13988 /*
13989 * Here even in the enobuf's case we want to do our
13990 * state update. The reason being we may have been
13991 * called by the input function. If so we have had
13992 * things change.
13993 */
13994 error = 0;
13995 goto enobufs;
13996 case EMSGSIZE:
13997 /*
13998 * For some reason the interface we used initially
13999 * to send segments changed to another or lowered
14000 * its MTU. If TSO was active we either got an
14001 * interface without TSO capabilits or TSO was
14002 * turned off. If we obtained mtu from ip_output()
14003 * then update it and try again.
14004 */
14005 /* Turn on tracing (or try to) */
14006 {
14007 int old_maxseg;
14008
14009 old_maxseg = tp->t_maxseg;
14010 BBR_STAT_INC(bbr_saw_emsgsiz);
14011 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts);
14012 if (mtu != 0)
14013 tcp_mss_update(tp, -1, mtu, NULL, NULL);
14014 if (old_maxseg <= tp->t_maxseg) {
14015 /* Huh it did not shrink? */
14016 tp->t_maxseg = old_maxseg - 40;
14017 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts);
14018 }
14019 /*
14020 * Nuke all other things that can interfere
14021 * with slot
14022 */
14023 if ((tot_len + len) && (len >= tp->t_maxseg)) {
14024 slot = bbr_get_pacing_delay(bbr,
14025 bbr->r_ctl.rc_bbr_hptsi_gain,
14026 (tot_len + len), cts, 0);
14027 if (slot < bbr_error_base_paceout)
14028 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14029 } else
14030 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14031 bbr->rc_output_starts_timer = 1;
14032 bbr_start_hpts_timer(bbr, tp, cts, 10, slot,
14033 tot_len);
14034 return (error);
14035 }
14036 case EPERM:
14037 tp->t_softerror = error;
14038 /* Fall through */
14039 case EHOSTDOWN:
14040 case EHOSTUNREACH:
14041 case ENETDOWN:
14042 case ENETUNREACH:
14043 if (TCPS_HAVERCVDSYN(tp->t_state)) {
14044 tp->t_softerror = error;
14045 }
14046 /* FALLTHROUGH */
14047 default:
14048 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt;
14049 bbr->rc_output_starts_timer = 1;
14050 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0);
14051 return (error);
14052 }
14053 #ifdef STATS
14054 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) &&
14055 len &&
14056 (rsm == NULL) &&
14057 (bbr->rc_in_persist == 0)) {
14058 tp->gput_seq = bbr_seq;
14059 tp->gput_ack = bbr_seq +
14060 min(sbavail(&so->so_snd) - sb_offset, sendwin);
14061 tp->gput_ts = cts;
14062 tp->t_flags |= TF_GPUTINPROG;
14063 #endif
14064 }
14065 KMOD_TCPSTAT_INC(tcps_sndtotal);
14066 if ((bbr->bbr_hdw_pace_ena) &&
14067 (bbr->bbr_attempt_hdwr_pace == 0) &&
14068 (bbr->rc_past_init_win) &&
14069 (bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
14070 (get_filter_value(&bbr->r_ctl.rc_delrate)) &&
14071 (inp->inp_route.ro_nh &&
14072 inp->inp_route.ro_nh->nh_ifp)) {
14073 /*
14074 * We are past the initial window and
14075 * have at least one measurement so we
14076 * could use hardware pacing if its available.
14077 * We have an interface and we have not attempted
14078 * to setup hardware pacing, lets try to now.
14079 */
14080 uint64_t rate_wanted;
14081 int err = 0;
14082
14083 rate_wanted = bbr_get_hardware_rate(bbr);
14084 bbr->bbr_attempt_hdwr_pace = 1;
14085 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp,
14086 inp->inp_route.ro_nh->nh_ifp,
14087 rate_wanted,
14088 (RS_PACING_GEQ|RS_PACING_SUB_OK),
14089 &err);
14090 if (bbr->r_ctl.crte) {
14091 bbr_type_log_hdwr_pacing(bbr,
14092 bbr->r_ctl.crte->ptbl->rs_ifp,
14093 rate_wanted,
14094 bbr->r_ctl.crte->rate,
14095 __LINE__, cts, err);
14096 BBR_STAT_INC(bbr_hdwr_rl_add_ok);
14097 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
14098 counter_u64_add(bbr_flows_whdwr_pacing, 1);
14099 bbr->bbr_hdrw_pacing = 1;
14100 /* Now what is our gain status? */
14101 if (bbr->r_ctl.crte->rate < rate_wanted) {
14102 /* We have a problem */
14103 bbr_setup_less_of_rate(bbr, cts,
14104 bbr->r_ctl.crte->rate, rate_wanted);
14105 } else {
14106 /* We are good */
14107 bbr->gain_is_limited = 0;
14108 bbr->skip_gain = 0;
14109 }
14110 tcp_bbr_tso_size_check(bbr, cts);
14111 } else {
14112 bbr_type_log_hdwr_pacing(bbr,
14113 inp->inp_route.ro_nh->nh_ifp,
14114 rate_wanted,
14115 0,
14116 __LINE__, cts, err);
14117 BBR_STAT_INC(bbr_hdwr_rl_add_fail);
14118 }
14119 }
14120 if (bbr->bbr_hdrw_pacing) {
14121 /*
14122 * Worry about cases where the route
14123 * changes or something happened that we
14124 * lost our hardware pacing possibly during
14125 * the last ip_output call.
14126 */
14127 if (inp->inp_snd_tag == NULL) {
14128 /* A change during ip output disabled hw pacing? */
14129 bbr->bbr_hdrw_pacing = 0;
14130 } else if ((inp->inp_route.ro_nh == NULL) ||
14131 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) {
14132 /*
14133 * We had an interface or route change,
14134 * detach from the current hdwr pacing
14135 * and setup to re-attempt next go
14136 * round.
14137 */
14138 bbr->bbr_hdrw_pacing = 0;
14139 bbr->bbr_attempt_hdwr_pace = 0;
14140 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
14141 tcp_bbr_tso_size_check(bbr, cts);
14142 }
14143 }
14144 /*
14145 * Data sent (as far as we can tell). If this advertises a larger
14146 * window than any other segment, then remember the size of the
14147 * advertised window. Any pending ACK has now been sent.
14148 */
14149 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
14150 tp->rcv_adv = tp->rcv_nxt + recwin;
14151
14152 tp->last_ack_sent = tp->rcv_nxt;
14153 if ((error == 0) &&
14154 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) &&
14155 (doing_tlp == 0) &&
14156 (tso == 0) &&
14157 (len > 0) &&
14158 ((flags & TH_RST) == 0) &&
14159 ((flags & TH_SYN) == 0) &&
14160 (IN_RECOVERY(tp->t_flags) == 0) &&
14161 (bbr->rc_in_persist == 0) &&
14162 (tot_len < bbr->r_ctl.rc_pace_max_segs)) {
14163 /*
14164 * For non-tso we need to goto again until we have sent out
14165 * enough data to match what we are hptsi out every hptsi
14166 * interval.
14167 */
14168 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14169 /* Make sure snd_nxt is drug up */
14170 tp->snd_nxt = tp->snd_max;
14171 }
14172 if (rsm != NULL) {
14173 rsm = NULL;
14174 goto skip_again;
14175 }
14176 rsm = NULL;
14177 sack_rxmit = 0;
14178 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
14179 goto again;
14180 }
14181 skip_again:
14182 if ((error == 0) && (flags & TH_FIN))
14183 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
14184 if ((error == 0) && (flags & TH_RST))
14185 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
14186 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) {
14187 /*
14188 * Calculate/Re-Calculate the hptsi slot in usecs based on
14189 * what we have sent so far
14190 */
14191 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
14192 if (bbr->rc_no_pacing)
14193 slot = 0;
14194 }
14195 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
14196 enobufs:
14197 if (bbr->rc_use_google == 0)
14198 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
14199 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14200 bbr->r_ctl.rc_lost_bytes)));
14201 bbr->rc_output_starts_timer = 1;
14202 if (bbr->bbr_use_rack_cheat &&
14203 (more_to_rxt ||
14204 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) {
14205 /* Rack cheats and shotguns out all rxt's 1ms apart */
14206 if (slot > 1000)
14207 slot = 1000;
14208 }
14209 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) {
14210 /*
14211 * We don't change the tso size until some number of sends
14212 * to give the hardware commands time to get down
14213 * to the interface.
14214 */
14215 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++;
14216 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) {
14217 bbr->hw_pacing_set = 1;
14218 tcp_bbr_tso_size_check(bbr, cts);
14219 }
14220 }
14221 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len);
14222 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14223 /* Make sure snd_nxt is drug up */
14224 tp->snd_nxt = tp->snd_max;
14225 }
14226 return (error);
14227
14228 }
14229
14230 /*
14231 * See bbr_output_wtime() for return values.
14232 */
14233 static int
bbr_output(struct tcpcb * tp)14234 bbr_output(struct tcpcb *tp)
14235 {
14236 int32_t ret;
14237 struct timeval tv;
14238 struct tcp_bbr *bbr;
14239
14240 NET_EPOCH_ASSERT();
14241
14242 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14243 INP_WLOCK_ASSERT(tp->t_inpcb);
14244 (void)tcp_get_usecs(&tv);
14245 ret = bbr_output_wtime(tp, &tv);
14246 return (ret);
14247 }
14248
14249 static void
bbr_mtu_chg(struct tcpcb * tp)14250 bbr_mtu_chg(struct tcpcb *tp)
14251 {
14252 struct tcp_bbr *bbr;
14253 struct bbr_sendmap *rsm, *frsm = NULL;
14254 uint32_t maxseg;
14255
14256 /*
14257 * The MTU has changed. a) Clear the sack filter. b) Mark everything
14258 * over the current size as SACK_PASS so a retransmit will occur.
14259 */
14260
14261 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14262 maxseg = tp->t_maxseg - bbr->rc_last_options;
14263 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
14264 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
14265 /* Don't mess with ones acked (by sack?) */
14266 if (rsm->r_flags & BBR_ACKED)
14267 continue;
14268 if ((rsm->r_end - rsm->r_start) > maxseg) {
14269 /*
14270 * We mark sack-passed on all the previous large
14271 * sends we did. This will force them to retransmit.
14272 */
14273 rsm->r_flags |= BBR_SACK_PASSED;
14274 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) &&
14275 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) {
14276 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
14277 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
14278 rsm->r_flags |= BBR_MARKED_LOST;
14279 }
14280 if (frsm == NULL)
14281 frsm = rsm;
14282 }
14283 }
14284 if (frsm) {
14285 bbr->r_ctl.rc_resend = frsm;
14286 }
14287 }
14288
14289 /*
14290 * bbr_ctloutput() must drop the inpcb lock before performing copyin on
14291 * socket option arguments. When it re-acquires the lock after the copy, it
14292 * has to revalidate that the connection is still valid for the socket
14293 * option.
14294 */
14295 static int
bbr_set_sockopt(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp,struct tcp_bbr * bbr)14296 bbr_set_sockopt(struct socket *so, struct sockopt *sopt,
14297 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14298 {
14299 struct epoch_tracker et;
14300 int32_t error = 0, optval;
14301
14302 switch (sopt->sopt_name) {
14303 case TCP_RACK_PACE_MAX_SEG:
14304 case TCP_RACK_MIN_TO:
14305 case TCP_RACK_REORD_THRESH:
14306 case TCP_RACK_REORD_FADE:
14307 case TCP_RACK_TLP_THRESH:
14308 case TCP_RACK_PKT_DELAY:
14309 case TCP_BBR_ALGORITHM:
14310 case TCP_BBR_TSLIMITS:
14311 case TCP_BBR_IWINTSO:
14312 case TCP_BBR_RECFORCE:
14313 case TCP_BBR_STARTUP_PG:
14314 case TCP_BBR_DRAIN_PG:
14315 case TCP_BBR_RWND_IS_APP:
14316 case TCP_BBR_PROBE_RTT_INT:
14317 case TCP_BBR_PROBE_RTT_GAIN:
14318 case TCP_BBR_PROBE_RTT_LEN:
14319 case TCP_BBR_STARTUP_LOSS_EXIT:
14320 case TCP_BBR_USEDEL_RATE:
14321 case TCP_BBR_MIN_RTO:
14322 case TCP_BBR_MAX_RTO:
14323 case TCP_BBR_PACE_PER_SEC:
14324 case TCP_DELACK:
14325 case TCP_BBR_PACE_DEL_TAR:
14326 case TCP_BBR_SEND_IWND_IN_TSO:
14327 case TCP_BBR_EXTRA_STATE:
14328 case TCP_BBR_UTTER_MAX_TSO:
14329 case TCP_BBR_MIN_TOPACEOUT:
14330 case TCP_BBR_FLOOR_MIN_TSO:
14331 case TCP_BBR_TSTMP_RAISES:
14332 case TCP_BBR_POLICER_DETECT:
14333 case TCP_BBR_USE_RACK_CHEAT:
14334 case TCP_DATA_AFTER_CLOSE:
14335 case TCP_BBR_HDWR_PACE:
14336 case TCP_BBR_PACE_SEG_MAX:
14337 case TCP_BBR_PACE_SEG_MIN:
14338 case TCP_BBR_PACE_CROSS:
14339 case TCP_BBR_PACE_OH:
14340 #ifdef NETFLIX_PEAKRATE
14341 case TCP_MAXPEAKRATE:
14342 #endif
14343 case TCP_BBR_TMR_PACE_OH:
14344 case TCP_BBR_RACK_RTT_USE:
14345 case TCP_BBR_RETRAN_WTSO:
14346 break;
14347 default:
14348 return (tcp_default_ctloutput(so, sopt, inp, tp));
14349 break;
14350 }
14351 INP_WUNLOCK(inp);
14352 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14353 if (error)
14354 return (error);
14355 INP_WLOCK(inp);
14356 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14357 INP_WUNLOCK(inp);
14358 return (ECONNRESET);
14359 }
14360 tp = intotcpcb(inp);
14361 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14362 switch (sopt->sopt_name) {
14363 case TCP_BBR_PACE_PER_SEC:
14364 BBR_OPTS_INC(tcp_bbr_pace_per_sec);
14365 bbr->r_ctl.bbr_hptsi_per_second = optval;
14366 break;
14367 case TCP_BBR_PACE_DEL_TAR:
14368 BBR_OPTS_INC(tcp_bbr_pace_del_tar);
14369 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval;
14370 break;
14371 case TCP_BBR_PACE_SEG_MAX:
14372 BBR_OPTS_INC(tcp_bbr_pace_seg_max);
14373 bbr->r_ctl.bbr_hptsi_segments_max = optval;
14374 break;
14375 case TCP_BBR_PACE_SEG_MIN:
14376 BBR_OPTS_INC(tcp_bbr_pace_seg_min);
14377 bbr->r_ctl.bbr_hptsi_bytes_min = optval;
14378 break;
14379 case TCP_BBR_PACE_CROSS:
14380 BBR_OPTS_INC(tcp_bbr_pace_cross);
14381 bbr->r_ctl.bbr_cross_over = optval;
14382 break;
14383 case TCP_BBR_ALGORITHM:
14384 BBR_OPTS_INC(tcp_bbr_algorithm);
14385 if (optval && (bbr->rc_use_google == 0)) {
14386 /* Turn on the google mode */
14387 bbr_google_mode_on(bbr);
14388 if ((optval > 3) && (optval < 500)) {
14389 /*
14390 * Must be at least greater than .3%
14391 * and must be less than 50.0%.
14392 */
14393 bbr->r_ctl.bbr_google_discount = optval;
14394 }
14395 } else if ((optval == 0) && (bbr->rc_use_google == 1)) {
14396 /* Turn off the google mode */
14397 bbr_google_mode_off(bbr);
14398 }
14399 break;
14400 case TCP_BBR_TSLIMITS:
14401 BBR_OPTS_INC(tcp_bbr_tslimits);
14402 if (optval == 1)
14403 bbr->rc_use_ts_limit = 1;
14404 else if (optval == 0)
14405 bbr->rc_use_ts_limit = 0;
14406 else
14407 error = EINVAL;
14408 break;
14409
14410 case TCP_BBR_IWINTSO:
14411 BBR_OPTS_INC(tcp_bbr_iwintso);
14412 if ((optval >= 0) && (optval < 128)) {
14413 uint32_t twin;
14414
14415 bbr->rc_init_win = optval;
14416 twin = bbr_initial_cwnd(bbr, tp);
14417 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd))
14418 tp->snd_cwnd = twin;
14419 else
14420 error = EBUSY;
14421 } else
14422 error = EINVAL;
14423 break;
14424 case TCP_BBR_STARTUP_PG:
14425 BBR_OPTS_INC(tcp_bbr_startup_pg);
14426 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) {
14427 bbr->r_ctl.rc_startup_pg = optval;
14428 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
14429 bbr->r_ctl.rc_bbr_hptsi_gain = optval;
14430 }
14431 } else
14432 error = EINVAL;
14433 break;
14434 case TCP_BBR_DRAIN_PG:
14435 BBR_OPTS_INC(tcp_bbr_drain_pg);
14436 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE))
14437 bbr->r_ctl.rc_drain_pg = optval;
14438 else
14439 error = EINVAL;
14440 break;
14441 case TCP_BBR_PROBE_RTT_LEN:
14442 BBR_OPTS_INC(tcp_bbr_probertt_len);
14443 if (optval <= 1)
14444 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND));
14445 else
14446 error = EINVAL;
14447 break;
14448 case TCP_BBR_PROBE_RTT_GAIN:
14449 BBR_OPTS_INC(tcp_bbr_probertt_gain);
14450 if (optval <= BBR_UNIT)
14451 bbr->r_ctl.bbr_rttprobe_gain_val = optval;
14452 else
14453 error = EINVAL;
14454 break;
14455 case TCP_BBR_PROBE_RTT_INT:
14456 BBR_OPTS_INC(tcp_bbr_probe_rtt_int);
14457 if (optval > 1000)
14458 bbr->r_ctl.rc_probertt_int = optval;
14459 else
14460 error = EINVAL;
14461 break;
14462 case TCP_BBR_MIN_TOPACEOUT:
14463 BBR_OPTS_INC(tcp_bbr_topaceout);
14464 if (optval == 0) {
14465 bbr->no_pacing_until = 0;
14466 bbr->rc_no_pacing = 0;
14467 } else if (optval <= 0x00ff) {
14468 bbr->no_pacing_until = optval;
14469 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) &&
14470 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){
14471 /* Turn on no pacing */
14472 bbr->rc_no_pacing = 1;
14473 }
14474 } else
14475 error = EINVAL;
14476 break;
14477 case TCP_BBR_STARTUP_LOSS_EXIT:
14478 BBR_OPTS_INC(tcp_bbr_startup_loss_exit);
14479 bbr->rc_loss_exit = optval;
14480 break;
14481 case TCP_BBR_USEDEL_RATE:
14482 error = EINVAL;
14483 break;
14484 case TCP_BBR_MIN_RTO:
14485 BBR_OPTS_INC(tcp_bbr_min_rto);
14486 bbr->r_ctl.rc_min_rto_ms = optval;
14487 break;
14488 case TCP_BBR_MAX_RTO:
14489 BBR_OPTS_INC(tcp_bbr_max_rto);
14490 bbr->rc_max_rto_sec = optval;
14491 break;
14492 case TCP_RACK_MIN_TO:
14493 /* Minimum time between rack t-o's in ms */
14494 BBR_OPTS_INC(tcp_rack_min_to);
14495 bbr->r_ctl.rc_min_to = optval;
14496 break;
14497 case TCP_RACK_REORD_THRESH:
14498 /* RACK reorder threshold (shift amount) */
14499 BBR_OPTS_INC(tcp_rack_reord_thresh);
14500 if ((optval > 0) && (optval < 31))
14501 bbr->r_ctl.rc_reorder_shift = optval;
14502 else
14503 error = EINVAL;
14504 break;
14505 case TCP_RACK_REORD_FADE:
14506 /* Does reordering fade after ms time */
14507 BBR_OPTS_INC(tcp_rack_reord_fade);
14508 bbr->r_ctl.rc_reorder_fade = optval;
14509 break;
14510 case TCP_RACK_TLP_THRESH:
14511 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14512 BBR_OPTS_INC(tcp_rack_tlp_thresh);
14513 if (optval)
14514 bbr->rc_tlp_threshold = optval;
14515 else
14516 error = EINVAL;
14517 break;
14518 case TCP_BBR_USE_RACK_CHEAT:
14519 BBR_OPTS_INC(tcp_use_rackcheat);
14520 if (bbr->rc_use_google) {
14521 error = EINVAL;
14522 break;
14523 }
14524 BBR_OPTS_INC(tcp_rack_cheat);
14525 if (optval)
14526 bbr->bbr_use_rack_cheat = 1;
14527 else
14528 bbr->bbr_use_rack_cheat = 0;
14529 break;
14530 case TCP_BBR_FLOOR_MIN_TSO:
14531 BBR_OPTS_INC(tcp_utter_max_tso);
14532 if ((optval >= 0) && (optval < 40))
14533 bbr->r_ctl.bbr_hptsi_segments_floor = optval;
14534 else
14535 error = EINVAL;
14536 break;
14537 case TCP_BBR_UTTER_MAX_TSO:
14538 BBR_OPTS_INC(tcp_utter_max_tso);
14539 if ((optval >= 0) && (optval < 0xffff))
14540 bbr->r_ctl.bbr_utter_max = optval;
14541 else
14542 error = EINVAL;
14543 break;
14544
14545 case TCP_BBR_EXTRA_STATE:
14546 BBR_OPTS_INC(tcp_extra_state);
14547 if (optval)
14548 bbr->rc_use_idle_restart = 1;
14549 else
14550 bbr->rc_use_idle_restart = 0;
14551 break;
14552 case TCP_BBR_SEND_IWND_IN_TSO:
14553 BBR_OPTS_INC(tcp_iwnd_tso);
14554 if (optval) {
14555 bbr->bbr_init_win_cheat = 1;
14556 if (bbr->rc_past_init_win == 0) {
14557 uint32_t cts;
14558 cts = tcp_get_usecs(&bbr->rc_tv);
14559 tcp_bbr_tso_size_check(bbr, cts);
14560 }
14561 } else
14562 bbr->bbr_init_win_cheat = 0;
14563 break;
14564 case TCP_BBR_HDWR_PACE:
14565 BBR_OPTS_INC(tcp_hdwr_pacing);
14566 if (optval){
14567 bbr->bbr_hdw_pace_ena = 1;
14568 bbr->bbr_attempt_hdwr_pace = 0;
14569 } else {
14570 bbr->bbr_hdw_pace_ena = 0;
14571 #ifdef RATELIMIT
14572 if (bbr->bbr_hdrw_pacing) {
14573 bbr->bbr_hdrw_pacing = 0;
14574 in_pcbdetach_txrtlmt(bbr->rc_inp);
14575 }
14576 #endif
14577 }
14578 break;
14579
14580 case TCP_DELACK:
14581 BBR_OPTS_INC(tcp_delack);
14582 if (optval < 100) {
14583 if (optval == 0) /* off */
14584 tp->t_delayed_ack = 0;
14585 else if (optval == 1) /* on which is 2 */
14586 tp->t_delayed_ack = 2;
14587 else /* higher than 2 and less than 100 */
14588 tp->t_delayed_ack = optval;
14589 if (tp->t_flags & TF_DELACK) {
14590 tp->t_flags &= ~TF_DELACK;
14591 tp->t_flags |= TF_ACKNOW;
14592 NET_EPOCH_ENTER(et);
14593 bbr_output(tp);
14594 NET_EPOCH_EXIT(et);
14595 }
14596 } else
14597 error = EINVAL;
14598 break;
14599 case TCP_RACK_PKT_DELAY:
14600 /* RACK added ms i.e. rack-rtt + reord + N */
14601 BBR_OPTS_INC(tcp_rack_pkt_delay);
14602 bbr->r_ctl.rc_pkt_delay = optval;
14603 break;
14604 #ifdef NETFLIX_PEAKRATE
14605 case TCP_MAXPEAKRATE:
14606 BBR_OPTS_INC(tcp_maxpeak);
14607 error = tcp_set_maxpeakrate(tp, optval);
14608 if (!error)
14609 tp->t_peakrate_thr = tp->t_maxpeakrate;
14610 break;
14611 #endif
14612 case TCP_BBR_RETRAN_WTSO:
14613 BBR_OPTS_INC(tcp_retran_wtso);
14614 if (optval)
14615 bbr->rc_resends_use_tso = 1;
14616 else
14617 bbr->rc_resends_use_tso = 0;
14618 break;
14619 case TCP_DATA_AFTER_CLOSE:
14620 BBR_OPTS_INC(tcp_data_ac);
14621 if (optval)
14622 bbr->rc_allow_data_af_clo = 1;
14623 else
14624 bbr->rc_allow_data_af_clo = 0;
14625 break;
14626 case TCP_BBR_POLICER_DETECT:
14627 BBR_OPTS_INC(tcp_policer_det);
14628 if (bbr->rc_use_google == 0)
14629 error = EINVAL;
14630 else if (optval)
14631 bbr->r_use_policer = 1;
14632 else
14633 bbr->r_use_policer = 0;
14634 break;
14635
14636 case TCP_BBR_TSTMP_RAISES:
14637 BBR_OPTS_INC(tcp_ts_raises);
14638 if (optval)
14639 bbr->ts_can_raise = 1;
14640 else
14641 bbr->ts_can_raise = 0;
14642 break;
14643 case TCP_BBR_TMR_PACE_OH:
14644 BBR_OPTS_INC(tcp_pacing_oh_tmr);
14645 if (bbr->rc_use_google) {
14646 error = EINVAL;
14647 } else {
14648 if (optval)
14649 bbr->r_ctl.rc_incr_tmrs = 1;
14650 else
14651 bbr->r_ctl.rc_incr_tmrs = 0;
14652 }
14653 break;
14654 case TCP_BBR_PACE_OH:
14655 BBR_OPTS_INC(tcp_pacing_oh);
14656 if (bbr->rc_use_google) {
14657 error = EINVAL;
14658 } else {
14659 if (optval > (BBR_INCL_TCP_OH|
14660 BBR_INCL_IP_OH|
14661 BBR_INCL_ENET_OH)) {
14662 error = EINVAL;
14663 break;
14664 }
14665 if (optval & BBR_INCL_TCP_OH)
14666 bbr->r_ctl.rc_inc_tcp_oh = 1;
14667 else
14668 bbr->r_ctl.rc_inc_tcp_oh = 0;
14669 if (optval & BBR_INCL_IP_OH)
14670 bbr->r_ctl.rc_inc_ip_oh = 1;
14671 else
14672 bbr->r_ctl.rc_inc_ip_oh = 0;
14673 if (optval & BBR_INCL_ENET_OH)
14674 bbr->r_ctl.rc_inc_enet_oh = 1;
14675 else
14676 bbr->r_ctl.rc_inc_enet_oh = 0;
14677 }
14678 break;
14679 default:
14680 return (tcp_default_ctloutput(so, sopt, inp, tp));
14681 break;
14682 }
14683 #ifdef NETFLIX_STATS
14684 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14685 #endif
14686 INP_WUNLOCK(inp);
14687 return (error);
14688 }
14689
14690 /*
14691 * return 0 on success, error-num on failure
14692 */
14693 static int
bbr_get_sockopt(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp,struct tcp_bbr * bbr)14694 bbr_get_sockopt(struct socket *so, struct sockopt *sopt,
14695 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14696 {
14697 int32_t error, optval;
14698
14699 /*
14700 * Because all our options are either boolean or an int, we can just
14701 * pull everything into optval and then unlock and copy. If we ever
14702 * add a option that is not a int, then this will have quite an
14703 * impact to this routine.
14704 */
14705 switch (sopt->sopt_name) {
14706 case TCP_BBR_PACE_PER_SEC:
14707 optval = bbr->r_ctl.bbr_hptsi_per_second;
14708 break;
14709 case TCP_BBR_PACE_DEL_TAR:
14710 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar;
14711 break;
14712 case TCP_BBR_PACE_SEG_MAX:
14713 optval = bbr->r_ctl.bbr_hptsi_segments_max;
14714 break;
14715 case TCP_BBR_MIN_TOPACEOUT:
14716 optval = bbr->no_pacing_until;
14717 break;
14718 case TCP_BBR_PACE_SEG_MIN:
14719 optval = bbr->r_ctl.bbr_hptsi_bytes_min;
14720 break;
14721 case TCP_BBR_PACE_CROSS:
14722 optval = bbr->r_ctl.bbr_cross_over;
14723 break;
14724 case TCP_BBR_ALGORITHM:
14725 optval = bbr->rc_use_google;
14726 break;
14727 case TCP_BBR_TSLIMITS:
14728 optval = bbr->rc_use_ts_limit;
14729 break;
14730 case TCP_BBR_IWINTSO:
14731 optval = bbr->rc_init_win;
14732 break;
14733 case TCP_BBR_STARTUP_PG:
14734 optval = bbr->r_ctl.rc_startup_pg;
14735 break;
14736 case TCP_BBR_DRAIN_PG:
14737 optval = bbr->r_ctl.rc_drain_pg;
14738 break;
14739 case TCP_BBR_PROBE_RTT_INT:
14740 optval = bbr->r_ctl.rc_probertt_int;
14741 break;
14742 case TCP_BBR_PROBE_RTT_LEN:
14743 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND);
14744 break;
14745 case TCP_BBR_PROBE_RTT_GAIN:
14746 optval = bbr->r_ctl.bbr_rttprobe_gain_val;
14747 break;
14748 case TCP_BBR_STARTUP_LOSS_EXIT:
14749 optval = bbr->rc_loss_exit;
14750 break;
14751 case TCP_BBR_USEDEL_RATE:
14752 error = EINVAL;
14753 break;
14754 case TCP_BBR_MIN_RTO:
14755 optval = bbr->r_ctl.rc_min_rto_ms;
14756 break;
14757 case TCP_BBR_MAX_RTO:
14758 optval = bbr->rc_max_rto_sec;
14759 break;
14760 case TCP_RACK_PACE_MAX_SEG:
14761 /* Max segments in a pace */
14762 optval = bbr->r_ctl.rc_pace_max_segs;
14763 break;
14764 case TCP_RACK_MIN_TO:
14765 /* Minimum time between rack t-o's in ms */
14766 optval = bbr->r_ctl.rc_min_to;
14767 break;
14768 case TCP_RACK_REORD_THRESH:
14769 /* RACK reorder threshold (shift amount) */
14770 optval = bbr->r_ctl.rc_reorder_shift;
14771 break;
14772 case TCP_RACK_REORD_FADE:
14773 /* Does reordering fade after ms time */
14774 optval = bbr->r_ctl.rc_reorder_fade;
14775 break;
14776 case TCP_BBR_USE_RACK_CHEAT:
14777 /* Do we use the rack cheat for rxt */
14778 optval = bbr->bbr_use_rack_cheat;
14779 break;
14780 case TCP_BBR_FLOOR_MIN_TSO:
14781 optval = bbr->r_ctl.bbr_hptsi_segments_floor;
14782 break;
14783 case TCP_BBR_UTTER_MAX_TSO:
14784 optval = bbr->r_ctl.bbr_utter_max;
14785 break;
14786 case TCP_BBR_SEND_IWND_IN_TSO:
14787 /* Do we send TSO size segments initially */
14788 optval = bbr->bbr_init_win_cheat;
14789 break;
14790 case TCP_BBR_EXTRA_STATE:
14791 optval = bbr->rc_use_idle_restart;
14792 break;
14793 case TCP_RACK_TLP_THRESH:
14794 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14795 optval = bbr->rc_tlp_threshold;
14796 break;
14797 case TCP_RACK_PKT_DELAY:
14798 /* RACK added ms i.e. rack-rtt + reord + N */
14799 optval = bbr->r_ctl.rc_pkt_delay;
14800 break;
14801 case TCP_BBR_RETRAN_WTSO:
14802 optval = bbr->rc_resends_use_tso;
14803 break;
14804 case TCP_DATA_AFTER_CLOSE:
14805 optval = bbr->rc_allow_data_af_clo;
14806 break;
14807 case TCP_DELACK:
14808 optval = tp->t_delayed_ack;
14809 break;
14810 case TCP_BBR_HDWR_PACE:
14811 optval = bbr->bbr_hdw_pace_ena;
14812 break;
14813 case TCP_BBR_POLICER_DETECT:
14814 optval = bbr->r_use_policer;
14815 break;
14816 case TCP_BBR_TSTMP_RAISES:
14817 optval = bbr->ts_can_raise;
14818 break;
14819 case TCP_BBR_TMR_PACE_OH:
14820 optval = bbr->r_ctl.rc_incr_tmrs;
14821 break;
14822 case TCP_BBR_PACE_OH:
14823 optval = 0;
14824 if (bbr->r_ctl.rc_inc_tcp_oh)
14825 optval |= BBR_INCL_TCP_OH;
14826 if (bbr->r_ctl.rc_inc_ip_oh)
14827 optval |= BBR_INCL_IP_OH;
14828 if (bbr->r_ctl.rc_inc_enet_oh)
14829 optval |= BBR_INCL_ENET_OH;
14830 break;
14831 default:
14832 return (tcp_default_ctloutput(so, sopt, inp, tp));
14833 break;
14834 }
14835 INP_WUNLOCK(inp);
14836 error = sooptcopyout(sopt, &optval, sizeof optval);
14837 return (error);
14838 }
14839
14840 /*
14841 * return 0 on success, error-num on failure
14842 */
14843 static int
bbr_ctloutput(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp)14844 bbr_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
14845 {
14846 int32_t error = EINVAL;
14847 struct tcp_bbr *bbr;
14848
14849 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14850 if (bbr == NULL) {
14851 /* Huh? */
14852 goto out;
14853 }
14854 if (sopt->sopt_dir == SOPT_SET) {
14855 return (bbr_set_sockopt(so, sopt, inp, tp, bbr));
14856 } else if (sopt->sopt_dir == SOPT_GET) {
14857 return (bbr_get_sockopt(so, sopt, inp, tp, bbr));
14858 }
14859 out:
14860 INP_WUNLOCK(inp);
14861 return (error);
14862 }
14863
14864 static int
bbr_pru_options(struct tcpcb * tp,int flags)14865 bbr_pru_options(struct tcpcb *tp, int flags)
14866 {
14867 if (flags & PRUS_OOB)
14868 return (EOPNOTSUPP);
14869 return (0);
14870 }
14871
14872 struct tcp_function_block __tcp_bbr = {
14873 .tfb_tcp_block_name = __XSTRING(STACKNAME),
14874 .tfb_tcp_output = bbr_output,
14875 .tfb_do_queued_segments = ctf_do_queued_segments,
14876 .tfb_do_segment_nounlock = bbr_do_segment_nounlock,
14877 .tfb_tcp_do_segment = bbr_do_segment,
14878 .tfb_tcp_ctloutput = bbr_ctloutput,
14879 .tfb_tcp_fb_init = bbr_init,
14880 .tfb_tcp_fb_fini = bbr_fini,
14881 .tfb_tcp_timer_stop_all = bbr_stopall,
14882 .tfb_tcp_timer_activate = bbr_timer_activate,
14883 .tfb_tcp_timer_active = bbr_timer_active,
14884 .tfb_tcp_timer_stop = bbr_timer_stop,
14885 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr,
14886 .tfb_tcp_handoff_ok = bbr_handoff_ok,
14887 .tfb_tcp_mtu_chg = bbr_mtu_chg,
14888 .tfb_pru_options = bbr_pru_options,
14889 };
14890
14891 static const char *bbr_stack_names[] = {
14892 __XSTRING(STACKNAME),
14893 #ifdef STACKALIAS
14894 __XSTRING(STACKALIAS),
14895 #endif
14896 };
14897
14898 static bool bbr_mod_inited = false;
14899
14900 static int
tcp_addbbr(module_t mod,int32_t type,void * data)14901 tcp_addbbr(module_t mod, int32_t type, void *data)
14902 {
14903 int32_t err = 0;
14904 int num_stacks;
14905
14906 switch (type) {
14907 case MOD_LOAD:
14908 printf("Attempting to load " __XSTRING(MODNAME) "\n");
14909 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
14910 sizeof(struct bbr_sendmap),
14911 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
14912 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
14913 sizeof(struct tcp_bbr),
14914 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
14915 sysctl_ctx_init(&bbr_sysctl_ctx);
14916 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
14917 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
14918 OID_AUTO,
14919 #ifdef STACKALIAS
14920 __XSTRING(STACKALIAS),
14921 #else
14922 __XSTRING(STACKNAME),
14923 #endif
14924 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
14925 "");
14926 if (bbr_sysctl_root == NULL) {
14927 printf("Failed to add sysctl node\n");
14928 err = EFAULT;
14929 goto free_uma;
14930 }
14931 bbr_init_sysctls();
14932 num_stacks = nitems(bbr_stack_names);
14933 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK,
14934 bbr_stack_names, &num_stacks);
14935 if (err) {
14936 printf("Failed to register %s stack name for "
14937 "%s module\n", bbr_stack_names[num_stacks],
14938 __XSTRING(MODNAME));
14939 sysctl_ctx_free(&bbr_sysctl_ctx);
14940 free_uma:
14941 uma_zdestroy(bbr_zone);
14942 uma_zdestroy(bbr_pcb_zone);
14943 bbr_counter_destroy();
14944 printf("Failed to register " __XSTRING(MODNAME)
14945 " module err:%d\n", err);
14946 return (err);
14947 }
14948 tcp_lro_reg_mbufq();
14949 bbr_mod_inited = true;
14950 printf(__XSTRING(MODNAME) " is now available\n");
14951 break;
14952 case MOD_QUIESCE:
14953 err = deregister_tcp_functions(&__tcp_bbr, true, false);
14954 break;
14955 case MOD_UNLOAD:
14956 err = deregister_tcp_functions(&__tcp_bbr, false, true);
14957 if (err == EBUSY)
14958 break;
14959 if (bbr_mod_inited) {
14960 uma_zdestroy(bbr_zone);
14961 uma_zdestroy(bbr_pcb_zone);
14962 sysctl_ctx_free(&bbr_sysctl_ctx);
14963 bbr_counter_destroy();
14964 printf(__XSTRING(MODNAME)
14965 " is now no longer available\n");
14966 bbr_mod_inited = false;
14967 }
14968 tcp_lro_dereg_mbufq();
14969 err = 0;
14970 break;
14971 default:
14972 return (EOPNOTSUPP);
14973 }
14974 return (err);
14975 }
14976
14977 static moduledata_t tcp_bbr = {
14978 .name = __XSTRING(MODNAME),
14979 .evhand = tcp_addbbr,
14980 .priv = 0
14981 };
14982
14983 MODULE_VERSION(MODNAME, 1);
14984 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
14985 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
14986