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 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 #include "opt_ratelimit.h"
38 #include <sys/param.h>
39 #include <sys/arb.h>
40 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/libkern.h>
43 #ifdef TCP_HHOOK
44 #include <sys/hhook.h>
45 #endif
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #ifdef STATS
54 #include <sys/qmath.h>
55 #include <sys/tree.h>
56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
57 #endif
58 #include <sys/refcount.h>
59 #include <sys/queue.h>
60 #include <sys/eventhandler.h>
61 #include <sys/smp.h>
62 #include <sys/kthread.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/tim_filter.h>
66 #include <sys/time.h>
67 #include <sys/protosw.h>
68 #include <vm/uma.h>
69 #include <sys/kern_prefetch.h>
70
71 #include <net/route.h>
72 #include <net/route/nhop.h>
73 #include <net/vnet.h>
74
75 #define TCPSTATES /* for logging */
76
77 #include <netinet/in.h>
78 #include <netinet/in_kdtrace.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
83 #include <netinet/ip_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/ip6_var.h>
87 #define TCPOUTFLAGS
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
94 #include <netinet/tcp_hpts.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_log_buf.h>
97 #include <netinet/tcp_ratelimit.h>
98 #include <netinet/tcp_lro.h>
99 #ifdef TCP_OFFLOAD
100 #include <netinet/tcp_offload.h>
101 #endif
102 #ifdef INET6
103 #include <netinet6/tcp6_var.h>
104 #endif
105 #include <netinet/tcp_fastopen.h>
106
107 #include <netipsec/ipsec_support.h>
108 #include <net/if.h>
109 #include <net/if_var.h>
110 #include <net/ethernet.h>
111
112 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
113 #include <netipsec/ipsec.h>
114 #include <netipsec/ipsec6.h>
115 #endif /* IPSEC */
116
117 #include <netinet/udp.h>
118 #include <netinet/udp_var.h>
119 #include <machine/in_cksum.h>
120
121 #ifdef MAC
122 #include <security/mac/mac_framework.h>
123 #endif
124
125 #include "sack_filter.h"
126 #include "tcp_bbr.h"
127 #include "rack_bbr_common.h"
128 uma_zone_t bbr_zone;
129 uma_zone_t bbr_pcb_zone;
130
131 struct sysctl_ctx_list bbr_sysctl_ctx;
132 struct sysctl_oid *bbr_sysctl_root;
133
134 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
135 (tv) = (value); \
136 if ((u_long)(tv) < (u_long)(tvmin)) \
137 (tv) = (tvmin); \
138 if ((u_long)(tv) > (u_long)(tvmax)) \
139 (tv) = (tvmax); \
140 } while(0)
141
142 /*#define BBR_INVARIANT 1*/
143
144 /*
145 * initial window
146 */
147 static uint32_t bbr_def_init_win = 10;
148 static int32_t bbr_persist_min = 250000; /* 250ms */
149 static int32_t bbr_persist_max = 1000000; /* 1 Second */
150 static int32_t bbr_cwnd_may_shrink = 0;
151 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
152 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
153 static int32_t bbr_hardware_pacing_limit = 8000;
154 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
155 static int32_t bbr_no_retran = 0;
156
157 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
158 static int32_t bbr_max_net_error_cnt = 10;
159 /* Should the following be dynamic too -- loss wise */
160 static int32_t bbr_rtt_gain_thresh = 0;
161 /* Measurement controls */
162 static int32_t bbr_use_google_algo = 1;
163 static int32_t bbr_ts_limiting = 1;
164 static int32_t bbr_ts_can_raise = 0;
165 static int32_t bbr_do_red = 600;
166 static int32_t bbr_red_scale = 20000;
167 static int32_t bbr_red_mul = 1;
168 static int32_t bbr_red_div = 2;
169 static int32_t bbr_red_growth_restrict = 1;
170 static int32_t bbr_target_is_bbunit = 0;
171 static int32_t bbr_drop_limit = 0;
172 /*
173 * How much gain do we need to see to
174 * stay in startup?
175 */
176 static int32_t bbr_marks_rxt_sack_passed = 0;
177 static int32_t bbr_start_exit = 25;
178 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
179 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
180 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
181 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
182 * if we go back ever to where the pacer
183 * has priority over timers.
184 */
185 static int32_t bbr_policer_call_from_rack_to = 0;
186 static int32_t bbr_policer_detection_enabled = 1;
187 static int32_t bbr_min_measurements_req = 1; /* We need at least 2
188 * measurements before we are
189 * "good" note that 2 == 1.
190 * This is because we use a >
191 * comparison. This means if
192 * min_measure was 0, it takes
193 * num-measures > min(0) and
194 * you get 1 measurement and
195 * you are good. Set to 1, you
196 * have to have two
197 * measurements (this is done
198 * to prevent it from being ok
199 * to have no measurements). */
200 static int32_t bbr_no_pacing_until = 4;
201
202 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
203 static int32_t bbr_min_peer_delta = 20; /* 20 units */
204 static int32_t bbr_delta_percent = 150; /* 15.0 % */
205
206 static int32_t bbr_target_cwnd_mult_limit = 8;
207 /*
208 * bbr_cwnd_min_val is the number of
209 * segments we hold to in the RTT probe
210 * state typically 4.
211 */
212 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
213
214 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
215
216 static int32_t bbr_gain_to_target = 1;
217 static int32_t bbr_gain_gets_extra_too = 1;
218 /*
219 * bbr_high_gain is the 2/ln(2) value we need
220 * to double the sending rate in startup. This
221 * is used for both cwnd and hptsi gain's.
222 */
223 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
224 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
225 static int32_t bbr_use_lower_gain_in_startup = 1;
226
227 /* thresholds for reduction on drain in sub-states/drain */
228 static int32_t bbr_drain_rtt = BBR_SRTT;
229 static int32_t bbr_drain_floor = 88;
230 static int32_t google_allow_early_out = 1;
231 static int32_t google_consider_lost = 1;
232 static int32_t bbr_drain_drop_mul = 4;
233 static int32_t bbr_drain_drop_div = 5;
234 static int32_t bbr_rand_ot = 50;
235 static int32_t bbr_can_force_probertt = 0;
236 static int32_t bbr_can_adjust_probertt = 1;
237 static int32_t bbr_probertt_sets_rtt = 0;
238 static int32_t bbr_can_use_ts_for_rtt = 1;
239 static int32_t bbr_is_ratio = 0;
240 static int32_t bbr_sub_drain_app_limit = 1;
241 static int32_t bbr_prtt_slam_cwnd = 1;
242 static int32_t bbr_sub_drain_slam_cwnd = 1;
243 static int32_t bbr_slam_cwnd_in_main_drain = 1;
244 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
245 * hold */
246 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
247 /*
248 * bbr_drain_gain is the reverse of the high_gain
249 * designed to drain back out the standing queue
250 * that is formed in startup by causing a larger
251 * hptsi gain and thus drainging the packets
252 * in flight.
253 */
254 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
255 static int32_t bbr_rttprobe_gain = 192;
256
257 /*
258 * The cwnd_gain is the default cwnd gain applied when
259 * calculating a target cwnd. Note that the cwnd is
260 * a secondary factor in the way BBR works (see the
261 * paper and think about it, it will take some time).
262 * Basically the hptsi_gain spreads the packets out
263 * so you never get more than BDP to the peer even
264 * if the cwnd is high. In our implemenation that
265 * means in non-recovery/retransmission scenarios
266 * cwnd will never be reached by the flight-size.
267 */
268 static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
269 static int32_t bbr_tlp_type_to_use = BBR_SRTT;
270 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
271 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
272 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
273 static int32_t bbr_ignore_data_after_close = 1;
274 static int16_t bbr_hptsi_gain[] = {
275 (BBR_UNIT *5 / 4),
276 (BBR_UNIT * 3 / 4),
277 BBR_UNIT,
278 BBR_UNIT,
279 BBR_UNIT,
280 BBR_UNIT,
281 BBR_UNIT,
282 BBR_UNIT
283 };
284 int32_t bbr_use_rack_resend_cheat = 1;
285 int32_t bbr_sends_full_iwnd = 1;
286
287 #define BBR_HPTSI_GAIN_MAX 8
288 /*
289 * The BBR module incorporates a number of
290 * TCP ideas that have been put out into the IETF
291 * over the last few years:
292 * - Yuchung Cheng's RACK TCP (for which its named) that
293 * will stop us using the number of dup acks and instead
294 * use time as the gage of when we retransmit.
295 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
296 * of Dukkipati et.al.
297 * - Van Jacobson's et.al BBR.
298 *
299 * RACK depends on SACK, so if an endpoint arrives that
300 * cannot do SACK the state machine below will shuttle the
301 * connection back to using the "default" TCP stack that is
302 * in FreeBSD.
303 *
304 * To implement BBR and RACK the original TCP stack was first decomposed
305 * into a functional state machine with individual states
306 * for each of the possible TCP connection states. The do_segment
307 * functions role in life is to mandate the connection supports SACK
308 * initially and then assure that the RACK state matches the conenction
309 * state before calling the states do_segment function. Data processing
310 * of inbound segments also now happens in the hpts_do_segment in general
311 * with only one exception. This is so we can keep the connection on
312 * a single CPU.
313 *
314 * Each state is simplified due to the fact that the original do_segment
315 * has been decomposed and we *know* what state we are in (no
316 * switches on the state) and all tests for SACK are gone. This
317 * greatly simplifies what each state does.
318 *
319 * TCP output is also over-written with a new version since it
320 * must maintain the new rack scoreboard and has had hptsi
321 * integrated as a requirment. Still todo is to eliminate the
322 * use of the callout_() system and use the hpts for all
323 * timers as well.
324 */
325 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
326 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
327 static const int32_t bbr_min_req_free = 2; /* The min we must have on the
328 * free list */
329 static int32_t bbr_tlp_thresh = 1;
330 static int32_t bbr_reorder_thresh = 2;
331 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
332 * 60,000,000 - 60 seconds */
333 static int32_t bbr_pkt_delay = 1000;
334 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
335 static int32_t bbr_incr_timers = 1;
336
337 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
338 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
339 static int32_t bbr_exit_startup_at_loss = 1;
340
341 /*
342 * bbr_lt_bw_ratio is 1/8th
343 * bbr_lt_bw_diff is < 4 Kbit/sec
344 */
345 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
346 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
347 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
348 * the lt_bw for */
349 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
350 * lt_bw */
351 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
352 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
353 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
354
355 static int32_t bbr_verbose_logging = 0;
356 /*
357 * Currently regular tcp has a rto_min of 30ms
358 * the backoff goes 12 times so that ends up
359 * being a total of 122.850 seconds before a
360 * connection is killed.
361 */
362 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
363 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
364
365 /****************************************************/
366 /* DEFAULT TSO SIZING (cpu performance impacting) */
367 /****************************************************/
368 /* What amount is our formula using to get TSO size */
369 static int32_t bbr_hptsi_per_second = 1000;
370
371 /*
372 * For hptsi under bbr_cross_over connections what is delay
373 * target 7ms (in usec) combined with a seg_max of 2
374 * gets us close to identical google behavior in
375 * TSO size selection (possibly more 1MSS sends).
376 */
377 static int32_t bbr_hptsi_segments_delay_tar = 7000;
378
379 /* Does pacing delay include overhead's in its time calculations? */
380 static int32_t bbr_include_enet_oh = 0;
381 static int32_t bbr_include_ip_oh = 1;
382 static int32_t bbr_include_tcp_oh = 1;
383 static int32_t bbr_google_discount = 10;
384
385 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
386 static int32_t bbr_state_is_pkt_epoch = 0;
387 static int32_t bbr_state_drain_2_tar = 1;
388 /* What is the max the 0 - bbr_cross_over MBPS TSO target
389 * can reach using our delay target. Note that this
390 * value becomes the floor for the cross over
391 * algorithm.
392 */
393 static int32_t bbr_hptsi_segments_max = 2;
394 static int32_t bbr_hptsi_segments_floor = 1;
395 static int32_t bbr_hptsi_utter_max = 0;
396
397 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
398 static int32_t bbr_hptsi_bytes_min = 1460;
399 static int32_t bbr_all_get_min = 0;
400
401 /* Cross over point from algo-a to algo-b */
402 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
403
404 /* Do we deal with our restart state? */
405 static int32_t bbr_uses_idle_restart = 0;
406 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
407
408 /* Do we allow hardware pacing? */
409 static int32_t bbr_allow_hdwr_pacing = 0;
410 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
411 static int32_t bbr_hdwr_pace_floor = 1;
412 static int32_t bbr_hdwr_pacing_delay_cnt = 10;
413
414 /****************************************************/
415 static int32_t bbr_resends_use_tso = 0;
416 static int32_t bbr_tlp_max_resend = 2;
417 static int32_t bbr_sack_block_limit = 128;
418
419 #define BBR_MAX_STAT 19
420 counter_u64_t bbr_state_time[BBR_MAX_STAT];
421 counter_u64_t bbr_state_lost[BBR_MAX_STAT];
422 counter_u64_t bbr_state_resend[BBR_MAX_STAT];
423 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
424 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
425 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
426 counter_u64_t bbr_flows_whdwr_pacing;
427 counter_u64_t bbr_flows_nohdwr_pacing;
428
429 counter_u64_t bbr_nohdwr_pacing_enobuf;
430 counter_u64_t bbr_hdwr_pacing_enobuf;
431
432 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
433
434 /*
435 * Static defintions we need for forward declarations.
436 */
437 static uint32_t
438 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
439 uint32_t useconds_time, uint64_t bw);
440 static uint32_t
441 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
442 static void
443 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
444 static void
445 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
446 static void
447 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
448 int dolog);
449 static uint32_t
450 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
451 static void
452 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
453 int32_t pkt_epoch, uint32_t losses);
454 static uint32_t
455 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts,
456 struct bbr_sendmap *rsm);
457 static uint32_t
458 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
459 static uint32_t
460 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
461 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts);
462 static void
463 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
464 int32_t line);
465 static void
466 bbr_set_state_target(struct tcp_bbr *bbr, int line);
467 static void
468 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
469 static void
470 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick,
471 int event, int line);
472 static void
473 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
474 static void
475 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
476 static void
477 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
478 uint32_t rtt, uint32_t line, uint8_t is_start,
479 uint16_t set);
480 static struct bbr_sendmap *
481 bbr_find_lowest_rsm(struct tcp_bbr *bbr);
482 static __inline uint32_t
483 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
484 static void
485 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot,
486 uint8_t which);
487 static void
488 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts,
489 uint32_t time_since_sent, uint32_t srtt,
490 uint32_t thresh, uint32_t to);
491 static void
492 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
493 static void
494 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
495 uint32_t del_by, uint32_t cts, uint32_t sloton,
496 uint32_t prev_delay);
497 static void
498 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
499 int32_t line);
500 static void
501 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr);
502 static void
503 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
504 static void
505 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
506 static void
507 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
508 static void
509 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
510 uint32_t cts, uint32_t usecs, uint64_t bw,
511 uint32_t override, int mod);
512 static int bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
513
514 static inline uint8_t
bbr_state_val(struct tcp_bbr * bbr)515 bbr_state_val(struct tcp_bbr *bbr)
516 {
517 return(bbr->rc_bbr_substate);
518 }
519
520 static inline uint32_t
get_min_cwnd(struct tcp_bbr * bbr)521 get_min_cwnd(struct tcp_bbr *bbr)
522 {
523 int mss;
524
525 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
526 bbr->r_ctl.rc_pace_max_segs);
527 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED)
528 return (bbr_cwnd_min_val_hs * mss);
529 else
530 return (bbr_cwnd_min_val * mss);
531 }
532
533 static uint32_t
bbr_get_persists_timer_val(struct tcpcb * tp,struct tcp_bbr * bbr)534 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr)
535 {
536 uint64_t srtt, var;
537 uint64_t ret_val;
538
539 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
540 if (tp->t_srtt == 0) {
541 srtt = (uint64_t)BBR_INITIAL_RTO;
542 var = 0;
543 } else {
544 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
545 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT);
546 }
547 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]),
548 bbr_persist_min, bbr_persist_max);
549 return ((uint32_t)ret_val);
550 }
551
552 static uint32_t
bbr_timer_start(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)553 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
554 {
555 /*
556 * Start the FR timer, we do this based on getting the first one in
557 * the rc_tmap. Note that if its NULL we must stop the timer. in all
558 * events we need to stop the running timer (if its running) before
559 * starting the new one.
560 */
561 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
562 int32_t idx;
563 int32_t is_tlp_timer = 0;
564 struct bbr_sendmap *rsm;
565
566 if (bbr->rc_all_timers_stopped) {
567 /* All timers have been stopped none are to run */
568 return (0);
569 }
570 if (bbr->rc_in_persist) {
571 /* We can't start any timer in persists */
572 return (bbr_get_persists_timer_val(tp, bbr));
573 }
574 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
575 if ((rsm == NULL) ||
576 ((tp->t_flags & TF_SACK_PERMIT) == 0) ||
577 (tp->t_state < TCPS_ESTABLISHED)) {
578 /* Nothing on the send map */
579 activate_rxt:
580 if (SEQ_LT(tp->snd_una, tp->snd_max) ||
581 sbavail(&tptosocket(tp)->so_snd)) {
582 uint64_t tov;
583
584 time_since_sent = 0;
585 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
586 if (rsm) {
587 idx = rsm->r_rtr_cnt - 1;
588 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
589 tstmp_touse = rsm->r_tim_lastsent[idx];
590 else
591 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
592 if (TSTMP_GT(tstmp_touse, cts))
593 time_since_sent = cts - tstmp_touse;
594 }
595 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
596 if (tp->t_srtt == 0)
597 tov = BBR_INITIAL_RTO;
598 else
599 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) +
600 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT);
601 if (tp->t_rxtshift)
602 tov *= tcp_backoff[tp->t_rxtshift];
603 if (tov > time_since_sent)
604 tov -= time_since_sent;
605 else
606 tov = bbr->r_ctl.rc_min_to;
607 TCPT_RANGESET_NOSLOP(to, tov,
608 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC),
609 (bbr->rc_max_rto_sec * USECS_IN_SECOND));
610 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to);
611 return (to);
612 }
613 return (0);
614 }
615 if (rsm->r_flags & BBR_ACKED) {
616 rsm = bbr_find_lowest_rsm(bbr);
617 if (rsm == NULL) {
618 /* No lowest? */
619 goto activate_rxt;
620 }
621 }
622 /* Convert from ms to usecs */
623 if (rsm->r_flags & BBR_SACK_PASSED) {
624 if ((tp->t_flags & TF_SENTFIN) &&
625 ((tp->snd_max - tp->snd_una) == 1) &&
626 (rsm->r_flags & BBR_HAS_FIN)) {
627 /*
628 * We don't start a bbr rack timer if all we have is
629 * a FIN outstanding.
630 */
631 goto activate_rxt;
632 }
633 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK);
634 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm);
635 idx = rsm->r_rtr_cnt - 1;
636 exp = rsm->r_tim_lastsent[idx] + thresh;
637 if (SEQ_GEQ(exp, cts)) {
638 to = exp - cts;
639 if (to < bbr->r_ctl.rc_min_to) {
640 to = bbr->r_ctl.rc_min_to;
641 }
642 } else {
643 to = bbr->r_ctl.rc_min_to;
644 }
645 } else {
646 /* Ok we need to do a TLP not RACK */
647 if (bbr->rc_tlp_in_progress != 0) {
648 /*
649 * The previous send was a TLP.
650 */
651 goto activate_rxt;
652 }
653 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
654 if (rsm == NULL) {
655 /* We found no rsm to TLP with. */
656 goto activate_rxt;
657 }
658 if (rsm->r_flags & BBR_HAS_FIN) {
659 /* If its a FIN we don't do TLP */
660 rsm = NULL;
661 goto activate_rxt;
662 }
663 time_since_sent = 0;
664 idx = rsm->r_rtr_cnt - 1;
665 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
666 tstmp_touse = rsm->r_tim_lastsent[idx];
667 else
668 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
669 if (TSTMP_GT(tstmp_touse, cts))
670 time_since_sent = cts - tstmp_touse;
671 is_tlp_timer = 1;
672 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use);
673 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts);
674 if (thresh > time_since_sent)
675 to = thresh - time_since_sent;
676 else
677 to = bbr->r_ctl.rc_min_to;
678 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
679 /*
680 * If the TLP time works out to larger than the max
681 * RTO lets not do TLP.. just RTO.
682 */
683 goto activate_rxt;
684 }
685 if ((bbr->rc_tlp_rtx_out == 1) &&
686 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) {
687 /*
688 * Second retransmit of the same TLP
689 * lets not.
690 */
691 bbr->rc_tlp_rtx_out = 0;
692 goto activate_rxt;
693 }
694 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) {
695 /*
696 * The tail is no longer the last one I did a probe
697 * on
698 */
699 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
700 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
701 }
702 }
703 if (is_tlp_timer == 0) {
704 BBR_STAT_INC(bbr_to_arm_rack);
705 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
706 } else {
707 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to);
708 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
709 /*
710 * We have exceeded how many times we can retran the
711 * current TLP timer, switch to the RTO timer.
712 */
713 goto activate_rxt;
714 } else {
715 BBR_STAT_INC(bbr_to_arm_tlp);
716 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
717 }
718 }
719 return (to);
720 }
721
722 static inline int32_t
bbr_minseg(struct tcp_bbr * bbr)723 bbr_minseg(struct tcp_bbr *bbr)
724 {
725 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options);
726 }
727
728 static void
bbr_start_hpts_timer(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t cts,int32_t frm,int32_t slot,uint32_t tot_len)729 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len)
730 {
731 struct inpcb *inp = tptoinpcb(tp);
732 struct hpts_diag diag;
733 uint32_t delayed_ack = 0;
734 uint32_t left = 0;
735 uint32_t hpts_timeout;
736 uint8_t stopped;
737 int32_t delay_calc = 0;
738 uint32_t prev_delay = 0;
739
740 if (tcp_in_hpts(tp)) {
741 /* A previous call is already set up */
742 return;
743 }
744 if ((tp->t_state == TCPS_CLOSED) ||
745 (tp->t_state == TCPS_LISTEN)) {
746 return;
747 }
748 stopped = bbr->rc_tmr_stopped;
749 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
750 left = bbr->r_ctl.rc_timer_exp - cts;
751 }
752 bbr->r_ctl.rc_hpts_flags = 0;
753 bbr->r_ctl.rc_timer_exp = 0;
754 prev_delay = bbr->r_ctl.rc_last_delay_val;
755 if (bbr->r_ctl.rc_last_delay_val &&
756 (slot == 0)) {
757 /*
758 * If a previous pacer delay was in place we
759 * are not coming from the output side (where
760 * we calculate a delay, more likely a timer).
761 */
762 slot = bbr->r_ctl.rc_last_delay_val;
763 if (TSTMP_GT(cts, bbr->rc_pacer_started)) {
764 /* Compensate for time passed */
765 delay_calc = cts - bbr->rc_pacer_started;
766 if (delay_calc <= slot)
767 slot -= delay_calc;
768 }
769 }
770 /* Do we have early to make up for by pushing out the pacing time? */
771 if (bbr->r_agg_early_set) {
772 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2);
773 slot += bbr->r_ctl.rc_agg_early;
774 bbr->r_ctl.rc_agg_early = 0;
775 bbr->r_agg_early_set = 0;
776 }
777 /* Are we running a total debt that needs to be compensated for? */
778 if (bbr->r_ctl.rc_hptsi_agg_delay) {
779 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) {
780 /* We nuke the delay */
781 slot -= bbr->r_ctl.rc_hptsi_agg_delay;
782 bbr->r_ctl.rc_hptsi_agg_delay = 0;
783 } else {
784 /* We nuke some of the delay, put in a minimal 100usecs */
785 bbr->r_ctl.rc_hptsi_agg_delay -= slot;
786 bbr->r_ctl.rc_last_delay_val = slot = 100;
787 }
788 }
789 bbr->r_ctl.rc_last_delay_val = slot;
790 hpts_timeout = bbr_timer_start(tp, bbr, cts);
791 if (tp->t_flags & TF_DELACK) {
792 if (bbr->rc_in_persist == 0) {
793 delayed_ack = bbr_delack_time;
794 } else {
795 /*
796 * We are in persists and have
797 * gotten a new data element.
798 */
799 if (hpts_timeout > bbr_delack_time) {
800 /*
801 * Lets make the persists timer (which acks)
802 * be the smaller of hpts_timeout and bbr_delack_time.
803 */
804 hpts_timeout = bbr_delack_time;
805 }
806 }
807 }
808 if (delayed_ack &&
809 ((hpts_timeout == 0) ||
810 (delayed_ack < hpts_timeout))) {
811 /* We need a Delayed ack timer */
812 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
813 hpts_timeout = delayed_ack;
814 }
815 if (slot) {
816 /* Mark that we have a pacing timer up */
817 BBR_STAT_INC(bbr_paced_segments);
818 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
819 }
820 /*
821 * If no timers are going to run and we will fall off thfe hptsi
822 * wheel, we resort to a keep-alive timer if its configured.
823 */
824 if ((hpts_timeout == 0) &&
825 (slot == 0)) {
826 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
827 (tp->t_state <= TCPS_CLOSING)) {
828 /*
829 * Ok we have no timer (persists, rack, tlp, rxt or
830 * del-ack), we don't have segments being paced. So
831 * all that is left is the keepalive timer.
832 */
833 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
834 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
835 } else {
836 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
837 }
838 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
839 }
840 }
841 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
842 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
843 /*
844 * RACK, TLP, persists and RXT timers all are restartable
845 * based on actions input .. i.e we received a packet (ack
846 * or sack) and that changes things (rw, or snd_una etc).
847 * Thus we can restart them with a new value. For
848 * keep-alive, delayed_ack we keep track of what was left
849 * and restart the timer with a smaller value.
850 */
851 if (left < hpts_timeout)
852 hpts_timeout = left;
853 }
854 if (bbr->r_ctl.rc_incr_tmrs && slot &&
855 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
856 /*
857 * If configured to do so, and the timer is either
858 * the TLP or RXT timer, we need to increase the timeout
859 * by the pacing time. Consider the bottleneck at my
860 * machine as an example, we are sending something
861 * to start a TLP on. The last packet won't be emitted
862 * fully until the pacing time (the bottleneck will hold
863 * the data in place). Once the packet is emitted that
864 * is when we want to start waiting for the TLP. This
865 * is most evident with hardware pacing (where the nic
866 * is holding the packet(s) before emitting). But it
867 * can also show up in the network so we do it for all
868 * cases. Technically we would take off one packet from
869 * this extra delay but this is easier and being more
870 * conservative is probably better.
871 */
872 hpts_timeout += slot;
873 }
874 if (hpts_timeout) {
875 /*
876 * Hack alert for now we can't time-out over 2147 seconds (a
877 * bit more than 35min)
878 */
879 if (hpts_timeout > 0x7ffffffe)
880 hpts_timeout = 0x7ffffffe;
881 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
882 } else
883 bbr->r_ctl.rc_timer_exp = 0;
884 if ((slot) &&
885 (bbr->rc_use_google ||
886 bbr->output_error_seen ||
887 (slot <= hpts_timeout)) ) {
888 /*
889 * Tell LRO that it can queue packets while
890 * we pace.
891 */
892 bbr->rc_tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
893 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
894 (bbr->rc_cwnd_limited == 0)) {
895 /*
896 * If we are not cwnd limited and we
897 * are running a rack timer we put on
898 * the do not disturbe even for sack.
899 */
900 tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
901 } else
902 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
903 bbr->rc_pacer_started = cts;
904
905 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
906 __LINE__, &diag);
907 bbr->rc_timer_first = 0;
908 bbr->bbr_timer_src = frm;
909 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1);
910 bbr_log_hpts_diag(bbr, cts, &diag);
911 } else if (hpts_timeout) {
912 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
913 __LINE__, &diag);
914 /*
915 * We add the flag here as well if the slot is set,
916 * since hpts will call in to clear the queue first before
917 * calling the output routine (which does our timers).
918 * We don't want to set the flag if its just a timer
919 * else the arrival of data might (that causes us
920 * to send more) might get delayed. Imagine being
921 * on a keep-alive timer and a request comes in for
922 * more data.
923 */
924 if (slot)
925 bbr->rc_pacer_started = cts;
926 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
927 (bbr->rc_cwnd_limited == 0)) {
928 /*
929 * For a rack timer, don't wake us even
930 * if a sack arrives as long as we are
931 * not cwnd limited.
932 */
933 tp->t_flags2 |= (TF2_MBUF_QUEUE_READY |
934 TF2_DONT_SACK_QUEUE);
935 } else {
936 /* All other timers wake us up */
937 tp->t_flags2 &= ~(TF2_MBUF_QUEUE_READY |
938 TF2_DONT_SACK_QUEUE);
939 }
940 bbr->bbr_timer_src = frm;
941 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0);
942 bbr_log_hpts_diag(bbr, cts, &diag);
943 bbr->rc_timer_first = 1;
944 }
945 bbr->rc_tmr_stopped = 0;
946 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay);
947 }
948
949 static void
bbr_timer_audit(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,struct sockbuf * sb)950 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb)
951 {
952 /*
953 * We received an ack, and then did not call send or were bounced
954 * out due to the hpts was running. Now a timer is up as well, is it
955 * the right timer?
956 */
957 struct inpcb *inp;
958 struct bbr_sendmap *rsm;
959 uint32_t hpts_timeout;
960 int tmr_up;
961
962 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
963 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
964 return;
965 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
966 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
967 (tmr_up == PACE_TMR_RXT)) {
968 /* Should be an RXT */
969 return;
970 }
971 inp = bbr->rc_inp;
972 if (rsm == NULL) {
973 /* Nothing outstanding? */
974 if (tp->t_flags & TF_DELACK) {
975 if (tmr_up == PACE_TMR_DELACK)
976 /*
977 * We are supposed to have delayed ack up
978 * and we do
979 */
980 return;
981 } else if (sbavail(&inp->inp_socket->so_snd) &&
982 (tmr_up == PACE_TMR_RXT)) {
983 /*
984 * if we hit enobufs then we would expect the
985 * possibility of nothing outstanding and the RXT up
986 * (and the hptsi timer).
987 */
988 return;
989 } else if (((V_tcp_always_keepalive ||
990 inp->inp_socket->so_options & SO_KEEPALIVE) &&
991 (tp->t_state <= TCPS_CLOSING)) &&
992 (tmr_up == PACE_TMR_KEEP) &&
993 (tp->snd_max == tp->snd_una)) {
994 /* We should have keep alive up and we do */
995 return;
996 }
997 }
998 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) {
999 if ((tp->t_flags & TF_SENTFIN) &&
1000 ((tp->snd_max - tp->snd_una) == 1) &&
1001 (rsm->r_flags & BBR_HAS_FIN)) {
1002 /* needs to be a RXT */
1003 if (tmr_up == PACE_TMR_RXT)
1004 return;
1005 else
1006 goto wrong_timer;
1007 } else if (tmr_up == PACE_TMR_RACK)
1008 return;
1009 else
1010 goto wrong_timer;
1011 } else if (rsm && (tmr_up == PACE_TMR_RACK)) {
1012 /* Rack timer has priority if we have data out */
1013 return;
1014 } else if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1015 ((tmr_up == PACE_TMR_TLP) ||
1016 (tmr_up == PACE_TMR_RXT))) {
1017 /*
1018 * Either a TLP or RXT is fine if no sack-passed is in place
1019 * and data is outstanding.
1020 */
1021 return;
1022 } else if (tmr_up == PACE_TMR_DELACK) {
1023 /*
1024 * If the delayed ack was going to go off before the
1025 * rtx/tlp/rack timer were going to expire, then that would
1026 * be the timer in control. Note we don't check the time
1027 * here trusting the code is correct.
1028 */
1029 return;
1030 }
1031 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1032 ((tmr_up == PACE_TMR_RXT) ||
1033 (tmr_up == PACE_TMR_TLP) ||
1034 (tmr_up == PACE_TMR_RACK))) {
1035 /*
1036 * We have outstanding data and
1037 * we *do* have a RACK, TLP or RXT
1038 * timer running. We won't restart
1039 * anything here since thats probably ok we
1040 * will get called with some timer here shortly.
1041 */
1042 return;
1043 }
1044 /*
1045 * Ok the timer originally started is not what we want now. We will
1046 * force the hpts to be stopped if any, and restart with the slot
1047 * set to what was in the saved slot.
1048 */
1049 wrong_timer:
1050 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) {
1051 if (tcp_in_hpts(tp))
1052 tcp_hpts_remove(tp);
1053 bbr_timer_cancel(bbr, __LINE__, cts);
1054 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val,
1055 0);
1056 } else {
1057 /*
1058 * Output is hptsi so we just need to switch the type of
1059 * timer. We don't bother with keep-alive, since when we
1060 * jump through the output, it will start the keep-alive if
1061 * nothing is sent.
1062 *
1063 * We only need a delayed-ack added and or the hpts_timeout.
1064 */
1065 hpts_timeout = bbr_timer_start(tp, bbr, cts);
1066 if (tp->t_flags & TF_DELACK) {
1067 if (hpts_timeout == 0) {
1068 hpts_timeout = bbr_delack_time;
1069 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1070 }
1071 else if (hpts_timeout > bbr_delack_time) {
1072 hpts_timeout = bbr_delack_time;
1073 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1074 }
1075 }
1076 if (hpts_timeout) {
1077 if (hpts_timeout > 0x7ffffffe)
1078 hpts_timeout = 0x7ffffffe;
1079 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
1080 }
1081 }
1082 }
1083
1084 int32_t bbr_clear_lost = 0;
1085
1086 /*
1087 * Considers the two time values now (cts) and earlier.
1088 * If cts is smaller than earlier, we could have
1089 * had a sequence wrap (our counter wraps every
1090 * 70 min or so) or it could be just clock skew
1091 * getting us two different time values. Clock skew
1092 * will show up within 10ms or so. So in such
1093 * a case (where cts is behind earlier time by
1094 * less than 10ms) we return 0. Otherwise we
1095 * return the true difference between them.
1096 */
1097 static inline uint32_t
bbr_calc_time(uint32_t cts,uint32_t earlier_time)1098 bbr_calc_time(uint32_t cts, uint32_t earlier_time) {
1099 /*
1100 * Given two timestamps, the current time stamp cts, and some other
1101 * time-stamp taken in theory earlier return the difference. The
1102 * trick is here sometimes locking will get the other timestamp
1103 * after the cts. If this occurs we need to return 0.
1104 */
1105 if (TSTMP_GEQ(cts, earlier_time))
1106 return (cts - earlier_time);
1107 /*
1108 * cts is behind earlier_time if its less than 10ms consider it 0.
1109 * If its more than 10ms difference then we had a time wrap. Else
1110 * its just the normal locking foo. I wonder if we should not go to
1111 * 64bit TS and get rid of this issue.
1112 */
1113 if (TSTMP_GEQ((cts + 10000), earlier_time))
1114 return (0);
1115 /*
1116 * Ok the time must have wrapped. So we need to answer a large
1117 * amount of time, which the normal subtraction should do.
1118 */
1119 return (cts - earlier_time);
1120 }
1121
1122 static int
sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)1123 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)
1124 {
1125 uint32_t stat;
1126 int32_t error;
1127
1128 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t));
1129 if (error || req->newptr == NULL)
1130 return error;
1131
1132 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
1133 if (error)
1134 return (error);
1135 if (stat == 1) {
1136 #ifdef BBR_INVARIANTS
1137 printf("Clearing BBR lost counters\n");
1138 #endif
1139 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT);
1140 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT);
1141 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT);
1142 } else if (stat == 2) {
1143 #ifdef BBR_INVARIANTS
1144 printf("Clearing BBR option counters\n");
1145 #endif
1146 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE);
1147 } else if (stat == 3) {
1148 #ifdef BBR_INVARIANTS
1149 printf("Clearing BBR stats counters\n");
1150 #endif
1151 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE);
1152 } else if (stat == 4) {
1153 #ifdef BBR_INVARIANTS
1154 printf("Clearing BBR out-size counters\n");
1155 #endif
1156 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE);
1157 }
1158 bbr_clear_lost = 0;
1159 return (0);
1160 }
1161
1162 static void
bbr_init_sysctls(void)1163 bbr_init_sysctls(void)
1164 {
1165 struct sysctl_oid *bbr_probertt;
1166 struct sysctl_oid *bbr_hptsi;
1167 struct sysctl_oid *bbr_measure;
1168 struct sysctl_oid *bbr_cwnd;
1169 struct sysctl_oid *bbr_timeout;
1170 struct sysctl_oid *bbr_states;
1171 struct sysctl_oid *bbr_startup;
1172 struct sysctl_oid *bbr_policer;
1173
1174 /* Probe rtt controls */
1175 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1176 SYSCTL_CHILDREN(bbr_sysctl_root),
1177 OID_AUTO,
1178 "probertt",
1179 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1180 "");
1181 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1182 SYSCTL_CHILDREN(bbr_probertt),
1183 OID_AUTO, "gain", CTLFLAG_RW,
1184 &bbr_rttprobe_gain, 192,
1185 "What is the filter gain drop in probe_rtt (0=disable)?");
1186 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1187 SYSCTL_CHILDREN(bbr_probertt),
1188 OID_AUTO, "cwnd", CTLFLAG_RW,
1189 &bbr_rtt_probe_cwndtarg, 4,
1190 "How many mss's are outstanding during probe-rtt");
1191 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1192 SYSCTL_CHILDREN(bbr_probertt),
1193 OID_AUTO, "int", CTLFLAG_RW,
1194 &bbr_rtt_probe_limit, 4000000,
1195 "If RTT has not shrank in this many micro-seconds enter probe-rtt");
1196 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1197 SYSCTL_CHILDREN(bbr_probertt),
1198 OID_AUTO, "mintime", CTLFLAG_RW,
1199 &bbr_rtt_probe_time, 200000,
1200 "How many microseconds in probe-rtt");
1201 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1202 SYSCTL_CHILDREN(bbr_probertt),
1203 OID_AUTO, "filter_len_sec", CTLFLAG_RW,
1204 &bbr_filter_len_sec, 6,
1205 "How long in seconds does the rttProp filter run?");
1206 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1207 SYSCTL_CHILDREN(bbr_probertt),
1208 OID_AUTO, "drain_rtt", CTLFLAG_RW,
1209 &bbr_drain_rtt, BBR_SRTT,
1210 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?");
1211 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1212 SYSCTL_CHILDREN(bbr_probertt),
1213 OID_AUTO, "can_force", CTLFLAG_RW,
1214 &bbr_can_force_probertt, 0,
1215 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??");
1216 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1217 SYSCTL_CHILDREN(bbr_probertt),
1218 OID_AUTO, "enter_sets_force", CTLFLAG_RW,
1219 &bbr_probertt_sets_rtt, 0,
1220 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?");
1221 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1222 SYSCTL_CHILDREN(bbr_probertt),
1223 OID_AUTO, "can_adjust", CTLFLAG_RW,
1224 &bbr_can_adjust_probertt, 1,
1225 "Can we dynamically adjust the probe-rtt limits and times?");
1226 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1227 SYSCTL_CHILDREN(bbr_probertt),
1228 OID_AUTO, "is_ratio", CTLFLAG_RW,
1229 &bbr_is_ratio, 0,
1230 "is the limit to filter a ratio?");
1231 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1232 SYSCTL_CHILDREN(bbr_probertt),
1233 OID_AUTO, "use_cwnd", CTLFLAG_RW,
1234 &bbr_prtt_slam_cwnd, 0,
1235 "Should we set/recover cwnd?");
1236 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1237 SYSCTL_CHILDREN(bbr_probertt),
1238 OID_AUTO, "can_use_ts", CTLFLAG_RW,
1239 &bbr_can_use_ts_for_rtt, 1,
1240 "Can we use the ms timestamp if available for retransmistted rtt calculations?");
1241
1242 /* Pacing controls */
1243 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1244 SYSCTL_CHILDREN(bbr_sysctl_root),
1245 OID_AUTO,
1246 "pacing",
1247 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1248 "");
1249 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1250 SYSCTL_CHILDREN(bbr_hptsi),
1251 OID_AUTO, "hw_pacing", CTLFLAG_RW,
1252 &bbr_allow_hdwr_pacing, 1,
1253 "Do we allow hardware pacing?");
1254 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1255 SYSCTL_CHILDREN(bbr_hptsi),
1256 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW,
1257 &bbr_hardware_pacing_limit, 4000,
1258 "Do we have a limited number of connections for pacing chelsio (0=no limit)?");
1259 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1260 SYSCTL_CHILDREN(bbr_hptsi),
1261 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW,
1262 &bbr_hdwr_pace_adjust, 2,
1263 "Multiplier to calculated tso size?");
1264 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1265 SYSCTL_CHILDREN(bbr_hptsi),
1266 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW,
1267 &bbr_hdwr_pace_floor, 1,
1268 "Do we invoke the hardware pacing floor?");
1269 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1270 SYSCTL_CHILDREN(bbr_hptsi),
1271 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW,
1272 &bbr_hdwr_pacing_delay_cnt, 10,
1273 "How many packets must be sent after hdwr pacing is enabled");
1274 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1275 SYSCTL_CHILDREN(bbr_hptsi),
1276 OID_AUTO, "bw_cross", CTLFLAG_RW,
1277 &bbr_cross_over, 3000000,
1278 "What is the point where we cross over to linux like TSO size set");
1279 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1280 SYSCTL_CHILDREN(bbr_hptsi),
1281 OID_AUTO, "seg_deltarg", CTLFLAG_RW,
1282 &bbr_hptsi_segments_delay_tar, 7000,
1283 "What is the worse case delay target for hptsi < 48Mbp connections");
1284 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1285 SYSCTL_CHILDREN(bbr_hptsi),
1286 OID_AUTO, "enet_oh", CTLFLAG_RW,
1287 &bbr_include_enet_oh, 0,
1288 "Do we include the ethernet overhead in calculating pacing delay?");
1289 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1290 SYSCTL_CHILDREN(bbr_hptsi),
1291 OID_AUTO, "ip_oh", CTLFLAG_RW,
1292 &bbr_include_ip_oh, 1,
1293 "Do we include the IP overhead in calculating pacing delay?");
1294 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1295 SYSCTL_CHILDREN(bbr_hptsi),
1296 OID_AUTO, "tcp_oh", CTLFLAG_RW,
1297 &bbr_include_tcp_oh, 0,
1298 "Do we include the TCP overhead in calculating pacing delay?");
1299 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1300 SYSCTL_CHILDREN(bbr_hptsi),
1301 OID_AUTO, "google_discount", CTLFLAG_RW,
1302 &bbr_google_discount, 10,
1303 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?");
1304 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1305 SYSCTL_CHILDREN(bbr_hptsi),
1306 OID_AUTO, "all_get_min", CTLFLAG_RW,
1307 &bbr_all_get_min, 0,
1308 "If you are less than a MSS do you just get the min?");
1309 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1310 SYSCTL_CHILDREN(bbr_hptsi),
1311 OID_AUTO, "tso_min", CTLFLAG_RW,
1312 &bbr_hptsi_bytes_min, 1460,
1313 "For 0 -> 24Mbps what is floor number of segments for TSO");
1314 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1315 SYSCTL_CHILDREN(bbr_hptsi),
1316 OID_AUTO, "seg_tso_max", CTLFLAG_RW,
1317 &bbr_hptsi_segments_max, 6,
1318 "For 0 -> 24Mbps what is top number of segments for TSO");
1319 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1320 SYSCTL_CHILDREN(bbr_hptsi),
1321 OID_AUTO, "seg_floor", CTLFLAG_RW,
1322 &bbr_hptsi_segments_floor, 1,
1323 "Minimum TSO size we will fall too in segments");
1324 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1325 SYSCTL_CHILDREN(bbr_hptsi),
1326 OID_AUTO, "utter_max", CTLFLAG_RW,
1327 &bbr_hptsi_utter_max, 0,
1328 "The absolute maximum that any pacing (outside of hardware) can be");
1329 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1330 SYSCTL_CHILDREN(bbr_hptsi),
1331 OID_AUTO, "seg_divisor", CTLFLAG_RW,
1332 &bbr_hptsi_per_second, 100,
1333 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps ");
1334 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1335 SYSCTL_CHILDREN(bbr_hptsi),
1336 OID_AUTO, "srtt_mul", CTLFLAG_RW,
1337 &bbr_hptsi_max_mul, 1,
1338 "The multiplier for pace len max");
1339 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1340 SYSCTL_CHILDREN(bbr_hptsi),
1341 OID_AUTO, "srtt_div", CTLFLAG_RW,
1342 &bbr_hptsi_max_div, 2,
1343 "The divisor for pace len max");
1344 /* Measurement controls */
1345 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1346 SYSCTL_CHILDREN(bbr_sysctl_root),
1347 OID_AUTO,
1348 "measure",
1349 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1350 "Measurement controls");
1351 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1352 SYSCTL_CHILDREN(bbr_measure),
1353 OID_AUTO, "min_i_bw", CTLFLAG_RW,
1354 &bbr_initial_bw_bps, 62500,
1355 "Minimum initial b/w in bytes per second");
1356 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1357 SYSCTL_CHILDREN(bbr_measure),
1358 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1359 &bbr_sack_not_required, 0,
1360 "Do we allow bbr to run on connections not supporting SACK?");
1361 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1362 SYSCTL_CHILDREN(bbr_measure),
1363 OID_AUTO, "use_google", CTLFLAG_RW,
1364 &bbr_use_google_algo, 0,
1365 "Use has close to google V1.0 has possible?");
1366 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1367 SYSCTL_CHILDREN(bbr_measure),
1368 OID_AUTO, "ts_limiting", CTLFLAG_RW,
1369 &bbr_ts_limiting, 1,
1370 "Do we attempt to use the peers timestamp to limit b/w caculations?");
1371 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1372 SYSCTL_CHILDREN(bbr_measure),
1373 OID_AUTO, "ts_can_raise", CTLFLAG_RW,
1374 &bbr_ts_can_raise, 0,
1375 "Can we raise the b/w via timestamp b/w calculation?");
1376 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1377 SYSCTL_CHILDREN(bbr_measure),
1378 OID_AUTO, "ts_delta", CTLFLAG_RW,
1379 &bbr_min_usec_delta, 20000,
1380 "How long in usec between ts of our sends in ts validation code?");
1381 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1382 SYSCTL_CHILDREN(bbr_measure),
1383 OID_AUTO, "ts_peer_delta", CTLFLAG_RW,
1384 &bbr_min_peer_delta, 20,
1385 "What min numerical value should be between the peer deltas?");
1386 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1387 SYSCTL_CHILDREN(bbr_measure),
1388 OID_AUTO, "ts_delta_percent", CTLFLAG_RW,
1389 &bbr_delta_percent, 150,
1390 "What percentage (150 = 15.0) do we allow variance for?");
1391 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1392 SYSCTL_CHILDREN(bbr_measure),
1393 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW,
1394 &bbr_min_measurements_req, 1,
1395 "What is the minimum measurement count we need before we switch to our b/w estimate");
1396 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1397 SYSCTL_CHILDREN(bbr_measure),
1398 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW,
1399 &bbr_no_pacing_until, 4,
1400 "How many pkt-epoch's (0 is off) do we need before pacing is on?");
1401 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1402 SYSCTL_CHILDREN(bbr_measure),
1403 OID_AUTO, "quanta", CTLFLAG_RW,
1404 &bbr_quanta, 2,
1405 "Extra quanta to add when calculating the target (ID section 4.2.3.2).");
1406 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1407 SYSCTL_CHILDREN(bbr_measure),
1408 OID_AUTO, "noretran", CTLFLAG_RW,
1409 &bbr_no_retran, 0,
1410 "Should google mode not use retransmission measurements for the b/w estimation?");
1411 /* State controls */
1412 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1413 SYSCTL_CHILDREN(bbr_sysctl_root),
1414 OID_AUTO,
1415 "states",
1416 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1417 "State controls");
1418 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1419 SYSCTL_CHILDREN(bbr_states),
1420 OID_AUTO, "idle_restart", CTLFLAG_RW,
1421 &bbr_uses_idle_restart, 0,
1422 "Do we use a new special idle_restart state to ramp back up quickly?");
1423 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1424 SYSCTL_CHILDREN(bbr_states),
1425 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW,
1426 &bbr_idle_restart_threshold, 100000,
1427 "How long must we be idle before we restart??");
1428 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1429 SYSCTL_CHILDREN(bbr_states),
1430 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW,
1431 &bbr_state_is_pkt_epoch, 0,
1432 "Do we use a pkt-epoch for substate if 0 rttProp?");
1433 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1434 SYSCTL_CHILDREN(bbr_states),
1435 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW,
1436 &bbr_rtt_gain_thresh, 0,
1437 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?");
1438 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1439 SYSCTL_CHILDREN(bbr_states),
1440 OID_AUTO, "drain_floor", CTLFLAG_RW,
1441 &bbr_drain_floor, 88,
1442 "What is the lowest we can drain (pg) too?");
1443 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1444 SYSCTL_CHILDREN(bbr_states),
1445 OID_AUTO, "drain_2_target", CTLFLAG_RW,
1446 &bbr_state_drain_2_tar, 1,
1447 "Do we drain to target in drain substate?");
1448 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1449 SYSCTL_CHILDREN(bbr_states),
1450 OID_AUTO, "gain_2_target", CTLFLAG_RW,
1451 &bbr_gain_to_target, 1,
1452 "Does probe bw gain to target??");
1453 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1454 SYSCTL_CHILDREN(bbr_states),
1455 OID_AUTO, "gain_extra_time", CTLFLAG_RW,
1456 &bbr_gain_gets_extra_too, 1,
1457 "Does probe bw gain get the extra time too?");
1458 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1459 SYSCTL_CHILDREN(bbr_states),
1460 OID_AUTO, "ld_div", CTLFLAG_RW,
1461 &bbr_drain_drop_div, 5,
1462 "Long drain drop divider?");
1463 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1464 SYSCTL_CHILDREN(bbr_states),
1465 OID_AUTO, "ld_mul", CTLFLAG_RW,
1466 &bbr_drain_drop_mul, 4,
1467 "Long drain drop multiplier?");
1468 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1469 SYSCTL_CHILDREN(bbr_states),
1470 OID_AUTO, "rand_ot_disc", CTLFLAG_RW,
1471 &bbr_rand_ot, 50,
1472 "Random discount of the ot?");
1473 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1474 SYSCTL_CHILDREN(bbr_states),
1475 OID_AUTO, "dr_filter_life", CTLFLAG_RW,
1476 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT,
1477 "How many packet-epochs does the b/w delivery rate last?");
1478 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1479 SYSCTL_CHILDREN(bbr_states),
1480 OID_AUTO, "subdrain_applimited", CTLFLAG_RW,
1481 &bbr_sub_drain_app_limit, 0,
1482 "Does our sub-state drain invoke app limited if its long?");
1483 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1484 SYSCTL_CHILDREN(bbr_states),
1485 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW,
1486 &bbr_sub_drain_slam_cwnd, 0,
1487 "Should we set/recover cwnd for sub-state drain?");
1488 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1489 SYSCTL_CHILDREN(bbr_states),
1490 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW,
1491 &bbr_slam_cwnd_in_main_drain, 0,
1492 "Should we set/recover cwnd for main-state drain?");
1493 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1494 SYSCTL_CHILDREN(bbr_states),
1495 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW,
1496 &google_allow_early_out, 1,
1497 "Should we allow google probe-bw/drain to exit early at flight target?");
1498 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1499 SYSCTL_CHILDREN(bbr_states),
1500 OID_AUTO, "google_exit_loss", CTLFLAG_RW,
1501 &google_consider_lost, 1,
1502 "Should we have losses exit gain of probebw in google mode??");
1503 /* Startup controls */
1504 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1505 SYSCTL_CHILDREN(bbr_sysctl_root),
1506 OID_AUTO,
1507 "startup",
1508 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1509 "Startup controls");
1510 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1511 SYSCTL_CHILDREN(bbr_startup),
1512 OID_AUTO, "cheat_iwnd", CTLFLAG_RW,
1513 &bbr_sends_full_iwnd, 1,
1514 "Do we not pace but burst out initial windows has our TSO size?");
1515 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1516 SYSCTL_CHILDREN(bbr_startup),
1517 OID_AUTO, "loss_threshold", CTLFLAG_RW,
1518 &bbr_startup_loss_thresh, 2000,
1519 "In startup what is the loss threshold in a pe that will exit us from startup?");
1520 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1521 SYSCTL_CHILDREN(bbr_startup),
1522 OID_AUTO, "use_lowerpg", CTLFLAG_RW,
1523 &bbr_use_lower_gain_in_startup, 1,
1524 "Should we use a lower hptsi gain if we see loss in startup?");
1525 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1526 SYSCTL_CHILDREN(bbr_startup),
1527 OID_AUTO, "gain", CTLFLAG_RW,
1528 &bbr_start_exit, 25,
1529 "What gain percent do we need to see to stay in startup??");
1530 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1531 SYSCTL_CHILDREN(bbr_startup),
1532 OID_AUTO, "low_gain", CTLFLAG_RW,
1533 &bbr_low_start_exit, 15,
1534 "What gain percent do we need to see to stay in the lower gain startup??");
1535 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1536 SYSCTL_CHILDREN(bbr_startup),
1537 OID_AUTO, "loss_exit", CTLFLAG_RW,
1538 &bbr_exit_startup_at_loss, 1,
1539 "Should we exit startup at loss in an epoch if we are not gaining?");
1540 /* CWND controls */
1541 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1542 SYSCTL_CHILDREN(bbr_sysctl_root),
1543 OID_AUTO,
1544 "cwnd",
1545 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1546 "Cwnd controls");
1547 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1548 SYSCTL_CHILDREN(bbr_cwnd),
1549 OID_AUTO, "tar_rtt", CTLFLAG_RW,
1550 &bbr_cwndtarget_rtt_touse, 0,
1551 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?");
1552 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1553 SYSCTL_CHILDREN(bbr_cwnd),
1554 OID_AUTO, "may_shrink", CTLFLAG_RW,
1555 &bbr_cwnd_may_shrink, 0,
1556 "Can the cwnd shrink if it would grow to more than the target?");
1557 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1558 SYSCTL_CHILDREN(bbr_cwnd),
1559 OID_AUTO, "max_target_limit", CTLFLAG_RW,
1560 &bbr_target_cwnd_mult_limit, 8,
1561 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?");
1562 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1563 SYSCTL_CHILDREN(bbr_cwnd),
1564 OID_AUTO, "highspeed_min", CTLFLAG_RW,
1565 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS,
1566 "What is the high-speed min cwnd (rttProp under 1ms)");
1567 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1568 SYSCTL_CHILDREN(bbr_cwnd),
1569 OID_AUTO, "lowspeed_min", CTLFLAG_RW,
1570 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS,
1571 "What is the min cwnd (rttProp > 1ms)");
1572 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1573 SYSCTL_CHILDREN(bbr_cwnd),
1574 OID_AUTO, "initwin", CTLFLAG_RW,
1575 &bbr_def_init_win, 10,
1576 "What is the BBR initial window, if 0 use tcp version");
1577 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1578 SYSCTL_CHILDREN(bbr_cwnd),
1579 OID_AUTO, "do_loss_red", CTLFLAG_RW,
1580 &bbr_do_red, 600,
1581 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?");
1582 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1583 SYSCTL_CHILDREN(bbr_cwnd),
1584 OID_AUTO, "red_scale", CTLFLAG_RW,
1585 &bbr_red_scale, 20000,
1586 "What RTT do we scale with?");
1587 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1588 SYSCTL_CHILDREN(bbr_cwnd),
1589 OID_AUTO, "red_growslow", CTLFLAG_RW,
1590 &bbr_red_growth_restrict, 1,
1591 "Do we restrict cwnd growth for whats in flight?");
1592 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1593 SYSCTL_CHILDREN(bbr_cwnd),
1594 OID_AUTO, "red_div", CTLFLAG_RW,
1595 &bbr_red_div, 2,
1596 "If we reduce whats the divisor?");
1597 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1598 SYSCTL_CHILDREN(bbr_cwnd),
1599 OID_AUTO, "red_mul", CTLFLAG_RW,
1600 &bbr_red_mul, 1,
1601 "If we reduce whats the mulitiplier?");
1602 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1603 SYSCTL_CHILDREN(bbr_cwnd),
1604 OID_AUTO, "target_is_unit", CTLFLAG_RW,
1605 &bbr_target_is_bbunit, 0,
1606 "Is the state target the pacing_gain or BBR_UNIT?");
1607 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1608 SYSCTL_CHILDREN(bbr_cwnd),
1609 OID_AUTO, "drop_limit", CTLFLAG_RW,
1610 &bbr_drop_limit, 0,
1611 "Number of segments limit for drop (0=use min_cwnd w/flight)?");
1612
1613 /* Timeout controls */
1614 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1615 SYSCTL_CHILDREN(bbr_sysctl_root),
1616 OID_AUTO,
1617 "timeout",
1618 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1619 "Time out controls");
1620 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1621 SYSCTL_CHILDREN(bbr_timeout),
1622 OID_AUTO, "delack", CTLFLAG_RW,
1623 &bbr_delack_time, 100000,
1624 "BBR's delayed ack time");
1625 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1626 SYSCTL_CHILDREN(bbr_timeout),
1627 OID_AUTO, "tlp_uses", CTLFLAG_RW,
1628 &bbr_tlp_type_to_use, 3,
1629 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt");
1630 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1631 SYSCTL_CHILDREN(bbr_timeout),
1632 OID_AUTO, "persmin", CTLFLAG_RW,
1633 &bbr_persist_min, 250000,
1634 "What is the minimum time in microseconds between persists");
1635 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1636 SYSCTL_CHILDREN(bbr_timeout),
1637 OID_AUTO, "persmax", CTLFLAG_RW,
1638 &bbr_persist_max, 1000000,
1639 "What is the largest delay in microseconds between persists");
1640 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1641 SYSCTL_CHILDREN(bbr_timeout),
1642 OID_AUTO, "tlp_minto", CTLFLAG_RW,
1643 &bbr_tlp_min, 10000,
1644 "TLP Min timeout in usecs");
1645 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1646 SYSCTL_CHILDREN(bbr_timeout),
1647 OID_AUTO, "tlp_dack_time", CTLFLAG_RW,
1648 &bbr_delayed_ack_time, 200000,
1649 "TLP delayed ack compensation value");
1650 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1651 SYSCTL_CHILDREN(bbr_sysctl_root),
1652 OID_AUTO, "minrto", CTLFLAG_RW,
1653 &bbr_rto_min_ms, 30,
1654 "Minimum RTO in ms");
1655 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1656 SYSCTL_CHILDREN(bbr_timeout),
1657 OID_AUTO, "maxrto", CTLFLAG_RW,
1658 &bbr_rto_max_sec, 4,
1659 "Maximum RTO in seconds -- should be at least as large as min_rto");
1660 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1661 SYSCTL_CHILDREN(bbr_timeout),
1662 OID_AUTO, "tlp_retry", CTLFLAG_RW,
1663 &bbr_tlp_max_resend, 2,
1664 "How many times does TLP retry a single segment or multiple with no ACK");
1665 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1666 SYSCTL_CHILDREN(bbr_timeout),
1667 OID_AUTO, "minto", CTLFLAG_RW,
1668 &bbr_min_to, 1000,
1669 "Minimum rack timeout in useconds");
1670 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1671 SYSCTL_CHILDREN(bbr_timeout),
1672 OID_AUTO, "pktdelay", CTLFLAG_RW,
1673 &bbr_pkt_delay, 1000,
1674 "Extra RACK time (in useconds) besides reordering thresh");
1675 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1676 SYSCTL_CHILDREN(bbr_timeout),
1677 OID_AUTO, "incr_tmrs", CTLFLAG_RW,
1678 &bbr_incr_timers, 1,
1679 "Increase the RXT/TLP timer by the pacing time used?");
1680 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1681 SYSCTL_CHILDREN(bbr_timeout),
1682 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW,
1683 &bbr_marks_rxt_sack_passed, 0,
1684 "Mark sack passed on all those not ack'd when a RXT hits?");
1685 /* Policer controls */
1686 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1687 SYSCTL_CHILDREN(bbr_sysctl_root),
1688 OID_AUTO,
1689 "policer",
1690 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1691 "Policer controls");
1692 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1693 SYSCTL_CHILDREN(bbr_policer),
1694 OID_AUTO, "detect_enable", CTLFLAG_RW,
1695 &bbr_policer_detection_enabled, 1,
1696 "Is policer detection enabled??");
1697 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1698 SYSCTL_CHILDREN(bbr_policer),
1699 OID_AUTO, "min_pes", CTLFLAG_RW,
1700 &bbr_lt_intvl_min_rtts, 4,
1701 "Minimum number of PE's?");
1702 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1703 SYSCTL_CHILDREN(bbr_policer),
1704 OID_AUTO, "bwdiff", CTLFLAG_RW,
1705 &bbr_lt_bw_diff, (4000/8),
1706 "Minimal bw diff?");
1707 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1708 SYSCTL_CHILDREN(bbr_policer),
1709 OID_AUTO, "bwratio", CTLFLAG_RW,
1710 &bbr_lt_bw_ratio, 8,
1711 "Minimal bw diff?");
1712 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1713 SYSCTL_CHILDREN(bbr_policer),
1714 OID_AUTO, "from_rack_rxt", CTLFLAG_RW,
1715 &bbr_policer_call_from_rack_to, 0,
1716 "Do we call the policer detection code from a rack-timeout?");
1717 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1718 SYSCTL_CHILDREN(bbr_policer),
1719 OID_AUTO, "false_postive", CTLFLAG_RW,
1720 &bbr_lt_intvl_fp, 0,
1721 "What packet epoch do we do false-positive detection at (0=no)?");
1722 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1723 SYSCTL_CHILDREN(bbr_policer),
1724 OID_AUTO, "loss_thresh", CTLFLAG_RW,
1725 &bbr_lt_loss_thresh, 196,
1726 "Loss threshold 196 = 19.6%?");
1727 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1728 SYSCTL_CHILDREN(bbr_policer),
1729 OID_AUTO, "false_postive_thresh", CTLFLAG_RW,
1730 &bbr_lt_fd_thresh, 100,
1731 "What percentage is the false detection threshold (150=15.0)?");
1732 /* All the rest */
1733 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1734 SYSCTL_CHILDREN(bbr_sysctl_root),
1735 OID_AUTO, "cheat_rxt", CTLFLAG_RW,
1736 &bbr_use_rack_resend_cheat, 0,
1737 "Do we burst 1ms between sends on retransmissions (like rack)?");
1738 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1739 SYSCTL_CHILDREN(bbr_sysctl_root),
1740 OID_AUTO, "error_paceout", CTLFLAG_RW,
1741 &bbr_error_base_paceout, 10000,
1742 "When we hit an error what is the min to pace out in usec's?");
1743 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1744 SYSCTL_CHILDREN(bbr_sysctl_root),
1745 OID_AUTO, "kill_paceout", CTLFLAG_RW,
1746 &bbr_max_net_error_cnt, 10,
1747 "When we hit this many errors in a row, kill the session?");
1748 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1749 SYSCTL_CHILDREN(bbr_sysctl_root),
1750 OID_AUTO, "data_after_close", CTLFLAG_RW,
1751 &bbr_ignore_data_after_close, 1,
1752 "Do we hold off sending a RST until all pending data is ack'd");
1753 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1754 SYSCTL_CHILDREN(bbr_sysctl_root),
1755 OID_AUTO, "resend_use_tso", CTLFLAG_RW,
1756 &bbr_resends_use_tso, 0,
1757 "Can resends use TSO?");
1758 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1759 SYSCTL_CHILDREN(bbr_sysctl_root),
1760 OID_AUTO, "sblklimit", CTLFLAG_RW,
1761 &bbr_sack_block_limit, 128,
1762 "When do we start ignoring small sack blocks");
1763 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1764 SYSCTL_CHILDREN(bbr_sysctl_root),
1765 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1766 &bbr_verbose_logging, 0,
1767 "Should BBR black box logging be verbose");
1768 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1769 SYSCTL_CHILDREN(bbr_sysctl_root),
1770 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1771 &bbr_reorder_thresh, 2,
1772 "What factor for rack will be added when seeing reordering (shift right)");
1773 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1774 SYSCTL_CHILDREN(bbr_sysctl_root),
1775 OID_AUTO, "reorder_fade", CTLFLAG_RW,
1776 &bbr_reorder_fade, 0,
1777 "Does reorder detection fade, if so how many ms (0 means never)");
1778 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1779 SYSCTL_CHILDREN(bbr_sysctl_root),
1780 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1781 &bbr_tlp_thresh, 1,
1782 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1783 /* Stats and counters */
1784 /* The pacing counters for hdwr/software can't be in the array */
1785 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1786 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1787 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1788 SYSCTL_CHILDREN(bbr_sysctl_root),
1789 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD,
1790 &bbr_hdwr_pacing_enobuf,
1791 "Total number of enobufs for hardware paced flows");
1792 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1793 SYSCTL_CHILDREN(bbr_sysctl_root),
1794 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD,
1795 &bbr_nohdwr_pacing_enobuf,
1796 "Total number of enobufs for non-hardware paced flows");
1797
1798 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK);
1799 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1800 SYSCTL_CHILDREN(bbr_sysctl_root),
1801 OID_AUTO, "hdwr_pacing", CTLFLAG_RD,
1802 &bbr_flows_whdwr_pacing,
1803 "Total number of hardware paced flows");
1804 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK);
1805 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1806 SYSCTL_CHILDREN(bbr_sysctl_root),
1807 OID_AUTO, "software_pacing", CTLFLAG_RD,
1808 &bbr_flows_nohdwr_pacing,
1809 "Total number of software paced flows");
1810 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK);
1811 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1812 OID_AUTO, "stats", CTLFLAG_RD,
1813 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats");
1814 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK);
1815 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1816 OID_AUTO, "opts", CTLFLAG_RD,
1817 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats");
1818 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK);
1819 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1820 OID_AUTO, "lost", CTLFLAG_RD,
1821 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur");
1822 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK);
1823 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1824 OID_AUTO, "stateresend", CTLFLAG_RD,
1825 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend");
1826 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK);
1827 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1828 OID_AUTO, "statetime", CTLFLAG_RD,
1829 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states");
1830 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1831 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1832 OID_AUTO, "outsize", CTLFLAG_RD,
1833 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls");
1834 SYSCTL_ADD_PROC(&bbr_sysctl_ctx,
1835 SYSCTL_CHILDREN(bbr_sysctl_root),
1836 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1837 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters");
1838 }
1839
1840 static void
bbr_counter_destroy(void)1841 bbr_counter_destroy(void)
1842 {
1843 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE);
1844 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE);
1845 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE);
1846 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT);
1847 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT);
1848 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT);
1849 counter_u64_free(bbr_nohdwr_pacing_enobuf);
1850 counter_u64_free(bbr_hdwr_pacing_enobuf);
1851 counter_u64_free(bbr_flows_whdwr_pacing);
1852 counter_u64_free(bbr_flows_nohdwr_pacing);
1853
1854 }
1855
1856 static __inline void
bbr_fill_in_logging_data(struct tcp_bbr * bbr,struct tcp_log_bbr * l,uint32_t cts)1857 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts)
1858 {
1859 memset(l, 0, sizeof(union tcp_log_stackspecific));
1860 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate;
1861 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate);
1862 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
1863 l->bw_inuse = bbr_get_bw(bbr);
1864 l->inflight = ctf_flight_size(bbr->rc_tp,
1865 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
1866 l->applimited = bbr->r_ctl.r_app_limited_until;
1867 l->delivered = bbr->r_ctl.rc_delivered;
1868 l->timeStamp = cts;
1869 l->lost = bbr->r_ctl.rc_lost;
1870 l->bbr_state = bbr->rc_bbr_state;
1871 l->bbr_substate = bbr_state_val(bbr);
1872 l->epoch = bbr->r_ctl.rc_rtt_epoch;
1873 l->lt_epoch = bbr->r_ctl.rc_lt_epoch;
1874 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
1875 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain;
1876 l->inhpts = tcp_in_hpts(bbr->rc_tp);
1877 l->use_lt_bw = bbr->rc_lt_use_bw;
1878 l->pkts_out = bbr->r_ctl.rc_flight_at_input;
1879 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch;
1880 }
1881
1882 static void
bbr_log_type_bw_reduce(struct tcp_bbr * bbr,int reason)1883 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason)
1884 {
1885 if (tcp_bblogging_on(bbr->rc_tp)) {
1886 union tcp_log_stackspecific log;
1887
1888 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1889 log.u_bbr.flex1 = 0;
1890 log.u_bbr.flex2 = 0;
1891 log.u_bbr.flex5 = 0;
1892 log.u_bbr.flex3 = 0;
1893 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate;
1894 log.u_bbr.flex7 = reason;
1895 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt;
1896 log.u_bbr.flex8 = 0;
1897 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1898 &bbr->rc_inp->inp_socket->so_rcv,
1899 &bbr->rc_inp->inp_socket->so_snd,
1900 BBR_LOG_BW_RED_EV, 0,
1901 0, &log, false, &bbr->rc_tv);
1902 }
1903 }
1904
1905 static void
bbr_log_type_rwnd_collapse(struct tcp_bbr * bbr,int seq,int mode,uint32_t count)1906 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count)
1907 {
1908 if (tcp_bblogging_on(bbr->rc_tp)) {
1909 union tcp_log_stackspecific log;
1910
1911 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1912 log.u_bbr.flex1 = seq;
1913 log.u_bbr.flex2 = count;
1914 log.u_bbr.flex8 = mode;
1915 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1916 &bbr->rc_inp->inp_socket->so_rcv,
1917 &bbr->rc_inp->inp_socket->so_snd,
1918 BBR_LOG_LOWGAIN, 0,
1919 0, &log, false, &bbr->rc_tv);
1920 }
1921 }
1922
1923 static void
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)1924 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling,
1925 uint8_t reason, uint32_t p_maxseg, int len)
1926 {
1927 if (tcp_bblogging_on(bbr->rc_tp)) {
1928 union tcp_log_stackspecific log;
1929
1930 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1931 log.u_bbr.flex1 = p_maxseg;
1932 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags;
1933 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
1934 log.u_bbr.flex4 = reason;
1935 log.u_bbr.flex5 = bbr->rc_in_persist;
1936 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val;
1937 log.u_bbr.flex7 = p_maxseg;
1938 log.u_bbr.flex8 = bbr->rc_in_persist;
1939 log.u_bbr.pkts_out = 0;
1940 log.u_bbr.applimited = len;
1941 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1942 &bbr->rc_inp->inp_socket->so_rcv,
1943 &bbr->rc_inp->inp_socket->so_snd,
1944 BBR_LOG_JUSTRET, 0,
1945 tlen, &log, false, &bbr->rc_tv);
1946 }
1947 }
1948
1949 static void
bbr_log_type_enter_rec(struct tcp_bbr * bbr,uint32_t seq)1950 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq)
1951 {
1952 if (tcp_bblogging_on(bbr->rc_tp)) {
1953 union tcp_log_stackspecific log;
1954
1955 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1956 log.u_bbr.flex1 = seq;
1957 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
1958 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start;
1959 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1960 &bbr->rc_inp->inp_socket->so_rcv,
1961 &bbr->rc_inp->inp_socket->so_snd,
1962 BBR_LOG_ENTREC, 0,
1963 0, &log, false, &bbr->rc_tv);
1964 }
1965 }
1966
1967 static void
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)1968 bbr_log_msgsize_fail(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t len, uint32_t maxseg, uint32_t mtu, int32_t csum_flags, int32_t tso, uint32_t cts)
1969 {
1970 if (tcp_bblogging_on(tp)) {
1971 union tcp_log_stackspecific log;
1972
1973 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1974 log.u_bbr.flex1 = tso;
1975 log.u_bbr.flex2 = maxseg;
1976 log.u_bbr.flex3 = mtu;
1977 log.u_bbr.flex4 = csum_flags;
1978 TCP_LOG_EVENTP(tp, NULL,
1979 &bbr->rc_inp->inp_socket->so_rcv,
1980 &bbr->rc_inp->inp_socket->so_snd,
1981 BBR_LOG_MSGSIZE, 0,
1982 0, &log, false, &bbr->rc_tv);
1983 }
1984 }
1985
1986 static void
bbr_log_flowend(struct tcp_bbr * bbr)1987 bbr_log_flowend(struct tcp_bbr *bbr)
1988 {
1989 if (tcp_bblogging_on(bbr->rc_tp)) {
1990 union tcp_log_stackspecific log;
1991 struct sockbuf *r, *s;
1992 struct timeval tv;
1993
1994 if (bbr->rc_inp->inp_socket) {
1995 r = &bbr->rc_inp->inp_socket->so_rcv;
1996 s = &bbr->rc_inp->inp_socket->so_snd;
1997 } else {
1998 r = s = NULL;
1999 }
2000 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv));
2001 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2002 r, s,
2003 TCP_LOG_FLOWEND, 0,
2004 0, &log, false, &tv);
2005 }
2006 }
2007
2008 static void
bbr_log_pkt_epoch(struct tcp_bbr * bbr,uint32_t cts,uint32_t line,uint32_t lost,uint32_t del)2009 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line,
2010 uint32_t lost, uint32_t del)
2011 {
2012 if (tcp_bblogging_on(bbr->rc_tp)) {
2013 union tcp_log_stackspecific log;
2014
2015 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2016 log.u_bbr.flex1 = lost;
2017 log.u_bbr.flex2 = del;
2018 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw;
2019 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt;
2020 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2021 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2022 log.u_bbr.flex7 = line;
2023 log.u_bbr.flex8 = 0;
2024 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count;
2025 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2026 &bbr->rc_inp->inp_socket->so_rcv,
2027 &bbr->rc_inp->inp_socket->so_snd,
2028 BBR_LOG_PKT_EPOCH, 0,
2029 0, &log, false, &bbr->rc_tv);
2030 }
2031 }
2032
2033 static void
bbr_log_time_epoch(struct tcp_bbr * bbr,uint32_t cts,uint32_t line,uint32_t epoch_time)2034 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time)
2035 {
2036 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2037 union tcp_log_stackspecific log;
2038
2039 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2040 log.u_bbr.flex1 = bbr->r_ctl.rc_lost;
2041 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat;
2042 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat;
2043 log.u_bbr.flex7 = line;
2044 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2045 &bbr->rc_inp->inp_socket->so_rcv,
2046 &bbr->rc_inp->inp_socket->so_snd,
2047 BBR_LOG_TIME_EPOCH, 0,
2048 0, &log, false, &bbr->rc_tv);
2049 }
2050 }
2051
2052 static void
bbr_log_set_of_state_target(struct tcp_bbr * bbr,uint32_t new_tar,int line,int meth)2053 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth)
2054 {
2055 if (tcp_bblogging_on(bbr->rc_tp)) {
2056 union tcp_log_stackspecific log;
2057
2058 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2059 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2060 log.u_bbr.flex2 = new_tar;
2061 log.u_bbr.flex3 = line;
2062 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2063 log.u_bbr.flex5 = bbr_quanta;
2064 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs;
2065 log.u_bbr.flex7 = bbr->rc_last_options;
2066 log.u_bbr.flex8 = meth;
2067 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2068 &bbr->rc_inp->inp_socket->so_rcv,
2069 &bbr->rc_inp->inp_socket->so_snd,
2070 BBR_LOG_STATE_TARGET, 0,
2071 0, &log, false, &bbr->rc_tv);
2072 }
2073
2074 }
2075
2076 static void
bbr_log_type_statechange(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2077 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2078 {
2079 if (tcp_bblogging_on(bbr->rc_tp)) {
2080 union tcp_log_stackspecific log;
2081
2082 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2083 log.u_bbr.flex1 = line;
2084 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2085 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2086 if (bbr_state_is_pkt_epoch)
2087 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
2088 else
2089 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP);
2090 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2091 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2092 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000);
2093 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra;
2094 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state;
2095 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2096 &bbr->rc_inp->inp_socket->so_rcv,
2097 &bbr->rc_inp->inp_socket->so_snd,
2098 BBR_LOG_STATE, 0,
2099 0, &log, false, &bbr->rc_tv);
2100 }
2101 }
2102
2103 static void
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)2104 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
2105 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond)
2106 {
2107 if (tcp_bblogging_on(bbr->rc_tp)) {
2108 union tcp_log_stackspecific log;
2109
2110 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2111 log.u_bbr.flex1 = line;
2112 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2113 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt;
2114 log.u_bbr.flex4 = applied;
2115 log.u_bbr.flex5 = rtt;
2116 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2117 log.u_bbr.flex7 = cond;
2118 log.u_bbr.flex8 = reas;
2119 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2120 &bbr->rc_inp->inp_socket->so_rcv,
2121 &bbr->rc_inp->inp_socket->so_snd,
2122 BBR_LOG_RTT_SHRINKS, 0,
2123 0, &log, false, &bbr->rc_tv);
2124 }
2125 }
2126
2127 static void
bbr_log_type_exit_rec(struct tcp_bbr * bbr)2128 bbr_log_type_exit_rec(struct tcp_bbr *bbr)
2129 {
2130 if (tcp_bblogging_on(bbr->rc_tp)) {
2131 union tcp_log_stackspecific log;
2132
2133 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2134 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start;
2135 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
2136 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2137 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2138 &bbr->rc_inp->inp_socket->so_rcv,
2139 &bbr->rc_inp->inp_socket->so_snd,
2140 BBR_LOG_EXITREC, 0,
2141 0, &log, false, &bbr->rc_tv);
2142 }
2143 }
2144
2145 static void
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)2146 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg,
2147 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line)
2148 {
2149 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2150 union tcp_log_stackspecific log;
2151
2152 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2153 log.u_bbr.flex1 = line;
2154 log.u_bbr.flex2 = prev_acked;
2155 log.u_bbr.flex3 = bytes_this_ack;
2156 log.u_bbr.flex4 = chg;
2157 log.u_bbr.flex5 = th_ack;
2158 log.u_bbr.flex6 = target;
2159 log.u_bbr.flex8 = meth;
2160 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2161 &bbr->rc_inp->inp_socket->so_rcv,
2162 &bbr->rc_inp->inp_socket->so_snd,
2163 BBR_LOG_CWND, 0,
2164 0, &log, false, &bbr->rc_tv);
2165 }
2166 }
2167
2168 static void
bbr_log_rtt_sample(struct tcp_bbr * bbr,uint32_t rtt,uint32_t tsin)2169 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin)
2170 {
2171 /*
2172 * Log the rtt sample we are applying to the srtt algorithm in
2173 * useconds.
2174 */
2175 if (tcp_bblogging_on(bbr->rc_tp)) {
2176 union tcp_log_stackspecific log;
2177
2178 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2179 log.u_bbr.flex1 = rtt;
2180 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time;
2181 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay;
2182 log.u_bbr.flex4 = bbr->rc_tp->ts_offset;
2183 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2184 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv);
2185 log.u_bbr.flex6 = tsin;
2186 log.u_bbr.flex7 = 0;
2187 log.u_bbr.flex8 = bbr->rc_ack_was_delayed;
2188 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2189 &bbr->rc_inp->inp_socket->so_rcv,
2190 &bbr->rc_inp->inp_socket->so_snd,
2191 TCP_LOG_RTT, 0,
2192 0, &log, false, &bbr->rc_tv);
2193 }
2194 }
2195
2196 static void
bbr_log_type_pesist(struct tcp_bbr * bbr,uint32_t cts,uint32_t time_in,int32_t line,uint8_t enter_exit)2197 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit)
2198 {
2199 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2200 union tcp_log_stackspecific log;
2201
2202 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2203 log.u_bbr.flex1 = time_in;
2204 log.u_bbr.flex2 = line;
2205 log.u_bbr.flex8 = enter_exit;
2206 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2207 &bbr->rc_inp->inp_socket->so_rcv,
2208 &bbr->rc_inp->inp_socket->so_snd,
2209 BBR_LOG_PERSIST, 0,
2210 0, &log, false, &bbr->rc_tv);
2211 }
2212 }
2213 static void
bbr_log_ack_clear(struct tcp_bbr * bbr,uint32_t cts)2214 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts)
2215 {
2216 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2217 union tcp_log_stackspecific log;
2218
2219 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2220 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age;
2221 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2222 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2223 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time;
2224 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2225 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2226 &bbr->rc_inp->inp_socket->so_rcv,
2227 &bbr->rc_inp->inp_socket->so_snd,
2228 BBR_LOG_ACKCLEAR, 0,
2229 0, &log, false, &bbr->rc_tv);
2230 }
2231 }
2232
2233 static void
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)2234 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen,
2235 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m)
2236 {
2237 if (tcp_bblogging_on(bbr->rc_tp)) {
2238 union tcp_log_stackspecific log;
2239 struct timeval tv;
2240
2241 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2242 log.u_bbr.flex1 = nsegs;
2243 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes;
2244 if (m) {
2245 struct timespec ts;
2246
2247 log.u_bbr.flex3 = m->m_flags;
2248 if (m->m_flags & M_TSTMP) {
2249 mbuf_tstmp2timespec(m, &ts);
2250 tv.tv_sec = ts.tv_sec;
2251 tv.tv_usec = ts.tv_nsec / 1000;
2252 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv);
2253 } else {
2254 log.u_bbr.lt_epoch = 0;
2255 }
2256 if (m->m_flags & M_TSTMP_LRO) {
2257 mbuf_tstmp2timeval(m, &tv);
2258 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv);
2259 } else {
2260 /* No arrival timestamp */
2261 log.u_bbr.flex5 = 0;
2262 }
2263
2264 log.u_bbr.pkts_out = tcp_get_usecs(&tv);
2265 } else {
2266 log.u_bbr.flex3 = 0;
2267 log.u_bbr.flex5 = 0;
2268 log.u_bbr.flex6 = 0;
2269 log.u_bbr.pkts_out = 0;
2270 }
2271 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state;
2272 log.u_bbr.flex7 = bbr->r_wanted_output;
2273 log.u_bbr.flex8 = bbr->rc_in_persist;
2274 TCP_LOG_EVENTP(bbr->rc_tp, th,
2275 &bbr->rc_inp->inp_socket->so_rcv,
2276 &bbr->rc_inp->inp_socket->so_snd,
2277 TCP_LOG_IN, 0,
2278 tlen, &log, true, &bbr->rc_tv);
2279 }
2280 }
2281
2282 static void
bbr_log_doseg_done(struct tcp_bbr * bbr,uint32_t cts,int32_t nxt_pkt,int32_t did_out)2283 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out)
2284 {
2285 if (tcp_bblogging_on(bbr->rc_tp)) {
2286 union tcp_log_stackspecific log;
2287
2288 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2289 log.u_bbr.flex1 = did_out;
2290 log.u_bbr.flex2 = nxt_pkt;
2291 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val;
2292 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2293 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp;
2294 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes;
2295 log.u_bbr.flex7 = bbr->r_wanted_output;
2296 log.u_bbr.flex8 = bbr->rc_in_persist;
2297 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay;
2298 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2299 &bbr->rc_inp->inp_socket->so_rcv,
2300 &bbr->rc_inp->inp_socket->so_snd,
2301 BBR_LOG_DOSEG_DONE, 0,
2302 0, &log, true, &bbr->rc_tv);
2303 }
2304 }
2305
2306 static void
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)2307 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts,
2308 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz)
2309 {
2310 if (tcp_bblogging_on(bbr->rc_tp)) {
2311 union tcp_log_stackspecific log;
2312
2313 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2314 log.u_bbr.flex1 = line;
2315 log.u_bbr.flex2 = o_len;
2316 log.u_bbr.flex3 = segcnt;
2317 log.u_bbr.flex4 = segsiz;
2318 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2319 &bbr->rc_inp->inp_socket->so_rcv,
2320 &bbr->rc_inp->inp_socket->so_snd,
2321 BBR_LOG_ENOBUF_JMP, ENOBUFS,
2322 len, &log, true, &bbr->rc_tv);
2323 }
2324 }
2325
2326 static void
bbr_log_to_processing(struct tcp_bbr * bbr,uint32_t cts,int32_t ret,int32_t timers,uint8_t hpts_calling)2327 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling)
2328 {
2329 if (tcp_bblogging_on(bbr->rc_tp)) {
2330 union tcp_log_stackspecific log;
2331
2332 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2333 log.u_bbr.flex1 = timers;
2334 log.u_bbr.flex2 = ret;
2335 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
2336 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2337 log.u_bbr.flex5 = cts;
2338 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2339 log.u_bbr.flex8 = hpts_calling;
2340 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2341 &bbr->rc_inp->inp_socket->so_rcv,
2342 &bbr->rc_inp->inp_socket->so_snd,
2343 BBR_LOG_TO_PROCESS, 0,
2344 0, &log, false, &bbr->rc_tv);
2345 }
2346 }
2347
2348 static void
bbr_log_to_event(struct tcp_bbr * bbr,uint32_t cts,int32_t to_num)2349 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num)
2350 {
2351 if (tcp_bblogging_on(bbr->rc_tp)) {
2352 union tcp_log_stackspecific log;
2353 uint64_t ar;
2354
2355 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2356 log.u_bbr.flex1 = bbr->bbr_timer_src;
2357 log.u_bbr.flex2 = 0;
2358 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2359 ar = (uint64_t)(bbr->r_ctl.rc_resend);
2360 ar >>= 32;
2361 ar &= 0x00000000ffffffff;
2362 log.u_bbr.flex4 = (uint32_t)ar;
2363 ar = (uint64_t)bbr->r_ctl.rc_resend;
2364 ar &= 0x00000000ffffffff;
2365 log.u_bbr.flex5 = (uint32_t)ar;
2366 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2367 log.u_bbr.flex8 = to_num;
2368 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2369 &bbr->rc_inp->inp_socket->so_rcv,
2370 &bbr->rc_inp->inp_socket->so_snd,
2371 BBR_LOG_RTO, 0,
2372 0, &log, false, &bbr->rc_tv);
2373 }
2374 }
2375
2376 static void
bbr_log_startup_event(struct tcp_bbr * bbr,uint32_t cts,uint32_t flex1,uint32_t flex2,uint32_t flex3,uint8_t reason)2377 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason)
2378 {
2379 if (tcp_bblogging_on(bbr->rc_tp)) {
2380 union tcp_log_stackspecific log;
2381
2382 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2383 log.u_bbr.flex1 = flex1;
2384 log.u_bbr.flex2 = flex2;
2385 log.u_bbr.flex3 = flex3;
2386 log.u_bbr.flex4 = 0;
2387 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2388 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2389 log.u_bbr.flex8 = reason;
2390 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw;
2391 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2392 &bbr->rc_inp->inp_socket->so_rcv,
2393 &bbr->rc_inp->inp_socket->so_snd,
2394 BBR_LOG_REDUCE, 0,
2395 0, &log, false, &bbr->rc_tv);
2396 }
2397 }
2398
2399 static void
bbr_log_hpts_diag(struct tcp_bbr * bbr,uint32_t cts,struct hpts_diag * diag)2400 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag)
2401 {
2402 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2403 union tcp_log_stackspecific log;
2404
2405 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2406 log.u_bbr.flex1 = diag->p_nxt_slot;
2407 log.u_bbr.flex2 = diag->p_cur_slot;
2408 log.u_bbr.flex3 = diag->slot_req;
2409 log.u_bbr.flex4 = diag->inp_hptsslot;
2410 log.u_bbr.flex5 = diag->slot_remaining;
2411 log.u_bbr.flex6 = diag->need_new_to;
2412 log.u_bbr.flex7 = diag->p_hpts_active;
2413 log.u_bbr.flex8 = diag->p_on_min_sleep;
2414 /* Hijack other fields as needed */
2415 log.u_bbr.epoch = diag->have_slept;
2416 log.u_bbr.lt_epoch = diag->yet_to_sleep;
2417 log.u_bbr.pkts_out = diag->co_ret;
2418 log.u_bbr.applimited = diag->hpts_sleep_time;
2419 log.u_bbr.delivered = diag->p_prev_slot;
2420 log.u_bbr.inflight = diag->p_runningslot;
2421 log.u_bbr.bw_inuse = diag->wheel_slot;
2422 log.u_bbr.rttProp = diag->wheel_cts;
2423 log.u_bbr.delRate = diag->maxslots;
2424 log.u_bbr.cur_del_rate = diag->p_curtick;
2425 log.u_bbr.cur_del_rate <<= 32;
2426 log.u_bbr.cur_del_rate |= diag->p_lasttick;
2427 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2428 &bbr->rc_inp->inp_socket->so_rcv,
2429 &bbr->rc_inp->inp_socket->so_snd,
2430 BBR_LOG_HPTSDIAG, 0,
2431 0, &log, false, &bbr->rc_tv);
2432 }
2433 }
2434
2435 static void
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)2436 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
2437 uint32_t thresh, uint32_t to)
2438 {
2439 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2440 union tcp_log_stackspecific log;
2441
2442 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2443 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar;
2444 log.u_bbr.flex2 = time_since_sent;
2445 log.u_bbr.flex3 = srtt;
2446 log.u_bbr.flex4 = thresh;
2447 log.u_bbr.flex5 = to;
2448 log.u_bbr.flex6 = bbr->rc_tp->t_srtt;
2449 log.u_bbr.flex8 = mode;
2450 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2451 &bbr->rc_inp->inp_socket->so_rcv,
2452 &bbr->rc_inp->inp_socket->so_snd,
2453 BBR_LOG_TIMERPREP, 0,
2454 0, &log, false, &bbr->rc_tv);
2455 }
2456 }
2457
2458 static void
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)2459 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
2460 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod)
2461 {
2462 if (tcp_bblogging_on(bbr->rc_tp)) {
2463 union tcp_log_stackspecific log;
2464
2465 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2466 log.u_bbr.flex1 = usecs;
2467 log.u_bbr.flex2 = len;
2468 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff);
2469 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff);
2470 if (override)
2471 log.u_bbr.flex5 = (1 << 2);
2472 else
2473 log.u_bbr.flex5 = 0;
2474 log.u_bbr.flex6 = override;
2475 log.u_bbr.flex7 = gain;
2476 log.u_bbr.flex8 = mod;
2477 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2478 &bbr->rc_inp->inp_socket->so_rcv,
2479 &bbr->rc_inp->inp_socket->so_snd,
2480 BBR_LOG_HPTSI_CALC, 0,
2481 len, &log, false, &bbr->rc_tv);
2482 }
2483 }
2484
2485 static void
bbr_log_to_start(struct tcp_bbr * bbr,uint32_t cts,uint32_t to,int32_t slot,uint8_t which)2486 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2487 {
2488 if (tcp_bblogging_on(bbr->rc_tp)) {
2489 union tcp_log_stackspecific log;
2490
2491 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2492
2493 log.u_bbr.flex1 = bbr->bbr_timer_src;
2494 log.u_bbr.flex2 = to;
2495 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2496 log.u_bbr.flex4 = slot;
2497 log.u_bbr.flex5 = bbr->rc_tp->t_hpts_slot;
2498 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2499 log.u_bbr.pkts_out = bbr->rc_tp->t_flags2;
2500 log.u_bbr.flex8 = which;
2501 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2502 &bbr->rc_inp->inp_socket->so_rcv,
2503 &bbr->rc_inp->inp_socket->so_snd,
2504 BBR_LOG_TIMERSTAR, 0,
2505 0, &log, false, &bbr->rc_tv);
2506 }
2507 }
2508
2509 static void
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)2510 bbr_log_thresh_choice(struct tcp_bbr *bbr, uint32_t cts, uint32_t thresh, uint32_t lro, uint32_t srtt, struct bbr_sendmap *rsm, uint8_t frm)
2511 {
2512 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2513 union tcp_log_stackspecific log;
2514
2515 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2516 log.u_bbr.flex1 = thresh;
2517 log.u_bbr.flex2 = lro;
2518 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts;
2519 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
2520 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2521 log.u_bbr.flex6 = srtt;
2522 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift;
2523 log.u_bbr.flex8 = frm;
2524 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2525 &bbr->rc_inp->inp_socket->so_rcv,
2526 &bbr->rc_inp->inp_socket->so_snd,
2527 BBR_LOG_THRESH_CALC, 0,
2528 0, &log, false, &bbr->rc_tv);
2529 }
2530 }
2531
2532 static void
bbr_log_to_cancel(struct tcp_bbr * bbr,int32_t line,uint32_t cts,uint8_t hpts_removed)2533 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed)
2534 {
2535 if (tcp_bblogging_on(bbr->rc_tp)) {
2536 union tcp_log_stackspecific log;
2537
2538 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2539 log.u_bbr.flex1 = line;
2540 log.u_bbr.flex2 = bbr->bbr_timer_src;
2541 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2542 log.u_bbr.flex4 = bbr->rc_in_persist;
2543 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2544 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2545 log.u_bbr.flex8 = hpts_removed;
2546 log.u_bbr.pkts_out = bbr->rc_pacer_started;
2547 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2548 &bbr->rc_inp->inp_socket->so_rcv,
2549 &bbr->rc_inp->inp_socket->so_snd,
2550 BBR_LOG_TIMERCANC, 0,
2551 0, &log, false, &bbr->rc_tv);
2552 }
2553 }
2554
2555 static void
bbr_log_tstmp_validation(struct tcp_bbr * bbr,uint64_t peer_delta,uint64_t delta)2556 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta)
2557 {
2558 if (tcp_bblogging_on(bbr->rc_tp)) {
2559 union tcp_log_stackspecific log;
2560
2561 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2562 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio;
2563 log.u_bbr.flex2 = (peer_delta >> 32);
2564 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff);
2565 log.u_bbr.flex4 = (delta >> 32);
2566 log.u_bbr.flex5 = (delta & 0x00000000ffffffff);
2567 log.u_bbr.flex7 = bbr->rc_ts_clock_set;
2568 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used;
2569 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2570 &bbr->rc_inp->inp_socket->so_rcv,
2571 &bbr->rc_inp->inp_socket->so_snd,
2572 BBR_LOG_TSTMP_VAL, 0,
2573 0, &log, false, &bbr->rc_tv);
2574 }
2575 }
2576
2577 static void
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)2578 bbr_log_type_tsosize(struct tcp_bbr *bbr, uint32_t cts, uint32_t tsosz, uint32_t tls, uint32_t old_val, uint32_t maxseg, int hdwr)
2579 {
2580 if (tcp_bblogging_on(bbr->rc_tp)) {
2581 union tcp_log_stackspecific log;
2582
2583 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2584 log.u_bbr.flex1 = tsosz;
2585 log.u_bbr.flex2 = tls;
2586 log.u_bbr.flex3 = tcp_min_hptsi_time;
2587 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min;
2588 log.u_bbr.flex5 = old_val;
2589 log.u_bbr.flex6 = maxseg;
2590 log.u_bbr.flex7 = bbr->rc_no_pacing;
2591 log.u_bbr.flex7 <<= 1;
2592 log.u_bbr.flex7 |= bbr->rc_past_init_win;
2593 if (hdwr)
2594 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google;
2595 else
2596 log.u_bbr.flex8 = bbr->rc_use_google;
2597 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2598 &bbr->rc_inp->inp_socket->so_rcv,
2599 &bbr->rc_inp->inp_socket->so_snd,
2600 BBR_LOG_BBRTSO, 0,
2601 0, &log, false, &bbr->rc_tv);
2602 }
2603 }
2604
2605 static void
bbr_log_type_rsmclear(struct tcp_bbr * bbr,uint32_t cts,struct bbr_sendmap * rsm,uint32_t flags,uint32_t line)2606 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm,
2607 uint32_t flags, uint32_t line)
2608 {
2609 if (tcp_bblogging_on(bbr->rc_tp)) {
2610 union tcp_log_stackspecific log;
2611
2612 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2613 log.u_bbr.flex1 = line;
2614 log.u_bbr.flex2 = rsm->r_start;
2615 log.u_bbr.flex3 = rsm->r_end;
2616 log.u_bbr.flex4 = rsm->r_delivered;
2617 log.u_bbr.flex5 = rsm->r_rtr_cnt;
2618 log.u_bbr.flex6 = rsm->r_dupack;
2619 log.u_bbr.flex7 = rsm->r_tim_lastsent[0];
2620 log.u_bbr.flex8 = rsm->r_flags;
2621 /* Hijack the pkts_out fids */
2622 log.u_bbr.applimited = flags;
2623 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2624 &bbr->rc_inp->inp_socket->so_rcv,
2625 &bbr->rc_inp->inp_socket->so_snd,
2626 BBR_RSM_CLEARED, 0,
2627 0, &log, false, &bbr->rc_tv);
2628 }
2629 }
2630
2631 static void
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)2632 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts,
2633 uint32_t flex3, uint32_t flex2, uint32_t flex5,
2634 uint32_t flex6, uint32_t pkts_out, int flex7,
2635 uint32_t flex4, uint32_t flex1)
2636 {
2637
2638 if (tcp_bblogging_on(bbr->rc_tp)) {
2639 union tcp_log_stackspecific log;
2640
2641 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2642 log.u_bbr.flex1 = flex1;
2643 log.u_bbr.flex2 = flex2;
2644 log.u_bbr.flex3 = flex3;
2645 log.u_bbr.flex4 = flex4;
2646 log.u_bbr.flex5 = flex5;
2647 log.u_bbr.flex6 = flex6;
2648 log.u_bbr.flex7 = flex7;
2649 /* Hijack the pkts_out fids */
2650 log.u_bbr.pkts_out = pkts_out;
2651 log.u_bbr.flex8 = flex8;
2652 if (bbr->rc_ack_was_delayed)
2653 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay;
2654 else
2655 log.u_bbr.epoch = 0;
2656 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2657 &bbr->rc_inp->inp_socket->so_rcv,
2658 &bbr->rc_inp->inp_socket->so_snd,
2659 BBR_LOG_BBRUPD, 0,
2660 flex2, &log, false, &bbr->rc_tv);
2661 }
2662 }
2663
2664 static void
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)2665 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason,
2666 uint32_t newbw, uint32_t obw, uint32_t diff,
2667 uint32_t tim)
2668 {
2669 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) {
2670 union tcp_log_stackspecific log;
2671
2672 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2673 log.u_bbr.flex1 = reason;
2674 log.u_bbr.flex2 = newbw;
2675 log.u_bbr.flex3 = obw;
2676 log.u_bbr.flex4 = diff;
2677 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost;
2678 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del;
2679 log.u_bbr.flex7 = bbr->rc_lt_is_sampling;
2680 log.u_bbr.pkts_out = tim;
2681 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw;
2682 if (bbr->rc_lt_use_bw == 0)
2683 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
2684 else
2685 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
2686 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2687 &bbr->rc_inp->inp_socket->so_rcv,
2688 &bbr->rc_inp->inp_socket->so_snd,
2689 BBR_LOG_BWSAMP, 0,
2690 0, &log, false, &bbr->rc_tv);
2691 }
2692 }
2693
2694 static inline void
bbr_log_progress_event(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t tick,int event,int line)2695 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line)
2696 {
2697 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2698 union tcp_log_stackspecific log;
2699
2700 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2701 log.u_bbr.flex1 = line;
2702 log.u_bbr.flex2 = tick;
2703 log.u_bbr.flex3 = tp->t_maxunacktime;
2704 log.u_bbr.flex4 = tp->t_acktime;
2705 log.u_bbr.flex8 = event;
2706 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2707 &bbr->rc_inp->inp_socket->so_rcv,
2708 &bbr->rc_inp->inp_socket->so_snd,
2709 BBR_LOG_PROGRESS, 0,
2710 0, &log, false, &bbr->rc_tv);
2711 }
2712 }
2713
2714 static void
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)2715 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp,
2716 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts,
2717 int error)
2718 {
2719 if (tcp_bblogging_on(bbr->rc_tp)) {
2720 union tcp_log_stackspecific log;
2721
2722 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2723 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2724 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2725 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2726 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2727 log.u_bbr.bw_inuse = rate;
2728 log.u_bbr.flex5 = line;
2729 log.u_bbr.flex6 = error;
2730 log.u_bbr.flex8 = bbr->skip_gain;
2731 log.u_bbr.flex8 <<= 1;
2732 log.u_bbr.flex8 |= bbr->gain_is_limited;
2733 log.u_bbr.flex8 <<= 1;
2734 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing;
2735 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
2736 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2737 &bbr->rc_inp->inp_socket->so_rcv,
2738 &bbr->rc_inp->inp_socket->so_snd,
2739 BBR_LOG_HDWR_PACE, 0,
2740 0, &log, false, &bbr->rc_tv);
2741 }
2742 }
2743
2744 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)2745 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)
2746 {
2747 if (tcp_bblogging_on(bbr->rc_tp)) {
2748 union tcp_log_stackspecific log;
2749
2750 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2751 log.u_bbr.flex1 = slot;
2752 log.u_bbr.flex2 = del_by;
2753 log.u_bbr.flex3 = prev_delay;
2754 log.u_bbr.flex4 = line;
2755 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val;
2756 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay;
2757 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags);
2758 log.u_bbr.flex8 = bbr->rc_in_persist;
2759 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2760 &bbr->rc_inp->inp_socket->so_rcv,
2761 &bbr->rc_inp->inp_socket->so_snd,
2762 BBR_LOG_BBRSND, 0,
2763 len, &log, false, &bbr->rc_tv);
2764 }
2765 }
2766
2767 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)2768 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)
2769 {
2770 if (tcp_bblogging_on(bbr->rc_tp)) {
2771 union tcp_log_stackspecific log;
2772
2773 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2774 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered;
2775 log.u_bbr.flex2 = 0;
2776 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt;
2777 log.u_bbr.flex4 = end;
2778 log.u_bbr.flex5 = seq;
2779 log.u_bbr.flex6 = t;
2780 log.u_bbr.flex7 = match;
2781 log.u_bbr.flex8 = flags;
2782 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2783 &bbr->rc_inp->inp_socket->so_rcv,
2784 &bbr->rc_inp->inp_socket->so_snd,
2785 BBR_LOG_BBRRTT, 0,
2786 0, &log, false, &bbr->rc_tv);
2787 }
2788 }
2789
2790 static void
bbr_log_exit_gain(struct tcp_bbr * bbr,uint32_t cts,int32_t entry_method)2791 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method)
2792 {
2793 if (tcp_bblogging_on(bbr->rc_tp)) {
2794 union tcp_log_stackspecific log;
2795
2796 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2797 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2798 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
2799 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch;
2800 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2801 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs;
2802 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight;
2803 log.u_bbr.flex7 = 0;
2804 log.u_bbr.flex8 = entry_method;
2805 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2806 &bbr->rc_inp->inp_socket->so_rcv,
2807 &bbr->rc_inp->inp_socket->so_snd,
2808 BBR_LOG_EXIT_GAIN, 0,
2809 0, &log, false, &bbr->rc_tv);
2810 }
2811 }
2812
2813 static void
bbr_log_settings_change(struct tcp_bbr * bbr,int settings_desired)2814 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired)
2815 {
2816 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) {
2817 union tcp_log_stackspecific log;
2818
2819 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2820 /* R-HU */
2821 log.u_bbr.flex1 = 0;
2822 log.u_bbr.flex2 = 0;
2823 log.u_bbr.flex3 = 0;
2824 log.u_bbr.flex4 = 0;
2825 log.u_bbr.flex7 = 0;
2826 log.u_bbr.flex8 = settings_desired;
2827
2828 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2829 &bbr->rc_inp->inp_socket->so_rcv,
2830 &bbr->rc_inp->inp_socket->so_snd,
2831 BBR_LOG_SETTINGS_CHG, 0,
2832 0, &log, false, &bbr->rc_tv);
2833 }
2834 }
2835
2836 /*
2837 * Returns the bw from the our filter.
2838 */
2839 static inline uint64_t
bbr_get_full_bw(struct tcp_bbr * bbr)2840 bbr_get_full_bw(struct tcp_bbr *bbr)
2841 {
2842 uint64_t bw;
2843
2844 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
2845
2846 return (bw);
2847 }
2848
2849 static inline void
bbr_set_pktepoch(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2850 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2851 {
2852 uint64_t calclr;
2853 uint32_t lost, del;
2854
2855 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch)
2856 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch;
2857 else
2858 lost = 0;
2859 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del;
2860 if (lost == 0) {
2861 calclr = 0;
2862 } else if (del) {
2863 calclr = lost;
2864 calclr *= (uint64_t)1000;
2865 calclr /= (uint64_t)del;
2866 } else {
2867 /* Nothing delivered? 100.0% loss */
2868 calclr = 1000;
2869 }
2870 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr;
2871 if (IN_RECOVERY(bbr->rc_tp->t_flags))
2872 bbr->r_ctl.recovery_lr += (uint32_t)calclr;
2873 bbr->r_ctl.rc_pkt_epoch++;
2874 if (bbr->rc_no_pacing &&
2875 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) {
2876 bbr->rc_no_pacing = 0;
2877 tcp_bbr_tso_size_check(bbr, cts);
2878 }
2879 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time);
2880 bbr->r_ctl.rc_pkt_epoch_time = cts;
2881 /* What was our loss rate */
2882 bbr_log_pkt_epoch(bbr, cts, line, lost, del);
2883 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered;
2884 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost;
2885 }
2886
2887 static inline void
bbr_set_epoch(struct tcp_bbr * bbr,uint32_t cts,int32_t line)2888 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2889 {
2890 uint32_t epoch_time;
2891
2892 /* Tick the RTT clock */
2893 bbr->r_ctl.rc_rtt_epoch++;
2894 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start;
2895 bbr_log_time_epoch(bbr, cts, line, epoch_time);
2896 bbr->r_ctl.rc_rcv_epoch_start = cts;
2897 }
2898
2899 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)2900 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked)
2901 {
2902 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) {
2903 bbr->rc_is_pkt_epoch_now = 1;
2904 }
2905 }
2906
2907 /*
2908 * Returns the bw from either the b/w filter
2909 * or from the lt_bw (if the connection is being
2910 * policed).
2911 */
2912 static inline uint64_t
__bbr_get_bw(struct tcp_bbr * bbr)2913 __bbr_get_bw(struct tcp_bbr *bbr)
2914 {
2915 uint64_t bw, min_bw;
2916 uint64_t rtt;
2917 int gm_measure_cnt = 1;
2918
2919 /*
2920 * For startup we make, like google, a
2921 * minimum b/w. This is generated from the
2922 * IW and the rttProp. We do fall back to srtt
2923 * if for some reason (initial handshake) we don't
2924 * have a rttProp. We, in the worst case, fall back
2925 * to the configured min_bw (rc_initial_hptsi_bw).
2926 */
2927 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
2928 /* Attempt first to use rttProp */
2929 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2930 if (rtt && (rtt < 0xffffffff)) {
2931 measure:
2932 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2933 ((uint64_t)1000000);
2934 min_bw /= rtt;
2935 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2936 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2937 }
2938
2939 } else if (bbr->rc_tp->t_srtt != 0) {
2940 /* No rttProp, use srtt? */
2941 rtt = bbr_get_rtt(bbr, BBR_SRTT);
2942 goto measure;
2943 } else {
2944 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2945 }
2946 } else
2947 min_bw = 0;
2948
2949 if ((bbr->rc_past_init_win == 0) &&
2950 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp)))
2951 bbr->rc_past_init_win = 1;
2952 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1))
2953 gm_measure_cnt = 0;
2954 if (gm_measure_cnt &&
2955 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) ||
2956 (bbr->rc_past_init_win == 0))) {
2957 /* For google we use our guess rate until we get 1 measurement */
2958
2959 use_initial_window:
2960 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2961 if (rtt && (rtt < 0xffffffff)) {
2962 /*
2963 * We have an RTT measurement. Use that in
2964 * combination with our initial window to calculate
2965 * a b/w.
2966 */
2967 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2968 ((uint64_t)1000000);
2969 bw /= rtt;
2970 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2971 bw = bbr->r_ctl.rc_initial_hptsi_bw;
2972 }
2973 } else {
2974 /* Drop back to the 40 and punt to a default */
2975 bw = bbr->r_ctl.rc_initial_hptsi_bw;
2976 }
2977 if (bw < 1)
2978 /* Probably should panic */
2979 bw = 1;
2980 if (bw > min_bw)
2981 return (bw);
2982 else
2983 return (min_bw);
2984 }
2985 if (bbr->rc_lt_use_bw)
2986 bw = bbr->r_ctl.rc_lt_bw;
2987 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0))
2988 bw = bbr->r_ctl.red_bw;
2989 else
2990 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
2991 if (bw == 0) {
2992 /* We should not be at 0, go to the initial window then */
2993 goto use_initial_window;
2994 }
2995 if (bw < 1)
2996 /* Probably should panic */
2997 bw = 1;
2998 if (bw < min_bw)
2999 bw = min_bw;
3000 return (bw);
3001 }
3002
3003 static inline uint64_t
bbr_get_bw(struct tcp_bbr * bbr)3004 bbr_get_bw(struct tcp_bbr *bbr)
3005 {
3006 uint64_t bw;
3007
3008 bw = __bbr_get_bw(bbr);
3009 return (bw);
3010 }
3011
3012 static inline void
bbr_reset_lt_bw_interval(struct tcp_bbr * bbr,uint32_t cts)3013 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts)
3014 {
3015 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch;
3016 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time;
3017 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3018 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3019 }
3020
3021 static inline void
bbr_reset_lt_bw_sampling(struct tcp_bbr * bbr,uint32_t cts)3022 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts)
3023 {
3024 bbr->rc_lt_is_sampling = 0;
3025 bbr->rc_lt_use_bw = 0;
3026 bbr->r_ctl.rc_lt_bw = 0;
3027 bbr_reset_lt_bw_interval(bbr, cts);
3028 }
3029
3030 static inline void
bbr_lt_bw_samp_done(struct tcp_bbr * bbr,uint64_t bw,uint32_t cts,uint32_t timin)3031 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin)
3032 {
3033 uint64_t diff;
3034
3035 /* Do we have a previous sample? */
3036 if (bbr->r_ctl.rc_lt_bw) {
3037 /* Get the diff in bytes per second */
3038 if (bbr->r_ctl.rc_lt_bw > bw)
3039 diff = bbr->r_ctl.rc_lt_bw - bw;
3040 else
3041 diff = bw - bbr->r_ctl.rc_lt_bw;
3042 if ((diff <= bbr_lt_bw_diff) ||
3043 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) {
3044 /* Consider us policed */
3045 uint32_t saved_bw;
3046
3047 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw;
3048 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */
3049 bbr->rc_lt_use_bw = 1;
3050 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
3051 /*
3052 * Use pkt based epoch for measuring length of
3053 * policer up
3054 */
3055 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch;
3056 /*
3057 * reason 4 is we need to start consider being
3058 * policed
3059 */
3060 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin);
3061 return;
3062 }
3063 }
3064 bbr->r_ctl.rc_lt_bw = bw;
3065 bbr_reset_lt_bw_interval(bbr, cts);
3066 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin);
3067 }
3068
3069 static void
bbr_randomize_extra_state_time(struct tcp_bbr * bbr)3070 bbr_randomize_extra_state_time(struct tcp_bbr *bbr)
3071 {
3072 uint32_t ran, deduct;
3073
3074 ran = arc4random_uniform(bbr_rand_ot);
3075 if (ran) {
3076 deduct = bbr->r_ctl.rc_level_state_extra / ran;
3077 bbr->r_ctl.rc_level_state_extra -= deduct;
3078 }
3079 }
3080 /*
3081 * Return randomly the starting state
3082 * to use in probebw.
3083 */
3084 static uint8_t
bbr_pick_probebw_substate(struct tcp_bbr * bbr,uint32_t cts)3085 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts)
3086 {
3087 uint32_t ran;
3088 uint8_t ret_val;
3089
3090 /* Initialize the offset to 0 */
3091 bbr->r_ctl.rc_exta_time_gd = 0;
3092 bbr->rc_hit_state_1 = 0;
3093 bbr->r_ctl.rc_level_state_extra = 0;
3094 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1));
3095 /*
3096 * The math works funny here :) the return value is used to set the
3097 * substate and then the state change is called which increments by
3098 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when
3099 * we fully enter the state. Note that the (8 - 1 - ran) assures that
3100 * we return 1 - 7, so we dont return 0 and end up starting in
3101 * state 1 (DRAIN).
3102 */
3103 ret_val = BBR_SUBSTATE_COUNT - 1 - ran;
3104 /* Set an epoch */
3105 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP))
3106 bbr_set_epoch(bbr, cts, __LINE__);
3107
3108 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
3109 return (ret_val);
3110 }
3111
3112 static void
bbr_lt_bw_sampling(struct tcp_bbr * bbr,uint32_t cts,int32_t loss_detected)3113 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected)
3114 {
3115 uint32_t diff, d_time;
3116 uint64_t del_time, bw, lost, delivered;
3117
3118 if (bbr->r_use_policer == 0)
3119 return;
3120 if (bbr->rc_lt_use_bw) {
3121 /* We are using lt bw do we stop yet? */
3122 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
3123 if (diff > bbr_lt_bw_max_rtts) {
3124 /* Reset it all */
3125 reset_all:
3126 bbr_reset_lt_bw_sampling(bbr, cts);
3127 if (bbr->rc_filled_pipe) {
3128 bbr_set_epoch(bbr, cts, __LINE__);
3129 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
3130 bbr_substate_change(bbr, cts, __LINE__, 0);
3131 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
3132 bbr_log_type_statechange(bbr, cts, __LINE__);
3133 } else {
3134 /*
3135 * This should not happen really
3136 * unless we remove the startup/drain
3137 * restrictions above.
3138 */
3139 bbr->rc_bbr_state = BBR_STATE_STARTUP;
3140 bbr_set_epoch(bbr, cts, __LINE__);
3141 bbr->r_ctl.rc_bbr_state_time = cts;
3142 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
3143 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
3144 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
3145 bbr_set_state_target(bbr, __LINE__);
3146 bbr_log_type_statechange(bbr, cts, __LINE__);
3147 }
3148 /* reason 0 is to stop using lt-bw */
3149 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0);
3150 return;
3151 }
3152 if (bbr_lt_intvl_fp == 0) {
3153 /* Not doing false-positive detection */
3154 return;
3155 }
3156 /* False positive detection */
3157 if (diff == bbr_lt_intvl_fp) {
3158 /* At bbr_lt_intvl_fp we record the lost */
3159 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3160 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3161 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) {
3162 /* Now is our loss rate still high? */
3163 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3164 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3165 if ((delivered == 0) ||
3166 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) {
3167 /* No still below our threshold */
3168 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0);
3169 } else {
3170 /* Yikes its still high, it must be a false positive */
3171 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0);
3172 goto reset_all;
3173 }
3174 }
3175 return;
3176 }
3177 /*
3178 * Wait for the first loss before sampling, to let the policer
3179 * exhaust its tokens and estimate the steady-state rate allowed by
3180 * the policer. Starting samples earlier includes bursts that
3181 * over-estimate the bw.
3182 */
3183 if (bbr->rc_lt_is_sampling == 0) {
3184 /* reason 1 is to begin doing the sampling */
3185 if (loss_detected == 0)
3186 return;
3187 bbr_reset_lt_bw_interval(bbr, cts);
3188 bbr->rc_lt_is_sampling = 1;
3189 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0);
3190 return;
3191 }
3192 /* Now how long were we delivering long term last> */
3193 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time))
3194 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time;
3195 else
3196 d_time = 0;
3197
3198 /* To avoid underestimates, reset sampling if we run out of data. */
3199 if (bbr->r_ctl.r_app_limited_until) {
3200 /* Can not measure in app-limited state */
3201 bbr_reset_lt_bw_sampling(bbr, cts);
3202 /* reason 2 is to reset sampling due to app limits */
3203 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time);
3204 return;
3205 }
3206 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
3207 if (diff < bbr_lt_intvl_min_rtts) {
3208 /*
3209 * need more samples (we don't
3210 * start on a round like linux so
3211 * we need 1 more).
3212 */
3213 /* 6 is not_enough time or no-loss */
3214 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3215 return;
3216 }
3217 if (diff > (4 * bbr_lt_intvl_min_rtts)) {
3218 /*
3219 * For now if we wait too long, reset all sampling. We need
3220 * to do some research here, its possible that we should
3221 * base this on how much loss as occurred.. something like
3222 * if its under 10% (or some thresh) reset all otherwise
3223 * don't. Thats for phase II I guess.
3224 */
3225 bbr_reset_lt_bw_sampling(bbr, cts);
3226 /* reason 3 is to reset sampling due too long of sampling */
3227 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3228 return;
3229 }
3230 /*
3231 * End sampling interval when a packet is lost, so we estimate the
3232 * policer tokens were exhausted. Stopping the sampling before the
3233 * tokens are exhausted under-estimates the policed rate.
3234 */
3235 if (loss_detected == 0) {
3236 /* 6 is not_enough time or no-loss */
3237 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3238 return;
3239 }
3240 /* Calculate packets lost and delivered in sampling interval. */
3241 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3242 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3243 if ((delivered == 0) ||
3244 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) {
3245 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time);
3246 return;
3247 }
3248 if (d_time < 1000) {
3249 /* Not enough time. wait */
3250 /* 6 is not_enough time or no-loss */
3251 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3252 return;
3253 }
3254 if (d_time >= (0xffffffff / USECS_IN_MSEC)) {
3255 /* Too long */
3256 bbr_reset_lt_bw_sampling(bbr, cts);
3257 /* reason 3 is to reset sampling due too long of sampling */
3258 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3259 return;
3260 }
3261 del_time = d_time;
3262 bw = delivered;
3263 bw *= (uint64_t)USECS_IN_SECOND;
3264 bw /= del_time;
3265 bbr_lt_bw_samp_done(bbr, bw, cts, d_time);
3266 }
3267
3268 /*
3269 * Allocate a sendmap from our zone.
3270 */
3271 static struct bbr_sendmap *
bbr_alloc(struct tcp_bbr * bbr)3272 bbr_alloc(struct tcp_bbr *bbr)
3273 {
3274 struct bbr_sendmap *rsm;
3275
3276 BBR_STAT_INC(bbr_to_alloc);
3277 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO));
3278 if (rsm) {
3279 bbr->r_ctl.rc_num_maps_alloced++;
3280 return (rsm);
3281 }
3282 if (bbr->r_ctl.rc_free_cnt) {
3283 BBR_STAT_INC(bbr_to_alloc_emerg);
3284 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
3285 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
3286 bbr->r_ctl.rc_free_cnt--;
3287 return (rsm);
3288 }
3289 BBR_STAT_INC(bbr_to_alloc_failed);
3290 return (NULL);
3291 }
3292
3293 static struct bbr_sendmap *
bbr_alloc_full_limit(struct tcp_bbr * bbr)3294 bbr_alloc_full_limit(struct tcp_bbr *bbr)
3295 {
3296 if ((V_tcp_map_entries_limit > 0) &&
3297 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3298 BBR_STAT_INC(bbr_alloc_limited);
3299 if (!bbr->alloc_limit_reported) {
3300 bbr->alloc_limit_reported = 1;
3301 BBR_STAT_INC(bbr_alloc_limited_conns);
3302 }
3303 return (NULL);
3304 }
3305 return (bbr_alloc(bbr));
3306 }
3307
3308 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3309 static struct bbr_sendmap *
bbr_alloc_limit(struct tcp_bbr * bbr,uint8_t limit_type)3310 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type)
3311 {
3312 struct bbr_sendmap *rsm;
3313
3314 if (limit_type) {
3315 /* currently there is only one limit type */
3316 if (V_tcp_map_split_limit > 0 &&
3317 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3318 BBR_STAT_INC(bbr_split_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 }
3326
3327 /* allocate and mark in the limit type, if set */
3328 rsm = bbr_alloc(bbr);
3329 if (rsm != NULL && limit_type) {
3330 rsm->r_limit_type = limit_type;
3331 bbr->r_ctl.rc_num_split_allocs++;
3332 }
3333 return (rsm);
3334 }
3335
3336 static void
bbr_free(struct tcp_bbr * bbr,struct bbr_sendmap * rsm)3337 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
3338 {
3339 if (rsm->r_limit_type) {
3340 /* currently there is only one limit type */
3341 bbr->r_ctl.rc_num_split_allocs--;
3342 }
3343 if (rsm->r_is_smallmap)
3344 bbr->r_ctl.rc_num_small_maps_alloced--;
3345 if (bbr->r_ctl.rc_tlp_send == rsm)
3346 bbr->r_ctl.rc_tlp_send = NULL;
3347 if (bbr->r_ctl.rc_resend == rsm) {
3348 bbr->r_ctl.rc_resend = NULL;
3349 }
3350 if (bbr->r_ctl.rc_next == rsm)
3351 bbr->r_ctl.rc_next = NULL;
3352 if (bbr->r_ctl.rc_sacklast == rsm)
3353 bbr->r_ctl.rc_sacklast = NULL;
3354 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
3355 memset(rsm, 0, sizeof(struct bbr_sendmap));
3356 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
3357 rsm->r_limit_type = 0;
3358 bbr->r_ctl.rc_free_cnt++;
3359 return;
3360 }
3361 bbr->r_ctl.rc_num_maps_alloced--;
3362 uma_zfree(bbr_zone, rsm);
3363 }
3364
3365 /*
3366 * Returns the BDP.
3367 */
3368 static uint64_t
bbr_get_bw_delay_prod(uint64_t rtt,uint64_t bw)3369 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) {
3370 /*
3371 * Calculate the bytes in flight needed given the bw (in bytes per
3372 * second) and the specifyed rtt in useconds. We need to put out the
3373 * returned value per RTT to match that rate. Gain will normally
3374 * raise it up from there.
3375 *
3376 * This should not overflow as long as the bandwidth is below 1
3377 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller
3378 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30).
3379 */
3380 uint64_t usec_per_sec;
3381
3382 usec_per_sec = USECS_IN_SECOND;
3383 return ((rtt * bw) / usec_per_sec);
3384 }
3385
3386 /*
3387 * Return the initial cwnd.
3388 */
3389 static uint32_t
bbr_initial_cwnd(struct tcp_bbr * bbr,struct tcpcb * tp)3390 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp)
3391 {
3392 uint32_t i_cwnd;
3393
3394 if (bbr->rc_init_win) {
3395 i_cwnd = bbr->rc_init_win * tp->t_maxseg;
3396 } else if (V_tcp_initcwnd_segments)
3397 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg),
3398 max(2 * tp->t_maxseg, 14600));
3399 else if (V_tcp_do_rfc3390)
3400 i_cwnd = min(4 * tp->t_maxseg,
3401 max(2 * tp->t_maxseg, 4380));
3402 else {
3403 /* Per RFC5681 Section 3.1 */
3404 if (tp->t_maxseg > 2190)
3405 i_cwnd = 2 * tp->t_maxseg;
3406 else if (tp->t_maxseg > 1095)
3407 i_cwnd = 3 * tp->t_maxseg;
3408 else
3409 i_cwnd = 4 * tp->t_maxseg;
3410 }
3411 return (i_cwnd);
3412 }
3413
3414 /*
3415 * Given a specified gain, return the target
3416 * cwnd based on that gain.
3417 */
3418 static uint32_t
bbr_get_raw_target_cwnd(struct tcp_bbr * bbr,uint32_t gain,uint64_t bw)3419 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw)
3420 {
3421 uint64_t bdp, rtt;
3422 uint32_t cwnd;
3423
3424 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) ||
3425 (bbr_get_full_bw(bbr) == 0)) {
3426 /* No measurements yet */
3427 return (bbr_initial_cwnd(bbr, bbr->rc_tp));
3428 }
3429 /*
3430 * Get bytes per RTT needed (rttProp is normally in
3431 * bbr_cwndtarget_rtt_touse)
3432 */
3433 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse);
3434 /* Get the bdp from the two values */
3435 bdp = bbr_get_bw_delay_prod(rtt, bw);
3436 /* Now apply the gain */
3437 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT));
3438
3439 return (cwnd);
3440 }
3441
3442 static uint32_t
bbr_get_target_cwnd(struct tcp_bbr * bbr,uint64_t bw,uint32_t gain)3443 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain)
3444 {
3445 uint32_t cwnd, mss;
3446
3447 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
3448 /* Get the base cwnd with gain rounded to a mss */
3449 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss);
3450 /*
3451 * Add in N (2 default since we do not have a
3452 * fq layer to trap packets in) quanta's per the I-D
3453 * section 4.2.3.2 quanta adjust.
3454 */
3455 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs);
3456 if (bbr->rc_use_google) {
3457 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3458 (bbr_state_val(bbr) == BBR_SUB_GAIN)) {
3459 /*
3460 * The linux implementation adds
3461 * an extra 2 x mss in gain cycle which
3462 * is documented no-where except in the code.
3463 * so we add more for Neal undocumented feature
3464 */
3465 cwnd += 2 * mss;
3466 }
3467 if ((cwnd / mss) & 0x1) {
3468 /* Round up for odd num mss */
3469 cwnd += mss;
3470 }
3471 }
3472 /* Are we below the min cwnd? */
3473 if (cwnd < get_min_cwnd(bbr))
3474 return (get_min_cwnd(bbr));
3475 return (cwnd);
3476 }
3477
3478 static uint16_t
bbr_gain_adjust(struct tcp_bbr * bbr,uint16_t gain)3479 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain)
3480 {
3481 if (gain < 1)
3482 gain = 1;
3483 return (gain);
3484 }
3485
3486 static uint32_t
bbr_get_header_oh(struct tcp_bbr * bbr)3487 bbr_get_header_oh(struct tcp_bbr *bbr)
3488 {
3489 int seg_oh;
3490
3491 seg_oh = 0;
3492 if (bbr->r_ctl.rc_inc_tcp_oh) {
3493 /* Do we include TCP overhead? */
3494 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr));
3495 }
3496 if (bbr->r_ctl.rc_inc_ip_oh) {
3497 /* Do we include IP overhead? */
3498 #ifdef INET6
3499 if (bbr->r_is_v6) {
3500 seg_oh += sizeof(struct ip6_hdr);
3501 } else
3502 #endif
3503 {
3504
3505 #ifdef INET
3506 seg_oh += sizeof(struct ip);
3507 #endif
3508 }
3509 }
3510 if (bbr->r_ctl.rc_inc_enet_oh) {
3511 /* Do we include the ethernet overhead? */
3512 seg_oh += sizeof(struct ether_header);
3513 }
3514 return(seg_oh);
3515 }
3516
3517 static uint32_t
bbr_get_pacing_length(struct tcp_bbr * bbr,uint16_t gain,uint32_t useconds_time,uint64_t bw)3518 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw)
3519 {
3520 uint64_t divor, res, tim;
3521
3522 if (useconds_time == 0)
3523 return (0);
3524 gain = bbr_gain_adjust(bbr, gain);
3525 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT;
3526 tim = useconds_time;
3527 res = (tim * bw * gain) / divor;
3528 if (res == 0)
3529 res = 1;
3530 return ((uint32_t)res);
3531 }
3532
3533 /*
3534 * Given a gain and a length return the delay in useconds that
3535 * should be used to evenly space out packets
3536 * on the connection (based on the gain factor).
3537 */
3538 static uint32_t
bbr_get_pacing_delay(struct tcp_bbr * bbr,uint16_t gain,int32_t len,uint32_t cts,int nolog)3539 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog)
3540 {
3541 uint64_t bw, lentim, res;
3542 uint32_t usecs, srtt, over = 0;
3543 uint32_t seg_oh, num_segs, maxseg;
3544
3545 if (len == 0)
3546 return (0);
3547
3548 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
3549 num_segs = (len + maxseg - 1) / maxseg;
3550 if (bbr->rc_use_google == 0) {
3551 seg_oh = bbr_get_header_oh(bbr);
3552 len += (num_segs * seg_oh);
3553 }
3554 gain = bbr_gain_adjust(bbr, gain);
3555 bw = bbr_get_bw(bbr);
3556 if (bbr->rc_use_google) {
3557 uint64_t cbw;
3558
3559 /*
3560 * Reduce the b/w by the google discount
3561 * factor 10 = 1%.
3562 */
3563 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount);
3564 cbw /= (uint64_t)1000;
3565 /* We don't apply a discount if it results in 0 */
3566 if (cbw > 0)
3567 bw = cbw;
3568 }
3569 lentim = ((uint64_t)len *
3570 (uint64_t)USECS_IN_SECOND *
3571 (uint64_t)BBR_UNIT);
3572 res = lentim / ((uint64_t)gain * bw);
3573 if (res == 0)
3574 res = 1;
3575 usecs = (uint32_t)res;
3576 srtt = bbr_get_rtt(bbr, BBR_SRTT);
3577 if (bbr_hptsi_max_mul && bbr_hptsi_max_div &&
3578 (bbr->rc_use_google == 0) &&
3579 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) {
3580 /*
3581 * We cannot let the delay be more than 1/2 the srtt time.
3582 * Otherwise we cannot pace out or send properly.
3583 */
3584 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div;
3585 BBR_STAT_INC(bbr_hpts_min_time);
3586 }
3587 if (!nolog)
3588 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1);
3589 return (usecs);
3590 }
3591
3592 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)3593 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack,
3594 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses)
3595 {
3596 uint64_t bw;
3597 uint32_t cwnd, target_cwnd, saved_bytes, maxseg;
3598 int32_t meth;
3599
3600 INP_WLOCK_ASSERT(tptoinpcb(tp));
3601
3602 #ifdef STATS
3603 if ((tp->t_flags & TF_GPUTINPROG) &&
3604 SEQ_GEQ(th->th_ack, tp->gput_ack)) {
3605 /*
3606 * Strech acks and compressed acks will cause this to
3607 * oscillate but we are doing it the same way as the main
3608 * stack so it will be compariable (though possibly not
3609 * ideal).
3610 */
3611 int32_t cgput;
3612 int64_t gput, time_stamp;
3613
3614 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8;
3615 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000));
3616 cgput = gput / time_stamp;
3617 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
3618 cgput);
3619 if (tp->t_stats_gput_prev > 0)
3620 stats_voi_update_abs_s32(tp->t_stats,
3621 VOI_TCP_GPUT_ND,
3622 ((gput - tp->t_stats_gput_prev) * 100) /
3623 tp->t_stats_gput_prev);
3624 tp->t_flags &= ~TF_GPUTINPROG;
3625 tp->t_stats_gput_prev = cgput;
3626 }
3627 #endif
3628 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3629 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
3630 /* We don't change anything in probe-rtt */
3631 return;
3632 }
3633 maxseg = tp->t_maxseg - bbr->rc_last_options;
3634 saved_bytes = bytes_this_ack;
3635 bytes_this_ack += sack_changed;
3636 if (bytes_this_ack > prev_acked) {
3637 bytes_this_ack -= prev_acked;
3638 /*
3639 * A byte ack'd gives us a full mss
3640 * to be like linux i.e. they count packets.
3641 */
3642 if ((bytes_this_ack < maxseg) && bbr->rc_use_google)
3643 bytes_this_ack = maxseg;
3644 } else {
3645 /* Unlikely */
3646 bytes_this_ack = 0;
3647 }
3648 cwnd = tp->snd_cwnd;
3649 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3650 if (bw)
3651 target_cwnd = bbr_get_target_cwnd(bbr,
3652 bw,
3653 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain);
3654 else
3655 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp);
3656 if (IN_RECOVERY(tp->t_flags) &&
3657 (bbr->bbr_prev_in_rec == 0)) {
3658 /*
3659 * We are entering recovery and
3660 * thus packet conservation.
3661 */
3662 bbr->pkt_conservation = 1;
3663 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime;
3664 cwnd = ctf_flight_size(tp,
3665 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3666 bytes_this_ack;
3667 }
3668 if (IN_RECOVERY(tp->t_flags)) {
3669 uint32_t flight;
3670
3671 bbr->bbr_prev_in_rec = 1;
3672 if (cwnd > losses) {
3673 cwnd -= losses;
3674 if (cwnd < maxseg)
3675 cwnd = maxseg;
3676 } else
3677 cwnd = maxseg;
3678 flight = ctf_flight_size(tp,
3679 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3680 bbr_log_type_cwndupd(bbr, flight, 0,
3681 losses, 10, 0, 0, line);
3682 if (bbr->pkt_conservation) {
3683 uint32_t time_in;
3684
3685 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start))
3686 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start;
3687 else
3688 time_in = 0;
3689
3690 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
3691 /* Clear packet conservation after an rttProp */
3692 bbr->pkt_conservation = 0;
3693 } else {
3694 if ((flight + bytes_this_ack) > cwnd)
3695 cwnd = flight + bytes_this_ack;
3696 if (cwnd < get_min_cwnd(bbr))
3697 cwnd = get_min_cwnd(bbr);
3698 tp->snd_cwnd = cwnd;
3699 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed,
3700 prev_acked, 1, target_cwnd, th->th_ack, line);
3701 return;
3702 }
3703 }
3704 } else
3705 bbr->bbr_prev_in_rec = 0;
3706 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) {
3707 bbr->r_ctl.restrict_growth--;
3708 if (bytes_this_ack > maxseg)
3709 bytes_this_ack = maxseg;
3710 }
3711 if (bbr->rc_filled_pipe) {
3712 /*
3713 * Here we have exited startup and filled the pipe. We will
3714 * thus allow the cwnd to shrink to the target. We hit here
3715 * mostly.
3716 */
3717 uint32_t s_cwnd;
3718
3719 meth = 2;
3720 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd);
3721 if (s_cwnd > cwnd)
3722 cwnd = s_cwnd;
3723 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing)
3724 cwnd = s_cwnd;
3725 } else {
3726 /*
3727 * Here we are still in startup, we increase cwnd by what
3728 * has been acked.
3729 */
3730 if ((cwnd < target_cwnd) ||
3731 (bbr->rc_past_init_win == 0)) {
3732 meth = 3;
3733 cwnd += bytes_this_ack;
3734 } else {
3735 /*
3736 * Method 4 means we are at target so no gain in
3737 * startup and past the initial window.
3738 */
3739 meth = 4;
3740 }
3741 }
3742 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr));
3743 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line);
3744 }
3745
3746 static void
tcp_bbr_partialack(struct tcpcb * tp)3747 tcp_bbr_partialack(struct tcpcb *tp)
3748 {
3749 struct tcp_bbr *bbr;
3750
3751 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3752 INP_WLOCK_ASSERT(tptoinpcb(tp));
3753 if (ctf_flight_size(tp,
3754 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
3755 tp->snd_cwnd) {
3756 bbr->r_wanted_output = 1;
3757 }
3758 }
3759
3760 static void
bbr_post_recovery(struct tcpcb * tp)3761 bbr_post_recovery(struct tcpcb *tp)
3762 {
3763 struct tcp_bbr *bbr;
3764 uint32_t flight;
3765
3766 INP_WLOCK_ASSERT(tptoinpcb(tp));
3767 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3768 /*
3769 * Here we just exit recovery.
3770 */
3771 EXIT_RECOVERY(tp->t_flags);
3772 /* Lock in our b/w reduction for the specified number of pkt-epochs */
3773 bbr->r_recovery_bw = 0;
3774 tp->snd_recover = tp->snd_una;
3775 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3776 bbr->pkt_conservation = 0;
3777 if (bbr->rc_use_google == 0) {
3778 /*
3779 * For non-google mode lets
3780 * go ahead and make sure we clear
3781 * the recovery state so if we
3782 * bounce back in to recovery we
3783 * will do PC.
3784 */
3785 bbr->bbr_prev_in_rec = 0;
3786 }
3787 bbr_log_type_exit_rec(bbr);
3788 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
3789 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
3790 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__);
3791 } else {
3792 /* For probe-rtt case lets fix up its saved_cwnd */
3793 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) {
3794 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent;
3795 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__);
3796 }
3797 }
3798 flight = ctf_flight_size(tp,
3799 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3800 if ((bbr->rc_use_google == 0) &&
3801 bbr_do_red) {
3802 uint64_t val, lr2use;
3803 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd;
3804 uint32_t *cwnd_p;
3805
3806 if (bbr_get_rtt(bbr, BBR_SRTT)) {
3807 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000);
3808 val /= bbr_get_rtt(bbr, BBR_SRTT);
3809 ratio = (uint32_t)val;
3810 } else
3811 ratio = 1000;
3812
3813 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div,
3814 bbr->r_ctl.recovery_lr, 21,
3815 ratio,
3816 bbr->r_ctl.rc_red_cwnd_pe,
3817 __LINE__);
3818 if ((ratio < bbr_do_red) || (bbr_do_red == 0))
3819 goto done;
3820 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3821 bbr_prtt_slam_cwnd) ||
3822 (bbr_sub_drain_slam_cwnd &&
3823 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3824 bbr->rc_hit_state_1 &&
3825 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) ||
3826 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) &&
3827 bbr_slam_cwnd_in_main_drain)) {
3828 /*
3829 * Here we must poke at the saved cwnd
3830 * as well as the cwnd.
3831 */
3832 cwnd = bbr->r_ctl.rc_saved_cwnd;
3833 cwnd_p = &bbr->r_ctl.rc_saved_cwnd;
3834 } else {
3835 cwnd = tp->snd_cwnd;
3836 cwnd_p = &tp->snd_cwnd;
3837 }
3838 maxseg = tp->t_maxseg - bbr->rc_last_options;
3839 /* Add the overall lr with the recovery lr */
3840 if (bbr->r_ctl.rc_lost == 0)
3841 lr2use = 0;
3842 else if (bbr->r_ctl.rc_delivered == 0)
3843 lr2use = 1000;
3844 else {
3845 lr2use = bbr->r_ctl.rc_lost * 1000;
3846 lr2use /= bbr->r_ctl.rc_delivered;
3847 }
3848 lr2use += bbr->r_ctl.recovery_lr;
3849 acks_inflight = (flight / (maxseg * 2));
3850 if (bbr_red_scale) {
3851 lr2use *= bbr_get_rtt(bbr, BBR_SRTT);
3852 lr2use /= bbr_red_scale;
3853 if ((bbr_red_growth_restrict) &&
3854 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1))
3855 bbr->r_ctl.restrict_growth += acks_inflight;
3856 }
3857 if (lr2use) {
3858 val = (uint64_t)cwnd * lr2use;
3859 val /= 1000;
3860 if (cwnd > val)
3861 newcwnd = roundup((cwnd - val), maxseg);
3862 else
3863 newcwnd = maxseg;
3864 } else {
3865 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul;
3866 val /= (uint64_t)bbr_red_div;
3867 newcwnd = roundup((uint32_t)val, maxseg);
3868 }
3869 /* with standard delayed acks how many acks can I expect? */
3870 if (bbr_drop_limit == 0) {
3871 /*
3872 * Anticpate how much we will
3873 * raise the cwnd based on the acks.
3874 */
3875 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) {
3876 /* We do enforce the min (with the acks) */
3877 newcwnd = (get_min_cwnd(bbr) - acks_inflight);
3878 }
3879 } else {
3880 /*
3881 * A strict drop limit of N is inplace
3882 */
3883 if (newcwnd < (bbr_drop_limit * maxseg)) {
3884 newcwnd = bbr_drop_limit * maxseg;
3885 }
3886 }
3887 /* For the next N acks do we restrict the growth */
3888 *cwnd_p = newcwnd;
3889 if (tp->snd_cwnd > newcwnd)
3890 tp->snd_cwnd = newcwnd;
3891 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22,
3892 (uint32_t)lr2use,
3893 bbr_get_rtt(bbr, BBR_SRTT), __LINE__);
3894 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch;
3895 }
3896 done:
3897 bbr->r_ctl.recovery_lr = 0;
3898 if (flight <= tp->snd_cwnd) {
3899 bbr->r_wanted_output = 1;
3900 }
3901 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3902 }
3903
3904 static void
bbr_setup_red_bw(struct tcp_bbr * bbr,uint32_t cts)3905 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts)
3906 {
3907 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3908 /* Limit the drop in b/w to 1/2 our current filter. */
3909 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate)
3910 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate;
3911 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2))
3912 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2;
3913 tcp_bbr_tso_size_check(bbr, cts);
3914 }
3915
3916 static void
bbr_cong_signal(struct tcpcb * tp,struct tcphdr * th,uint32_t type,struct bbr_sendmap * rsm)3917 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm)
3918 {
3919 struct tcp_bbr *bbr;
3920
3921 INP_WLOCK_ASSERT(tptoinpcb(tp));
3922 #ifdef STATS
3923 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
3924 #endif
3925 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3926 switch (type) {
3927 case CC_NDUPACK:
3928 if (!IN_RECOVERY(tp->t_flags)) {
3929 tp->snd_recover = tp->snd_max;
3930 /* Start a new epoch */
3931 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
3932 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) {
3933 /*
3934 * Move forward the lt epoch
3935 * so it won't count the truncated
3936 * epoch.
3937 */
3938 bbr->r_ctl.rc_lt_epoch++;
3939 }
3940 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
3941 /*
3942 * Just like the policer detection code
3943 * if we are in startup we must push
3944 * forward the last startup epoch
3945 * to hide the truncated PE.
3946 */
3947 bbr->r_ctl.rc_bbr_last_startup_epoch++;
3948 }
3949 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd;
3950 ENTER_RECOVERY(tp->t_flags);
3951 bbr->rc_tlp_rtx_out = 0;
3952 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate;
3953 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3954 if (tcp_in_hpts(bbr->rc_tp) &&
3955 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) {
3956 /*
3957 * When we enter recovery, we need to restart
3958 * any timers. This may mean we gain an agg
3959 * early, which will be made up for at the last
3960 * rxt out.
3961 */
3962 bbr->rc_timer_first = 1;
3963 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
3964 }
3965 /*
3966 * Calculate a new cwnd based on to the current
3967 * delivery rate with no gain. We get the bdp
3968 * without gaining it up like we normally would and
3969 * we use the last cur_del_rate.
3970 */
3971 if ((bbr->rc_use_google == 0) &&
3972 (bbr->r_ctl.bbr_rttprobe_gain_val ||
3973 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) {
3974 tp->snd_cwnd = ctf_flight_size(tp,
3975 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3976 (tp->t_maxseg - bbr->rc_last_options);
3977 if (tp->snd_cwnd < get_min_cwnd(bbr)) {
3978 /* We always gate to min cwnd */
3979 tp->snd_cwnd = get_min_cwnd(bbr);
3980 }
3981 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__);
3982 }
3983 bbr_log_type_enter_rec(bbr, rsm->r_start);
3984 }
3985 break;
3986 case CC_RTO_ERR:
3987 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
3988 /* RTO was unnecessary, so reset everything. */
3989 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime);
3990 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
3991 tp->snd_cwnd = tp->snd_cwnd_prev;
3992 tp->snd_ssthresh = tp->snd_ssthresh_prev;
3993 tp->snd_recover = tp->snd_recover_prev;
3994 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
3995 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__);
3996 }
3997 tp->t_badrxtwin = 0;
3998 break;
3999 }
4000 }
4001
4002 /*
4003 * Indicate whether this ack should be delayed. We can delay the ack if
4004 * following conditions are met:
4005 * - There is no delayed ack timer in progress.
4006 * - Our last ack wasn't a 0-sized window. We never want to delay
4007 * the ack that opens up a 0-sized window.
4008 * - LRO wasn't used for this segment. We make sure by checking that the
4009 * segment size is not larger than the MSS.
4010 * - Delayed acks are enabled or this is a half-synchronized T/TCP
4011 * connection.
4012 * - The data being acked is less than a full segment (a stretch ack
4013 * of more than a segment we should ack.
4014 * - nsegs is 1 (if its more than that we received more than 1 ack).
4015 */
4016 #define DELAY_ACK(tp, bbr, nsegs) \
4017 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4018 ((tp->t_flags & TF_DELACK) == 0) && \
4019 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \
4020 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4021
4022 /*
4023 * Return the lowest RSM in the map of
4024 * packets still in flight that is not acked.
4025 * This should normally find on the first one
4026 * since we remove packets from the send
4027 * map after they are marked ACKED.
4028 */
4029 static struct bbr_sendmap *
bbr_find_lowest_rsm(struct tcp_bbr * bbr)4030 bbr_find_lowest_rsm(struct tcp_bbr *bbr)
4031 {
4032 struct bbr_sendmap *rsm;
4033
4034 /*
4035 * Walk the time-order transmitted list looking for an rsm that is
4036 * not acked. This will be the one that was sent the longest time
4037 * ago that is still outstanding.
4038 */
4039 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) {
4040 if (rsm->r_flags & BBR_ACKED) {
4041 continue;
4042 }
4043 goto finish;
4044 }
4045 finish:
4046 return (rsm);
4047 }
4048
4049 static struct bbr_sendmap *
bbr_find_high_nonack(struct tcp_bbr * bbr,struct bbr_sendmap * rsm)4050 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
4051 {
4052 struct bbr_sendmap *prsm;
4053
4054 /*
4055 * Walk the sequence order list backward until we hit and arrive at
4056 * the highest seq not acked. In theory when this is called it
4057 * should be the last segment (which it was not).
4058 */
4059 prsm = rsm;
4060 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4061 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) {
4062 continue;
4063 }
4064 return (prsm);
4065 }
4066 return (NULL);
4067 }
4068
4069 /*
4070 * Returns to the caller the number of microseconds that
4071 * the packet can be outstanding before we think we
4072 * should have had an ack returned.
4073 */
4074 static uint32_t
bbr_calc_thresh_rack(struct tcp_bbr * bbr,uint32_t srtt,uint32_t cts,struct bbr_sendmap * rsm)4075 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm)
4076 {
4077 /*
4078 * lro is the flag we use to determine if we have seen reordering.
4079 * If it gets set we have seen reordering. The reorder logic either
4080 * works in one of two ways:
4081 *
4082 * If reorder-fade is configured, then we track the last time we saw
4083 * re-ordering occur. If we reach the point where enough time as
4084 * passed we no longer consider reordering has occuring.
4085 *
4086 * Or if reorder-face is 0, then once we see reordering we consider
4087 * the connection to alway be subject to reordering and just set lro
4088 * to 1.
4089 *
4090 * In the end if lro is non-zero we add the extra time for
4091 * reordering in.
4092 */
4093 int32_t lro;
4094 uint32_t thresh, t_rxtcur;
4095
4096 if (srtt == 0)
4097 srtt = 1;
4098 if (bbr->r_ctl.rc_reorder_ts) {
4099 if (bbr->r_ctl.rc_reorder_fade) {
4100 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) {
4101 lro = cts - bbr->r_ctl.rc_reorder_ts;
4102 if (lro == 0) {
4103 /*
4104 * No time as passed since the last
4105 * reorder, mark it as reordering.
4106 */
4107 lro = 1;
4108 }
4109 } else {
4110 /* Negative time? */
4111 lro = 0;
4112 }
4113 if (lro > bbr->r_ctl.rc_reorder_fade) {
4114 /* Turn off reordering seen too */
4115 bbr->r_ctl.rc_reorder_ts = 0;
4116 lro = 0;
4117 }
4118 } else {
4119 /* Reodering does not fade */
4120 lro = 1;
4121 }
4122 } else {
4123 lro = 0;
4124 }
4125 thresh = srtt + bbr->r_ctl.rc_pkt_delay;
4126 if (lro) {
4127 /* It must be set, if not you get 1/4 rtt */
4128 if (bbr->r_ctl.rc_reorder_shift)
4129 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift);
4130 else
4131 thresh += (srtt >> 2);
4132 } else {
4133 thresh += 1000;
4134 }
4135 /* We don't let the rack timeout be above a RTO */
4136 if ((bbr->rc_tp)->t_srtt == 0)
4137 t_rxtcur = BBR_INITIAL_RTO;
4138 else
4139 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
4140 if (thresh > t_rxtcur) {
4141 thresh = t_rxtcur;
4142 }
4143 /* And we don't want it above the RTO max either */
4144 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4145 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4146 }
4147 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK);
4148 return (thresh);
4149 }
4150
4151 /*
4152 * Return to the caller the amount of time in mico-seconds
4153 * that should be used for the TLP timer from the last
4154 * send time of this packet.
4155 */
4156 static uint32_t
bbr_calc_thresh_tlp(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t srtt,uint32_t cts)4157 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
4158 struct bbr_sendmap *rsm, uint32_t srtt,
4159 uint32_t cts)
4160 {
4161 uint32_t thresh, len, maxseg, t_rxtcur;
4162 struct bbr_sendmap *prsm;
4163
4164 if (srtt == 0)
4165 srtt = 1;
4166 if (bbr->rc_tlp_threshold)
4167 thresh = srtt + (srtt / bbr->rc_tlp_threshold);
4168 else
4169 thresh = (srtt * 2);
4170 maxseg = tp->t_maxseg - bbr->rc_last_options;
4171 /* Get the previous sent packet, if any */
4172 len = rsm->r_end - rsm->r_start;
4173
4174 /* 2.1 behavior */
4175 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext);
4176 if (prsm && (len <= maxseg)) {
4177 /*
4178 * Two packets outstanding, thresh should be (2*srtt) +
4179 * possible inter-packet delay (if any).
4180 */
4181 uint32_t inter_gap = 0;
4182 int idx, nidx;
4183
4184 idx = rsm->r_rtr_cnt - 1;
4185 nidx = prsm->r_rtr_cnt - 1;
4186 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
4187 /* Yes it was sent later (or at the same time) */
4188 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
4189 }
4190 thresh += inter_gap;
4191 } else if (len <= maxseg) {
4192 /*
4193 * Possibly compensate for delayed-ack.
4194 */
4195 uint32_t alt_thresh;
4196
4197 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time;
4198 if (alt_thresh > thresh)
4199 thresh = alt_thresh;
4200 }
4201 /* Not above the current RTO */
4202 if (tp->t_srtt == 0)
4203 t_rxtcur = BBR_INITIAL_RTO;
4204 else
4205 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur);
4206
4207 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP);
4208 /* Not above an RTO */
4209 if (thresh > t_rxtcur) {
4210 thresh = t_rxtcur;
4211 }
4212 /* Not above a RTO max */
4213 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4214 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4215 }
4216 /* And now apply the user TLP min */
4217 if (thresh < bbr_tlp_min) {
4218 thresh = bbr_tlp_min;
4219 }
4220 return (thresh);
4221 }
4222
4223 /*
4224 * Return one of three RTTs to use (in microseconds).
4225 */
4226 static __inline uint32_t
bbr_get_rtt(struct tcp_bbr * bbr,int32_t rtt_type)4227 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type)
4228 {
4229 uint32_t f_rtt;
4230 uint32_t srtt;
4231
4232 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
4233 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) {
4234 /* We have no rtt at all */
4235 if (bbr->rc_tp->t_srtt == 0)
4236 f_rtt = BBR_INITIAL_RTO;
4237 else
4238 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4239 /*
4240 * Since we don't know how good the rtt is apply a
4241 * delayed-ack min
4242 */
4243 if (f_rtt < bbr_delayed_ack_time) {
4244 f_rtt = bbr_delayed_ack_time;
4245 }
4246 }
4247 /* Take the filter version or last measured pkt-rtt */
4248 if (rtt_type == BBR_RTT_PROP) {
4249 srtt = f_rtt;
4250 } else if (rtt_type == BBR_RTT_PKTRTT) {
4251 if (bbr->r_ctl.rc_pkt_epoch_rtt) {
4252 srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
4253 } else {
4254 /* No pkt rtt yet */
4255 srtt = f_rtt;
4256 }
4257 } else if (rtt_type == BBR_RTT_RACK) {
4258 srtt = bbr->r_ctl.rc_last_rtt;
4259 /* We need to add in any internal delay for our timer */
4260 if (bbr->rc_ack_was_delayed)
4261 srtt += bbr->r_ctl.rc_ack_hdwr_delay;
4262 } else if (rtt_type == BBR_SRTT) {
4263 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4264 } else {
4265 /* TSNH */
4266 srtt = f_rtt;
4267 #ifdef BBR_INVARIANTS
4268 panic("Unknown rtt request type %d", rtt_type);
4269 #endif
4270 }
4271 return (srtt);
4272 }
4273
4274 static int
bbr_is_lost(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts)4275 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts)
4276 {
4277 uint32_t thresh;
4278
4279 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK),
4280 cts, rsm);
4281 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) {
4282 /* It is lost (past time) */
4283 return (1);
4284 }
4285 return (0);
4286 }
4287
4288 /*
4289 * Return a sendmap if we need to retransmit something.
4290 */
4291 static struct bbr_sendmap *
bbr_check_recovery_mode(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4292 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4293 {
4294 /*
4295 * Check to see that we don't need to fall into recovery. We will
4296 * need to do so if our oldest transmit is past the time we should
4297 * have had an ack.
4298 */
4299
4300 struct bbr_sendmap *rsm;
4301 int32_t idx;
4302
4303 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) {
4304 /* Nothing outstanding that we know of */
4305 return (NULL);
4306 }
4307 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
4308 if (rsm == NULL) {
4309 /* Nothing in the transmit map */
4310 return (NULL);
4311 }
4312 if (tp->t_flags & TF_SENTFIN) {
4313 /* Fin restricted, don't find anything once a fin is sent */
4314 return (NULL);
4315 }
4316 if (rsm->r_flags & BBR_ACKED) {
4317 /*
4318 * Ok the first one is acked (this really should not happen
4319 * since we remove the from the tmap once they are acked)
4320 */
4321 rsm = bbr_find_lowest_rsm(bbr);
4322 if (rsm == NULL)
4323 return (NULL);
4324 }
4325 idx = rsm->r_rtr_cnt - 1;
4326 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) {
4327 /* Send timestamp is the same or less? can't be ready */
4328 return (NULL);
4329 }
4330 /* Get our RTT time */
4331 if (bbr_is_lost(bbr, rsm, cts) &&
4332 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
4333 (rsm->r_flags & BBR_SACK_PASSED))) {
4334 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4335 rsm->r_flags |= BBR_MARKED_LOST;
4336 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4337 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4338 }
4339 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm);
4340 #ifdef BBR_INVARIANTS
4341 if ((rsm->r_end - rsm->r_start) == 0)
4342 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm);
4343 #endif
4344 return (rsm);
4345 }
4346 return (NULL);
4347 }
4348
4349 /*
4350 * RACK Timer, here we simply do logging and house keeping.
4351 * the normal bbr_output_wtime() function will call the
4352 * appropriate thing to check if we need to do a RACK retransmit.
4353 * We return 1, saying don't proceed with bbr_output_wtime only
4354 * when all timers have been stopped (destroyed PCB?).
4355 */
4356 static int
bbr_timeout_rack(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4357 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4358 {
4359 /*
4360 * This timer simply provides an internal trigger to send out data.
4361 * The check_recovery_mode call will see if there are needed
4362 * retransmissions, if so we will enter fast-recovery. The output
4363 * call may or may not do the same thing depending on sysctl
4364 * settings.
4365 */
4366 uint32_t lost;
4367
4368 if (bbr->rc_all_timers_stopped) {
4369 return (1);
4370 }
4371 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4372 /* Its not time yet */
4373 return (0);
4374 }
4375 BBR_STAT_INC(bbr_to_tot);
4376 lost = bbr->r_ctl.rc_lost;
4377 if (bbr->r_state && (bbr->r_state != tp->t_state))
4378 bbr_set_state(tp, bbr, 0);
4379 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK);
4380 if (bbr->r_ctl.rc_resend == NULL) {
4381 /* Lets do the check here */
4382 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
4383 }
4384 if (bbr_policer_call_from_rack_to)
4385 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4386 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
4387 return (0);
4388 }
4389
4390 static __inline void
bbr_clone_rsm(struct tcp_bbr * bbr,struct bbr_sendmap * nrsm,struct bbr_sendmap * rsm,uint32_t start)4391 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start)
4392 {
4393 int idx;
4394
4395 nrsm->r_start = start;
4396 nrsm->r_end = rsm->r_end;
4397 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
4398 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed;
4399 nrsm->r_flags = rsm->r_flags;
4400 /* We don't transfer forward the SYN flag */
4401 nrsm->r_flags &= ~BBR_HAS_SYN;
4402 /* We move forward the FIN flag, not that this should happen */
4403 rsm->r_flags &= ~BBR_HAS_FIN;
4404 nrsm->r_dupack = rsm->r_dupack;
4405 nrsm->r_rtr_bytes = 0;
4406 nrsm->r_is_gain = rsm->r_is_gain;
4407 nrsm->r_is_drain = rsm->r_is_drain;
4408 nrsm->r_delivered = rsm->r_delivered;
4409 nrsm->r_ts_valid = rsm->r_ts_valid;
4410 nrsm->r_del_ack_ts = rsm->r_del_ack_ts;
4411 nrsm->r_del_time = rsm->r_del_time;
4412 nrsm->r_app_limited = rsm->r_app_limited;
4413 nrsm->r_first_sent_time = rsm->r_first_sent_time;
4414 nrsm->r_flight_at_send = rsm->r_flight_at_send;
4415 /* We split a piece the lower section looses any just_ret flag. */
4416 nrsm->r_bbr_state = rsm->r_bbr_state;
4417 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
4418 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
4419 }
4420 rsm->r_end = nrsm->r_start;
4421 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
4422 idx /= 8;
4423 /* Check if we got too small */
4424 if ((rsm->r_is_smallmap == 0) &&
4425 ((rsm->r_end - rsm->r_start) <= idx)) {
4426 bbr->r_ctl.rc_num_small_maps_alloced++;
4427 rsm->r_is_smallmap = 1;
4428 }
4429 /* Check the new one as well */
4430 if ((nrsm->r_end - nrsm->r_start) <= idx) {
4431 bbr->r_ctl.rc_num_small_maps_alloced++;
4432 nrsm->r_is_smallmap = 1;
4433 }
4434 }
4435
4436 static int
bbr_sack_mergable(struct bbr_sendmap * at,uint32_t start,uint32_t end)4437 bbr_sack_mergable(struct bbr_sendmap *at,
4438 uint32_t start, uint32_t end)
4439 {
4440 /*
4441 * Given a sack block defined by
4442 * start and end, and a current position
4443 * at. Return 1 if either side of at
4444 * would show that the block is mergable
4445 * to that side. A block to be mergable
4446 * must have overlap with the start/end
4447 * and be in the SACK'd state.
4448 */
4449 struct bbr_sendmap *l_rsm;
4450 struct bbr_sendmap *r_rsm;
4451
4452 /* first get the either side blocks */
4453 l_rsm = TAILQ_PREV(at, bbr_head, r_next);
4454 r_rsm = TAILQ_NEXT(at, r_next);
4455 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) {
4456 /* Potentially mergeable */
4457 if ((l_rsm->r_end == start) ||
4458 (SEQ_LT(start, l_rsm->r_end) &&
4459 SEQ_GT(end, l_rsm->r_end))) {
4460 /*
4461 * map blk |------|
4462 * sack blk |------|
4463 * <or>
4464 * map blk |------|
4465 * sack blk |------|
4466 */
4467 return (1);
4468 }
4469 }
4470 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) {
4471 /* Potentially mergeable */
4472 if ((r_rsm->r_start == end) ||
4473 (SEQ_LT(start, r_rsm->r_start) &&
4474 SEQ_GT(end, r_rsm->r_start))) {
4475 /*
4476 * map blk |---------|
4477 * sack blk |----|
4478 * <or>
4479 * map blk |---------|
4480 * sack blk |-------|
4481 */
4482 return (1);
4483 }
4484 }
4485 return (0);
4486 }
4487
4488 static struct bbr_sendmap *
bbr_merge_rsm(struct tcp_bbr * bbr,struct bbr_sendmap * l_rsm,struct bbr_sendmap * r_rsm)4489 bbr_merge_rsm(struct tcp_bbr *bbr,
4490 struct bbr_sendmap *l_rsm,
4491 struct bbr_sendmap *r_rsm)
4492 {
4493 /*
4494 * We are merging two ack'd RSM's,
4495 * the l_rsm is on the left (lower seq
4496 * values) and the r_rsm is on the right
4497 * (higher seq value). The simplest way
4498 * to merge these is to move the right
4499 * one into the left. I don't think there
4500 * is any reason we need to try to find
4501 * the oldest (or last oldest retransmitted).
4502 */
4503 l_rsm->r_end = r_rsm->r_end;
4504 if (l_rsm->r_dupack < r_rsm->r_dupack)
4505 l_rsm->r_dupack = r_rsm->r_dupack;
4506 if (r_rsm->r_rtr_bytes)
4507 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
4508 if (r_rsm->r_in_tmap) {
4509 /* This really should not happen */
4510 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext);
4511 }
4512 if (r_rsm->r_app_limited)
4513 l_rsm->r_app_limited = r_rsm->r_app_limited;
4514 /* Now the flags */
4515 if (r_rsm->r_flags & BBR_HAS_FIN)
4516 l_rsm->r_flags |= BBR_HAS_FIN;
4517 if (r_rsm->r_flags & BBR_TLP)
4518 l_rsm->r_flags |= BBR_TLP;
4519 if (r_rsm->r_flags & BBR_RWND_COLLAPSED)
4520 l_rsm->r_flags |= BBR_RWND_COLLAPSED;
4521 if (r_rsm->r_flags & BBR_MARKED_LOST) {
4522 /* This really should not happen */
4523 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start;
4524 }
4525 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next);
4526 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
4527 /* Transfer the split limit to the map we free */
4528 r_rsm->r_limit_type = l_rsm->r_limit_type;
4529 l_rsm->r_limit_type = 0;
4530 }
4531 bbr_free(bbr, r_rsm);
4532 return(l_rsm);
4533 }
4534
4535 /*
4536 * TLP Timer, here we simply setup what segment we want to
4537 * have the TLP expire on, the normal bbr_output_wtime() will then
4538 * send it out.
4539 *
4540 * We return 1, saying don't proceed with bbr_output_wtime only
4541 * when all timers have been stopped (destroyed PCB?).
4542 */
4543 static int
bbr_timeout_tlp(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4544 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4545 {
4546 /*
4547 * Tail Loss Probe.
4548 */
4549 struct bbr_sendmap *rsm = NULL;
4550 struct socket *so;
4551 uint32_t amm;
4552 uint32_t out, avail;
4553 uint32_t maxseg;
4554 int collapsed_win = 0;
4555
4556 if (bbr->rc_all_timers_stopped) {
4557 return (1);
4558 }
4559 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4560 /* Its not time yet */
4561 return (0);
4562 }
4563 if (ctf_progress_timeout_check(tp, true)) {
4564 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4565 return (-ETIMEDOUT); /* tcp_drop() */
4566 }
4567 /* Did we somehow get into persists? */
4568 if (bbr->rc_in_persist) {
4569 return (0);
4570 }
4571 if (bbr->r_state && (bbr->r_state != tp->t_state))
4572 bbr_set_state(tp, bbr, 0);
4573 BBR_STAT_INC(bbr_tlp_tot);
4574 maxseg = tp->t_maxseg - bbr->rc_last_options;
4575 /*
4576 * A TLP timer has expired. We have been idle for 2 rtts. So we now
4577 * need to figure out how to force a full MSS segment out.
4578 */
4579 so = tptosocket(tp);
4580 avail = sbavail(&so->so_snd);
4581 out = ctf_outstanding(tp);
4582 if (out > tp->snd_wnd) {
4583 /* special case, we need a retransmission */
4584 collapsed_win = 1;
4585 goto need_retran;
4586 }
4587 if (avail > out) {
4588 /* New data is available */
4589 amm = avail - out;
4590 if (amm > maxseg) {
4591 amm = maxseg;
4592 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) {
4593 /* not enough to fill a MTU and no-delay is off */
4594 goto need_retran;
4595 }
4596 /* Set the send-new override */
4597 if ((out + amm) <= tp->snd_wnd) {
4598 bbr->rc_tlp_new_data = 1;
4599 } else {
4600 goto need_retran;
4601 }
4602 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4603 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max;
4604 bbr->r_ctl.rc_tlp_send = NULL;
4605 /* cap any slots */
4606 BBR_STAT_INC(bbr_tlp_newdata);
4607 goto send;
4608 }
4609 need_retran:
4610 /*
4611 * Ok we need to arrange the last un-acked segment to be re-sent, or
4612 * optionally the first un-acked segment.
4613 */
4614 if (collapsed_win == 0) {
4615 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
4616 if (rsm && (BBR_ACKED | BBR_HAS_FIN)) {
4617 rsm = bbr_find_high_nonack(bbr, rsm);
4618 }
4619 if (rsm == NULL) {
4620 goto restore;
4621 }
4622 } else {
4623 /*
4624 * We must find the last segment
4625 * that was acceptable by the client.
4626 */
4627 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4628 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) {
4629 /* Found one */
4630 break;
4631 }
4632 }
4633 if (rsm == NULL) {
4634 /* None? if so send the first */
4635 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4636 if (rsm == NULL)
4637 goto restore;
4638 }
4639 }
4640 if ((rsm->r_end - rsm->r_start) > maxseg) {
4641 /*
4642 * We need to split this the last segment in two.
4643 */
4644 struct bbr_sendmap *nrsm;
4645
4646 nrsm = bbr_alloc_full_limit(bbr);
4647 if (nrsm == NULL) {
4648 /*
4649 * We can't get memory to split, we can either just
4650 * not split it. Or retransmit the whole piece, lets
4651 * do the large send (BTLP :-) ).
4652 */
4653 goto go_for_it;
4654 }
4655 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg));
4656 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
4657 if (rsm->r_in_tmap) {
4658 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
4659 nrsm->r_in_tmap = 1;
4660 }
4661 rsm->r_flags &= (~BBR_HAS_FIN);
4662 rsm = nrsm;
4663 }
4664 go_for_it:
4665 bbr->r_ctl.rc_tlp_send = rsm;
4666 bbr->rc_tlp_rtx_out = 1;
4667 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) {
4668 bbr->r_ctl.rc_tlp_seg_send_cnt++;
4669 tp->t_rxtshift++;
4670 } else {
4671 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
4672 bbr->r_ctl.rc_tlp_seg_send_cnt = 1;
4673 }
4674 send:
4675 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
4676 /*
4677 * Can't [re]/transmit a segment we have retransmitted the
4678 * max times. We need the retransmit timer to take over.
4679 */
4680 restore:
4681 bbr->rc_tlp_new_data = 0;
4682 bbr->r_ctl.rc_tlp_send = NULL;
4683 if (rsm)
4684 rsm->r_flags &= ~BBR_TLP;
4685 BBR_STAT_INC(bbr_tlp_retran_fail);
4686 return (0);
4687 } else if (rsm) {
4688 rsm->r_flags |= BBR_TLP;
4689 }
4690 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) &&
4691 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) {
4692 /*
4693 * We have retransmitted to many times for TLP. Switch to
4694 * the regular RTO timer
4695 */
4696 goto restore;
4697 }
4698 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP);
4699 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
4700 return (0);
4701 }
4702
4703 /*
4704 * Delayed ack Timer, here we simply need to setup the
4705 * ACK_NOW flag and remove the DELACK flag. From there
4706 * the output routine will send the ack out.
4707 *
4708 * We only return 1, saying don't proceed, if all timers
4709 * are stopped (destroyed PCB?).
4710 */
4711 static int
bbr_timeout_delack(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4712 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4713 {
4714 if (bbr->rc_all_timers_stopped) {
4715 return (1);
4716 }
4717 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK);
4718 tp->t_flags &= ~TF_DELACK;
4719 tp->t_flags |= TF_ACKNOW;
4720 KMOD_TCPSTAT_INC(tcps_delack);
4721 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
4722 return (0);
4723 }
4724
4725 /*
4726 * Here we send a KEEP-ALIVE like probe to the
4727 * peer, we do not send data.
4728 *
4729 * We only return 1, saying don't proceed, if all timers
4730 * are stopped (destroyed PCB?).
4731 */
4732 static int
bbr_timeout_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4733 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4734 {
4735 struct tcptemp *t_template;
4736 int32_t retval = 1;
4737
4738 if (bbr->rc_all_timers_stopped) {
4739 return (1);
4740 }
4741 if (bbr->rc_in_persist == 0)
4742 return (0);
4743
4744 /*
4745 * Persistence timer into zero window. Force a byte to be output, if
4746 * possible.
4747 */
4748 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST);
4749 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
4750 KMOD_TCPSTAT_INC(tcps_persisttimeo);
4751 /*
4752 * Have we exceeded the user specified progress time?
4753 */
4754 if (ctf_progress_timeout_check(tp, true)) {
4755 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4756 return (-ETIMEDOUT); /* tcp_drop() */
4757 }
4758 /*
4759 * Hack: if the peer is dead/unreachable, we do not time out if the
4760 * window is closed. After a full backoff, drop the connection if
4761 * the idle time (no responses to probes) reaches the maximum
4762 * backoff that we would use if retransmitting.
4763 */
4764 if (tp->t_rxtshift >= V_tcp_retries &&
4765 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4766 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
4767 KMOD_TCPSTAT_INC(tcps_persistdrop);
4768 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4769 return (-ETIMEDOUT); /* tcp_drop() */
4770 }
4771 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) &&
4772 tp->snd_una == tp->snd_max) {
4773 bbr_exit_persist(tp, bbr, cts, __LINE__);
4774 retval = 0;
4775 goto out;
4776 }
4777 /*
4778 * If the user has closed the socket then drop a persisting
4779 * connection after a much reduced timeout.
4780 */
4781 if (tp->t_state > TCPS_CLOSE_WAIT &&
4782 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
4783 KMOD_TCPSTAT_INC(tcps_persistdrop);
4784 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4785 return (-ETIMEDOUT); /* tcp_drop() */
4786 }
4787 t_template = tcpip_maketemplate(bbr->rc_inp);
4788 if (t_template) {
4789 tcp_respond(tp, t_template->tt_ipgen,
4790 &t_template->tt_t, (struct mbuf *)NULL,
4791 tp->rcv_nxt, tp->snd_una - 1, 0);
4792 /* This sends an ack */
4793 if (tp->t_flags & TF_DELACK)
4794 tp->t_flags &= ~TF_DELACK;
4795 free(t_template, M_TEMP);
4796 }
4797 if (tp->t_rxtshift < V_tcp_retries)
4798 tp->t_rxtshift++;
4799 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0);
4800 out:
4801 return (retval);
4802 }
4803
4804 /*
4805 * If a keepalive goes off, we had no other timers
4806 * happening. We always return 1 here since this
4807 * routine either drops the connection or sends
4808 * out a segment with respond.
4809 */
4810 static int
bbr_timeout_keepalive(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4811 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4812 {
4813 struct tcptemp *t_template;
4814 struct inpcb *inp = tptoinpcb(tp);
4815
4816 if (bbr->rc_all_timers_stopped) {
4817 return (1);
4818 }
4819 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
4820 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP);
4821 /*
4822 * Keep-alive timer went off; send something or drop connection if
4823 * idle for too long.
4824 */
4825 KMOD_TCPSTAT_INC(tcps_keeptimeo);
4826 if (tp->t_state < TCPS_ESTABLISHED)
4827 goto dropit;
4828 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4829 tp->t_state <= TCPS_CLOSING) {
4830 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4831 goto dropit;
4832 /*
4833 * Send a packet designed to force a response if the peer is
4834 * up and reachable: either an ACK if the connection is
4835 * still alive, or an RST if the peer has closed the
4836 * connection due to timeout or reboot. Using sequence
4837 * number tp->snd_una-1 causes the transmitted zero-length
4838 * segment to lie outside the receive window; by the
4839 * protocol spec, this requires the correspondent TCP to
4840 * respond.
4841 */
4842 KMOD_TCPSTAT_INC(tcps_keepprobe);
4843 t_template = tcpip_maketemplate(inp);
4844 if (t_template) {
4845 tcp_respond(tp, t_template->tt_ipgen,
4846 &t_template->tt_t, (struct mbuf *)NULL,
4847 tp->rcv_nxt, tp->snd_una - 1, 0);
4848 free(t_template, M_TEMP);
4849 }
4850 }
4851 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0);
4852 return (1);
4853 dropit:
4854 KMOD_TCPSTAT_INC(tcps_keepdrops);
4855 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
4856 return (-ETIMEDOUT); /* tcp_drop() */
4857 }
4858
4859 /*
4860 * Retransmit helper function, clear up all the ack
4861 * flags and take care of important book keeping.
4862 */
4863 static void
bbr_remxt_tmr(struct tcpcb * tp)4864 bbr_remxt_tmr(struct tcpcb *tp)
4865 {
4866 /*
4867 * The retransmit timer went off, all sack'd blocks must be
4868 * un-acked.
4869 */
4870 struct bbr_sendmap *rsm, *trsm = NULL;
4871 struct tcp_bbr *bbr;
4872 uint32_t cts, lost;
4873
4874 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
4875 cts = tcp_get_usecs(&bbr->rc_tv);
4876 lost = bbr->r_ctl.rc_lost;
4877 if (bbr->r_state && (bbr->r_state != tp->t_state))
4878 bbr_set_state(tp, bbr, 0);
4879
4880 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
4881 if (rsm->r_flags & BBR_ACKED) {
4882 uint32_t old_flags;
4883
4884 rsm->r_dupack = 0;
4885 if (rsm->r_in_tmap == 0) {
4886 /* We must re-add it back to the tlist */
4887 if (trsm == NULL) {
4888 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
4889 } else {
4890 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext);
4891 }
4892 rsm->r_in_tmap = 1;
4893 }
4894 old_flags = rsm->r_flags;
4895 rsm->r_flags |= BBR_RXT_CLEARED;
4896 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS);
4897 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
4898 } else {
4899 if ((tp->t_state < TCPS_ESTABLISHED) &&
4900 (rsm->r_start == tp->snd_una)) {
4901 /*
4902 * Special case for TCP FO. Where
4903 * we sent more data beyond the snd_max.
4904 * We don't mark that as lost and stop here.
4905 */
4906 break;
4907 }
4908 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4909 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4910 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4911 }
4912 if (bbr_marks_rxt_sack_passed) {
4913 /*
4914 * With this option, we will rack out
4915 * in 1ms increments the rest of the packets.
4916 */
4917 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST;
4918 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4919 } else {
4920 /*
4921 * With this option we only mark them lost
4922 * and remove all sack'd markings. We will run
4923 * another RXT or a TLP. This will cause
4924 * us to eventually send more based on what
4925 * ack's come in.
4926 */
4927 rsm->r_flags |= BBR_MARKED_LOST;
4928 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4929 rsm->r_flags &= ~BBR_SACK_PASSED;
4930 }
4931 }
4932 trsm = rsm;
4933 }
4934 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4935 /* Clear the count (we just un-acked them) */
4936 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR);
4937 bbr->rc_tlp_new_data = 0;
4938 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4939 /* zap the behindness on a rxt */
4940 bbr->r_ctl.rc_hptsi_agg_delay = 0;
4941 bbr->r_agg_early_set = 0;
4942 bbr->r_ctl.rc_agg_early = 0;
4943 bbr->rc_tlp_rtx_out = 0;
4944 bbr->r_ctl.rc_sacked = 0;
4945 bbr->r_ctl.rc_sacklast = NULL;
4946 bbr->r_timer_override = 1;
4947 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4948 }
4949
4950 /*
4951 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
4952 * we will setup to retransmit the lowest seq number outstanding.
4953 */
4954 static int
bbr_timeout_rxt(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)4955 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4956 {
4957 struct inpcb *inp = tptoinpcb(tp);
4958 int32_t rexmt;
4959 int32_t retval = 0;
4960 bool isipv6;
4961
4962 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
4963 if (bbr->rc_all_timers_stopped) {
4964 return (1);
4965 }
4966 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
4967 (tp->snd_una == tp->snd_max)) {
4968 /* Nothing outstanding .. nothing to do */
4969 return (0);
4970 }
4971 /*
4972 * Retransmission timer went off. Message has not been acked within
4973 * retransmit interval. Back off to a longer retransmit interval
4974 * and retransmit one segment.
4975 */
4976 if (ctf_progress_timeout_check(tp, true)) {
4977 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4978 return (-ETIMEDOUT); /* tcp_drop() */
4979 }
4980 bbr_remxt_tmr(tp);
4981 if ((bbr->r_ctl.rc_resend == NULL) ||
4982 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) {
4983 /*
4984 * If the rwnd collapsed on
4985 * the one we are retransmitting
4986 * it does not count against the
4987 * rxt count.
4988 */
4989 tp->t_rxtshift++;
4990 }
4991 if (tp->t_rxtshift > V_tcp_retries) {
4992 tp->t_rxtshift = V_tcp_retries;
4993 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
4994 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
4995 /* XXXGL: previously t_softerror was casted to uint16_t */
4996 MPASS(tp->t_softerror >= 0);
4997 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
4998 return (retval); /* tcp_drop() */
4999 }
5000 if (tp->t_state == TCPS_SYN_SENT) {
5001 /*
5002 * If the SYN was retransmitted, indicate CWND to be limited
5003 * to 1 segment in cc_conn_init().
5004 */
5005 tp->snd_cwnd = 1;
5006 } else if (tp->t_rxtshift == 1) {
5007 /*
5008 * first retransmit; record ssthresh and cwnd so they can be
5009 * recovered if this turns out to be a "bad" retransmit. A
5010 * retransmit is considered "bad" if an ACK for this segment
5011 * is received within RTT/2 interval; the assumption here is
5012 * that the ACK was already in flight. See "On Estimating
5013 * End-to-End Network Path Properties" by Allman and Paxson
5014 * for more details.
5015 */
5016 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5017 if (!IN_RECOVERY(tp->t_flags)) {
5018 tp->snd_cwnd_prev = tp->snd_cwnd;
5019 tp->snd_ssthresh_prev = tp->snd_ssthresh;
5020 tp->snd_recover_prev = tp->snd_recover;
5021 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
5022 tp->t_flags |= TF_PREVVALID;
5023 } else {
5024 tp->t_flags &= ~TF_PREVVALID;
5025 }
5026 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5027 } else {
5028 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5029 tp->t_flags &= ~TF_PREVVALID;
5030 }
5031 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
5032 if ((tp->t_state == TCPS_SYN_SENT) ||
5033 (tp->t_state == TCPS_SYN_RECEIVED))
5034 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift];
5035 else
5036 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
5037 TCPT_RANGESET(tp->t_rxtcur, rexmt,
5038 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms),
5039 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
5040 /*
5041 * We enter the path for PLMTUD if connection is established or, if
5042 * connection is FIN_WAIT_1 status, reason for the last is that if
5043 * amount of data we send is very small, we could send it in couple
5044 * of packets and process straight to FIN. In that case we won't
5045 * catch ESTABLISHED state.
5046 */
5047 #ifdef INET6
5048 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
5049 #else
5050 isipv6 = false;
5051 #endif
5052 if (((V_tcp_pmtud_blackhole_detect == 1) ||
5053 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
5054 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
5055 ((tp->t_state == TCPS_ESTABLISHED) ||
5056 (tp->t_state == TCPS_FIN_WAIT_1))) {
5057 /*
5058 * Idea here is that at each stage of mtu probe (usually,
5059 * 1448 -> 1188 -> 524) should be given 2 chances to recover
5060 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
5061 * should take care of that.
5062 */
5063 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
5064 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
5065 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
5066 tp->t_rxtshift % 2 == 0)) {
5067 /*
5068 * Enter Path MTU Black-hole Detection mechanism: -
5069 * Disable Path MTU Discovery (IP "DF" bit). -
5070 * Reduce MTU to lower value than what we negotiated
5071 * with peer.
5072 */
5073 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
5074 /*
5075 * Record that we may have found a black
5076 * hole.
5077 */
5078 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
5079 /* Keep track of previous MSS. */
5080 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
5081 }
5082 /*
5083 * Reduce the MSS to blackhole value or to the
5084 * default in an attempt to retransmit.
5085 */
5086 #ifdef INET6
5087 isipv6 = bbr->r_is_v6;
5088 if (isipv6 &&
5089 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
5090 /* Use the sysctl tuneable blackhole MSS. */
5091 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
5092 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5093 } else if (isipv6) {
5094 /* Use the default MSS. */
5095 tp->t_maxseg = V_tcp_v6mssdflt;
5096 /*
5097 * Disable Path MTU Discovery when we switch
5098 * to minmss.
5099 */
5100 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5101 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5102 }
5103 #endif
5104 #if defined(INET6) && defined(INET)
5105 else
5106 #endif
5107 #ifdef INET
5108 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
5109 /* Use the sysctl tuneable blackhole MSS. */
5110 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
5111 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5112 } else {
5113 /* Use the default MSS. */
5114 tp->t_maxseg = V_tcp_mssdflt;
5115 /*
5116 * Disable Path MTU Discovery when we switch
5117 * to minmss.
5118 */
5119 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5120 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5121 }
5122 #endif
5123 } else {
5124 /*
5125 * If further retransmissions are still unsuccessful
5126 * with a lowered MTU, maybe this isn't a blackhole
5127 * and we restore the previous MSS and blackhole
5128 * detection flags. The limit '6' is determined by
5129 * giving each probe stage (1448, 1188, 524) 2
5130 * chances to recover.
5131 */
5132 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
5133 (tp->t_rxtshift >= 6)) {
5134 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
5135 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
5136 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
5137 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5138 }
5139 }
5140 }
5141 /*
5142 * Disable RFC1323 and SACK if we haven't got any response to our
5143 * third SYN to work-around some broken terminal servers (most of
5144 * which have hopefully been retired) that have bad VJ header
5145 * compression code which trashes TCP segments containing
5146 * unknown-to-them TCP options.
5147 */
5148 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
5149 (tp->t_rxtshift == 3))
5150 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
5151 /*
5152 * If we backed off this far, our srtt estimate is probably bogus.
5153 * Clobber it so we'll take the next rtt measurement as our srtt;
5154 * move the current srtt into rttvar to keep the current retransmit
5155 * times until then.
5156 */
5157 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5158 #ifdef INET6
5159 if (bbr->r_is_v6)
5160 in6_losing(inp);
5161 else
5162 #endif
5163 in_losing(inp);
5164 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5165 tp->t_srtt = 0;
5166 }
5167 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
5168 tp->snd_recover = tp->snd_max;
5169 tp->t_flags |= TF_ACKNOW;
5170 tp->t_rtttime = 0;
5171
5172 return (retval);
5173 }
5174
5175 static int
bbr_process_timers(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,uint8_t hpts_calling)5176 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling)
5177 {
5178 int32_t ret = 0;
5179 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5180
5181 if (timers == 0) {
5182 return (0);
5183 }
5184 if (tp->t_state == TCPS_LISTEN) {
5185 /* no timers on listen sockets */
5186 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5187 return (0);
5188 return (1);
5189 }
5190 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
5191 uint32_t left;
5192
5193 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5194 ret = -1;
5195 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5196 return (0);
5197 }
5198 if (hpts_calling == 0) {
5199 ret = -2;
5200 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5201 return (0);
5202 }
5203 /*
5204 * Ok our timer went off early and we are not paced false
5205 * alarm, go back to sleep.
5206 */
5207 left = bbr->r_ctl.rc_timer_exp - cts;
5208 ret = -3;
5209 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling);
5210 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left));
5211 return (1);
5212 }
5213 bbr->rc_tmr_stopped = 0;
5214 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5215 if (timers & PACE_TMR_DELACK) {
5216 ret = bbr_timeout_delack(tp, bbr, cts);
5217 } else if (timers & PACE_TMR_PERSIT) {
5218 ret = bbr_timeout_persist(tp, bbr, cts);
5219 } else if (timers & PACE_TMR_RACK) {
5220 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5221 ret = bbr_timeout_rack(tp, bbr, cts);
5222 } else if (timers & PACE_TMR_TLP) {
5223 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5224 ret = bbr_timeout_tlp(tp, bbr, cts);
5225 } else if (timers & PACE_TMR_RXT) {
5226 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5227 ret = bbr_timeout_rxt(tp, bbr, cts);
5228 } else if (timers & PACE_TMR_KEEP) {
5229 ret = bbr_timeout_keepalive(tp, bbr, cts);
5230 }
5231 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling);
5232 return (ret);
5233 }
5234
5235 static void
bbr_timer_cancel(struct tcp_bbr * bbr,int32_t line,uint32_t cts)5236 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts)
5237 {
5238 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5239 uint8_t hpts_removed = 0;
5240
5241 if (tcp_in_hpts(bbr->rc_tp) &&
5242 (bbr->rc_timer_first == 1)) {
5243 /*
5244 * If we are canceling timer's when we have the
5245 * timer ahead of the output being paced. We also
5246 * must remove ourselves from the hpts.
5247 */
5248 hpts_removed = 1;
5249 tcp_hpts_remove(bbr->rc_tp);
5250 if (bbr->r_ctl.rc_last_delay_val) {
5251 /* Update the last hptsi delay too */
5252 uint32_t time_since_send;
5253
5254 if (TSTMP_GT(cts, bbr->rc_pacer_started))
5255 time_since_send = cts - bbr->rc_pacer_started;
5256 else
5257 time_since_send = 0;
5258 if (bbr->r_ctl.rc_last_delay_val > time_since_send) {
5259 /* Cut down our slot time */
5260 bbr->r_ctl.rc_last_delay_val -= time_since_send;
5261 } else {
5262 bbr->r_ctl.rc_last_delay_val = 0;
5263 }
5264 bbr->rc_pacer_started = cts;
5265 }
5266 }
5267 bbr->rc_timer_first = 0;
5268 bbr_log_to_cancel(bbr, line, cts, hpts_removed);
5269 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5270 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5271 }
5272 }
5273
5274 static int
bbr_stopall(struct tcpcb * tp)5275 bbr_stopall(struct tcpcb *tp)
5276 {
5277 struct tcp_bbr *bbr;
5278
5279 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
5280 bbr->rc_all_timers_stopped = 1;
5281
5282 tcp_hpts_remove(tp);
5283
5284 return (0);
5285 }
5286
5287 static uint32_t
bbr_get_earliest_send_outstanding(struct tcp_bbr * bbr,struct bbr_sendmap * u_rsm,uint32_t cts)5288 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts)
5289 {
5290 struct bbr_sendmap *rsm;
5291
5292 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
5293 if ((rsm == NULL) || (u_rsm == rsm))
5294 return (cts);
5295 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
5296 }
5297
5298 static void
bbr_update_rsm(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts,uint32_t pacing_time)5299 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr,
5300 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time)
5301 {
5302 int32_t idx;
5303
5304 rsm->r_rtr_cnt++;
5305 rsm->r_dupack = 0;
5306 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) {
5307 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS;
5308 rsm->r_flags |= BBR_OVERMAX;
5309 }
5310 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
5311 /* Take off the collapsed flag at rxt */
5312 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
5313 }
5314 if (rsm->r_flags & BBR_MARKED_LOST) {
5315 /* We have retransmitted, its no longer lost */
5316 rsm->r_flags &= ~BBR_MARKED_LOST;
5317 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
5318 }
5319 if (rsm->r_flags & BBR_RXT_CLEARED) {
5320 /*
5321 * We hit a RXT timer on it and
5322 * we cleared the "acked" flag.
5323 * We now have it going back into
5324 * flight, we can remove the cleared
5325 * flag and possibly do accounting on
5326 * this piece.
5327 */
5328 rsm->r_flags &= ~BBR_RXT_CLEARED;
5329 }
5330 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) {
5331 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5332 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5333 }
5334 idx = rsm->r_rtr_cnt - 1;
5335 rsm->r_tim_lastsent[idx] = cts;
5336 rsm->r_pacing_delay = pacing_time;
5337 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5338 rsm->r_ts_valid = bbr->rc_ts_valid;
5339 if (bbr->rc_ts_valid)
5340 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5341 if (bbr->r_ctl.r_app_limited_until)
5342 rsm->r_app_limited = 1;
5343 else
5344 rsm->r_app_limited = 0;
5345 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5346 rsm->r_bbr_state = bbr_state_val(bbr);
5347 else
5348 rsm->r_bbr_state = 8;
5349 if (rsm->r_flags & BBR_ACKED) {
5350 /* Problably MTU discovery messing with us */
5351 uint32_t old_flags;
5352
5353 old_flags = rsm->r_flags;
5354 rsm->r_flags &= ~BBR_ACKED;
5355 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
5356 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5357 if (bbr->r_ctl.rc_sacked == 0)
5358 bbr->r_ctl.rc_sacklast = NULL;
5359 }
5360 if (rsm->r_in_tmap) {
5361 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5362 }
5363 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5364 rsm->r_in_tmap = 1;
5365 if (rsm->r_flags & BBR_SACK_PASSED) {
5366 /* We have retransmitted due to the SACK pass */
5367 rsm->r_flags &= ~BBR_SACK_PASSED;
5368 rsm->r_flags |= BBR_WAS_SACKPASS;
5369 }
5370 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5371 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5372 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5373 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
5374 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5375 rsm->r_is_gain = 1;
5376 rsm->r_is_drain = 0;
5377 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5378 rsm->r_is_drain = 1;
5379 rsm->r_is_gain = 0;
5380 } else {
5381 rsm->r_is_drain = 0;
5382 rsm->r_is_gain = 0;
5383 }
5384 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */
5385 }
5386
5387 /*
5388 * Returns 0, or the sequence where we stopped
5389 * updating. We also update the lenp to be the amount
5390 * of data left.
5391 */
5392
5393 static uint32_t
bbr_update_entry(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts,int32_t * lenp,uint32_t pacing_time)5394 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr,
5395 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time)
5396 {
5397 /*
5398 * We (re-)transmitted starting at rsm->r_start for some length
5399 * (possibly less than r_end.
5400 */
5401 struct bbr_sendmap *nrsm;
5402 uint32_t c_end;
5403 int32_t len;
5404
5405 len = *lenp;
5406 c_end = rsm->r_start + len;
5407 if (SEQ_GEQ(c_end, rsm->r_end)) {
5408 /*
5409 * We retransmitted the whole piece or more than the whole
5410 * slopping into the next rsm.
5411 */
5412 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5413 if (c_end == rsm->r_end) {
5414 *lenp = 0;
5415 return (0);
5416 } else {
5417 int32_t act_len;
5418
5419 /* Hangs over the end return whats left */
5420 act_len = rsm->r_end - rsm->r_start;
5421 *lenp = (len - act_len);
5422 return (rsm->r_end);
5423 }
5424 /* We don't get out of this block. */
5425 }
5426 /*
5427 * Here we retransmitted less than the whole thing which means we
5428 * have to split this into what was transmitted and what was not.
5429 */
5430 nrsm = bbr_alloc_full_limit(bbr);
5431 if (nrsm == NULL) {
5432 *lenp = 0;
5433 return (0);
5434 }
5435 /*
5436 * So here we are going to take the original rsm and make it what we
5437 * retransmitted. nrsm will be the tail portion we did not
5438 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
5439 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
5440 * 1, 6 and the new piece will be 6, 11.
5441 */
5442 bbr_clone_rsm(bbr, nrsm, rsm, c_end);
5443 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
5444 nrsm->r_dupack = 0;
5445 if (rsm->r_in_tmap) {
5446 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5447 nrsm->r_in_tmap = 1;
5448 }
5449 rsm->r_flags &= (~BBR_HAS_FIN);
5450 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5451 *lenp = 0;
5452 return (0);
5453 }
5454
5455 static uint64_t
bbr_get_hardware_rate(struct tcp_bbr * bbr)5456 bbr_get_hardware_rate(struct tcp_bbr *bbr)
5457 {
5458 uint64_t bw;
5459
5460 bw = bbr_get_bw(bbr);
5461 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN];
5462 bw /= (uint64_t)BBR_UNIT;
5463 return(bw);
5464 }
5465
5466 static void
bbr_setup_less_of_rate(struct tcp_bbr * bbr,uint32_t cts,uint64_t act_rate,uint64_t rate_wanted)5467 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts,
5468 uint64_t act_rate, uint64_t rate_wanted)
5469 {
5470 /*
5471 * We could not get a full gains worth
5472 * of rate.
5473 */
5474 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) {
5475 /* we can't even get the real rate */
5476 uint64_t red;
5477
5478 bbr->skip_gain = 1;
5479 bbr->gain_is_limited = 0;
5480 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate;
5481 if (red)
5482 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts);
5483 } else {
5484 /* We can use a lower gain */
5485 bbr->skip_gain = 0;
5486 bbr->gain_is_limited = 1;
5487 }
5488 }
5489
5490 static void
bbr_update_hardware_pacing_rate(struct tcp_bbr * bbr,uint32_t cts)5491 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts)
5492 {
5493 const struct tcp_hwrate_limit_table *nrte;
5494 int error, rate = -1;
5495
5496 if (bbr->r_ctl.crte == NULL)
5497 return;
5498 if ((bbr->rc_inp->inp_route.ro_nh == NULL) ||
5499 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) {
5500 /* Lost our routes? */
5501 /* Clear the way for a re-attempt */
5502 bbr->bbr_attempt_hdwr_pace = 0;
5503 lost_rate:
5504 bbr->gain_is_limited = 0;
5505 bbr->skip_gain = 0;
5506 bbr->bbr_hdrw_pacing = 0;
5507 counter_u64_add(bbr_flows_whdwr_pacing, -1);
5508 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
5509 tcp_bbr_tso_size_check(bbr, cts);
5510 return;
5511 }
5512 rate = bbr_get_hardware_rate(bbr);
5513 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte,
5514 bbr->rc_tp,
5515 bbr->rc_inp->inp_route.ro_nh->nh_ifp,
5516 rate,
5517 (RS_PACING_GEQ|RS_PACING_SUB_OK),
5518 &error, NULL);
5519 if (nrte == NULL) {
5520 goto lost_rate;
5521 }
5522 if (nrte != bbr->r_ctl.crte) {
5523 bbr->r_ctl.crte = nrte;
5524 if (error == 0) {
5525 BBR_STAT_INC(bbr_hdwr_rl_mod_ok);
5526 if (bbr->r_ctl.crte->rate < rate) {
5527 /* We have a problem */
5528 bbr_setup_less_of_rate(bbr, cts,
5529 bbr->r_ctl.crte->rate, rate);
5530 } else {
5531 /* We are good */
5532 bbr->gain_is_limited = 0;
5533 bbr->skip_gain = 0;
5534 }
5535 } else {
5536 /* A failure should release the tag */
5537 BBR_STAT_INC(bbr_hdwr_rl_mod_fail);
5538 bbr->gain_is_limited = 0;
5539 bbr->skip_gain = 0;
5540 bbr->bbr_hdrw_pacing = 0;
5541 }
5542 bbr_type_log_hdwr_pacing(bbr,
5543 bbr->r_ctl.crte->ptbl->rs_ifp,
5544 rate,
5545 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate),
5546 __LINE__,
5547 cts,
5548 error);
5549 }
5550 }
5551
5552 static void
bbr_adjust_for_hw_pacing(struct tcp_bbr * bbr,uint32_t cts)5553 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts)
5554 {
5555 /*
5556 * If we have hardware pacing support
5557 * we need to factor that in for our
5558 * TSO size.
5559 */
5560 const struct tcp_hwrate_limit_table *rlp;
5561 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay;
5562
5563 if ((bbr->bbr_hdrw_pacing == 0) ||
5564 (IN_RECOVERY(bbr->rc_tp->t_flags)) ||
5565 (bbr->r_ctl.crte == NULL))
5566 return;
5567 if (bbr->hw_pacing_set == 0) {
5568 /* Not yet by the hdwr pacing count delay */
5569 return;
5570 }
5571 if (bbr_hdwr_pace_adjust == 0) {
5572 /* No adjustment */
5573 return;
5574 }
5575 rlp = bbr->r_ctl.crte;
5576 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options)
5577 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5578 else
5579 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5580 /*
5581 * So lets first get the
5582 * time we will take between
5583 * TSO sized sends currently without
5584 * hardware help.
5585 */
5586 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT,
5587 bbr->r_ctl.rc_pace_max_segs, cts, 1);
5588 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg;
5589 hdwr_delay *= rlp->time_between;
5590 if (cur_delay > hdwr_delay)
5591 delta = cur_delay - hdwr_delay;
5592 else
5593 delta = 0;
5594 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay,
5595 (bbr->r_ctl.rc_pace_max_segs / maxseg),
5596 1);
5597 if (delta &&
5598 (delta < (max(rlp->time_between,
5599 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) {
5600 /*
5601 * Now lets divide by the pacing
5602 * time between each segment the
5603 * hardware sends rounding up and
5604 * derive a bytes from that. We multiply
5605 * that by bbr_hdwr_pace_adjust to get
5606 * more bang for our buck.
5607 *
5608 * The goal is to have the software pacer
5609 * waiting no more than an additional
5610 * pacing delay if we can (without the
5611 * compensation i.e. x bbr_hdwr_pace_adjust).
5612 */
5613 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between),
5614 (bbr->r_ctl.rc_pace_max_segs/maxseg));
5615 seg_sz *= bbr_hdwr_pace_adjust;
5616 if (bbr_hdwr_pace_floor &&
5617 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5618 /* Currently hardware paces
5619 * out rs_min_seg segments at a time.
5620 * We need to make sure we always send at least
5621 * a full burst of bbr_hdwr_pace_floor down.
5622 */
5623 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5624 }
5625 seg_sz *= maxseg;
5626 } else if (delta == 0) {
5627 /*
5628 * The highest pacing rate is
5629 * above our b/w gained. This means
5630 * we probably are going quite fast at
5631 * the hardware highest rate. Lets just multiply
5632 * the calculated TSO size by the
5633 * multiplier factor (its probably
5634 * 4 segments in the default config for
5635 * mlx).
5636 */
5637 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust;
5638 if (bbr_hdwr_pace_floor &&
5639 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5640 /* Currently hardware paces
5641 * out rs_min_seg segments at a time.
5642 * We need to make sure we always send at least
5643 * a full burst of bbr_hdwr_pace_floor down.
5644 */
5645 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5646 }
5647 } else {
5648 /*
5649 * The pacing time difference is so
5650 * big that the hardware will
5651 * pace out more rapidly then we
5652 * really want and then we
5653 * will have a long delay. Lets just keep
5654 * the same TSO size so its as if
5655 * we were not using hdwr pacing (we
5656 * just gain a bit of spacing from the
5657 * hardware if seg_sz > 1).
5658 */
5659 seg_sz = bbr->r_ctl.rc_pace_max_segs;
5660 }
5661 if (seg_sz > bbr->r_ctl.rc_pace_max_segs)
5662 new_tso = seg_sz;
5663 else
5664 new_tso = bbr->r_ctl.rc_pace_max_segs;
5665 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg))
5666 new_tso = PACE_MAX_IP_BYTES - maxseg;
5667
5668 if (new_tso != bbr->r_ctl.rc_pace_max_segs) {
5669 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0);
5670 bbr->r_ctl.rc_pace_max_segs = new_tso;
5671 }
5672 }
5673
5674 static void
tcp_bbr_tso_size_check(struct tcp_bbr * bbr,uint32_t cts)5675 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts)
5676 {
5677 uint64_t bw;
5678 uint32_t old_tso = 0, new_tso;
5679 uint32_t maxseg, bytes;
5680 uint32_t tls_seg=0;
5681 /*
5682 * Google/linux uses the following algorithm to determine
5683 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18):
5684 *
5685 * bytes = bw_in_bytes_per_second / 1000
5686 * bytes = min(bytes, 64k)
5687 * tso_segs = bytes / MSS
5688 * if (bw < 1.2Mbs)
5689 * min_tso_segs = 1
5690 * else
5691 * min_tso_segs = 2
5692 * tso_segs = max(tso_segs, min_tso_segs)
5693 *
5694 * * Note apply a device specific limit (we apply this in the
5695 * tcp_m_copym).
5696 * Note that before the initial measurement is made google bursts out
5697 * a full iwnd just like new-reno/cubic.
5698 *
5699 * We do not use this algorithm. Instead we
5700 * use a two phased approach:
5701 *
5702 * if ( bw <= per-tcb-cross-over)
5703 * goal_tso = calculate how much with this bw we
5704 * can send in goal-time seconds.
5705 * if (goal_tso > mss)
5706 * seg = goal_tso / mss
5707 * tso = seg * mss
5708 * else
5709 * tso = mss
5710 * if (tso > per-tcb-max)
5711 * tso = per-tcb-max
5712 * else if ( bw > 512Mbps)
5713 * tso = max-tso (64k/mss)
5714 * else
5715 * goal_tso = bw / per-tcb-divsor
5716 * seg = (goal_tso + mss-1)/mss
5717 * tso = seg * mss
5718 *
5719 * if (tso < per-tcb-floor)
5720 * tso = per-tcb-floor
5721 * if (tso > per-tcb-utter_max)
5722 * tso = per-tcb-utter_max
5723 *
5724 * Note the default per-tcb-divisor is 1000 (same as google).
5725 * the goal cross over is 30Mbps however. To recreate googles
5726 * algorithm you need to set:
5727 *
5728 * cross-over = 23,168,000 bps
5729 * goal-time = 18000
5730 * per-tcb-max = 2
5731 * per-tcb-divisor = 1000
5732 * per-tcb-floor = 1
5733 *
5734 * This will get you "google bbr" behavior with respect to tso size.
5735 *
5736 * Note we do set anything TSO size until we are past the initial
5737 * window. Before that we gnerally use either a single MSS
5738 * or we use the full IW size (so we burst a IW at a time)
5739 */
5740
5741 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) {
5742 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5743 } else {
5744 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5745 }
5746 old_tso = bbr->r_ctl.rc_pace_max_segs;
5747 if (bbr->rc_past_init_win == 0) {
5748 /*
5749 * Not enough data has been acknowledged to make a
5750 * judgement. Set up the initial TSO based on if we
5751 * are sending a full IW at once or not.
5752 */
5753 if (bbr->rc_use_google)
5754 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2);
5755 else if (bbr->bbr_init_win_cheat)
5756 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp);
5757 else
5758 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5759 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg)
5760 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg;
5761 if (bbr->r_ctl.rc_pace_max_segs == 0) {
5762 bbr->r_ctl.rc_pace_max_segs = maxseg;
5763 }
5764 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0);
5765 bbr_adjust_for_hw_pacing(bbr, cts);
5766 return;
5767 }
5768 /**
5769 * Now lets set the TSO goal based on our delivery rate in
5770 * bytes per second. Note we only do this if
5771 * we have acked at least the initial cwnd worth of data.
5772 */
5773 bw = bbr_get_bw(bbr);
5774 if (IN_RECOVERY(bbr->rc_tp->t_flags) &&
5775 (bbr->rc_use_google == 0)) {
5776 /* We clamp to one MSS in recovery */
5777 new_tso = maxseg;
5778 } else if (bbr->rc_use_google) {
5779 int min_tso_segs;
5780
5781 /* Google considers the gain too */
5782 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) {
5783 bw *= bbr->r_ctl.rc_bbr_hptsi_gain;
5784 bw /= BBR_UNIT;
5785 }
5786 bytes = bw / 1024;
5787 if (bytes > (64 * 1024))
5788 bytes = 64 * 1024;
5789 new_tso = bytes / maxseg;
5790 if (bw < ONE_POINT_TWO_MEG)
5791 min_tso_segs = 1;
5792 else
5793 min_tso_segs = 2;
5794 if (new_tso < min_tso_segs)
5795 new_tso = min_tso_segs;
5796 new_tso *= maxseg;
5797 } else if (bbr->rc_no_pacing) {
5798 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg;
5799 } else if (bw <= bbr->r_ctl.bbr_cross_over) {
5800 /*
5801 * Calculate the worse case b/w TSO if we are inserting no
5802 * more than a delay_target number of TSO's.
5803 */
5804 uint32_t tso_len, min_tso;
5805
5806 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw);
5807 if (tso_len > maxseg) {
5808 new_tso = tso_len / maxseg;
5809 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max)
5810 new_tso = bbr->r_ctl.bbr_hptsi_segments_max;
5811 new_tso *= maxseg;
5812 } else {
5813 /*
5814 * less than a full sized frame yikes.. long rtt or
5815 * low bw?
5816 */
5817 min_tso = bbr_minseg(bbr);
5818 if ((tso_len > min_tso) && (bbr_all_get_min == 0))
5819 new_tso = rounddown(tso_len, min_tso);
5820 else
5821 new_tso = min_tso;
5822 }
5823 } else if (bw > FIVETWELVE_MBPS) {
5824 /*
5825 * This guy is so fast b/w wise that we can TSO as large as
5826 * possible of segments that the NIC will allow.
5827 */
5828 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5829 } else {
5830 /*
5831 * This formula is based on attempting to send a segment or
5832 * more every bbr_hptsi_per_second. The default is 1000
5833 * which means you are targeting what you can send every 1ms
5834 * based on the peers bw.
5835 *
5836 * If the number drops to say 500, then you are looking more
5837 * at 2ms and you will raise how much we send in a single
5838 * TSO thus saving CPU (less bbr_output_wtime() calls). The
5839 * trade off of course is you will send more at once and
5840 * thus tend to clump up the sends into larger "bursts"
5841 * building a queue.
5842 */
5843 bw /= bbr->r_ctl.bbr_hptsi_per_second;
5844 new_tso = roundup(bw, (uint64_t)maxseg);
5845 /*
5846 * Gate the floor to match what our lower than 48Mbps
5847 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus
5848 * becomes the floor for this calculation.
5849 */
5850 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg))
5851 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg);
5852 }
5853 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor)))
5854 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor;
5855 if (new_tso > PACE_MAX_IP_BYTES)
5856 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5857 /* Enforce an utter maximum. */
5858 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) {
5859 new_tso = bbr->r_ctl.bbr_utter_max * maxseg;
5860 }
5861 if (old_tso != new_tso) {
5862 /* Only log changes */
5863 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0);
5864 bbr->r_ctl.rc_pace_max_segs = new_tso;
5865 }
5866 /* We have hardware pacing! */
5867 bbr_adjust_for_hw_pacing(bbr, cts);
5868 }
5869
5870 static void
bbr_log_output(struct tcp_bbr * bbr,struct tcpcb * tp,struct tcpopt * to,int32_t len,uint32_t seq_out,uint16_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)5871 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len,
5872 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts,
5873 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc,
5874 struct sockbuf *sb)
5875 {
5876
5877 struct bbr_sendmap *rsm, *nrsm;
5878 register uint32_t snd_max, snd_una;
5879 uint32_t pacing_time;
5880 /*
5881 * Add to the RACK log of packets in flight or retransmitted. If
5882 * there is a TS option we will use the TS echoed, if not we will
5883 * grab a TS.
5884 *
5885 * Retransmissions will increment the count and move the ts to its
5886 * proper place. Note that if options do not include TS's then we
5887 * won't be able to effectively use the ACK for an RTT on a retran.
5888 *
5889 * Notes about r_start and r_end. Lets consider a send starting at
5890 * sequence 1 for 10 bytes. In such an example the r_start would be
5891 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
5892 * This means that r_end is actually the first sequence for the next
5893 * slot (11).
5894 *
5895 */
5896 INP_WLOCK_ASSERT(tptoinpcb(tp));
5897 if (err) {
5898 /*
5899 * We don't log errors -- we could but snd_max does not
5900 * advance in this case either.
5901 */
5902 return;
5903 }
5904 if (th_flags & TH_RST) {
5905 /*
5906 * We don't log resets and we return immediately from
5907 * sending
5908 */
5909 *abandon = 1;
5910 return;
5911 }
5912 snd_una = tp->snd_una;
5913 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) {
5914 /*
5915 * The call to bbr_log_output is made before bumping
5916 * snd_max. This means we can record one extra byte on a SYN
5917 * or FIN if seq_out is adding more on and a FIN is present
5918 * (and we are not resending).
5919 */
5920 if ((th_flags & TH_SYN) && (tp->iss == seq_out))
5921 len++;
5922 if (th_flags & TH_FIN)
5923 len++;
5924 }
5925 if (SEQ_LEQ((seq_out + len), snd_una)) {
5926 /* Are sending an old segment to induce an ack (keep-alive)? */
5927 return;
5928 }
5929 if (SEQ_LT(seq_out, snd_una)) {
5930 /* huh? should we panic? */
5931 uint32_t end;
5932
5933 end = seq_out + len;
5934 seq_out = snd_una;
5935 len = end - seq_out;
5936 }
5937 snd_max = tp->snd_max;
5938 if (len == 0) {
5939 /* We don't log zero window probes */
5940 return;
5941 }
5942 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1);
5943 /* First question is it a retransmission? */
5944 if (seq_out == snd_max) {
5945 again:
5946 rsm = bbr_alloc(bbr);
5947 if (rsm == NULL) {
5948 return;
5949 }
5950 rsm->r_flags = 0;
5951 if (th_flags & TH_SYN)
5952 rsm->r_flags |= BBR_HAS_SYN;
5953 if (th_flags & TH_FIN)
5954 rsm->r_flags |= BBR_HAS_FIN;
5955 rsm->r_tim_lastsent[0] = cts;
5956 rsm->r_rtr_cnt = 1;
5957 rsm->r_rtr_bytes = 0;
5958 rsm->r_start = seq_out;
5959 rsm->r_end = rsm->r_start + len;
5960 rsm->r_dupack = 0;
5961 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5962 rsm->r_pacing_delay = pacing_time;
5963 rsm->r_ts_valid = bbr->rc_ts_valid;
5964 if (bbr->rc_ts_valid)
5965 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5966 rsm->r_del_time = bbr->r_ctl.rc_del_time;
5967 if (bbr->r_ctl.r_app_limited_until)
5968 rsm->r_app_limited = 1;
5969 else
5970 rsm->r_app_limited = 0;
5971 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5972 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5973 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5974 /*
5975 * Here we must also add in this rsm since snd_max
5976 * is updated after we return from a new send.
5977 */
5978 rsm->r_flight_at_send += len;
5979 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
5980 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5981 rsm->r_in_tmap = 1;
5982 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5983 rsm->r_bbr_state = bbr_state_val(bbr);
5984 else
5985 rsm->r_bbr_state = 8;
5986 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5987 rsm->r_is_gain = 1;
5988 rsm->r_is_drain = 0;
5989 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5990 rsm->r_is_drain = 1;
5991 rsm->r_is_gain = 0;
5992 } else {
5993 rsm->r_is_drain = 0;
5994 rsm->r_is_gain = 0;
5995 }
5996 return;
5997 }
5998 /*
5999 * If we reach here its a retransmission and we need to find it.
6000 */
6001 more:
6002 if (hintrsm && (hintrsm->r_start == seq_out)) {
6003 rsm = hintrsm;
6004 hintrsm = NULL;
6005 } else if (bbr->r_ctl.rc_next) {
6006 /* We have a hint from a previous run */
6007 rsm = bbr->r_ctl.rc_next;
6008 } else {
6009 /* No hints sorry */
6010 rsm = NULL;
6011 }
6012 if ((rsm) && (rsm->r_start == seq_out)) {
6013 /*
6014 * We used rc_next or hintrsm to retransmit, hopefully the
6015 * likely case.
6016 */
6017 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6018 if (len == 0) {
6019 return;
6020 } else {
6021 goto more;
6022 }
6023 }
6024 /* Ok it was not the last pointer go through it the hard way. */
6025 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6026 if (rsm->r_start == seq_out) {
6027 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6028 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
6029 if (len == 0) {
6030 return;
6031 } else {
6032 continue;
6033 }
6034 }
6035 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6036 /* Transmitted within this piece */
6037 /*
6038 * Ok we must split off the front and then let the
6039 * update do the rest
6040 */
6041 nrsm = bbr_alloc_full_limit(bbr);
6042 if (nrsm == NULL) {
6043 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
6044 return;
6045 }
6046 /*
6047 * copy rsm to nrsm and then trim the front of rsm
6048 * to not include this part.
6049 */
6050 bbr_clone_rsm(bbr, nrsm, rsm, seq_out);
6051 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
6052 if (rsm->r_in_tmap) {
6053 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6054 nrsm->r_in_tmap = 1;
6055 }
6056 rsm->r_flags &= (~BBR_HAS_FIN);
6057 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time);
6058 if (len == 0) {
6059 return;
6060 }
6061 }
6062 }
6063 /*
6064 * Hmm not found in map did they retransmit both old and on into the
6065 * new?
6066 */
6067 if (seq_out == tp->snd_max) {
6068 goto again;
6069 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6070 #ifdef BBR_INVARIANTS
6071 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6072 seq_out, len, tp->snd_una, tp->snd_max);
6073 printf("Starting Dump of all rack entries\n");
6074 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6075 printf("rsm:%p start:%u end:%u\n",
6076 rsm, rsm->r_start, rsm->r_end);
6077 }
6078 printf("Dump complete\n");
6079 panic("seq_out not found rack:%p tp:%p",
6080 bbr, tp);
6081 #endif
6082 } else {
6083 #ifdef BBR_INVARIANTS
6084 /*
6085 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6086 * flag)
6087 */
6088 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6089 seq_out, len, tp->snd_max, tp);
6090 #endif
6091 }
6092 }
6093
6094 static void
bbr_collapse_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,int32_t rtt)6095 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt)
6096 {
6097 /*
6098 * Collapse timeout back the cum-ack moved.
6099 */
6100 tp->t_rxtshift = 0;
6101 tp->t_softerror = 0;
6102 }
6103
6104 static void
tcp_bbr_xmit_timer(struct tcp_bbr * bbr,uint32_t rtt_usecs,uint32_t rsm_send_time,uint32_t r_start,uint32_t tsin)6105 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin)
6106 {
6107 bbr->rtt_valid = 1;
6108 bbr->r_ctl.cur_rtt = rtt_usecs;
6109 bbr->r_ctl.ts_in = tsin;
6110 if (rsm_send_time)
6111 bbr->r_ctl.cur_rtt_send_time = rsm_send_time;
6112 }
6113
6114 static void
bbr_make_timestamp_determination(struct tcp_bbr * bbr)6115 bbr_make_timestamp_determination(struct tcp_bbr *bbr)
6116 {
6117 /**
6118 * We have in our bbr control:
6119 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp).
6120 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts).
6121 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts)
6122 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time)
6123 *
6124 * Now we can calculate the time between the sends by doing:
6125 *
6126 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts
6127 *
6128 * And the peer's time between receiving them by doing:
6129 *
6130 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp
6131 *
6132 * We want to figure out if the timestamp values are in msec, 10msec or usec.
6133 * We also may find that we can't use the timestamps if say we see
6134 * that the peer_delta indicates that though we may have taken 10ms to
6135 * pace out the data, it only saw 1ms between the two packets. This would
6136 * indicate that somewhere on the path is a batching entity that is giving
6137 * out time-slices of the actual b/w. This would mean we could not use
6138 * reliably the peers timestamps.
6139 *
6140 * We expect delta > peer_delta initially. Until we figure out the
6141 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio.
6142 * If we place 1000 there then its a ms vs our usec. If we place 10000 there
6143 * then its 10ms vs our usec. If the peer is running a usec clock we would
6144 * put a 1 there. If the value is faster then ours, we will disable the
6145 * use of timestamps (though we could revist this later if we find it to be not
6146 * just an isolated one or two flows)).
6147 *
6148 * To detect the batching middle boxes we will come up with our compensation and
6149 * if with it in place, we find the peer is drastically off (by some margin) in
6150 * the smaller direction, then we will assume the worst case and disable use of timestamps.
6151 *
6152 */
6153 uint64_t delta, peer_delta, delta_up;
6154
6155 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts;
6156 if (delta < bbr_min_usec_delta) {
6157 /*
6158 * Have not seen a min amount of time
6159 * between our send times so we can
6160 * make a determination of the timestamp
6161 * yet.
6162 */
6163 return;
6164 }
6165 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp;
6166 if (peer_delta < bbr_min_peer_delta) {
6167 /*
6168 * We may have enough in the form of
6169 * our delta but the peers number
6170 * has not changed that much. It could
6171 * be its clock ratio is such that
6172 * we need more data (10ms tick) or
6173 * there may be other compression scenarios
6174 * going on. In any event we need the
6175 * spread to be larger.
6176 */
6177 return;
6178 }
6179 /* Ok lets first see which way our delta is going */
6180 if (peer_delta > delta) {
6181 /* Very unlikely, the peer without
6182 * compensation shows that it saw
6183 * the two sends arrive further apart
6184 * then we saw then in micro-seconds.
6185 */
6186 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) {
6187 /* well it looks like the peer is a micro-second clock. */
6188 bbr->rc_ts_clock_set = 1;
6189 bbr->r_ctl.bbr_peer_tsratio = 1;
6190 } else {
6191 bbr->rc_ts_cant_be_used = 1;
6192 bbr->rc_ts_clock_set = 1;
6193 }
6194 return;
6195 }
6196 /* Ok we know that the peer_delta is smaller than our send distance */
6197 bbr->rc_ts_clock_set = 1;
6198 /* First question is it within the percentage that they are using usec time? */
6199 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent;
6200 if ((peer_delta + delta_up) >= delta) {
6201 /* Its a usec clock */
6202 bbr->r_ctl.bbr_peer_tsratio = 1;
6203 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6204 return;
6205 }
6206 /* Ok if not usec, what about 10usec (though unlikely)? */
6207 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent;
6208 if (((peer_delta * 10) + delta_up) >= delta) {
6209 bbr->r_ctl.bbr_peer_tsratio = 10;
6210 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6211 return;
6212 }
6213 /* And what about 100usec (though again unlikely)? */
6214 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent;
6215 if (((peer_delta * 100) + delta_up) >= delta) {
6216 bbr->r_ctl.bbr_peer_tsratio = 100;
6217 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6218 return;
6219 }
6220 /* And how about 1 msec (the most likely one)? */
6221 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent;
6222 if (((peer_delta * 1000) + delta_up) >= delta) {
6223 bbr->r_ctl.bbr_peer_tsratio = 1000;
6224 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6225 return;
6226 }
6227 /* Ok if not msec could it be 10 msec? */
6228 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent;
6229 if (((peer_delta * 10000) + delta_up) >= delta) {
6230 bbr->r_ctl.bbr_peer_tsratio = 10000;
6231 return;
6232 }
6233 /* If we fall down here the clock tick so slowly we can't use it */
6234 bbr->rc_ts_cant_be_used = 1;
6235 bbr->r_ctl.bbr_peer_tsratio = 0;
6236 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6237 }
6238
6239 /*
6240 * Collect new round-trip time estimate
6241 * and update averages and current timeout.
6242 */
6243 static void
tcp_bbr_xmit_timer_commit(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t cts)6244 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts)
6245 {
6246 int32_t delta;
6247 uint32_t rtt, tsin;
6248 int32_t rtt_ticks;
6249
6250 if (bbr->rtt_valid == 0)
6251 /* No valid sample */
6252 return;
6253
6254 rtt = bbr->r_ctl.cur_rtt;
6255 tsin = bbr->r_ctl.ts_in;
6256 if (bbr->rc_prtt_set_ts) {
6257 /*
6258 * We are to force feed the rttProp filter due
6259 * to an entry into PROBE_RTT. This assures
6260 * that the times are sync'd between when we
6261 * go into PROBE_RTT and the filter expiration.
6262 *
6263 * Google does not use a true filter, so they do
6264 * this implicitly since they only keep one value
6265 * and when they enter probe-rtt they update the
6266 * value to the newest rtt.
6267 */
6268 uint32_t rtt_prop;
6269
6270 bbr->rc_prtt_set_ts = 0;
6271 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
6272 if (rtt > rtt_prop)
6273 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts);
6274 else
6275 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6276 }
6277 #ifdef STATS
6278 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt));
6279 #endif
6280 if (bbr->rc_ack_was_delayed)
6281 rtt += bbr->r_ctl.rc_ack_hdwr_delay;
6282
6283 if (rtt < bbr->r_ctl.rc_lowest_rtt)
6284 bbr->r_ctl.rc_lowest_rtt = rtt;
6285 bbr_log_rtt_sample(bbr, rtt, tsin);
6286 if (bbr->r_init_rtt) {
6287 /*
6288 * The initial rtt is not-trusted, nuke it and lets get
6289 * our first valid measurement in.
6290 */
6291 bbr->r_init_rtt = 0;
6292 tp->t_srtt = 0;
6293 }
6294 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) {
6295 /*
6296 * So we have not yet figured out
6297 * what the peers TSTMP value is
6298 * in (most likely ms). We need a
6299 * series of cum-ack's to determine
6300 * this reliably.
6301 */
6302 if (bbr->rc_ack_is_cumack) {
6303 if (bbr->rc_ts_data_set) {
6304 /* Lets attempt to determine the timestamp granularity. */
6305 bbr_make_timestamp_determination(bbr);
6306 } else {
6307 bbr->rc_ts_data_set = 1;
6308 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts;
6309 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time;
6310 }
6311 } else {
6312 /*
6313 * We have to have consecutive acks
6314 * reset any "filled" state to none.
6315 */
6316 bbr->rc_ts_data_set = 0;
6317 }
6318 }
6319 /* Round it up */
6320 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1)));
6321 if (rtt_ticks == 0)
6322 rtt_ticks = 1;
6323 if (tp->t_srtt != 0) {
6324 /*
6325 * srtt is stored as fixed point with 5 bits after the
6326 * binary point (i.e., scaled by 8). The following magic is
6327 * equivalent to the smoothing algorithm in rfc793 with an
6328 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6329 * Adjust rtt to origin 0.
6330 */
6331
6332 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT)
6333 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6334
6335 tp->t_srtt += delta;
6336 if (tp->t_srtt <= 0)
6337 tp->t_srtt = 1;
6338
6339 /*
6340 * We accumulate a smoothed rtt variance (actually, a
6341 * smoothed mean difference), then set the retransmit timer
6342 * to smoothed rtt + 4 times the smoothed variance. rttvar
6343 * is stored as fixed point with 4 bits after the binary
6344 * point (scaled by 16). The following is equivalent to
6345 * rfc793 smoothing with an alpha of .75 (rttvar =
6346 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6347 * wired-in beta.
6348 */
6349 if (delta < 0)
6350 delta = -delta;
6351 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6352 tp->t_rttvar += delta;
6353 if (tp->t_rttvar <= 0)
6354 tp->t_rttvar = 1;
6355 } else {
6356 /*
6357 * No rtt measurement yet - use the unsmoothed rtt. Set the
6358 * variance to half the rtt (so our first retransmit happens
6359 * at 3*rtt).
6360 */
6361 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT;
6362 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1);
6363 }
6364 KMOD_TCPSTAT_INC(tcps_rttupdated);
6365 if (tp->t_rttupdated < UCHAR_MAX)
6366 tp->t_rttupdated++;
6367 #ifdef STATS
6368 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks));
6369 #endif
6370 /*
6371 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6372 * way we do the smoothing, srtt and rttvar will each average +1/2
6373 * tick of bias. When we compute the retransmit timer, we want 1/2
6374 * tick of rounding and 1 extra tick because of +-1/2 tick
6375 * uncertainty in the firing of the timer. The bias will give us
6376 * exactly the 1.5 tick we need. But, because the bias is
6377 * statistical, we have to test that we don't drop below the minimum
6378 * feasible timer (which is 2 ticks).
6379 */
6380 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6381 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2),
6382 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
6383
6384 /*
6385 * We received an ack for a packet that wasn't retransmitted; it is
6386 * probably safe to discard any error indications we've received
6387 * recently. This isn't quite right, but close enough for now (a
6388 * route might have failed after we sent a segment, and the return
6389 * path might not be symmetrical).
6390 */
6391 tp->t_softerror = 0;
6392 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
6393 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt)
6394 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt;
6395 }
6396
6397 static void
bbr_set_reduced_rtt(struct tcp_bbr * bbr,uint32_t cts,uint32_t line)6398 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line)
6399 {
6400 bbr->r_ctl.rc_rtt_shrinks = cts;
6401 if (bbr_can_force_probertt &&
6402 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
6403 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
6404 /*
6405 * We should enter probe-rtt its been too long
6406 * since we have been there.
6407 */
6408 bbr_enter_probe_rtt(bbr, cts, __LINE__);
6409 } else
6410 bbr_check_probe_rtt_limits(bbr, cts);
6411 }
6412
6413 static void
tcp_bbr_commit_bw(struct tcp_bbr * bbr,uint32_t cts)6414 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts)
6415 {
6416 uint64_t orig_bw;
6417
6418 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) {
6419 /* We never apply a zero measurement */
6420 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0,
6421 0, 0, 0, 0, 0, 0);
6422 return;
6423 }
6424 if (bbr->r_ctl.r_measurement_count < 0xffffffff)
6425 bbr->r_ctl.r_measurement_count++;
6426 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
6427 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch);
6428 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw,
6429 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate),
6430 0, 0, 0, 0, 0, 0);
6431 if (orig_bw &&
6432 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) {
6433 if (bbr->bbr_hdrw_pacing) {
6434 /*
6435 * Apply a new rate to the hardware
6436 * possibly.
6437 */
6438 bbr_update_hardware_pacing_rate(bbr, cts);
6439 }
6440 bbr_set_state_target(bbr, __LINE__);
6441 tcp_bbr_tso_size_check(bbr, cts);
6442 if (bbr->r_recovery_bw) {
6443 bbr_setup_red_bw(bbr, cts);
6444 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW);
6445 }
6446 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate))
6447 tcp_bbr_tso_size_check(bbr, cts);
6448 }
6449
6450 static void
bbr_nf_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6451 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6452 {
6453 if (bbr->rc_in_persist == 0) {
6454 /* We log only when not in persist */
6455 /* Translate to a Bytes Per Second */
6456 uint64_t tim, bw, ts_diff, ts_bw;
6457 uint32_t delivered;
6458
6459 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6460 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6461 else
6462 tim = 1;
6463 /*
6464 * Now that we have processed the tim (skipping the sample
6465 * or possibly updating the time, go ahead and
6466 * calculate the cdr.
6467 */
6468 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6469 bw = (uint64_t)delivered;
6470 bw *= (uint64_t)USECS_IN_SECOND;
6471 bw /= tim;
6472 if (bw == 0) {
6473 /* We must have a calculatable amount */
6474 return;
6475 }
6476 /*
6477 * If we are using this b/w shove it in now so we
6478 * can see in the trace viewer if it gets over-ridden.
6479 */
6480 if (rsm->r_ts_valid &&
6481 bbr->rc_ts_valid &&
6482 bbr->rc_ts_clock_set &&
6483 (bbr->rc_ts_cant_be_used == 0) &&
6484 bbr->rc_use_ts_limit) {
6485 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1);
6486 ts_diff *= bbr->r_ctl.bbr_peer_tsratio;
6487 if ((delivered == 0) ||
6488 (rtt < 1000)) {
6489 /* Can't use the ts */
6490 bbr_log_type_bbrupd(bbr, 61, cts,
6491 ts_diff,
6492 bbr->r_ctl.last_inbound_ts,
6493 rsm->r_del_ack_ts, 0,
6494 0, 0, 0, delivered);
6495 } else {
6496 ts_bw = (uint64_t)delivered;
6497 ts_bw *= (uint64_t)USECS_IN_SECOND;
6498 ts_bw /= ts_diff;
6499 bbr_log_type_bbrupd(bbr, 62, cts,
6500 (ts_bw >> 32),
6501 (ts_bw & 0xffffffff), 0, 0,
6502 0, 0, ts_diff, delivered);
6503 if ((bbr->ts_can_raise) &&
6504 (ts_bw > bw)) {
6505 bbr_log_type_bbrupd(bbr, 8, cts,
6506 delivered,
6507 ts_diff,
6508 (bw >> 32),
6509 (bw & 0x00000000ffffffff),
6510 0, 0, 0, 0);
6511 bw = ts_bw;
6512 } else if (ts_bw && (ts_bw < bw)) {
6513 bbr_log_type_bbrupd(bbr, 7, cts,
6514 delivered,
6515 ts_diff,
6516 (bw >> 32),
6517 (bw & 0x00000000ffffffff),
6518 0, 0, 0, 0);
6519 bw = ts_bw;
6520 }
6521 }
6522 }
6523 if (rsm->r_first_sent_time &&
6524 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6525 uint64_t sbw, sti;
6526 /*
6527 * We use what was in flight at the time of our
6528 * send and the size of this send to figure
6529 * out what we have been sending at (amount).
6530 * For the time we take from the time of
6531 * the send of the first send outstanding
6532 * until this send plus this sends pacing
6533 * time. This gives us a good calculation
6534 * as to the rate we have been sending at.
6535 */
6536
6537 sbw = (uint64_t)(rsm->r_flight_at_send);
6538 sbw *= (uint64_t)USECS_IN_SECOND;
6539 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6540 sti += rsm->r_pacing_delay;
6541 sbw /= sti;
6542 if (sbw < bw) {
6543 bbr_log_type_bbrupd(bbr, 6, cts,
6544 delivered,
6545 (uint32_t)sti,
6546 (bw >> 32),
6547 (uint32_t)bw,
6548 rsm->r_first_sent_time, 0, (sbw >> 32),
6549 (uint32_t)sbw);
6550 bw = sbw;
6551 }
6552 }
6553 /* Use the google algorithm for b/w measurements */
6554 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6555 if ((rsm->r_app_limited == 0) ||
6556 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) {
6557 tcp_bbr_commit_bw(bbr, cts);
6558 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6559 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6560 }
6561 }
6562 }
6563
6564 static void
bbr_google_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6565 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6566 {
6567 if (bbr->rc_in_persist == 0) {
6568 /* We log only when not in persist */
6569 /* Translate to a Bytes Per Second */
6570 uint64_t tim, bw;
6571 uint32_t delivered;
6572 int no_apply = 0;
6573
6574 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6575 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6576 else
6577 tim = 1;
6578 /*
6579 * Now that we have processed the tim (skipping the sample
6580 * or possibly updating the time, go ahead and
6581 * calculate the cdr.
6582 */
6583 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6584 bw = (uint64_t)delivered;
6585 bw *= (uint64_t)USECS_IN_SECOND;
6586 bw /= tim;
6587 if (tim < bbr->r_ctl.rc_lowest_rtt) {
6588 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6589 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6590
6591 no_apply = 1;
6592 }
6593 /*
6594 * If we are using this b/w shove it in now so we
6595 * can see in the trace viewer if it gets over-ridden.
6596 */
6597 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6598 /* Gate by the sending rate */
6599 if (rsm->r_first_sent_time &&
6600 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6601 uint64_t sbw, sti;
6602 /*
6603 * We use what was in flight at the time of our
6604 * send and the size of this send to figure
6605 * out what we have been sending at (amount).
6606 * For the time we take from the time of
6607 * the send of the first send outstanding
6608 * until this send plus this sends pacing
6609 * time. This gives us a good calculation
6610 * as to the rate we have been sending at.
6611 */
6612
6613 sbw = (uint64_t)(rsm->r_flight_at_send);
6614 sbw *= (uint64_t)USECS_IN_SECOND;
6615 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6616 sti += rsm->r_pacing_delay;
6617 sbw /= sti;
6618 if (sbw < bw) {
6619 bbr_log_type_bbrupd(bbr, 6, cts,
6620 delivered,
6621 (uint32_t)sti,
6622 (bw >> 32),
6623 (uint32_t)bw,
6624 rsm->r_first_sent_time, 0, (sbw >> 32),
6625 (uint32_t)sbw);
6626 bw = sbw;
6627 }
6628 if ((sti > tim) &&
6629 (sti < bbr->r_ctl.rc_lowest_rtt)) {
6630 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6631 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6632 no_apply = 1;
6633 } else
6634 no_apply = 0;
6635 }
6636 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6637 if ((no_apply == 0) &&
6638 ((rsm->r_app_limited == 0) ||
6639 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) {
6640 tcp_bbr_commit_bw(bbr, cts);
6641 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6642 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6643 }
6644 }
6645 }
6646
6647 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)6648 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin,
6649 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to)
6650 {
6651 uint64_t old_rttprop;
6652
6653 /* Update our delivery time and amount */
6654 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start);
6655 bbr->r_ctl.rc_del_time = cts;
6656 if (rtt == 0) {
6657 /*
6658 * 0 means its a retransmit, for now we don't use these for
6659 * the rest of BBR.
6660 */
6661 return;
6662 }
6663 if ((bbr->rc_use_google == 0) &&
6664 (match != BBR_RTT_BY_EXACTMATCH) &&
6665 (match != BBR_RTT_BY_TIMESTAMP)){
6666 /*
6667 * We get a lot of rtt updates, lets not pay attention to
6668 * any that are not an exact match. That way we don't have
6669 * to worry about timestamps and the whole nonsense of
6670 * unsure if its a retransmission etc (if we ever had the
6671 * timestamp fixed to always have the last thing sent this
6672 * would not be a issue).
6673 */
6674 return;
6675 }
6676 if ((bbr_no_retran && bbr->rc_use_google) &&
6677 (match != BBR_RTT_BY_EXACTMATCH) &&
6678 (match != BBR_RTT_BY_TIMESTAMP)){
6679 /*
6680 * We only do measurements in google mode
6681 * with bbr_no_retran on for sure things.
6682 */
6683 return;
6684 }
6685 /* Only update srtt if we know by exact match */
6686 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin);
6687 if (ack_type == BBR_CUM_ACKED)
6688 bbr->rc_ack_is_cumack = 1;
6689 else
6690 bbr->rc_ack_is_cumack = 0;
6691 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP);
6692 /*
6693 * Note the following code differs to the original
6694 * BBR spec. It calls for <= not <. However after a
6695 * long discussion in email with Neal, he acknowledged
6696 * that it should be < than so that we will have flows
6697 * going into probe-rtt (we were seeing cases where that
6698 * did not happen and caused ugly things to occur). We
6699 * have added this agreed upon fix to our code base.
6700 */
6701 if (rtt < old_rttprop) {
6702 /* Update when we last saw a rtt drop */
6703 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0);
6704 bbr_set_reduced_rtt(bbr, cts, __LINE__);
6705 }
6706 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts,
6707 match, rsm->r_start, rsm->r_flags);
6708 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6709 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) {
6710 /*
6711 * The RTT-prop moved, reset the target (may be a
6712 * nop for some states).
6713 */
6714 bbr_set_state_target(bbr, __LINE__);
6715 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)
6716 bbr_log_rtt_shrinks(bbr, cts, 0, 0,
6717 __LINE__, BBR_RTTS_NEW_TARGET, 0);
6718 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP))
6719 /* It went up */
6720 bbr_check_probe_rtt_limits(bbr, cts);
6721 }
6722 if ((bbr->rc_use_google == 0) &&
6723 (match == BBR_RTT_BY_TIMESTAMP)) {
6724 /*
6725 * We don't do b/w update with
6726 * these since they are not really
6727 * reliable.
6728 */
6729 return;
6730 }
6731 if (bbr->r_ctl.r_app_limited_until &&
6732 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) {
6733 /* We are no longer app-limited */
6734 bbr->r_ctl.r_app_limited_until = 0;
6735 }
6736 if (bbr->rc_use_google) {
6737 bbr_google_measurement(bbr, rsm, rtt, cts);
6738 } else {
6739 bbr_nf_measurement(bbr, rsm, rtt, cts);
6740 }
6741 }
6742
6743 /*
6744 * Convert a timestamp that the main stack
6745 * uses (milliseconds) into one that bbr uses
6746 * (microseconds). Return that converted timestamp.
6747 */
6748 static uint32_t
bbr_ts_convert(uint32_t cts)6749 bbr_ts_convert(uint32_t cts) {
6750 uint32_t sec, msec;
6751
6752 sec = cts / MS_IN_USEC;
6753 msec = cts - (MS_IN_USEC * sec);
6754 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC));
6755 }
6756
6757 /*
6758 * Return 0 if we did not update the RTT time, return
6759 * 1 if we did.
6760 */
6761 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)6762 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr,
6763 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack)
6764 {
6765 int32_t i;
6766 uint32_t t, uts = 0;
6767
6768 if ((rsm->r_flags & BBR_ACKED) ||
6769 (rsm->r_flags & BBR_WAS_RENEGED) ||
6770 (rsm->r_flags & BBR_RXT_CLEARED)) {
6771 /* Already done */
6772 return (0);
6773 }
6774 if (rsm->r_rtt_not_allowed) {
6775 /* Not allowed */
6776 return (0);
6777 }
6778 if (rsm->r_rtr_cnt == 1) {
6779 /*
6780 * Only one transmit. Hopefully the normal case.
6781 */
6782 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0]))
6783 t = cts - rsm->r_tim_lastsent[0];
6784 else
6785 t = 1;
6786 if ((int)t <= 0)
6787 t = 1;
6788 bbr->r_ctl.rc_last_rtt = t;
6789 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6790 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to);
6791 return (1);
6792 }
6793 /* Convert to usecs */
6794 if ((bbr_can_use_ts_for_rtt == 1) &&
6795 (bbr->rc_use_google == 1) &&
6796 (ack_type == BBR_CUM_ACKED) &&
6797 (to->to_flags & TOF_TS) &&
6798 (to->to_tsecr != 0)) {
6799 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr;
6800 if (t < 1)
6801 t = 1;
6802 t *= MS_IN_USEC;
6803 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6804 BBR_RTT_BY_TIMESTAMP,
6805 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)],
6806 ack_type, to);
6807 return (1);
6808 }
6809 uts = bbr_ts_convert(to->to_tsecr);
6810 if ((to->to_flags & TOF_TS) &&
6811 (to->to_tsecr != 0) &&
6812 (ack_type == BBR_CUM_ACKED) &&
6813 ((rsm->r_flags & BBR_OVERMAX) == 0)) {
6814 /*
6815 * Now which timestamp does it match? In this block the ACK
6816 * may be coming from a previous transmission.
6817 */
6818 uint32_t fudge;
6819
6820 fudge = BBR_TIMER_FUDGE;
6821 for (i = 0; i < rsm->r_rtr_cnt; i++) {
6822 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) &&
6823 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) {
6824 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6825 t = cts - rsm->r_tim_lastsent[i];
6826 else
6827 t = 1;
6828 if ((int)t <= 0)
6829 t = 1;
6830 bbr->r_ctl.rc_last_rtt = t;
6831 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING,
6832 rsm->r_tim_lastsent[i], ack_type, to);
6833 if ((i + 1) < rsm->r_rtr_cnt) {
6834 /* Likely */
6835 return (0);
6836 } else if (rsm->r_flags & BBR_TLP) {
6837 bbr->rc_tlp_rtx_out = 0;
6838 }
6839 return (1);
6840 }
6841 }
6842 /* Fall through if we can't find a matching timestamp */
6843 }
6844 /*
6845 * Ok its a SACK block that we retransmitted. or a windows
6846 * machine without timestamps. We can tell nothing from the
6847 * time-stamp since its not there or the time the peer last
6848 * received a segment that moved forward its cum-ack point.
6849 *
6850 * Lets look at the last retransmit and see what we can tell
6851 * (with BBR for space we only keep 2 note we have to keep
6852 * at least 2 so the map can not be condensed more).
6853 */
6854 i = rsm->r_rtr_cnt - 1;
6855 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6856 t = cts - rsm->r_tim_lastsent[i];
6857 else
6858 goto not_sure;
6859 if (t < bbr->r_ctl.rc_lowest_rtt) {
6860 /*
6861 * We retransmitted and the ack came back in less
6862 * than the smallest rtt we have observed in the
6863 * windowed rtt. We most likey did an improper
6864 * retransmit as outlined in 4.2 Step 3 point 2 in
6865 * the rack-draft.
6866 *
6867 * Use the prior transmission to update all the
6868 * information as long as there is only one prior
6869 * transmission.
6870 */
6871 if ((rsm->r_flags & BBR_OVERMAX) == 0) {
6872 #ifdef BBR_INVARIANTS
6873 if (rsm->r_rtr_cnt == 1)
6874 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags);
6875 #endif
6876 i = rsm->r_rtr_cnt - 2;
6877 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6878 t = cts - rsm->r_tim_lastsent[i];
6879 else
6880 t = 1;
6881 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET,
6882 rsm->r_tim_lastsent[i], ack_type, to);
6883 return (0);
6884 } else {
6885 /*
6886 * Too many prior transmissions, just
6887 * updated BBR delivered
6888 */
6889 not_sure:
6890 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
6891 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
6892 }
6893 } else {
6894 /*
6895 * We retransmitted it and the retransmit did the
6896 * job.
6897 */
6898 if (rsm->r_flags & BBR_TLP)
6899 bbr->rc_tlp_rtx_out = 0;
6900 if ((rsm->r_flags & BBR_OVERMAX) == 0)
6901 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts,
6902 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to);
6903 else
6904 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
6905 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
6906 return (1);
6907 }
6908 return (0);
6909 }
6910
6911 /*
6912 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
6913 */
6914 static void
bbr_log_sack_passed(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm)6915 bbr_log_sack_passed(struct tcpcb *tp,
6916 struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
6917 {
6918 struct bbr_sendmap *nrsm;
6919
6920 nrsm = rsm;
6921 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap,
6922 bbr_head, r_tnext) {
6923 if (nrsm == rsm) {
6924 /* Skip original segment he is acked */
6925 continue;
6926 }
6927 if (nrsm->r_flags & BBR_ACKED) {
6928 /* Skip ack'd segments */
6929 continue;
6930 }
6931 if (nrsm->r_flags & BBR_SACK_PASSED) {
6932 /*
6933 * We found one that is already marked
6934 * passed, we have been here before and
6935 * so all others below this are marked.
6936 */
6937 break;
6938 }
6939 BBR_STAT_INC(bbr_sack_passed);
6940 nrsm->r_flags |= BBR_SACK_PASSED;
6941 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) &&
6942 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) {
6943 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start;
6944 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start;
6945 nrsm->r_flags |= BBR_MARKED_LOST;
6946 }
6947 nrsm->r_flags &= ~BBR_WAS_SACKPASS;
6948 }
6949 }
6950
6951 /*
6952 * Returns the number of bytes that were
6953 * newly ack'd by sack blocks.
6954 */
6955 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)6956 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack,
6957 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts)
6958 {
6959 int32_t times = 0;
6960 uint32_t start, end, changed = 0;
6961 struct bbr_sendmap *rsm, *nrsm;
6962 int32_t used_ref = 1;
6963 uint8_t went_back = 0, went_fwd = 0;
6964
6965 start = sack->start;
6966 end = sack->end;
6967 rsm = *prsm;
6968 if (rsm == NULL)
6969 used_ref = 0;
6970
6971 /* Do we locate the block behind where we last were? */
6972 if (rsm && SEQ_LT(start, rsm->r_start)) {
6973 went_back = 1;
6974 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
6975 if (SEQ_GEQ(start, rsm->r_start) &&
6976 SEQ_LT(start, rsm->r_end)) {
6977 goto do_rest_ofb;
6978 }
6979 }
6980 }
6981 start_at_beginning:
6982 went_fwd = 1;
6983 /*
6984 * Ok lets locate the block where this guy is fwd from rsm (if its
6985 * set)
6986 */
6987 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) {
6988 if (SEQ_GEQ(start, rsm->r_start) &&
6989 SEQ_LT(start, rsm->r_end)) {
6990 break;
6991 }
6992 }
6993 do_rest_ofb:
6994 if (rsm == NULL) {
6995 /*
6996 * This happens when we get duplicate sack blocks with the
6997 * same end. For example SACK 4: 100 SACK 3: 100 The sort
6998 * will not change there location so we would just start at
6999 * the end of the first one and get lost.
7000 */
7001 if (tp->t_flags & TF_SENTFIN) {
7002 /*
7003 * Check to see if we have not logged the FIN that
7004 * went out.
7005 */
7006 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7007 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
7008 /*
7009 * Ok we did not get the FIN logged.
7010 */
7011 nrsm->r_end++;
7012 rsm = nrsm;
7013 goto do_rest_ofb;
7014 }
7015 }
7016 if (times == 1) {
7017 #ifdef BBR_INVARIANTS
7018 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p",
7019 tp, bbr, sack, to, prsm);
7020 #else
7021 goto out;
7022 #endif
7023 }
7024 times++;
7025 BBR_STAT_INC(bbr_sack_proc_restart);
7026 rsm = NULL;
7027 goto start_at_beginning;
7028 }
7029 /* Ok we have an ACK for some piece of rsm */
7030 if (rsm->r_start != start) {
7031 /*
7032 * Need to split this in two pieces the before and after.
7033 */
7034 if (bbr_sack_mergable(rsm, start, end))
7035 nrsm = bbr_alloc_full_limit(bbr);
7036 else
7037 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7038 if (nrsm == NULL) {
7039 /* We could not allocate ignore the sack */
7040 struct sackblk blk;
7041
7042 blk.start = start;
7043 blk.end = end;
7044 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7045 goto out;
7046 }
7047 bbr_clone_rsm(bbr, nrsm, rsm, start);
7048 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7049 if (rsm->r_in_tmap) {
7050 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7051 nrsm->r_in_tmap = 1;
7052 }
7053 rsm->r_flags &= (~BBR_HAS_FIN);
7054 rsm = nrsm;
7055 }
7056 if (SEQ_GEQ(end, rsm->r_end)) {
7057 /*
7058 * The end of this block is either beyond this guy or right
7059 * at this guy.
7060 */
7061 if ((rsm->r_flags & BBR_ACKED) == 0) {
7062 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7063 changed += (rsm->r_end - rsm->r_start);
7064 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7065 bbr_log_sack_passed(tp, bbr, rsm);
7066 if (rsm->r_flags & BBR_MARKED_LOST) {
7067 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7068 }
7069 /* Is Reordering occuring? */
7070 if (rsm->r_flags & BBR_SACK_PASSED) {
7071 BBR_STAT_INC(bbr_reorder_seen);
7072 bbr->r_ctl.rc_reorder_ts = cts;
7073 if (rsm->r_flags & BBR_MARKED_LOST) {
7074 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7075 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7076 /* LT sampling also needs adjustment */
7077 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7078 }
7079 }
7080 rsm->r_flags |= BBR_ACKED;
7081 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7082 if (rsm->r_in_tmap) {
7083 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7084 rsm->r_in_tmap = 0;
7085 }
7086 }
7087 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7088 if (end == rsm->r_end) {
7089 /* This block only - done */
7090 goto out;
7091 }
7092 /* There is more not coverend by this rsm move on */
7093 start = rsm->r_end;
7094 nrsm = TAILQ_NEXT(rsm, r_next);
7095 rsm = nrsm;
7096 times = 0;
7097 goto do_rest_ofb;
7098 }
7099 if (rsm->r_flags & BBR_ACKED) {
7100 /* Been here done that */
7101 goto out;
7102 }
7103 /* Ok we need to split off this one at the tail */
7104 if (bbr_sack_mergable(rsm, start, end))
7105 nrsm = bbr_alloc_full_limit(bbr);
7106 else
7107 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7108 if (nrsm == NULL) {
7109 /* failed XXXrrs what can we do but loose the sack info? */
7110 struct sackblk blk;
7111
7112 blk.start = start;
7113 blk.end = end;
7114 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7115 goto out;
7116 }
7117 /* Clone it */
7118 bbr_clone_rsm(bbr, nrsm, rsm, end);
7119 /* The sack block does not cover this guy fully */
7120 rsm->r_flags &= (~BBR_HAS_FIN);
7121 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7122 if (rsm->r_in_tmap) {
7123 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7124 nrsm->r_in_tmap = 1;
7125 }
7126 nrsm->r_dupack = 0;
7127 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7128 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7129 changed += (rsm->r_end - rsm->r_start);
7130 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7131 bbr_log_sack_passed(tp, bbr, rsm);
7132 /* Is Reordering occuring? */
7133 if (rsm->r_flags & BBR_MARKED_LOST) {
7134 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7135 }
7136 if (rsm->r_flags & BBR_SACK_PASSED) {
7137 BBR_STAT_INC(bbr_reorder_seen);
7138 bbr->r_ctl.rc_reorder_ts = cts;
7139 if (rsm->r_flags & BBR_MARKED_LOST) {
7140 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7141 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7142 /* LT sampling also needs adjustment */
7143 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7144 }
7145 }
7146 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7147 rsm->r_flags |= BBR_ACKED;
7148 if (rsm->r_in_tmap) {
7149 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7150 rsm->r_in_tmap = 0;
7151 }
7152 out:
7153 if (rsm && (rsm->r_flags & BBR_ACKED)) {
7154 /*
7155 * Now can we merge this newly acked
7156 * block with either the previous or
7157 * next block?
7158 */
7159 nrsm = TAILQ_NEXT(rsm, r_next);
7160 if (nrsm &&
7161 (nrsm->r_flags & BBR_ACKED)) {
7162 /* yep this and next can be merged */
7163 rsm = bbr_merge_rsm(bbr, rsm, nrsm);
7164 }
7165 /* Now what about the previous? */
7166 nrsm = TAILQ_PREV(rsm, bbr_head, r_next);
7167 if (nrsm &&
7168 (nrsm->r_flags & BBR_ACKED)) {
7169 /* yep the previous and this can be merged */
7170 rsm = bbr_merge_rsm(bbr, nrsm, rsm);
7171 }
7172 }
7173 if (used_ref == 0) {
7174 BBR_STAT_INC(bbr_sack_proc_all);
7175 } else {
7176 BBR_STAT_INC(bbr_sack_proc_short);
7177 }
7178 if (went_fwd && went_back) {
7179 BBR_STAT_INC(bbr_sack_search_both);
7180 } else if (went_fwd) {
7181 BBR_STAT_INC(bbr_sack_search_fwd);
7182 } else if (went_back) {
7183 BBR_STAT_INC(bbr_sack_search_back);
7184 }
7185 /* Save off where the next seq is */
7186 if (rsm)
7187 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
7188 else
7189 bbr->r_ctl.rc_sacklast = NULL;
7190 *prsm = rsm;
7191 return (changed);
7192 }
7193
7194 static void inline
bbr_peer_reneges(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,tcp_seq th_ack)7195 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack)
7196 {
7197 struct bbr_sendmap *tmap;
7198
7199 BBR_STAT_INC(bbr_reneges_seen);
7200 tmap = NULL;
7201 while (rsm && (rsm->r_flags & BBR_ACKED)) {
7202 /* Its no longer sacked, mark it so */
7203 uint32_t oflags;
7204 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7205 #ifdef BBR_INVARIANTS
7206 if (rsm->r_in_tmap) {
7207 panic("bbr:%p rsm:%p flags:0x%x in tmap?",
7208 bbr, rsm, rsm->r_flags);
7209 }
7210 #endif
7211 oflags = rsm->r_flags;
7212 if (rsm->r_flags & BBR_MARKED_LOST) {
7213 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7214 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7215 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7216 /* LT sampling also needs adjustment */
7217 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7218 }
7219 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST);
7220 rsm->r_flags |= BBR_WAS_RENEGED;
7221 rsm->r_flags |= BBR_RXT_CLEARED;
7222 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__);
7223 /* Rebuild it into our tmap */
7224 if (tmap == NULL) {
7225 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7226 tmap = rsm;
7227 } else {
7228 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7229 tmap = rsm;
7230 }
7231 tmap->r_in_tmap = 1;
7232 /*
7233 * XXXrrs Delivered? Should we do anything here?
7234 *
7235 * Of course we don't on a rxt timeout so maybe its ok that
7236 * we don't?
7237 *
7238 * For now lets not.
7239 */
7240 rsm = TAILQ_NEXT(rsm, r_next);
7241 }
7242 /*
7243 * Now lets possibly clear the sack filter so we start recognizing
7244 * sacks that cover this area.
7245 */
7246 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack);
7247 }
7248
7249 static void
bbr_log_syn(struct tcpcb * tp,struct tcpopt * to)7250 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to)
7251 {
7252 struct tcp_bbr *bbr;
7253 struct bbr_sendmap *rsm;
7254 uint32_t cts;
7255
7256 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7257 cts = bbr->r_ctl.rc_rcvtime;
7258 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7259 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) {
7260 if ((rsm->r_end - rsm->r_start) <= 1) {
7261 /* Log out the SYN completely */
7262 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7263 rsm->r_rtr_bytes = 0;
7264 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7265 if (rsm->r_in_tmap) {
7266 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7267 rsm->r_in_tmap = 0;
7268 }
7269 if (bbr->r_ctl.rc_next == rsm) {
7270 /* scoot along the marker */
7271 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7272 }
7273 if (to != NULL)
7274 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0);
7275 bbr_free(bbr, rsm);
7276 } else {
7277 /* There is more (Fast open)? strip out SYN. */
7278 rsm->r_flags &= ~BBR_HAS_SYN;
7279 rsm->r_start++;
7280 }
7281 }
7282 }
7283
7284 /*
7285 * Returns the number of bytes that were
7286 * acknowledged by SACK blocks.
7287 */
7288
7289 static uint32_t
bbr_log_ack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th,uint32_t * prev_acked)7290 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
7291 uint32_t *prev_acked)
7292 {
7293 uint32_t changed, last_seq, entered_recovery = 0;
7294 struct tcp_bbr *bbr;
7295 struct bbr_sendmap *rsm;
7296 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7297 register uint32_t th_ack;
7298 int32_t i, j, k, new_sb, num_sack_blks = 0;
7299 uint32_t cts, acked, ack_point, sack_changed = 0;
7300 uint32_t p_maxseg, maxseg, p_acked = 0;
7301
7302 INP_WLOCK_ASSERT(tptoinpcb(tp));
7303 if (tcp_get_flags(th) & TH_RST) {
7304 /* We don't log resets */
7305 return (0);
7306 }
7307 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7308 cts = bbr->r_ctl.rc_rcvtime;
7309
7310 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7311 changed = 0;
7312 maxseg = tp->t_maxseg - bbr->rc_last_options;
7313 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg);
7314 th_ack = th->th_ack;
7315 if (SEQ_GT(th_ack, tp->snd_una)) {
7316 acked = th_ack - tp->snd_una;
7317 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__);
7318 bbr->rc_tp->t_acktime = ticks;
7319 } else
7320 acked = 0;
7321 if (SEQ_LEQ(th_ack, tp->snd_una)) {
7322 /* Only sent here for sack processing */
7323 goto proc_sack;
7324 }
7325 if (rsm && SEQ_GT(th_ack, rsm->r_start)) {
7326 changed = th_ack - rsm->r_start;
7327 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) {
7328 /*
7329 * For the SYN incoming case we will not have called
7330 * tcp_output for the sending of the SYN, so there will be
7331 * no map. All other cases should probably be a panic.
7332 */
7333 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) {
7334 /*
7335 * We have a timestamp that can be used to generate
7336 * an initial RTT.
7337 */
7338 uint32_t ts, now, rtt;
7339
7340 ts = bbr_ts_convert(to->to_tsecr);
7341 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv));
7342 rtt = now - ts;
7343 if (rtt < 1)
7344 rtt = 1;
7345 bbr_log_type_bbrrttprop(bbr, rtt,
7346 tp->iss, 0, cts,
7347 BBR_RTT_BY_TIMESTAMP, tp->iss, 0);
7348 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
7349 changed = 1;
7350 bbr->r_wanted_output = 1;
7351 goto out;
7352 }
7353 goto proc_sack;
7354 } else if (rsm == NULL) {
7355 goto out;
7356 }
7357 if (changed) {
7358 /*
7359 * The ACK point is advancing to th_ack, we must drop off
7360 * the packets in the rack log and calculate any eligble
7361 * RTT's.
7362 */
7363 bbr->r_wanted_output = 1;
7364 more:
7365 if (rsm == NULL) {
7366 if (tp->t_flags & TF_SENTFIN) {
7367 /* if we send a FIN we will not hav a map */
7368 goto proc_sack;
7369 }
7370 #ifdef BBR_INVARIANTS
7371 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n",
7372 tp,
7373 th, tp->t_state, bbr,
7374 tp->snd_una, tp->snd_max, changed);
7375 #endif
7376 goto proc_sack;
7377 }
7378 }
7379 if (SEQ_LT(th_ack, rsm->r_start)) {
7380 /* Huh map is missing this */
7381 #ifdef BBR_INVARIANTS
7382 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n",
7383 rsm->r_start,
7384 th_ack, tp->t_state,
7385 bbr->r_state, bbr);
7386 panic("th-ack is bad bbr:%p tp:%p", bbr, tp);
7387 #endif
7388 goto proc_sack;
7389 } else if (th_ack == rsm->r_start) {
7390 /* None here to ack */
7391 goto proc_sack;
7392 }
7393 /*
7394 * Clear the dup ack counter, it will
7395 * either be freed or if there is some
7396 * remaining we need to start it at zero.
7397 */
7398 rsm->r_dupack = 0;
7399 /* Now do we consume the whole thing? */
7400 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7401 /* Its all consumed. */
7402 uint32_t left;
7403
7404 if (rsm->r_flags & BBR_ACKED) {
7405 /*
7406 * It was acked on the scoreboard -- remove it from
7407 * total
7408 */
7409 p_acked += (rsm->r_end - rsm->r_start);
7410 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7411 if (bbr->r_ctl.rc_sacked == 0)
7412 bbr->r_ctl.rc_sacklast = NULL;
7413 } else {
7414 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack);
7415 if (rsm->r_flags & BBR_MARKED_LOST) {
7416 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7417 }
7418 if (rsm->r_flags & BBR_SACK_PASSED) {
7419 /*
7420 * There are acked segments ACKED on the
7421 * scoreboard further up. We are seeing
7422 * reordering.
7423 */
7424 BBR_STAT_INC(bbr_reorder_seen);
7425 bbr->r_ctl.rc_reorder_ts = cts;
7426 if (rsm->r_flags & BBR_MARKED_LOST) {
7427 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7428 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7429 /* LT sampling also needs adjustment */
7430 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7431 }
7432 }
7433 rsm->r_flags &= ~BBR_MARKED_LOST;
7434 }
7435 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7436 rsm->r_rtr_bytes = 0;
7437 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7438 if (rsm->r_in_tmap) {
7439 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7440 rsm->r_in_tmap = 0;
7441 }
7442 if (bbr->r_ctl.rc_next == rsm) {
7443 /* scoot along the marker */
7444 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7445 }
7446 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7447 /* Adjust the packet counts */
7448 left = th_ack - rsm->r_end;
7449 /* Free back to zone */
7450 bbr_free(bbr, rsm);
7451 if (left) {
7452 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7453 goto more;
7454 }
7455 goto proc_sack;
7456 }
7457 if (rsm->r_flags & BBR_ACKED) {
7458 /*
7459 * It was acked on the scoreboard -- remove it from total
7460 * for the part being cum-acked.
7461 */
7462 p_acked += (rsm->r_end - rsm->r_start);
7463 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7464 if (bbr->r_ctl.rc_sacked == 0)
7465 bbr->r_ctl.rc_sacklast = NULL;
7466 } else {
7467 /*
7468 * It was acked up to th_ack point for the first time
7469 */
7470 struct bbr_sendmap lrsm;
7471
7472 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap));
7473 lrsm.r_end = th_ack;
7474 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack);
7475 }
7476 if ((rsm->r_flags & BBR_MARKED_LOST) &&
7477 ((rsm->r_flags & BBR_ACKED) == 0)) {
7478 /*
7479 * It was marked lost and partly ack'd now
7480 * for the first time. We lower the rc_lost_bytes
7481 * and still leave it MARKED.
7482 */
7483 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start;
7484 }
7485 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7486 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7487 rsm->r_rtr_bytes = 0;
7488 /* adjust packet count */
7489 rsm->r_start = th_ack;
7490 proc_sack:
7491 /* Check for reneging */
7492 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7493 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) {
7494 /*
7495 * The peer has moved snd_una up to the edge of this send,
7496 * i.e. one that it had previously acked. The only way that
7497 * can be true if the peer threw away data (space issues)
7498 * that it had previously sacked (else it would have given
7499 * us snd_una up to (rsm->r_end). We need to undo the acked
7500 * markings here.
7501 *
7502 * Note we have to look to make sure th_ack is our
7503 * rsm->r_start in case we get an old ack where th_ack is
7504 * behind snd_una.
7505 */
7506 bbr_peer_reneges(bbr, rsm, th->th_ack);
7507 }
7508 if ((to->to_flags & TOF_SACK) == 0) {
7509 /* We are done nothing left to log */
7510 goto out;
7511 }
7512 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7513 if (rsm) {
7514 last_seq = rsm->r_end;
7515 } else {
7516 last_seq = tp->snd_max;
7517 }
7518 /* Sack block processing */
7519 if (SEQ_GT(th_ack, tp->snd_una))
7520 ack_point = th_ack;
7521 else
7522 ack_point = tp->snd_una;
7523 for (i = 0; i < to->to_nsacks; i++) {
7524 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7525 &sack, sizeof(sack));
7526 sack.start = ntohl(sack.start);
7527 sack.end = ntohl(sack.end);
7528 if (SEQ_GT(sack.end, sack.start) &&
7529 SEQ_GT(sack.start, ack_point) &&
7530 SEQ_LT(sack.start, tp->snd_max) &&
7531 SEQ_GT(sack.end, ack_point) &&
7532 SEQ_LEQ(sack.end, tp->snd_max)) {
7533 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) &&
7534 (SEQ_LT(sack.end, last_seq)) &&
7535 ((sack.end - sack.start) < (p_maxseg / 8))) {
7536 /*
7537 * Not the last piece and its smaller than
7538 * 1/8th of a p_maxseg. We ignore this.
7539 */
7540 BBR_STAT_INC(bbr_runt_sacks);
7541 continue;
7542 }
7543 sack_blocks[num_sack_blks] = sack;
7544 num_sack_blks++;
7545 } else if (SEQ_LEQ(sack.start, th_ack) &&
7546 SEQ_LEQ(sack.end, th_ack)) {
7547 /*
7548 * Its a D-SACK block.
7549 */
7550 tcp_record_dsack(tp, sack.start, sack.end, 0);
7551 }
7552 }
7553 if (num_sack_blks == 0)
7554 goto out;
7555 /*
7556 * Sort the SACK blocks so we can update the rack scoreboard with
7557 * just one pass.
7558 */
7559 new_sb = sack_filter_blks(&bbr->r_ctl.bbr_sf, sack_blocks,
7560 num_sack_blks, th->th_ack);
7561 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks);
7562 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks);
7563 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb));
7564 num_sack_blks = new_sb;
7565 if (num_sack_blks < 2) {
7566 goto do_sack_work;
7567 }
7568 /* Sort the sacks */
7569 for (i = 0; i < num_sack_blks; i++) {
7570 for (j = i + 1; j < num_sack_blks; j++) {
7571 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7572 sack = sack_blocks[i];
7573 sack_blocks[i] = sack_blocks[j];
7574 sack_blocks[j] = sack;
7575 }
7576 }
7577 }
7578 /*
7579 * Now are any of the sack block ends the same (yes some
7580 * implememtations send these)?
7581 */
7582 again:
7583 if (num_sack_blks > 1) {
7584 for (i = 0; i < num_sack_blks; i++) {
7585 for (j = i + 1; j < num_sack_blks; j++) {
7586 if (sack_blocks[i].end == sack_blocks[j].end) {
7587 /*
7588 * Ok these two have the same end we
7589 * want the smallest end and then
7590 * throw away the larger and start
7591 * again.
7592 */
7593 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7594 /*
7595 * The second block covers
7596 * more area use that
7597 */
7598 sack_blocks[i].start = sack_blocks[j].start;
7599 }
7600 /*
7601 * Now collapse out the dup-sack and
7602 * lower the count
7603 */
7604 for (k = (j + 1); k < num_sack_blks; k++) {
7605 sack_blocks[j].start = sack_blocks[k].start;
7606 sack_blocks[j].end = sack_blocks[k].end;
7607 j++;
7608 }
7609 num_sack_blks--;
7610 goto again;
7611 }
7612 }
7613 }
7614 }
7615 do_sack_work:
7616 rsm = bbr->r_ctl.rc_sacklast;
7617 for (i = 0; i < num_sack_blks; i++) {
7618 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts);
7619 if (acked) {
7620 bbr->r_wanted_output = 1;
7621 changed += acked;
7622 sack_changed += acked;
7623 }
7624 }
7625 out:
7626 *prev_acked = p_acked;
7627 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
7628 /*
7629 * Ok we have a high probability that we need to go in to
7630 * recovery since we have data sack'd
7631 */
7632 struct bbr_sendmap *rsm;
7633
7634 rsm = bbr_check_recovery_mode(tp, bbr, cts);
7635 if (rsm) {
7636 /* Enter recovery */
7637 entered_recovery = 1;
7638 bbr->r_wanted_output = 1;
7639 /*
7640 * When we enter recovery we need to assure we send
7641 * one packet.
7642 */
7643 if (bbr->r_ctl.rc_resend == NULL) {
7644 bbr->r_ctl.rc_resend = rsm;
7645 }
7646 }
7647 }
7648 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
7649 /*
7650 * See if we need to rack-retransmit anything if so set it
7651 * up as the thing to resend assuming something else is not
7652 * already in that position.
7653 */
7654 if (bbr->r_ctl.rc_resend == NULL) {
7655 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
7656 }
7657 }
7658 /*
7659 * We return the amount that changed via sack, this is used by the
7660 * ack-received code to augment what was changed between th_ack <->
7661 * snd_una.
7662 */
7663 return (sack_changed);
7664 }
7665
7666 static void
bbr_strike_dupack(struct tcp_bbr * bbr)7667 bbr_strike_dupack(struct tcp_bbr *bbr)
7668 {
7669 struct bbr_sendmap *rsm;
7670
7671 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
7672 if (rsm && (rsm->r_dupack < 0xff)) {
7673 rsm->r_dupack++;
7674 if (rsm->r_dupack >= DUP_ACK_THRESHOLD)
7675 bbr->r_wanted_output = 1;
7676 }
7677 }
7678
7679 /*
7680 * Return value of 1, we do not need to call bbr_process_data().
7681 * return value of 0, bbr_process_data can be called.
7682 * For ret_val if its 0 the TCB is locked and valid, if its non-zero
7683 * its unlocked and probably unsafe to touch the TCB.
7684 */
7685 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)7686 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
7687 struct tcpcb *tp, struct tcpopt *to,
7688 uint32_t tiwin, int32_t tlen,
7689 int32_t * ofia, int32_t thflags, int32_t * ret_val)
7690 {
7691 int32_t ourfinisacked = 0;
7692 int32_t acked_amount;
7693 uint16_t nsegs;
7694 int32_t acked;
7695 uint32_t lost, sack_changed = 0;
7696 struct mbuf *mfree;
7697 struct tcp_bbr *bbr;
7698 uint32_t prev_acked = 0;
7699
7700 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7701 lost = bbr->r_ctl.rc_lost;
7702 nsegs = max(1, m->m_pkthdr.lro_nsegs);
7703 if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) {
7704 /* Checking SEG.ACK against ISS is definitely redundant. */
7705 tp->t_flags2 |= TF2_NO_ISS_CHECK;
7706 }
7707 if (!V_tcp_insecure_ack) {
7708 tcp_seq seq_min;
7709 bool ghost_ack_check;
7710
7711 if (tp->t_flags2 & TF2_NO_ISS_CHECK) {
7712 /* Check for too old ACKs (RFC 5961, Section 5.2). */
7713 seq_min = tp->snd_una - tp->max_sndwnd;
7714 ghost_ack_check = false;
7715 } else {
7716 if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) {
7717 /* Checking for ghost ACKs is stricter. */
7718 seq_min = tp->iss + 1;
7719 ghost_ack_check = true;
7720 } else {
7721 /*
7722 * Checking for too old ACKs (RFC 5961,
7723 * Section 5.2) is stricter.
7724 */
7725 seq_min = tp->snd_una - tp->max_sndwnd;
7726 ghost_ack_check = false;
7727 }
7728 }
7729 if (SEQ_LT(th->th_ack, seq_min)) {
7730 if (ghost_ack_check)
7731 TCPSTAT_INC(tcps_rcvghostack);
7732 else
7733 TCPSTAT_INC(tcps_rcvacktooold);
7734 /* Send challenge ACK. */
7735 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7736 bbr->r_wanted_output = 1;
7737 return (1);
7738 }
7739 }
7740 if (SEQ_GT(th->th_ack, tp->snd_max)) {
7741 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7742 bbr->r_wanted_output = 1;
7743 return (1);
7744 }
7745 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
7746 /* Process the ack */
7747 if (bbr->rc_in_persist)
7748 tp->t_rxtshift = 0;
7749 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
7750 bbr_strike_dupack(bbr);
7751 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
7752 }
7753 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost));
7754 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
7755 /*
7756 * Old ack, behind the last one rcv'd or a duplicate ack
7757 * with SACK info.
7758 */
7759 if (th->th_ack == tp->snd_una) {
7760 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0);
7761 if (bbr->r_state == TCPS_SYN_SENT) {
7762 /*
7763 * Special case on where we sent SYN. When
7764 * the SYN-ACK is processed in syn_sent
7765 * state it bumps the snd_una. This causes
7766 * us to hit here even though we did ack 1
7767 * byte.
7768 *
7769 * Go through the nothing left case so we
7770 * send data.
7771 */
7772 goto nothing_left;
7773 }
7774 }
7775 return (0);
7776 }
7777 /*
7778 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
7779 * something we sent.
7780 */
7781 if (tp->t_flags & TF_NEEDSYN) {
7782 /*
7783 * T/TCP: Connection was half-synchronized, and our SYN has
7784 * been ACK'd (so connection is now fully synchronized). Go
7785 * to non-starred state, increment snd_una for ACK of SYN,
7786 * and check if we can do window scaling.
7787 */
7788 tp->t_flags &= ~TF_NEEDSYN;
7789 tp->snd_una++;
7790 /* Do window scaling? */
7791 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
7792 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
7793 tp->rcv_scale = tp->request_r_scale;
7794 /* Send window already scaled. */
7795 }
7796 }
7797 INP_WLOCK_ASSERT(tptoinpcb(tp));
7798
7799 acked = BYTES_THIS_ACK(tp, th);
7800 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
7801 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
7802
7803 /*
7804 * If we just performed our first retransmit, and the ACK arrives
7805 * within our recovery window, then it was a mistake to do the
7806 * retransmit in the first place. Recover our original cwnd and
7807 * ssthresh, and proceed to transmit where we left off.
7808 */
7809 if (tp->t_flags & TF_PREVVALID) {
7810 tp->t_flags &= ~TF_PREVVALID;
7811 if (tp->t_rxtshift == 1 &&
7812 (int)(ticks - tp->t_badrxtwin) < 0)
7813 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
7814 }
7815 SOCKBUF_LOCK(&so->so_snd);
7816 acked_amount = min(acked, (int)sbavail(&so->so_snd));
7817 tp->snd_wnd -= acked_amount;
7818 mfree = sbcut_locked(&so->so_snd, acked_amount);
7819 /* NB: sowwakeup_locked() does an implicit unlock. */
7820 sowwakeup_locked(so);
7821 m_freem(mfree);
7822 if (SEQ_GT(th->th_ack, tp->snd_una)) {
7823 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
7824 }
7825 tp->snd_una = th->th_ack;
7826 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost));
7827 if (IN_RECOVERY(tp->t_flags)) {
7828 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
7829 (SEQ_LT(th->th_ack, tp->snd_max))) {
7830 tcp_bbr_partialack(tp);
7831 } else {
7832 bbr_post_recovery(tp);
7833 }
7834 }
7835 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
7836 tp->snd_recover = tp->snd_una;
7837 }
7838 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
7839 tp->snd_nxt = tp->snd_max;
7840 }
7841 if (tp->snd_una == tp->snd_max) {
7842 /* Nothing left outstanding */
7843 nothing_left:
7844 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
7845 if (sbavail(&so->so_snd) == 0)
7846 bbr->rc_tp->t_acktime = 0;
7847 if ((sbused(&so->so_snd) == 0) &&
7848 (tp->t_flags & TF_SENTFIN)) {
7849 ourfinisacked = 1;
7850 }
7851 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
7852 if (bbr->rc_in_persist == 0) {
7853 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
7854 }
7855 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
7856 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
7857 /*
7858 * We invalidate the last ack here since we
7859 * don't want to transfer forward the time
7860 * for our sum's calculations.
7861 */
7862 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7863 (sbavail(&so->so_snd) == 0) &&
7864 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
7865 /*
7866 * The socket was gone and the peer sent data, time
7867 * to reset him.
7868 */
7869 *ret_val = 1;
7870 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
7871 /* tcp_close will kill the inp pre-log the Reset */
7872 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
7873 tp = tcp_close(tp);
7874 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
7875 BBR_STAT_INC(bbr_dropped_af_data);
7876 return (1);
7877 }
7878 /* Set need output so persist might get set */
7879 bbr->r_wanted_output = 1;
7880 }
7881 if (ofia)
7882 *ofia = ourfinisacked;
7883 return (0);
7884 }
7885
7886 static void
bbr_enter_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)7887 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
7888 {
7889 if (bbr->rc_in_persist == 0) {
7890 bbr_timer_cancel(bbr, __LINE__, cts);
7891 bbr->r_ctl.rc_last_delay_val = 0;
7892 tp->t_rxtshift = 0;
7893 bbr->rc_in_persist = 1;
7894 bbr->r_ctl.rc_went_idle_time = cts;
7895 /* We should be capped when rw went to 0 but just in case */
7896 bbr_log_type_pesist(bbr, cts, 0, line, 1);
7897 /* Time freezes for the state, so do the accounting now */
7898 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
7899 uint32_t time_in;
7900
7901 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
7902 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
7903 int32_t idx;
7904
7905 idx = bbr_state_val(bbr);
7906 counter_u64_add(bbr_state_time[(idx + 5)], time_in);
7907 } else {
7908 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
7909 }
7910 }
7911 bbr->r_ctl.rc_bbr_state_time = cts;
7912 }
7913 }
7914
7915 static void
bbr_restart_after_idle(struct tcp_bbr * bbr,uint32_t cts,uint32_t idle_time)7916 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time)
7917 {
7918 /*
7919 * Note that if idle time does not exceed our
7920 * threshold, we do nothing continuing the state
7921 * transitions we were last walking through.
7922 */
7923 if (idle_time >= bbr_idle_restart_threshold) {
7924 if (bbr->rc_use_idle_restart) {
7925 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT;
7926 /*
7927 * Set our target using BBR_UNIT, so
7928 * we increase at a dramatic rate but
7929 * we stop when we get the pipe
7930 * full again for our current b/w estimate.
7931 */
7932 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
7933 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
7934 bbr_set_state_target(bbr, __LINE__);
7935 /* Now setup our gains to ramp up */
7936 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
7937 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
7938 bbr_log_type_statechange(bbr, cts, __LINE__);
7939 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
7940 bbr_substate_change(bbr, cts, __LINE__, 1);
7941 }
7942 }
7943 }
7944
7945 static void
bbr_exit_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)7946 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
7947 {
7948 uint32_t idle_time;
7949
7950 if (bbr->rc_in_persist == 0)
7951 return;
7952 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time);
7953 bbr->rc_in_persist = 0;
7954 bbr->rc_hit_state_1 = 0;
7955 bbr->r_ctl.rc_del_time = cts;
7956 /*
7957 * We invalidate the last ack here since we
7958 * don't want to transfer forward the time
7959 * for our sum's calculations.
7960 */
7961 if (tcp_in_hpts(bbr->rc_tp)) {
7962 tcp_hpts_remove(bbr->rc_tp);
7963 bbr->rc_timer_first = 0;
7964 bbr->r_ctl.rc_hpts_flags = 0;
7965 bbr->r_ctl.rc_last_delay_val = 0;
7966 bbr->r_ctl.rc_hptsi_agg_delay = 0;
7967 bbr->r_agg_early_set = 0;
7968 bbr->r_ctl.rc_agg_early = 0;
7969 }
7970 bbr_log_type_pesist(bbr, cts, idle_time, line, 0);
7971 if (idle_time >= bbr_rtt_probe_time) {
7972 /*
7973 * This qualifies as a RTT_PROBE session since we drop the
7974 * data outstanding to nothing and waited more than
7975 * bbr_rtt_probe_time.
7976 */
7977 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0);
7978 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts;
7979 }
7980 tp->t_rxtshift = 0;
7981 /*
7982 * If in probeBW and we have persisted more than an RTT lets do
7983 * special handling.
7984 */
7985 /* Force a time based epoch */
7986 bbr_set_epoch(bbr, cts, __LINE__);
7987 /*
7988 * Setup the lost so we don't count anything against the guy
7989 * we have been stuck with during persists.
7990 */
7991 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
7992 /* Time un-freezes for the state */
7993 bbr->r_ctl.rc_bbr_state_time = cts;
7994 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) ||
7995 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) {
7996 /*
7997 * If we are going back to probe-bw
7998 * or probe_rtt, we may need to possibly
7999 * do a fast restart.
8000 */
8001 bbr_restart_after_idle(bbr, cts, idle_time);
8002 }
8003 }
8004
8005 static void
bbr_collapsed_window(struct tcp_bbr * bbr)8006 bbr_collapsed_window(struct tcp_bbr *bbr)
8007 {
8008 /*
8009 * Now we must walk the
8010 * send map and divide the
8011 * ones left stranded. These
8012 * guys can't cause us to abort
8013 * the connection and are really
8014 * "unsent". However if a buggy
8015 * client actually did keep some
8016 * of the data i.e. collapsed the win
8017 * and refused to ack and then opened
8018 * the win and acked that data. We would
8019 * get into an ack war, the simplier
8020 * method then of just pretending we
8021 * did not send those segments something
8022 * won't work.
8023 */
8024 struct bbr_sendmap *rsm, *nrsm;
8025 tcp_seq max_seq;
8026 uint32_t maxseg;
8027 int can_split = 0;
8028 int fnd = 0;
8029
8030 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
8031 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd;
8032 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0);
8033 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
8034 /* Find the first seq past or at maxseq */
8035 if (rsm->r_flags & BBR_RWND_COLLAPSED)
8036 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8037 if (SEQ_GEQ(max_seq, rsm->r_start) &&
8038 SEQ_GEQ(rsm->r_end, max_seq)) {
8039 fnd = 1;
8040 break;
8041 }
8042 }
8043 bbr->rc_has_collapsed = 0;
8044 if (!fnd) {
8045 /* Nothing to do strange */
8046 return;
8047 }
8048 /*
8049 * Now can we split?
8050 *
8051 * We don't want to split if splitting
8052 * would generate too many small segments
8053 * less we let an attacker fragment our
8054 * send_map and leave us out of memory.
8055 */
8056 if ((max_seq != rsm->r_start) &&
8057 (max_seq != rsm->r_end)){
8058 /* can we split? */
8059 int res1, res2;
8060
8061 res1 = max_seq - rsm->r_start;
8062 res2 = rsm->r_end - max_seq;
8063 if ((res1 >= (maxseg/8)) &&
8064 (res2 >= (maxseg/8))) {
8065 /* No small pieces here */
8066 can_split = 1;
8067 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) {
8068 /* We are under the limit */
8069 can_split = 1;
8070 }
8071 }
8072 /* Ok do we need to split this rsm? */
8073 if (max_seq == rsm->r_start) {
8074 /* It's this guy no split required */
8075 nrsm = rsm;
8076 } else if (max_seq == rsm->r_end) {
8077 /* It's the next one no split required. */
8078 nrsm = TAILQ_NEXT(rsm, r_next);
8079 if (nrsm == NULL) {
8080 /* Huh? */
8081 return;
8082 }
8083 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) {
8084 /* yep we need to split it */
8085 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
8086 if (nrsm == NULL) {
8087 /* failed XXXrrs what can we do mark the whole? */
8088 nrsm = rsm;
8089 goto no_split;
8090 }
8091 /* Clone it */
8092 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0);
8093 bbr_clone_rsm(bbr, nrsm, rsm, max_seq);
8094 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
8095 if (rsm->r_in_tmap) {
8096 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8097 nrsm->r_in_tmap = 1;
8098 }
8099 } else {
8100 /*
8101 * Split not allowed just start here just
8102 * use this guy.
8103 */
8104 nrsm = rsm;
8105 }
8106 no_split:
8107 BBR_STAT_INC(bbr_collapsed_win);
8108 /* reuse fnd as a count */
8109 fnd = 0;
8110 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) {
8111 nrsm->r_flags |= BBR_RWND_COLLAPSED;
8112 fnd++;
8113 bbr->rc_has_collapsed = 1;
8114 }
8115 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd);
8116 }
8117
8118 static void
bbr_un_collapse_window(struct tcp_bbr * bbr)8119 bbr_un_collapse_window(struct tcp_bbr *bbr)
8120 {
8121 struct bbr_sendmap *rsm;
8122 int cleared = 0;
8123
8124 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
8125 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
8126 /* Clear the flag */
8127 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8128 cleared++;
8129 } else
8130 break;
8131 }
8132 bbr_log_type_rwnd_collapse(bbr,
8133 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared);
8134 bbr->rc_has_collapsed = 0;
8135 }
8136
8137 /*
8138 * Return value of 1, the TCB is unlocked and most
8139 * likely gone, return value of 0, the TCB is still
8140 * locked.
8141 */
8142 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)8143 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8144 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8145 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8146 {
8147 /*
8148 * Update window information. Don't look at window if no ACK: TAC's
8149 * send garbage on first SYN.
8150 */
8151 uint16_t nsegs;
8152 int32_t tfo_syn;
8153 struct tcp_bbr *bbr;
8154
8155 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8156 INP_WLOCK_ASSERT(tptoinpcb(tp));
8157 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8158 if ((thflags & TH_ACK) &&
8159 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8160 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8161 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8162 /* keep track of pure window updates */
8163 if (tlen == 0 &&
8164 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8165 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8166 tp->snd_wnd = tiwin;
8167 tp->snd_wl1 = th->th_seq;
8168 tp->snd_wl2 = th->th_ack;
8169 if (tp->snd_wnd > tp->max_sndwnd)
8170 tp->max_sndwnd = tp->snd_wnd;
8171 bbr->r_wanted_output = 1;
8172 } else if (thflags & TH_ACK) {
8173 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8174 tp->snd_wnd = tiwin;
8175 tp->snd_wl1 = th->th_seq;
8176 tp->snd_wl2 = th->th_ack;
8177 }
8178 }
8179 if (tp->snd_wnd < ctf_outstanding(tp))
8180 /* The peer collapsed its window on us */
8181 bbr_collapsed_window(bbr);
8182 else if (bbr->rc_has_collapsed)
8183 bbr_un_collapse_window(bbr);
8184 /* Was persist timer active and now we have window space? */
8185 if ((bbr->rc_in_persist != 0) &&
8186 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8187 bbr_minseg(bbr)))) {
8188 /*
8189 * Make the rate persist at end of persist mode if idle long
8190 * enough
8191 */
8192 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8193
8194 /* Make sure we output to start the timer */
8195 bbr->r_wanted_output = 1;
8196 }
8197 /* Do we need to enter persist? */
8198 if ((bbr->rc_in_persist == 0) &&
8199 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8200 TCPS_HAVEESTABLISHED(tp->t_state) &&
8201 (tp->snd_max == tp->snd_una) &&
8202 sbavail(&so->so_snd) &&
8203 (sbavail(&so->so_snd) > tp->snd_wnd)) {
8204 /* No send window.. we must enter persist */
8205 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8206 }
8207 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8208 m_freem(m);
8209 return (0);
8210 }
8211 /*
8212 * We don't support urgent data but
8213 * drag along the up just to make sure
8214 * if there is a stack switch no one
8215 * is surprised.
8216 */
8217 tp->rcv_up = tp->rcv_nxt;
8218
8219 /*
8220 * Process the segment text, merging it into the TCP sequencing
8221 * queue, and arranging for acknowledgment of receipt if necessary.
8222 * This process logically involves adjusting tp->rcv_wnd as data is
8223 * presented to the user (this happens in tcp_usrreq.c, case
8224 * PRU_RCVD). If a FIN has already been received on this connection
8225 * then we just ignore the text.
8226 */
8227 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8228 IS_FASTOPEN(tp->t_flags));
8229 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
8230 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8231 tcp_seq save_start = th->th_seq;
8232 tcp_seq save_rnxt = tp->rcv_nxt;
8233 int save_tlen = tlen;
8234
8235 m_adj(m, drop_hdrlen); /* delayed header drop */
8236 /*
8237 * Insert segment which includes th into TCP reassembly
8238 * queue with control block tp. Set thflags to whether
8239 * reassembly now includes a segment with FIN. This handles
8240 * the common case inline (segment is the next to be
8241 * received on an established connection, and the queue is
8242 * empty), avoiding linkage into and removal from the queue
8243 * and repetition of various conversions. Set DELACK for
8244 * segments received in order, but ack immediately when
8245 * segments are out of order (so fast retransmit can work).
8246 */
8247 if (th->th_seq == tp->rcv_nxt &&
8248 SEGQ_EMPTY(tp) &&
8249 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8250 tfo_syn)) {
8251 #ifdef NETFLIX_SB_LIMITS
8252 u_int mcnt, appended;
8253
8254 if (so->so_rcv.sb_shlim) {
8255 mcnt = m_memcnt(m);
8256 appended = 0;
8257 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8258 CFO_NOSLEEP, NULL) == false) {
8259 counter_u64_add(tcp_sb_shlim_fails, 1);
8260 m_freem(m);
8261 return (0);
8262 }
8263 }
8264
8265 #endif
8266 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) {
8267 bbr->bbr_segs_rcvd += max(1, nsegs);
8268 tp->t_flags |= TF_DELACK;
8269 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8270 } else {
8271 bbr->r_wanted_output = 1;
8272 tp->t_flags |= TF_ACKNOW;
8273 }
8274 tp->rcv_nxt += tlen;
8275 if (tlen &&
8276 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8277 (tp->t_fbyte_in == 0)) {
8278 tp->t_fbyte_in = ticks;
8279 if (tp->t_fbyte_in == 0)
8280 tp->t_fbyte_in = 1;
8281 if (tp->t_fbyte_out && tp->t_fbyte_in)
8282 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8283 }
8284 thflags = tcp_get_flags(th) & TH_FIN;
8285 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8286 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8287 SOCKBUF_LOCK(&so->so_rcv);
8288 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
8289 m_freem(m);
8290 else
8291 #ifdef NETFLIX_SB_LIMITS
8292 appended =
8293 #endif
8294 sbappendstream_locked(&so->so_rcv, m, 0);
8295 /* NB: sorwakeup_locked() does an implicit unlock. */
8296 sorwakeup_locked(so);
8297 #ifdef NETFLIX_SB_LIMITS
8298 if (so->so_rcv.sb_shlim && appended != mcnt)
8299 counter_fo_release(so->so_rcv.sb_shlim,
8300 mcnt - appended);
8301 #endif
8302
8303 } else {
8304 /*
8305 * XXX: Due to the header drop above "th" is
8306 * theoretically invalid by now. Fortunately
8307 * m_adj() doesn't actually frees any mbufs when
8308 * trimming from the head.
8309 */
8310 tcp_seq temp = save_start;
8311
8312 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8313 tp->t_flags |= TF_ACKNOW;
8314 if (tp->t_flags & TF_WAKESOR) {
8315 tp->t_flags &= ~TF_WAKESOR;
8316 /* NB: sorwakeup_locked() does an implicit unlock. */
8317 sorwakeup_locked(so);
8318 }
8319 }
8320 if ((tp->t_flags & TF_SACK_PERMIT) &&
8321 (save_tlen > 0) &&
8322 TCPS_HAVEESTABLISHED(tp->t_state)) {
8323 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8324 /*
8325 * DSACK actually handled in the fastpath
8326 * above.
8327 */
8328 tcp_update_sack_list(tp, save_start,
8329 save_start + save_tlen);
8330 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8331 if ((tp->rcv_numsacks >= 1) &&
8332 (tp->sackblks[0].end == save_start)) {
8333 /*
8334 * Partial overlap, recorded at todrop
8335 * above.
8336 */
8337 tcp_update_sack_list(tp,
8338 tp->sackblks[0].start,
8339 tp->sackblks[0].end);
8340 } else {
8341 tcp_update_dsack_list(tp, save_start,
8342 save_start + save_tlen);
8343 }
8344 } else if (tlen >= save_tlen) {
8345 /* Update of sackblks. */
8346 tcp_update_dsack_list(tp, save_start,
8347 save_start + save_tlen);
8348 } else if (tlen > 0) {
8349 tcp_update_dsack_list(tp, save_start,
8350 save_start + tlen);
8351 }
8352 }
8353 } else {
8354 m_freem(m);
8355 thflags &= ~TH_FIN;
8356 }
8357
8358 /*
8359 * If FIN is received ACK the FIN and let the user know that the
8360 * connection is closing.
8361 */
8362 if (thflags & TH_FIN) {
8363 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8364 /* The socket upcall is handled by socantrcvmore. */
8365 socantrcvmore(so);
8366 /*
8367 * If connection is half-synchronized (ie NEEDSYN
8368 * flag on) then delay ACK, so it may be piggybacked
8369 * when SYN is sent. Otherwise, since we received a
8370 * FIN then no more input can be expected, send ACK
8371 * now.
8372 */
8373 if (tp->t_flags & TF_NEEDSYN) {
8374 tp->t_flags |= TF_DELACK;
8375 bbr_timer_cancel(bbr,
8376 __LINE__, bbr->r_ctl.rc_rcvtime);
8377 } else {
8378 tp->t_flags |= TF_ACKNOW;
8379 }
8380 tp->rcv_nxt++;
8381 }
8382 switch (tp->t_state) {
8383 /*
8384 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8385 * CLOSE_WAIT state.
8386 */
8387 case TCPS_SYN_RECEIVED:
8388 tp->t_starttime = ticks;
8389 /* FALLTHROUGH */
8390 case TCPS_ESTABLISHED:
8391 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8392 break;
8393
8394 /*
8395 * If still in FIN_WAIT_1 STATE FIN has not been
8396 * acked so enter the CLOSING state.
8397 */
8398 case TCPS_FIN_WAIT_1:
8399 tcp_state_change(tp, TCPS_CLOSING);
8400 break;
8401
8402 /*
8403 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8404 * starting the time-wait timer, turning off the
8405 * other standard timers.
8406 */
8407 case TCPS_FIN_WAIT_2:
8408 bbr->rc_timer_first = 1;
8409 bbr_timer_cancel(bbr,
8410 __LINE__, bbr->r_ctl.rc_rcvtime);
8411 tcp_twstart(tp);
8412 return (1);
8413 }
8414 }
8415 /*
8416 * Return any desired output.
8417 */
8418 if ((tp->t_flags & TF_ACKNOW) ||
8419 (sbavail(&so->so_snd) > ctf_outstanding(tp))) {
8420 bbr->r_wanted_output = 1;
8421 }
8422 return (0);
8423 }
8424
8425 /*
8426 * Here nothing is really faster, its just that we
8427 * have broken out the fast-data path also just like
8428 * the fast-ack. Return 1 if we processed the packet
8429 * return 0 if you need to take the "slow-path".
8430 */
8431 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)8432 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8433 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8434 uint32_t tiwin, int32_t nxt_pkt)
8435 {
8436 uint16_t nsegs;
8437 int32_t newsize = 0; /* automatic sockbuf scaling */
8438 struct tcp_bbr *bbr;
8439 #ifdef NETFLIX_SB_LIMITS
8440 u_int mcnt, appended;
8441 #endif
8442
8443 /* On the hpts and we would have called output */
8444 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8445
8446 /*
8447 * If last ACK falls within this segment's sequence numbers, record
8448 * the timestamp. NOTE that the test is modified according to the
8449 * latest proposal of the [email protected] list (Braden 1993/04/26).
8450 */
8451 if (bbr->r_ctl.rc_resend != NULL) {
8452 return (0);
8453 }
8454 if (tiwin && tiwin != tp->snd_wnd) {
8455 return (0);
8456 }
8457 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8458 return (0);
8459 }
8460 if (__predict_false((to->to_flags & TOF_TS) &&
8461 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8462 return (0);
8463 }
8464 if (__predict_false((th->th_ack != tp->snd_una))) {
8465 return (0);
8466 }
8467 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8468 return (0);
8469 }
8470 if ((to->to_flags & TOF_TS) != 0 &&
8471 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8472 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
8473 tp->ts_recent = to->to_tsval;
8474 }
8475 /*
8476 * This is a pure, in-sequence data packet with nothing on the
8477 * reassembly queue and we have enough buffer space to take it.
8478 */
8479 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8480
8481 #ifdef NETFLIX_SB_LIMITS
8482 if (so->so_rcv.sb_shlim) {
8483 mcnt = m_memcnt(m);
8484 appended = 0;
8485 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8486 CFO_NOSLEEP, NULL) == false) {
8487 counter_u64_add(tcp_sb_shlim_fails, 1);
8488 m_freem(m);
8489 return (1);
8490 }
8491 }
8492 #endif
8493 /* Clean receiver SACK report if present */
8494 if (tp->rcv_numsacks)
8495 tcp_clean_sackreport(tp);
8496 KMOD_TCPSTAT_INC(tcps_preddat);
8497 tp->rcv_nxt += tlen;
8498 if (tlen &&
8499 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8500 (tp->t_fbyte_in == 0)) {
8501 tp->t_fbyte_in = ticks;
8502 if (tp->t_fbyte_in == 0)
8503 tp->t_fbyte_in = 1;
8504 if (tp->t_fbyte_out && tp->t_fbyte_in)
8505 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8506 }
8507 /*
8508 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8509 */
8510 tp->snd_wl1 = th->th_seq;
8511 /*
8512 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8513 */
8514 tp->rcv_up = tp->rcv_nxt;
8515 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8516 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8517 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8518
8519 /* Add data to socket buffer. */
8520 SOCKBUF_LOCK(&so->so_rcv);
8521 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8522 m_freem(m);
8523 } else {
8524 /*
8525 * Set new socket buffer size. Give up when limit is
8526 * reached.
8527 */
8528 if (newsize)
8529 if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
8530 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8531 m_adj(m, drop_hdrlen); /* delayed header drop */
8532
8533 #ifdef NETFLIX_SB_LIMITS
8534 appended =
8535 #endif
8536 sbappendstream_locked(&so->so_rcv, m, 0);
8537 ctf_calc_rwin(so, tp);
8538 }
8539 /* NB: sorwakeup_locked() does an implicit unlock. */
8540 sorwakeup_locked(so);
8541 #ifdef NETFLIX_SB_LIMITS
8542 if (so->so_rcv.sb_shlim && mcnt != appended)
8543 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8544 #endif
8545 if (DELAY_ACK(tp, bbr, nsegs)) {
8546 bbr->bbr_segs_rcvd += max(1, nsegs);
8547 tp->t_flags |= TF_DELACK;
8548 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8549 } else {
8550 bbr->r_wanted_output = 1;
8551 tp->t_flags |= TF_ACKNOW;
8552 }
8553 return (1);
8554 }
8555
8556 /*
8557 * This subfunction is used to try to highly optimize the
8558 * fast path. We again allow window updates that are
8559 * in sequence to remain in the fast-path. We also add
8560 * in the __predict's to attempt to help the compiler.
8561 * Note that if we return a 0, then we can *not* process
8562 * it and the caller should push the packet into the
8563 * slow-path. If we return 1, then all is well and
8564 * the packet is fully processed.
8565 */
8566 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)8567 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8568 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8569 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
8570 {
8571 int32_t acked;
8572 uint16_t nsegs;
8573 uint32_t sack_changed;
8574 uint32_t prev_acked = 0;
8575 struct tcp_bbr *bbr;
8576
8577 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8578 /* Old ack, behind (or duplicate to) the last one rcv'd */
8579 return (0);
8580 }
8581 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8582 /* Above what we have sent? */
8583 return (0);
8584 }
8585 if (__predict_false(tiwin == 0)) {
8586 /* zero window */
8587 return (0);
8588 }
8589 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8590 /* We need a SYN or a FIN, unlikely.. */
8591 return (0);
8592 }
8593 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8594 /* Timestamp is behind .. old ack with seq wrap? */
8595 return (0);
8596 }
8597 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8598 /* Still recovering */
8599 return (0);
8600 }
8601 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8602 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) {
8603 /* We are retransmitting */
8604 return (0);
8605 }
8606 if (__predict_false(bbr->rc_in_persist != 0)) {
8607 /* In persist mode */
8608 return (0);
8609 }
8610 if (bbr->r_ctl.rc_sacked) {
8611 /* We have sack holes on our scoreboard */
8612 return (0);
8613 }
8614 /* Ok if we reach here, we can process a fast-ack */
8615 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8616 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
8617 /*
8618 * We never detect loss in fast ack [we can't
8619 * have a sack and can't be in recovery so
8620 * we always pass 0 (nothing detected)].
8621 */
8622 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0);
8623 /* Did the window get updated? */
8624 if (tiwin != tp->snd_wnd) {
8625 tp->snd_wnd = tiwin;
8626 tp->snd_wl1 = th->th_seq;
8627 if (tp->snd_wnd > tp->max_sndwnd)
8628 tp->max_sndwnd = tp->snd_wnd;
8629 }
8630 /* Do we need to exit persists? */
8631 if ((bbr->rc_in_persist != 0) &&
8632 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8633 bbr_minseg(bbr)))) {
8634 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8635 bbr->r_wanted_output = 1;
8636 }
8637 /* Do we need to enter persists? */
8638 if ((bbr->rc_in_persist == 0) &&
8639 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8640 TCPS_HAVEESTABLISHED(tp->t_state) &&
8641 (tp->snd_max == tp->snd_una) &&
8642 sbavail(&so->so_snd) &&
8643 (sbavail(&so->so_snd) > tp->snd_wnd)) {
8644 /* No send window.. we must enter persist */
8645 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8646 }
8647 /*
8648 * If last ACK falls within this segment's sequence numbers, record
8649 * the timestamp. NOTE that the test is modified according to the
8650 * latest proposal of the [email protected] list (Braden 1993/04/26).
8651 */
8652 if ((to->to_flags & TOF_TS) != 0 &&
8653 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8654 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime;
8655 tp->ts_recent = to->to_tsval;
8656 }
8657 /*
8658 * This is a pure ack for outstanding data.
8659 */
8660 KMOD_TCPSTAT_INC(tcps_predack);
8661
8662 /*
8663 * "bad retransmit" recovery.
8664 */
8665 if (tp->t_flags & TF_PREVVALID) {
8666 tp->t_flags &= ~TF_PREVVALID;
8667 if (tp->t_rxtshift == 1 &&
8668 (int)(ticks - tp->t_badrxtwin) < 0)
8669 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
8670 }
8671 /*
8672 * Recalculate the transmit timer / rtt.
8673 *
8674 * Some boxes send broken timestamp replies during the SYN+ACK
8675 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8676 * and blow up the retransmit timer.
8677 */
8678 acked = BYTES_THIS_ACK(tp, th);
8679
8680 #ifdef TCP_HHOOK
8681 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
8682 hhook_run_tcp_est_in(tp, th, to);
8683 #endif
8684
8685 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
8686 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8687 sbdrop(&so->so_snd, acked);
8688
8689 if (SEQ_GT(th->th_ack, tp->snd_una))
8690 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8691 tp->snd_una = th->th_ack;
8692 if (tp->snd_wnd < ctf_outstanding(tp))
8693 /* The peer collapsed its window on us */
8694 bbr_collapsed_window(bbr);
8695 else if (bbr->rc_has_collapsed)
8696 bbr_un_collapse_window(bbr);
8697
8698 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8699 tp->snd_recover = tp->snd_una;
8700 }
8701 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0);
8702 /*
8703 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
8704 */
8705 tp->snd_wl2 = th->th_ack;
8706 m_freem(m);
8707 /*
8708 * If all outstanding data are acked, stop retransmit timer,
8709 * otherwise restart timer using current (possibly backed-off)
8710 * value. If process is waiting for space, wakeup/selwakeup/signal.
8711 * If data are ready to send, let tcp_output decide between more
8712 * output or persist.
8713 * Wake up the socket if we have room to write more.
8714 */
8715 sowwakeup(so);
8716 if (tp->snd_una == tp->snd_max) {
8717 /* Nothing left outstanding */
8718 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8719 if (sbavail(&so->so_snd) == 0)
8720 bbr->rc_tp->t_acktime = 0;
8721 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8722 if (bbr->rc_in_persist == 0) {
8723 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8724 }
8725 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8726 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8727 /*
8728 * We invalidate the last ack here since we
8729 * don't want to transfer forward the time
8730 * for our sum's calculations.
8731 */
8732 bbr->r_wanted_output = 1;
8733 }
8734 if (sbavail(&so->so_snd)) {
8735 bbr->r_wanted_output = 1;
8736 }
8737 return (1);
8738 }
8739
8740 /*
8741 * Return value of 1, the TCB is unlocked and most
8742 * likely gone, return value of 0, the TCB is still
8743 * locked.
8744 */
8745 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)8746 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
8747 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8748 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8749 {
8750 int32_t todrop;
8751 int32_t ourfinisacked = 0;
8752 struct tcp_bbr *bbr;
8753 int32_t ret_val = 0;
8754
8755 INP_WLOCK_ASSERT(tptoinpcb(tp));
8756
8757 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8758 ctf_calc_rwin(so, tp);
8759 /*
8760 * If the state is SYN_SENT: if seg contains an ACK, but not for our
8761 * SYN, drop the input. if seg contains a RST, then drop the
8762 * connection. if seg does not contain SYN, then drop it. Otherwise
8763 * this is an acceptable SYN segment initialize tp->rcv_nxt and
8764 * tp->irs if seg contains ack then advance tp->snd_una. BRR does
8765 * not support ECN so we will not say we are capable. if SYN has
8766 * been acked change to ESTABLISHED else SYN_RCVD state arrange for
8767 * segment to be acked (eventually) continue processing rest of
8768 * data/controls, beginning with URG
8769 */
8770 if ((thflags & TH_ACK) &&
8771 (SEQ_LEQ(th->th_ack, tp->iss) ||
8772 SEQ_GT(th->th_ack, tp->snd_max))) {
8773 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8774 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8775 return (1);
8776 }
8777 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
8778 TCP_PROBE5(connect__refused, NULL, tp,
8779 mtod(m, const char *), tp, th);
8780 tp = tcp_drop(tp, ECONNREFUSED);
8781 ctf_do_drop(m, tp);
8782 return (1);
8783 }
8784 if (thflags & TH_RST) {
8785 ctf_do_drop(m, tp);
8786 return (1);
8787 }
8788 if (!(thflags & TH_SYN)) {
8789 ctf_do_drop(m, tp);
8790 return (1);
8791 }
8792 tp->irs = th->th_seq;
8793 tcp_rcvseqinit(tp);
8794 if (thflags & TH_ACK) {
8795 int tfo_partial = 0;
8796
8797 KMOD_TCPSTAT_INC(tcps_connects);
8798 soisconnected(so);
8799 #ifdef MAC
8800 mac_socketpeer_set_from_mbuf(m, so);
8801 #endif
8802 /* Do window scaling on this connection? */
8803 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
8804 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
8805 tp->rcv_scale = tp->request_r_scale;
8806 }
8807 tp->rcv_adv += min(tp->rcv_wnd,
8808 TCP_MAXWIN << tp->rcv_scale);
8809 /*
8810 * If not all the data that was sent in the TFO SYN
8811 * has been acked, resend the remainder right away.
8812 */
8813 if (IS_FASTOPEN(tp->t_flags) &&
8814 (tp->snd_una != tp->snd_max)) {
8815 tp->snd_nxt = th->th_ack;
8816 tfo_partial = 1;
8817 }
8818 /*
8819 * If there's data, delay ACK; if there's also a FIN ACKNOW
8820 * will be turned on later.
8821 */
8822 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) {
8823 bbr->bbr_segs_rcvd += 1;
8824 tp->t_flags |= TF_DELACK;
8825 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8826 } else {
8827 bbr->r_wanted_output = 1;
8828 tp->t_flags |= TF_ACKNOW;
8829 }
8830 if (SEQ_GT(th->th_ack, tp->iss)) {
8831 /*
8832 * The SYN is acked
8833 * handle it specially.
8834 */
8835 bbr_log_syn(tp, to);
8836 }
8837 if (SEQ_GT(th->th_ack, tp->snd_una)) {
8838 /*
8839 * We advance snd_una for the
8840 * fast open case. If th_ack is
8841 * acknowledging data beyond
8842 * snd_una we can't just call
8843 * ack-processing since the
8844 * data stream in our send-map
8845 * will start at snd_una + 1 (one
8846 * beyond the SYN). If its just
8847 * equal we don't need to do that
8848 * and there is no send_map.
8849 */
8850 tp->snd_una++;
8851 }
8852 /*
8853 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
8854 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
8855 */
8856 tp->t_starttime = ticks;
8857 if (tp->t_flags & TF_NEEDFIN) {
8858 tcp_state_change(tp, TCPS_FIN_WAIT_1);
8859 tp->t_flags &= ~TF_NEEDFIN;
8860 thflags &= ~TH_SYN;
8861 } else {
8862 tcp_state_change(tp, TCPS_ESTABLISHED);
8863 TCP_PROBE5(connect__established, NULL, tp,
8864 mtod(m, const char *), tp, th);
8865 cc_conn_init(tp);
8866 }
8867 } else {
8868 /*
8869 * Received initial SYN in SYN-SENT[*] state => simultaneous
8870 * open. If segment contains CC option and there is a
8871 * cached CC, apply TAO test. If it succeeds, connection is *
8872 * half-synchronized. Otherwise, do 3-way handshake:
8873 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
8874 * there was no CC option, clear cached CC value.
8875 */
8876 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
8877 tcp_state_change(tp, TCPS_SYN_RECEIVED);
8878 }
8879 /*
8880 * Advance th->th_seq to correspond to first data byte. If data,
8881 * trim to stay within window, dropping FIN if necessary.
8882 */
8883 th->th_seq++;
8884 if (tlen > tp->rcv_wnd) {
8885 todrop = tlen - tp->rcv_wnd;
8886 m_adj(m, -todrop);
8887 tlen = tp->rcv_wnd;
8888 thflags &= ~TH_FIN;
8889 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
8890 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
8891 }
8892 tp->snd_wl1 = th->th_seq - 1;
8893 tp->rcv_up = th->th_seq;
8894 /*
8895 * Client side of transaction: already sent SYN and data. If the
8896 * remote host used T/TCP to validate the SYN, our data will be
8897 * ACK'd; if so, enter normal data segment processing in the middle
8898 * of step 5, ack processing. Otherwise, goto step 6.
8899 */
8900 if (thflags & TH_ACK) {
8901 if ((to->to_flags & TOF_TS) != 0) {
8902 uint32_t t, rtt;
8903
8904 t = tcp_tv_to_mssectick(&bbr->rc_tv);
8905 if (TSTMP_GEQ(t, to->to_tsecr)) {
8906 rtt = t - to->to_tsecr;
8907 if (rtt == 0) {
8908 rtt = 1;
8909 }
8910 rtt *= MS_IN_USEC;
8911 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
8912 apply_filter_min_small(&bbr->r_ctl.rc_rttprop,
8913 rtt, bbr->r_ctl.rc_rcvtime);
8914 }
8915 }
8916 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
8917 return (ret_val);
8918 /* We may have changed to FIN_WAIT_1 above */
8919 if (tp->t_state == TCPS_FIN_WAIT_1) {
8920 /*
8921 * In FIN_WAIT_1 STATE in addition to the processing
8922 * for the ESTABLISHED state if our FIN is now
8923 * acknowledged then enter FIN_WAIT_2.
8924 */
8925 if (ourfinisacked) {
8926 /*
8927 * If we can't receive any more data, then
8928 * closing user can proceed. Starting the
8929 * timer is contrary to the specification,
8930 * but if we don't get a FIN we'll hang
8931 * forever.
8932 *
8933 * XXXjl: we should release the tp also, and
8934 * use a compressed state.
8935 */
8936 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8937 soisdisconnected(so);
8938 tcp_timer_activate(tp, TT_2MSL,
8939 (tcp_fast_finwait2_recycle ?
8940 tcp_finwait2_timeout :
8941 TP_MAXIDLE(tp)));
8942 }
8943 tcp_state_change(tp, TCPS_FIN_WAIT_2);
8944 }
8945 }
8946 }
8947 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
8948 tiwin, thflags, nxt_pkt));
8949 }
8950
8951 /*
8952 * Return value of 1, the TCB is unlocked and most
8953 * likely gone, return value of 0, the TCB is still
8954 * locked.
8955 */
8956 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)8957 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
8958 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8959 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8960 {
8961 int32_t ourfinisacked = 0;
8962 int32_t ret_val;
8963 struct tcp_bbr *bbr;
8964
8965 INP_WLOCK_ASSERT(tptoinpcb(tp));
8966
8967 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8968 ctf_calc_rwin(so, tp);
8969 if ((thflags & TH_RST) ||
8970 (tp->t_fin_is_rst && (thflags & TH_FIN)))
8971 return (ctf_process_rst(m, th, so, tp));
8972 if ((thflags & TH_ACK) &&
8973 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
8974 SEQ_GT(th->th_ack, tp->snd_max))) {
8975 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8976 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8977 return (1);
8978 }
8979 if (IS_FASTOPEN(tp->t_flags)) {
8980 /*
8981 * When a TFO connection is in SYN_RECEIVED, the only valid
8982 * packets are the initial SYN, a retransmit/copy of the
8983 * initial SYN (possibly with a subset of the original
8984 * data), a valid ACK, a FIN, or a RST.
8985 */
8986 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
8987 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8988 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8989 return (1);
8990 } else if (thflags & TH_SYN) {
8991 /* non-initial SYN is ignored */
8992 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
8993 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
8994 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
8995 ctf_do_drop(m, NULL);
8996 return (0);
8997 }
8998 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
8999 ctf_do_drop(m, NULL);
9000 return (0);
9001 }
9002 }
9003 /*
9004 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9005 * it's less than ts_recent, drop it.
9006 */
9007 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9008 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9009 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9010 return (ret_val);
9011 }
9012 /*
9013 * In the SYN-RECEIVED state, validate that the packet belongs to
9014 * this connection before trimming the data to fit the receive
9015 * window. Check the sequence number versus IRS since we know the
9016 * sequence numbers haven't wrapped. This is a partial fix for the
9017 * "LAND" DoS attack.
9018 */
9019 if (SEQ_LT(th->th_seq, tp->irs)) {
9020 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9021 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9022 return (1);
9023 }
9024 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9025 return (ret_val);
9026 }
9027 /*
9028 * If last ACK falls within this segment's sequence numbers, record
9029 * its timestamp. NOTE: 1) That the test incorporates suggestions
9030 * from the latest proposal of the [email protected] list (Braden
9031 * 1993/04/26). 2) That updating only on newer timestamps interferes
9032 * with our earlier PAWS tests, so this check should be solely
9033 * predicated on the sequence space of this segment. 3) That we
9034 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9035 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9036 * SEG.Len, This modified check allows us to overcome RFC1323's
9037 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9038 * p.869. In such cases, we can still calculate the RTT correctly
9039 * when RCV.NXT == Last.ACK.Sent.
9040 */
9041 if ((to->to_flags & TOF_TS) != 0 &&
9042 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9043 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9044 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9045 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9046 tp->ts_recent = to->to_tsval;
9047 }
9048 tp->snd_wnd = tiwin;
9049 /*
9050 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9051 * is on (half-synchronized state), then queue data for later
9052 * processing; else drop segment and return.
9053 */
9054 if ((thflags & TH_ACK) == 0) {
9055 if (IS_FASTOPEN(tp->t_flags)) {
9056 cc_conn_init(tp);
9057 }
9058 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9059 tiwin, thflags, nxt_pkt));
9060 }
9061 KMOD_TCPSTAT_INC(tcps_connects);
9062 if (tp->t_flags & TF_SONOTCONN) {
9063 tp->t_flags &= ~TF_SONOTCONN;
9064 soisconnected(so);
9065 }
9066 /* Do window scaling? */
9067 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9068 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9069 tp->rcv_scale = tp->request_r_scale;
9070 }
9071 /*
9072 * ok for the first time in lets see if we can use the ts to figure
9073 * out what the initial RTT was.
9074 */
9075 if ((to->to_flags & TOF_TS) != 0) {
9076 uint32_t t, rtt;
9077
9078 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9079 if (TSTMP_GEQ(t, to->to_tsecr)) {
9080 rtt = t - to->to_tsecr;
9081 if (rtt == 0) {
9082 rtt = 1;
9083 }
9084 rtt *= MS_IN_USEC;
9085 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9086 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime);
9087 }
9088 }
9089 /* Drop off any SYN in the send map (probably not there) */
9090 if (thflags & TH_ACK)
9091 bbr_log_syn(tp, to);
9092 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
9093 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9094 tp->t_tfo_pending = NULL;
9095 }
9096 /*
9097 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9098 * FIN-WAIT-1
9099 */
9100 tp->t_starttime = ticks;
9101 if (tp->t_flags & TF_NEEDFIN) {
9102 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9103 tp->t_flags &= ~TF_NEEDFIN;
9104 } else {
9105 tcp_state_change(tp, TCPS_ESTABLISHED);
9106 TCP_PROBE5(accept__established, NULL, tp,
9107 mtod(m, const char *), tp, th);
9108 /*
9109 * TFO connections call cc_conn_init() during SYN
9110 * processing. Calling it again here for such connections
9111 * is not harmless as it would undo the snd_cwnd reduction
9112 * that occurs when a TFO SYN|ACK is retransmitted.
9113 */
9114 if (!IS_FASTOPEN(tp->t_flags))
9115 cc_conn_init(tp);
9116 }
9117 /*
9118 * Account for the ACK of our SYN prior to
9119 * regular ACK processing below, except for
9120 * simultaneous SYN, which is handled later.
9121 */
9122 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
9123 tp->snd_una++;
9124 /*
9125 * If segment contains data or ACK, will call tcp_reass() later; if
9126 * not, do so now to pass queued data to user.
9127 */
9128 if (tlen == 0 && (thflags & TH_FIN) == 0) {
9129 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9130 (struct mbuf *)0);
9131 if (tp->t_flags & TF_WAKESOR) {
9132 tp->t_flags &= ~TF_WAKESOR;
9133 /* NB: sorwakeup_locked() does an implicit unlock. */
9134 sorwakeup_locked(so);
9135 }
9136 }
9137 tp->snd_wl1 = th->th_seq - 1;
9138 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9139 return (ret_val);
9140 }
9141 if (tp->t_state == TCPS_FIN_WAIT_1) {
9142 /* We could have went to FIN_WAIT_1 (or EST) above */
9143 /*
9144 * In FIN_WAIT_1 STATE in addition to the processing for the
9145 * ESTABLISHED state if our FIN is now acknowledged then
9146 * enter FIN_WAIT_2.
9147 */
9148 if (ourfinisacked) {
9149 /*
9150 * If we can't receive any more data, then closing
9151 * user can proceed. Starting the timer is contrary
9152 * to the specification, but if we don't get a FIN
9153 * we'll hang forever.
9154 *
9155 * XXXjl: we should release the tp also, and use a
9156 * compressed state.
9157 */
9158 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9159 soisdisconnected(so);
9160 tcp_timer_activate(tp, TT_2MSL,
9161 (tcp_fast_finwait2_recycle ?
9162 tcp_finwait2_timeout :
9163 TP_MAXIDLE(tp)));
9164 }
9165 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9166 }
9167 }
9168 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9169 tiwin, thflags, nxt_pkt));
9170 }
9171
9172 /*
9173 * Return value of 1, the TCB is unlocked and most
9174 * likely gone, return value of 0, the TCB is still
9175 * locked.
9176 */
9177 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)9178 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9179 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9180 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9181 {
9182 struct tcp_bbr *bbr;
9183 int32_t ret_val;
9184
9185 INP_WLOCK_ASSERT(tptoinpcb(tp));
9186
9187 /*
9188 * Header prediction: check for the two common cases of a
9189 * uni-directional data xfer. If the packet has no control flags,
9190 * is in-sequence, the window didn't change and we're not
9191 * retransmitting, it's a candidate. If the length is zero and the
9192 * ack moved forward, we're the sender side of the xfer. Just free
9193 * the data acked & wake any higher level process that was blocked
9194 * waiting for space. If the length is non-zero and the ack didn't
9195 * move, we're the receiver side. If we're getting packets in-order
9196 * (the reassembly queue is empty), add the data toc The socket
9197 * buffer and note that we need a delayed ack. Make sure that the
9198 * hidden state-flags are also off. Since we check for
9199 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9200 */
9201 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9202 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) {
9203 /*
9204 * If we have delived under 4 segments increase the initial
9205 * window if raised by the peer. We use this to determine
9206 * dynamic and static rwnd's at the end of a connection.
9207 */
9208 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd);
9209 }
9210 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9211 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
9212 __predict_true(SEGQ_EMPTY(tp)) &&
9213 __predict_true(th->th_seq == tp->rcv_nxt)) {
9214 if (tlen == 0) {
9215 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9216 tiwin, nxt_pkt, iptos)) {
9217 return (0);
9218 }
9219 } else {
9220 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9221 tiwin, nxt_pkt)) {
9222 return (0);
9223 }
9224 }
9225 }
9226 ctf_calc_rwin(so, tp);
9227
9228 if ((thflags & TH_RST) ||
9229 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9230 return (ctf_process_rst(m, th, so, tp));
9231 /*
9232 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9233 * synchronized state.
9234 */
9235 if (thflags & TH_SYN) {
9236 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9237 return (ret_val);
9238 }
9239 /*
9240 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9241 * it's less than ts_recent, drop it.
9242 */
9243 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9244 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9245 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9246 return (ret_val);
9247 }
9248 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9249 return (ret_val);
9250 }
9251 /*
9252 * If last ACK falls within this segment's sequence numbers, record
9253 * its timestamp. NOTE: 1) That the test incorporates suggestions
9254 * from the latest proposal of the [email protected] list (Braden
9255 * 1993/04/26). 2) That updating only on newer timestamps interferes
9256 * with our earlier PAWS tests, so this check should be solely
9257 * predicated on the sequence space of this segment. 3) That we
9258 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9259 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9260 * SEG.Len, This modified check allows us to overcome RFC1323's
9261 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9262 * p.869. In such cases, we can still calculate the RTT correctly
9263 * when RCV.NXT == Last.ACK.Sent.
9264 */
9265 if ((to->to_flags & TOF_TS) != 0 &&
9266 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9267 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9268 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9269 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9270 tp->ts_recent = to->to_tsval;
9271 }
9272 /*
9273 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9274 * is on (half-synchronized state), then queue data for later
9275 * processing; else drop segment and return.
9276 */
9277 if ((thflags & TH_ACK) == 0) {
9278 if (tp->t_flags & TF_NEEDSYN) {
9279 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9280 tiwin, thflags, nxt_pkt));
9281 } else if (tp->t_flags & TF_ACKNOW) {
9282 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9283 bbr->r_wanted_output = 1;
9284 return (ret_val);
9285 } else {
9286 ctf_do_drop(m, NULL);
9287 return (0);
9288 }
9289 }
9290 /*
9291 * Ack processing.
9292 */
9293 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9294 return (ret_val);
9295 }
9296 if (sbavail(&so->so_snd)) {
9297 if (ctf_progress_timeout_check(tp, true)) {
9298 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9299 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9300 return (1);
9301 }
9302 }
9303 /* State changes only happen in bbr_process_data() */
9304 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9305 tiwin, thflags, nxt_pkt));
9306 }
9307
9308 /*
9309 * Return value of 1, the TCB is unlocked and most
9310 * likely gone, return value of 0, the TCB is still
9311 * locked.
9312 */
9313 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)9314 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9315 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9316 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9317 {
9318 struct tcp_bbr *bbr;
9319 int32_t ret_val;
9320
9321 INP_WLOCK_ASSERT(tptoinpcb(tp));
9322
9323 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9324 ctf_calc_rwin(so, tp);
9325 if ((thflags & TH_RST) ||
9326 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9327 return (ctf_process_rst(m, th, so, tp));
9328 /*
9329 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9330 * synchronized state.
9331 */
9332 if (thflags & TH_SYN) {
9333 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9334 return (ret_val);
9335 }
9336 /*
9337 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9338 * it's less than ts_recent, drop it.
9339 */
9340 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9341 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9342 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9343 return (ret_val);
9344 }
9345 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9346 return (ret_val);
9347 }
9348 /*
9349 * If last ACK falls within this segment's sequence numbers, record
9350 * its timestamp. NOTE: 1) That the test incorporates suggestions
9351 * from the latest proposal of the [email protected] list (Braden
9352 * 1993/04/26). 2) That updating only on newer timestamps interferes
9353 * with our earlier PAWS tests, so this check should be solely
9354 * predicated on the sequence space of this segment. 3) That we
9355 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9356 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9357 * SEG.Len, This modified check allows us to overcome RFC1323's
9358 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9359 * p.869. In such cases, we can still calculate the RTT correctly
9360 * when RCV.NXT == Last.ACK.Sent.
9361 */
9362 if ((to->to_flags & TOF_TS) != 0 &&
9363 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9364 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9365 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9366 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9367 tp->ts_recent = to->to_tsval;
9368 }
9369 /*
9370 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9371 * is on (half-synchronized state), then queue data for later
9372 * processing; else drop segment and return.
9373 */
9374 if ((thflags & TH_ACK) == 0) {
9375 if (tp->t_flags & TF_NEEDSYN) {
9376 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9377 tiwin, thflags, nxt_pkt));
9378 } else if (tp->t_flags & TF_ACKNOW) {
9379 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9380 bbr->r_wanted_output = 1;
9381 return (ret_val);
9382 } else {
9383 ctf_do_drop(m, NULL);
9384 return (0);
9385 }
9386 }
9387 /*
9388 * Ack processing.
9389 */
9390 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9391 return (ret_val);
9392 }
9393 if (sbavail(&so->so_snd)) {
9394 if (ctf_progress_timeout_check(tp, true)) {
9395 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9396 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9397 return (1);
9398 }
9399 }
9400 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9401 tiwin, thflags, nxt_pkt));
9402 }
9403
9404 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)9405 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr,
9406 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so)
9407 {
9408
9409 if (bbr->rc_allow_data_af_clo == 0) {
9410 close_now:
9411 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9412 /* tcp_close will kill the inp pre-log the Reset */
9413 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
9414 tp = tcp_close(tp);
9415 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9416 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9417 return (1);
9418 }
9419 if (sbavail(&so->so_snd) == 0)
9420 goto close_now;
9421 /* Ok we allow data that is ignored and a followup reset */
9422 tp->rcv_nxt = th->th_seq + *tlen;
9423 tp->t_flags2 |= TF2_DROP_AF_DATA;
9424 bbr->r_wanted_output = 1;
9425 *tlen = 0;
9426 return (0);
9427 }
9428
9429 /*
9430 * Return value of 1, the TCB is unlocked and most
9431 * likely gone, return value of 0, the TCB is still
9432 * locked.
9433 */
9434 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)9435 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9436 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9437 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9438 {
9439 int32_t ourfinisacked = 0;
9440 int32_t ret_val;
9441 struct tcp_bbr *bbr;
9442
9443 INP_WLOCK_ASSERT(tptoinpcb(tp));
9444
9445 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9446 ctf_calc_rwin(so, tp);
9447 if ((thflags & TH_RST) ||
9448 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9449 return (ctf_process_rst(m, th, so, tp));
9450 /*
9451 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9452 * synchronized state.
9453 */
9454 if (thflags & TH_SYN) {
9455 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9456 return (ret_val);
9457 }
9458 /*
9459 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9460 * it's less than ts_recent, drop it.
9461 */
9462 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9463 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9464 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9465 return (ret_val);
9466 }
9467 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9468 return (ret_val);
9469 }
9470 /*
9471 * If new data are received on a connection after the user processes
9472 * are gone, then RST the other end.
9473 * We call a new function now so we might continue and setup
9474 * to reset at all data being ack'd.
9475 */
9476 if ((tp->t_flags & TF_CLOSED) && tlen &&
9477 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9478 return (1);
9479 /*
9480 * If last ACK falls within this segment's sequence numbers, record
9481 * its timestamp. NOTE: 1) That the test incorporates suggestions
9482 * from the latest proposal of the [email protected] list (Braden
9483 * 1993/04/26). 2) That updating only on newer timestamps interferes
9484 * with our earlier PAWS tests, so this check should be solely
9485 * predicated on the sequence space of this segment. 3) That we
9486 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9487 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9488 * SEG.Len, This modified check allows us to overcome RFC1323's
9489 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9490 * p.869. In such cases, we can still calculate the RTT correctly
9491 * when RCV.NXT == Last.ACK.Sent.
9492 */
9493 if ((to->to_flags & TOF_TS) != 0 &&
9494 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9495 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9496 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9497 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9498 tp->ts_recent = to->to_tsval;
9499 }
9500 /*
9501 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9502 * is on (half-synchronized state), then queue data for later
9503 * processing; else drop segment and return.
9504 */
9505 if ((thflags & TH_ACK) == 0) {
9506 if (tp->t_flags & TF_NEEDSYN) {
9507 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9508 tiwin, thflags, nxt_pkt));
9509 } else if (tp->t_flags & TF_ACKNOW) {
9510 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9511 bbr->r_wanted_output = 1;
9512 return (ret_val);
9513 } else {
9514 ctf_do_drop(m, NULL);
9515 return (0);
9516 }
9517 }
9518 /*
9519 * Ack processing.
9520 */
9521 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9522 return (ret_val);
9523 }
9524 if (ourfinisacked) {
9525 /*
9526 * If we can't receive any more data, then closing user can
9527 * proceed. Starting the timer is contrary to the
9528 * specification, but if we don't get a FIN we'll hang
9529 * forever.
9530 *
9531 * XXXjl: we should release the tp also, and use a
9532 * compressed state.
9533 */
9534 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9535 soisdisconnected(so);
9536 tcp_timer_activate(tp, TT_2MSL,
9537 (tcp_fast_finwait2_recycle ?
9538 tcp_finwait2_timeout :
9539 TP_MAXIDLE(tp)));
9540 }
9541 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9542 }
9543 if (sbavail(&so->so_snd)) {
9544 if (ctf_progress_timeout_check(tp, true)) {
9545 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9546 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9547 return (1);
9548 }
9549 }
9550 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9551 tiwin, thflags, nxt_pkt));
9552 }
9553
9554 /*
9555 * Return value of 1, the TCB is unlocked and most
9556 * likely gone, return value of 0, the TCB is still
9557 * locked.
9558 */
9559 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)9560 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9561 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9562 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9563 {
9564 int32_t ourfinisacked = 0;
9565 int32_t ret_val;
9566 struct tcp_bbr *bbr;
9567
9568 INP_WLOCK_ASSERT(tptoinpcb(tp));
9569
9570 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9571 ctf_calc_rwin(so, tp);
9572 if ((thflags & TH_RST) ||
9573 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9574 return (ctf_process_rst(m, th, so, tp));
9575 /*
9576 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9577 * synchronized state.
9578 */
9579 if (thflags & TH_SYN) {
9580 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9581 return (ret_val);
9582 }
9583 /*
9584 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9585 * it's less than ts_recent, drop it.
9586 */
9587 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9588 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9589 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9590 return (ret_val);
9591 }
9592 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9593 return (ret_val);
9594 }
9595 /*
9596 * If last ACK falls within this segment's sequence numbers, record
9597 * its timestamp. NOTE: 1) That the test incorporates suggestions
9598 * from the latest proposal of the [email protected] list (Braden
9599 * 1993/04/26). 2) That updating only on newer timestamps interferes
9600 * with our earlier PAWS tests, so this check should be solely
9601 * predicated on the sequence space of this segment. 3) That we
9602 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9603 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9604 * SEG.Len, This modified check allows us to overcome RFC1323's
9605 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9606 * p.869. In such cases, we can still calculate the RTT correctly
9607 * when RCV.NXT == Last.ACK.Sent.
9608 */
9609 if ((to->to_flags & TOF_TS) != 0 &&
9610 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9611 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9612 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9613 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9614 tp->ts_recent = to->to_tsval;
9615 }
9616 /*
9617 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9618 * is on (half-synchronized state), then queue data for later
9619 * processing; else drop segment and return.
9620 */
9621 if ((thflags & TH_ACK) == 0) {
9622 if (tp->t_flags & TF_NEEDSYN) {
9623 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9624 tiwin, thflags, nxt_pkt));
9625 } else if (tp->t_flags & TF_ACKNOW) {
9626 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9627 bbr->r_wanted_output = 1;
9628 return (ret_val);
9629 } else {
9630 ctf_do_drop(m, NULL);
9631 return (0);
9632 }
9633 }
9634 /*
9635 * Ack processing.
9636 */
9637 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9638 return (ret_val);
9639 }
9640 if (ourfinisacked) {
9641 tcp_twstart(tp);
9642 m_freem(m);
9643 return (1);
9644 }
9645 if (sbavail(&so->so_snd)) {
9646 if (ctf_progress_timeout_check(tp, true)) {
9647 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9648 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9649 return (1);
9650 }
9651 }
9652 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9653 tiwin, thflags, nxt_pkt));
9654 }
9655
9656 /*
9657 * Return value of 1, the TCB is unlocked and most
9658 * likely gone, return value of 0, the TCB is still
9659 * locked.
9660 */
9661 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)9662 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
9663 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9664 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9665 {
9666 int32_t ourfinisacked = 0;
9667 int32_t ret_val;
9668 struct tcp_bbr *bbr;
9669
9670 INP_WLOCK_ASSERT(tptoinpcb(tp));
9671
9672 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9673 ctf_calc_rwin(so, tp);
9674 if ((thflags & TH_RST) ||
9675 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9676 return (ctf_process_rst(m, th, so, tp));
9677 /*
9678 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9679 * synchronized state.
9680 */
9681 if (thflags & TH_SYN) {
9682 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9683 return (ret_val);
9684 }
9685 /*
9686 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9687 * it's less than ts_recent, drop it.
9688 */
9689 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9690 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9691 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9692 return (ret_val);
9693 }
9694 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9695 return (ret_val);
9696 }
9697 /*
9698 * If last ACK falls within this segment's sequence numbers, record
9699 * its timestamp. NOTE: 1) That the test incorporates suggestions
9700 * from the latest proposal of the [email protected] list (Braden
9701 * 1993/04/26). 2) That updating only on newer timestamps interferes
9702 * with our earlier PAWS tests, so this check should be solely
9703 * predicated on the sequence space of this segment. 3) That we
9704 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9705 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9706 * SEG.Len, This modified check allows us to overcome RFC1323's
9707 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9708 * p.869. In such cases, we can still calculate the RTT correctly
9709 * when RCV.NXT == Last.ACK.Sent.
9710 */
9711 if ((to->to_flags & TOF_TS) != 0 &&
9712 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9713 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9714 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9715 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9716 tp->ts_recent = to->to_tsval;
9717 }
9718 /*
9719 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9720 * is on (half-synchronized state), then queue data for later
9721 * processing; else drop segment and return.
9722 */
9723 if ((thflags & TH_ACK) == 0) {
9724 if (tp->t_flags & TF_NEEDSYN) {
9725 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9726 tiwin, thflags, nxt_pkt));
9727 } else if (tp->t_flags & TF_ACKNOW) {
9728 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9729 bbr->r_wanted_output = 1;
9730 return (ret_val);
9731 } else {
9732 ctf_do_drop(m, NULL);
9733 return (0);
9734 }
9735 }
9736 /*
9737 * case TCPS_LAST_ACK: Ack processing.
9738 */
9739 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9740 return (ret_val);
9741 }
9742 if (ourfinisacked) {
9743 tp = tcp_close(tp);
9744 ctf_do_drop(m, tp);
9745 return (1);
9746 }
9747 if (sbavail(&so->so_snd)) {
9748 if (ctf_progress_timeout_check(tp, true)) {
9749 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9750 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9751 return (1);
9752 }
9753 }
9754 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9755 tiwin, thflags, nxt_pkt));
9756 }
9757
9758 /*
9759 * Return value of 1, the TCB is unlocked and most
9760 * likely gone, return value of 0, the TCB is still
9761 * locked.
9762 */
9763 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)9764 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
9765 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9766 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9767 {
9768 int32_t ourfinisacked = 0;
9769 int32_t ret_val;
9770 struct tcp_bbr *bbr;
9771
9772 INP_WLOCK_ASSERT(tptoinpcb(tp));
9773
9774 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9775 ctf_calc_rwin(so, tp);
9776 /* Reset receive buffer auto scaling when not in bulk receive mode. */
9777 if ((thflags & TH_RST) ||
9778 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9779 return (ctf_process_rst(m, th, so, tp));
9780
9781 /*
9782 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9783 * synchronized state.
9784 */
9785 if (thflags & TH_SYN) {
9786 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9787 return (ret_val);
9788 }
9789 /*
9790 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9791 * it's less than ts_recent, drop it.
9792 */
9793 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9794 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9795 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9796 return (ret_val);
9797 }
9798 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9799 return (ret_val);
9800 }
9801 /*
9802 * If new data are received on a connection after the user processes
9803 * are gone, then we may RST the other end depending on the outcome
9804 * of bbr_check_data_after_close.
9805 * We call a new function now so we might continue and setup
9806 * to reset at all data being ack'd.
9807 */
9808 if ((tp->t_flags & TF_CLOSED) && tlen &&
9809 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9810 return (1);
9811 /*
9812 * If last ACK falls within this segment's sequence numbers, record
9813 * its timestamp. NOTE: 1) That the test incorporates suggestions
9814 * from the latest proposal of the [email protected] list (Braden
9815 * 1993/04/26). 2) That updating only on newer timestamps interferes
9816 * with our earlier PAWS tests, so this check should be solely
9817 * predicated on the sequence space of this segment. 3) That we
9818 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9819 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9820 * SEG.Len, This modified check allows us to overcome RFC1323's
9821 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9822 * p.869. In such cases, we can still calculate the RTT correctly
9823 * when RCV.NXT == Last.ACK.Sent.
9824 */
9825 if ((to->to_flags & TOF_TS) != 0 &&
9826 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9827 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9828 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9829 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9830 tp->ts_recent = to->to_tsval;
9831 }
9832 /*
9833 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9834 * is on (half-synchronized state), then queue data for later
9835 * processing; else drop segment and return.
9836 */
9837 if ((thflags & TH_ACK) == 0) {
9838 if (tp->t_flags & TF_NEEDSYN) {
9839 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9840 tiwin, thflags, nxt_pkt));
9841 } else if (tp->t_flags & TF_ACKNOW) {
9842 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9843 bbr->r_wanted_output = 1;
9844 return (ret_val);
9845 } else {
9846 ctf_do_drop(m, NULL);
9847 return (0);
9848 }
9849 }
9850 /*
9851 * Ack processing.
9852 */
9853 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9854 return (ret_val);
9855 }
9856 if (sbavail(&so->so_snd)) {
9857 if (ctf_progress_timeout_check(tp, true)) {
9858 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9859 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9860 return (1);
9861 }
9862 }
9863 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9864 tiwin, thflags, nxt_pkt));
9865 }
9866
9867 static void
bbr_stop_all_timers(struct tcpcb * tp,struct tcp_bbr * bbr)9868 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr)
9869 {
9870 /*
9871 * Assure no timers are running.
9872 */
9873 if (tcp_timer_active(tp, TT_PERSIST)) {
9874 /* We enter in persists, set the flag appropriately */
9875 bbr->rc_in_persist = 1;
9876 }
9877 if (tcp_in_hpts(bbr->rc_tp)) {
9878 tcp_hpts_remove(bbr->rc_tp);
9879 }
9880 }
9881
9882 static void
bbr_google_mode_on(struct tcp_bbr * bbr)9883 bbr_google_mode_on(struct tcp_bbr *bbr)
9884 {
9885 bbr->rc_use_google = 1;
9886 bbr->rc_no_pacing = 0;
9887 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
9888 bbr->r_use_policer = bbr_policer_detection_enabled;
9889 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
9890 bbr->bbr_use_rack_cheat = 0;
9891 bbr->r_ctl.rc_incr_tmrs = 0;
9892 bbr->r_ctl.rc_inc_tcp_oh = 0;
9893 bbr->r_ctl.rc_inc_ip_oh = 0;
9894 bbr->r_ctl.rc_inc_enet_oh = 0;
9895 reset_time(&bbr->r_ctl.rc_delrate,
9896 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
9897 reset_time_small(&bbr->r_ctl.rc_rttprop,
9898 (11 * USECS_IN_SECOND));
9899 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
9900 }
9901
9902 static void
bbr_google_mode_off(struct tcp_bbr * bbr)9903 bbr_google_mode_off(struct tcp_bbr *bbr)
9904 {
9905 bbr->rc_use_google = 0;
9906 bbr->r_ctl.bbr_google_discount = 0;
9907 bbr->no_pacing_until = bbr_no_pacing_until;
9908 bbr->r_use_policer = 0;
9909 if (bbr->no_pacing_until)
9910 bbr->rc_no_pacing = 1;
9911 else
9912 bbr->rc_no_pacing = 0;
9913 if (bbr_use_rack_resend_cheat)
9914 bbr->bbr_use_rack_cheat = 1;
9915 else
9916 bbr->bbr_use_rack_cheat = 0;
9917 if (bbr_incr_timers)
9918 bbr->r_ctl.rc_incr_tmrs = 1;
9919 else
9920 bbr->r_ctl.rc_incr_tmrs = 0;
9921 if (bbr_include_tcp_oh)
9922 bbr->r_ctl.rc_inc_tcp_oh = 1;
9923 else
9924 bbr->r_ctl.rc_inc_tcp_oh = 0;
9925 if (bbr_include_ip_oh)
9926 bbr->r_ctl.rc_inc_ip_oh = 1;
9927 else
9928 bbr->r_ctl.rc_inc_ip_oh = 0;
9929 if (bbr_include_enet_oh)
9930 bbr->r_ctl.rc_inc_enet_oh = 1;
9931 else
9932 bbr->r_ctl.rc_inc_enet_oh = 0;
9933 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
9934 reset_time(&bbr->r_ctl.rc_delrate,
9935 bbr_num_pktepo_for_del_limit);
9936 reset_time_small(&bbr->r_ctl.rc_rttprop,
9937 (bbr_filter_len_sec * USECS_IN_SECOND));
9938 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
9939 }
9940 /*
9941 * Return 0 on success, non-zero on failure
9942 * which indicates the error (usually no memory).
9943 */
9944 static int
bbr_init(struct tcpcb * tp,void ** ptr)9945 bbr_init(struct tcpcb *tp, void **ptr)
9946 {
9947 struct inpcb *inp = tptoinpcb(tp);
9948 struct tcp_bbr *bbr = NULL;
9949 uint32_t cts;
9950
9951 tcp_hpts_init(tp);
9952
9953 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO));
9954 if (*ptr == NULL) {
9955 /*
9956 * We need to allocate memory but cant. The INP and INP_INFO
9957 * locks and they are recursive (happens during setup. So a
9958 * scheme to drop the locks fails :(
9959 *
9960 */
9961 return (ENOMEM);
9962 }
9963 bbr = (struct tcp_bbr *)*ptr;
9964 bbr->rtt_valid = 0;
9965 tp->t_flags2 |= TF2_CANNOT_DO_ECN;
9966 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
9967 /* Take off any undesired flags */
9968 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY;
9969 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
9970 tp->t_flags2 &= ~TF2_MBUF_ACKCMP;
9971 tp->t_flags2 &= ~TF2_MBUF_L_ACKS;
9972
9973 TAILQ_INIT(&bbr->r_ctl.rc_map);
9974 TAILQ_INIT(&bbr->r_ctl.rc_free);
9975 TAILQ_INIT(&bbr->r_ctl.rc_tmap);
9976 bbr->rc_tp = tp;
9977 bbr->rc_inp = inp;
9978 cts = tcp_get_usecs(&bbr->rc_tv);
9979 tp->t_acktime = 0;
9980 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close;
9981 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade;
9982 bbr->rc_tlp_threshold = bbr_tlp_thresh;
9983 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh;
9984 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay;
9985 bbr->r_ctl.rc_min_to = bbr_min_to;
9986 bbr->rc_bbr_state = BBR_STATE_STARTUP;
9987 bbr->r_ctl.bbr_lost_at_state = 0;
9988 bbr->r_ctl.rc_lost_at_startup = 0;
9989 bbr->rc_all_timers_stopped = 0;
9990 bbr->r_ctl.rc_bbr_lastbtlbw = 0;
9991 bbr->r_ctl.rc_pkt_epoch_del = 0;
9992 bbr->r_ctl.rc_pkt_epoch = 0;
9993 bbr->r_ctl.rc_lowest_rtt = 0xffffffff;
9994 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain;
9995 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
9996 bbr->r_ctl.rc_went_idle_time = cts;
9997 bbr->rc_pacer_started = cts;
9998 bbr->r_ctl.rc_pkt_epoch_time = cts;
9999 bbr->r_ctl.rc_rcvtime = cts;
10000 bbr->r_ctl.rc_bbr_state_time = cts;
10001 bbr->r_ctl.rc_del_time = cts;
10002 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
10003 bbr->r_ctl.last_in_probertt = cts;
10004 bbr->skip_gain = 0;
10005 bbr->gain_is_limited = 0;
10006 bbr->no_pacing_until = bbr_no_pacing_until;
10007 if (bbr->no_pacing_until)
10008 bbr->rc_no_pacing = 1;
10009 if (bbr_use_google_algo) {
10010 bbr->rc_no_pacing = 0;
10011 bbr->rc_use_google = 1;
10012 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10013 bbr->r_use_policer = bbr_policer_detection_enabled;
10014 } else {
10015 bbr->rc_use_google = 0;
10016 bbr->r_ctl.bbr_google_discount = 0;
10017 bbr->r_use_policer = 0;
10018 }
10019 if (bbr_ts_limiting)
10020 bbr->rc_use_ts_limit = 1;
10021 else
10022 bbr->rc_use_ts_limit = 0;
10023 if (bbr_ts_can_raise)
10024 bbr->ts_can_raise = 1;
10025 else
10026 bbr->ts_can_raise = 0;
10027 if (V_tcp_delack_enabled == 1)
10028 tp->t_delayed_ack = 2;
10029 else if (V_tcp_delack_enabled == 0)
10030 tp->t_delayed_ack = 0;
10031 else if (V_tcp_delack_enabled < 100)
10032 tp->t_delayed_ack = V_tcp_delack_enabled;
10033 else
10034 tp->t_delayed_ack = 2;
10035 if (bbr->rc_use_google == 0)
10036 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10037 else
10038 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10039 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms;
10040 bbr->rc_max_rto_sec = bbr_rto_max_sec;
10041 bbr->rc_init_win = bbr_def_init_win;
10042 if (tp->t_flags & TF_REQ_TSTMP)
10043 bbr->rc_last_options = TCP_TS_OVERHEAD;
10044 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options;
10045 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd;
10046 bbr->r_init_rtt = 1;
10047
10048 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
10049 if (bbr_allow_hdwr_pacing)
10050 bbr->bbr_hdw_pace_ena = 1;
10051 else
10052 bbr->bbr_hdw_pace_ena = 0;
10053 if (bbr_sends_full_iwnd)
10054 bbr->bbr_init_win_cheat = 1;
10055 else
10056 bbr->bbr_init_win_cheat = 0;
10057 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max;
10058 bbr->r_ctl.rc_drain_pg = bbr_drain_gain;
10059 bbr->r_ctl.rc_startup_pg = bbr_high_gain;
10060 bbr->rc_loss_exit = bbr_exit_startup_at_loss;
10061 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain;
10062 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second;
10063 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar;
10064 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max;
10065 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor;
10066 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min;
10067 bbr->r_ctl.bbr_cross_over = bbr_cross_over;
10068 bbr->r_ctl.rc_rtt_shrinks = cts;
10069 if (bbr->rc_use_google) {
10070 setup_time_filter(&bbr->r_ctl.rc_delrate,
10071 FILTER_TYPE_MAX,
10072 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10073 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10074 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND));
10075 } else {
10076 setup_time_filter(&bbr->r_ctl.rc_delrate,
10077 FILTER_TYPE_MAX,
10078 bbr_num_pktepo_for_del_limit);
10079 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10080 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND));
10081 }
10082 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0);
10083 if (bbr_uses_idle_restart)
10084 bbr->rc_use_idle_restart = 1;
10085 else
10086 bbr->rc_use_idle_restart = 0;
10087 bbr->r_ctl.rc_bbr_cur_del_rate = 0;
10088 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps;
10089 if (bbr_resends_use_tso)
10090 bbr->rc_resends_use_tso = 1;
10091 if (tp->snd_una != tp->snd_max) {
10092 /* Create a send map for the current outstanding data */
10093 struct bbr_sendmap *rsm;
10094
10095 rsm = bbr_alloc(bbr);
10096 if (rsm == NULL) {
10097 uma_zfree(bbr_pcb_zone, *ptr);
10098 *ptr = NULL;
10099 return (ENOMEM);
10100 }
10101 rsm->r_rtt_not_allowed = 1;
10102 rsm->r_tim_lastsent[0] = cts;
10103 rsm->r_rtr_cnt = 1;
10104 rsm->r_rtr_bytes = 0;
10105 rsm->r_start = tp->snd_una;
10106 rsm->r_end = tp->snd_max;
10107 rsm->r_dupack = 0;
10108 rsm->r_delivered = bbr->r_ctl.rc_delivered;
10109 rsm->r_ts_valid = 0;
10110 rsm->r_del_ack_ts = tp->ts_recent;
10111 rsm->r_del_time = cts;
10112 if (bbr->r_ctl.r_app_limited_until)
10113 rsm->r_app_limited = 1;
10114 else
10115 rsm->r_app_limited = 0;
10116 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
10117 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
10118 rsm->r_in_tmap = 1;
10119 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
10120 rsm->r_bbr_state = bbr_state_val(bbr);
10121 else
10122 rsm->r_bbr_state = 8;
10123 }
10124 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0))
10125 bbr->bbr_use_rack_cheat = 1;
10126 if (bbr_incr_timers && (bbr->rc_use_google == 0))
10127 bbr->r_ctl.rc_incr_tmrs = 1;
10128 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0))
10129 bbr->r_ctl.rc_inc_tcp_oh = 1;
10130 if (bbr_include_ip_oh && (bbr->rc_use_google == 0))
10131 bbr->r_ctl.rc_inc_ip_oh = 1;
10132 if (bbr_include_enet_oh && (bbr->rc_use_google == 0))
10133 bbr->r_ctl.rc_inc_enet_oh = 1;
10134
10135 bbr_log_type_statechange(bbr, cts, __LINE__);
10136 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
10137 (tp->t_srtt)) {
10138 uint32_t rtt;
10139
10140 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
10141 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
10142 }
10143 /* announce the settings and state */
10144 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT);
10145 tcp_bbr_tso_size_check(bbr, cts);
10146 /*
10147 * Now call the generic function to start a timer. This will place
10148 * the TCB on the hptsi wheel if a timer is needed with appropriate
10149 * flags.
10150 */
10151 bbr_stop_all_timers(tp, bbr);
10152 /*
10153 * Validate the timers are not in usec, if they are convert.
10154 * BBR should in theory move to USEC and get rid of a
10155 * lot of the TICKS_2 calls.. but for now we stay
10156 * with tick timers.
10157 */
10158 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
10159 TCPT_RANGESET(tp->t_rxtcur,
10160 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
10161 tp->t_rttmin, TCPTV_REXMTMAX);
10162 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0);
10163 return (0);
10164 }
10165
10166 /*
10167 * Return 0 if we can accept the connection. Return
10168 * non-zero if we can't handle the connection. A EAGAIN
10169 * means you need to wait until the connection is up.
10170 * a EADDRNOTAVAIL means we can never handle the connection
10171 * (no SACK).
10172 */
10173 static int
bbr_handoff_ok(struct tcpcb * tp)10174 bbr_handoff_ok(struct tcpcb *tp)
10175 {
10176 if ((tp->t_state == TCPS_CLOSED) ||
10177 (tp->t_state == TCPS_LISTEN)) {
10178 /* Sure no problem though it may not stick */
10179 return (0);
10180 }
10181 if ((tp->t_state == TCPS_SYN_SENT) ||
10182 (tp->t_state == TCPS_SYN_RECEIVED)) {
10183 /*
10184 * We really don't know you have to get to ESTAB or beyond
10185 * to tell.
10186 */
10187 return (EAGAIN);
10188 }
10189 if (tp->t_flags & TF_SENTFIN)
10190 return (EINVAL);
10191 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) {
10192 return (0);
10193 }
10194 /*
10195 * If we reach here we don't do SACK on this connection so we can
10196 * never do rack.
10197 */
10198 return (EINVAL);
10199 }
10200
10201 static void
bbr_fini(struct tcpcb * tp,int32_t tcb_is_purged)10202 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10203 {
10204 if (tp->t_fb_ptr) {
10205 uint32_t calc;
10206 struct tcp_bbr *bbr;
10207 struct bbr_sendmap *rsm;
10208
10209 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10210 if (bbr->r_ctl.crte)
10211 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
10212 bbr_log_flowend(bbr);
10213 bbr->rc_tp = NULL;
10214 if (bbr->bbr_hdrw_pacing)
10215 counter_u64_add(bbr_flows_whdwr_pacing, -1);
10216 else
10217 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
10218 if (bbr->r_ctl.crte != NULL) {
10219 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp);
10220 bbr->r_ctl.crte = NULL;
10221 }
10222 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10223 while (rsm) {
10224 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
10225 uma_zfree(bbr_zone, rsm);
10226 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10227 }
10228 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10229 while (rsm) {
10230 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
10231 uma_zfree(bbr_zone, rsm);
10232 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10233 }
10234 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd;
10235 if (calc > (bbr->r_ctl.rc_init_rwnd / 10))
10236 BBR_STAT_INC(bbr_dynamic_rwnd);
10237 else
10238 BBR_STAT_INC(bbr_static_rwnd);
10239 bbr->r_ctl.rc_free_cnt = 0;
10240 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10241 tp->t_fb_ptr = NULL;
10242 }
10243 /* Make sure snd_nxt is correctly set */
10244 tp->snd_nxt = tp->snd_max;
10245 }
10246
10247 static void
bbr_set_state(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t win)10248 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win)
10249 {
10250 switch (tp->t_state) {
10251 case TCPS_SYN_SENT:
10252 bbr->r_state = TCPS_SYN_SENT;
10253 bbr->r_substate = bbr_do_syn_sent;
10254 break;
10255 case TCPS_SYN_RECEIVED:
10256 bbr->r_state = TCPS_SYN_RECEIVED;
10257 bbr->r_substate = bbr_do_syn_recv;
10258 break;
10259 case TCPS_ESTABLISHED:
10260 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd);
10261 bbr->r_state = TCPS_ESTABLISHED;
10262 bbr->r_substate = bbr_do_established;
10263 break;
10264 case TCPS_CLOSE_WAIT:
10265 bbr->r_state = TCPS_CLOSE_WAIT;
10266 bbr->r_substate = bbr_do_close_wait;
10267 break;
10268 case TCPS_FIN_WAIT_1:
10269 bbr->r_state = TCPS_FIN_WAIT_1;
10270 bbr->r_substate = bbr_do_fin_wait_1;
10271 break;
10272 case TCPS_CLOSING:
10273 bbr->r_state = TCPS_CLOSING;
10274 bbr->r_substate = bbr_do_closing;
10275 break;
10276 case TCPS_LAST_ACK:
10277 bbr->r_state = TCPS_LAST_ACK;
10278 bbr->r_substate = bbr_do_lastack;
10279 break;
10280 case TCPS_FIN_WAIT_2:
10281 bbr->r_state = TCPS_FIN_WAIT_2;
10282 bbr->r_substate = bbr_do_fin_wait_2;
10283 break;
10284 case TCPS_LISTEN:
10285 case TCPS_CLOSED:
10286 case TCPS_TIME_WAIT:
10287 default:
10288 break;
10289 };
10290 }
10291
10292 static void
bbr_substate_change(struct tcp_bbr * bbr,uint32_t cts,int32_t line,int dolog)10293 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog)
10294 {
10295 /*
10296 * Now what state are we going into now? Is there adjustments
10297 * needed?
10298 */
10299 int32_t old_state;
10300
10301 old_state = bbr_state_val(bbr);
10302 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) {
10303 /* Save the lowest srtt we saw in our end of the sub-state */
10304 bbr->rc_hit_state_1 = 0;
10305 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff)
10306 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state;
10307 }
10308 bbr->rc_bbr_substate++;
10309 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) {
10310 /* Cycle back to first state-> gain */
10311 bbr->rc_bbr_substate = 0;
10312 }
10313 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10314 /*
10315 * We enter the gain(5/4) cycle (possibly less if
10316 * shallow buffer detection is enabled)
10317 */
10318 if (bbr->skip_gain) {
10319 /*
10320 * Hardware pacing has set our rate to
10321 * the max and limited our b/w just
10322 * do level i.e. no gain.
10323 */
10324 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1];
10325 } else if (bbr->gain_is_limited &&
10326 bbr->bbr_hdrw_pacing &&
10327 bbr->r_ctl.crte) {
10328 /*
10329 * We can't gain above the hardware pacing
10330 * rate which is less than our rate + the gain
10331 * calculate the gain needed to reach the hardware
10332 * pacing rate..
10333 */
10334 uint64_t bw, rate, gain_calc;
10335
10336 bw = bbr_get_bw(bbr);
10337 rate = bbr->r_ctl.crte->rate;
10338 if ((rate > bw) &&
10339 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) {
10340 gain_calc = (rate * BBR_UNIT) / bw;
10341 if (gain_calc < BBR_UNIT)
10342 gain_calc = BBR_UNIT;
10343 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc;
10344 } else {
10345 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10346 }
10347 } else
10348 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10349 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) {
10350 bbr->r_ctl.rc_bbr_state_atflight = cts;
10351 } else
10352 bbr->r_ctl.rc_bbr_state_atflight = 0;
10353 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10354 bbr->rc_hit_state_1 = 1;
10355 bbr->r_ctl.rc_exta_time_gd = 0;
10356 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10357 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10358 if (bbr_state_drain_2_tar) {
10359 bbr->r_ctl.rc_bbr_state_atflight = 0;
10360 } else
10361 bbr->r_ctl.rc_bbr_state_atflight = cts;
10362 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN];
10363 } else {
10364 /* All other cycles hit here 2-7 */
10365 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) {
10366 if (bbr_sub_drain_slam_cwnd &&
10367 (bbr->rc_use_google == 0) &&
10368 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10369 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10370 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10371 }
10372 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP))
10373 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) -
10374 bbr_get_rtt(bbr, BBR_RTT_PROP));
10375 else
10376 bbr->r_ctl.rc_exta_time_gd = 0;
10377 if (bbr->r_ctl.rc_exta_time_gd) {
10378 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd;
10379 /* Now chop up the time for each state (div by 7) */
10380 bbr->r_ctl.rc_level_state_extra /= 7;
10381 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) {
10382 /* Add a randomization */
10383 bbr_randomize_extra_state_time(bbr);
10384 }
10385 }
10386 }
10387 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10388 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)];
10389 }
10390 if (bbr->rc_use_google) {
10391 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10392 }
10393 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10394 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10395 if (dolog)
10396 bbr_log_type_statechange(bbr, cts, line);
10397
10398 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10399 uint32_t time_in;
10400
10401 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10402 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
10403 counter_u64_add(bbr_state_time[(old_state + 5)], time_in);
10404 } else {
10405 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10406 }
10407 }
10408 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
10409 bbr_set_state_target(bbr, __LINE__);
10410 if (bbr_sub_drain_slam_cwnd &&
10411 (bbr->rc_use_google == 0) &&
10412 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10413 /* Slam down the cwnd */
10414 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10415 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10416 if (bbr_sub_drain_app_limit) {
10417 /* Go app limited if we are on a long drain */
10418 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered +
10419 ctf_flight_size(bbr->rc_tp,
10420 (bbr->r_ctl.rc_sacked +
10421 bbr->r_ctl.rc_lost_bytes)));
10422 }
10423 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10424 }
10425 if (bbr->rc_lt_use_bw) {
10426 /* In policed mode we clamp pacing_gain to BBR_UNIT */
10427 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10428 }
10429 /* Google changes TSO size every cycle */
10430 if (bbr->rc_use_google)
10431 tcp_bbr_tso_size_check(bbr, cts);
10432 bbr->r_ctl.gain_epoch = cts;
10433 bbr->r_ctl.rc_bbr_state_time = cts;
10434 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch;
10435 }
10436
10437 static void
bbr_set_probebw_google_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10438 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10439 {
10440 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) &&
10441 (google_allow_early_out == 1) &&
10442 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) {
10443 /* We have reached out target flight size possibly early */
10444 goto change_state;
10445 }
10446 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10447 return;
10448 }
10449 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) {
10450 /*
10451 * Must be a rttProp movement forward before
10452 * we can change states.
10453 */
10454 return;
10455 }
10456 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10457 /*
10458 * The needed time has passed but for
10459 * the gain cycle extra rules apply:
10460 * 1) If we have seen loss, we exit
10461 * 2) If we have not reached the target
10462 * we stay in GAIN (gain-to-target).
10463 */
10464 if (google_consider_lost && losses)
10465 goto change_state;
10466 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) {
10467 return;
10468 }
10469 }
10470 change_state:
10471 /* For gain we must reach our target, all others last 1 rttProp */
10472 bbr_substate_change(bbr, cts, __LINE__, 1);
10473 }
10474
10475 static void
bbr_set_probebw_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10476 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10477 {
10478 uint32_t flight, bbr_cur_cycle_time;
10479
10480 if (bbr->rc_use_google) {
10481 bbr_set_probebw_google_gains(bbr, cts, losses);
10482 return;
10483 }
10484 if (cts == 0) {
10485 /*
10486 * Never alow cts to be 0 we
10487 * do this so we can judge if
10488 * we have set a timestamp.
10489 */
10490 cts = 1;
10491 }
10492 if (bbr_state_is_pkt_epoch)
10493 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
10494 else
10495 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP);
10496
10497 if (bbr->r_ctl.rc_bbr_state_atflight == 0) {
10498 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10499 flight = ctf_flight_size(bbr->rc_tp,
10500 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10501 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) {
10502 /* Keep it slam down */
10503 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) {
10504 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10505 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10506 }
10507 if (bbr_sub_drain_app_limit) {
10508 /* Go app limited if we are on a long drain */
10509 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight);
10510 }
10511 }
10512 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) &&
10513 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) ||
10514 (flight >= bbr->r_ctl.flightsize_at_drain))) {
10515 /*
10516 * Still here after the same time as
10517 * the gain. We need to drain harder
10518 * for the next srtt. Reduce by a set amount
10519 * the gain drop is capped at DRAIN states
10520 * value (88).
10521 */
10522 bbr->r_ctl.flightsize_at_drain = flight;
10523 if (bbr_drain_drop_mul &&
10524 bbr_drain_drop_div &&
10525 (bbr_drain_drop_mul < bbr_drain_drop_div)) {
10526 /* Use your specific drop value (def 4/5 = 20%) */
10527 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul;
10528 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div;
10529 } else {
10530 /* You get drop of 20% */
10531 bbr->r_ctl.rc_bbr_hptsi_gain *= 4;
10532 bbr->r_ctl.rc_bbr_hptsi_gain /= 5;
10533 }
10534 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) {
10535 /* Reduce our gain again to the bottom */
10536 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
10537 }
10538 bbr_log_exit_gain(bbr, cts, 4);
10539 /*
10540 * Extend out so we wait another
10541 * epoch before dropping again.
10542 */
10543 bbr->r_ctl.gain_epoch = cts;
10544 }
10545 if (flight <= bbr->r_ctl.rc_target_at_state) {
10546 if (bbr_sub_drain_slam_cwnd &&
10547 (bbr->rc_use_google == 0) &&
10548 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10549 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10550 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10551 }
10552 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10553 bbr_log_exit_gain(bbr, cts, 3);
10554 }
10555 } else {
10556 /* Its a gain */
10557 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) {
10558 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10559 goto change_state;
10560 }
10561 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) ||
10562 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >=
10563 bbr->rc_tp->snd_wnd)) {
10564 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10565 bbr_log_exit_gain(bbr, cts, 2);
10566 }
10567 }
10568 /**
10569 * We fall through and return always one of two things has
10570 * occurred.
10571 * 1) We are still not at target
10572 * <or>
10573 * 2) We reached the target and set rc_bbr_state_atflight
10574 * which means we no longer hit this block
10575 * next time we are called.
10576 */
10577 return;
10578 }
10579 change_state:
10580 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time))
10581 return;
10582 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) {
10583 /* Less than a full time-period has passed */
10584 return;
10585 }
10586 if (bbr->r_ctl.rc_level_state_extra &&
10587 (bbr_state_val(bbr) > BBR_SUB_DRAIN) &&
10588 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10589 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10590 /* Less than a full time-period + extra has passed */
10591 return;
10592 }
10593 if (bbr_gain_gets_extra_too &&
10594 bbr->r_ctl.rc_level_state_extra &&
10595 (bbr_state_val(bbr) == BBR_SUB_GAIN) &&
10596 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10597 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10598 /* Less than a full time-period + extra has passed */
10599 return;
10600 }
10601 bbr_substate_change(bbr, cts, __LINE__, 1);
10602 }
10603
10604 static uint32_t
bbr_get_a_state_target(struct tcp_bbr * bbr,uint32_t gain)10605 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain)
10606 {
10607 uint32_t mss, tar;
10608
10609 if (bbr->rc_use_google) {
10610 /* Google just uses the cwnd target */
10611 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain);
10612 } else {
10613 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
10614 bbr->r_ctl.rc_pace_max_segs);
10615 /* Get the base cwnd with gain rounded to a mss */
10616 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr),
10617 gain), mss);
10618 /* Make sure it is within our min */
10619 if (tar < get_min_cwnd(bbr))
10620 return (get_min_cwnd(bbr));
10621 }
10622 return (tar);
10623 }
10624
10625 static void
bbr_set_state_target(struct tcp_bbr * bbr,int line)10626 bbr_set_state_target(struct tcp_bbr *bbr, int line)
10627 {
10628 uint32_t tar, meth;
10629
10630 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
10631 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
10632 /* Special case using old probe-rtt method */
10633 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10634 meth = 1;
10635 } else {
10636 /* Non-probe-rtt case and reduced probe-rtt */
10637 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
10638 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) {
10639 /* For gain cycle we use the hptsi gain */
10640 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10641 meth = 2;
10642 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) {
10643 /*
10644 * If configured, or for google all other states
10645 * get BBR_UNIT.
10646 */
10647 tar = bbr_get_a_state_target(bbr, BBR_UNIT);
10648 meth = 3;
10649 } else {
10650 /*
10651 * Or we set a target based on the pacing gain
10652 * for non-google mode and default (non-configured).
10653 * Note we don't set a target goal below drain (192).
10654 */
10655 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) {
10656 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]);
10657 meth = 4;
10658 } else {
10659 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10660 meth = 5;
10661 }
10662 }
10663 }
10664 bbr_log_set_of_state_target(bbr, tar, line, meth);
10665 bbr->r_ctl.rc_target_at_state = tar;
10666 }
10667
10668 static void
bbr_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts,int32_t line)10669 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
10670 {
10671 /* Change to probe_rtt */
10672 uint32_t time_in;
10673
10674 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10675 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10676 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10677 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain
10678 + bbr->r_ctl.rc_delivered);
10679 /* Setup so we force feed the filter */
10680 if (bbr->rc_use_google || bbr_probertt_sets_rtt)
10681 bbr->rc_prtt_set_ts = 1;
10682 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10683 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10684 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10685 }
10686 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0);
10687 bbr->r_ctl.rc_rtt_shrinks = cts;
10688 bbr->r_ctl.last_in_probertt = cts;
10689 bbr->r_ctl.rc_probertt_srttchktim = cts;
10690 bbr->r_ctl.rc_bbr_state_time = cts;
10691 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT;
10692 /* We need to force the filter to update */
10693
10694 if ((bbr_sub_drain_slam_cwnd) &&
10695 bbr->rc_hit_state_1 &&
10696 (bbr->rc_use_google == 0) &&
10697 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10698 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd)
10699 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10700 } else
10701 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10702 /* Update the lost */
10703 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10704 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){
10705 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */
10706 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10707 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10708 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10709 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10710 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6);
10711 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd;
10712 } else {
10713 /*
10714 * We bring it down slowly by using a hptsi gain that is
10715 * probably 75%. This will slowly float down our outstanding
10716 * without tampering with the cwnd.
10717 */
10718 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
10719 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10720 bbr_set_state_target(bbr, __LINE__);
10721 if (bbr_prtt_slam_cwnd &&
10722 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
10723 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10724 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10725 }
10726 }
10727 if (ctf_flight_size(bbr->rc_tp,
10728 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
10729 bbr->r_ctl.rc_target_at_state) {
10730 /* We are at target */
10731 bbr->r_ctl.rc_bbr_enters_probertt = cts;
10732 } else {
10733 /* We need to come down to reach target before our time begins */
10734 bbr->r_ctl.rc_bbr_enters_probertt = 0;
10735 }
10736 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch;
10737 BBR_STAT_INC(bbr_enter_probertt);
10738 bbr_log_exit_gain(bbr, cts, 0);
10739 bbr_log_type_statechange(bbr, cts, line);
10740 }
10741
10742 static void
bbr_check_probe_rtt_limits(struct tcp_bbr * bbr,uint32_t cts)10743 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts)
10744 {
10745 /*
10746 * Sanity check on probe-rtt intervals.
10747 * In crazy situations where we are competing
10748 * against new-reno flows with huge buffers
10749 * our rtt-prop interval could come to dominate
10750 * things if we can't get through a full set
10751 * of cycles, we need to adjust it.
10752 */
10753 if (bbr_can_adjust_probertt &&
10754 (bbr->rc_use_google == 0)) {
10755 uint16_t val = 0;
10756 uint32_t cur_rttp, fval, newval, baseval;
10757
10758 /* Are we to small and go into probe-rtt to often? */
10759 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1));
10760 cur_rttp = roundup(baseval, USECS_IN_SECOND);
10761 fval = bbr_filter_len_sec * USECS_IN_SECOND;
10762 if (bbr_is_ratio == 0) {
10763 if (fval > bbr_rtt_probe_limit)
10764 newval = cur_rttp + (fval - bbr_rtt_probe_limit);
10765 else
10766 newval = cur_rttp;
10767 } else {
10768 int mul;
10769
10770 mul = fval / bbr_rtt_probe_limit;
10771 newval = cur_rttp * mul;
10772 }
10773 if (cur_rttp > bbr->r_ctl.rc_probertt_int) {
10774 bbr->r_ctl.rc_probertt_int = cur_rttp;
10775 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10776 val = 1;
10777 } else {
10778 /*
10779 * No adjustments were made
10780 * do we need to shrink it?
10781 */
10782 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) {
10783 if (cur_rttp <= bbr_rtt_probe_limit) {
10784 /*
10785 * Things have calmed down lets
10786 * shrink all the way to default
10787 */
10788 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10789 reset_time_small(&bbr->r_ctl.rc_rttprop,
10790 (bbr_filter_len_sec * USECS_IN_SECOND));
10791 cur_rttp = bbr_rtt_probe_limit;
10792 newval = (bbr_filter_len_sec * USECS_IN_SECOND);
10793 val = 2;
10794 } else {
10795 /*
10796 * Well does some adjustment make sense?
10797 */
10798 if (cur_rttp < bbr->r_ctl.rc_probertt_int) {
10799 /* We can reduce interval time some */
10800 bbr->r_ctl.rc_probertt_int = cur_rttp;
10801 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10802 val = 3;
10803 }
10804 }
10805 }
10806 }
10807 if (val)
10808 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val);
10809 }
10810 }
10811
10812 static void
bbr_exit_probe_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)10813 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
10814 {
10815 /* Exit probe-rtt */
10816
10817 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) {
10818 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10819 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10820 }
10821 bbr_log_exit_gain(bbr, cts, 1);
10822 bbr->rc_hit_state_1 = 0;
10823 bbr->r_ctl.rc_rtt_shrinks = cts;
10824 bbr->r_ctl.last_in_probertt = cts;
10825 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0);
10826 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10827 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp,
10828 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
10829 bbr->r_ctl.rc_delivered);
10830 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10831 uint32_t time_in;
10832
10833 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10834 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10835 }
10836 if (bbr->rc_filled_pipe) {
10837 /* Switch to probe_bw */
10838 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
10839 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
10840 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10841 bbr_substate_change(bbr, cts, __LINE__, 0);
10842 bbr_log_type_statechange(bbr, cts, __LINE__);
10843 } else {
10844 /* Back to startup */
10845 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10846 bbr->r_ctl.rc_bbr_state_time = cts;
10847 /*
10848 * We don't want to give a complete free 3
10849 * measurements until we exit, so we use
10850 * the number of pe's we were in probe-rtt
10851 * to add to the startup_epoch. That way
10852 * we will still retain the old state.
10853 */
10854 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt);
10855 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10856 /* Make sure to use the lower pg when shifting back in */
10857 if (bbr->r_ctl.rc_lost &&
10858 bbr_use_lower_gain_in_startup &&
10859 (bbr->rc_use_google == 0))
10860 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
10861 else
10862 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
10863 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
10864 /* Probably not needed but set it anyway */
10865 bbr_set_state_target(bbr, __LINE__);
10866 bbr_log_type_statechange(bbr, cts, __LINE__);
10867 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10868 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0);
10869 }
10870 bbr_check_probe_rtt_limits(bbr, cts);
10871 }
10872
10873 static int32_t inline
bbr_should_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts)10874 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts)
10875 {
10876 if ((bbr->rc_past_init_win == 1) &&
10877 (bbr->rc_in_persist == 0) &&
10878 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) {
10879 return (1);
10880 }
10881 if (bbr_can_force_probertt &&
10882 (bbr->rc_in_persist == 0) &&
10883 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
10884 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
10885 return (1);
10886 }
10887 return (0);
10888 }
10889
10890 static int32_t
bbr_google_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t pkt_epoch)10891 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch)
10892 {
10893 uint64_t btlbw, gain;
10894 if (pkt_epoch == 0) {
10895 /*
10896 * Need to be on a pkt-epoch to continue.
10897 */
10898 return (0);
10899 }
10900 btlbw = bbr_get_full_bw(bbr);
10901 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
10902 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
10903 if (btlbw >= gain) {
10904 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
10905 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10906 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
10907 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
10908 }
10909 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)
10910 return (1);
10911 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10912 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
10913 return(0);
10914 }
10915
10916 static int32_t inline
bbr_state_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch)10917 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch)
10918 {
10919 /* Have we gained 25% in the last 3 packet based epoch's? */
10920 uint64_t btlbw, gain;
10921 int do_exit;
10922 int delta, rtt_gain;
10923
10924 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
10925 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
10926 /*
10927 * This qualifies as a RTT_PROBE session since we drop the
10928 * data outstanding to nothing and waited more than
10929 * bbr_rtt_probe_time.
10930 */
10931 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
10932 bbr_set_reduced_rtt(bbr, cts, __LINE__);
10933 }
10934 if (bbr_should_enter_probe_rtt(bbr, cts)) {
10935 bbr_enter_probe_rtt(bbr, cts, __LINE__);
10936 return (0);
10937 }
10938 if (bbr->rc_use_google)
10939 return (bbr_google_startup(bbr, cts, pkt_epoch));
10940
10941 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
10942 (bbr_use_lower_gain_in_startup)) {
10943 /* Drop to a lower gain 1.5 x since we saw loss */
10944 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
10945 }
10946 if (pkt_epoch == 0) {
10947 /*
10948 * Need to be on a pkt-epoch to continue.
10949 */
10950 return (0);
10951 }
10952 if (bbr_rtt_gain_thresh) {
10953 /*
10954 * Do we allow a flow to stay
10955 * in startup with no loss and no
10956 * gain in rtt over a set threshold?
10957 */
10958 if (bbr->r_ctl.rc_pkt_epoch_rtt &&
10959 bbr->r_ctl.startup_last_srtt &&
10960 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) {
10961 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt;
10962 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt;
10963 } else
10964 rtt_gain = 0;
10965 if ((bbr->r_ctl.startup_last_srtt == 0) ||
10966 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt))
10967 /* First time or new lower value */
10968 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
10969
10970 if ((bbr->r_ctl.rc_lost == 0) &&
10971 (rtt_gain < bbr_rtt_gain_thresh)) {
10972 /*
10973 * No loss, and we are under
10974 * our gain threhold for
10975 * increasing RTT.
10976 */
10977 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
10978 bbr->r_ctl.rc_bbr_last_startup_epoch++;
10979 bbr_log_startup_event(bbr, cts, rtt_gain,
10980 delta, bbr->r_ctl.startup_last_srtt, 10);
10981 return (0);
10982 }
10983 }
10984 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) &&
10985 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) &&
10986 (!IN_RECOVERY(bbr->rc_tp->t_flags))) {
10987 /*
10988 * We only assess if we have a new measurement when
10989 * we have no loss and are not in recovery.
10990 * Drag up by one our last_startup epoch so we will hold
10991 * the number of non-gain we have already accumulated.
10992 */
10993 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
10994 bbr->r_ctl.rc_bbr_last_startup_epoch++;
10995 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10996 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9);
10997 return (0);
10998 }
10999 /* Case where we reduced the lost (bad retransmit) */
11000 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost)
11001 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11002 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count;
11003 btlbw = bbr_get_full_bw(bbr);
11004 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower)
11005 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11006 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11007 else
11008 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11009 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11010 do_exit = 0;
11011 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw)
11012 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11013 if (btlbw >= gain) {
11014 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11015 /* Update the lost so we won't exit in next set of tests */
11016 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11017 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11018 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11019 }
11020 if ((bbr->rc_loss_exit &&
11021 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11022 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) &&
11023 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) {
11024 /*
11025 * If we had no gain, we had loss and that loss was above
11026 * our threshould, the rwnd is not constrained, and we have
11027 * had at least 3 packet epochs exit. Note that this is
11028 * switched off by sysctl. Google does not do this by the
11029 * way.
11030 */
11031 if ((ctf_flight_size(bbr->rc_tp,
11032 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11033 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) {
11034 do_exit = 1;
11035 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11036 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4);
11037 } else {
11038 /* Just record an updated loss value */
11039 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11040 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11041 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5);
11042 }
11043 } else
11044 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11045 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) ||
11046 do_exit) {
11047 /* Return 1 to exit the startup state. */
11048 return (1);
11049 }
11050 /* Stay in startup */
11051 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11052 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11053 return (0);
11054 }
11055
11056 static void
bbr_state_change(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch,uint32_t losses)11057 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses)
11058 {
11059 /*
11060 * A tick occurred in the rtt epoch do we need to do anything?
11061 */
11062 #ifdef BBR_INVARIANTS
11063 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
11064 (bbr->rc_bbr_state != BBR_STATE_DRAIN) &&
11065 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) &&
11066 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
11067 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) {
11068 /* Debug code? */
11069 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state);
11070 }
11071 #endif
11072 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
11073 /* Do we exit the startup state? */
11074 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) {
11075 uint32_t time_in;
11076
11077 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11078 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6);
11079 bbr->rc_filled_pipe = 1;
11080 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11081 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11082 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11083 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11084 } else
11085 time_in = 0;
11086 if (bbr->rc_no_pacing)
11087 bbr->rc_no_pacing = 0;
11088 bbr->r_ctl.rc_bbr_state_time = cts;
11089 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg;
11090 bbr->rc_bbr_state = BBR_STATE_DRAIN;
11091 bbr_set_state_target(bbr, __LINE__);
11092 if ((bbr->rc_use_google == 0) &&
11093 bbr_slam_cwnd_in_main_drain) {
11094 /* Here we don't have to worry about probe-rtt */
11095 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
11096 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11097 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11098 }
11099 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
11100 bbr_log_type_statechange(bbr, cts, __LINE__);
11101 if (ctf_flight_size(bbr->rc_tp,
11102 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
11103 bbr->r_ctl.rc_target_at_state) {
11104 /*
11105 * Switch to probe_bw if we are already
11106 * there
11107 */
11108 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11109 bbr_substate_change(bbr, cts, __LINE__, 0);
11110 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11111 bbr_log_type_statechange(bbr, cts, __LINE__);
11112 }
11113 }
11114 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) {
11115 uint32_t inflight;
11116 struct tcpcb *tp;
11117
11118 tp = bbr->rc_tp;
11119 inflight = ctf_flight_size(tp,
11120 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11121 if (inflight >= bbr->r_ctl.rc_target_at_state) {
11122 /* We have reached a flight of the cwnd target */
11123 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11124 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11125 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
11126 bbr_set_state_target(bbr, __LINE__);
11127 /*
11128 * Rig it so we don't do anything crazy and
11129 * start fresh with a new randomization.
11130 */
11131 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
11132 bbr->rc_bbr_substate = BBR_SUB_LEVEL6;
11133 bbr_substate_change(bbr, cts, __LINE__, 1);
11134 }
11135 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) {
11136 /* Has in-flight reached the bdp (or less)? */
11137 uint32_t inflight;
11138 struct tcpcb *tp;
11139
11140 tp = bbr->rc_tp;
11141 inflight = ctf_flight_size(tp,
11142 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11143 if ((bbr->rc_use_google == 0) &&
11144 bbr_slam_cwnd_in_main_drain &&
11145 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11146 /*
11147 * Here we don't have to worry about probe-rtt
11148 * re-slam it, but keep it slammed down.
11149 */
11150 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11151 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11152 }
11153 if (inflight <= bbr->r_ctl.rc_target_at_state) {
11154 /* We have drained */
11155 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11156 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11157 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11158 uint32_t time_in;
11159
11160 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11161 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11162 }
11163 if ((bbr->rc_use_google == 0) &&
11164 bbr_slam_cwnd_in_main_drain &&
11165 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
11166 /* Restore the cwnd */
11167 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11168 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11169 }
11170 /* Setup probe-rtt has being done now RRS-HERE */
11171 bbr->r_ctl.rc_rtt_shrinks = cts;
11172 bbr->r_ctl.last_in_probertt = cts;
11173 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0);
11174 /* Randomly pick a sub-state */
11175 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11176 bbr_substate_change(bbr, cts, __LINE__, 0);
11177 bbr_log_type_statechange(bbr, cts, __LINE__);
11178 }
11179 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) {
11180 uint32_t flight;
11181
11182 flight = ctf_flight_size(bbr->rc_tp,
11183 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11184 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered);
11185 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) &&
11186 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11187 /*
11188 * We must keep cwnd at the desired MSS.
11189 */
11190 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
11191 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11192 } else if ((bbr_prtt_slam_cwnd) &&
11193 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11194 /* Re-slam it */
11195 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11196 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11197 }
11198 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) {
11199 /* Has outstanding reached our target? */
11200 if (flight <= bbr->r_ctl.rc_target_at_state) {
11201 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0);
11202 bbr->r_ctl.rc_bbr_enters_probertt = cts;
11203 /* If time is exactly 0, be 1usec off */
11204 if (bbr->r_ctl.rc_bbr_enters_probertt == 0)
11205 bbr->r_ctl.rc_bbr_enters_probertt = 1;
11206 if (bbr->rc_use_google == 0) {
11207 /*
11208 * Restore any lowering that as occurred to
11209 * reach here
11210 */
11211 if (bbr->r_ctl.bbr_rttprobe_gain_val)
11212 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
11213 else
11214 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11215 }
11216 }
11217 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) &&
11218 (bbr->rc_use_google == 0) &&
11219 bbr->r_ctl.bbr_rttprobe_gain_val &&
11220 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) ||
11221 (flight >= bbr->r_ctl.flightsize_at_drain))) {
11222 /*
11223 * We have doddled with our current hptsi
11224 * gain an srtt and have still not made it
11225 * to target, or we have increased our flight.
11226 * Lets reduce the gain by xx%
11227 * flooring the reduce at DRAIN (based on
11228 * mul/div)
11229 */
11230 int red;
11231
11232 bbr->r_ctl.flightsize_at_drain = flight;
11233 bbr->r_ctl.rc_probertt_srttchktim = cts;
11234 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1);
11235 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) {
11236 /* Reduce our gain again */
11237 bbr->r_ctl.rc_bbr_hptsi_gain -= red;
11238 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0);
11239 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) {
11240 /* one more chance before we give up */
11241 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
11242 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0);
11243 } else {
11244 /* At the very bottom */
11245 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1);
11246 }
11247 }
11248 }
11249 if (bbr->r_ctl.rc_bbr_enters_probertt &&
11250 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) &&
11251 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) {
11252 /* Time to exit probe RTT normally */
11253 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts);
11254 }
11255 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
11256 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11257 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11258 /*
11259 * This qualifies as a RTT_PROBE session since we
11260 * drop the data outstanding to nothing and waited
11261 * more than bbr_rtt_probe_time.
11262 */
11263 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11264 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11265 }
11266 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11267 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11268 } else {
11269 bbr_set_probebw_gains(bbr, cts, losses);
11270 }
11271 }
11272 }
11273
11274 static void
bbr_check_bbr_for_state(struct tcp_bbr * bbr,uint32_t cts,int32_t line,uint32_t losses)11275 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses)
11276 {
11277 int32_t epoch = 0;
11278
11279 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
11280 bbr_set_epoch(bbr, cts, line);
11281 /* At each epoch doe lt bw sampling */
11282 epoch = 1;
11283 }
11284 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses);
11285 }
11286
11287 static int
bbr_do_segment_nounlock(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos,int32_t nxt_pkt,struct timeval * tv)11288 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
11289 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
11290 struct timeval *tv)
11291 {
11292 struct inpcb *inp = tptoinpcb(tp);
11293 struct socket *so = tptosocket(tp);
11294 int32_t thflags, retval;
11295 uint32_t cts, lcts;
11296 uint32_t tiwin;
11297 struct tcpopt to;
11298 struct tcp_bbr *bbr;
11299 struct bbr_sendmap *rsm;
11300 struct timeval ltv;
11301 int32_t did_out = 0;
11302 uint16_t nsegs;
11303 int32_t prev_state;
11304 uint32_t lost;
11305
11306 nsegs = max(1, m->m_pkthdr.lro_nsegs);
11307 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11308 /* add in our stats */
11309 kern_prefetch(bbr, &prev_state);
11310 prev_state = 0;
11311 thflags = tcp_get_flags(th);
11312 /*
11313 * If this is either a state-changing packet or current state isn't
11314 * established, we require a write lock on tcbinfo. Otherwise, we
11315 * allow the tcbinfo to be in either alocked or unlocked, as the
11316 * caller may have unnecessarily acquired a write lock due to a
11317 * race.
11318 */
11319 INP_WLOCK_ASSERT(tptoinpcb(tp));
11320 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
11321 __func__));
11322 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
11323 __func__));
11324
11325 tp->t_rcvtime = ticks;
11326 /*
11327 * Unscale the window into a 32-bit value. For the SYN_SENT state
11328 * the scale is zero.
11329 */
11330 tiwin = th->th_win << tp->snd_scale;
11331 #ifdef STATS
11332 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
11333 #endif
11334
11335 if (m->m_flags & M_TSTMP) {
11336 /* Prefer the hardware timestamp if present */
11337 struct timespec ts;
11338
11339 mbuf_tstmp2timespec(m, &ts);
11340 bbr->rc_tv.tv_sec = ts.tv_sec;
11341 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11342 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11343 } else if (m->m_flags & M_TSTMP_LRO) {
11344 /* Next the arrival timestamp */
11345 struct timespec ts;
11346
11347 mbuf_tstmp2timespec(m, &ts);
11348 bbr->rc_tv.tv_sec = ts.tv_sec;
11349 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11350 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11351 } else {
11352 /*
11353 * Ok just get the current time.
11354 */
11355 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv);
11356 }
11357 /*
11358 * Parse options on any incoming segment.
11359 */
11360 tcp_dooptions(&to, (u_char *)(th + 1),
11361 (th->th_off << 2) - sizeof(struct tcphdr),
11362 (thflags & TH_SYN) ? TO_SYN : 0);
11363
11364 /*
11365 * If timestamps were negotiated during SYN/ACK and a
11366 * segment without a timestamp is received, silently drop
11367 * the segment, unless it is a RST segment or missing timestamps are
11368 * tolerated.
11369 * See section 3.2 of RFC 7323.
11370 */
11371 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
11372 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
11373 retval = 0;
11374 m_freem(m);
11375 goto done_with_input;
11376 }
11377 /*
11378 * If echoed timestamp is later than the current time, fall back to
11379 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
11380 * were used when this connection was established.
11381 */
11382 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
11383 to.to_tsecr -= tp->ts_offset;
11384 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv)))
11385 to.to_tsecr = 0;
11386 }
11387 /*
11388 * If its the first time in we need to take care of options and
11389 * verify we can do SACK for rack!
11390 */
11391 if (bbr->r_state == 0) {
11392 /*
11393 * Process options only when we get SYN/ACK back. The SYN
11394 * case for incoming connections is handled in tcp_syncache.
11395 * According to RFC1323 the window field in a SYN (i.e., a
11396 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
11397 * this is traditional behavior, may need to be cleaned up.
11398 */
11399 if (bbr->rc_inp == NULL) {
11400 bbr->rc_inp = inp;
11401 }
11402 /*
11403 * We need to init rc_inp here since its not init'd when
11404 * bbr_init is called
11405 */
11406 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
11407 if ((to.to_flags & TOF_SCALE) &&
11408 (tp->t_flags & TF_REQ_SCALE)) {
11409 tp->t_flags |= TF_RCVD_SCALE;
11410 tp->snd_scale = to.to_wscale;
11411 } else
11412 tp->t_flags &= ~TF_REQ_SCALE;
11413 /*
11414 * Initial send window. It will be updated with the
11415 * next incoming segment to the scaled value.
11416 */
11417 tp->snd_wnd = th->th_win;
11418 if ((to.to_flags & TOF_TS) &&
11419 (tp->t_flags & TF_REQ_TSTMP)) {
11420 tp->t_flags |= TF_RCVD_TSTMP;
11421 tp->ts_recent = to.to_tsval;
11422 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
11423 } else
11424 tp->t_flags &= ~TF_REQ_TSTMP;
11425 if (to.to_flags & TOF_MSS)
11426 tcp_mss(tp, to.to_mss);
11427 if ((tp->t_flags & TF_SACK_PERMIT) &&
11428 (to.to_flags & TOF_SACKPERM) == 0)
11429 tp->t_flags &= ~TF_SACK_PERMIT;
11430 if (IS_FASTOPEN(tp->t_flags)) {
11431 if (to.to_flags & TOF_FASTOPEN) {
11432 uint16_t mss;
11433
11434 if (to.to_flags & TOF_MSS)
11435 mss = to.to_mss;
11436 else
11437 if ((inp->inp_vflag & INP_IPV6) != 0)
11438 mss = TCP6_MSS;
11439 else
11440 mss = TCP_MSS;
11441 tcp_fastopen_update_cache(tp, mss,
11442 to.to_tfo_len, to.to_tfo_cookie);
11443 } else
11444 tcp_fastopen_disable_path(tp);
11445 }
11446 }
11447 /*
11448 * At this point we are at the initial call. Here we decide
11449 * if we are doing RACK or not. We do this by seeing if
11450 * TF_SACK_PERMIT is set, if not rack is *not* possible and
11451 * we switch to the default code.
11452 */
11453 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
11454 /* Bail */
11455 tcp_switch_back_to_default(tp);
11456 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
11457 tlen, iptos);
11458 return (1);
11459 }
11460 /* Set the flag */
11461 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
11462 tcp_set_hpts(tp);
11463 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack);
11464 }
11465 if (thflags & TH_ACK) {
11466 /* Track ack types */
11467 if (to.to_flags & TOF_SACK)
11468 BBR_STAT_INC(bbr_acks_with_sacks);
11469 else
11470 BBR_STAT_INC(bbr_plain_acks);
11471 }
11472 /*
11473 * This is the one exception case where we set the rack state
11474 * always. All other times (timers etc) we must have a rack-state
11475 * set (so we assure we have done the checks above for SACK).
11476 */
11477 if (thflags & TH_FIN)
11478 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
11479 if (bbr->r_state != tp->t_state)
11480 bbr_set_state(tp, bbr, tiwin);
11481
11482 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL)
11483 kern_prefetch(rsm, &prev_state);
11484 prev_state = bbr->r_state;
11485 bbr->rc_ack_was_delayed = 0;
11486 lost = bbr->r_ctl.rc_lost;
11487 bbr->rc_is_pkt_epoch_now = 0;
11488 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) {
11489 /* Get the real time into lcts and figure the real delay */
11490 lcts = tcp_get_usecs(<v);
11491 if (TSTMP_GT(lcts, cts)) {
11492 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts;
11493 bbr->rc_ack_was_delayed = 1;
11494 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay,
11495 bbr->r_ctl.highest_hdwr_delay))
11496 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay;
11497 } else {
11498 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11499 bbr->rc_ack_was_delayed = 0;
11500 }
11501 } else {
11502 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11503 bbr->rc_ack_was_delayed = 0;
11504 }
11505 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m);
11506 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
11507 retval = 0;
11508 m_freem(m);
11509 goto done_with_input;
11510 }
11511 /*
11512 * If a segment with the ACK-bit set arrives in the SYN-SENT state
11513 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
11514 */
11515 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
11516 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
11517 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11518 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11519 return (1);
11520 }
11521 if (tiwin > bbr->r_ctl.rc_high_rwnd)
11522 bbr->r_ctl.rc_high_rwnd = tiwin;
11523 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp,
11524 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11525 bbr->rtt_valid = 0;
11526 if (to.to_flags & TOF_TS) {
11527 bbr->rc_ts_valid = 1;
11528 bbr->r_ctl.last_inbound_ts = to.to_tsval;
11529 } else {
11530 bbr->rc_ts_valid = 0;
11531 bbr->r_ctl.last_inbound_ts = 0;
11532 }
11533 retval = (*bbr->r_substate) (m, th, so,
11534 tp, &to, drop_hdrlen,
11535 tlen, tiwin, thflags, nxt_pkt, iptos);
11536 if (nxt_pkt == 0)
11537 BBR_STAT_INC(bbr_rlock_left_ret0);
11538 else
11539 BBR_STAT_INC(bbr_rlock_left_ret1);
11540 if (retval == 0) {
11541 /*
11542 * If retval is 1 the tcb is unlocked and most likely the tp
11543 * is gone.
11544 */
11545 INP_WLOCK_ASSERT(inp);
11546 tcp_bbr_xmit_timer_commit(bbr, tp, cts);
11547 if (bbr->rc_is_pkt_epoch_now)
11548 bbr_set_pktepoch(bbr, cts, __LINE__);
11549 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost));
11550 if (nxt_pkt == 0) {
11551 if (bbr->r_wanted_output != 0) {
11552 bbr->rc_output_starts_timer = 0;
11553 did_out = 1;
11554 if (tcp_output(tp) < 0)
11555 return (1);
11556 } else
11557 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0);
11558 }
11559 if ((nxt_pkt == 0) &&
11560 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11561 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11562 (tp->t_flags & TF_DELACK) ||
11563 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11564 (tp->t_state <= TCPS_CLOSING)))) {
11565 /*
11566 * We could not send (probably in the hpts but
11567 * stopped the timer)?
11568 */
11569 if ((tp->snd_max == tp->snd_una) &&
11570 ((tp->t_flags & TF_DELACK) == 0) &&
11571 (tcp_in_hpts(tp)) &&
11572 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11573 /*
11574 * keep alive not needed if we are hptsi
11575 * output yet
11576 */
11577 ;
11578 } else {
11579 if (tcp_in_hpts(tp)) {
11580 tcp_hpts_remove(tp);
11581 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11582 (TSTMP_GT(lcts, bbr->rc_pacer_started))) {
11583 uint32_t del;
11584
11585 del = lcts - bbr->rc_pacer_started;
11586 if (bbr->r_ctl.rc_last_delay_val > del) {
11587 BBR_STAT_INC(bbr_force_timer_start);
11588 bbr->r_ctl.rc_last_delay_val -= del;
11589 bbr->rc_pacer_started = lcts;
11590 } else {
11591 /* We are late */
11592 bbr->r_ctl.rc_last_delay_val = 0;
11593 BBR_STAT_INC(bbr_force_output);
11594 if (tcp_output(tp) < 0)
11595 return (1);
11596 }
11597 }
11598 }
11599 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val,
11600 0);
11601 }
11602 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) {
11603 /* Do we have the correct timer running? */
11604 bbr_timer_audit(tp, bbr, lcts, &so->so_snd);
11605 }
11606 /* Clear the flag, it may have been cleared by output but we may not have */
11607 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
11608 tp->t_flags2 &= ~TF2_HPTS_CALLS;
11609 /* Do we have a new state */
11610 if (bbr->r_state != tp->t_state)
11611 bbr_set_state(tp, bbr, tiwin);
11612 done_with_input:
11613 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out);
11614 if (did_out)
11615 bbr->r_wanted_output = 0;
11616 }
11617 return (retval);
11618 }
11619
11620 static void
bbr_do_segment(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos)11621 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
11622 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11623 {
11624 struct timeval tv;
11625 int retval;
11626
11627 /* First lets see if we have old packets */
11628 if (!STAILQ_EMPTY(&tp->t_inqueue)) {
11629 if (ctf_do_queued_segments(tp, 1)) {
11630 m_freem(m);
11631 return;
11632 }
11633 }
11634 if (m->m_flags & M_TSTMP_LRO) {
11635 mbuf_tstmp2timeval(m, &tv);
11636 } else {
11637 /* Should not be should we kassert instead? */
11638 tcp_get_usecs(&tv);
11639 }
11640 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos,
11641 0, &tv);
11642 if (retval == 0) {
11643 INP_WUNLOCK(tptoinpcb(tp));
11644 }
11645 }
11646
11647 /*
11648 * Return how much data can be sent without violating the
11649 * cwnd or rwnd.
11650 */
11651
11652 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)11653 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin,
11654 uint32_t avail, int32_t sb_offset, uint32_t cts)
11655 {
11656 uint32_t len;
11657
11658 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11659 /* We never want to go over our peers rcv-window */
11660 len = 0;
11661 } else {
11662 uint32_t flight;
11663
11664 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11665 if (flight >= sendwin) {
11666 /*
11667 * We have in flight what we are allowed by cwnd (if
11668 * it was rwnd blocking it would have hit above out
11669 * >= tp->snd_wnd).
11670 */
11671 return (0);
11672 }
11673 len = sendwin - flight;
11674 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11675 /* We would send too much (beyond the rwnd) */
11676 len = tp->snd_wnd - ctf_outstanding(tp);
11677 }
11678 if ((len + sb_offset) > avail) {
11679 /*
11680 * We don't have that much in the SB, how much is
11681 * there?
11682 */
11683 len = avail - sb_offset;
11684 }
11685 }
11686 return (len);
11687 }
11688
11689 static inline void
bbr_do_send_accounting(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,int32_t len,int32_t error)11690 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11691 {
11692 if (error) {
11693 return;
11694 }
11695 if (rsm) {
11696 if (rsm->r_flags & BBR_TLP) {
11697 /*
11698 * TLP should not count in retran count, but in its
11699 * own bin
11700 */
11701 KMOD_TCPSTAT_INC(tcps_tlpresends);
11702 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len);
11703 } else {
11704 /* Retransmit */
11705 tp->t_sndrexmitpack++;
11706 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
11707 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
11708 #ifdef STATS
11709 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
11710 len);
11711 #endif
11712 }
11713 /*
11714 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is
11715 * sub-state
11716 */
11717 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len);
11718 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) {
11719 /* Non probe_bw log in 1, 2, or 4. */
11720 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len);
11721 } else {
11722 /*
11723 * Log our probe state 3, and log also 5-13 to show
11724 * us the recovery sub-state for the send. This
11725 * means that 3 == (5+6+7+8+9+10+11+12+13)
11726 */
11727 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len);
11728 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len);
11729 }
11730 /* Place in both 16's the totals of retransmitted */
11731 counter_u64_add(bbr_state_lost[16], len);
11732 counter_u64_add(bbr_state_resend[16], len);
11733 /* Place in 17's the total sent */
11734 counter_u64_add(bbr_state_resend[17], len);
11735 counter_u64_add(bbr_state_lost[17], len);
11736
11737 } else {
11738 /* New sends */
11739 KMOD_TCPSTAT_INC(tcps_sndpack);
11740 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
11741 /* Place in 17's the total sent */
11742 counter_u64_add(bbr_state_resend[17], len);
11743 counter_u64_add(bbr_state_lost[17], len);
11744 #ifdef STATS
11745 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
11746 len);
11747 #endif
11748 }
11749 }
11750
11751 static void
bbr_cwnd_limiting(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t in_level)11752 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level)
11753 {
11754 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) {
11755 /*
11756 * Limit the cwnd to not be above N x the target plus whats
11757 * is outstanding. The target is based on the current b/w
11758 * estimate.
11759 */
11760 uint32_t target;
11761
11762 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT);
11763 target += ctf_outstanding(tp);
11764 target *= bbr_target_cwnd_mult_limit;
11765 if (tp->snd_cwnd > target)
11766 tp->snd_cwnd = target;
11767 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__);
11768 }
11769 }
11770
11771 static int
bbr_window_update_needed(struct tcpcb * tp,struct socket * so,uint32_t recwin,int32_t maxseg)11772 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg)
11773 {
11774 /*
11775 * "adv" is the amount we could increase the window, taking into
11776 * account that we are limited by TCP_MAXWIN << tp->rcv_scale.
11777 */
11778 int32_t adv;
11779 int32_t oldwin;
11780
11781 adv = recwin;
11782 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
11783 oldwin = (tp->rcv_adv - tp->rcv_nxt);
11784 if (adv > oldwin)
11785 adv -= oldwin;
11786 else {
11787 /* We can't increase the window */
11788 adv = 0;
11789 }
11790 } else
11791 oldwin = 0;
11792
11793 /*
11794 * If the new window size ends up being the same as or less
11795 * than the old size when it is scaled, then don't force
11796 * a window update.
11797 */
11798 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
11799 return (0);
11800
11801 if (adv >= (2 * maxseg) &&
11802 (adv >= (so->so_rcv.sb_hiwat / 4) ||
11803 recwin <= (so->so_rcv.sb_hiwat / 8) ||
11804 so->so_rcv.sb_hiwat <= 8 * maxseg)) {
11805 return (1);
11806 }
11807 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
11808 return (1);
11809 return (0);
11810 }
11811
11812 /*
11813 * Return 0 on success and a errno on failure to send.
11814 * Note that a 0 return may not mean we sent anything
11815 * if the TCB was on the hpts. A non-zero return
11816 * does indicate the error we got from ip[6]_output.
11817 */
11818 static int
bbr_output_wtime(struct tcpcb * tp,const struct timeval * tv)11819 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv)
11820 {
11821 struct socket *so;
11822 int32_t len;
11823 uint32_t cts;
11824 uint32_t recwin, sendwin;
11825 int32_t sb_offset;
11826 int32_t flags, abandon, error = 0;
11827 struct tcp_log_buffer *lgb;
11828 struct mbuf *m;
11829 struct mbuf *mb;
11830 uint32_t if_hw_tsomaxsegcount = 0;
11831 uint32_t if_hw_tsomaxsegsize = 0;
11832 uint32_t if_hw_tsomax = 0;
11833 struct ip *ip = NULL;
11834 struct tcp_bbr *bbr;
11835 struct tcphdr *th;
11836 struct udphdr *udp = NULL;
11837 u_char opt[TCP_MAXOLEN];
11838 unsigned ipoptlen, optlen, hdrlen;
11839 unsigned ulen;
11840 uint32_t bbr_seq;
11841 uint32_t delay_calc=0;
11842 uint8_t doing_tlp = 0;
11843 uint8_t local_options;
11844 #ifdef BBR_INVARIANTS
11845 uint8_t doing_retran_from = 0;
11846 uint8_t picked_up_retran = 0;
11847 #endif
11848 uint8_t wanted_cookie = 0;
11849 uint8_t more_to_rxt=0;
11850 int32_t prefetch_so_done = 0;
11851 int32_t prefetch_rsm = 0;
11852 uint32_t tot_len = 0;
11853 uint32_t maxseg, pace_max_segs, p_maxseg;
11854 int32_t csum_flags = 0;
11855 int32_t hw_tls;
11856 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
11857 unsigned ipsec_optlen = 0;
11858
11859 #endif
11860 volatile int32_t sack_rxmit;
11861 struct bbr_sendmap *rsm = NULL;
11862 int32_t tso, mtu;
11863 struct tcpopt to;
11864 int32_t slot = 0;
11865 struct inpcb *inp;
11866 struct sockbuf *sb;
11867 bool hpts_calling;
11868 #ifdef INET6
11869 struct ip6_hdr *ip6 = NULL;
11870 int32_t isipv6;
11871 #endif
11872 uint8_t app_limited = BBR_JR_SENT_DATA;
11873 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11874 /* We take a cache hit here */
11875 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval));
11876 cts = tcp_tv_to_usectick(&bbr->rc_tv);
11877 inp = bbr->rc_inp;
11878 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
11879 tp->t_flags2 &= ~TF2_HPTS_CALLS;
11880 so = inp->inp_socket;
11881 sb = &so->so_snd;
11882 if (tp->t_nic_ktls_xmit)
11883 hw_tls = 1;
11884 else
11885 hw_tls = 0;
11886 kern_prefetch(sb, &maxseg);
11887 maxseg = tp->t_maxseg - bbr->rc_last_options;
11888 if (bbr_minseg(bbr) < maxseg) {
11889 tcp_bbr_tso_size_check(bbr, cts);
11890 }
11891 /* Remove any flags that indicate we are pacing on the inp */
11892 pace_max_segs = bbr->r_ctl.rc_pace_max_segs;
11893 p_maxseg = min(maxseg, pace_max_segs);
11894 INP_WLOCK_ASSERT(inp);
11895 #ifdef TCP_OFFLOAD
11896 if (tp->t_flags & TF_TOE)
11897 return (tcp_offload_output(tp));
11898 #endif
11899
11900 #ifdef INET6
11901 if (bbr->r_state) {
11902 /* Use the cache line loaded if possible */
11903 isipv6 = bbr->r_is_v6;
11904 } else {
11905 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
11906 }
11907 #endif
11908 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
11909 tcp_in_hpts(tp)) {
11910 /*
11911 * We are on the hpts for some timer but not hptsi output.
11912 * Possibly remove from the hpts so we can send/recv etc.
11913 */
11914 if ((tp->t_flags & TF_ACKNOW) == 0) {
11915 /*
11916 * No immediate demand right now to send an ack, but
11917 * the user may have read, making room for new data
11918 * (a window update). If so we may want to cancel
11919 * whatever timer is running (KEEP/DEL-ACK?) and
11920 * continue to send out a window update. Or we may
11921 * have gotten more data into the socket buffer to
11922 * send.
11923 */
11924 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
11925 (long)TCP_MAXWIN << tp->rcv_scale);
11926 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) &&
11927 ((tcp_outflags[tp->t_state] & TH_RST) == 0) &&
11928 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <=
11929 (tp->snd_max - tp->snd_una))) {
11930 /*
11931 * Nothing new to send and no window update
11932 * is needed to send. Lets just return and
11933 * let the timer-run off.
11934 */
11935 return (0);
11936 }
11937 }
11938 tcp_hpts_remove(tp);
11939 bbr_timer_cancel(bbr, __LINE__, cts);
11940 }
11941 if (bbr->r_ctl.rc_last_delay_val) {
11942 /* Calculate a rough delay for early escape to sending */
11943 if (SEQ_GT(cts, bbr->rc_pacer_started))
11944 delay_calc = cts - bbr->rc_pacer_started;
11945 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
11946 delay_calc -= bbr->r_ctl.rc_last_delay_val;
11947 else
11948 delay_calc = 0;
11949 }
11950 /* Mark that we have called bbr_output(). */
11951 if ((bbr->r_timer_override) ||
11952 (tp->t_state < TCPS_ESTABLISHED)) {
11953 /* Timeouts or early states are exempt */
11954 if (tcp_in_hpts(tp))
11955 tcp_hpts_remove(tp);
11956 } else if (tcp_in_hpts(tp)) {
11957 if ((bbr->r_ctl.rc_last_delay_val) &&
11958 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11959 delay_calc) {
11960 /*
11961 * We were being paced for output and the delay has
11962 * already exceeded when we were supposed to be
11963 * called, lets go ahead and pull out of the hpts
11964 * and call output.
11965 */
11966 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1);
11967 bbr->r_ctl.rc_last_delay_val = 0;
11968 tcp_hpts_remove(tp);
11969 } else if (tp->t_state == TCPS_CLOSED) {
11970 bbr->r_ctl.rc_last_delay_val = 0;
11971 tcp_hpts_remove(tp);
11972 } else {
11973 /*
11974 * On the hpts, you shall not pass! even if ACKNOW
11975 * is on, we will when the hpts fires, unless of
11976 * course we are overdue.
11977 */
11978 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1);
11979 return (0);
11980 }
11981 }
11982 bbr->rc_cwnd_limited = 0;
11983 if (bbr->r_ctl.rc_last_delay_val) {
11984 /* recalculate the real delay and deal with over/under */
11985 if (SEQ_GT(cts, bbr->rc_pacer_started))
11986 delay_calc = cts - bbr->rc_pacer_started;
11987 else
11988 delay_calc = 0;
11989 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
11990 /* Setup the delay which will be added in */
11991 delay_calc -= bbr->r_ctl.rc_last_delay_val;
11992 else {
11993 /*
11994 * We are early setup to adjust
11995 * our slot time.
11996 */
11997 uint64_t merged_val;
11998
11999 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc);
12000 bbr->r_agg_early_set = 1;
12001 if (bbr->r_ctl.rc_hptsi_agg_delay) {
12002 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) {
12003 /* Nope our previous late cancels out the early */
12004 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early;
12005 bbr->r_agg_early_set = 0;
12006 bbr->r_ctl.rc_agg_early = 0;
12007 } else {
12008 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay;
12009 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12010 }
12011 }
12012 merged_val = bbr->rc_pacer_started;
12013 merged_val <<= 32;
12014 merged_val |= bbr->r_ctl.rc_last_delay_val;
12015 bbr_log_pacing_delay_calc(bbr, hpts_calling,
12016 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val,
12017 bbr->r_agg_early_set, 3);
12018 bbr->r_ctl.rc_last_delay_val = 0;
12019 BBR_STAT_INC(bbr_early);
12020 delay_calc = 0;
12021 }
12022 } else {
12023 /* We were not delayed due to hptsi */
12024 if (bbr->r_agg_early_set)
12025 bbr->r_ctl.rc_agg_early = 0;
12026 bbr->r_agg_early_set = 0;
12027 delay_calc = 0;
12028 }
12029 if (delay_calc) {
12030 /*
12031 * We had a hptsi delay which means we are falling behind on
12032 * sending at the expected rate. Calculate an extra amount
12033 * of data we can send, if any, to put us back on track.
12034 */
12035 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay)
12036 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff;
12037 else
12038 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc;
12039 }
12040 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12041 if ((tp->snd_una == tp->snd_max) &&
12042 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
12043 (sbavail(sb))) {
12044 /*
12045 * Ok we have been idle with nothing outstanding
12046 * we possibly need to start fresh with either a new
12047 * suite of states or a fast-ramp up.
12048 */
12049 bbr_restart_after_idle(bbr,
12050 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time));
12051 }
12052 /*
12053 * Now was there a hptsi delay where we are behind? We only count
12054 * being behind if: a) We are not in recovery. b) There was a delay.
12055 * <and> c) We had room to send something.
12056 *
12057 */
12058 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
12059 int retval;
12060
12061 retval = bbr_process_timers(tp, bbr, cts, hpts_calling);
12062 if (retval != 0) {
12063 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1);
12064 /*
12065 * If timers want tcp_drop(), then pass error out,
12066 * otherwise suppress it.
12067 */
12068 return (retval < 0 ? retval : 0);
12069 }
12070 }
12071 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY;
12072 if (hpts_calling &&
12073 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12074 bbr->r_ctl.rc_last_delay_val = 0;
12075 }
12076 bbr->r_timer_override = 0;
12077 bbr->r_wanted_output = 0;
12078 /*
12079 * For TFO connections in SYN_RECEIVED, only allow the initial
12080 * SYN|ACK and those sent by the retransmit timer.
12081 */
12082 if (IS_FASTOPEN(tp->t_flags) &&
12083 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12084 (tp->t_state == TCPS_SYN_SENT)) &&
12085 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12086 (tp->t_rxtshift == 0)) { /* not a retransmit */
12087 len = 0;
12088 goto just_return_nolock;
12089 }
12090 /*
12091 * Before sending anything check for a state update. For hpts
12092 * calling without input this is important. If its input calling
12093 * then this was already done.
12094 */
12095 if (bbr->rc_use_google == 0)
12096 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12097 again:
12098 /*
12099 * If we've recently taken a timeout, snd_max will be greater than
12100 * snd_max. BBR in general does not pay much attention to snd_nxt
12101 * for historic reasons the persist timer still uses it. This means
12102 * we have to look at it. All retransmissions that are not persits
12103 * use the rsm that needs to be sent so snd_nxt is ignored. At the
12104 * end of this routine we pull snd_nxt always up to snd_max.
12105 */
12106 doing_tlp = 0;
12107 #ifdef BBR_INVARIANTS
12108 doing_retran_from = picked_up_retran = 0;
12109 #endif
12110 error = 0;
12111 tso = 0;
12112 slot = 0;
12113 mtu = 0;
12114 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12115 sb_offset = tp->snd_max - tp->snd_una;
12116 flags = tcp_outflags[tp->t_state];
12117 sack_rxmit = 0;
12118 len = 0;
12119 rsm = NULL;
12120 if (flags & TH_RST) {
12121 SOCKBUF_LOCK(sb);
12122 goto send;
12123 }
12124 recheck_resend:
12125 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
12126 /* We need to always have one in reserve */
12127 rsm = bbr_alloc(bbr);
12128 if (rsm == NULL) {
12129 error = ENOMEM;
12130 /* Lie to get on the hpts */
12131 tot_len = tp->t_maxseg;
12132 if (hpts_calling)
12133 /* Retry in a ms */
12134 slot = 1001;
12135 goto just_return_nolock;
12136 }
12137 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
12138 bbr->r_ctl.rc_free_cnt++;
12139 rsm = NULL;
12140 }
12141 /* What do we send, a resend? */
12142 if (bbr->r_ctl.rc_resend == NULL) {
12143 /* Check for rack timeout */
12144 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
12145 if (bbr->r_ctl.rc_resend) {
12146 #ifdef BBR_INVARIANTS
12147 picked_up_retran = 1;
12148 #endif
12149 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend);
12150 }
12151 }
12152 if (bbr->r_ctl.rc_resend) {
12153 rsm = bbr->r_ctl.rc_resend;
12154 #ifdef BBR_INVARIANTS
12155 doing_retran_from = 1;
12156 #endif
12157 /* Remove any TLP flags its a RACK or T-O */
12158 rsm->r_flags &= ~BBR_TLP;
12159 bbr->r_ctl.rc_resend = NULL;
12160 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12161 #ifdef BBR_INVARIANTS
12162 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n",
12163 tp, bbr, rsm, rsm->r_start, tp->snd_una);
12164 goto recheck_resend;
12165 #else
12166 /* TSNH */
12167 rsm = NULL;
12168 goto recheck_resend;
12169 #endif
12170 }
12171 if (rsm->r_flags & BBR_HAS_SYN) {
12172 /* Only retransmit a SYN by itself */
12173 len = 0;
12174 if ((flags & TH_SYN) == 0) {
12175 /* Huh something is wrong */
12176 rsm->r_start++;
12177 if (rsm->r_start == rsm->r_end) {
12178 /* Clean it up, somehow we missed the ack? */
12179 bbr_log_syn(tp, NULL);
12180 } else {
12181 /* TFO with data? */
12182 rsm->r_flags &= ~BBR_HAS_SYN;
12183 len = rsm->r_end - rsm->r_start;
12184 }
12185 } else {
12186 /* Retransmitting SYN */
12187 rsm = NULL;
12188 SOCKBUF_LOCK(sb);
12189 goto send;
12190 }
12191 } else
12192 len = rsm->r_end - rsm->r_start;
12193 if ((bbr->rc_resends_use_tso == 0) &&
12194 (len > maxseg)) {
12195 len = maxseg;
12196 more_to_rxt = 1;
12197 }
12198 sb_offset = rsm->r_start - tp->snd_una;
12199 if (len > 0) {
12200 sack_rxmit = 1;
12201 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12202 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12203 min(len, maxseg));
12204 } else {
12205 /* I dont think this can happen */
12206 rsm = NULL;
12207 goto recheck_resend;
12208 }
12209 BBR_STAT_INC(bbr_resends_set);
12210 } else if (bbr->r_ctl.rc_tlp_send) {
12211 /*
12212 * Tail loss probe
12213 */
12214 doing_tlp = 1;
12215 rsm = bbr->r_ctl.rc_tlp_send;
12216 bbr->r_ctl.rc_tlp_send = NULL;
12217 sack_rxmit = 1;
12218 len = rsm->r_end - rsm->r_start;
12219 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12220 len = maxseg;
12221
12222 if (SEQ_GT(tp->snd_una, rsm->r_start)) {
12223 #ifdef BBR_INVARIANTS
12224 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u",
12225 tp, bbr, tp->snd_una, rsm, rsm->r_start);
12226 #else
12227 /* TSNH */
12228 rsm = NULL;
12229 goto recheck_resend;
12230 #endif
12231 }
12232 sb_offset = rsm->r_start - tp->snd_una;
12233 BBR_STAT_INC(bbr_tlp_set);
12234 }
12235 /*
12236 * Enforce a connection sendmap count limit if set
12237 * as long as we are not retransmiting.
12238 */
12239 if ((rsm == NULL) &&
12240 (V_tcp_map_entries_limit > 0) &&
12241 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12242 BBR_STAT_INC(bbr_alloc_limited);
12243 if (!bbr->alloc_limit_reported) {
12244 bbr->alloc_limit_reported = 1;
12245 BBR_STAT_INC(bbr_alloc_limited_conns);
12246 }
12247 goto just_return_nolock;
12248 }
12249 #ifdef BBR_INVARIANTS
12250 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) {
12251 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u",
12252 tp, bbr, rsm, sb_offset, len);
12253 }
12254 #endif
12255 /*
12256 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12257 * state flags.
12258 */
12259 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL))
12260 flags |= TH_FIN;
12261 if (tp->t_flags & TF_NEEDSYN)
12262 flags |= TH_SYN;
12263
12264 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) {
12265 /* we are retransmitting the fin */
12266 len--;
12267 if (len) {
12268 /*
12269 * When retransmitting data do *not* include the
12270 * FIN. This could happen from a TLP probe if we
12271 * allowed data with a FIN.
12272 */
12273 flags &= ~TH_FIN;
12274 }
12275 } else if (rsm) {
12276 if (flags & TH_FIN)
12277 flags &= ~TH_FIN;
12278 }
12279 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12280 void *end_rsm;
12281
12282 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
12283 if (end_rsm)
12284 kern_prefetch(end_rsm, &prefetch_rsm);
12285 prefetch_rsm = 1;
12286 }
12287 SOCKBUF_LOCK(sb);
12288 /*
12289 * If snd_nxt == snd_max and we have transmitted a FIN, the
12290 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12291 * negative length. This can also occur when TCP opens up its
12292 * congestion window while receiving additional duplicate acks after
12293 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12294 * the fast-retransmit.
12295 *
12296 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12297 * set to snd_una, the sb_offset will be 0, and the length may wind
12298 * up 0.
12299 *
12300 * If sack_rxmit is true we are retransmitting from the scoreboard
12301 * in which case len is already set.
12302 */
12303 if (sack_rxmit == 0) {
12304 uint32_t avail;
12305
12306 avail = sbavail(sb);
12307 if (SEQ_GT(tp->snd_max, tp->snd_una))
12308 sb_offset = tp->snd_max - tp->snd_una;
12309 else
12310 sb_offset = 0;
12311 if (bbr->rc_tlp_new_data) {
12312 /* TLP is forcing out new data */
12313 uint32_t tlplen;
12314
12315 doing_tlp = 1;
12316 tlplen = maxseg;
12317
12318 if (tlplen > (uint32_t)(avail - sb_offset)) {
12319 tlplen = (uint32_t)(avail - sb_offset);
12320 }
12321 if (tlplen > tp->snd_wnd) {
12322 len = tp->snd_wnd;
12323 } else {
12324 len = tlplen;
12325 }
12326 bbr->rc_tlp_new_data = 0;
12327 } else {
12328 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts);
12329 if ((len < p_maxseg) &&
12330 (bbr->rc_in_persist == 0) &&
12331 (ctf_outstanding(tp) >= (2 * p_maxseg)) &&
12332 ((avail - sb_offset) >= p_maxseg)) {
12333 /*
12334 * We are not completing whats in the socket
12335 * buffer (i.e. there is at least a segment
12336 * waiting to send) and we have 2 or more
12337 * segments outstanding. There is no sense
12338 * of sending a little piece. Lets defer and
12339 * and wait until we can send a whole
12340 * segment.
12341 */
12342 len = 0;
12343 }
12344 if (bbr->rc_in_persist) {
12345 /*
12346 * We are in persists, figure out if
12347 * a retransmit is available (maybe the previous
12348 * persists we sent) or if we have to send new
12349 * data.
12350 */
12351 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
12352 if (rsm) {
12353 len = rsm->r_end - rsm->r_start;
12354 if (rsm->r_flags & BBR_HAS_FIN)
12355 len--;
12356 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12357 len = maxseg;
12358 if (len > 1)
12359 BBR_STAT_INC(bbr_persist_reneg);
12360 /*
12361 * XXXrrs we could force the len to
12362 * 1 byte here to cause the chunk to
12363 * split apart.. but that would then
12364 * mean we always retransmit it as
12365 * one byte even after the window
12366 * opens.
12367 */
12368 sack_rxmit = 1;
12369 sb_offset = rsm->r_start - tp->snd_una;
12370 } else {
12371 /*
12372 * First time through in persists or peer
12373 * acked our one byte. Though we do have
12374 * to have something in the sb.
12375 */
12376 len = 1;
12377 sb_offset = 0;
12378 if (avail == 0)
12379 len = 0;
12380 }
12381 }
12382 }
12383 }
12384 if (prefetch_so_done == 0) {
12385 kern_prefetch(so, &prefetch_so_done);
12386 prefetch_so_done = 1;
12387 }
12388 /*
12389 * Lop off SYN bit if it has already been sent. However, if this is
12390 * SYN-SENT state and if segment contains data and if we don't know
12391 * that foreign host supports TAO, suppress sending segment.
12392 */
12393 if ((flags & TH_SYN) && (rsm == NULL) &&
12394 SEQ_GT(tp->snd_max, tp->snd_una)) {
12395 if (tp->t_state != TCPS_SYN_RECEIVED)
12396 flags &= ~TH_SYN;
12397 /*
12398 * When sending additional segments following a TFO SYN|ACK,
12399 * do not include the SYN bit.
12400 */
12401 if (IS_FASTOPEN(tp->t_flags) &&
12402 (tp->t_state == TCPS_SYN_RECEIVED))
12403 flags &= ~TH_SYN;
12404 sb_offset--, len++;
12405 if (sbavail(sb) == 0)
12406 len = 0;
12407 } else if ((flags & TH_SYN) && rsm) {
12408 /*
12409 * Subtract one from the len for the SYN being
12410 * retransmitted.
12411 */
12412 len--;
12413 }
12414 /*
12415 * Be careful not to send data and/or FIN on SYN segments. This
12416 * measure is needed to prevent interoperability problems with not
12417 * fully conformant TCP implementations.
12418 */
12419 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12420 len = 0;
12421 flags &= ~TH_FIN;
12422 }
12423 /*
12424 * On TFO sockets, ensure no data is sent in the following cases:
12425 *
12426 * - When retransmitting SYN|ACK on a passively-created socket
12427 * - When retransmitting SYN on an actively created socket
12428 * - When sending a zero-length cookie (cookie request) on an
12429 * actively created socket
12430 * - When the socket is in the CLOSED state (RST is being sent)
12431 */
12432 if (IS_FASTOPEN(tp->t_flags) &&
12433 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12434 ((tp->t_state == TCPS_SYN_SENT) &&
12435 (tp->t_tfo_client_cookie_len == 0)) ||
12436 (flags & TH_RST))) {
12437 len = 0;
12438 sack_rxmit = 0;
12439 rsm = NULL;
12440 }
12441 /* Without fast-open there should never be data sent on a SYN */
12442 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags)))
12443 len = 0;
12444 if (len <= 0) {
12445 /*
12446 * If FIN has been sent but not acked, but we haven't been
12447 * called to retransmit, len will be < 0. Otherwise, window
12448 * shrank after we sent into it. If window shrank to 0,
12449 * cancel pending retransmit, pull snd_nxt back to (closed)
12450 * window, and set the persist timer if it isn't already
12451 * going. If the window didn't close completely, just wait
12452 * for an ACK.
12453 *
12454 * We also do a general check here to ensure that we will
12455 * set the persist timer when we have data to send, but a
12456 * 0-byte window. This makes sure the persist timer is set
12457 * even if the packet hits one of the "goto send" lines
12458 * below.
12459 */
12460 len = 0;
12461 if ((tp->snd_wnd == 0) &&
12462 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12463 (tp->snd_una == tp->snd_max) &&
12464 (sb_offset < (int)sbavail(sb))) {
12465 /*
12466 * Not enough room in the rwnd to send
12467 * a paced segment out.
12468 */
12469 bbr_enter_persist(tp, bbr, cts, __LINE__);
12470 }
12471 } else if ((rsm == NULL) &&
12472 (doing_tlp == 0) &&
12473 (len < bbr->r_ctl.rc_pace_max_segs)) {
12474 /*
12475 * We are not sending a full segment for
12476 * some reason. Should we not send anything (think
12477 * sws or persists)?
12478 */
12479 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12480 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12481 (len < (int)(sbavail(sb) - sb_offset))) {
12482 /*
12483 * Here the rwnd is less than
12484 * the pacing size, this is not a retransmit,
12485 * we are established and
12486 * the send is not the last in the socket buffer
12487 * lets not send, and possibly enter persists.
12488 */
12489 len = 0;
12490 if (tp->snd_max == tp->snd_una)
12491 bbr_enter_persist(tp, bbr, cts, __LINE__);
12492 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) &&
12493 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12494 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12495 (len < (int)(sbavail(sb) - sb_offset)) &&
12496 (len < bbr_minseg(bbr))) {
12497 /*
12498 * Here we are not retransmitting, and
12499 * the cwnd is not so small that we could
12500 * not send at least a min size (rxt timer
12501 * not having gone off), We have 2 segments or
12502 * more already in flight, its not the tail end
12503 * of the socket buffer and the cwnd is blocking
12504 * us from sending out minimum pacing segment size.
12505 * Lets not send anything.
12506 */
12507 bbr->rc_cwnd_limited = 1;
12508 len = 0;
12509 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12510 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12511 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12512 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12513 (len < (int)(sbavail(sb) - sb_offset)) &&
12514 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12515 /*
12516 * Here we have a send window but we have
12517 * filled it up and we can't send another pacing segment.
12518 * We also have in flight more than 2 segments
12519 * and we are not completing the sb i.e. we allow
12520 * the last bytes of the sb to go out even if
12521 * its not a full pacing segment.
12522 */
12523 len = 0;
12524 }
12525 }
12526 /* len will be >= 0 after this point. */
12527 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12528 tcp_sndbuf_autoscale(tp, so, sendwin);
12529 /*
12530 *
12531 */
12532 if (bbr->rc_in_persist &&
12533 len &&
12534 (rsm == NULL) &&
12535 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) {
12536 /*
12537 * We are in persist, not doing a retransmit and don't have enough space
12538 * yet to send a full TSO. So is it at the end of the sb
12539 * if so we need to send else nuke to 0 and don't send.
12540 */
12541 int sbleft;
12542 if (sbavail(sb) > sb_offset)
12543 sbleft = sbavail(sb) - sb_offset;
12544 else
12545 sbleft = 0;
12546 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) {
12547 /* not at end of sb lets not send */
12548 len = 0;
12549 }
12550 }
12551 /*
12552 * Decide if we can use TCP Segmentation Offloading (if supported by
12553 * hardware).
12554 *
12555 * TSO may only be used if we are in a pure bulk sending state. The
12556 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12557 * options prevent using TSO. With TSO the TCP header is the same
12558 * (except for the sequence number) for all generated packets. This
12559 * makes it impossible to transmit any options which vary per
12560 * generated segment or packet.
12561 *
12562 * IPv4 handling has a clear separation of ip options and ip header
12563 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen()
12564 * does the right thing below to provide length of just ip options
12565 * and thus checking for ipoptlen is enough to decide if ip options
12566 * are present.
12567 */
12568 #ifdef INET6
12569 if (isipv6)
12570 ipoptlen = ip6_optlen(inp);
12571 else
12572 #endif
12573 if (inp->inp_options)
12574 ipoptlen = inp->inp_options->m_len -
12575 offsetof(struct ipoption, ipopt_list);
12576 else
12577 ipoptlen = 0;
12578 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12579 /*
12580 * Pre-calculate here as we save another lookup into the darknesses
12581 * of IPsec that way and can actually decide if TSO is ok.
12582 */
12583 #ifdef INET6
12584 if (isipv6 && IPSEC_ENABLED(ipv6))
12585 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
12586 #ifdef INET
12587 else
12588 #endif
12589 #endif /* INET6 */
12590 #ifdef INET
12591 if (IPSEC_ENABLED(ipv4))
12592 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
12593 #endif /* INET */
12594 #endif /* IPSEC */
12595 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12596 ipoptlen += ipsec_optlen;
12597 #endif
12598 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
12599 (len > maxseg) &&
12600 (tp->t_port == 0) &&
12601 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12602 tp->rcv_numsacks == 0 &&
12603 ipoptlen == 0)
12604 tso = 1;
12605
12606 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12607 (long)TCP_MAXWIN << tp->rcv_scale);
12608 /*
12609 * Sender silly window avoidance. We transmit under the following
12610 * conditions when len is non-zero:
12611 *
12612 * - We have a full segment (or more with TSO) - This is the last
12613 * buffer in a write()/send() and we are either idle or running
12614 * NODELAY - we've timed out (e.g. persist timer) - we have more
12615 * then 1/2 the maximum send window's worth of data (receiver may be
12616 * limited the window size) - we need to retransmit
12617 */
12618 if (rsm)
12619 goto send;
12620 if (len) {
12621 if (sack_rxmit)
12622 goto send;
12623 if (len >= p_maxseg)
12624 goto send;
12625 /*
12626 * NOTE! on localhost connections an 'ack' from the remote
12627 * end may occur synchronously with the output and cause us
12628 * to flush a buffer queued with moretocome. XXX
12629 *
12630 */
12631 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */
12632 ((tp->t_flags & TF_NODELAY) ||
12633 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) &&
12634 (tp->t_flags & TF_NOPUSH) == 0) {
12635 goto send;
12636 }
12637 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12638 goto send;
12639 }
12640 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12641 goto send;
12642 }
12643 }
12644 /*
12645 * Sending of standalone window updates.
12646 *
12647 * Window updates are important when we close our window due to a
12648 * full socket buffer and are opening it again after the application
12649 * reads data from it. Once the window has opened again and the
12650 * remote end starts to send again the ACK clock takes over and
12651 * provides the most current window information.
12652 *
12653 * We must avoid the silly window syndrome whereas every read from
12654 * the receive buffer, no matter how small, causes a window update
12655 * to be sent. We also should avoid sending a flurry of window
12656 * updates when the socket buffer had queued a lot of data and the
12657 * application is doing small reads.
12658 *
12659 * Prevent a flurry of pointless window updates by only sending an
12660 * update when we can increase the advertized window by more than
12661 * 1/4th of the socket buffer capacity. When the buffer is getting
12662 * full or is very small be more aggressive and send an update
12663 * whenever we can increase by two mss sized segments. In all other
12664 * situations the ACK's to new incoming data will carry further
12665 * window increases.
12666 *
12667 * Don't send an independent window update if a delayed ACK is
12668 * pending (it will get piggy-backed on it) or the remote side
12669 * already has done a half-close and won't send more data. Skip
12670 * this if the connection is in T/TCP half-open state.
12671 */
12672 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
12673 !(tp->t_flags & TF_DELACK) &&
12674 !TCPS_HAVERCVDFIN(tp->t_state)) {
12675 /* Check to see if we should do a window update */
12676 if (bbr_window_update_needed(tp, so, recwin, maxseg))
12677 goto send;
12678 }
12679 /*
12680 * Send if we owe the peer an ACK, RST, SYN. ACKNOW
12681 * is also a catch-all for the retransmit timer timeout case.
12682 */
12683 if (tp->t_flags & TF_ACKNOW) {
12684 goto send;
12685 }
12686 if (flags & TH_RST) {
12687 /* Always send a RST if one is due */
12688 goto send;
12689 }
12690 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) {
12691 goto send;
12692 }
12693 /*
12694 * If our state indicates that FIN should be sent and we have not
12695 * yet done so, then we need to send.
12696 */
12697 if (flags & TH_FIN &&
12698 ((tp->t_flags & TF_SENTFIN) == 0)) {
12699 goto send;
12700 }
12701 /*
12702 * No reason to send a segment, just return.
12703 */
12704 just_return:
12705 SOCKBUF_UNLOCK(sb);
12706 just_return_nolock:
12707 if (tot_len)
12708 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
12709 if (bbr->rc_no_pacing)
12710 slot = 0;
12711 if (tot_len == 0) {
12712 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >=
12713 tp->snd_wnd) {
12714 BBR_STAT_INC(bbr_rwnd_limited);
12715 app_limited = BBR_JR_RWND_LIMITED;
12716 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12717 if ((bbr->rc_in_persist == 0) &&
12718 TCPS_HAVEESTABLISHED(tp->t_state) &&
12719 (tp->snd_max == tp->snd_una) &&
12720 sbavail(&so->so_snd)) {
12721 /* No send window.. we must enter persist */
12722 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
12723 }
12724 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
12725 BBR_STAT_INC(bbr_app_limited);
12726 app_limited = BBR_JR_APP_LIMITED;
12727 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12728 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12729 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) {
12730 BBR_STAT_INC(bbr_cwnd_limited);
12731 app_limited = BBR_JR_CWND_LIMITED;
12732 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12733 bbr->r_ctl.rc_lost_bytes)));
12734 bbr->rc_cwnd_limited = 1;
12735 } else {
12736 BBR_STAT_INC(bbr_app_limited);
12737 app_limited = BBR_JR_APP_LIMITED;
12738 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12739 }
12740 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12741 bbr->r_agg_early_set = 0;
12742 bbr->r_ctl.rc_agg_early = 0;
12743 bbr->r_ctl.rc_last_delay_val = 0;
12744 } else if (bbr->rc_use_google == 0)
12745 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12746 /* Are we app limited? */
12747 if ((app_limited == BBR_JR_APP_LIMITED) ||
12748 (app_limited == BBR_JR_RWND_LIMITED)) {
12749 /**
12750 * We are application limited.
12751 */
12752 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12753 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered);
12754 }
12755 if (tot_len == 0)
12756 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1);
12757 /* Dont update the time if we did not send */
12758 bbr->r_ctl.rc_last_delay_val = 0;
12759 bbr->rc_output_starts_timer = 1;
12760 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len);
12761 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len);
12762 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12763 /* Make sure snd_nxt is drug up */
12764 tp->snd_nxt = tp->snd_max;
12765 }
12766 return (error);
12767
12768 send:
12769 if (doing_tlp == 0) {
12770 /*
12771 * Data not a TLP, and its not the rxt firing. If it is the
12772 * rxt firing, we want to leave the tlp_in_progress flag on
12773 * so we don't send another TLP. It has to be a rack timer
12774 * or normal send (response to acked data) to clear the tlp
12775 * in progress flag.
12776 */
12777 bbr->rc_tlp_in_progress = 0;
12778 bbr->rc_tlp_rtx_out = 0;
12779 } else {
12780 /*
12781 * Its a TLP.
12782 */
12783 bbr->rc_tlp_in_progress = 1;
12784 }
12785 bbr_timer_cancel(bbr, __LINE__, cts);
12786 if (rsm == NULL) {
12787 if (sbused(sb) > 0) {
12788 /*
12789 * This is sub-optimal. We only send a stand alone
12790 * FIN on its own segment.
12791 */
12792 if (flags & TH_FIN) {
12793 flags &= ~TH_FIN;
12794 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) {
12795 /* Lets not send this */
12796 slot = 0;
12797 goto just_return;
12798 }
12799 }
12800 }
12801 } else {
12802 /*
12803 * We do *not* send a FIN on a retransmit if it has data.
12804 * The if clause here where len > 1 should never come true.
12805 */
12806 if ((len > 0) &&
12807 (((rsm->r_flags & BBR_HAS_FIN) == 0) &&
12808 (flags & TH_FIN))) {
12809 flags &= ~TH_FIN;
12810 len--;
12811 }
12812 }
12813 SOCKBUF_LOCK_ASSERT(sb);
12814 if (len > 0) {
12815 if ((tp->snd_una == tp->snd_max) &&
12816 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
12817 /*
12818 * This qualifies as a RTT_PROBE session since we
12819 * drop the data outstanding to nothing and waited
12820 * more than bbr_rtt_probe_time.
12821 */
12822 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
12823 bbr_set_reduced_rtt(bbr, cts, __LINE__);
12824 }
12825 if (len >= maxseg)
12826 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
12827 else
12828 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
12829 }
12830 /*
12831 * Before ESTABLISHED, force sending of initial options unless TCP
12832 * set not to do any options. NOTE: we assume that the IP/TCP header
12833 * plus TCP options always fit in a single mbuf, leaving room for a
12834 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
12835 * + optlen <= MCLBYTES
12836 */
12837 optlen = 0;
12838 #ifdef INET6
12839 if (isipv6)
12840 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12841 else
12842 #endif
12843 hdrlen = sizeof(struct tcpiphdr);
12844
12845 /*
12846 * Compute options for segment. We only have to care about SYN and
12847 * established connection segments. Options for SYN-ACK segments
12848 * are handled in TCP syncache.
12849 */
12850 to.to_flags = 0;
12851 local_options = 0;
12852 if ((tp->t_flags & TF_NOOPT) == 0) {
12853 /* Maximum segment size. */
12854 if (flags & TH_SYN) {
12855 to.to_mss = tcp_mssopt(&inp->inp_inc);
12856 if (tp->t_port)
12857 to.to_mss -= V_tcp_udp_tunneling_overhead;
12858 to.to_flags |= TOF_MSS;
12859 /*
12860 * On SYN or SYN|ACK transmits on TFO connections,
12861 * only include the TFO option if it is not a
12862 * retransmit, as the presence of the TFO option may
12863 * have caused the original SYN or SYN|ACK to have
12864 * been dropped by a middlebox.
12865 */
12866 if (IS_FASTOPEN(tp->t_flags) &&
12867 (tp->t_rxtshift == 0)) {
12868 if (tp->t_state == TCPS_SYN_RECEIVED) {
12869 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
12870 to.to_tfo_cookie =
12871 (u_int8_t *)&tp->t_tfo_cookie.server;
12872 to.to_flags |= TOF_FASTOPEN;
12873 wanted_cookie = 1;
12874 } else if (tp->t_state == TCPS_SYN_SENT) {
12875 to.to_tfo_len =
12876 tp->t_tfo_client_cookie_len;
12877 to.to_tfo_cookie =
12878 tp->t_tfo_cookie.client;
12879 to.to_flags |= TOF_FASTOPEN;
12880 wanted_cookie = 1;
12881 }
12882 }
12883 }
12884 /* Window scaling. */
12885 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
12886 to.to_wscale = tp->request_r_scale;
12887 to.to_flags |= TOF_SCALE;
12888 }
12889 /* Timestamps. */
12890 if ((tp->t_flags & TF_RCVD_TSTMP) ||
12891 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
12892 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset;
12893 to.to_tsecr = tp->ts_recent;
12894 to.to_flags |= TOF_TS;
12895 local_options += TCPOLEN_TIMESTAMP + 2;
12896 }
12897 /* Set receive buffer autosizing timestamp. */
12898 if (tp->rfbuf_ts == 0 &&
12899 (so->so_rcv.sb_flags & SB_AUTOSIZE))
12900 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv);
12901 /* Selective ACK's. */
12902 if (flags & TH_SYN)
12903 to.to_flags |= TOF_SACKPERM;
12904 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
12905 tp->rcv_numsacks > 0) {
12906 to.to_flags |= TOF_SACK;
12907 to.to_nsacks = tp->rcv_numsacks;
12908 to.to_sacks = (u_char *)tp->sackblks;
12909 }
12910 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
12911 /* TCP-MD5 (RFC2385). */
12912 if (tp->t_flags & TF_SIGNATURE)
12913 to.to_flags |= TOF_SIGNATURE;
12914 #endif /* TCP_SIGNATURE */
12915
12916 /* Processing the options. */
12917 hdrlen += (optlen = tcp_addoptions(&to, opt));
12918 /*
12919 * If we wanted a TFO option to be added, but it was unable
12920 * to fit, ensure no data is sent.
12921 */
12922 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
12923 !(to.to_flags & TOF_FASTOPEN))
12924 len = 0;
12925 }
12926 if (tp->t_port) {
12927 if (V_tcp_udp_tunneling_port == 0) {
12928 /* The port was removed?? */
12929 SOCKBUF_UNLOCK(&so->so_snd);
12930 return (EHOSTUNREACH);
12931 }
12932 hdrlen += sizeof(struct udphdr);
12933 }
12934 #ifdef INET6
12935 if (isipv6)
12936 ipoptlen = ip6_optlen(inp);
12937 else
12938 #endif
12939 if (inp->inp_options)
12940 ipoptlen = inp->inp_options->m_len -
12941 offsetof(struct ipoption, ipopt_list);
12942 else
12943 ipoptlen = 0;
12944 ipoptlen = 0;
12945 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12946 ipoptlen += ipsec_optlen;
12947 #endif
12948 if (bbr->rc_last_options != local_options) {
12949 /*
12950 * Cache the options length this generally does not change
12951 * on a connection. We use this to calculate TSO.
12952 */
12953 bbr->rc_last_options = local_options;
12954 }
12955 maxseg = tp->t_maxseg - (ipoptlen + optlen);
12956 p_maxseg = min(maxseg, pace_max_segs);
12957 /*
12958 * Adjust data length if insertion of options will bump the packet
12959 * length beyond the t_maxseg length. Clear the FIN bit because we
12960 * cut off the tail of the segment.
12961 */
12962 if (len > maxseg) {
12963 if (len != 0 && (flags & TH_FIN)) {
12964 flags &= ~TH_FIN;
12965 }
12966 if (tso) {
12967 uint32_t moff;
12968 int32_t max_len;
12969
12970 /* extract TSO information */
12971 if_hw_tsomax = tp->t_tsomax;
12972 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
12973 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
12974 KASSERT(ipoptlen == 0,
12975 ("%s: TSO can't do IP options", __func__));
12976
12977 /*
12978 * Check if we should limit by maximum payload
12979 * length:
12980 */
12981 if (if_hw_tsomax != 0) {
12982 /* compute maximum TSO length */
12983 max_len = (if_hw_tsomax - hdrlen -
12984 max_linkhdr);
12985 if (max_len <= 0) {
12986 len = 0;
12987 } else if (len > max_len) {
12988 len = max_len;
12989 }
12990 }
12991 /*
12992 * Prevent the last segment from being fractional
12993 * unless the send sockbuf can be emptied:
12994 */
12995 if ((sb_offset + len) < sbavail(sb)) {
12996 moff = len % (uint32_t)maxseg;
12997 if (moff != 0) {
12998 len -= moff;
12999 }
13000 }
13001 /*
13002 * In case there are too many small fragments don't
13003 * use TSO:
13004 */
13005 if (len <= maxseg) {
13006 len = maxseg;
13007 tso = 0;
13008 }
13009 } else {
13010 /* Not doing TSO */
13011 if (optlen + ipoptlen >= tp->t_maxseg) {
13012 /*
13013 * Since we don't have enough space to put
13014 * the IP header chain and the TCP header in
13015 * one packet as required by RFC 7112, don't
13016 * send it. Also ensure that at least one
13017 * byte of the payload can be put into the
13018 * TCP segment.
13019 */
13020 SOCKBUF_UNLOCK(&so->so_snd);
13021 error = EMSGSIZE;
13022 sack_rxmit = 0;
13023 goto out;
13024 }
13025 len = maxseg;
13026 }
13027 } else {
13028 /* Not doing TSO */
13029 if_hw_tsomaxsegcount = 0;
13030 tso = 0;
13031 }
13032 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13033 ("%s: len > IP_MAXPACKET", __func__));
13034 #ifdef DIAGNOSTIC
13035 #ifdef INET6
13036 if (max_linkhdr + hdrlen > MCLBYTES)
13037 #else
13038 if (max_linkhdr + hdrlen > MHLEN)
13039 #endif
13040 panic("tcphdr too big");
13041 #endif
13042 /*
13043 * This KASSERT is here to catch edge cases at a well defined place.
13044 * Before, those had triggered (random) panic conditions further
13045 * down.
13046 */
13047 #ifdef BBR_INVARIANTS
13048 if (sack_rxmit) {
13049 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
13050 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u",
13051 rsm, tp, bbr, rsm->r_start, tp->snd_una);
13052 }
13053 }
13054 #endif
13055 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13056 if ((len == 0) &&
13057 (flags & TH_FIN) &&
13058 (sbused(sb))) {
13059 /*
13060 * We have outstanding data, don't send a fin by itself!.
13061 */
13062 slot = 0;
13063 goto just_return;
13064 }
13065 /*
13066 * Grab a header mbuf, attaching a copy of data to be transmitted,
13067 * and initialize the header from the template for sends on this
13068 * connection.
13069 */
13070 if (len) {
13071 uint32_t moff;
13072
13073 /*
13074 * We place a limit on sending with hptsi.
13075 */
13076 if ((rsm == NULL) && len > pace_max_segs)
13077 len = pace_max_segs;
13078 if (len <= maxseg)
13079 tso = 0;
13080 #ifdef INET6
13081 if (MHLEN < hdrlen + max_linkhdr)
13082 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13083 else
13084 #endif
13085 m = m_gethdr(M_NOWAIT, MT_DATA);
13086
13087 if (m == NULL) {
13088 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13089 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13090 SOCKBUF_UNLOCK(sb);
13091 error = ENOBUFS;
13092 sack_rxmit = 0;
13093 goto out;
13094 }
13095 m->m_data += max_linkhdr;
13096 m->m_len = hdrlen;
13097 /*
13098 * Start the m_copy functions from the closest mbuf to the
13099 * sb_offset in the socket buffer chain.
13100 */
13101 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) {
13102 #ifdef BBR_INVARIANTS
13103 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0)))
13104 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u",
13105 tp, bbr, len, sb_offset, sbavail(sb), rsm,
13106 doing_retran_from,
13107 picked_up_retran,
13108 doing_tlp);
13109
13110 #endif
13111 /*
13112 * In this messed up situation we have two choices,
13113 * a) pretend the send worked, and just start timers
13114 * and what not (not good since that may lead us
13115 * back here a lot). <or> b) Send the lowest segment
13116 * in the map. <or> c) Drop the connection. Lets do
13117 * <b> which if it continues to happen will lead to
13118 * <c> via timeouts.
13119 */
13120 BBR_STAT_INC(bbr_offset_recovery);
13121 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
13122 sb_offset = 0;
13123 if (rsm == NULL) {
13124 sack_rxmit = 0;
13125 len = sbavail(sb);
13126 } else {
13127 sack_rxmit = 1;
13128 if (rsm->r_start != tp->snd_una) {
13129 /*
13130 * Things are really messed up, <c>
13131 * is the only thing to do.
13132 */
13133 BBR_STAT_INC(bbr_offset_drop);
13134 SOCKBUF_UNLOCK(sb);
13135 (void)m_free(m);
13136 return (-EFAULT); /* tcp_drop() */
13137 }
13138 len = rsm->r_end - rsm->r_start;
13139 }
13140 if (len > sbavail(sb))
13141 len = sbavail(sb);
13142 if (len > maxseg)
13143 len = maxseg;
13144 }
13145 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13146 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13147 m_copydata(mb, moff, (int)len,
13148 mtod(m, caddr_t)+hdrlen);
13149 if (rsm == NULL)
13150 sbsndptr_adv(sb, mb, len);
13151 m->m_len += len;
13152 } else {
13153 struct sockbuf *msb;
13154
13155 if (rsm)
13156 msb = NULL;
13157 else
13158 msb = sb;
13159 #ifdef BBR_INVARIANTS
13160 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) {
13161 if (rsm) {
13162 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 ",
13163 tp, bbr, len, moff,
13164 sbavail(sb), rsm,
13165 tp->snd_una, rsm->r_flags, rsm->r_start,
13166 doing_retran_from,
13167 picked_up_retran,
13168 doing_tlp, sack_rxmit);
13169 } else {
13170 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u",
13171 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una);
13172 }
13173 }
13174 #endif
13175 m->m_next = tcp_m_copym(
13176 mb, moff, &len,
13177 if_hw_tsomaxsegcount,
13178 if_hw_tsomaxsegsize, msb,
13179 ((rsm == NULL) ? hw_tls : 0)
13180 #ifdef NETFLIX_COPY_ARGS
13181 , NULL, NULL
13182 #endif
13183 );
13184 if (len <= maxseg) {
13185 /*
13186 * Must have ran out of mbufs for the copy
13187 * shorten it to no longer need tso. Lets
13188 * not put on sendalot since we are low on
13189 * mbufs.
13190 */
13191 tso = 0;
13192 }
13193 if (m->m_next == NULL) {
13194 SOCKBUF_UNLOCK(sb);
13195 (void)m_free(m);
13196 error = ENOBUFS;
13197 sack_rxmit = 0;
13198 goto out;
13199 }
13200 }
13201 #ifdef BBR_INVARIANTS
13202 if (tso && len < maxseg) {
13203 panic("tp:%p tso on, but len:%d < maxseg:%d",
13204 tp, len, maxseg);
13205 }
13206 if (tso && if_hw_tsomaxsegcount) {
13207 int32_t seg_cnt = 0;
13208 struct mbuf *foo;
13209
13210 foo = m;
13211 while (foo) {
13212 seg_cnt++;
13213 foo = foo->m_next;
13214 }
13215 if (seg_cnt > if_hw_tsomaxsegcount) {
13216 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount);
13217 }
13218 }
13219 #endif
13220 /*
13221 * If we're sending everything we've got, set PUSH. (This
13222 * will keep happy those implementations which only give
13223 * data to the user when a buffer fills or a PUSH comes in.)
13224 */
13225 if (sb_offset + len == sbused(sb) &&
13226 sbused(sb) &&
13227 !(flags & TH_SYN)) {
13228 flags |= TH_PUSH;
13229 }
13230 SOCKBUF_UNLOCK(sb);
13231 } else {
13232 SOCKBUF_UNLOCK(sb);
13233 if (tp->t_flags & TF_ACKNOW)
13234 KMOD_TCPSTAT_INC(tcps_sndacks);
13235 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13236 KMOD_TCPSTAT_INC(tcps_sndctrl);
13237 else
13238 KMOD_TCPSTAT_INC(tcps_sndwinup);
13239
13240 m = m_gethdr(M_NOWAIT, MT_DATA);
13241 if (m == NULL) {
13242 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13243 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13244 error = ENOBUFS;
13245 /* Fudge the send time since we could not send */
13246 sack_rxmit = 0;
13247 goto out;
13248 }
13249 #ifdef INET6
13250 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13251 MHLEN >= hdrlen) {
13252 M_ALIGN(m, hdrlen);
13253 } else
13254 #endif
13255 m->m_data += max_linkhdr;
13256 m->m_len = hdrlen;
13257 }
13258 SOCKBUF_UNLOCK_ASSERT(sb);
13259 m->m_pkthdr.rcvif = (struct ifnet *)0;
13260 #ifdef MAC
13261 mac_inpcb_create_mbuf(inp, m);
13262 #endif
13263 #ifdef INET6
13264 if (isipv6) {
13265 ip6 = mtod(m, struct ip6_hdr *);
13266 if (tp->t_port) {
13267 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
13268 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13269 udp->uh_dport = tp->t_port;
13270 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13271 udp->uh_ulen = htons(ulen);
13272 th = (struct tcphdr *)(udp + 1);
13273 } else {
13274 th = (struct tcphdr *)(ip6 + 1);
13275 }
13276 tcpip_fillheaders(inp, tp->t_port, ip6, th);
13277 } else
13278 #endif /* INET6 */
13279 {
13280 ip = mtod(m, struct ip *);
13281 if (tp->t_port) {
13282 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
13283 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13284 udp->uh_dport = tp->t_port;
13285 ulen = hdrlen + len - sizeof(struct ip);
13286 udp->uh_ulen = htons(ulen);
13287 th = (struct tcphdr *)(udp + 1);
13288 } else {
13289 th = (struct tcphdr *)(ip + 1);
13290 }
13291 tcpip_fillheaders(inp, tp->t_port, ip, th);
13292 }
13293 /*
13294 * If we are doing retransmissions, then snd_nxt will not reflect
13295 * the first unsent octet. For ACK only packets, we do not want the
13296 * sequence number of the retransmitted packet, we want the sequence
13297 * number of the next unsent octet. So, if there is no data (and no
13298 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13299 * ti_seq. But if we are in persist state, snd_max might reflect
13300 * one byte beyond the right edge of the window, so use snd_nxt in
13301 * that case, since we know we aren't doing a retransmission.
13302 * (retransmit and persist are mutually exclusive...)
13303 */
13304 if (sack_rxmit == 0) {
13305 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) {
13306 /* New data (including new persists) */
13307 th->th_seq = htonl(tp->snd_max);
13308 bbr_seq = tp->snd_max;
13309 } else if (flags & TH_SYN) {
13310 /* Syn's always send from iss */
13311 th->th_seq = htonl(tp->iss);
13312 bbr_seq = tp->iss;
13313 } else if (flags & TH_FIN) {
13314 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) {
13315 /*
13316 * If we sent the fin already its 1 minus
13317 * snd_max
13318 */
13319 th->th_seq = (htonl(tp->snd_max - 1));
13320 bbr_seq = (tp->snd_max - 1);
13321 } else {
13322 /* First time FIN use snd_max */
13323 th->th_seq = htonl(tp->snd_max);
13324 bbr_seq = tp->snd_max;
13325 }
13326 } else {
13327 /*
13328 * len == 0 and not persist we use snd_max, sending
13329 * an ack unless we have sent the fin then its 1
13330 * minus.
13331 */
13332 /*
13333 * XXXRRS Question if we are in persists and we have
13334 * nothing outstanding to send and we have not sent
13335 * a FIN, we will send an ACK. In such a case it
13336 * might be better to send (tp->snd_una - 1) which
13337 * would force the peer to ack.
13338 */
13339 if (tp->t_flags & TF_SENTFIN) {
13340 th->th_seq = htonl(tp->snd_max - 1);
13341 bbr_seq = (tp->snd_max - 1);
13342 } else {
13343 th->th_seq = htonl(tp->snd_max);
13344 bbr_seq = tp->snd_max;
13345 }
13346 }
13347 } else {
13348 /* All retransmits use the rsm to guide the send */
13349 th->th_seq = htonl(rsm->r_start);
13350 bbr_seq = rsm->r_start;
13351 }
13352 th->th_ack = htonl(tp->rcv_nxt);
13353 if (optlen) {
13354 bcopy(opt, th + 1, optlen);
13355 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13356 }
13357 tcp_set_flags(th, flags);
13358 /*
13359 * Calculate receive window. Don't shrink window, but avoid silly
13360 * window syndrome.
13361 */
13362 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) &&
13363 recwin < maxseg)))
13364 recwin = 0;
13365 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13366 recwin < (tp->rcv_adv - tp->rcv_nxt))
13367 recwin = (tp->rcv_adv - tp->rcv_nxt);
13368 if (recwin > TCP_MAXWIN << tp->rcv_scale)
13369 recwin = TCP_MAXWIN << tp->rcv_scale;
13370
13371 /*
13372 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13373 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13374 * handled in syncache.
13375 */
13376 if (flags & TH_SYN)
13377 th->th_win = htons((u_short)
13378 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13379 else {
13380 /* Avoid shrinking window with window scaling. */
13381 recwin = roundup2(recwin, 1 << tp->rcv_scale);
13382 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13383 }
13384 /*
13385 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13386 * window. This may cause the remote transmitter to stall. This
13387 * flag tells soreceive() to disable delayed acknowledgements when
13388 * draining the buffer. This can occur if the receiver is
13389 * attempting to read more data than can be buffered prior to
13390 * transmitting on the connection.
13391 */
13392 if (th->th_win == 0) {
13393 tp->t_sndzerowin++;
13394 tp->t_flags |= TF_RXWIN0SENT;
13395 } else
13396 tp->t_flags &= ~TF_RXWIN0SENT;
13397 /*
13398 * We don't support urgent data, but drag along
13399 * the pointer in case of a stack switch.
13400 */
13401 tp->snd_up = tp->snd_una;
13402 /*
13403 * Put TCP length in extended header, and then checksum extended
13404 * header and data.
13405 */
13406 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13407
13408 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13409 if (to.to_flags & TOF_SIGNATURE) {
13410 /*
13411 * Calculate MD5 signature and put it into the place
13412 * determined before. NOTE: since TCP options buffer doesn't
13413 * point into mbuf's data, calculate offset and use it.
13414 */
13415 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13416 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13417 /*
13418 * Do not send segment if the calculation of MD5
13419 * digest has failed.
13420 */
13421 goto out;
13422 }
13423 }
13424 #endif
13425
13426 #ifdef INET6
13427 if (isipv6) {
13428 /*
13429 * ip6_plen is not need to be filled now, and will be filled
13430 * in ip6_output.
13431 */
13432 if (tp->t_port) {
13433 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13434 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13435 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13436 th->th_sum = htons(0);
13437 UDPSTAT_INC(udps_opackets);
13438 } else {
13439 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13440 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13441 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
13442 optlen + len, IPPROTO_TCP, 0);
13443 }
13444 }
13445 #endif
13446 #if defined(INET6) && defined(INET)
13447 else
13448 #endif
13449 #ifdef INET
13450 {
13451 if (tp->t_port) {
13452 m->m_pkthdr.csum_flags = CSUM_UDP;
13453 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13454 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13455 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13456 th->th_sum = htons(0);
13457 UDPSTAT_INC(udps_opackets);
13458 } else {
13459 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP;
13460 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13461 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13462 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13463 IPPROTO_TCP + len + optlen));
13464 }
13465 /* IP version must be set here for ipv4/ipv6 checking later */
13466 KASSERT(ip->ip_v == IPVERSION,
13467 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13468 }
13469 #endif
13470
13471 /*
13472 * Enable TSO and specify the size of the segments. The TCP pseudo
13473 * header checksum is always provided. XXX: Fixme: This is currently
13474 * not the case for IPv6.
13475 */
13476 if (tso) {
13477 KASSERT(len > maxseg,
13478 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg));
13479 m->m_pkthdr.csum_flags |= CSUM_TSO;
13480 csum_flags |= CSUM_TSO;
13481 m->m_pkthdr.tso_segsz = maxseg;
13482 }
13483 KASSERT(len + hdrlen == m_length(m, NULL),
13484 ("%s: mbuf chain different than expected: %d + %u != %u",
13485 __func__, len, hdrlen, m_length(m, NULL)));
13486
13487 #ifdef TCP_HHOOK
13488 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */
13489 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13490 #endif
13491
13492 /* Log to the black box */
13493 if (tcp_bblogging_on(tp)) {
13494 union tcp_log_stackspecific log;
13495
13496 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
13497 /* Record info on type of transmission */
13498 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay;
13499 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3);
13500 log.u_bbr.flex3 = maxseg;
13501 log.u_bbr.flex4 = delay_calc;
13502 log.u_bbr.flex5 = bbr->rc_past_init_win;
13503 log.u_bbr.flex5 <<= 1;
13504 log.u_bbr.flex5 |= bbr->rc_no_pacing;
13505 log.u_bbr.flex5 <<= 29;
13506 log.u_bbr.flex5 |= tp->t_maxseg;
13507 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs;
13508 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr);
13509 /* lets poke in the low and the high here for debugging */
13510 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
13511 if (rsm || sack_rxmit) {
13512 if (doing_tlp)
13513 log.u_bbr.flex8 = 2;
13514 else
13515 log.u_bbr.flex8 = 1;
13516 } else {
13517 log.u_bbr.flex8 = 0;
13518 }
13519 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13520 len, &log, false, NULL, NULL, 0, tv);
13521 } else {
13522 lgb = NULL;
13523 }
13524 /*
13525 * Fill in IP length and desired time to live and send to IP level.
13526 * There should be a better way to handle ttl and tos; we could keep
13527 * them in the template, but need a way to checksum without them.
13528 */
13529 /*
13530 * m->m_pkthdr.len should have been set before cksum calcuration,
13531 * because in6_cksum() need it.
13532 */
13533 #ifdef INET6
13534 if (isipv6) {
13535 /*
13536 * we separately set hoplimit for every segment, since the
13537 * user might want to change the value via setsockopt. Also,
13538 * desired default hop limit might be changed via Neighbor
13539 * Discovery.
13540 */
13541 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13542
13543 /*
13544 * Set the packet size here for the benefit of DTrace
13545 * probes. ip6_output() will set it properly; it's supposed
13546 * to include the option header lengths as well.
13547 */
13548 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13549
13550 if (V_path_mtu_discovery && maxseg > V_tcp_minmss)
13551 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13552 else
13553 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13554
13555 if (tp->t_state == TCPS_SYN_SENT)
13556 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13557
13558 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13559 /* TODO: IPv6 IP6TOS_ECT bit on */
13560 error = ip6_output(m, inp->in6p_outputopts,
13561 &inp->inp_route6,
13562 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13563 NULL, NULL, inp);
13564
13565 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
13566 mtu = inp->inp_route6.ro_nh->nh_mtu;
13567 }
13568 #endif /* INET6 */
13569 #if defined(INET) && defined(INET6)
13570 else
13571 #endif
13572 #ifdef INET
13573 {
13574 ip->ip_len = htons(m->m_pkthdr.len);
13575 #ifdef INET6
13576 if (isipv6)
13577 ip->ip_ttl = in6_selecthlim(inp, NULL);
13578 #endif /* INET6 */
13579 /*
13580 * If we do path MTU discovery, then we set DF on every
13581 * packet. This might not be the best thing to do according
13582 * to RFC3390 Section 2. However the tcp hostcache migitates
13583 * the problem so it affects only the first tcp connection
13584 * with a host.
13585 *
13586 * NB: Don't set DF on small MTU/MSS to have a safe
13587 * fallback.
13588 */
13589 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13590 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13591 if (tp->t_port == 0 || len < V_tcp_minmss) {
13592 ip->ip_off |= htons(IP_DF);
13593 }
13594 } else {
13595 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13596 }
13597
13598 if (tp->t_state == TCPS_SYN_SENT)
13599 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
13600
13601 TCP_PROBE5(send, NULL, tp, ip, tp, th);
13602
13603 error = ip_output(m, inp->inp_options, &inp->inp_route,
13604 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
13605 inp);
13606 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
13607 mtu = inp->inp_route.ro_nh->nh_mtu;
13608 }
13609 #endif /* INET */
13610 if (lgb) {
13611 lgb->tlb_errno = error;
13612 lgb = NULL;
13613 }
13614
13615 out:
13616 /*
13617 * In transmit state, time the transmission and arrange for the
13618 * retransmit. In persist state, just set snd_max.
13619 */
13620 if (error == 0) {
13621 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
13622 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13623 (tp->t_flags & TF_SACK_PERMIT) &&
13624 tp->rcv_numsacks > 0)
13625 tcp_clean_dsack_blocks(tp);
13626 /* We sent an ack clear the bbr_segs_rcvd count */
13627 bbr->output_error_seen = 0;
13628 bbr->oerror_cnt = 0;
13629 bbr->bbr_segs_rcvd = 0;
13630 if (len == 0)
13631 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1);
13632 /* Do accounting for new sends */
13633 if ((len > 0) && (rsm == NULL)) {
13634 int idx;
13635 if (tp->snd_una == tp->snd_max) {
13636 /*
13637 * Special case to match google, when
13638 * nothing is in flight the delivered
13639 * time does get updated to the current
13640 * time (see tcp_rate_bsd.c).
13641 */
13642 bbr->r_ctl.rc_del_time = cts;
13643 }
13644 if (len >= maxseg) {
13645 idx = (len / maxseg) + 3;
13646 if (idx >= TCP_MSS_ACCT_ATIMER)
13647 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1);
13648 else
13649 counter_u64_add(bbr_out_size[idx], 1);
13650 } else {
13651 /* smaller than a MSS */
13652 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options);
13653 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV)
13654 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1);
13655 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1);
13656 }
13657 }
13658 }
13659 abandon = 0;
13660 /*
13661 * We must do the send accounting before we log the output,
13662 * otherwise the state of the rsm could change and we account to the
13663 * wrong bucket.
13664 */
13665 if (len > 0) {
13666 bbr_do_send_accounting(tp, bbr, rsm, len, error);
13667 if (error == 0) {
13668 if (tp->snd_una == tp->snd_max)
13669 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
13670 }
13671 }
13672 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error,
13673 cts, mb, &abandon, rsm, 0, sb);
13674 if (abandon) {
13675 /*
13676 * If bbr_log_output destroys the TCB or sees a TH_RST being
13677 * sent we should hit this condition.
13678 */
13679 return (0);
13680 }
13681 if (bbr->rc_in_persist == 0) {
13682 /*
13683 * Advance snd_nxt over sequence space of this segment.
13684 */
13685 if (error)
13686 /* We don't log or do anything with errors */
13687 goto skip_upd;
13688
13689 if (tp->snd_una == tp->snd_max &&
13690 (len || (flags & (TH_SYN | TH_FIN)))) {
13691 /*
13692 * Update the time we just added data since none was
13693 * outstanding.
13694 */
13695 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13696 bbr->rc_tp->t_acktime = ticks;
13697 }
13698 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) {
13699 if (flags & TH_SYN) {
13700 /*
13701 * Smack the snd_max to iss + 1
13702 * if its a FO we will add len below.
13703 */
13704 tp->snd_max = tp->iss + 1;
13705 }
13706 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13707 tp->snd_max++;
13708 tp->t_flags |= TF_SENTFIN;
13709 }
13710 }
13711 if (sack_rxmit == 0)
13712 tp->snd_max += len;
13713 skip_upd:
13714 if ((error == 0) && len)
13715 tot_len += len;
13716 } else {
13717 /* Persists case */
13718 int32_t xlen = len;
13719
13720 if (error)
13721 goto nomore;
13722
13723 if (flags & TH_SYN)
13724 ++xlen;
13725 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13726 ++xlen;
13727 tp->t_flags |= TF_SENTFIN;
13728 }
13729 if (xlen && (tp->snd_una == tp->snd_max)) {
13730 /*
13731 * Update the time we just added data since none was
13732 * outstanding.
13733 */
13734 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13735 bbr->rc_tp->t_acktime = ticks;
13736 }
13737 if (sack_rxmit == 0)
13738 tp->snd_max += xlen;
13739 tot_len += (len + optlen + ipoptlen);
13740 }
13741 nomore:
13742 if (error) {
13743 /*
13744 * Failures do not advance the seq counter above. For the
13745 * case of ENOBUFS we will fall out and become ack-clocked.
13746 * capping the cwnd at the current flight.
13747 * Everything else will just have to retransmit with the timer
13748 * (no pacer).
13749 */
13750 SOCKBUF_UNLOCK_ASSERT(sb);
13751 BBR_STAT_INC(bbr_saw_oerr);
13752 /* Clear all delay/early tracks */
13753 bbr->r_ctl.rc_hptsi_agg_delay = 0;
13754 bbr->r_ctl.rc_agg_early = 0;
13755 bbr->r_agg_early_set = 0;
13756 bbr->output_error_seen = 1;
13757 if (bbr->oerror_cnt < 0xf)
13758 bbr->oerror_cnt++;
13759 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) {
13760 /* drop the session */
13761 return (-ENETDOWN);
13762 }
13763 switch (error) {
13764 case ENOBUFS:
13765 /*
13766 * Make this guy have to get ack's to send
13767 * more but lets make sure we don't
13768 * slam him below a T-O (1MSS).
13769 */
13770 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
13771 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13772 bbr->r_ctl.rc_lost_bytes)) - maxseg;
13773 if (tp->snd_cwnd < maxseg)
13774 tp->snd_cwnd = maxseg;
13775 }
13776 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt;
13777 BBR_STAT_INC(bbr_saw_enobuf);
13778 if (bbr->bbr_hdrw_pacing)
13779 counter_u64_add(bbr_hdwr_pacing_enobuf, 1);
13780 else
13781 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1);
13782 /*
13783 * Here even in the enobuf's case we want to do our
13784 * state update. The reason being we may have been
13785 * called by the input function. If so we have had
13786 * things change.
13787 */
13788 error = 0;
13789 goto enobufs;
13790 case EMSGSIZE:
13791 /*
13792 * For some reason the interface we used initially
13793 * to send segments changed to another or lowered
13794 * its MTU. If TSO was active we either got an
13795 * interface without TSO capabilits or TSO was
13796 * turned off. If we obtained mtu from ip_output()
13797 * then update it and try again.
13798 */
13799 /* Turn on tracing (or try to) */
13800 {
13801 int old_maxseg;
13802
13803 old_maxseg = tp->t_maxseg;
13804 BBR_STAT_INC(bbr_saw_emsgsiz);
13805 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts);
13806 if (mtu != 0)
13807 tcp_mss_update(tp, -1, mtu, NULL, NULL);
13808 if (old_maxseg <= tp->t_maxseg) {
13809 /* Huh it did not shrink? */
13810 tp->t_maxseg = old_maxseg - 40;
13811 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts);
13812 }
13813 /*
13814 * Nuke all other things that can interfere
13815 * with slot
13816 */
13817 if ((tot_len + len) && (len >= tp->t_maxseg)) {
13818 slot = bbr_get_pacing_delay(bbr,
13819 bbr->r_ctl.rc_bbr_hptsi_gain,
13820 (tot_len + len), cts, 0);
13821 if (slot < bbr_error_base_paceout)
13822 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
13823 } else
13824 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
13825 bbr->rc_output_starts_timer = 1;
13826 bbr_start_hpts_timer(bbr, tp, cts, 10, slot,
13827 tot_len);
13828 return (error);
13829 }
13830 case EPERM:
13831 case EACCES:
13832 tp->t_softerror = error;
13833 /* FALLTHROUGH */
13834 case EHOSTDOWN:
13835 case EHOSTUNREACH:
13836 case ENETDOWN:
13837 case ENETUNREACH:
13838 if (TCPS_HAVERCVDSYN(tp->t_state)) {
13839 tp->t_softerror = error;
13840 error = 0;
13841 }
13842 /* FALLTHROUGH */
13843 default:
13844 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt;
13845 bbr->rc_output_starts_timer = 1;
13846 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0);
13847 return (error);
13848 }
13849 #ifdef STATS
13850 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) &&
13851 len &&
13852 (rsm == NULL) &&
13853 (bbr->rc_in_persist == 0)) {
13854 tp->gput_seq = bbr_seq;
13855 tp->gput_ack = bbr_seq +
13856 min(sbavail(&so->so_snd) - sb_offset, sendwin);
13857 tp->gput_ts = cts;
13858 tp->t_flags |= TF_GPUTINPROG;
13859 #endif
13860 }
13861 KMOD_TCPSTAT_INC(tcps_sndtotal);
13862 if ((bbr->bbr_hdw_pace_ena) &&
13863 (bbr->bbr_attempt_hdwr_pace == 0) &&
13864 (bbr->rc_past_init_win) &&
13865 (bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
13866 (get_filter_value(&bbr->r_ctl.rc_delrate)) &&
13867 (inp->inp_route.ro_nh &&
13868 inp->inp_route.ro_nh->nh_ifp)) {
13869 /*
13870 * We are past the initial window and
13871 * have at least one measurement so we
13872 * could use hardware pacing if its available.
13873 * We have an interface and we have not attempted
13874 * to setup hardware pacing, lets try to now.
13875 */
13876 uint64_t rate_wanted;
13877 int err = 0;
13878
13879 rate_wanted = bbr_get_hardware_rate(bbr);
13880 bbr->bbr_attempt_hdwr_pace = 1;
13881 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp,
13882 inp->inp_route.ro_nh->nh_ifp,
13883 rate_wanted,
13884 (RS_PACING_GEQ|RS_PACING_SUB_OK),
13885 &err, NULL);
13886 if (bbr->r_ctl.crte) {
13887 bbr_type_log_hdwr_pacing(bbr,
13888 bbr->r_ctl.crte->ptbl->rs_ifp,
13889 rate_wanted,
13890 bbr->r_ctl.crte->rate,
13891 __LINE__, cts, err);
13892 BBR_STAT_INC(bbr_hdwr_rl_add_ok);
13893 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
13894 counter_u64_add(bbr_flows_whdwr_pacing, 1);
13895 bbr->bbr_hdrw_pacing = 1;
13896 /* Now what is our gain status? */
13897 if (bbr->r_ctl.crte->rate < rate_wanted) {
13898 /* We have a problem */
13899 bbr_setup_less_of_rate(bbr, cts,
13900 bbr->r_ctl.crte->rate, rate_wanted);
13901 } else {
13902 /* We are good */
13903 bbr->gain_is_limited = 0;
13904 bbr->skip_gain = 0;
13905 }
13906 tcp_bbr_tso_size_check(bbr, cts);
13907 } else {
13908 bbr_type_log_hdwr_pacing(bbr,
13909 inp->inp_route.ro_nh->nh_ifp,
13910 rate_wanted,
13911 0,
13912 __LINE__, cts, err);
13913 BBR_STAT_INC(bbr_hdwr_rl_add_fail);
13914 }
13915 }
13916 if (bbr->bbr_hdrw_pacing) {
13917 /*
13918 * Worry about cases where the route
13919 * changes or something happened that we
13920 * lost our hardware pacing possibly during
13921 * the last ip_output call.
13922 */
13923 if (inp->inp_snd_tag == NULL) {
13924 /* A change during ip output disabled hw pacing? */
13925 bbr->bbr_hdrw_pacing = 0;
13926 } else if ((inp->inp_route.ro_nh == NULL) ||
13927 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) {
13928 /*
13929 * We had an interface or route change,
13930 * detach from the current hdwr pacing
13931 * and setup to re-attempt next go
13932 * round.
13933 */
13934 bbr->bbr_hdrw_pacing = 0;
13935 bbr->bbr_attempt_hdwr_pace = 0;
13936 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
13937 tcp_bbr_tso_size_check(bbr, cts);
13938 }
13939 }
13940 /*
13941 * Data sent (as far as we can tell). If this advertises a larger
13942 * window than any other segment, then remember the size of the
13943 * advertised window. Any pending ACK has now been sent.
13944 */
13945 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
13946 tp->rcv_adv = tp->rcv_nxt + recwin;
13947
13948 tp->last_ack_sent = tp->rcv_nxt;
13949 if ((error == 0) &&
13950 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) &&
13951 (doing_tlp == 0) &&
13952 (tso == 0) &&
13953 (len > 0) &&
13954 ((flags & TH_RST) == 0) &&
13955 ((flags & TH_SYN) == 0) &&
13956 (IN_RECOVERY(tp->t_flags) == 0) &&
13957 (bbr->rc_in_persist == 0) &&
13958 (tot_len < bbr->r_ctl.rc_pace_max_segs)) {
13959 /*
13960 * For non-tso we need to goto again until we have sent out
13961 * enough data to match what we are hptsi out every hptsi
13962 * interval.
13963 */
13964 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
13965 /* Make sure snd_nxt is drug up */
13966 tp->snd_nxt = tp->snd_max;
13967 }
13968 if (rsm != NULL) {
13969 rsm = NULL;
13970 goto skip_again;
13971 }
13972 rsm = NULL;
13973 sack_rxmit = 0;
13974 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
13975 goto again;
13976 }
13977 skip_again:
13978 if ((error == 0) && (flags & TH_FIN))
13979 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
13980 if ((error == 0) && (flags & TH_RST))
13981 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13982 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) {
13983 /*
13984 * Calculate/Re-Calculate the hptsi slot in usecs based on
13985 * what we have sent so far
13986 */
13987 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
13988 if (bbr->rc_no_pacing)
13989 slot = 0;
13990 }
13991 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
13992 enobufs:
13993 if (bbr->rc_use_google == 0)
13994 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
13995 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13996 bbr->r_ctl.rc_lost_bytes)));
13997 bbr->rc_output_starts_timer = 1;
13998 if (bbr->bbr_use_rack_cheat &&
13999 (more_to_rxt ||
14000 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) {
14001 /* Rack cheats and shotguns out all rxt's 1ms apart */
14002 if (slot > 1000)
14003 slot = 1000;
14004 }
14005 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) {
14006 /*
14007 * We don't change the tso size until some number of sends
14008 * to give the hardware commands time to get down
14009 * to the interface.
14010 */
14011 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++;
14012 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) {
14013 bbr->hw_pacing_set = 1;
14014 tcp_bbr_tso_size_check(bbr, cts);
14015 }
14016 }
14017 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len);
14018 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14019 /* Make sure snd_nxt is drug up */
14020 tp->snd_nxt = tp->snd_max;
14021 }
14022 return (error);
14023
14024 }
14025
14026 /*
14027 * See bbr_output_wtime() for return values.
14028 */
14029 static int
bbr_output(struct tcpcb * tp)14030 bbr_output(struct tcpcb *tp)
14031 {
14032 int32_t ret;
14033 struct timeval tv;
14034
14035 NET_EPOCH_ASSERT();
14036
14037 INP_WLOCK_ASSERT(tptoinpcb(tp));
14038 (void)tcp_get_usecs(&tv);
14039 ret = bbr_output_wtime(tp, &tv);
14040 return (ret);
14041 }
14042
14043 static void
bbr_mtu_chg(struct tcpcb * tp)14044 bbr_mtu_chg(struct tcpcb *tp)
14045 {
14046 struct tcp_bbr *bbr;
14047 struct bbr_sendmap *rsm, *frsm = NULL;
14048 uint32_t maxseg;
14049
14050 /*
14051 * The MTU has changed. a) Clear the sack filter. b) Mark everything
14052 * over the current size as SACK_PASS so a retransmit will occur.
14053 */
14054
14055 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14056 maxseg = tp->t_maxseg - bbr->rc_last_options;
14057 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
14058 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
14059 /* Don't mess with ones acked (by sack?) */
14060 if (rsm->r_flags & BBR_ACKED)
14061 continue;
14062 if ((rsm->r_end - rsm->r_start) > maxseg) {
14063 /*
14064 * We mark sack-passed on all the previous large
14065 * sends we did. This will force them to retransmit.
14066 */
14067 rsm->r_flags |= BBR_SACK_PASSED;
14068 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) &&
14069 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) {
14070 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
14071 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
14072 rsm->r_flags |= BBR_MARKED_LOST;
14073 }
14074 if (frsm == NULL)
14075 frsm = rsm;
14076 }
14077 }
14078 if (frsm) {
14079 bbr->r_ctl.rc_resend = frsm;
14080 }
14081 }
14082
14083 static int
bbr_pru_options(struct tcpcb * tp,int flags)14084 bbr_pru_options(struct tcpcb *tp, int flags)
14085 {
14086 if (flags & PRUS_OOB)
14087 return (EOPNOTSUPP);
14088 return (0);
14089 }
14090
14091 static void
bbr_switch_failed(struct tcpcb * tp)14092 bbr_switch_failed(struct tcpcb *tp)
14093 {
14094 /*
14095 * If a switch fails we only need to
14096 * make sure mbuf_queuing is still in place.
14097 * We also need to make sure we are still in
14098 * ticks granularity (though we should probably
14099 * change bbr to go to USECs).
14100 *
14101 * For timers we need to see if we are still in the
14102 * pacer (if our flags are up) if so we are good, if
14103 * not we need to get back into the pacer.
14104 */
14105 struct timeval tv;
14106 uint32_t cts;
14107 uint32_t toval;
14108 struct tcp_bbr *bbr;
14109 struct hpts_diag diag;
14110
14111 tp->t_flags2 |= TF2_CANNOT_DO_ECN;
14112 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14113 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
14114 if (tp->t_in_hpts > IHPTS_NONE) {
14115 return;
14116 }
14117 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14118 cts = tcp_get_usecs(&tv);
14119 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14120 if (TSTMP_GT(bbr->rc_pacer_started, cts)) {
14121 toval = bbr->rc_pacer_started - cts;
14122 } else {
14123 /* one slot please */
14124 toval = HPTS_TICKS_PER_SLOT;
14125 }
14126 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14127 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
14128 toval = bbr->r_ctl.rc_timer_exp - cts;
14129 } else {
14130 /* one slot please */
14131 toval = HPTS_TICKS_PER_SLOT;
14132 }
14133 } else
14134 toval = HPTS_TICKS_PER_SLOT;
14135 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14136 __LINE__, &diag);
14137 bbr_log_hpts_diag(bbr, cts, &diag);
14138 }
14139
14140 struct tcp_function_block __tcp_bbr = {
14141 .tfb_tcp_block_name = __XSTRING(STACKNAME),
14142 .tfb_tcp_output = bbr_output,
14143 .tfb_do_queued_segments = ctf_do_queued_segments,
14144 .tfb_do_segment_nounlock = bbr_do_segment_nounlock,
14145 .tfb_tcp_do_segment = bbr_do_segment,
14146 .tfb_tcp_ctloutput = bbr_ctloutput,
14147 .tfb_tcp_fb_init = bbr_init,
14148 .tfb_tcp_fb_fini = bbr_fini,
14149 .tfb_tcp_timer_stop_all = bbr_stopall,
14150 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr,
14151 .tfb_tcp_handoff_ok = bbr_handoff_ok,
14152 .tfb_tcp_mtu_chg = bbr_mtu_chg,
14153 .tfb_pru_options = bbr_pru_options,
14154 .tfb_switch_failed = bbr_switch_failed,
14155 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK,
14156 };
14157
14158 /*
14159 * bbr_ctloutput() must drop the inpcb lock before performing copyin on
14160 * socket option arguments. When it re-acquires the lock after the copy, it
14161 * has to revalidate that the connection is still valid for the socket
14162 * option.
14163 */
14164 static int
bbr_set_sockopt(struct tcpcb * tp,struct sockopt * sopt)14165 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
14166 {
14167 struct epoch_tracker et;
14168 struct inpcb *inp = tptoinpcb(tp);
14169 struct tcp_bbr *bbr;
14170 int32_t error = 0, optval;
14171
14172 switch (sopt->sopt_level) {
14173 case IPPROTO_IPV6:
14174 case IPPROTO_IP:
14175 return (tcp_default_ctloutput(tp, sopt));
14176 }
14177
14178 switch (sopt->sopt_name) {
14179 case TCP_RACK_PACE_MAX_SEG:
14180 case TCP_RACK_MIN_TO:
14181 case TCP_RACK_REORD_THRESH:
14182 case TCP_RACK_REORD_FADE:
14183 case TCP_RACK_TLP_THRESH:
14184 case TCP_RACK_PKT_DELAY:
14185 case TCP_BBR_ALGORITHM:
14186 case TCP_BBR_TSLIMITS:
14187 case TCP_BBR_IWINTSO:
14188 case TCP_BBR_RECFORCE:
14189 case TCP_BBR_STARTUP_PG:
14190 case TCP_BBR_DRAIN_PG:
14191 case TCP_BBR_RWND_IS_APP:
14192 case TCP_BBR_PROBE_RTT_INT:
14193 case TCP_BBR_PROBE_RTT_GAIN:
14194 case TCP_BBR_PROBE_RTT_LEN:
14195 case TCP_BBR_STARTUP_LOSS_EXIT:
14196 case TCP_BBR_USEDEL_RATE:
14197 case TCP_BBR_MIN_RTO:
14198 case TCP_BBR_MAX_RTO:
14199 case TCP_BBR_PACE_PER_SEC:
14200 case TCP_DELACK:
14201 case TCP_BBR_PACE_DEL_TAR:
14202 case TCP_BBR_SEND_IWND_IN_TSO:
14203 case TCP_BBR_EXTRA_STATE:
14204 case TCP_BBR_UTTER_MAX_TSO:
14205 case TCP_BBR_MIN_TOPACEOUT:
14206 case TCP_BBR_FLOOR_MIN_TSO:
14207 case TCP_BBR_TSTMP_RAISES:
14208 case TCP_BBR_POLICER_DETECT:
14209 case TCP_BBR_USE_RACK_CHEAT:
14210 case TCP_DATA_AFTER_CLOSE:
14211 case TCP_BBR_HDWR_PACE:
14212 case TCP_BBR_PACE_SEG_MAX:
14213 case TCP_BBR_PACE_SEG_MIN:
14214 case TCP_BBR_PACE_CROSS:
14215 case TCP_BBR_PACE_OH:
14216 case TCP_BBR_TMR_PACE_OH:
14217 case TCP_BBR_RACK_RTT_USE:
14218 case TCP_BBR_RETRAN_WTSO:
14219 break;
14220 default:
14221 return (tcp_default_ctloutput(tp, sopt));
14222 break;
14223 }
14224 INP_WUNLOCK(inp);
14225 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14226 if (error)
14227 return (error);
14228 INP_WLOCK(inp);
14229 if (inp->inp_flags & INP_DROPPED) {
14230 INP_WUNLOCK(inp);
14231 return (ECONNRESET);
14232 }
14233 if (tp->t_fb != &__tcp_bbr) {
14234 INP_WUNLOCK(inp);
14235 return (ENOPROTOOPT);
14236 }
14237 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14238 switch (sopt->sopt_name) {
14239 case TCP_BBR_PACE_PER_SEC:
14240 BBR_OPTS_INC(tcp_bbr_pace_per_sec);
14241 bbr->r_ctl.bbr_hptsi_per_second = optval;
14242 break;
14243 case TCP_BBR_PACE_DEL_TAR:
14244 BBR_OPTS_INC(tcp_bbr_pace_del_tar);
14245 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval;
14246 break;
14247 case TCP_BBR_PACE_SEG_MAX:
14248 BBR_OPTS_INC(tcp_bbr_pace_seg_max);
14249 bbr->r_ctl.bbr_hptsi_segments_max = optval;
14250 break;
14251 case TCP_BBR_PACE_SEG_MIN:
14252 BBR_OPTS_INC(tcp_bbr_pace_seg_min);
14253 bbr->r_ctl.bbr_hptsi_bytes_min = optval;
14254 break;
14255 case TCP_BBR_PACE_CROSS:
14256 BBR_OPTS_INC(tcp_bbr_pace_cross);
14257 bbr->r_ctl.bbr_cross_over = optval;
14258 break;
14259 case TCP_BBR_ALGORITHM:
14260 BBR_OPTS_INC(tcp_bbr_algorithm);
14261 if (optval && (bbr->rc_use_google == 0)) {
14262 /* Turn on the google mode */
14263 bbr_google_mode_on(bbr);
14264 if ((optval > 3) && (optval < 500)) {
14265 /*
14266 * Must be at least greater than .3%
14267 * and must be less than 50.0%.
14268 */
14269 bbr->r_ctl.bbr_google_discount = optval;
14270 }
14271 } else if ((optval == 0) && (bbr->rc_use_google == 1)) {
14272 /* Turn off the google mode */
14273 bbr_google_mode_off(bbr);
14274 }
14275 break;
14276 case TCP_BBR_TSLIMITS:
14277 BBR_OPTS_INC(tcp_bbr_tslimits);
14278 if (optval == 1)
14279 bbr->rc_use_ts_limit = 1;
14280 else if (optval == 0)
14281 bbr->rc_use_ts_limit = 0;
14282 else
14283 error = EINVAL;
14284 break;
14285
14286 case TCP_BBR_IWINTSO:
14287 BBR_OPTS_INC(tcp_bbr_iwintso);
14288 if ((optval >= 0) && (optval < 128)) {
14289 uint32_t twin;
14290
14291 bbr->rc_init_win = optval;
14292 twin = bbr_initial_cwnd(bbr, tp);
14293 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd))
14294 tp->snd_cwnd = twin;
14295 else
14296 error = EBUSY;
14297 } else
14298 error = EINVAL;
14299 break;
14300 case TCP_BBR_STARTUP_PG:
14301 BBR_OPTS_INC(tcp_bbr_startup_pg);
14302 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) {
14303 bbr->r_ctl.rc_startup_pg = optval;
14304 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
14305 bbr->r_ctl.rc_bbr_hptsi_gain = optval;
14306 }
14307 } else
14308 error = EINVAL;
14309 break;
14310 case TCP_BBR_DRAIN_PG:
14311 BBR_OPTS_INC(tcp_bbr_drain_pg);
14312 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE))
14313 bbr->r_ctl.rc_drain_pg = optval;
14314 else
14315 error = EINVAL;
14316 break;
14317 case TCP_BBR_PROBE_RTT_LEN:
14318 BBR_OPTS_INC(tcp_bbr_probertt_len);
14319 if (optval <= 1)
14320 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND));
14321 else
14322 error = EINVAL;
14323 break;
14324 case TCP_BBR_PROBE_RTT_GAIN:
14325 BBR_OPTS_INC(tcp_bbr_probertt_gain);
14326 if (optval <= BBR_UNIT)
14327 bbr->r_ctl.bbr_rttprobe_gain_val = optval;
14328 else
14329 error = EINVAL;
14330 break;
14331 case TCP_BBR_PROBE_RTT_INT:
14332 BBR_OPTS_INC(tcp_bbr_probe_rtt_int);
14333 if (optval > 1000)
14334 bbr->r_ctl.rc_probertt_int = optval;
14335 else
14336 error = EINVAL;
14337 break;
14338 case TCP_BBR_MIN_TOPACEOUT:
14339 BBR_OPTS_INC(tcp_bbr_topaceout);
14340 if (optval == 0) {
14341 bbr->no_pacing_until = 0;
14342 bbr->rc_no_pacing = 0;
14343 } else if (optval <= 0x00ff) {
14344 bbr->no_pacing_until = optval;
14345 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) &&
14346 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){
14347 /* Turn on no pacing */
14348 bbr->rc_no_pacing = 1;
14349 }
14350 } else
14351 error = EINVAL;
14352 break;
14353 case TCP_BBR_STARTUP_LOSS_EXIT:
14354 BBR_OPTS_INC(tcp_bbr_startup_loss_exit);
14355 bbr->rc_loss_exit = optval;
14356 break;
14357 case TCP_BBR_USEDEL_RATE:
14358 error = EINVAL;
14359 break;
14360 case TCP_BBR_MIN_RTO:
14361 BBR_OPTS_INC(tcp_bbr_min_rto);
14362 bbr->r_ctl.rc_min_rto_ms = optval;
14363 break;
14364 case TCP_BBR_MAX_RTO:
14365 BBR_OPTS_INC(tcp_bbr_max_rto);
14366 bbr->rc_max_rto_sec = optval;
14367 break;
14368 case TCP_RACK_MIN_TO:
14369 /* Minimum time between rack t-o's in ms */
14370 BBR_OPTS_INC(tcp_rack_min_to);
14371 bbr->r_ctl.rc_min_to = optval;
14372 break;
14373 case TCP_RACK_REORD_THRESH:
14374 /* RACK reorder threshold (shift amount) */
14375 BBR_OPTS_INC(tcp_rack_reord_thresh);
14376 if ((optval > 0) && (optval < 31))
14377 bbr->r_ctl.rc_reorder_shift = optval;
14378 else
14379 error = EINVAL;
14380 break;
14381 case TCP_RACK_REORD_FADE:
14382 /* Does reordering fade after ms time */
14383 BBR_OPTS_INC(tcp_rack_reord_fade);
14384 bbr->r_ctl.rc_reorder_fade = optval;
14385 break;
14386 case TCP_RACK_TLP_THRESH:
14387 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14388 BBR_OPTS_INC(tcp_rack_tlp_thresh);
14389 if (optval)
14390 bbr->rc_tlp_threshold = optval;
14391 else
14392 error = EINVAL;
14393 break;
14394 case TCP_BBR_USE_RACK_CHEAT:
14395 BBR_OPTS_INC(tcp_use_rackcheat);
14396 if (bbr->rc_use_google) {
14397 error = EINVAL;
14398 break;
14399 }
14400 BBR_OPTS_INC(tcp_rack_cheat);
14401 if (optval)
14402 bbr->bbr_use_rack_cheat = 1;
14403 else
14404 bbr->bbr_use_rack_cheat = 0;
14405 break;
14406 case TCP_BBR_FLOOR_MIN_TSO:
14407 BBR_OPTS_INC(tcp_utter_max_tso);
14408 if ((optval >= 0) && (optval < 40))
14409 bbr->r_ctl.bbr_hptsi_segments_floor = optval;
14410 else
14411 error = EINVAL;
14412 break;
14413 case TCP_BBR_UTTER_MAX_TSO:
14414 BBR_OPTS_INC(tcp_utter_max_tso);
14415 if ((optval >= 0) && (optval < 0xffff))
14416 bbr->r_ctl.bbr_utter_max = optval;
14417 else
14418 error = EINVAL;
14419 break;
14420
14421 case TCP_BBR_EXTRA_STATE:
14422 BBR_OPTS_INC(tcp_extra_state);
14423 if (optval)
14424 bbr->rc_use_idle_restart = 1;
14425 else
14426 bbr->rc_use_idle_restart = 0;
14427 break;
14428 case TCP_BBR_SEND_IWND_IN_TSO:
14429 BBR_OPTS_INC(tcp_iwnd_tso);
14430 if (optval) {
14431 bbr->bbr_init_win_cheat = 1;
14432 if (bbr->rc_past_init_win == 0) {
14433 uint32_t cts;
14434 cts = tcp_get_usecs(&bbr->rc_tv);
14435 tcp_bbr_tso_size_check(bbr, cts);
14436 }
14437 } else
14438 bbr->bbr_init_win_cheat = 0;
14439 break;
14440 case TCP_BBR_HDWR_PACE:
14441 BBR_OPTS_INC(tcp_hdwr_pacing);
14442 if (optval){
14443 bbr->bbr_hdw_pace_ena = 1;
14444 bbr->bbr_attempt_hdwr_pace = 0;
14445 } else {
14446 bbr->bbr_hdw_pace_ena = 0;
14447 #ifdef RATELIMIT
14448 if (bbr->r_ctl.crte != NULL) {
14449 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp);
14450 bbr->r_ctl.crte = NULL;
14451 }
14452 #endif
14453 }
14454 break;
14455
14456 case TCP_DELACK:
14457 BBR_OPTS_INC(tcp_delack);
14458 if (optval < 100) {
14459 if (optval == 0) /* off */
14460 tp->t_delayed_ack = 0;
14461 else if (optval == 1) /* on which is 2 */
14462 tp->t_delayed_ack = 2;
14463 else /* higher than 2 and less than 100 */
14464 tp->t_delayed_ack = optval;
14465 if (tp->t_flags & TF_DELACK) {
14466 tp->t_flags &= ~TF_DELACK;
14467 tp->t_flags |= TF_ACKNOW;
14468 NET_EPOCH_ENTER(et);
14469 bbr_output(tp);
14470 NET_EPOCH_EXIT(et);
14471 }
14472 } else
14473 error = EINVAL;
14474 break;
14475 case TCP_RACK_PKT_DELAY:
14476 /* RACK added ms i.e. rack-rtt + reord + N */
14477 BBR_OPTS_INC(tcp_rack_pkt_delay);
14478 bbr->r_ctl.rc_pkt_delay = optval;
14479 break;
14480
14481 case TCP_BBR_RETRAN_WTSO:
14482 BBR_OPTS_INC(tcp_retran_wtso);
14483 if (optval)
14484 bbr->rc_resends_use_tso = 1;
14485 else
14486 bbr->rc_resends_use_tso = 0;
14487 break;
14488 case TCP_DATA_AFTER_CLOSE:
14489 BBR_OPTS_INC(tcp_data_ac);
14490 if (optval)
14491 bbr->rc_allow_data_af_clo = 1;
14492 else
14493 bbr->rc_allow_data_af_clo = 0;
14494 break;
14495 case TCP_BBR_POLICER_DETECT:
14496 BBR_OPTS_INC(tcp_policer_det);
14497 if (bbr->rc_use_google == 0)
14498 error = EINVAL;
14499 else if (optval)
14500 bbr->r_use_policer = 1;
14501 else
14502 bbr->r_use_policer = 0;
14503 break;
14504
14505 case TCP_BBR_TSTMP_RAISES:
14506 BBR_OPTS_INC(tcp_ts_raises);
14507 if (optval)
14508 bbr->ts_can_raise = 1;
14509 else
14510 bbr->ts_can_raise = 0;
14511 break;
14512 case TCP_BBR_TMR_PACE_OH:
14513 BBR_OPTS_INC(tcp_pacing_oh_tmr);
14514 if (bbr->rc_use_google) {
14515 error = EINVAL;
14516 } else {
14517 if (optval)
14518 bbr->r_ctl.rc_incr_tmrs = 1;
14519 else
14520 bbr->r_ctl.rc_incr_tmrs = 0;
14521 }
14522 break;
14523 case TCP_BBR_PACE_OH:
14524 BBR_OPTS_INC(tcp_pacing_oh);
14525 if (bbr->rc_use_google) {
14526 error = EINVAL;
14527 } else {
14528 if (optval > (BBR_INCL_TCP_OH|
14529 BBR_INCL_IP_OH|
14530 BBR_INCL_ENET_OH)) {
14531 error = EINVAL;
14532 break;
14533 }
14534 if (optval & BBR_INCL_TCP_OH)
14535 bbr->r_ctl.rc_inc_tcp_oh = 1;
14536 else
14537 bbr->r_ctl.rc_inc_tcp_oh = 0;
14538 if (optval & BBR_INCL_IP_OH)
14539 bbr->r_ctl.rc_inc_ip_oh = 1;
14540 else
14541 bbr->r_ctl.rc_inc_ip_oh = 0;
14542 if (optval & BBR_INCL_ENET_OH)
14543 bbr->r_ctl.rc_inc_enet_oh = 1;
14544 else
14545 bbr->r_ctl.rc_inc_enet_oh = 0;
14546 }
14547 break;
14548 default:
14549 return (tcp_default_ctloutput(tp, sopt));
14550 break;
14551 }
14552 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14553 INP_WUNLOCK(inp);
14554 return (error);
14555 }
14556
14557 /*
14558 * return 0 on success, error-num on failure
14559 */
14560 static int
bbr_get_sockopt(struct tcpcb * tp,struct sockopt * sopt)14561 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
14562 {
14563 struct inpcb *inp = tptoinpcb(tp);
14564 struct tcp_bbr *bbr;
14565 int32_t error, optval;
14566
14567 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14568 if (bbr == NULL) {
14569 INP_WUNLOCK(inp);
14570 return (EINVAL);
14571 }
14572 /*
14573 * Because all our options are either boolean or an int, we can just
14574 * pull everything into optval and then unlock and copy. If we ever
14575 * add a option that is not a int, then this will have quite an
14576 * impact to this routine.
14577 */
14578 switch (sopt->sopt_name) {
14579 case TCP_BBR_PACE_PER_SEC:
14580 optval = bbr->r_ctl.bbr_hptsi_per_second;
14581 break;
14582 case TCP_BBR_PACE_DEL_TAR:
14583 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar;
14584 break;
14585 case TCP_BBR_PACE_SEG_MAX:
14586 optval = bbr->r_ctl.bbr_hptsi_segments_max;
14587 break;
14588 case TCP_BBR_MIN_TOPACEOUT:
14589 optval = bbr->no_pacing_until;
14590 break;
14591 case TCP_BBR_PACE_SEG_MIN:
14592 optval = bbr->r_ctl.bbr_hptsi_bytes_min;
14593 break;
14594 case TCP_BBR_PACE_CROSS:
14595 optval = bbr->r_ctl.bbr_cross_over;
14596 break;
14597 case TCP_BBR_ALGORITHM:
14598 optval = bbr->rc_use_google;
14599 break;
14600 case TCP_BBR_TSLIMITS:
14601 optval = bbr->rc_use_ts_limit;
14602 break;
14603 case TCP_BBR_IWINTSO:
14604 optval = bbr->rc_init_win;
14605 break;
14606 case TCP_BBR_STARTUP_PG:
14607 optval = bbr->r_ctl.rc_startup_pg;
14608 break;
14609 case TCP_BBR_DRAIN_PG:
14610 optval = bbr->r_ctl.rc_drain_pg;
14611 break;
14612 case TCP_BBR_PROBE_RTT_INT:
14613 optval = bbr->r_ctl.rc_probertt_int;
14614 break;
14615 case TCP_BBR_PROBE_RTT_LEN:
14616 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND);
14617 break;
14618 case TCP_BBR_PROBE_RTT_GAIN:
14619 optval = bbr->r_ctl.bbr_rttprobe_gain_val;
14620 break;
14621 case TCP_BBR_STARTUP_LOSS_EXIT:
14622 optval = bbr->rc_loss_exit;
14623 break;
14624 case TCP_BBR_USEDEL_RATE:
14625 error = EINVAL;
14626 break;
14627 case TCP_BBR_MIN_RTO:
14628 optval = bbr->r_ctl.rc_min_rto_ms;
14629 break;
14630 case TCP_BBR_MAX_RTO:
14631 optval = bbr->rc_max_rto_sec;
14632 break;
14633 case TCP_RACK_PACE_MAX_SEG:
14634 /* Max segments in a pace */
14635 optval = bbr->r_ctl.rc_pace_max_segs;
14636 break;
14637 case TCP_RACK_MIN_TO:
14638 /* Minimum time between rack t-o's in ms */
14639 optval = bbr->r_ctl.rc_min_to;
14640 break;
14641 case TCP_RACK_REORD_THRESH:
14642 /* RACK reorder threshold (shift amount) */
14643 optval = bbr->r_ctl.rc_reorder_shift;
14644 break;
14645 case TCP_RACK_REORD_FADE:
14646 /* Does reordering fade after ms time */
14647 optval = bbr->r_ctl.rc_reorder_fade;
14648 break;
14649 case TCP_BBR_USE_RACK_CHEAT:
14650 /* Do we use the rack cheat for rxt */
14651 optval = bbr->bbr_use_rack_cheat;
14652 break;
14653 case TCP_BBR_FLOOR_MIN_TSO:
14654 optval = bbr->r_ctl.bbr_hptsi_segments_floor;
14655 break;
14656 case TCP_BBR_UTTER_MAX_TSO:
14657 optval = bbr->r_ctl.bbr_utter_max;
14658 break;
14659 case TCP_BBR_SEND_IWND_IN_TSO:
14660 /* Do we send TSO size segments initially */
14661 optval = bbr->bbr_init_win_cheat;
14662 break;
14663 case TCP_BBR_EXTRA_STATE:
14664 optval = bbr->rc_use_idle_restart;
14665 break;
14666 case TCP_RACK_TLP_THRESH:
14667 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14668 optval = bbr->rc_tlp_threshold;
14669 break;
14670 case TCP_RACK_PKT_DELAY:
14671 /* RACK added ms i.e. rack-rtt + reord + N */
14672 optval = bbr->r_ctl.rc_pkt_delay;
14673 break;
14674 case TCP_BBR_RETRAN_WTSO:
14675 optval = bbr->rc_resends_use_tso;
14676 break;
14677 case TCP_DATA_AFTER_CLOSE:
14678 optval = bbr->rc_allow_data_af_clo;
14679 break;
14680 case TCP_DELACK:
14681 optval = tp->t_delayed_ack;
14682 break;
14683 case TCP_BBR_HDWR_PACE:
14684 optval = bbr->bbr_hdw_pace_ena;
14685 break;
14686 case TCP_BBR_POLICER_DETECT:
14687 optval = bbr->r_use_policer;
14688 break;
14689 case TCP_BBR_TSTMP_RAISES:
14690 optval = bbr->ts_can_raise;
14691 break;
14692 case TCP_BBR_TMR_PACE_OH:
14693 optval = bbr->r_ctl.rc_incr_tmrs;
14694 break;
14695 case TCP_BBR_PACE_OH:
14696 optval = 0;
14697 if (bbr->r_ctl.rc_inc_tcp_oh)
14698 optval |= BBR_INCL_TCP_OH;
14699 if (bbr->r_ctl.rc_inc_ip_oh)
14700 optval |= BBR_INCL_IP_OH;
14701 if (bbr->r_ctl.rc_inc_enet_oh)
14702 optval |= BBR_INCL_ENET_OH;
14703 break;
14704 default:
14705 return (tcp_default_ctloutput(tp, sopt));
14706 break;
14707 }
14708 INP_WUNLOCK(inp);
14709 error = sooptcopyout(sopt, &optval, sizeof optval);
14710 return (error);
14711 }
14712
14713 /*
14714 * return 0 on success, error-num on failure
14715 */
14716 static int
bbr_ctloutput(struct tcpcb * tp,struct sockopt * sopt)14717 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
14718 {
14719 if (sopt->sopt_dir == SOPT_SET) {
14720 return (bbr_set_sockopt(tp, sopt));
14721 } else if (sopt->sopt_dir == SOPT_GET) {
14722 return (bbr_get_sockopt(tp, sopt));
14723 } else {
14724 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
14725 }
14726 }
14727
14728 static const char *bbr_stack_names[] = {
14729 __XSTRING(STACKNAME),
14730 #ifdef STACKALIAS
14731 __XSTRING(STACKALIAS),
14732 #endif
14733 };
14734
14735 static bool bbr_mod_inited = false;
14736
14737 static int
tcp_addbbr(module_t mod,int32_t type,void * data)14738 tcp_addbbr(module_t mod, int32_t type, void *data)
14739 {
14740 int32_t err = 0;
14741 int num_stacks;
14742
14743 switch (type) {
14744 case MOD_LOAD:
14745 printf("Attempting to load " __XSTRING(MODNAME) "\n");
14746 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
14747 sizeof(struct bbr_sendmap),
14748 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
14749 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
14750 sizeof(struct tcp_bbr),
14751 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
14752 sysctl_ctx_init(&bbr_sysctl_ctx);
14753 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
14754 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
14755 OID_AUTO,
14756 #ifdef STACKALIAS
14757 __XSTRING(STACKALIAS),
14758 #else
14759 __XSTRING(STACKNAME),
14760 #endif
14761 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
14762 "");
14763 if (bbr_sysctl_root == NULL) {
14764 printf("Failed to add sysctl node\n");
14765 err = EFAULT;
14766 goto free_uma;
14767 }
14768 bbr_init_sysctls();
14769 num_stacks = nitems(bbr_stack_names);
14770 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK,
14771 bbr_stack_names, &num_stacks);
14772 if (err) {
14773 printf("Failed to register %s stack name for "
14774 "%s module\n", bbr_stack_names[num_stacks],
14775 __XSTRING(MODNAME));
14776 sysctl_ctx_free(&bbr_sysctl_ctx);
14777 free_uma:
14778 uma_zdestroy(bbr_zone);
14779 uma_zdestroy(bbr_pcb_zone);
14780 bbr_counter_destroy();
14781 printf("Failed to register " __XSTRING(MODNAME)
14782 " module err:%d\n", err);
14783 return (err);
14784 }
14785 tcp_lro_reg_mbufq();
14786 bbr_mod_inited = true;
14787 printf(__XSTRING(MODNAME) " is now available\n");
14788 break;
14789 case MOD_QUIESCE:
14790 err = deregister_tcp_functions(&__tcp_bbr, true, false);
14791 break;
14792 case MOD_UNLOAD:
14793 err = deregister_tcp_functions(&__tcp_bbr, false, true);
14794 if (err == EBUSY)
14795 break;
14796 if (bbr_mod_inited) {
14797 uma_zdestroy(bbr_zone);
14798 uma_zdestroy(bbr_pcb_zone);
14799 sysctl_ctx_free(&bbr_sysctl_ctx);
14800 bbr_counter_destroy();
14801 printf(__XSTRING(MODNAME)
14802 " is now no longer available\n");
14803 bbr_mod_inited = false;
14804 }
14805 tcp_lro_dereg_mbufq();
14806 err = 0;
14807 break;
14808 default:
14809 return (EOPNOTSUPP);
14810 }
14811 return (err);
14812 }
14813
14814 static moduledata_t tcp_bbr = {
14815 .name = __XSTRING(MODNAME),
14816 .evhand = tcp_addbbr,
14817 .priv = 0
14818 };
14819
14820 MODULE_VERSION(MODNAME, 1);
14821 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
14822 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
14823