1 /*-
2 * Copyright (c) 2016-2020 Netflix, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26 /**
27 * Author: Randall Stewart <rrs@netflix.com>
28 * This work is based on the ACM Queue paper
29 * BBR - Congestion Based Congestion Control
30 * and also numerous discussions with Neal, Yuchung and Van.
31 */
32
33 #include <sys/cdefs.h>
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 #include "opt_ratelimit.h"
38 #include <sys/param.h>
39 #include <sys/arb.h>
40 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/libkern.h>
43 #ifdef TCP_HHOOK
44 #include <sys/hhook.h>
45 #endif
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #ifdef STATS
54 #include <sys/qmath.h>
55 #include <sys/tree.h>
56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
57 #endif
58 #include <sys/refcount.h>
59 #include <sys/queue.h>
60 #include <sys/eventhandler.h>
61 #include <sys/smp.h>
62 #include <sys/kthread.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/tim_filter.h>
66 #include <sys/time.h>
67 #include <sys/protosw.h>
68 #include <vm/uma.h>
69 #include <sys/kern_prefetch.h>
70
71 #include <net/route.h>
72 #include <net/route/nhop.h>
73 #include <net/vnet.h>
74
75 #define TCPSTATES /* for logging */
76
77 #include <netinet/in.h>
78 #include <netinet/in_kdtrace.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
83 #include <netinet/ip_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/ip6_var.h>
87 #define TCPOUTFLAGS
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
94 #include <netinet/tcp_hpts.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_log_buf.h>
97 #include <netinet/tcp_ratelimit.h>
98 #include <netinet/tcp_lro.h>
99 #ifdef TCP_OFFLOAD
100 #include <netinet/tcp_offload.h>
101 #endif
102 #ifdef INET6
103 #include <netinet6/tcp6_var.h>
104 #endif
105 #include <netinet/tcp_fastopen.h>
106
107 #include <netipsec/ipsec_support.h>
108 #include <net/if.h>
109 #include <net/if_var.h>
110 #include <net/ethernet.h>
111
112 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
113 #include <netipsec/ipsec.h>
114 #include <netipsec/ipsec6.h>
115 #endif /* IPSEC */
116
117 #include <netinet/udp.h>
118 #include <netinet/udp_var.h>
119 #include <machine/in_cksum.h>
120
121 #ifdef MAC
122 #include <security/mac/mac_framework.h>
123 #endif
124
125 #include "sack_filter.h"
126 #include "tcp_bbr.h"
127 #include "rack_bbr_common.h"
128 uma_zone_t bbr_zone;
129 uma_zone_t bbr_pcb_zone;
130
131 struct sysctl_ctx_list bbr_sysctl_ctx;
132 struct sysctl_oid *bbr_sysctl_root;
133
134 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
135 (tv) = (value); \
136 if ((u_long)(tv) < (u_long)(tvmin)) \
137 (tv) = (tvmin); \
138 if ((u_long)(tv) > (u_long)(tvmax)) \
139 (tv) = (tvmax); \
140 } while(0)
141
142 /*#define BBR_INVARIANT 1*/
143
144 /*
145 * initial window
146 */
147 static uint32_t bbr_def_init_win = 10;
148 static int32_t bbr_persist_min = 250000; /* 250ms */
149 static int32_t bbr_persist_max = 1000000; /* 1 Second */
150 static int32_t bbr_cwnd_may_shrink = 0;
151 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
152 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
153 static int32_t bbr_hardware_pacing_limit = 8000;
154 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
155 static int32_t bbr_no_retran = 0;
156
157 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
158 static int32_t bbr_max_net_error_cnt = 10;
159 /* Should the following be dynamic too -- loss wise */
160 static int32_t bbr_rtt_gain_thresh = 0;
161 /* Measurement controls */
162 static int32_t bbr_use_google_algo = 1;
163 static int32_t bbr_ts_limiting = 1;
164 static int32_t bbr_ts_can_raise = 0;
165 static int32_t bbr_do_red = 600;
166 static int32_t bbr_red_scale = 20000;
167 static int32_t bbr_red_mul = 1;
168 static int32_t bbr_red_div = 2;
169 static int32_t bbr_red_growth_restrict = 1;
170 static int32_t bbr_target_is_bbunit = 0;
171 static int32_t bbr_drop_limit = 0;
172 /*
173 * How much gain do we need to see to
174 * stay in startup?
175 */
176 static int32_t bbr_marks_rxt_sack_passed = 0;
177 static int32_t bbr_start_exit = 25;
178 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
179 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
180 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
181 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
182 * if we go back ever to where the pacer
183 * has priority over timers.
184 */
185 static int32_t bbr_policer_call_from_rack_to = 0;
186 static int32_t bbr_policer_detection_enabled = 1;
187 static int32_t bbr_min_measurements_req = 1; /* We need at least 2
188 * measurements before we are
189 * "good" note that 2 == 1.
190 * This is because we use a >
191 * comparison. This means if
192 * min_measure was 0, it takes
193 * num-measures > min(0) and
194 * you get 1 measurement and
195 * you are good. Set to 1, you
196 * have to have two
197 * measurements (this is done
198 * to prevent it from being ok
199 * to have no measurements). */
200 static int32_t bbr_no_pacing_until = 4;
201
202 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
203 static int32_t bbr_min_peer_delta = 20; /* 20 units */
204 static int32_t bbr_delta_percent = 150; /* 15.0 % */
205
206 static int32_t bbr_target_cwnd_mult_limit = 8;
207 /*
208 * bbr_cwnd_min_val is the number of
209 * segments we hold to in the RTT probe
210 * state typically 4.
211 */
212 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
213
214 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
215
216 static int32_t bbr_gain_to_target = 1;
217 static int32_t bbr_gain_gets_extra_too = 1;
218 /*
219 * bbr_high_gain is the 2/ln(2) value we need
220 * to double the sending rate in startup. This
221 * is used for both cwnd and hptsi gain's.
222 */
223 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
224 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
225 static int32_t bbr_use_lower_gain_in_startup = 1;
226
227 /* thresholds for reduction on drain in sub-states/drain */
228 static int32_t bbr_drain_rtt = BBR_SRTT;
229 static int32_t bbr_drain_floor = 88;
230 static int32_t google_allow_early_out = 1;
231 static int32_t google_consider_lost = 1;
232 static int32_t bbr_drain_drop_mul = 4;
233 static int32_t bbr_drain_drop_div = 5;
234 static int32_t bbr_rand_ot = 50;
235 static int32_t bbr_can_force_probertt = 0;
236 static int32_t bbr_can_adjust_probertt = 1;
237 static int32_t bbr_probertt_sets_rtt = 0;
238 static int32_t bbr_can_use_ts_for_rtt = 1;
239 static int32_t bbr_is_ratio = 0;
240 static int32_t bbr_sub_drain_app_limit = 1;
241 static int32_t bbr_prtt_slam_cwnd = 1;
242 static int32_t bbr_sub_drain_slam_cwnd = 1;
243 static int32_t bbr_slam_cwnd_in_main_drain = 1;
244 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
245 * hold */
246 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
247 /*
248 * bbr_drain_gain is the reverse of the high_gain
249 * designed to drain back out the standing queue
250 * that is formed in startup by causing a larger
251 * hptsi gain and thus drainging the packets
252 * in flight.
253 */
254 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
255 static int32_t bbr_rttprobe_gain = 192;
256
257 /*
258 * The cwnd_gain is the default cwnd gain applied when
259 * calculating a target cwnd. Note that the cwnd is
260 * a secondary factor in the way BBR works (see the
261 * paper and think about it, it will take some time).
262 * Basically the hptsi_gain spreads the packets out
263 * so you never get more than BDP to the peer even
264 * if the cwnd is high. In our implemenation that
265 * means in non-recovery/retransmission scenarios
266 * cwnd will never be reached by the flight-size.
267 */
268 static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
269 static int32_t bbr_tlp_type_to_use = BBR_SRTT;
270 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
271 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
272 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
273 static int32_t bbr_ignore_data_after_close = 1;
274 static int16_t bbr_hptsi_gain[] = {
275 (BBR_UNIT *5 / 4),
276 (BBR_UNIT * 3 / 4),
277 BBR_UNIT,
278 BBR_UNIT,
279 BBR_UNIT,
280 BBR_UNIT,
281 BBR_UNIT,
282 BBR_UNIT
283 };
284 int32_t bbr_use_rack_resend_cheat = 1;
285 int32_t bbr_sends_full_iwnd = 1;
286
287 #define BBR_HPTSI_GAIN_MAX 8
288 /*
289 * The BBR module incorporates a number of
290 * TCP ideas that have been put out into the IETF
291 * over the last few years:
292 * - Yuchung Cheng's RACK TCP (for which its named) that
293 * will stop us using the number of dup acks and instead
294 * use time as the gage of when we retransmit.
295 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
296 * of Dukkipati et.al.
297 * - Van Jacobson's et.al BBR.
298 *
299 * RACK depends on SACK, so if an endpoint arrives that
300 * cannot do SACK the state machine below will shuttle the
301 * connection back to using the "default" TCP stack that is
302 * in FreeBSD.
303 *
304 * To implement BBR and RACK the original TCP stack was first decomposed
305 * into a functional state machine with individual states
306 * for each of the possible TCP connection states. The do_segment
307 * functions role in life is to mandate the connection supports SACK
308 * initially and then assure that the RACK state matches the conenction
309 * state before calling the states do_segment function. Data processing
310 * of inbound segments also now happens in the hpts_do_segment in general
311 * with only one exception. This is so we can keep the connection on
312 * a single CPU.
313 *
314 * Each state is simplified due to the fact that the original do_segment
315 * has been decomposed and we *know* what state we are in (no
316 * switches on the state) and all tests for SACK are gone. This
317 * greatly simplifies what each state does.
318 *
319 * TCP output is also over-written with a new version since it
320 * must maintain the new rack scoreboard and has had hptsi
321 * integrated as a requirment. Still todo is to eliminate the
322 * use of the callout_() system and use the hpts for all
323 * timers as well.
324 */
325 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
326 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
327 static const int32_t bbr_min_req_free = 2; /* The min we must have on the
328 * free list */
329 static int32_t bbr_tlp_thresh = 1;
330 static int32_t bbr_reorder_thresh = 2;
331 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
332 * 60,000,000 - 60 seconds */
333 static int32_t bbr_pkt_delay = 1000;
334 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
335 static int32_t bbr_incr_timers = 1;
336
337 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
338 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
339 static int32_t bbr_exit_startup_at_loss = 1;
340
341 /*
342 * bbr_lt_bw_ratio is 1/8th
343 * bbr_lt_bw_diff is < 4 Kbit/sec
344 */
345 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
346 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
347 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
348 * the lt_bw for */
349 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
350 * lt_bw */
351 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
352 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
353 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
354
355 static int32_t bbr_verbose_logging = 0;
356 /*
357 * Currently regular tcp has a rto_min of 30ms
358 * the backoff goes 12 times so that ends up
359 * being a total of 122.850 seconds before a
360 * connection is killed.
361 */
362 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
363 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
364
365 /****************************************************/
366 /* DEFAULT TSO SIZING (cpu performance impacting) */
367 /****************************************************/
368 /* What amount is our formula using to get TSO size */
369 static int32_t bbr_hptsi_per_second = 1000;
370
371 /*
372 * For hptsi under bbr_cross_over connections what is delay
373 * target 7ms (in usec) combined with a seg_max of 2
374 * gets us close to identical google behavior in
375 * TSO size selection (possibly more 1MSS sends).
376 */
377 static int32_t bbr_hptsi_segments_delay_tar = 7000;
378
379 /* Does pacing delay include overhead's in its time calculations? */
380 static int32_t bbr_include_enet_oh = 0;
381 static int32_t bbr_include_ip_oh = 1;
382 static int32_t bbr_include_tcp_oh = 1;
383 static int32_t bbr_google_discount = 10;
384
385 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
386 static int32_t bbr_state_is_pkt_epoch = 0;
387 static int32_t bbr_state_drain_2_tar = 1;
388 /* What is the max the 0 - bbr_cross_over MBPS TSO target
389 * can reach using our delay target. Note that this
390 * value becomes the floor for the cross over
391 * algorithm.
392 */
393 static int32_t bbr_hptsi_segments_max = 2;
394 static int32_t bbr_hptsi_segments_floor = 1;
395 static int32_t bbr_hptsi_utter_max = 0;
396
397 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
398 static int32_t bbr_hptsi_bytes_min = 1460;
399 static int32_t bbr_all_get_min = 0;
400
401 /* Cross over point from algo-a to algo-b */
402 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
403
404 /* Do we deal with our restart state? */
405 static int32_t bbr_uses_idle_restart = 0;
406 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
407
408 /* Do we allow hardware pacing? */
409 static int32_t bbr_allow_hdwr_pacing = 0;
410 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
411 static int32_t bbr_hdwr_pace_floor = 1;
412 static int32_t bbr_hdwr_pacing_delay_cnt = 10;
413
414 /****************************************************/
415 static int32_t bbr_resends_use_tso = 0;
416 static int32_t bbr_tlp_max_resend = 2;
417 static int32_t bbr_sack_block_limit = 128;
418
419 #define BBR_MAX_STAT 19
420 counter_u64_t bbr_state_time[BBR_MAX_STAT];
421 counter_u64_t bbr_state_lost[BBR_MAX_STAT];
422 counter_u64_t bbr_state_resend[BBR_MAX_STAT];
423 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
424 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
425 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
426 counter_u64_t bbr_flows_whdwr_pacing;
427 counter_u64_t bbr_flows_nohdwr_pacing;
428
429 counter_u64_t bbr_nohdwr_pacing_enobuf;
430 counter_u64_t bbr_hdwr_pacing_enobuf;
431
432 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
433
434 /*
435 * Static defintions we need for forward declarations.
436 */
437 static uint32_t
438 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
439 uint32_t useconds_time, uint64_t bw);
440 static uint32_t
441 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
442 static void
443 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
444 static void
445 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
446 static void
447 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
448 int dolog);
449 static uint32_t
450 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
451 static void
452 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
453 int32_t pkt_epoch, uint32_t losses);
454 static uint32_t
455 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts,
456 struct bbr_sendmap *rsm);
457 static uint32_t
458 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
459 static uint32_t
460 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
461 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts);
462 static void
463 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
464 int32_t line);
465 static void
466 bbr_set_state_target(struct tcp_bbr *bbr, int line);
467 static void
468 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
469 static void
470 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick,
471 int event, int line);
472 static void
473 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
474 static void
475 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
476 static void
477 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
478 uint32_t rtt, uint32_t line, uint8_t is_start,
479 uint16_t set);
480 static struct bbr_sendmap *
481 bbr_find_lowest_rsm(struct tcp_bbr *bbr);
482 static __inline uint32_t
483 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
484 static void
485 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot,
486 uint8_t which);
487 static void
488 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts,
489 uint32_t time_since_sent, uint32_t srtt,
490 uint32_t thresh, uint32_t to);
491 static void
492 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
493 static void
494 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
495 uint32_t del_by, uint32_t cts, uint32_t sloton,
496 uint32_t prev_delay);
497 static void
498 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
499 int32_t line);
500 static void
501 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr);
502 static void
503 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
504 static void
505 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
506 static void
507 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
508 static void
509 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
510 uint32_t cts, uint32_t usecs, uint64_t bw,
511 uint32_t override, int mod);
512 static int bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
513
514 static inline uint8_t
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 if (tp->t_maxseg < V_tcp_mssdflt) {
5138 /*
5139 * The MSS is so small we should not
5140 * process incoming SACK's since we are
5141 * subject to attack in such a case.
5142 */
5143 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
5144 } else {
5145 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
5146 }
5147 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5148 }
5149 }
5150 }
5151 /*
5152 * Disable RFC1323 and SACK if we haven't got any response to our
5153 * third SYN to work-around some broken terminal servers (most of
5154 * which have hopefully been retired) that have bad VJ header
5155 * compression code which trashes TCP segments containing
5156 * unknown-to-them TCP options.
5157 */
5158 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
5159 (tp->t_rxtshift == 3))
5160 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
5161 /*
5162 * If we backed off this far, our srtt estimate is probably bogus.
5163 * Clobber it so we'll take the next rtt measurement as our srtt;
5164 * move the current srtt into rttvar to keep the current retransmit
5165 * times until then.
5166 */
5167 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5168 #ifdef INET6
5169 if (bbr->r_is_v6)
5170 in6_losing(inp);
5171 else
5172 #endif
5173 in_losing(inp);
5174 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5175 tp->t_srtt = 0;
5176 }
5177 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
5178 tp->snd_recover = tp->snd_max;
5179 tp->t_flags |= TF_ACKNOW;
5180 tp->t_rtttime = 0;
5181
5182 return (retval);
5183 }
5184
5185 static int
bbr_process_timers(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,uint8_t hpts_calling)5186 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling)
5187 {
5188 int32_t ret = 0;
5189 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5190
5191 if (timers == 0) {
5192 return (0);
5193 }
5194 if (tp->t_state == TCPS_LISTEN) {
5195 /* no timers on listen sockets */
5196 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5197 return (0);
5198 return (1);
5199 }
5200 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
5201 uint32_t left;
5202
5203 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5204 ret = -1;
5205 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5206 return (0);
5207 }
5208 if (hpts_calling == 0) {
5209 ret = -2;
5210 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5211 return (0);
5212 }
5213 /*
5214 * Ok our timer went off early and we are not paced false
5215 * alarm, go back to sleep.
5216 */
5217 left = bbr->r_ctl.rc_timer_exp - cts;
5218 ret = -3;
5219 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling);
5220 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left));
5221 return (1);
5222 }
5223 bbr->rc_tmr_stopped = 0;
5224 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5225 if (timers & PACE_TMR_DELACK) {
5226 ret = bbr_timeout_delack(tp, bbr, cts);
5227 } else if (timers & PACE_TMR_PERSIT) {
5228 ret = bbr_timeout_persist(tp, bbr, cts);
5229 } else if (timers & PACE_TMR_RACK) {
5230 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5231 ret = bbr_timeout_rack(tp, bbr, cts);
5232 } else if (timers & PACE_TMR_TLP) {
5233 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5234 ret = bbr_timeout_tlp(tp, bbr, cts);
5235 } else if (timers & PACE_TMR_RXT) {
5236 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5237 ret = bbr_timeout_rxt(tp, bbr, cts);
5238 } else if (timers & PACE_TMR_KEEP) {
5239 ret = bbr_timeout_keepalive(tp, bbr, cts);
5240 }
5241 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling);
5242 return (ret);
5243 }
5244
5245 static void
bbr_timer_cancel(struct tcp_bbr * bbr,int32_t line,uint32_t cts)5246 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts)
5247 {
5248 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5249 uint8_t hpts_removed = 0;
5250
5251 if (tcp_in_hpts(bbr->rc_tp) &&
5252 (bbr->rc_timer_first == 1)) {
5253 /*
5254 * If we are canceling timer's when we have the
5255 * timer ahead of the output being paced. We also
5256 * must remove ourselves from the hpts.
5257 */
5258 hpts_removed = 1;
5259 tcp_hpts_remove(bbr->rc_tp);
5260 if (bbr->r_ctl.rc_last_delay_val) {
5261 /* Update the last hptsi delay too */
5262 uint32_t time_since_send;
5263
5264 if (TSTMP_GT(cts, bbr->rc_pacer_started))
5265 time_since_send = cts - bbr->rc_pacer_started;
5266 else
5267 time_since_send = 0;
5268 if (bbr->r_ctl.rc_last_delay_val > time_since_send) {
5269 /* Cut down our slot time */
5270 bbr->r_ctl.rc_last_delay_val -= time_since_send;
5271 } else {
5272 bbr->r_ctl.rc_last_delay_val = 0;
5273 }
5274 bbr->rc_pacer_started = cts;
5275 }
5276 }
5277 bbr->rc_timer_first = 0;
5278 bbr_log_to_cancel(bbr, line, cts, hpts_removed);
5279 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5280 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5281 }
5282 }
5283
5284 static int
bbr_stopall(struct tcpcb * tp)5285 bbr_stopall(struct tcpcb *tp)
5286 {
5287 struct tcp_bbr *bbr;
5288
5289 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
5290 bbr->rc_all_timers_stopped = 1;
5291
5292 tcp_hpts_remove(tp);
5293
5294 return (0);
5295 }
5296
5297 static uint32_t
bbr_get_earliest_send_outstanding(struct tcp_bbr * bbr,struct bbr_sendmap * u_rsm,uint32_t cts)5298 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts)
5299 {
5300 struct bbr_sendmap *rsm;
5301
5302 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
5303 if ((rsm == NULL) || (u_rsm == rsm))
5304 return (cts);
5305 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
5306 }
5307
5308 static void
bbr_update_rsm(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t cts,uint32_t pacing_time)5309 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr,
5310 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time)
5311 {
5312 int32_t idx;
5313
5314 rsm->r_rtr_cnt++;
5315 rsm->r_dupack = 0;
5316 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) {
5317 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS;
5318 rsm->r_flags |= BBR_OVERMAX;
5319 }
5320 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
5321 /* Take off the collapsed flag at rxt */
5322 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
5323 }
5324 if (rsm->r_flags & BBR_MARKED_LOST) {
5325 /* We have retransmitted, its no longer lost */
5326 rsm->r_flags &= ~BBR_MARKED_LOST;
5327 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
5328 }
5329 if (rsm->r_flags & BBR_RXT_CLEARED) {
5330 /*
5331 * We hit a RXT timer on it and
5332 * we cleared the "acked" flag.
5333 * We now have it going back into
5334 * flight, we can remove the cleared
5335 * flag and possibly do accounting on
5336 * this piece.
5337 */
5338 rsm->r_flags &= ~BBR_RXT_CLEARED;
5339 }
5340 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) {
5341 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5342 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5343 }
5344 idx = rsm->r_rtr_cnt - 1;
5345 rsm->r_tim_lastsent[idx] = cts;
5346 rsm->r_pacing_delay = pacing_time;
5347 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5348 rsm->r_ts_valid = bbr->rc_ts_valid;
5349 if (bbr->rc_ts_valid)
5350 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5351 if (bbr->r_ctl.r_app_limited_until)
5352 rsm->r_app_limited = 1;
5353 else
5354 rsm->r_app_limited = 0;
5355 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5356 rsm->r_bbr_state = bbr_state_val(bbr);
5357 else
5358 rsm->r_bbr_state = 8;
5359 if (rsm->r_flags & BBR_ACKED) {
5360 /* Problably MTU discovery messing with us */
5361 uint32_t old_flags;
5362
5363 old_flags = rsm->r_flags;
5364 rsm->r_flags &= ~BBR_ACKED;
5365 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
5366 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5367 if (bbr->r_ctl.rc_sacked == 0)
5368 bbr->r_ctl.rc_sacklast = NULL;
5369 }
5370 if (rsm->r_in_tmap) {
5371 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5372 }
5373 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5374 rsm->r_in_tmap = 1;
5375 if (rsm->r_flags & BBR_SACK_PASSED) {
5376 /* We have retransmitted due to the SACK pass */
5377 rsm->r_flags &= ~BBR_SACK_PASSED;
5378 rsm->r_flags |= BBR_WAS_SACKPASS;
5379 }
5380 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5381 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5382 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5383 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
5384 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5385 rsm->r_is_gain = 1;
5386 rsm->r_is_drain = 0;
5387 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5388 rsm->r_is_drain = 1;
5389 rsm->r_is_gain = 0;
5390 } else {
5391 rsm->r_is_drain = 0;
5392 rsm->r_is_gain = 0;
5393 }
5394 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */
5395 }
5396
5397 /*
5398 * Returns 0, or the sequence where we stopped
5399 * updating. We also update the lenp to be the amount
5400 * of data left.
5401 */
5402
5403 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)5404 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr,
5405 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time)
5406 {
5407 /*
5408 * We (re-)transmitted starting at rsm->r_start for some length
5409 * (possibly less than r_end.
5410 */
5411 struct bbr_sendmap *nrsm;
5412 uint32_t c_end;
5413 int32_t len;
5414
5415 len = *lenp;
5416 c_end = rsm->r_start + len;
5417 if (SEQ_GEQ(c_end, rsm->r_end)) {
5418 /*
5419 * We retransmitted the whole piece or more than the whole
5420 * slopping into the next rsm.
5421 */
5422 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5423 if (c_end == rsm->r_end) {
5424 *lenp = 0;
5425 return (0);
5426 } else {
5427 int32_t act_len;
5428
5429 /* Hangs over the end return whats left */
5430 act_len = rsm->r_end - rsm->r_start;
5431 *lenp = (len - act_len);
5432 return (rsm->r_end);
5433 }
5434 /* We don't get out of this block. */
5435 }
5436 /*
5437 * Here we retransmitted less than the whole thing which means we
5438 * have to split this into what was transmitted and what was not.
5439 */
5440 nrsm = bbr_alloc_full_limit(bbr);
5441 if (nrsm == NULL) {
5442 *lenp = 0;
5443 return (0);
5444 }
5445 /*
5446 * So here we are going to take the original rsm and make it what we
5447 * retransmitted. nrsm will be the tail portion we did not
5448 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
5449 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
5450 * 1, 6 and the new piece will be 6, 11.
5451 */
5452 bbr_clone_rsm(bbr, nrsm, rsm, c_end);
5453 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
5454 nrsm->r_dupack = 0;
5455 if (rsm->r_in_tmap) {
5456 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5457 nrsm->r_in_tmap = 1;
5458 }
5459 rsm->r_flags &= (~BBR_HAS_FIN);
5460 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5461 *lenp = 0;
5462 return (0);
5463 }
5464
5465 static uint64_t
bbr_get_hardware_rate(struct tcp_bbr * bbr)5466 bbr_get_hardware_rate(struct tcp_bbr *bbr)
5467 {
5468 uint64_t bw;
5469
5470 bw = bbr_get_bw(bbr);
5471 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN];
5472 bw /= (uint64_t)BBR_UNIT;
5473 return(bw);
5474 }
5475
5476 static void
bbr_setup_less_of_rate(struct tcp_bbr * bbr,uint32_t cts,uint64_t act_rate,uint64_t rate_wanted)5477 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts,
5478 uint64_t act_rate, uint64_t rate_wanted)
5479 {
5480 /*
5481 * We could not get a full gains worth
5482 * of rate.
5483 */
5484 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) {
5485 /* we can't even get the real rate */
5486 uint64_t red;
5487
5488 bbr->skip_gain = 1;
5489 bbr->gain_is_limited = 0;
5490 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate;
5491 if (red)
5492 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts);
5493 } else {
5494 /* We can use a lower gain */
5495 bbr->skip_gain = 0;
5496 bbr->gain_is_limited = 1;
5497 }
5498 }
5499
5500 static void
bbr_update_hardware_pacing_rate(struct tcp_bbr * bbr,uint32_t cts)5501 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts)
5502 {
5503 const struct tcp_hwrate_limit_table *nrte;
5504 int error, rate = -1;
5505
5506 if (bbr->r_ctl.crte == NULL)
5507 return;
5508 if ((bbr->rc_inp->inp_route.ro_nh == NULL) ||
5509 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) {
5510 /* Lost our routes? */
5511 /* Clear the way for a re-attempt */
5512 bbr->bbr_attempt_hdwr_pace = 0;
5513 lost_rate:
5514 bbr->gain_is_limited = 0;
5515 bbr->skip_gain = 0;
5516 bbr->bbr_hdrw_pacing = 0;
5517 counter_u64_add(bbr_flows_whdwr_pacing, -1);
5518 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
5519 tcp_bbr_tso_size_check(bbr, cts);
5520 return;
5521 }
5522 rate = bbr_get_hardware_rate(bbr);
5523 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte,
5524 bbr->rc_tp,
5525 bbr->rc_inp->inp_route.ro_nh->nh_ifp,
5526 rate,
5527 (RS_PACING_GEQ|RS_PACING_SUB_OK),
5528 &error, NULL);
5529 if (nrte == NULL) {
5530 goto lost_rate;
5531 }
5532 if (nrte != bbr->r_ctl.crte) {
5533 bbr->r_ctl.crte = nrte;
5534 if (error == 0) {
5535 BBR_STAT_INC(bbr_hdwr_rl_mod_ok);
5536 if (bbr->r_ctl.crte->rate < rate) {
5537 /* We have a problem */
5538 bbr_setup_less_of_rate(bbr, cts,
5539 bbr->r_ctl.crte->rate, rate);
5540 } else {
5541 /* We are good */
5542 bbr->gain_is_limited = 0;
5543 bbr->skip_gain = 0;
5544 }
5545 } else {
5546 /* A failure should release the tag */
5547 BBR_STAT_INC(bbr_hdwr_rl_mod_fail);
5548 bbr->gain_is_limited = 0;
5549 bbr->skip_gain = 0;
5550 bbr->bbr_hdrw_pacing = 0;
5551 }
5552 bbr_type_log_hdwr_pacing(bbr,
5553 bbr->r_ctl.crte->ptbl->rs_ifp,
5554 rate,
5555 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate),
5556 __LINE__,
5557 cts,
5558 error);
5559 }
5560 }
5561
5562 static void
bbr_adjust_for_hw_pacing(struct tcp_bbr * bbr,uint32_t cts)5563 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts)
5564 {
5565 /*
5566 * If we have hardware pacing support
5567 * we need to factor that in for our
5568 * TSO size.
5569 */
5570 const struct tcp_hwrate_limit_table *rlp;
5571 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay;
5572
5573 if ((bbr->bbr_hdrw_pacing == 0) ||
5574 (IN_RECOVERY(bbr->rc_tp->t_flags)) ||
5575 (bbr->r_ctl.crte == NULL))
5576 return;
5577 if (bbr->hw_pacing_set == 0) {
5578 /* Not yet by the hdwr pacing count delay */
5579 return;
5580 }
5581 if (bbr_hdwr_pace_adjust == 0) {
5582 /* No adjustment */
5583 return;
5584 }
5585 rlp = bbr->r_ctl.crte;
5586 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options)
5587 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5588 else
5589 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5590 /*
5591 * So lets first get the
5592 * time we will take between
5593 * TSO sized sends currently without
5594 * hardware help.
5595 */
5596 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT,
5597 bbr->r_ctl.rc_pace_max_segs, cts, 1);
5598 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg;
5599 hdwr_delay *= rlp->time_between;
5600 if (cur_delay > hdwr_delay)
5601 delta = cur_delay - hdwr_delay;
5602 else
5603 delta = 0;
5604 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay,
5605 (bbr->r_ctl.rc_pace_max_segs / maxseg),
5606 1);
5607 if (delta &&
5608 (delta < (max(rlp->time_between,
5609 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) {
5610 /*
5611 * Now lets divide by the pacing
5612 * time between each segment the
5613 * hardware sends rounding up and
5614 * derive a bytes from that. We multiply
5615 * that by bbr_hdwr_pace_adjust to get
5616 * more bang for our buck.
5617 *
5618 * The goal is to have the software pacer
5619 * waiting no more than an additional
5620 * pacing delay if we can (without the
5621 * compensation i.e. x bbr_hdwr_pace_adjust).
5622 */
5623 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between),
5624 (bbr->r_ctl.rc_pace_max_segs/maxseg));
5625 seg_sz *= bbr_hdwr_pace_adjust;
5626 if (bbr_hdwr_pace_floor &&
5627 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5628 /* Currently hardware paces
5629 * out rs_min_seg segments at a time.
5630 * We need to make sure we always send at least
5631 * a full burst of bbr_hdwr_pace_floor down.
5632 */
5633 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5634 }
5635 seg_sz *= maxseg;
5636 } else if (delta == 0) {
5637 /*
5638 * The highest pacing rate is
5639 * above our b/w gained. This means
5640 * we probably are going quite fast at
5641 * the hardware highest rate. Lets just multiply
5642 * the calculated TSO size by the
5643 * multiplier factor (its probably
5644 * 4 segments in the default config for
5645 * mlx).
5646 */
5647 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust;
5648 if (bbr_hdwr_pace_floor &&
5649 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5650 /* Currently hardware paces
5651 * out rs_min_seg segments at a time.
5652 * We need to make sure we always send at least
5653 * a full burst of bbr_hdwr_pace_floor down.
5654 */
5655 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5656 }
5657 } else {
5658 /*
5659 * The pacing time difference is so
5660 * big that the hardware will
5661 * pace out more rapidly then we
5662 * really want and then we
5663 * will have a long delay. Lets just keep
5664 * the same TSO size so its as if
5665 * we were not using hdwr pacing (we
5666 * just gain a bit of spacing from the
5667 * hardware if seg_sz > 1).
5668 */
5669 seg_sz = bbr->r_ctl.rc_pace_max_segs;
5670 }
5671 if (seg_sz > bbr->r_ctl.rc_pace_max_segs)
5672 new_tso = seg_sz;
5673 else
5674 new_tso = bbr->r_ctl.rc_pace_max_segs;
5675 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg))
5676 new_tso = PACE_MAX_IP_BYTES - maxseg;
5677
5678 if (new_tso != bbr->r_ctl.rc_pace_max_segs) {
5679 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0);
5680 bbr->r_ctl.rc_pace_max_segs = new_tso;
5681 }
5682 }
5683
5684 static void
tcp_bbr_tso_size_check(struct tcp_bbr * bbr,uint32_t cts)5685 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts)
5686 {
5687 uint64_t bw;
5688 uint32_t old_tso = 0, new_tso;
5689 uint32_t maxseg, bytes;
5690 uint32_t tls_seg=0;
5691 /*
5692 * Google/linux uses the following algorithm to determine
5693 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18):
5694 *
5695 * bytes = bw_in_bytes_per_second / 1000
5696 * bytes = min(bytes, 64k)
5697 * tso_segs = bytes / MSS
5698 * if (bw < 1.2Mbs)
5699 * min_tso_segs = 1
5700 * else
5701 * min_tso_segs = 2
5702 * tso_segs = max(tso_segs, min_tso_segs)
5703 *
5704 * * Note apply a device specific limit (we apply this in the
5705 * tcp_m_copym).
5706 * Note that before the initial measurement is made google bursts out
5707 * a full iwnd just like new-reno/cubic.
5708 *
5709 * We do not use this algorithm. Instead we
5710 * use a two phased approach:
5711 *
5712 * if ( bw <= per-tcb-cross-over)
5713 * goal_tso = calculate how much with this bw we
5714 * can send in goal-time seconds.
5715 * if (goal_tso > mss)
5716 * seg = goal_tso / mss
5717 * tso = seg * mss
5718 * else
5719 * tso = mss
5720 * if (tso > per-tcb-max)
5721 * tso = per-tcb-max
5722 * else if ( bw > 512Mbps)
5723 * tso = max-tso (64k/mss)
5724 * else
5725 * goal_tso = bw / per-tcb-divsor
5726 * seg = (goal_tso + mss-1)/mss
5727 * tso = seg * mss
5728 *
5729 * if (tso < per-tcb-floor)
5730 * tso = per-tcb-floor
5731 * if (tso > per-tcb-utter_max)
5732 * tso = per-tcb-utter_max
5733 *
5734 * Note the default per-tcb-divisor is 1000 (same as google).
5735 * the goal cross over is 30Mbps however. To recreate googles
5736 * algorithm you need to set:
5737 *
5738 * cross-over = 23,168,000 bps
5739 * goal-time = 18000
5740 * per-tcb-max = 2
5741 * per-tcb-divisor = 1000
5742 * per-tcb-floor = 1
5743 *
5744 * This will get you "google bbr" behavior with respect to tso size.
5745 *
5746 * Note we do set anything TSO size until we are past the initial
5747 * window. Before that we gnerally use either a single MSS
5748 * or we use the full IW size (so we burst a IW at a time)
5749 */
5750
5751 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) {
5752 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5753 } else {
5754 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5755 }
5756 old_tso = bbr->r_ctl.rc_pace_max_segs;
5757 if (bbr->rc_past_init_win == 0) {
5758 /*
5759 * Not enough data has been acknowledged to make a
5760 * judgement. Set up the initial TSO based on if we
5761 * are sending a full IW at once or not.
5762 */
5763 if (bbr->rc_use_google)
5764 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2);
5765 else if (bbr->bbr_init_win_cheat)
5766 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp);
5767 else
5768 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5769 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg)
5770 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg;
5771 if (bbr->r_ctl.rc_pace_max_segs == 0) {
5772 bbr->r_ctl.rc_pace_max_segs = maxseg;
5773 }
5774 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0);
5775 bbr_adjust_for_hw_pacing(bbr, cts);
5776 return;
5777 }
5778 /**
5779 * Now lets set the TSO goal based on our delivery rate in
5780 * bytes per second. Note we only do this if
5781 * we have acked at least the initial cwnd worth of data.
5782 */
5783 bw = bbr_get_bw(bbr);
5784 if (IN_RECOVERY(bbr->rc_tp->t_flags) &&
5785 (bbr->rc_use_google == 0)) {
5786 /* We clamp to one MSS in recovery */
5787 new_tso = maxseg;
5788 } else if (bbr->rc_use_google) {
5789 int min_tso_segs;
5790
5791 /* Google considers the gain too */
5792 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) {
5793 bw *= bbr->r_ctl.rc_bbr_hptsi_gain;
5794 bw /= BBR_UNIT;
5795 }
5796 bytes = bw / 1024;
5797 if (bytes > (64 * 1024))
5798 bytes = 64 * 1024;
5799 new_tso = bytes / maxseg;
5800 if (bw < ONE_POINT_TWO_MEG)
5801 min_tso_segs = 1;
5802 else
5803 min_tso_segs = 2;
5804 if (new_tso < min_tso_segs)
5805 new_tso = min_tso_segs;
5806 new_tso *= maxseg;
5807 } else if (bbr->rc_no_pacing) {
5808 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg;
5809 } else if (bw <= bbr->r_ctl.bbr_cross_over) {
5810 /*
5811 * Calculate the worse case b/w TSO if we are inserting no
5812 * more than a delay_target number of TSO's.
5813 */
5814 uint32_t tso_len, min_tso;
5815
5816 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw);
5817 if (tso_len > maxseg) {
5818 new_tso = tso_len / maxseg;
5819 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max)
5820 new_tso = bbr->r_ctl.bbr_hptsi_segments_max;
5821 new_tso *= maxseg;
5822 } else {
5823 /*
5824 * less than a full sized frame yikes.. long rtt or
5825 * low bw?
5826 */
5827 min_tso = bbr_minseg(bbr);
5828 if ((tso_len > min_tso) && (bbr_all_get_min == 0))
5829 new_tso = rounddown(tso_len, min_tso);
5830 else
5831 new_tso = min_tso;
5832 }
5833 } else if (bw > FIVETWELVE_MBPS) {
5834 /*
5835 * This guy is so fast b/w wise that we can TSO as large as
5836 * possible of segments that the NIC will allow.
5837 */
5838 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5839 } else {
5840 /*
5841 * This formula is based on attempting to send a segment or
5842 * more every bbr_hptsi_per_second. The default is 1000
5843 * which means you are targeting what you can send every 1ms
5844 * based on the peers bw.
5845 *
5846 * If the number drops to say 500, then you are looking more
5847 * at 2ms and you will raise how much we send in a single
5848 * TSO thus saving CPU (less bbr_output_wtime() calls). The
5849 * trade off of course is you will send more at once and
5850 * thus tend to clump up the sends into larger "bursts"
5851 * building a queue.
5852 */
5853 bw /= bbr->r_ctl.bbr_hptsi_per_second;
5854 new_tso = roundup(bw, (uint64_t)maxseg);
5855 /*
5856 * Gate the floor to match what our lower than 48Mbps
5857 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus
5858 * becomes the floor for this calculation.
5859 */
5860 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg))
5861 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg);
5862 }
5863 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor)))
5864 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor;
5865 if (new_tso > PACE_MAX_IP_BYTES)
5866 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5867 /* Enforce an utter maximum. */
5868 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) {
5869 new_tso = bbr->r_ctl.bbr_utter_max * maxseg;
5870 }
5871 if (old_tso != new_tso) {
5872 /* Only log changes */
5873 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0);
5874 bbr->r_ctl.rc_pace_max_segs = new_tso;
5875 }
5876 /* We have hardware pacing! */
5877 bbr_adjust_for_hw_pacing(bbr, cts);
5878 }
5879
5880 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)5881 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len,
5882 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts,
5883 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc,
5884 struct sockbuf *sb)
5885 {
5886
5887 struct bbr_sendmap *rsm, *nrsm;
5888 register uint32_t snd_max, snd_una;
5889 uint32_t pacing_time;
5890 /*
5891 * Add to the RACK log of packets in flight or retransmitted. If
5892 * there is a TS option we will use the TS echoed, if not we will
5893 * grab a TS.
5894 *
5895 * Retransmissions will increment the count and move the ts to its
5896 * proper place. Note that if options do not include TS's then we
5897 * won't be able to effectively use the ACK for an RTT on a retran.
5898 *
5899 * Notes about r_start and r_end. Lets consider a send starting at
5900 * sequence 1 for 10 bytes. In such an example the r_start would be
5901 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
5902 * This means that r_end is actually the first sequence for the next
5903 * slot (11).
5904 *
5905 */
5906 INP_WLOCK_ASSERT(tptoinpcb(tp));
5907 if (err) {
5908 /*
5909 * We don't log errors -- we could but snd_max does not
5910 * advance in this case either.
5911 */
5912 return;
5913 }
5914 if (th_flags & TH_RST) {
5915 /*
5916 * We don't log resets and we return immediately from
5917 * sending
5918 */
5919 *abandon = 1;
5920 return;
5921 }
5922 snd_una = tp->snd_una;
5923 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) {
5924 /*
5925 * The call to bbr_log_output is made before bumping
5926 * snd_max. This means we can record one extra byte on a SYN
5927 * or FIN if seq_out is adding more on and a FIN is present
5928 * (and we are not resending).
5929 */
5930 if ((th_flags & TH_SYN) && (tp->iss == seq_out))
5931 len++;
5932 if (th_flags & TH_FIN)
5933 len++;
5934 }
5935 if (SEQ_LEQ((seq_out + len), snd_una)) {
5936 /* Are sending an old segment to induce an ack (keep-alive)? */
5937 return;
5938 }
5939 if (SEQ_LT(seq_out, snd_una)) {
5940 /* huh? should we panic? */
5941 uint32_t end;
5942
5943 end = seq_out + len;
5944 seq_out = snd_una;
5945 len = end - seq_out;
5946 }
5947 snd_max = tp->snd_max;
5948 if (len == 0) {
5949 /* We don't log zero window probes */
5950 return;
5951 }
5952 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1);
5953 /* First question is it a retransmission? */
5954 if (seq_out == snd_max) {
5955 again:
5956 rsm = bbr_alloc(bbr);
5957 if (rsm == NULL) {
5958 return;
5959 }
5960 rsm->r_flags = 0;
5961 if (th_flags & TH_SYN)
5962 rsm->r_flags |= BBR_HAS_SYN;
5963 if (th_flags & TH_FIN)
5964 rsm->r_flags |= BBR_HAS_FIN;
5965 rsm->r_tim_lastsent[0] = cts;
5966 rsm->r_rtr_cnt = 1;
5967 rsm->r_rtr_bytes = 0;
5968 rsm->r_start = seq_out;
5969 rsm->r_end = rsm->r_start + len;
5970 rsm->r_dupack = 0;
5971 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5972 rsm->r_pacing_delay = pacing_time;
5973 rsm->r_ts_valid = bbr->rc_ts_valid;
5974 if (bbr->rc_ts_valid)
5975 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5976 rsm->r_del_time = bbr->r_ctl.rc_del_time;
5977 if (bbr->r_ctl.r_app_limited_until)
5978 rsm->r_app_limited = 1;
5979 else
5980 rsm->r_app_limited = 0;
5981 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5982 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5983 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5984 /*
5985 * Here we must also add in this rsm since snd_max
5986 * is updated after we return from a new send.
5987 */
5988 rsm->r_flight_at_send += len;
5989 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
5990 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5991 rsm->r_in_tmap = 1;
5992 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5993 rsm->r_bbr_state = bbr_state_val(bbr);
5994 else
5995 rsm->r_bbr_state = 8;
5996 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5997 rsm->r_is_gain = 1;
5998 rsm->r_is_drain = 0;
5999 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
6000 rsm->r_is_drain = 1;
6001 rsm->r_is_gain = 0;
6002 } else {
6003 rsm->r_is_drain = 0;
6004 rsm->r_is_gain = 0;
6005 }
6006 return;
6007 }
6008 /*
6009 * If we reach here its a retransmission and we need to find it.
6010 */
6011 more:
6012 if (hintrsm && (hintrsm->r_start == seq_out)) {
6013 rsm = hintrsm;
6014 hintrsm = NULL;
6015 } else if (bbr->r_ctl.rc_next) {
6016 /* We have a hint from a previous run */
6017 rsm = bbr->r_ctl.rc_next;
6018 } else {
6019 /* No hints sorry */
6020 rsm = NULL;
6021 }
6022 if ((rsm) && (rsm->r_start == seq_out)) {
6023 /*
6024 * We used rc_next or hintrsm to retransmit, hopefully the
6025 * likely case.
6026 */
6027 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6028 if (len == 0) {
6029 return;
6030 } else {
6031 goto more;
6032 }
6033 }
6034 /* Ok it was not the last pointer go through it the hard way. */
6035 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6036 if (rsm->r_start == seq_out) {
6037 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6038 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
6039 if (len == 0) {
6040 return;
6041 } else {
6042 continue;
6043 }
6044 }
6045 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6046 /* Transmitted within this piece */
6047 /*
6048 * Ok we must split off the front and then let the
6049 * update do the rest
6050 */
6051 nrsm = bbr_alloc_full_limit(bbr);
6052 if (nrsm == NULL) {
6053 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
6054 return;
6055 }
6056 /*
6057 * copy rsm to nrsm and then trim the front of rsm
6058 * to not include this part.
6059 */
6060 bbr_clone_rsm(bbr, nrsm, rsm, seq_out);
6061 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
6062 if (rsm->r_in_tmap) {
6063 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6064 nrsm->r_in_tmap = 1;
6065 }
6066 rsm->r_flags &= (~BBR_HAS_FIN);
6067 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time);
6068 if (len == 0) {
6069 return;
6070 }
6071 }
6072 }
6073 /*
6074 * Hmm not found in map did they retransmit both old and on into the
6075 * new?
6076 */
6077 if (seq_out == tp->snd_max) {
6078 goto again;
6079 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6080 #ifdef BBR_INVARIANTS
6081 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6082 seq_out, len, tp->snd_una, tp->snd_max);
6083 printf("Starting Dump of all rack entries\n");
6084 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6085 printf("rsm:%p start:%u end:%u\n",
6086 rsm, rsm->r_start, rsm->r_end);
6087 }
6088 printf("Dump complete\n");
6089 panic("seq_out not found rack:%p tp:%p",
6090 bbr, tp);
6091 #endif
6092 } else {
6093 #ifdef BBR_INVARIANTS
6094 /*
6095 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6096 * flag)
6097 */
6098 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6099 seq_out, len, tp->snd_max, tp);
6100 #endif
6101 }
6102 }
6103
6104 static void
bbr_collapse_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,int32_t rtt)6105 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt)
6106 {
6107 /*
6108 * Collapse timeout back the cum-ack moved.
6109 */
6110 tp->t_rxtshift = 0;
6111 tp->t_softerror = 0;
6112 }
6113
6114 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)6115 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin)
6116 {
6117 bbr->rtt_valid = 1;
6118 bbr->r_ctl.cur_rtt = rtt_usecs;
6119 bbr->r_ctl.ts_in = tsin;
6120 if (rsm_send_time)
6121 bbr->r_ctl.cur_rtt_send_time = rsm_send_time;
6122 }
6123
6124 static void
bbr_make_timestamp_determination(struct tcp_bbr * bbr)6125 bbr_make_timestamp_determination(struct tcp_bbr *bbr)
6126 {
6127 /**
6128 * We have in our bbr control:
6129 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp).
6130 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts).
6131 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts)
6132 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time)
6133 *
6134 * Now we can calculate the time between the sends by doing:
6135 *
6136 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts
6137 *
6138 * And the peer's time between receiving them by doing:
6139 *
6140 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp
6141 *
6142 * We want to figure out if the timestamp values are in msec, 10msec or usec.
6143 * We also may find that we can't use the timestamps if say we see
6144 * that the peer_delta indicates that though we may have taken 10ms to
6145 * pace out the data, it only saw 1ms between the two packets. This would
6146 * indicate that somewhere on the path is a batching entity that is giving
6147 * out time-slices of the actual b/w. This would mean we could not use
6148 * reliably the peers timestamps.
6149 *
6150 * We expect delta > peer_delta initially. Until we figure out the
6151 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio.
6152 * If we place 1000 there then its a ms vs our usec. If we place 10000 there
6153 * then its 10ms vs our usec. If the peer is running a usec clock we would
6154 * put a 1 there. If the value is faster then ours, we will disable the
6155 * use of timestamps (though we could revist this later if we find it to be not
6156 * just an isolated one or two flows)).
6157 *
6158 * To detect the batching middle boxes we will come up with our compensation and
6159 * if with it in place, we find the peer is drastically off (by some margin) in
6160 * the smaller direction, then we will assume the worst case and disable use of timestamps.
6161 *
6162 */
6163 uint64_t delta, peer_delta, delta_up;
6164
6165 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts;
6166 if (delta < bbr_min_usec_delta) {
6167 /*
6168 * Have not seen a min amount of time
6169 * between our send times so we can
6170 * make a determination of the timestamp
6171 * yet.
6172 */
6173 return;
6174 }
6175 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp;
6176 if (peer_delta < bbr_min_peer_delta) {
6177 /*
6178 * We may have enough in the form of
6179 * our delta but the peers number
6180 * has not changed that much. It could
6181 * be its clock ratio is such that
6182 * we need more data (10ms tick) or
6183 * there may be other compression scenarios
6184 * going on. In any event we need the
6185 * spread to be larger.
6186 */
6187 return;
6188 }
6189 /* Ok lets first see which way our delta is going */
6190 if (peer_delta > delta) {
6191 /* Very unlikely, the peer without
6192 * compensation shows that it saw
6193 * the two sends arrive further apart
6194 * then we saw then in micro-seconds.
6195 */
6196 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) {
6197 /* well it looks like the peer is a micro-second clock. */
6198 bbr->rc_ts_clock_set = 1;
6199 bbr->r_ctl.bbr_peer_tsratio = 1;
6200 } else {
6201 bbr->rc_ts_cant_be_used = 1;
6202 bbr->rc_ts_clock_set = 1;
6203 }
6204 return;
6205 }
6206 /* Ok we know that the peer_delta is smaller than our send distance */
6207 bbr->rc_ts_clock_set = 1;
6208 /* First question is it within the percentage that they are using usec time? */
6209 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent;
6210 if ((peer_delta + delta_up) >= delta) {
6211 /* Its a usec clock */
6212 bbr->r_ctl.bbr_peer_tsratio = 1;
6213 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6214 return;
6215 }
6216 /* Ok if not usec, what about 10usec (though unlikely)? */
6217 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent;
6218 if (((peer_delta * 10) + delta_up) >= delta) {
6219 bbr->r_ctl.bbr_peer_tsratio = 10;
6220 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6221 return;
6222 }
6223 /* And what about 100usec (though again unlikely)? */
6224 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent;
6225 if (((peer_delta * 100) + delta_up) >= delta) {
6226 bbr->r_ctl.bbr_peer_tsratio = 100;
6227 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6228 return;
6229 }
6230 /* And how about 1 msec (the most likely one)? */
6231 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent;
6232 if (((peer_delta * 1000) + delta_up) >= delta) {
6233 bbr->r_ctl.bbr_peer_tsratio = 1000;
6234 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6235 return;
6236 }
6237 /* Ok if not msec could it be 10 msec? */
6238 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent;
6239 if (((peer_delta * 10000) + delta_up) >= delta) {
6240 bbr->r_ctl.bbr_peer_tsratio = 10000;
6241 return;
6242 }
6243 /* If we fall down here the clock tick so slowly we can't use it */
6244 bbr->rc_ts_cant_be_used = 1;
6245 bbr->r_ctl.bbr_peer_tsratio = 0;
6246 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6247 }
6248
6249 /*
6250 * Collect new round-trip time estimate
6251 * and update averages and current timeout.
6252 */
6253 static void
tcp_bbr_xmit_timer_commit(struct tcp_bbr * bbr,struct tcpcb * tp,uint32_t cts)6254 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts)
6255 {
6256 int32_t delta;
6257 uint32_t rtt, tsin;
6258 int32_t rtt_ticks;
6259
6260 if (bbr->rtt_valid == 0)
6261 /* No valid sample */
6262 return;
6263
6264 rtt = bbr->r_ctl.cur_rtt;
6265 tsin = bbr->r_ctl.ts_in;
6266 if (bbr->rc_prtt_set_ts) {
6267 /*
6268 * We are to force feed the rttProp filter due
6269 * to an entry into PROBE_RTT. This assures
6270 * that the times are sync'd between when we
6271 * go into PROBE_RTT and the filter expiration.
6272 *
6273 * Google does not use a true filter, so they do
6274 * this implicitly since they only keep one value
6275 * and when they enter probe-rtt they update the
6276 * value to the newest rtt.
6277 */
6278 uint32_t rtt_prop;
6279
6280 bbr->rc_prtt_set_ts = 0;
6281 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
6282 if (rtt > rtt_prop)
6283 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts);
6284 else
6285 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6286 }
6287 #ifdef STATS
6288 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt));
6289 #endif
6290 if (bbr->rc_ack_was_delayed)
6291 rtt += bbr->r_ctl.rc_ack_hdwr_delay;
6292
6293 if (rtt < bbr->r_ctl.rc_lowest_rtt)
6294 bbr->r_ctl.rc_lowest_rtt = rtt;
6295 bbr_log_rtt_sample(bbr, rtt, tsin);
6296 if (bbr->r_init_rtt) {
6297 /*
6298 * The initial rtt is not-trusted, nuke it and lets get
6299 * our first valid measurement in.
6300 */
6301 bbr->r_init_rtt = 0;
6302 tp->t_srtt = 0;
6303 }
6304 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) {
6305 /*
6306 * So we have not yet figured out
6307 * what the peers TSTMP value is
6308 * in (most likely ms). We need a
6309 * series of cum-ack's to determine
6310 * this reliably.
6311 */
6312 if (bbr->rc_ack_is_cumack) {
6313 if (bbr->rc_ts_data_set) {
6314 /* Lets attempt to determine the timestamp granularity. */
6315 bbr_make_timestamp_determination(bbr);
6316 } else {
6317 bbr->rc_ts_data_set = 1;
6318 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts;
6319 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time;
6320 }
6321 } else {
6322 /*
6323 * We have to have consecutive acks
6324 * reset any "filled" state to none.
6325 */
6326 bbr->rc_ts_data_set = 0;
6327 }
6328 }
6329 /* Round it up */
6330 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1)));
6331 if (rtt_ticks == 0)
6332 rtt_ticks = 1;
6333 if (tp->t_srtt != 0) {
6334 /*
6335 * srtt is stored as fixed point with 5 bits after the
6336 * binary point (i.e., scaled by 8). The following magic is
6337 * equivalent to the smoothing algorithm in rfc793 with an
6338 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6339 * Adjust rtt to origin 0.
6340 */
6341
6342 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT)
6343 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6344
6345 tp->t_srtt += delta;
6346 if (tp->t_srtt <= 0)
6347 tp->t_srtt = 1;
6348
6349 /*
6350 * We accumulate a smoothed rtt variance (actually, a
6351 * smoothed mean difference), then set the retransmit timer
6352 * to smoothed rtt + 4 times the smoothed variance. rttvar
6353 * is stored as fixed point with 4 bits after the binary
6354 * point (scaled by 16). The following is equivalent to
6355 * rfc793 smoothing with an alpha of .75 (rttvar =
6356 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6357 * wired-in beta.
6358 */
6359 if (delta < 0)
6360 delta = -delta;
6361 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6362 tp->t_rttvar += delta;
6363 if (tp->t_rttvar <= 0)
6364 tp->t_rttvar = 1;
6365 } else {
6366 /*
6367 * No rtt measurement yet - use the unsmoothed rtt. Set the
6368 * variance to half the rtt (so our first retransmit happens
6369 * at 3*rtt).
6370 */
6371 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT;
6372 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1);
6373 }
6374 KMOD_TCPSTAT_INC(tcps_rttupdated);
6375 if (tp->t_rttupdated < UCHAR_MAX)
6376 tp->t_rttupdated++;
6377 #ifdef STATS
6378 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks));
6379 #endif
6380 /*
6381 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6382 * way we do the smoothing, srtt and rttvar will each average +1/2
6383 * tick of bias. When we compute the retransmit timer, we want 1/2
6384 * tick of rounding and 1 extra tick because of +-1/2 tick
6385 * uncertainty in the firing of the timer. The bias will give us
6386 * exactly the 1.5 tick we need. But, because the bias is
6387 * statistical, we have to test that we don't drop below the minimum
6388 * feasible timer (which is 2 ticks).
6389 */
6390 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6391 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2),
6392 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
6393
6394 /*
6395 * We received an ack for a packet that wasn't retransmitted; it is
6396 * probably safe to discard any error indications we've received
6397 * recently. This isn't quite right, but close enough for now (a
6398 * route might have failed after we sent a segment, and the return
6399 * path might not be symmetrical).
6400 */
6401 tp->t_softerror = 0;
6402 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
6403 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt)
6404 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt;
6405 }
6406
6407 static void
bbr_set_reduced_rtt(struct tcp_bbr * bbr,uint32_t cts,uint32_t line)6408 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line)
6409 {
6410 bbr->r_ctl.rc_rtt_shrinks = cts;
6411 if (bbr_can_force_probertt &&
6412 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
6413 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
6414 /*
6415 * We should enter probe-rtt its been too long
6416 * since we have been there.
6417 */
6418 bbr_enter_probe_rtt(bbr, cts, __LINE__);
6419 } else
6420 bbr_check_probe_rtt_limits(bbr, cts);
6421 }
6422
6423 static void
tcp_bbr_commit_bw(struct tcp_bbr * bbr,uint32_t cts)6424 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts)
6425 {
6426 uint64_t orig_bw;
6427
6428 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) {
6429 /* We never apply a zero measurement */
6430 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0,
6431 0, 0, 0, 0, 0, 0);
6432 return;
6433 }
6434 if (bbr->r_ctl.r_measurement_count < 0xffffffff)
6435 bbr->r_ctl.r_measurement_count++;
6436 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
6437 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch);
6438 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw,
6439 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate),
6440 0, 0, 0, 0, 0, 0);
6441 if (orig_bw &&
6442 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) {
6443 if (bbr->bbr_hdrw_pacing) {
6444 /*
6445 * Apply a new rate to the hardware
6446 * possibly.
6447 */
6448 bbr_update_hardware_pacing_rate(bbr, cts);
6449 }
6450 bbr_set_state_target(bbr, __LINE__);
6451 tcp_bbr_tso_size_check(bbr, cts);
6452 if (bbr->r_recovery_bw) {
6453 bbr_setup_red_bw(bbr, cts);
6454 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW);
6455 }
6456 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate))
6457 tcp_bbr_tso_size_check(bbr, cts);
6458 }
6459
6460 static void
bbr_nf_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6461 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6462 {
6463 if (bbr->rc_in_persist == 0) {
6464 /* We log only when not in persist */
6465 /* Translate to a Bytes Per Second */
6466 uint64_t tim, bw, ts_diff, ts_bw;
6467 uint32_t delivered;
6468
6469 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6470 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6471 else
6472 tim = 1;
6473 /*
6474 * Now that we have processed the tim (skipping the sample
6475 * or possibly updating the time, go ahead and
6476 * calculate the cdr.
6477 */
6478 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6479 bw = (uint64_t)delivered;
6480 bw *= (uint64_t)USECS_IN_SECOND;
6481 bw /= tim;
6482 if (bw == 0) {
6483 /* We must have a calculatable amount */
6484 return;
6485 }
6486 /*
6487 * If we are using this b/w shove it in now so we
6488 * can see in the trace viewer if it gets over-ridden.
6489 */
6490 if (rsm->r_ts_valid &&
6491 bbr->rc_ts_valid &&
6492 bbr->rc_ts_clock_set &&
6493 (bbr->rc_ts_cant_be_used == 0) &&
6494 bbr->rc_use_ts_limit) {
6495 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1);
6496 ts_diff *= bbr->r_ctl.bbr_peer_tsratio;
6497 if ((delivered == 0) ||
6498 (rtt < 1000)) {
6499 /* Can't use the ts */
6500 bbr_log_type_bbrupd(bbr, 61, cts,
6501 ts_diff,
6502 bbr->r_ctl.last_inbound_ts,
6503 rsm->r_del_ack_ts, 0,
6504 0, 0, 0, delivered);
6505 } else {
6506 ts_bw = (uint64_t)delivered;
6507 ts_bw *= (uint64_t)USECS_IN_SECOND;
6508 ts_bw /= ts_diff;
6509 bbr_log_type_bbrupd(bbr, 62, cts,
6510 (ts_bw >> 32),
6511 (ts_bw & 0xffffffff), 0, 0,
6512 0, 0, ts_diff, delivered);
6513 if ((bbr->ts_can_raise) &&
6514 (ts_bw > bw)) {
6515 bbr_log_type_bbrupd(bbr, 8, cts,
6516 delivered,
6517 ts_diff,
6518 (bw >> 32),
6519 (bw & 0x00000000ffffffff),
6520 0, 0, 0, 0);
6521 bw = ts_bw;
6522 } else if (ts_bw && (ts_bw < bw)) {
6523 bbr_log_type_bbrupd(bbr, 7, cts,
6524 delivered,
6525 ts_diff,
6526 (bw >> 32),
6527 (bw & 0x00000000ffffffff),
6528 0, 0, 0, 0);
6529 bw = ts_bw;
6530 }
6531 }
6532 }
6533 if (rsm->r_first_sent_time &&
6534 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6535 uint64_t sbw, sti;
6536 /*
6537 * We use what was in flight at the time of our
6538 * send and the size of this send to figure
6539 * out what we have been sending at (amount).
6540 * For the time we take from the time of
6541 * the send of the first send outstanding
6542 * until this send plus this sends pacing
6543 * time. This gives us a good calculation
6544 * as to the rate we have been sending at.
6545 */
6546
6547 sbw = (uint64_t)(rsm->r_flight_at_send);
6548 sbw *= (uint64_t)USECS_IN_SECOND;
6549 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6550 sti += rsm->r_pacing_delay;
6551 sbw /= sti;
6552 if (sbw < bw) {
6553 bbr_log_type_bbrupd(bbr, 6, cts,
6554 delivered,
6555 (uint32_t)sti,
6556 (bw >> 32),
6557 (uint32_t)bw,
6558 rsm->r_first_sent_time, 0, (sbw >> 32),
6559 (uint32_t)sbw);
6560 bw = sbw;
6561 }
6562 }
6563 /* Use the google algorithm for b/w measurements */
6564 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6565 if ((rsm->r_app_limited == 0) ||
6566 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) {
6567 tcp_bbr_commit_bw(bbr, cts);
6568 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6569 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6570 }
6571 }
6572 }
6573
6574 static void
bbr_google_measurement(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,uint32_t rtt,uint32_t cts)6575 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6576 {
6577 if (bbr->rc_in_persist == 0) {
6578 /* We log only when not in persist */
6579 /* Translate to a Bytes Per Second */
6580 uint64_t tim, bw;
6581 uint32_t delivered;
6582 int no_apply = 0;
6583
6584 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6585 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6586 else
6587 tim = 1;
6588 /*
6589 * Now that we have processed the tim (skipping the sample
6590 * or possibly updating the time, go ahead and
6591 * calculate the cdr.
6592 */
6593 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6594 bw = (uint64_t)delivered;
6595 bw *= (uint64_t)USECS_IN_SECOND;
6596 bw /= tim;
6597 if (tim < bbr->r_ctl.rc_lowest_rtt) {
6598 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6599 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6600
6601 no_apply = 1;
6602 }
6603 /*
6604 * If we are using this b/w shove it in now so we
6605 * can see in the trace viewer if it gets over-ridden.
6606 */
6607 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6608 /* Gate by the sending rate */
6609 if (rsm->r_first_sent_time &&
6610 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6611 uint64_t sbw, sti;
6612 /*
6613 * We use what was in flight at the time of our
6614 * send and the size of this send to figure
6615 * out what we have been sending at (amount).
6616 * For the time we take from the time of
6617 * the send of the first send outstanding
6618 * until this send plus this sends pacing
6619 * time. This gives us a good calculation
6620 * as to the rate we have been sending at.
6621 */
6622
6623 sbw = (uint64_t)(rsm->r_flight_at_send);
6624 sbw *= (uint64_t)USECS_IN_SECOND;
6625 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6626 sti += rsm->r_pacing_delay;
6627 sbw /= sti;
6628 if (sbw < bw) {
6629 bbr_log_type_bbrupd(bbr, 6, cts,
6630 delivered,
6631 (uint32_t)sti,
6632 (bw >> 32),
6633 (uint32_t)bw,
6634 rsm->r_first_sent_time, 0, (sbw >> 32),
6635 (uint32_t)sbw);
6636 bw = sbw;
6637 }
6638 if ((sti > tim) &&
6639 (sti < bbr->r_ctl.rc_lowest_rtt)) {
6640 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6641 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6642 no_apply = 1;
6643 } else
6644 no_apply = 0;
6645 }
6646 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6647 if ((no_apply == 0) &&
6648 ((rsm->r_app_limited == 0) ||
6649 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) {
6650 tcp_bbr_commit_bw(bbr, cts);
6651 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6652 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6653 }
6654 }
6655 }
6656
6657 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)6658 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin,
6659 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to)
6660 {
6661 uint64_t old_rttprop;
6662
6663 /* Update our delivery time and amount */
6664 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start);
6665 bbr->r_ctl.rc_del_time = cts;
6666 if (rtt == 0) {
6667 /*
6668 * 0 means its a retransmit, for now we don't use these for
6669 * the rest of BBR.
6670 */
6671 return;
6672 }
6673 if ((bbr->rc_use_google == 0) &&
6674 (match != BBR_RTT_BY_EXACTMATCH) &&
6675 (match != BBR_RTT_BY_TIMESTAMP)){
6676 /*
6677 * We get a lot of rtt updates, lets not pay attention to
6678 * any that are not an exact match. That way we don't have
6679 * to worry about timestamps and the whole nonsense of
6680 * unsure if its a retransmission etc (if we ever had the
6681 * timestamp fixed to always have the last thing sent this
6682 * would not be a issue).
6683 */
6684 return;
6685 }
6686 if ((bbr_no_retran && bbr->rc_use_google) &&
6687 (match != BBR_RTT_BY_EXACTMATCH) &&
6688 (match != BBR_RTT_BY_TIMESTAMP)){
6689 /*
6690 * We only do measurements in google mode
6691 * with bbr_no_retran on for sure things.
6692 */
6693 return;
6694 }
6695 /* Only update srtt if we know by exact match */
6696 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin);
6697 if (ack_type == BBR_CUM_ACKED)
6698 bbr->rc_ack_is_cumack = 1;
6699 else
6700 bbr->rc_ack_is_cumack = 0;
6701 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP);
6702 /*
6703 * Note the following code differs to the original
6704 * BBR spec. It calls for <= not <. However after a
6705 * long discussion in email with Neal, he acknowledged
6706 * that it should be < than so that we will have flows
6707 * going into probe-rtt (we were seeing cases where that
6708 * did not happen and caused ugly things to occur). We
6709 * have added this agreed upon fix to our code base.
6710 */
6711 if (rtt < old_rttprop) {
6712 /* Update when we last saw a rtt drop */
6713 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0);
6714 bbr_set_reduced_rtt(bbr, cts, __LINE__);
6715 }
6716 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts,
6717 match, rsm->r_start, rsm->r_flags);
6718 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6719 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) {
6720 /*
6721 * The RTT-prop moved, reset the target (may be a
6722 * nop for some states).
6723 */
6724 bbr_set_state_target(bbr, __LINE__);
6725 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)
6726 bbr_log_rtt_shrinks(bbr, cts, 0, 0,
6727 __LINE__, BBR_RTTS_NEW_TARGET, 0);
6728 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP))
6729 /* It went up */
6730 bbr_check_probe_rtt_limits(bbr, cts);
6731 }
6732 if ((bbr->rc_use_google == 0) &&
6733 (match == BBR_RTT_BY_TIMESTAMP)) {
6734 /*
6735 * We don't do b/w update with
6736 * these since they are not really
6737 * reliable.
6738 */
6739 return;
6740 }
6741 if (bbr->r_ctl.r_app_limited_until &&
6742 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) {
6743 /* We are no longer app-limited */
6744 bbr->r_ctl.r_app_limited_until = 0;
6745 }
6746 if (bbr->rc_use_google) {
6747 bbr_google_measurement(bbr, rsm, rtt, cts);
6748 } else {
6749 bbr_nf_measurement(bbr, rsm, rtt, cts);
6750 }
6751 }
6752
6753 /*
6754 * Convert a timestamp that the main stack
6755 * uses (milliseconds) into one that bbr uses
6756 * (microseconds). Return that converted timestamp.
6757 */
6758 static uint32_t
bbr_ts_convert(uint32_t cts)6759 bbr_ts_convert(uint32_t cts) {
6760 uint32_t sec, msec;
6761
6762 sec = cts / MS_IN_USEC;
6763 msec = cts - (MS_IN_USEC * sec);
6764 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC));
6765 }
6766
6767 /*
6768 * Return 0 if we did not update the RTT time, return
6769 * 1 if we did.
6770 */
6771 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)6772 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr,
6773 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack)
6774 {
6775 int32_t i;
6776 uint32_t t, uts = 0;
6777
6778 if ((rsm->r_flags & BBR_ACKED) ||
6779 (rsm->r_flags & BBR_WAS_RENEGED) ||
6780 (rsm->r_flags & BBR_RXT_CLEARED)) {
6781 /* Already done */
6782 return (0);
6783 }
6784 if (rsm->r_rtt_not_allowed) {
6785 /* Not allowed */
6786 return (0);
6787 }
6788 if (rsm->r_rtr_cnt == 1) {
6789 /*
6790 * Only one transmit. Hopefully the normal case.
6791 */
6792 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0]))
6793 t = cts - rsm->r_tim_lastsent[0];
6794 else
6795 t = 1;
6796 if ((int)t <= 0)
6797 t = 1;
6798 bbr->r_ctl.rc_last_rtt = t;
6799 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6800 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to);
6801 return (1);
6802 }
6803 /* Convert to usecs */
6804 if ((bbr_can_use_ts_for_rtt == 1) &&
6805 (bbr->rc_use_google == 1) &&
6806 (ack_type == BBR_CUM_ACKED) &&
6807 (to->to_flags & TOF_TS) &&
6808 (to->to_tsecr != 0)) {
6809 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr;
6810 if (t < 1)
6811 t = 1;
6812 t *= MS_IN_USEC;
6813 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6814 BBR_RTT_BY_TIMESTAMP,
6815 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)],
6816 ack_type, to);
6817 return (1);
6818 }
6819 uts = bbr_ts_convert(to->to_tsecr);
6820 if ((to->to_flags & TOF_TS) &&
6821 (to->to_tsecr != 0) &&
6822 (ack_type == BBR_CUM_ACKED) &&
6823 ((rsm->r_flags & BBR_OVERMAX) == 0)) {
6824 /*
6825 * Now which timestamp does it match? In this block the ACK
6826 * may be coming from a previous transmission.
6827 */
6828 uint32_t fudge;
6829
6830 fudge = BBR_TIMER_FUDGE;
6831 for (i = 0; i < rsm->r_rtr_cnt; i++) {
6832 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) &&
6833 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) {
6834 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6835 t = cts - rsm->r_tim_lastsent[i];
6836 else
6837 t = 1;
6838 if ((int)t <= 0)
6839 t = 1;
6840 bbr->r_ctl.rc_last_rtt = t;
6841 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING,
6842 rsm->r_tim_lastsent[i], ack_type, to);
6843 if ((i + 1) < rsm->r_rtr_cnt) {
6844 /* Likely */
6845 return (0);
6846 } else if (rsm->r_flags & BBR_TLP) {
6847 bbr->rc_tlp_rtx_out = 0;
6848 }
6849 return (1);
6850 }
6851 }
6852 /* Fall through if we can't find a matching timestamp */
6853 }
6854 /*
6855 * Ok its a SACK block that we retransmitted. or a windows
6856 * machine without timestamps. We can tell nothing from the
6857 * time-stamp since its not there or the time the peer last
6858 * received a segment that moved forward its cum-ack point.
6859 *
6860 * Lets look at the last retransmit and see what we can tell
6861 * (with BBR for space we only keep 2 note we have to keep
6862 * at least 2 so the map can not be condensed more).
6863 */
6864 i = rsm->r_rtr_cnt - 1;
6865 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6866 t = cts - rsm->r_tim_lastsent[i];
6867 else
6868 goto not_sure;
6869 if (t < bbr->r_ctl.rc_lowest_rtt) {
6870 /*
6871 * We retransmitted and the ack came back in less
6872 * than the smallest rtt we have observed in the
6873 * windowed rtt. We most likey did an improper
6874 * retransmit as outlined in 4.2 Step 3 point 2 in
6875 * the rack-draft.
6876 *
6877 * Use the prior transmission to update all the
6878 * information as long as there is only one prior
6879 * transmission.
6880 */
6881 if ((rsm->r_flags & BBR_OVERMAX) == 0) {
6882 #ifdef BBR_INVARIANTS
6883 if (rsm->r_rtr_cnt == 1)
6884 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags);
6885 #endif
6886 i = rsm->r_rtr_cnt - 2;
6887 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6888 t = cts - rsm->r_tim_lastsent[i];
6889 else
6890 t = 1;
6891 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET,
6892 rsm->r_tim_lastsent[i], ack_type, to);
6893 return (0);
6894 } else {
6895 /*
6896 * Too many prior transmissions, just
6897 * updated BBR delivered
6898 */
6899 not_sure:
6900 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
6901 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
6902 }
6903 } else {
6904 /*
6905 * We retransmitted it and the retransmit did the
6906 * job.
6907 */
6908 if (rsm->r_flags & BBR_TLP)
6909 bbr->rc_tlp_rtx_out = 0;
6910 if ((rsm->r_flags & BBR_OVERMAX) == 0)
6911 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts,
6912 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to);
6913 else
6914 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
6915 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
6916 return (1);
6917 }
6918 return (0);
6919 }
6920
6921 /*
6922 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
6923 */
6924 static void
bbr_log_sack_passed(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm)6925 bbr_log_sack_passed(struct tcpcb *tp,
6926 struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
6927 {
6928 struct bbr_sendmap *nrsm;
6929
6930 nrsm = rsm;
6931 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap,
6932 bbr_head, r_tnext) {
6933 if (nrsm == rsm) {
6934 /* Skip original segment he is acked */
6935 continue;
6936 }
6937 if (nrsm->r_flags & BBR_ACKED) {
6938 /* Skip ack'd segments */
6939 continue;
6940 }
6941 if (nrsm->r_flags & BBR_SACK_PASSED) {
6942 /*
6943 * We found one that is already marked
6944 * passed, we have been here before and
6945 * so all others below this are marked.
6946 */
6947 break;
6948 }
6949 BBR_STAT_INC(bbr_sack_passed);
6950 nrsm->r_flags |= BBR_SACK_PASSED;
6951 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) &&
6952 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) {
6953 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start;
6954 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start;
6955 nrsm->r_flags |= BBR_MARKED_LOST;
6956 }
6957 nrsm->r_flags &= ~BBR_WAS_SACKPASS;
6958 }
6959 }
6960
6961 /*
6962 * Returns the number of bytes that were
6963 * newly ack'd by sack blocks.
6964 */
6965 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)6966 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack,
6967 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts)
6968 {
6969 int32_t times = 0;
6970 uint32_t start, end, changed = 0;
6971 struct bbr_sendmap *rsm, *nrsm;
6972 int32_t used_ref = 1;
6973 uint8_t went_back = 0, went_fwd = 0;
6974
6975 start = sack->start;
6976 end = sack->end;
6977 rsm = *prsm;
6978 if (rsm == NULL)
6979 used_ref = 0;
6980
6981 /* Do we locate the block behind where we last were? */
6982 if (rsm && SEQ_LT(start, rsm->r_start)) {
6983 went_back = 1;
6984 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
6985 if (SEQ_GEQ(start, rsm->r_start) &&
6986 SEQ_LT(start, rsm->r_end)) {
6987 goto do_rest_ofb;
6988 }
6989 }
6990 }
6991 start_at_beginning:
6992 went_fwd = 1;
6993 /*
6994 * Ok lets locate the block where this guy is fwd from rsm (if its
6995 * set)
6996 */
6997 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) {
6998 if (SEQ_GEQ(start, rsm->r_start) &&
6999 SEQ_LT(start, rsm->r_end)) {
7000 break;
7001 }
7002 }
7003 do_rest_ofb:
7004 if (rsm == NULL) {
7005 /*
7006 * This happens when we get duplicate sack blocks with the
7007 * same end. For example SACK 4: 100 SACK 3: 100 The sort
7008 * will not change there location so we would just start at
7009 * the end of the first one and get lost.
7010 */
7011 if (tp->t_flags & TF_SENTFIN) {
7012 /*
7013 * Check to see if we have not logged the FIN that
7014 * went out.
7015 */
7016 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7017 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
7018 /*
7019 * Ok we did not get the FIN logged.
7020 */
7021 nrsm->r_end++;
7022 rsm = nrsm;
7023 goto do_rest_ofb;
7024 }
7025 }
7026 if (times == 1) {
7027 #ifdef BBR_INVARIANTS
7028 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p",
7029 tp, bbr, sack, to, prsm);
7030 #else
7031 goto out;
7032 #endif
7033 }
7034 times++;
7035 BBR_STAT_INC(bbr_sack_proc_restart);
7036 rsm = NULL;
7037 goto start_at_beginning;
7038 }
7039 /* Ok we have an ACK for some piece of rsm */
7040 if (rsm->r_start != start) {
7041 /*
7042 * Need to split this in two pieces the before and after.
7043 */
7044 if (bbr_sack_mergable(rsm, start, end))
7045 nrsm = bbr_alloc_full_limit(bbr);
7046 else
7047 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7048 if (nrsm == NULL) {
7049 /* We could not allocate ignore the sack */
7050 struct sackblk blk;
7051
7052 blk.start = start;
7053 blk.end = end;
7054 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7055 goto out;
7056 }
7057 bbr_clone_rsm(bbr, nrsm, rsm, start);
7058 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7059 if (rsm->r_in_tmap) {
7060 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7061 nrsm->r_in_tmap = 1;
7062 }
7063 rsm->r_flags &= (~BBR_HAS_FIN);
7064 rsm = nrsm;
7065 }
7066 if (SEQ_GEQ(end, rsm->r_end)) {
7067 /*
7068 * The end of this block is either beyond this guy or right
7069 * at this guy.
7070 */
7071 if ((rsm->r_flags & BBR_ACKED) == 0) {
7072 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7073 changed += (rsm->r_end - rsm->r_start);
7074 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7075 bbr_log_sack_passed(tp, bbr, rsm);
7076 if (rsm->r_flags & BBR_MARKED_LOST) {
7077 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7078 }
7079 /* Is Reordering occuring? */
7080 if (rsm->r_flags & BBR_SACK_PASSED) {
7081 BBR_STAT_INC(bbr_reorder_seen);
7082 bbr->r_ctl.rc_reorder_ts = cts;
7083 if (rsm->r_flags & BBR_MARKED_LOST) {
7084 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7085 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7086 /* LT sampling also needs adjustment */
7087 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7088 }
7089 }
7090 rsm->r_flags |= BBR_ACKED;
7091 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7092 if (rsm->r_in_tmap) {
7093 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7094 rsm->r_in_tmap = 0;
7095 }
7096 }
7097 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7098 if (end == rsm->r_end) {
7099 /* This block only - done */
7100 goto out;
7101 }
7102 /* There is more not coverend by this rsm move on */
7103 start = rsm->r_end;
7104 nrsm = TAILQ_NEXT(rsm, r_next);
7105 rsm = nrsm;
7106 times = 0;
7107 goto do_rest_ofb;
7108 }
7109 if (rsm->r_flags & BBR_ACKED) {
7110 /* Been here done that */
7111 goto out;
7112 }
7113 /* Ok we need to split off this one at the tail */
7114 if (bbr_sack_mergable(rsm, start, end))
7115 nrsm = bbr_alloc_full_limit(bbr);
7116 else
7117 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7118 if (nrsm == NULL) {
7119 /* failed XXXrrs what can we do but loose the sack info? */
7120 struct sackblk blk;
7121
7122 blk.start = start;
7123 blk.end = end;
7124 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7125 goto out;
7126 }
7127 /* Clone it */
7128 bbr_clone_rsm(bbr, nrsm, rsm, end);
7129 /* The sack block does not cover this guy fully */
7130 rsm->r_flags &= (~BBR_HAS_FIN);
7131 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7132 if (rsm->r_in_tmap) {
7133 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7134 nrsm->r_in_tmap = 1;
7135 }
7136 nrsm->r_dupack = 0;
7137 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7138 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7139 changed += (rsm->r_end - rsm->r_start);
7140 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7141 bbr_log_sack_passed(tp, bbr, rsm);
7142 /* Is Reordering occuring? */
7143 if (rsm->r_flags & BBR_MARKED_LOST) {
7144 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7145 }
7146 if (rsm->r_flags & BBR_SACK_PASSED) {
7147 BBR_STAT_INC(bbr_reorder_seen);
7148 bbr->r_ctl.rc_reorder_ts = cts;
7149 if (rsm->r_flags & BBR_MARKED_LOST) {
7150 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7151 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7152 /* LT sampling also needs adjustment */
7153 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7154 }
7155 }
7156 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7157 rsm->r_flags |= BBR_ACKED;
7158 if (rsm->r_in_tmap) {
7159 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7160 rsm->r_in_tmap = 0;
7161 }
7162 out:
7163 if (rsm && (rsm->r_flags & BBR_ACKED)) {
7164 /*
7165 * Now can we merge this newly acked
7166 * block with either the previous or
7167 * next block?
7168 */
7169 nrsm = TAILQ_NEXT(rsm, r_next);
7170 if (nrsm &&
7171 (nrsm->r_flags & BBR_ACKED)) {
7172 /* yep this and next can be merged */
7173 rsm = bbr_merge_rsm(bbr, rsm, nrsm);
7174 }
7175 /* Now what about the previous? */
7176 nrsm = TAILQ_PREV(rsm, bbr_head, r_next);
7177 if (nrsm &&
7178 (nrsm->r_flags & BBR_ACKED)) {
7179 /* yep the previous and this can be merged */
7180 rsm = bbr_merge_rsm(bbr, nrsm, rsm);
7181 }
7182 }
7183 if (used_ref == 0) {
7184 BBR_STAT_INC(bbr_sack_proc_all);
7185 } else {
7186 BBR_STAT_INC(bbr_sack_proc_short);
7187 }
7188 if (went_fwd && went_back) {
7189 BBR_STAT_INC(bbr_sack_search_both);
7190 } else if (went_fwd) {
7191 BBR_STAT_INC(bbr_sack_search_fwd);
7192 } else if (went_back) {
7193 BBR_STAT_INC(bbr_sack_search_back);
7194 }
7195 /* Save off where the next seq is */
7196 if (rsm)
7197 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
7198 else
7199 bbr->r_ctl.rc_sacklast = NULL;
7200 *prsm = rsm;
7201 return (changed);
7202 }
7203
7204 static void inline
bbr_peer_reneges(struct tcp_bbr * bbr,struct bbr_sendmap * rsm,tcp_seq th_ack)7205 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack)
7206 {
7207 struct bbr_sendmap *tmap;
7208
7209 BBR_STAT_INC(bbr_reneges_seen);
7210 tmap = NULL;
7211 while (rsm && (rsm->r_flags & BBR_ACKED)) {
7212 /* Its no longer sacked, mark it so */
7213 uint32_t oflags;
7214 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7215 #ifdef BBR_INVARIANTS
7216 if (rsm->r_in_tmap) {
7217 panic("bbr:%p rsm:%p flags:0x%x in tmap?",
7218 bbr, rsm, rsm->r_flags);
7219 }
7220 #endif
7221 oflags = rsm->r_flags;
7222 if (rsm->r_flags & BBR_MARKED_LOST) {
7223 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7224 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7225 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7226 /* LT sampling also needs adjustment */
7227 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7228 }
7229 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST);
7230 rsm->r_flags |= BBR_WAS_RENEGED;
7231 rsm->r_flags |= BBR_RXT_CLEARED;
7232 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__);
7233 /* Rebuild it into our tmap */
7234 if (tmap == NULL) {
7235 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7236 tmap = rsm;
7237 } else {
7238 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7239 tmap = rsm;
7240 }
7241 tmap->r_in_tmap = 1;
7242 /*
7243 * XXXrrs Delivered? Should we do anything here?
7244 *
7245 * Of course we don't on a rxt timeout so maybe its ok that
7246 * we don't?
7247 *
7248 * For now lets not.
7249 */
7250 rsm = TAILQ_NEXT(rsm, r_next);
7251 }
7252 /*
7253 * Now lets possibly clear the sack filter so we start recognizing
7254 * sacks that cover this area.
7255 */
7256 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack);
7257 }
7258
7259 static void
bbr_log_syn(struct tcpcb * tp,struct tcpopt * to)7260 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to)
7261 {
7262 struct tcp_bbr *bbr;
7263 struct bbr_sendmap *rsm;
7264 uint32_t cts;
7265
7266 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7267 cts = bbr->r_ctl.rc_rcvtime;
7268 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7269 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) {
7270 if ((rsm->r_end - rsm->r_start) <= 1) {
7271 /* Log out the SYN completely */
7272 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7273 rsm->r_rtr_bytes = 0;
7274 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7275 if (rsm->r_in_tmap) {
7276 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7277 rsm->r_in_tmap = 0;
7278 }
7279 if (bbr->r_ctl.rc_next == rsm) {
7280 /* scoot along the marker */
7281 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7282 }
7283 if (to != NULL)
7284 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0);
7285 bbr_free(bbr, rsm);
7286 } else {
7287 /* There is more (Fast open)? strip out SYN. */
7288 rsm->r_flags &= ~BBR_HAS_SYN;
7289 rsm->r_start++;
7290 }
7291 }
7292 }
7293
7294 /*
7295 * Returns the number of bytes that were
7296 * acknowledged by SACK blocks.
7297 */
7298
7299 static uint32_t
bbr_log_ack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th,uint32_t * prev_acked)7300 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
7301 uint32_t *prev_acked)
7302 {
7303 uint32_t changed, last_seq, entered_recovery = 0;
7304 struct tcp_bbr *bbr;
7305 struct bbr_sendmap *rsm;
7306 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7307 register uint32_t th_ack;
7308 int32_t i, j, k, new_sb, num_sack_blks = 0;
7309 uint32_t cts, acked, ack_point, sack_changed = 0;
7310 uint32_t p_maxseg, maxseg, p_acked = 0;
7311
7312 INP_WLOCK_ASSERT(tptoinpcb(tp));
7313 if (tcp_get_flags(th) & TH_RST) {
7314 /* We don't log resets */
7315 return (0);
7316 }
7317 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7318 cts = bbr->r_ctl.rc_rcvtime;
7319
7320 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7321 changed = 0;
7322 maxseg = tp->t_maxseg - bbr->rc_last_options;
7323 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg);
7324 th_ack = th->th_ack;
7325 if (SEQ_GT(th_ack, tp->snd_una)) {
7326 acked = th_ack - tp->snd_una;
7327 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__);
7328 bbr->rc_tp->t_acktime = ticks;
7329 } else
7330 acked = 0;
7331 if (SEQ_LEQ(th_ack, tp->snd_una)) {
7332 /* Only sent here for sack processing */
7333 goto proc_sack;
7334 }
7335 if (rsm && SEQ_GT(th_ack, rsm->r_start)) {
7336 changed = th_ack - rsm->r_start;
7337 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) {
7338 /*
7339 * For the SYN incoming case we will not have called
7340 * tcp_output for the sending of the SYN, so there will be
7341 * no map. All other cases should probably be a panic.
7342 */
7343 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) {
7344 /*
7345 * We have a timestamp that can be used to generate
7346 * an initial RTT.
7347 */
7348 uint32_t ts, now, rtt;
7349
7350 ts = bbr_ts_convert(to->to_tsecr);
7351 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv));
7352 rtt = now - ts;
7353 if (rtt < 1)
7354 rtt = 1;
7355 bbr_log_type_bbrrttprop(bbr, rtt,
7356 tp->iss, 0, cts,
7357 BBR_RTT_BY_TIMESTAMP, tp->iss, 0);
7358 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
7359 changed = 1;
7360 bbr->r_wanted_output = 1;
7361 goto out;
7362 }
7363 goto proc_sack;
7364 } else if (rsm == NULL) {
7365 goto out;
7366 }
7367 if (changed) {
7368 /*
7369 * The ACK point is advancing to th_ack, we must drop off
7370 * the packets in the rack log and calculate any eligble
7371 * RTT's.
7372 */
7373 bbr->r_wanted_output = 1;
7374 more:
7375 if (rsm == NULL) {
7376 if (tp->t_flags & TF_SENTFIN) {
7377 /* if we send a FIN we will not hav a map */
7378 goto proc_sack;
7379 }
7380 #ifdef BBR_INVARIANTS
7381 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n",
7382 tp,
7383 th, tp->t_state, bbr,
7384 tp->snd_una, tp->snd_max, changed);
7385 #endif
7386 goto proc_sack;
7387 }
7388 }
7389 if (SEQ_LT(th_ack, rsm->r_start)) {
7390 /* Huh map is missing this */
7391 #ifdef BBR_INVARIANTS
7392 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n",
7393 rsm->r_start,
7394 th_ack, tp->t_state,
7395 bbr->r_state, bbr);
7396 panic("th-ack is bad bbr:%p tp:%p", bbr, tp);
7397 #endif
7398 goto proc_sack;
7399 } else if (th_ack == rsm->r_start) {
7400 /* None here to ack */
7401 goto proc_sack;
7402 }
7403 /*
7404 * Clear the dup ack counter, it will
7405 * either be freed or if there is some
7406 * remaining we need to start it at zero.
7407 */
7408 rsm->r_dupack = 0;
7409 /* Now do we consume the whole thing? */
7410 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7411 /* Its all consumed. */
7412 uint32_t left;
7413
7414 if (rsm->r_flags & BBR_ACKED) {
7415 /*
7416 * It was acked on the scoreboard -- remove it from
7417 * total
7418 */
7419 p_acked += (rsm->r_end - rsm->r_start);
7420 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7421 if (bbr->r_ctl.rc_sacked == 0)
7422 bbr->r_ctl.rc_sacklast = NULL;
7423 } else {
7424 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack);
7425 if (rsm->r_flags & BBR_MARKED_LOST) {
7426 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7427 }
7428 if (rsm->r_flags & BBR_SACK_PASSED) {
7429 /*
7430 * There are acked segments ACKED on the
7431 * scoreboard further up. We are seeing
7432 * reordering.
7433 */
7434 BBR_STAT_INC(bbr_reorder_seen);
7435 bbr->r_ctl.rc_reorder_ts = cts;
7436 if (rsm->r_flags & BBR_MARKED_LOST) {
7437 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7438 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7439 /* LT sampling also needs adjustment */
7440 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7441 }
7442 }
7443 rsm->r_flags &= ~BBR_MARKED_LOST;
7444 }
7445 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7446 rsm->r_rtr_bytes = 0;
7447 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7448 if (rsm->r_in_tmap) {
7449 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7450 rsm->r_in_tmap = 0;
7451 }
7452 if (bbr->r_ctl.rc_next == rsm) {
7453 /* scoot along the marker */
7454 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7455 }
7456 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7457 /* Adjust the packet counts */
7458 left = th_ack - rsm->r_end;
7459 /* Free back to zone */
7460 bbr_free(bbr, rsm);
7461 if (left) {
7462 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7463 goto more;
7464 }
7465 goto proc_sack;
7466 }
7467 if (rsm->r_flags & BBR_ACKED) {
7468 /*
7469 * It was acked on the scoreboard -- remove it from total
7470 * for the part being cum-acked.
7471 */
7472 p_acked += (rsm->r_end - rsm->r_start);
7473 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7474 if (bbr->r_ctl.rc_sacked == 0)
7475 bbr->r_ctl.rc_sacklast = NULL;
7476 } else {
7477 /*
7478 * It was acked up to th_ack point for the first time
7479 */
7480 struct bbr_sendmap lrsm;
7481
7482 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap));
7483 lrsm.r_end = th_ack;
7484 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack);
7485 }
7486 if ((rsm->r_flags & BBR_MARKED_LOST) &&
7487 ((rsm->r_flags & BBR_ACKED) == 0)) {
7488 /*
7489 * It was marked lost and partly ack'd now
7490 * for the first time. We lower the rc_lost_bytes
7491 * and still leave it MARKED.
7492 */
7493 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start;
7494 }
7495 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7496 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7497 rsm->r_rtr_bytes = 0;
7498 /* adjust packet count */
7499 rsm->r_start = th_ack;
7500 proc_sack:
7501 /* Check for reneging */
7502 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7503 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) {
7504 /*
7505 * The peer has moved snd_una up to the edge of this send,
7506 * i.e. one that it had previously acked. The only way that
7507 * can be true if the peer threw away data (space issues)
7508 * that it had previously sacked (else it would have given
7509 * us snd_una up to (rsm->r_end). We need to undo the acked
7510 * markings here.
7511 *
7512 * Note we have to look to make sure th_ack is our
7513 * rsm->r_start in case we get an old ack where th_ack is
7514 * behind snd_una.
7515 */
7516 bbr_peer_reneges(bbr, rsm, th->th_ack);
7517 }
7518 if ((to->to_flags & TOF_SACK) == 0) {
7519 /* We are done nothing left to log */
7520 goto out;
7521 }
7522 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7523 if (rsm) {
7524 last_seq = rsm->r_end;
7525 } else {
7526 last_seq = tp->snd_max;
7527 }
7528 /* Sack block processing */
7529 if (SEQ_GT(th_ack, tp->snd_una))
7530 ack_point = th_ack;
7531 else
7532 ack_point = tp->snd_una;
7533 for (i = 0; i < to->to_nsacks; i++) {
7534 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7535 &sack, sizeof(sack));
7536 sack.start = ntohl(sack.start);
7537 sack.end = ntohl(sack.end);
7538 if (SEQ_GT(sack.end, sack.start) &&
7539 SEQ_GT(sack.start, ack_point) &&
7540 SEQ_LT(sack.start, tp->snd_max) &&
7541 SEQ_GT(sack.end, ack_point) &&
7542 SEQ_LEQ(sack.end, tp->snd_max)) {
7543 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) &&
7544 (SEQ_LT(sack.end, last_seq)) &&
7545 ((sack.end - sack.start) < (p_maxseg / 8))) {
7546 /*
7547 * Not the last piece and its smaller than
7548 * 1/8th of a p_maxseg. We ignore this.
7549 */
7550 BBR_STAT_INC(bbr_runt_sacks);
7551 continue;
7552 }
7553 sack_blocks[num_sack_blks] = sack;
7554 num_sack_blks++;
7555 } else if (SEQ_LEQ(sack.start, th_ack) &&
7556 SEQ_LEQ(sack.end, th_ack)) {
7557 /*
7558 * Its a D-SACK block.
7559 */
7560 tcp_record_dsack(tp, sack.start, sack.end, 0);
7561 }
7562 }
7563 if (num_sack_blks == 0)
7564 goto out;
7565 /*
7566 * Sort the SACK blocks so we can update the rack scoreboard with
7567 * just one pass.
7568 */
7569 new_sb = sack_filter_blks(tp, &bbr->r_ctl.bbr_sf, sack_blocks,
7570 num_sack_blks, th->th_ack);
7571 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks);
7572 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks);
7573 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb));
7574 num_sack_blks = new_sb;
7575 if (num_sack_blks < 2) {
7576 goto do_sack_work;
7577 }
7578 /* Sort the sacks */
7579 for (i = 0; i < num_sack_blks; i++) {
7580 for (j = i + 1; j < num_sack_blks; j++) {
7581 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7582 sack = sack_blocks[i];
7583 sack_blocks[i] = sack_blocks[j];
7584 sack_blocks[j] = sack;
7585 }
7586 }
7587 }
7588 /*
7589 * Now are any of the sack block ends the same (yes some
7590 * implememtations send these)?
7591 */
7592 again:
7593 if (num_sack_blks > 1) {
7594 for (i = 0; i < num_sack_blks; i++) {
7595 for (j = i + 1; j < num_sack_blks; j++) {
7596 if (sack_blocks[i].end == sack_blocks[j].end) {
7597 /*
7598 * Ok these two have the same end we
7599 * want the smallest end and then
7600 * throw away the larger and start
7601 * again.
7602 */
7603 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7604 /*
7605 * The second block covers
7606 * more area use that
7607 */
7608 sack_blocks[i].start = sack_blocks[j].start;
7609 }
7610 /*
7611 * Now collapse out the dup-sack and
7612 * lower the count
7613 */
7614 for (k = (j + 1); k < num_sack_blks; k++) {
7615 sack_blocks[j].start = sack_blocks[k].start;
7616 sack_blocks[j].end = sack_blocks[k].end;
7617 j++;
7618 }
7619 num_sack_blks--;
7620 goto again;
7621 }
7622 }
7623 }
7624 }
7625 do_sack_work:
7626 rsm = bbr->r_ctl.rc_sacklast;
7627 for (i = 0; i < num_sack_blks; i++) {
7628 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts);
7629 if (acked) {
7630 bbr->r_wanted_output = 1;
7631 changed += acked;
7632 sack_changed += acked;
7633 }
7634 }
7635 out:
7636 *prev_acked = p_acked;
7637 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
7638 /*
7639 * Ok we have a high probability that we need to go in to
7640 * recovery since we have data sack'd
7641 */
7642 struct bbr_sendmap *rsm;
7643
7644 rsm = bbr_check_recovery_mode(tp, bbr, cts);
7645 if (rsm) {
7646 /* Enter recovery */
7647 entered_recovery = 1;
7648 bbr->r_wanted_output = 1;
7649 /*
7650 * When we enter recovery we need to assure we send
7651 * one packet.
7652 */
7653 if (bbr->r_ctl.rc_resend == NULL) {
7654 bbr->r_ctl.rc_resend = rsm;
7655 }
7656 }
7657 }
7658 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
7659 /*
7660 * See if we need to rack-retransmit anything if so set it
7661 * up as the thing to resend assuming something else is not
7662 * already in that position.
7663 */
7664 if (bbr->r_ctl.rc_resend == NULL) {
7665 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
7666 }
7667 }
7668 /*
7669 * We return the amount that changed via sack, this is used by the
7670 * ack-received code to augment what was changed between th_ack <->
7671 * snd_una.
7672 */
7673 return (sack_changed);
7674 }
7675
7676 static void
bbr_strike_dupack(struct tcp_bbr * bbr)7677 bbr_strike_dupack(struct tcp_bbr *bbr)
7678 {
7679 struct bbr_sendmap *rsm;
7680
7681 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
7682 if (rsm && (rsm->r_dupack < 0xff)) {
7683 rsm->r_dupack++;
7684 if (rsm->r_dupack >= DUP_ACK_THRESHOLD)
7685 bbr->r_wanted_output = 1;
7686 }
7687 }
7688
7689 /*
7690 * Return value of 1, we do not need to call bbr_process_data().
7691 * return value of 0, bbr_process_data can be called.
7692 * For ret_val if its 0 the TCB is locked and valid, if its non-zero
7693 * its unlocked and probably unsafe to touch the TCB.
7694 */
7695 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)7696 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
7697 struct tcpcb *tp, struct tcpopt *to,
7698 uint32_t tiwin, int32_t tlen,
7699 int32_t * ofia, int32_t thflags, int32_t * ret_val)
7700 {
7701 int32_t ourfinisacked = 0;
7702 int32_t acked_amount;
7703 uint16_t nsegs;
7704 int32_t acked;
7705 uint32_t lost, sack_changed = 0;
7706 struct mbuf *mfree;
7707 struct tcp_bbr *bbr;
7708 uint32_t prev_acked = 0;
7709
7710 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7711 lost = bbr->r_ctl.rc_lost;
7712 nsegs = max(1, m->m_pkthdr.lro_nsegs);
7713 if (SEQ_GT(th->th_ack, tp->snd_max)) {
7714 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7715 bbr->r_wanted_output = 1;
7716 return (1);
7717 }
7718 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
7719 /* Process the ack */
7720 if (bbr->rc_in_persist)
7721 tp->t_rxtshift = 0;
7722 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
7723 bbr_strike_dupack(bbr);
7724 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
7725 }
7726 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost));
7727 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
7728 /*
7729 * Old ack, behind the last one rcv'd or a duplicate ack
7730 * with SACK info.
7731 */
7732 if (th->th_ack == tp->snd_una) {
7733 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0);
7734 if (bbr->r_state == TCPS_SYN_SENT) {
7735 /*
7736 * Special case on where we sent SYN. When
7737 * the SYN-ACK is processed in syn_sent
7738 * state it bumps the snd_una. This causes
7739 * us to hit here even though we did ack 1
7740 * byte.
7741 *
7742 * Go through the nothing left case so we
7743 * send data.
7744 */
7745 goto nothing_left;
7746 }
7747 }
7748 return (0);
7749 }
7750 /*
7751 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
7752 * something we sent.
7753 */
7754 if (tp->t_flags & TF_NEEDSYN) {
7755 /*
7756 * T/TCP: Connection was half-synchronized, and our SYN has
7757 * been ACK'd (so connection is now fully synchronized). Go
7758 * to non-starred state, increment snd_una for ACK of SYN,
7759 * and check if we can do window scaling.
7760 */
7761 tp->t_flags &= ~TF_NEEDSYN;
7762 tp->snd_una++;
7763 /* Do window scaling? */
7764 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
7765 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
7766 tp->rcv_scale = tp->request_r_scale;
7767 /* Send window already scaled. */
7768 }
7769 }
7770 INP_WLOCK_ASSERT(tptoinpcb(tp));
7771
7772 acked = BYTES_THIS_ACK(tp, th);
7773 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
7774 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
7775
7776 /*
7777 * If we just performed our first retransmit, and the ACK arrives
7778 * within our recovery window, then it was a mistake to do the
7779 * retransmit in the first place. Recover our original cwnd and
7780 * ssthresh, and proceed to transmit where we left off.
7781 */
7782 if (tp->t_flags & TF_PREVVALID) {
7783 tp->t_flags &= ~TF_PREVVALID;
7784 if (tp->t_rxtshift == 1 &&
7785 (int)(ticks - tp->t_badrxtwin) < 0)
7786 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
7787 }
7788 SOCKBUF_LOCK(&so->so_snd);
7789 acked_amount = min(acked, (int)sbavail(&so->so_snd));
7790 tp->snd_wnd -= acked_amount;
7791 mfree = sbcut_locked(&so->so_snd, acked_amount);
7792 /* NB: sowwakeup_locked() does an implicit unlock. */
7793 sowwakeup_locked(so);
7794 m_freem(mfree);
7795 if (SEQ_GT(th->th_ack, tp->snd_una)) {
7796 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
7797 }
7798 tp->snd_una = th->th_ack;
7799 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost));
7800 if (IN_RECOVERY(tp->t_flags)) {
7801 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
7802 (SEQ_LT(th->th_ack, tp->snd_max))) {
7803 tcp_bbr_partialack(tp);
7804 } else {
7805 bbr_post_recovery(tp);
7806 }
7807 }
7808 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
7809 tp->snd_recover = tp->snd_una;
7810 }
7811 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
7812 tp->snd_nxt = tp->snd_max;
7813 }
7814 if (tp->snd_una == tp->snd_max) {
7815 /* Nothing left outstanding */
7816 nothing_left:
7817 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
7818 if (sbavail(&so->so_snd) == 0)
7819 bbr->rc_tp->t_acktime = 0;
7820 if ((sbused(&so->so_snd) == 0) &&
7821 (tp->t_flags & TF_SENTFIN)) {
7822 ourfinisacked = 1;
7823 }
7824 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
7825 if (bbr->rc_in_persist == 0) {
7826 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
7827 }
7828 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
7829 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
7830 /*
7831 * We invalidate the last ack here since we
7832 * don't want to transfer forward the time
7833 * for our sum's calculations.
7834 */
7835 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7836 (sbavail(&so->so_snd) == 0) &&
7837 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
7838 /*
7839 * The socket was gone and the peer sent data, time
7840 * to reset him.
7841 */
7842 *ret_val = 1;
7843 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
7844 /* tcp_close will kill the inp pre-log the Reset */
7845 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
7846 tp = tcp_close(tp);
7847 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
7848 BBR_STAT_INC(bbr_dropped_af_data);
7849 return (1);
7850 }
7851 /* Set need output so persist might get set */
7852 bbr->r_wanted_output = 1;
7853 }
7854 if (ofia)
7855 *ofia = ourfinisacked;
7856 return (0);
7857 }
7858
7859 static void
bbr_enter_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)7860 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
7861 {
7862 if (bbr->rc_in_persist == 0) {
7863 bbr_timer_cancel(bbr, __LINE__, cts);
7864 bbr->r_ctl.rc_last_delay_val = 0;
7865 tp->t_rxtshift = 0;
7866 bbr->rc_in_persist = 1;
7867 bbr->r_ctl.rc_went_idle_time = cts;
7868 /* We should be capped when rw went to 0 but just in case */
7869 bbr_log_type_pesist(bbr, cts, 0, line, 1);
7870 /* Time freezes for the state, so do the accounting now */
7871 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
7872 uint32_t time_in;
7873
7874 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
7875 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
7876 int32_t idx;
7877
7878 idx = bbr_state_val(bbr);
7879 counter_u64_add(bbr_state_time[(idx + 5)], time_in);
7880 } else {
7881 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
7882 }
7883 }
7884 bbr->r_ctl.rc_bbr_state_time = cts;
7885 }
7886 }
7887
7888 static void
bbr_restart_after_idle(struct tcp_bbr * bbr,uint32_t cts,uint32_t idle_time)7889 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time)
7890 {
7891 /*
7892 * Note that if idle time does not exceed our
7893 * threshold, we do nothing continuing the state
7894 * transitions we were last walking through.
7895 */
7896 if (idle_time >= bbr_idle_restart_threshold) {
7897 if (bbr->rc_use_idle_restart) {
7898 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT;
7899 /*
7900 * Set our target using BBR_UNIT, so
7901 * we increase at a dramatic rate but
7902 * we stop when we get the pipe
7903 * full again for our current b/w estimate.
7904 */
7905 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
7906 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
7907 bbr_set_state_target(bbr, __LINE__);
7908 /* Now setup our gains to ramp up */
7909 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
7910 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
7911 bbr_log_type_statechange(bbr, cts, __LINE__);
7912 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
7913 bbr_substate_change(bbr, cts, __LINE__, 1);
7914 }
7915 }
7916 }
7917
7918 static void
bbr_exit_persist(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts,int32_t line)7919 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
7920 {
7921 uint32_t idle_time;
7922
7923 if (bbr->rc_in_persist == 0)
7924 return;
7925 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time);
7926 bbr->rc_in_persist = 0;
7927 bbr->rc_hit_state_1 = 0;
7928 bbr->r_ctl.rc_del_time = cts;
7929 /*
7930 * We invalidate the last ack here since we
7931 * don't want to transfer forward the time
7932 * for our sum's calculations.
7933 */
7934 if (tcp_in_hpts(bbr->rc_tp)) {
7935 tcp_hpts_remove(bbr->rc_tp);
7936 bbr->rc_timer_first = 0;
7937 bbr->r_ctl.rc_hpts_flags = 0;
7938 bbr->r_ctl.rc_last_delay_val = 0;
7939 bbr->r_ctl.rc_hptsi_agg_delay = 0;
7940 bbr->r_agg_early_set = 0;
7941 bbr->r_ctl.rc_agg_early = 0;
7942 }
7943 bbr_log_type_pesist(bbr, cts, idle_time, line, 0);
7944 if (idle_time >= bbr_rtt_probe_time) {
7945 /*
7946 * This qualifies as a RTT_PROBE session since we drop the
7947 * data outstanding to nothing and waited more than
7948 * bbr_rtt_probe_time.
7949 */
7950 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0);
7951 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts;
7952 }
7953 tp->t_rxtshift = 0;
7954 /*
7955 * If in probeBW and we have persisted more than an RTT lets do
7956 * special handling.
7957 */
7958 /* Force a time based epoch */
7959 bbr_set_epoch(bbr, cts, __LINE__);
7960 /*
7961 * Setup the lost so we don't count anything against the guy
7962 * we have been stuck with during persists.
7963 */
7964 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
7965 /* Time un-freezes for the state */
7966 bbr->r_ctl.rc_bbr_state_time = cts;
7967 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) ||
7968 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) {
7969 /*
7970 * If we are going back to probe-bw
7971 * or probe_rtt, we may need to possibly
7972 * do a fast restart.
7973 */
7974 bbr_restart_after_idle(bbr, cts, idle_time);
7975 }
7976 }
7977
7978 static void
bbr_collapsed_window(struct tcp_bbr * bbr)7979 bbr_collapsed_window(struct tcp_bbr *bbr)
7980 {
7981 /*
7982 * Now we must walk the
7983 * send map and divide the
7984 * ones left stranded. These
7985 * guys can't cause us to abort
7986 * the connection and are really
7987 * "unsent". However if a buggy
7988 * client actually did keep some
7989 * of the data i.e. collapsed the win
7990 * and refused to ack and then opened
7991 * the win and acked that data. We would
7992 * get into an ack war, the simplier
7993 * method then of just pretending we
7994 * did not send those segments something
7995 * won't work.
7996 */
7997 struct bbr_sendmap *rsm, *nrsm;
7998 tcp_seq max_seq;
7999 uint32_t maxseg;
8000 int can_split = 0;
8001 int fnd = 0;
8002
8003 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
8004 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd;
8005 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0);
8006 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
8007 /* Find the first seq past or at maxseq */
8008 if (rsm->r_flags & BBR_RWND_COLLAPSED)
8009 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8010 if (SEQ_GEQ(max_seq, rsm->r_start) &&
8011 SEQ_GEQ(rsm->r_end, max_seq)) {
8012 fnd = 1;
8013 break;
8014 }
8015 }
8016 bbr->rc_has_collapsed = 0;
8017 if (!fnd) {
8018 /* Nothing to do strange */
8019 return;
8020 }
8021 /*
8022 * Now can we split?
8023 *
8024 * We don't want to split if splitting
8025 * would generate too many small segments
8026 * less we let an attacker fragment our
8027 * send_map and leave us out of memory.
8028 */
8029 if ((max_seq != rsm->r_start) &&
8030 (max_seq != rsm->r_end)){
8031 /* can we split? */
8032 int res1, res2;
8033
8034 res1 = max_seq - rsm->r_start;
8035 res2 = rsm->r_end - max_seq;
8036 if ((res1 >= (maxseg/8)) &&
8037 (res2 >= (maxseg/8))) {
8038 /* No small pieces here */
8039 can_split = 1;
8040 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) {
8041 /* We are under the limit */
8042 can_split = 1;
8043 }
8044 }
8045 /* Ok do we need to split this rsm? */
8046 if (max_seq == rsm->r_start) {
8047 /* It's this guy no split required */
8048 nrsm = rsm;
8049 } else if (max_seq == rsm->r_end) {
8050 /* It's the next one no split required. */
8051 nrsm = TAILQ_NEXT(rsm, r_next);
8052 if (nrsm == NULL) {
8053 /* Huh? */
8054 return;
8055 }
8056 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) {
8057 /* yep we need to split it */
8058 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
8059 if (nrsm == NULL) {
8060 /* failed XXXrrs what can we do mark the whole? */
8061 nrsm = rsm;
8062 goto no_split;
8063 }
8064 /* Clone it */
8065 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0);
8066 bbr_clone_rsm(bbr, nrsm, rsm, max_seq);
8067 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
8068 if (rsm->r_in_tmap) {
8069 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8070 nrsm->r_in_tmap = 1;
8071 }
8072 } else {
8073 /*
8074 * Split not allowed just start here just
8075 * use this guy.
8076 */
8077 nrsm = rsm;
8078 }
8079 no_split:
8080 BBR_STAT_INC(bbr_collapsed_win);
8081 /* reuse fnd as a count */
8082 fnd = 0;
8083 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) {
8084 nrsm->r_flags |= BBR_RWND_COLLAPSED;
8085 fnd++;
8086 bbr->rc_has_collapsed = 1;
8087 }
8088 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd);
8089 }
8090
8091 static void
bbr_un_collapse_window(struct tcp_bbr * bbr)8092 bbr_un_collapse_window(struct tcp_bbr *bbr)
8093 {
8094 struct bbr_sendmap *rsm;
8095 int cleared = 0;
8096
8097 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
8098 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
8099 /* Clear the flag */
8100 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8101 cleared++;
8102 } else
8103 break;
8104 }
8105 bbr_log_type_rwnd_collapse(bbr,
8106 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared);
8107 bbr->rc_has_collapsed = 0;
8108 }
8109
8110 /*
8111 * Return value of 1, the TCB is unlocked and most
8112 * likely gone, return value of 0, the TCB is still
8113 * locked.
8114 */
8115 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)8116 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8117 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8118 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8119 {
8120 /*
8121 * Update window information. Don't look at window if no ACK: TAC's
8122 * send garbage on first SYN.
8123 */
8124 uint16_t nsegs;
8125 int32_t tfo_syn;
8126 struct tcp_bbr *bbr;
8127
8128 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8129 INP_WLOCK_ASSERT(tptoinpcb(tp));
8130 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8131 if ((thflags & TH_ACK) &&
8132 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8133 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8134 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8135 /* keep track of pure window updates */
8136 if (tlen == 0 &&
8137 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8138 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8139 tp->snd_wnd = tiwin;
8140 tp->snd_wl1 = th->th_seq;
8141 tp->snd_wl2 = th->th_ack;
8142 if (tp->snd_wnd > tp->max_sndwnd)
8143 tp->max_sndwnd = tp->snd_wnd;
8144 bbr->r_wanted_output = 1;
8145 } else if (thflags & TH_ACK) {
8146 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8147 tp->snd_wnd = tiwin;
8148 tp->snd_wl1 = th->th_seq;
8149 tp->snd_wl2 = th->th_ack;
8150 }
8151 }
8152 if (tp->snd_wnd < ctf_outstanding(tp))
8153 /* The peer collapsed its window on us */
8154 bbr_collapsed_window(bbr);
8155 else if (bbr->rc_has_collapsed)
8156 bbr_un_collapse_window(bbr);
8157 /* Was persist timer active and now we have window space? */
8158 if ((bbr->rc_in_persist != 0) &&
8159 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8160 bbr_minseg(bbr)))) {
8161 /*
8162 * Make the rate persist at end of persist mode if idle long
8163 * enough
8164 */
8165 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8166
8167 /* Make sure we output to start the timer */
8168 bbr->r_wanted_output = 1;
8169 }
8170 /* Do we need to enter persist? */
8171 if ((bbr->rc_in_persist == 0) &&
8172 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8173 TCPS_HAVEESTABLISHED(tp->t_state) &&
8174 (tp->snd_max == tp->snd_una) &&
8175 sbavail(&so->so_snd) &&
8176 (sbavail(&so->so_snd) > tp->snd_wnd)) {
8177 /* No send window.. we must enter persist */
8178 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8179 }
8180 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8181 m_freem(m);
8182 return (0);
8183 }
8184 /*
8185 * We don't support urgent data but
8186 * drag along the up just to make sure
8187 * if there is a stack switch no one
8188 * is surprised.
8189 */
8190 tp->rcv_up = tp->rcv_nxt;
8191
8192 /*
8193 * Process the segment text, merging it into the TCP sequencing
8194 * queue, and arranging for acknowledgment of receipt if necessary.
8195 * This process logically involves adjusting tp->rcv_wnd as data is
8196 * presented to the user (this happens in tcp_usrreq.c, case
8197 * PRU_RCVD). If a FIN has already been received on this connection
8198 * then we just ignore the text.
8199 */
8200 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8201 (tp->t_flags & TF_FASTOPEN));
8202 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
8203 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8204 tcp_seq save_start = th->th_seq;
8205 tcp_seq save_rnxt = tp->rcv_nxt;
8206 int save_tlen = tlen;
8207
8208 m_adj(m, drop_hdrlen); /* delayed header drop */
8209 /*
8210 * Insert segment which includes th into TCP reassembly
8211 * queue with control block tp. Set thflags to whether
8212 * reassembly now includes a segment with FIN. This handles
8213 * the common case inline (segment is the next to be
8214 * received on an established connection, and the queue is
8215 * empty), avoiding linkage into and removal from the queue
8216 * and repetition of various conversions. Set DELACK for
8217 * segments received in order, but ack immediately when
8218 * segments are out of order (so fast retransmit can work).
8219 */
8220 if (th->th_seq == tp->rcv_nxt &&
8221 SEGQ_EMPTY(tp) &&
8222 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8223 tfo_syn)) {
8224 #ifdef NETFLIX_SB_LIMITS
8225 u_int mcnt, appended;
8226
8227 if (so->so_rcv.sb_shlim) {
8228 mcnt = m_memcnt(m);
8229 appended = 0;
8230 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8231 CFO_NOSLEEP, NULL) == false) {
8232 counter_u64_add(tcp_sb_shlim_fails, 1);
8233 m_freem(m);
8234 return (0);
8235 }
8236 }
8237
8238 #endif
8239 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) {
8240 bbr->bbr_segs_rcvd += max(1, nsegs);
8241 tp->t_flags |= TF_DELACK;
8242 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8243 } else {
8244 bbr->r_wanted_output = 1;
8245 tp->t_flags |= TF_ACKNOW;
8246 }
8247 tp->rcv_nxt += tlen;
8248 if (tlen &&
8249 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8250 (tp->t_fbyte_in == 0)) {
8251 tp->t_fbyte_in = ticks;
8252 if (tp->t_fbyte_in == 0)
8253 tp->t_fbyte_in = 1;
8254 if (tp->t_fbyte_out && tp->t_fbyte_in)
8255 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8256 }
8257 thflags = tcp_get_flags(th) & TH_FIN;
8258 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8259 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8260 SOCKBUF_LOCK(&so->so_rcv);
8261 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
8262 m_freem(m);
8263 else
8264 #ifdef NETFLIX_SB_LIMITS
8265 appended =
8266 #endif
8267 sbappendstream_locked(&so->so_rcv, m, 0);
8268 /* NB: sorwakeup_locked() does an implicit unlock. */
8269 sorwakeup_locked(so);
8270 #ifdef NETFLIX_SB_LIMITS
8271 if (so->so_rcv.sb_shlim && appended != mcnt)
8272 counter_fo_release(so->so_rcv.sb_shlim,
8273 mcnt - appended);
8274 #endif
8275
8276 } else {
8277 /*
8278 * XXX: Due to the header drop above "th" is
8279 * theoretically invalid by now. Fortunately
8280 * m_adj() doesn't actually frees any mbufs when
8281 * trimming from the head.
8282 */
8283 tcp_seq temp = save_start;
8284
8285 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8286 tp->t_flags |= TF_ACKNOW;
8287 if (tp->t_flags & TF_WAKESOR) {
8288 tp->t_flags &= ~TF_WAKESOR;
8289 /* NB: sorwakeup_locked() does an implicit unlock. */
8290 sorwakeup_locked(so);
8291 }
8292 }
8293 if ((tp->t_flags & TF_SACK_PERMIT) &&
8294 (save_tlen > 0) &&
8295 TCPS_HAVEESTABLISHED(tp->t_state)) {
8296 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8297 /*
8298 * DSACK actually handled in the fastpath
8299 * above.
8300 */
8301 tcp_update_sack_list(tp, save_start,
8302 save_start + save_tlen);
8303 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8304 if ((tp->rcv_numsacks >= 1) &&
8305 (tp->sackblks[0].end == save_start)) {
8306 /*
8307 * Partial overlap, recorded at todrop
8308 * above.
8309 */
8310 tcp_update_sack_list(tp,
8311 tp->sackblks[0].start,
8312 tp->sackblks[0].end);
8313 } else {
8314 tcp_update_dsack_list(tp, save_start,
8315 save_start + save_tlen);
8316 }
8317 } else if (tlen >= save_tlen) {
8318 /* Update of sackblks. */
8319 tcp_update_dsack_list(tp, save_start,
8320 save_start + save_tlen);
8321 } else if (tlen > 0) {
8322 tcp_update_dsack_list(tp, save_start,
8323 save_start + tlen);
8324 }
8325 }
8326 } else {
8327 m_freem(m);
8328 thflags &= ~TH_FIN;
8329 }
8330
8331 /*
8332 * If FIN is received ACK the FIN and let the user know that the
8333 * connection is closing.
8334 */
8335 if (thflags & TH_FIN) {
8336 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8337 /* The socket upcall is handled by socantrcvmore. */
8338 socantrcvmore(so);
8339 /*
8340 * If connection is half-synchronized (ie NEEDSYN
8341 * flag on) then delay ACK, so it may be piggybacked
8342 * when SYN is sent. Otherwise, since we received a
8343 * FIN then no more input can be expected, send ACK
8344 * now.
8345 */
8346 if (tp->t_flags & TF_NEEDSYN) {
8347 tp->t_flags |= TF_DELACK;
8348 bbr_timer_cancel(bbr,
8349 __LINE__, bbr->r_ctl.rc_rcvtime);
8350 } else {
8351 tp->t_flags |= TF_ACKNOW;
8352 }
8353 tp->rcv_nxt++;
8354 }
8355 switch (tp->t_state) {
8356 /*
8357 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8358 * CLOSE_WAIT state.
8359 */
8360 case TCPS_SYN_RECEIVED:
8361 tp->t_starttime = ticks;
8362 /* FALLTHROUGH */
8363 case TCPS_ESTABLISHED:
8364 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8365 break;
8366
8367 /*
8368 * If still in FIN_WAIT_1 STATE FIN has not been
8369 * acked so enter the CLOSING state.
8370 */
8371 case TCPS_FIN_WAIT_1:
8372 tcp_state_change(tp, TCPS_CLOSING);
8373 break;
8374
8375 /*
8376 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8377 * starting the time-wait timer, turning off the
8378 * other standard timers.
8379 */
8380 case TCPS_FIN_WAIT_2:
8381 bbr->rc_timer_first = 1;
8382 bbr_timer_cancel(bbr,
8383 __LINE__, bbr->r_ctl.rc_rcvtime);
8384 tcp_twstart(tp);
8385 return (1);
8386 }
8387 }
8388 /*
8389 * Return any desired output.
8390 */
8391 if ((tp->t_flags & TF_ACKNOW) ||
8392 (sbavail(&so->so_snd) > ctf_outstanding(tp))) {
8393 bbr->r_wanted_output = 1;
8394 }
8395 return (0);
8396 }
8397
8398 /*
8399 * Here nothing is really faster, its just that we
8400 * have broken out the fast-data path also just like
8401 * the fast-ack. Return 1 if we processed the packet
8402 * return 0 if you need to take the "slow-path".
8403 */
8404 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)8405 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8406 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8407 uint32_t tiwin, int32_t nxt_pkt)
8408 {
8409 uint16_t nsegs;
8410 int32_t newsize = 0; /* automatic sockbuf scaling */
8411 struct tcp_bbr *bbr;
8412 #ifdef NETFLIX_SB_LIMITS
8413 u_int mcnt, appended;
8414 #endif
8415
8416 /* On the hpts and we would have called output */
8417 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8418
8419 /*
8420 * If last ACK falls within this segment's sequence numbers, record
8421 * the timestamp. NOTE that the test is modified according to the
8422 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8423 */
8424 if (bbr->r_ctl.rc_resend != NULL) {
8425 return (0);
8426 }
8427 if (tiwin && tiwin != tp->snd_wnd) {
8428 return (0);
8429 }
8430 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8431 return (0);
8432 }
8433 if (__predict_false((to->to_flags & TOF_TS) &&
8434 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8435 return (0);
8436 }
8437 if (__predict_false((th->th_ack != tp->snd_una))) {
8438 return (0);
8439 }
8440 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8441 return (0);
8442 }
8443 if ((to->to_flags & TOF_TS) != 0 &&
8444 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8445 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
8446 tp->ts_recent = to->to_tsval;
8447 }
8448 /*
8449 * This is a pure, in-sequence data packet with nothing on the
8450 * reassembly queue and we have enough buffer space to take it.
8451 */
8452 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8453
8454 #ifdef NETFLIX_SB_LIMITS
8455 if (so->so_rcv.sb_shlim) {
8456 mcnt = m_memcnt(m);
8457 appended = 0;
8458 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8459 CFO_NOSLEEP, NULL) == false) {
8460 counter_u64_add(tcp_sb_shlim_fails, 1);
8461 m_freem(m);
8462 return (1);
8463 }
8464 }
8465 #endif
8466 /* Clean receiver SACK report if present */
8467 if (tp->rcv_numsacks)
8468 tcp_clean_sackreport(tp);
8469 KMOD_TCPSTAT_INC(tcps_preddat);
8470 tp->rcv_nxt += tlen;
8471 if (tlen &&
8472 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8473 (tp->t_fbyte_in == 0)) {
8474 tp->t_fbyte_in = ticks;
8475 if (tp->t_fbyte_in == 0)
8476 tp->t_fbyte_in = 1;
8477 if (tp->t_fbyte_out && tp->t_fbyte_in)
8478 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8479 }
8480 /*
8481 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8482 */
8483 tp->snd_wl1 = th->th_seq;
8484 /*
8485 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8486 */
8487 tp->rcv_up = tp->rcv_nxt;
8488 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8489 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8490 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8491
8492 /* Add data to socket buffer. */
8493 SOCKBUF_LOCK(&so->so_rcv);
8494 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8495 m_freem(m);
8496 } else {
8497 /*
8498 * Set new socket buffer size. Give up when limit is
8499 * reached.
8500 */
8501 if (newsize)
8502 if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
8503 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8504 m_adj(m, drop_hdrlen); /* delayed header drop */
8505
8506 #ifdef NETFLIX_SB_LIMITS
8507 appended =
8508 #endif
8509 sbappendstream_locked(&so->so_rcv, m, 0);
8510 ctf_calc_rwin(so, tp);
8511 }
8512 /* NB: sorwakeup_locked() does an implicit unlock. */
8513 sorwakeup_locked(so);
8514 #ifdef NETFLIX_SB_LIMITS
8515 if (so->so_rcv.sb_shlim && mcnt != appended)
8516 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8517 #endif
8518 if (DELAY_ACK(tp, bbr, nsegs)) {
8519 bbr->bbr_segs_rcvd += max(1, nsegs);
8520 tp->t_flags |= TF_DELACK;
8521 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8522 } else {
8523 bbr->r_wanted_output = 1;
8524 tp->t_flags |= TF_ACKNOW;
8525 }
8526 return (1);
8527 }
8528
8529 /*
8530 * This subfunction is used to try to highly optimize the
8531 * fast path. We again allow window updates that are
8532 * in sequence to remain in the fast-path. We also add
8533 * in the __predict's to attempt to help the compiler.
8534 * Note that if we return a 0, then we can *not* process
8535 * it and the caller should push the packet into the
8536 * slow-path. If we return 1, then all is well and
8537 * the packet is fully processed.
8538 */
8539 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)8540 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8541 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8542 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
8543 {
8544 int32_t acked;
8545 uint16_t nsegs;
8546 uint32_t sack_changed;
8547 uint32_t prev_acked = 0;
8548 struct tcp_bbr *bbr;
8549
8550 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8551 /* Old ack, behind (or duplicate to) the last one rcv'd */
8552 return (0);
8553 }
8554 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8555 /* Above what we have sent? */
8556 return (0);
8557 }
8558 if (__predict_false(tiwin == 0)) {
8559 /* zero window */
8560 return (0);
8561 }
8562 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8563 /* We need a SYN or a FIN, unlikely.. */
8564 return (0);
8565 }
8566 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8567 /* Timestamp is behind .. old ack with seq wrap? */
8568 return (0);
8569 }
8570 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8571 /* Still recovering */
8572 return (0);
8573 }
8574 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8575 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) {
8576 /* We are retransmitting */
8577 return (0);
8578 }
8579 if (__predict_false(bbr->rc_in_persist != 0)) {
8580 /* In persist mode */
8581 return (0);
8582 }
8583 if (bbr->r_ctl.rc_sacked) {
8584 /* We have sack holes on our scoreboard */
8585 return (0);
8586 }
8587 /* Ok if we reach here, we can process a fast-ack */
8588 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8589 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
8590 /*
8591 * We never detect loss in fast ack [we can't
8592 * have a sack and can't be in recovery so
8593 * we always pass 0 (nothing detected)].
8594 */
8595 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0);
8596 /* Did the window get updated? */
8597 if (tiwin != tp->snd_wnd) {
8598 tp->snd_wnd = tiwin;
8599 tp->snd_wl1 = th->th_seq;
8600 if (tp->snd_wnd > tp->max_sndwnd)
8601 tp->max_sndwnd = tp->snd_wnd;
8602 }
8603 /* Do we need to exit persists? */
8604 if ((bbr->rc_in_persist != 0) &&
8605 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8606 bbr_minseg(bbr)))) {
8607 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8608 bbr->r_wanted_output = 1;
8609 }
8610 /* Do we need to enter persists? */
8611 if ((bbr->rc_in_persist == 0) &&
8612 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8613 TCPS_HAVEESTABLISHED(tp->t_state) &&
8614 (tp->snd_max == tp->snd_una) &&
8615 sbavail(&so->so_snd) &&
8616 (sbavail(&so->so_snd) > tp->snd_wnd)) {
8617 /* No send window.. we must enter persist */
8618 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8619 }
8620 /*
8621 * If last ACK falls within this segment's sequence numbers, record
8622 * the timestamp. NOTE that the test is modified according to the
8623 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8624 */
8625 if ((to->to_flags & TOF_TS) != 0 &&
8626 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8627 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime;
8628 tp->ts_recent = to->to_tsval;
8629 }
8630 /*
8631 * This is a pure ack for outstanding data.
8632 */
8633 KMOD_TCPSTAT_INC(tcps_predack);
8634
8635 /*
8636 * "bad retransmit" recovery.
8637 */
8638 if (tp->t_flags & TF_PREVVALID) {
8639 tp->t_flags &= ~TF_PREVVALID;
8640 if (tp->t_rxtshift == 1 &&
8641 (int)(ticks - tp->t_badrxtwin) < 0)
8642 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
8643 }
8644 /*
8645 * Recalculate the transmit timer / rtt.
8646 *
8647 * Some boxes send broken timestamp replies during the SYN+ACK
8648 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8649 * and blow up the retransmit timer.
8650 */
8651 acked = BYTES_THIS_ACK(tp, th);
8652
8653 #ifdef TCP_HHOOK
8654 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
8655 hhook_run_tcp_est_in(tp, th, to);
8656 #endif
8657
8658 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
8659 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8660 sbdrop(&so->so_snd, acked);
8661
8662 if (SEQ_GT(th->th_ack, tp->snd_una))
8663 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8664 tp->snd_una = th->th_ack;
8665 if (tp->snd_wnd < ctf_outstanding(tp))
8666 /* The peer collapsed its window on us */
8667 bbr_collapsed_window(bbr);
8668 else if (bbr->rc_has_collapsed)
8669 bbr_un_collapse_window(bbr);
8670
8671 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8672 tp->snd_recover = tp->snd_una;
8673 }
8674 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0);
8675 /*
8676 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
8677 */
8678 tp->snd_wl2 = th->th_ack;
8679 m_freem(m);
8680 /*
8681 * If all outstanding data are acked, stop retransmit timer,
8682 * otherwise restart timer using current (possibly backed-off)
8683 * value. If process is waiting for space, wakeup/selwakeup/signal.
8684 * If data are ready to send, let tcp_output decide between more
8685 * output or persist.
8686 * Wake up the socket if we have room to write more.
8687 */
8688 sowwakeup(so);
8689 if (tp->snd_una == tp->snd_max) {
8690 /* Nothing left outstanding */
8691 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8692 if (sbavail(&so->so_snd) == 0)
8693 bbr->rc_tp->t_acktime = 0;
8694 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8695 if (bbr->rc_in_persist == 0) {
8696 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8697 }
8698 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8699 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8700 /*
8701 * We invalidate the last ack here since we
8702 * don't want to transfer forward the time
8703 * for our sum's calculations.
8704 */
8705 bbr->r_wanted_output = 1;
8706 }
8707 if (sbavail(&so->so_snd)) {
8708 bbr->r_wanted_output = 1;
8709 }
8710 return (1);
8711 }
8712
8713 /*
8714 * Return value of 1, the TCB is unlocked and most
8715 * likely gone, return value of 0, the TCB is still
8716 * locked.
8717 */
8718 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)8719 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
8720 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8721 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8722 {
8723 int32_t todrop;
8724 int32_t ourfinisacked = 0;
8725 struct tcp_bbr *bbr;
8726 int32_t ret_val = 0;
8727
8728 INP_WLOCK_ASSERT(tptoinpcb(tp));
8729
8730 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8731 ctf_calc_rwin(so, tp);
8732 /*
8733 * If the state is SYN_SENT: if seg contains an ACK, but not for our
8734 * SYN, drop the input. if seg contains a RST, then drop the
8735 * connection. if seg does not contain SYN, then drop it. Otherwise
8736 * this is an acceptable SYN segment initialize tp->rcv_nxt and
8737 * tp->irs if seg contains ack then advance tp->snd_una. BRR does
8738 * not support ECN so we will not say we are capable. if SYN has
8739 * been acked change to ESTABLISHED else SYN_RCVD state arrange for
8740 * segment to be acked (eventually) continue processing rest of
8741 * data/controls, beginning with URG
8742 */
8743 if ((thflags & TH_ACK) &&
8744 (SEQ_LEQ(th->th_ack, tp->iss) ||
8745 SEQ_GT(th->th_ack, tp->snd_max))) {
8746 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8747 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8748 return (1);
8749 }
8750 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
8751 TCP_PROBE5(connect__refused, NULL, tp,
8752 mtod(m, const char *), tp, th);
8753 tp = tcp_drop(tp, ECONNREFUSED);
8754 ctf_do_drop(m, tp);
8755 return (1);
8756 }
8757 if (thflags & TH_RST) {
8758 ctf_do_drop(m, tp);
8759 return (1);
8760 }
8761 if (!(thflags & TH_SYN)) {
8762 ctf_do_drop(m, tp);
8763 return (1);
8764 }
8765 tp->irs = th->th_seq;
8766 tcp_rcvseqinit(tp);
8767 if (thflags & TH_ACK) {
8768 int tfo_partial = 0;
8769
8770 KMOD_TCPSTAT_INC(tcps_connects);
8771 soisconnected(so);
8772 #ifdef MAC
8773 mac_socketpeer_set_from_mbuf(m, so);
8774 #endif
8775 /* Do window scaling on this connection? */
8776 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
8777 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
8778 tp->rcv_scale = tp->request_r_scale;
8779 }
8780 tp->rcv_adv += min(tp->rcv_wnd,
8781 TCP_MAXWIN << tp->rcv_scale);
8782 /*
8783 * If not all the data that was sent in the TFO SYN
8784 * has been acked, resend the remainder right away.
8785 */
8786 if ((tp->t_flags & TF_FASTOPEN) &&
8787 (tp->snd_una != tp->snd_max)) {
8788 tp->snd_nxt = th->th_ack;
8789 tfo_partial = 1;
8790 }
8791 /*
8792 * If there's data, delay ACK; if there's also a FIN ACKNOW
8793 * will be turned on later.
8794 */
8795 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) {
8796 bbr->bbr_segs_rcvd += 1;
8797 tp->t_flags |= TF_DELACK;
8798 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8799 } else {
8800 bbr->r_wanted_output = 1;
8801 tp->t_flags |= TF_ACKNOW;
8802 }
8803 if (SEQ_GT(th->th_ack, tp->iss)) {
8804 /*
8805 * The SYN is acked
8806 * handle it specially.
8807 */
8808 bbr_log_syn(tp, to);
8809 }
8810 if (SEQ_GT(th->th_ack, tp->snd_una)) {
8811 /*
8812 * We advance snd_una for the
8813 * fast open case. If th_ack is
8814 * acknowledging data beyond
8815 * snd_una we can't just call
8816 * ack-processing since the
8817 * data stream in our send-map
8818 * will start at snd_una + 1 (one
8819 * beyond the SYN). If its just
8820 * equal we don't need to do that
8821 * and there is no send_map.
8822 */
8823 tp->snd_una++;
8824 }
8825 /*
8826 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
8827 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
8828 */
8829 tp->t_starttime = ticks;
8830 if (tp->t_flags & TF_NEEDFIN) {
8831 tcp_state_change(tp, TCPS_FIN_WAIT_1);
8832 tp->t_flags &= ~TF_NEEDFIN;
8833 thflags &= ~TH_SYN;
8834 } else {
8835 tcp_state_change(tp, TCPS_ESTABLISHED);
8836 TCP_PROBE5(connect__established, NULL, tp,
8837 mtod(m, const char *), tp, th);
8838 cc_conn_init(tp);
8839 }
8840 } else {
8841 /*
8842 * Received initial SYN in SYN-SENT[*] state => simultaneous
8843 * open. If segment contains CC option and there is a
8844 * cached CC, apply TAO test. If it succeeds, connection is *
8845 * half-synchronized. Otherwise, do 3-way handshake:
8846 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
8847 * there was no CC option, clear cached CC value.
8848 */
8849 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
8850 tcp_state_change(tp, TCPS_SYN_RECEIVED);
8851 }
8852 /*
8853 * Advance th->th_seq to correspond to first data byte. If data,
8854 * trim to stay within window, dropping FIN if necessary.
8855 */
8856 th->th_seq++;
8857 if (tlen > tp->rcv_wnd) {
8858 todrop = tlen - tp->rcv_wnd;
8859 m_adj(m, -todrop);
8860 tlen = tp->rcv_wnd;
8861 thflags &= ~TH_FIN;
8862 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
8863 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
8864 }
8865 tp->snd_wl1 = th->th_seq - 1;
8866 tp->rcv_up = th->th_seq;
8867 /*
8868 * Client side of transaction: already sent SYN and data. If the
8869 * remote host used T/TCP to validate the SYN, our data will be
8870 * ACK'd; if so, enter normal data segment processing in the middle
8871 * of step 5, ack processing. Otherwise, goto step 6.
8872 */
8873 if (thflags & TH_ACK) {
8874 if ((to->to_flags & TOF_TS) != 0) {
8875 uint32_t t, rtt;
8876
8877 t = tcp_tv_to_mssectick(&bbr->rc_tv);
8878 if (TSTMP_GEQ(t, to->to_tsecr)) {
8879 rtt = t - to->to_tsecr;
8880 if (rtt == 0) {
8881 rtt = 1;
8882 }
8883 rtt *= MS_IN_USEC;
8884 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
8885 apply_filter_min_small(&bbr->r_ctl.rc_rttprop,
8886 rtt, bbr->r_ctl.rc_rcvtime);
8887 }
8888 }
8889 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
8890 return (ret_val);
8891 /* We may have changed to FIN_WAIT_1 above */
8892 if (tp->t_state == TCPS_FIN_WAIT_1) {
8893 /*
8894 * In FIN_WAIT_1 STATE in addition to the processing
8895 * for the ESTABLISHED state if our FIN is now
8896 * acknowledged then enter FIN_WAIT_2.
8897 */
8898 if (ourfinisacked) {
8899 /*
8900 * If we can't receive any more data, then
8901 * closing user can proceed. Starting the
8902 * timer is contrary to the specification,
8903 * but if we don't get a FIN we'll hang
8904 * forever.
8905 *
8906 * XXXjl: we should release the tp also, and
8907 * use a compressed state.
8908 */
8909 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8910 soisdisconnected(so);
8911 tcp_timer_activate(tp, TT_2MSL,
8912 (tcp_fast_finwait2_recycle ?
8913 tcp_finwait2_timeout :
8914 TP_MAXIDLE(tp)));
8915 }
8916 tcp_state_change(tp, TCPS_FIN_WAIT_2);
8917 }
8918 }
8919 }
8920 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
8921 tiwin, thflags, nxt_pkt));
8922 }
8923
8924 /*
8925 * Return value of 1, the TCB is unlocked and most
8926 * likely gone, return value of 0, the TCB is still
8927 * locked.
8928 */
8929 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)8930 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
8931 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8932 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8933 {
8934 int32_t ourfinisacked = 0;
8935 int32_t ret_val;
8936 struct tcp_bbr *bbr;
8937
8938 INP_WLOCK_ASSERT(tptoinpcb(tp));
8939
8940 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8941 ctf_calc_rwin(so, tp);
8942 if ((thflags & TH_RST) ||
8943 (tp->t_fin_is_rst && (thflags & TH_FIN)))
8944 return (ctf_process_rst(m, th, so, tp));
8945 if ((thflags & TH_ACK) &&
8946 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
8947 SEQ_GT(th->th_ack, tp->snd_max))) {
8948 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8949 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8950 return (1);
8951 }
8952 if (tp->t_flags & TF_FASTOPEN) {
8953 /*
8954 * When a TFO connection is in SYN_RECEIVED, the only valid
8955 * packets are the initial SYN, a retransmit/copy of the
8956 * initial SYN (possibly with a subset of the original
8957 * data), a valid ACK, a FIN, or a RST.
8958 */
8959 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
8960 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8961 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8962 return (1);
8963 } else if (thflags & TH_SYN) {
8964 /* non-initial SYN is ignored */
8965 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
8966 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
8967 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
8968 ctf_do_drop(m, NULL);
8969 return (0);
8970 }
8971 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
8972 ctf_do_drop(m, NULL);
8973 return (0);
8974 }
8975 }
8976 /*
8977 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
8978 * it's less than ts_recent, drop it.
8979 */
8980 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
8981 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
8982 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
8983 return (ret_val);
8984 }
8985 /*
8986 * In the SYN-RECEIVED state, validate that the packet belongs to
8987 * this connection before trimming the data to fit the receive
8988 * window. Check the sequence number versus IRS since we know the
8989 * sequence numbers haven't wrapped. This is a partial fix for the
8990 * "LAND" DoS attack.
8991 */
8992 if (SEQ_LT(th->th_seq, tp->irs)) {
8993 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8994 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8995 return (1);
8996 }
8997 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
8998 return (ret_val);
8999 }
9000 /*
9001 * If last ACK falls within this segment's sequence numbers, record
9002 * its timestamp. NOTE: 1) That the test incorporates suggestions
9003 * from the latest proposal of the tcplw@cray.com list (Braden
9004 * 1993/04/26). 2) That updating only on newer timestamps interferes
9005 * with our earlier PAWS tests, so this check should be solely
9006 * predicated on the sequence space of this segment. 3) That we
9007 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9008 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9009 * SEG.Len, This modified check allows us to overcome RFC1323's
9010 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9011 * p.869. In such cases, we can still calculate the RTT correctly
9012 * when RCV.NXT == Last.ACK.Sent.
9013 */
9014 if ((to->to_flags & TOF_TS) != 0 &&
9015 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9016 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9017 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9018 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9019 tp->ts_recent = to->to_tsval;
9020 }
9021 tp->snd_wnd = tiwin;
9022 /*
9023 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9024 * is on (half-synchronized state), then queue data for later
9025 * processing; else drop segment and return.
9026 */
9027 if ((thflags & TH_ACK) == 0) {
9028 if (tp->t_flags & TF_FASTOPEN) {
9029 cc_conn_init(tp);
9030 }
9031 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9032 tiwin, thflags, nxt_pkt));
9033 }
9034 KMOD_TCPSTAT_INC(tcps_connects);
9035 if (tp->t_flags & TF_SONOTCONN) {
9036 tp->t_flags &= ~TF_SONOTCONN;
9037 soisconnected(so);
9038 }
9039 /* Do window scaling? */
9040 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9041 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9042 tp->rcv_scale = tp->request_r_scale;
9043 }
9044 /*
9045 * ok for the first time in lets see if we can use the ts to figure
9046 * out what the initial RTT was.
9047 */
9048 if ((to->to_flags & TOF_TS) != 0) {
9049 uint32_t t, rtt;
9050
9051 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9052 if (TSTMP_GEQ(t, to->to_tsecr)) {
9053 rtt = t - to->to_tsecr;
9054 if (rtt == 0) {
9055 rtt = 1;
9056 }
9057 rtt *= MS_IN_USEC;
9058 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9059 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime);
9060 }
9061 }
9062 /* Drop off any SYN in the send map (probably not there) */
9063 if (thflags & TH_ACK)
9064 bbr_log_syn(tp, to);
9065 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) {
9066 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9067 tp->t_tfo_pending = NULL;
9068 }
9069 /*
9070 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9071 * FIN-WAIT-1
9072 */
9073 tp->t_starttime = ticks;
9074 if (tp->t_flags & TF_NEEDFIN) {
9075 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9076 tp->t_flags &= ~TF_NEEDFIN;
9077 } else {
9078 tcp_state_change(tp, TCPS_ESTABLISHED);
9079 TCP_PROBE5(accept__established, NULL, tp,
9080 mtod(m, const char *), tp, th);
9081 /*
9082 * TFO connections call cc_conn_init() during SYN
9083 * processing. Calling it again here for such connections
9084 * is not harmless as it would undo the snd_cwnd reduction
9085 * that occurs when a TFO SYN|ACK is retransmitted.
9086 */
9087 if (!(tp->t_flags & TF_FASTOPEN))
9088 cc_conn_init(tp);
9089 }
9090 /*
9091 * Account for the ACK of our SYN prior to
9092 * regular ACK processing below, except for
9093 * simultaneous SYN, which is handled later.
9094 */
9095 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
9096 tp->snd_una++;
9097 /*
9098 * If segment contains data or ACK, will call tcp_reass() later; if
9099 * not, do so now to pass queued data to user.
9100 */
9101 if (tlen == 0 && (thflags & TH_FIN) == 0) {
9102 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9103 (struct mbuf *)0);
9104 if (tp->t_flags & TF_WAKESOR) {
9105 tp->t_flags &= ~TF_WAKESOR;
9106 /* NB: sorwakeup_locked() does an implicit unlock. */
9107 sorwakeup_locked(so);
9108 }
9109 }
9110 tp->snd_wl1 = th->th_seq - 1;
9111 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9112 return (ret_val);
9113 }
9114 if (tp->t_state == TCPS_FIN_WAIT_1) {
9115 /* We could have went to FIN_WAIT_1 (or EST) above */
9116 /*
9117 * In FIN_WAIT_1 STATE in addition to the processing for the
9118 * ESTABLISHED state if our FIN is now acknowledged then
9119 * enter FIN_WAIT_2.
9120 */
9121 if (ourfinisacked) {
9122 /*
9123 * If we can't receive any more data, then closing
9124 * user can proceed. Starting the timer is contrary
9125 * to the specification, but if we don't get a FIN
9126 * we'll hang forever.
9127 *
9128 * XXXjl: we should release the tp also, and use a
9129 * compressed state.
9130 */
9131 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9132 soisdisconnected(so);
9133 tcp_timer_activate(tp, TT_2MSL,
9134 (tcp_fast_finwait2_recycle ?
9135 tcp_finwait2_timeout :
9136 TP_MAXIDLE(tp)));
9137 }
9138 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9139 }
9140 }
9141 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9142 tiwin, thflags, nxt_pkt));
9143 }
9144
9145 /*
9146 * Return value of 1, the TCB is unlocked and most
9147 * likely gone, return value of 0, the TCB is still
9148 * locked.
9149 */
9150 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)9151 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9152 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9153 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9154 {
9155 struct tcp_bbr *bbr;
9156 int32_t ret_val;
9157
9158 INP_WLOCK_ASSERT(tptoinpcb(tp));
9159
9160 /*
9161 * Header prediction: check for the two common cases of a
9162 * uni-directional data xfer. If the packet has no control flags,
9163 * is in-sequence, the window didn't change and we're not
9164 * retransmitting, it's a candidate. If the length is zero and the
9165 * ack moved forward, we're the sender side of the xfer. Just free
9166 * the data acked & wake any higher level process that was blocked
9167 * waiting for space. If the length is non-zero and the ack didn't
9168 * move, we're the receiver side. If we're getting packets in-order
9169 * (the reassembly queue is empty), add the data toc The socket
9170 * buffer and note that we need a delayed ack. Make sure that the
9171 * hidden state-flags are also off. Since we check for
9172 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9173 */
9174 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9175 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) {
9176 /*
9177 * If we have delived under 4 segments increase the initial
9178 * window if raised by the peer. We use this to determine
9179 * dynamic and static rwnd's at the end of a connection.
9180 */
9181 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd);
9182 }
9183 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9184 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
9185 __predict_true(SEGQ_EMPTY(tp)) &&
9186 __predict_true(th->th_seq == tp->rcv_nxt)) {
9187 if (tlen == 0) {
9188 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9189 tiwin, nxt_pkt, iptos)) {
9190 return (0);
9191 }
9192 } else {
9193 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9194 tiwin, nxt_pkt)) {
9195 return (0);
9196 }
9197 }
9198 }
9199 ctf_calc_rwin(so, tp);
9200
9201 if ((thflags & TH_RST) ||
9202 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9203 return (ctf_process_rst(m, th, so, tp));
9204 /*
9205 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9206 * synchronized state.
9207 */
9208 if (thflags & TH_SYN) {
9209 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9210 return (ret_val);
9211 }
9212 /*
9213 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9214 * it's less than ts_recent, drop it.
9215 */
9216 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9217 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9218 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9219 return (ret_val);
9220 }
9221 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9222 return (ret_val);
9223 }
9224 /*
9225 * If last ACK falls within this segment's sequence numbers, record
9226 * its timestamp. NOTE: 1) That the test incorporates suggestions
9227 * from the latest proposal of the tcplw@cray.com list (Braden
9228 * 1993/04/26). 2) That updating only on newer timestamps interferes
9229 * with our earlier PAWS tests, so this check should be solely
9230 * predicated on the sequence space of this segment. 3) That we
9231 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9232 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9233 * SEG.Len, This modified check allows us to overcome RFC1323's
9234 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9235 * p.869. In such cases, we can still calculate the RTT correctly
9236 * when RCV.NXT == Last.ACK.Sent.
9237 */
9238 if ((to->to_flags & TOF_TS) != 0 &&
9239 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9240 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9241 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9242 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9243 tp->ts_recent = to->to_tsval;
9244 }
9245 /*
9246 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9247 * is on (half-synchronized state), then queue data for later
9248 * processing; else drop segment and return.
9249 */
9250 if ((thflags & TH_ACK) == 0) {
9251 if (tp->t_flags & TF_NEEDSYN) {
9252 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9253 tiwin, thflags, nxt_pkt));
9254 } else if (tp->t_flags & TF_ACKNOW) {
9255 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9256 bbr->r_wanted_output = 1;
9257 return (ret_val);
9258 } else {
9259 ctf_do_drop(m, NULL);
9260 return (0);
9261 }
9262 }
9263 /*
9264 * Ack processing.
9265 */
9266 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9267 return (ret_val);
9268 }
9269 if (sbavail(&so->so_snd)) {
9270 if (ctf_progress_timeout_check(tp, true)) {
9271 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9272 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9273 return (1);
9274 }
9275 }
9276 /* State changes only happen in bbr_process_data() */
9277 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9278 tiwin, thflags, nxt_pkt));
9279 }
9280
9281 /*
9282 * Return value of 1, the TCB is unlocked and most
9283 * likely gone, return value of 0, the TCB is still
9284 * locked.
9285 */
9286 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)9287 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9288 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9289 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9290 {
9291 struct tcp_bbr *bbr;
9292 int32_t ret_val;
9293
9294 INP_WLOCK_ASSERT(tptoinpcb(tp));
9295
9296 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9297 ctf_calc_rwin(so, tp);
9298 if ((thflags & TH_RST) ||
9299 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9300 return (ctf_process_rst(m, th, so, tp));
9301 /*
9302 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9303 * synchronized state.
9304 */
9305 if (thflags & TH_SYN) {
9306 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9307 return (ret_val);
9308 }
9309 /*
9310 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9311 * it's less than ts_recent, drop it.
9312 */
9313 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9314 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9315 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9316 return (ret_val);
9317 }
9318 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9319 return (ret_val);
9320 }
9321 /*
9322 * If last ACK falls within this segment's sequence numbers, record
9323 * its timestamp. NOTE: 1) That the test incorporates suggestions
9324 * from the latest proposal of the tcplw@cray.com list (Braden
9325 * 1993/04/26). 2) That updating only on newer timestamps interferes
9326 * with our earlier PAWS tests, so this check should be solely
9327 * predicated on the sequence space of this segment. 3) That we
9328 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9329 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9330 * SEG.Len, This modified check allows us to overcome RFC1323's
9331 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9332 * p.869. In such cases, we can still calculate the RTT correctly
9333 * when RCV.NXT == Last.ACK.Sent.
9334 */
9335 if ((to->to_flags & TOF_TS) != 0 &&
9336 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9337 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9338 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9339 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9340 tp->ts_recent = to->to_tsval;
9341 }
9342 /*
9343 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9344 * is on (half-synchronized state), then queue data for later
9345 * processing; else drop segment and return.
9346 */
9347 if ((thflags & TH_ACK) == 0) {
9348 if (tp->t_flags & TF_NEEDSYN) {
9349 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9350 tiwin, thflags, nxt_pkt));
9351 } else if (tp->t_flags & TF_ACKNOW) {
9352 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9353 bbr->r_wanted_output = 1;
9354 return (ret_val);
9355 } else {
9356 ctf_do_drop(m, NULL);
9357 return (0);
9358 }
9359 }
9360 /*
9361 * Ack processing.
9362 */
9363 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9364 return (ret_val);
9365 }
9366 if (sbavail(&so->so_snd)) {
9367 if (ctf_progress_timeout_check(tp, true)) {
9368 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9369 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9370 return (1);
9371 }
9372 }
9373 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9374 tiwin, thflags, nxt_pkt));
9375 }
9376
9377 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)9378 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr,
9379 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so)
9380 {
9381
9382 if (bbr->rc_allow_data_af_clo == 0) {
9383 close_now:
9384 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9385 /* tcp_close will kill the inp pre-log the Reset */
9386 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
9387 tp = tcp_close(tp);
9388 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9389 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9390 return (1);
9391 }
9392 if (sbavail(&so->so_snd) == 0)
9393 goto close_now;
9394 /* Ok we allow data that is ignored and a followup reset */
9395 tp->rcv_nxt = th->th_seq + *tlen;
9396 tp->t_flags2 |= TF2_DROP_AF_DATA;
9397 bbr->r_wanted_output = 1;
9398 *tlen = 0;
9399 return (0);
9400 }
9401
9402 /*
9403 * Return value of 1, the TCB is unlocked and most
9404 * likely gone, return value of 0, the TCB is still
9405 * locked.
9406 */
9407 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)9408 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9409 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9410 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9411 {
9412 int32_t ourfinisacked = 0;
9413 int32_t ret_val;
9414 struct tcp_bbr *bbr;
9415
9416 INP_WLOCK_ASSERT(tptoinpcb(tp));
9417
9418 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9419 ctf_calc_rwin(so, tp);
9420 if ((thflags & TH_RST) ||
9421 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9422 return (ctf_process_rst(m, th, so, tp));
9423 /*
9424 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9425 * synchronized state.
9426 */
9427 if (thflags & TH_SYN) {
9428 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9429 return (ret_val);
9430 }
9431 /*
9432 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9433 * it's less than ts_recent, drop it.
9434 */
9435 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9436 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9437 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9438 return (ret_val);
9439 }
9440 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9441 return (ret_val);
9442 }
9443 /*
9444 * If new data are received on a connection after the user processes
9445 * are gone, then RST the other end.
9446 * We call a new function now so we might continue and setup
9447 * to reset at all data being ack'd.
9448 */
9449 if ((tp->t_flags & TF_CLOSED) && tlen &&
9450 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9451 return (1);
9452 /*
9453 * If last ACK falls within this segment's sequence numbers, record
9454 * its timestamp. NOTE: 1) That the test incorporates suggestions
9455 * from the latest proposal of the tcplw@cray.com list (Braden
9456 * 1993/04/26). 2) That updating only on newer timestamps interferes
9457 * with our earlier PAWS tests, so this check should be solely
9458 * predicated on the sequence space of this segment. 3) That we
9459 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9460 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9461 * SEG.Len, This modified check allows us to overcome RFC1323's
9462 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9463 * p.869. In such cases, we can still calculate the RTT correctly
9464 * when RCV.NXT == Last.ACK.Sent.
9465 */
9466 if ((to->to_flags & TOF_TS) != 0 &&
9467 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9468 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9469 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9470 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9471 tp->ts_recent = to->to_tsval;
9472 }
9473 /*
9474 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9475 * is on (half-synchronized state), then queue data for later
9476 * processing; else drop segment and return.
9477 */
9478 if ((thflags & TH_ACK) == 0) {
9479 if (tp->t_flags & TF_NEEDSYN) {
9480 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9481 tiwin, thflags, nxt_pkt));
9482 } else if (tp->t_flags & TF_ACKNOW) {
9483 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9484 bbr->r_wanted_output = 1;
9485 return (ret_val);
9486 } else {
9487 ctf_do_drop(m, NULL);
9488 return (0);
9489 }
9490 }
9491 /*
9492 * Ack processing.
9493 */
9494 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9495 return (ret_val);
9496 }
9497 if (ourfinisacked) {
9498 /*
9499 * If we can't receive any more data, then closing user can
9500 * proceed. Starting the timer is contrary to the
9501 * specification, but if we don't get a FIN we'll hang
9502 * forever.
9503 *
9504 * XXXjl: we should release the tp also, and use a
9505 * compressed state.
9506 */
9507 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9508 soisdisconnected(so);
9509 tcp_timer_activate(tp, TT_2MSL,
9510 (tcp_fast_finwait2_recycle ?
9511 tcp_finwait2_timeout :
9512 TP_MAXIDLE(tp)));
9513 }
9514 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9515 }
9516 if (sbavail(&so->so_snd)) {
9517 if (ctf_progress_timeout_check(tp, true)) {
9518 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9519 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9520 return (1);
9521 }
9522 }
9523 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9524 tiwin, thflags, nxt_pkt));
9525 }
9526
9527 /*
9528 * Return value of 1, the TCB is unlocked and most
9529 * likely gone, return value of 0, the TCB is still
9530 * locked.
9531 */
9532 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)9533 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9534 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9535 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9536 {
9537 int32_t ourfinisacked = 0;
9538 int32_t ret_val;
9539 struct tcp_bbr *bbr;
9540
9541 INP_WLOCK_ASSERT(tptoinpcb(tp));
9542
9543 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9544 ctf_calc_rwin(so, tp);
9545 if ((thflags & TH_RST) ||
9546 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9547 return (ctf_process_rst(m, th, so, tp));
9548 /*
9549 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9550 * synchronized state.
9551 */
9552 if (thflags & TH_SYN) {
9553 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9554 return (ret_val);
9555 }
9556 /*
9557 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9558 * it's less than ts_recent, drop it.
9559 */
9560 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9561 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9562 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9563 return (ret_val);
9564 }
9565 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9566 return (ret_val);
9567 }
9568 /*
9569 * If last ACK falls within this segment's sequence numbers, record
9570 * its timestamp. NOTE: 1) That the test incorporates suggestions
9571 * from the latest proposal of the tcplw@cray.com list (Braden
9572 * 1993/04/26). 2) That updating only on newer timestamps interferes
9573 * with our earlier PAWS tests, so this check should be solely
9574 * predicated on the sequence space of this segment. 3) That we
9575 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9576 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9577 * SEG.Len, This modified check allows us to overcome RFC1323's
9578 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9579 * p.869. In such cases, we can still calculate the RTT correctly
9580 * when RCV.NXT == Last.ACK.Sent.
9581 */
9582 if ((to->to_flags & TOF_TS) != 0 &&
9583 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9584 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9585 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9586 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9587 tp->ts_recent = to->to_tsval;
9588 }
9589 /*
9590 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9591 * is on (half-synchronized state), then queue data for later
9592 * processing; else drop segment and return.
9593 */
9594 if ((thflags & TH_ACK) == 0) {
9595 if (tp->t_flags & TF_NEEDSYN) {
9596 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9597 tiwin, thflags, nxt_pkt));
9598 } else if (tp->t_flags & TF_ACKNOW) {
9599 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9600 bbr->r_wanted_output = 1;
9601 return (ret_val);
9602 } else {
9603 ctf_do_drop(m, NULL);
9604 return (0);
9605 }
9606 }
9607 /*
9608 * Ack processing.
9609 */
9610 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9611 return (ret_val);
9612 }
9613 if (ourfinisacked) {
9614 tcp_twstart(tp);
9615 m_freem(m);
9616 return (1);
9617 }
9618 if (sbavail(&so->so_snd)) {
9619 if (ctf_progress_timeout_check(tp, true)) {
9620 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9621 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9622 return (1);
9623 }
9624 }
9625 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9626 tiwin, thflags, nxt_pkt));
9627 }
9628
9629 /*
9630 * Return value of 1, the TCB is unlocked and most
9631 * likely gone, return value of 0, the TCB is still
9632 * locked.
9633 */
9634 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)9635 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
9636 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9637 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9638 {
9639 int32_t ourfinisacked = 0;
9640 int32_t ret_val;
9641 struct tcp_bbr *bbr;
9642
9643 INP_WLOCK_ASSERT(tptoinpcb(tp));
9644
9645 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9646 ctf_calc_rwin(so, tp);
9647 if ((thflags & TH_RST) ||
9648 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9649 return (ctf_process_rst(m, th, so, tp));
9650 /*
9651 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9652 * synchronized state.
9653 */
9654 if (thflags & TH_SYN) {
9655 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9656 return (ret_val);
9657 }
9658 /*
9659 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9660 * it's less than ts_recent, drop it.
9661 */
9662 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9663 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9664 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9665 return (ret_val);
9666 }
9667 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9668 return (ret_val);
9669 }
9670 /*
9671 * If last ACK falls within this segment's sequence numbers, record
9672 * its timestamp. NOTE: 1) That the test incorporates suggestions
9673 * from the latest proposal of the tcplw@cray.com list (Braden
9674 * 1993/04/26). 2) That updating only on newer timestamps interferes
9675 * with our earlier PAWS tests, so this check should be solely
9676 * predicated on the sequence space of this segment. 3) That we
9677 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9678 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9679 * SEG.Len, This modified check allows us to overcome RFC1323's
9680 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9681 * p.869. In such cases, we can still calculate the RTT correctly
9682 * when RCV.NXT == Last.ACK.Sent.
9683 */
9684 if ((to->to_flags & TOF_TS) != 0 &&
9685 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9686 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9687 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9688 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9689 tp->ts_recent = to->to_tsval;
9690 }
9691 /*
9692 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9693 * is on (half-synchronized state), then queue data for later
9694 * processing; else drop segment and return.
9695 */
9696 if ((thflags & TH_ACK) == 0) {
9697 if (tp->t_flags & TF_NEEDSYN) {
9698 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9699 tiwin, thflags, nxt_pkt));
9700 } else if (tp->t_flags & TF_ACKNOW) {
9701 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9702 bbr->r_wanted_output = 1;
9703 return (ret_val);
9704 } else {
9705 ctf_do_drop(m, NULL);
9706 return (0);
9707 }
9708 }
9709 /*
9710 * case TCPS_LAST_ACK: Ack processing.
9711 */
9712 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9713 return (ret_val);
9714 }
9715 if (ourfinisacked) {
9716 tp = tcp_close(tp);
9717 ctf_do_drop(m, tp);
9718 return (1);
9719 }
9720 if (sbavail(&so->so_snd)) {
9721 if (ctf_progress_timeout_check(tp, true)) {
9722 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9723 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9724 return (1);
9725 }
9726 }
9727 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9728 tiwin, thflags, nxt_pkt));
9729 }
9730
9731 /*
9732 * Return value of 1, the TCB is unlocked and most
9733 * likely gone, return value of 0, the TCB is still
9734 * locked.
9735 */
9736 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)9737 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
9738 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9739 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9740 {
9741 int32_t ourfinisacked = 0;
9742 int32_t ret_val;
9743 struct tcp_bbr *bbr;
9744
9745 INP_WLOCK_ASSERT(tptoinpcb(tp));
9746
9747 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9748 ctf_calc_rwin(so, tp);
9749 /* Reset receive buffer auto scaling when not in bulk receive mode. */
9750 if ((thflags & TH_RST) ||
9751 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9752 return (ctf_process_rst(m, th, so, tp));
9753
9754 /*
9755 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9756 * synchronized state.
9757 */
9758 if (thflags & TH_SYN) {
9759 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
9760 return (ret_val);
9761 }
9762 /*
9763 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9764 * it's less than ts_recent, drop it.
9765 */
9766 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9767 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9768 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9769 return (ret_val);
9770 }
9771 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9772 return (ret_val);
9773 }
9774 /*
9775 * If new data are received on a connection after the user processes
9776 * are gone, then we may RST the other end depending on the outcome
9777 * of bbr_check_data_after_close.
9778 * We call a new function now so we might continue and setup
9779 * to reset at all data being ack'd.
9780 */
9781 if ((tp->t_flags & TF_CLOSED) && tlen &&
9782 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9783 return (1);
9784 /*
9785 * If last ACK falls within this segment's sequence numbers, record
9786 * its timestamp. NOTE: 1) That the test incorporates suggestions
9787 * from the latest proposal of the tcplw@cray.com list (Braden
9788 * 1993/04/26). 2) That updating only on newer timestamps interferes
9789 * with our earlier PAWS tests, so this check should be solely
9790 * predicated on the sequence space of this segment. 3) That we
9791 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9792 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9793 * SEG.Len, This modified check allows us to overcome RFC1323's
9794 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9795 * p.869. In such cases, we can still calculate the RTT correctly
9796 * when RCV.NXT == Last.ACK.Sent.
9797 */
9798 if ((to->to_flags & TOF_TS) != 0 &&
9799 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9800 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9801 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9802 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9803 tp->ts_recent = to->to_tsval;
9804 }
9805 /*
9806 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9807 * is on (half-synchronized state), then queue data for later
9808 * processing; else drop segment and return.
9809 */
9810 if ((thflags & TH_ACK) == 0) {
9811 if (tp->t_flags & TF_NEEDSYN) {
9812 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9813 tiwin, thflags, nxt_pkt));
9814 } else if (tp->t_flags & TF_ACKNOW) {
9815 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9816 bbr->r_wanted_output = 1;
9817 return (ret_val);
9818 } else {
9819 ctf_do_drop(m, NULL);
9820 return (0);
9821 }
9822 }
9823 /*
9824 * Ack processing.
9825 */
9826 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9827 return (ret_val);
9828 }
9829 if (sbavail(&so->so_snd)) {
9830 if (ctf_progress_timeout_check(tp, true)) {
9831 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9832 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9833 return (1);
9834 }
9835 }
9836 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9837 tiwin, thflags, nxt_pkt));
9838 }
9839
9840 static void
bbr_stop_all_timers(struct tcpcb * tp,struct tcp_bbr * bbr)9841 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr)
9842 {
9843 /*
9844 * Assure no timers are running.
9845 */
9846 if (tcp_timer_active(tp, TT_PERSIST)) {
9847 /* We enter in persists, set the flag appropriately */
9848 bbr->rc_in_persist = 1;
9849 }
9850 if (tcp_in_hpts(bbr->rc_tp)) {
9851 tcp_hpts_remove(bbr->rc_tp);
9852 }
9853 }
9854
9855 static void
bbr_google_mode_on(struct tcp_bbr * bbr)9856 bbr_google_mode_on(struct tcp_bbr *bbr)
9857 {
9858 bbr->rc_use_google = 1;
9859 bbr->rc_no_pacing = 0;
9860 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
9861 bbr->r_use_policer = bbr_policer_detection_enabled;
9862 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
9863 bbr->bbr_use_rack_cheat = 0;
9864 bbr->r_ctl.rc_incr_tmrs = 0;
9865 bbr->r_ctl.rc_inc_tcp_oh = 0;
9866 bbr->r_ctl.rc_inc_ip_oh = 0;
9867 bbr->r_ctl.rc_inc_enet_oh = 0;
9868 reset_time(&bbr->r_ctl.rc_delrate,
9869 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
9870 reset_time_small(&bbr->r_ctl.rc_rttprop,
9871 (11 * USECS_IN_SECOND));
9872 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
9873 }
9874
9875 static void
bbr_google_mode_off(struct tcp_bbr * bbr)9876 bbr_google_mode_off(struct tcp_bbr *bbr)
9877 {
9878 bbr->rc_use_google = 0;
9879 bbr->r_ctl.bbr_google_discount = 0;
9880 bbr->no_pacing_until = bbr_no_pacing_until;
9881 bbr->r_use_policer = 0;
9882 if (bbr->no_pacing_until)
9883 bbr->rc_no_pacing = 1;
9884 else
9885 bbr->rc_no_pacing = 0;
9886 if (bbr_use_rack_resend_cheat)
9887 bbr->bbr_use_rack_cheat = 1;
9888 else
9889 bbr->bbr_use_rack_cheat = 0;
9890 if (bbr_incr_timers)
9891 bbr->r_ctl.rc_incr_tmrs = 1;
9892 else
9893 bbr->r_ctl.rc_incr_tmrs = 0;
9894 if (bbr_include_tcp_oh)
9895 bbr->r_ctl.rc_inc_tcp_oh = 1;
9896 else
9897 bbr->r_ctl.rc_inc_tcp_oh = 0;
9898 if (bbr_include_ip_oh)
9899 bbr->r_ctl.rc_inc_ip_oh = 1;
9900 else
9901 bbr->r_ctl.rc_inc_ip_oh = 0;
9902 if (bbr_include_enet_oh)
9903 bbr->r_ctl.rc_inc_enet_oh = 1;
9904 else
9905 bbr->r_ctl.rc_inc_enet_oh = 0;
9906 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
9907 reset_time(&bbr->r_ctl.rc_delrate,
9908 bbr_num_pktepo_for_del_limit);
9909 reset_time_small(&bbr->r_ctl.rc_rttprop,
9910 (bbr_filter_len_sec * USECS_IN_SECOND));
9911 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
9912 }
9913 /*
9914 * Return 0 on success, non-zero on failure
9915 * which indicates the error (usually no memory).
9916 */
9917 static int
bbr_init(struct tcpcb * tp,void ** ptr)9918 bbr_init(struct tcpcb *tp, void **ptr)
9919 {
9920 struct inpcb *inp = tptoinpcb(tp);
9921 struct tcp_bbr *bbr = NULL;
9922 uint32_t cts;
9923
9924 tcp_hpts_init(tp);
9925
9926 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO));
9927 if (*ptr == NULL) {
9928 /*
9929 * We need to allocate memory but cant. The INP and INP_INFO
9930 * locks and they are recursive (happens during setup. So a
9931 * scheme to drop the locks fails :(
9932 *
9933 */
9934 return (ENOMEM);
9935 }
9936 bbr = (struct tcp_bbr *)*ptr;
9937 bbr->rtt_valid = 0;
9938 tp->t_flags2 |= TF2_CANNOT_DO_ECN;
9939 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
9940 /* Take off any undesired flags */
9941 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY;
9942 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
9943 tp->t_flags2 &= ~TF2_MBUF_ACKCMP;
9944 tp->t_flags2 &= ~TF2_MBUF_L_ACKS;
9945
9946 TAILQ_INIT(&bbr->r_ctl.rc_map);
9947 TAILQ_INIT(&bbr->r_ctl.rc_free);
9948 TAILQ_INIT(&bbr->r_ctl.rc_tmap);
9949 bbr->rc_tp = tp;
9950 bbr->rc_inp = inp;
9951 cts = tcp_get_usecs(&bbr->rc_tv);
9952 tp->t_acktime = 0;
9953 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close;
9954 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade;
9955 bbr->rc_tlp_threshold = bbr_tlp_thresh;
9956 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh;
9957 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay;
9958 bbr->r_ctl.rc_min_to = bbr_min_to;
9959 bbr->rc_bbr_state = BBR_STATE_STARTUP;
9960 bbr->r_ctl.bbr_lost_at_state = 0;
9961 bbr->r_ctl.rc_lost_at_startup = 0;
9962 bbr->rc_all_timers_stopped = 0;
9963 bbr->r_ctl.rc_bbr_lastbtlbw = 0;
9964 bbr->r_ctl.rc_pkt_epoch_del = 0;
9965 bbr->r_ctl.rc_pkt_epoch = 0;
9966 bbr->r_ctl.rc_lowest_rtt = 0xffffffff;
9967 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain;
9968 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
9969 bbr->r_ctl.rc_went_idle_time = cts;
9970 bbr->rc_pacer_started = cts;
9971 bbr->r_ctl.rc_pkt_epoch_time = cts;
9972 bbr->r_ctl.rc_rcvtime = cts;
9973 bbr->r_ctl.rc_bbr_state_time = cts;
9974 bbr->r_ctl.rc_del_time = cts;
9975 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
9976 bbr->r_ctl.last_in_probertt = cts;
9977 bbr->skip_gain = 0;
9978 bbr->gain_is_limited = 0;
9979 bbr->no_pacing_until = bbr_no_pacing_until;
9980 if (bbr->no_pacing_until)
9981 bbr->rc_no_pacing = 1;
9982 if (bbr_use_google_algo) {
9983 bbr->rc_no_pacing = 0;
9984 bbr->rc_use_google = 1;
9985 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
9986 bbr->r_use_policer = bbr_policer_detection_enabled;
9987 } else {
9988 bbr->rc_use_google = 0;
9989 bbr->r_ctl.bbr_google_discount = 0;
9990 bbr->r_use_policer = 0;
9991 }
9992 if (bbr_ts_limiting)
9993 bbr->rc_use_ts_limit = 1;
9994 else
9995 bbr->rc_use_ts_limit = 0;
9996 if (bbr_ts_can_raise)
9997 bbr->ts_can_raise = 1;
9998 else
9999 bbr->ts_can_raise = 0;
10000 if (V_tcp_delack_enabled == 1)
10001 tp->t_delayed_ack = 2;
10002 else if (V_tcp_delack_enabled == 0)
10003 tp->t_delayed_ack = 0;
10004 else if (V_tcp_delack_enabled < 100)
10005 tp->t_delayed_ack = V_tcp_delack_enabled;
10006 else
10007 tp->t_delayed_ack = 2;
10008 if (bbr->rc_use_google == 0)
10009 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10010 else
10011 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10012 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms;
10013 bbr->rc_max_rto_sec = bbr_rto_max_sec;
10014 bbr->rc_init_win = bbr_def_init_win;
10015 if (tp->t_flags & TF_REQ_TSTMP)
10016 bbr->rc_last_options = TCP_TS_OVERHEAD;
10017 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options;
10018 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd;
10019 bbr->r_init_rtt = 1;
10020
10021 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
10022 if (bbr_allow_hdwr_pacing)
10023 bbr->bbr_hdw_pace_ena = 1;
10024 else
10025 bbr->bbr_hdw_pace_ena = 0;
10026 if (bbr_sends_full_iwnd)
10027 bbr->bbr_init_win_cheat = 1;
10028 else
10029 bbr->bbr_init_win_cheat = 0;
10030 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max;
10031 bbr->r_ctl.rc_drain_pg = bbr_drain_gain;
10032 bbr->r_ctl.rc_startup_pg = bbr_high_gain;
10033 bbr->rc_loss_exit = bbr_exit_startup_at_loss;
10034 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain;
10035 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second;
10036 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar;
10037 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max;
10038 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor;
10039 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min;
10040 bbr->r_ctl.bbr_cross_over = bbr_cross_over;
10041 bbr->r_ctl.rc_rtt_shrinks = cts;
10042 if (bbr->rc_use_google) {
10043 setup_time_filter(&bbr->r_ctl.rc_delrate,
10044 FILTER_TYPE_MAX,
10045 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10046 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10047 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND));
10048 } else {
10049 setup_time_filter(&bbr->r_ctl.rc_delrate,
10050 FILTER_TYPE_MAX,
10051 bbr_num_pktepo_for_del_limit);
10052 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10053 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND));
10054 }
10055 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0);
10056 if (bbr_uses_idle_restart)
10057 bbr->rc_use_idle_restart = 1;
10058 else
10059 bbr->rc_use_idle_restart = 0;
10060 bbr->r_ctl.rc_bbr_cur_del_rate = 0;
10061 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps;
10062 if (bbr_resends_use_tso)
10063 bbr->rc_resends_use_tso = 1;
10064 if (tp->snd_una != tp->snd_max) {
10065 /* Create a send map for the current outstanding data */
10066 struct bbr_sendmap *rsm;
10067
10068 rsm = bbr_alloc(bbr);
10069 if (rsm == NULL) {
10070 uma_zfree(bbr_pcb_zone, *ptr);
10071 *ptr = NULL;
10072 return (ENOMEM);
10073 }
10074 rsm->r_rtt_not_allowed = 1;
10075 rsm->r_tim_lastsent[0] = cts;
10076 rsm->r_rtr_cnt = 1;
10077 rsm->r_rtr_bytes = 0;
10078 rsm->r_start = tp->snd_una;
10079 rsm->r_end = tp->snd_max;
10080 rsm->r_dupack = 0;
10081 rsm->r_delivered = bbr->r_ctl.rc_delivered;
10082 rsm->r_ts_valid = 0;
10083 rsm->r_del_ack_ts = tp->ts_recent;
10084 rsm->r_del_time = cts;
10085 if (bbr->r_ctl.r_app_limited_until)
10086 rsm->r_app_limited = 1;
10087 else
10088 rsm->r_app_limited = 0;
10089 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
10090 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
10091 rsm->r_in_tmap = 1;
10092 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
10093 rsm->r_bbr_state = bbr_state_val(bbr);
10094 else
10095 rsm->r_bbr_state = 8;
10096 }
10097 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0))
10098 bbr->bbr_use_rack_cheat = 1;
10099 if (bbr_incr_timers && (bbr->rc_use_google == 0))
10100 bbr->r_ctl.rc_incr_tmrs = 1;
10101 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0))
10102 bbr->r_ctl.rc_inc_tcp_oh = 1;
10103 if (bbr_include_ip_oh && (bbr->rc_use_google == 0))
10104 bbr->r_ctl.rc_inc_ip_oh = 1;
10105 if (bbr_include_enet_oh && (bbr->rc_use_google == 0))
10106 bbr->r_ctl.rc_inc_enet_oh = 1;
10107
10108 bbr_log_type_statechange(bbr, cts, __LINE__);
10109 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
10110 (tp->t_srtt)) {
10111 uint32_t rtt;
10112
10113 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
10114 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
10115 }
10116 /* announce the settings and state */
10117 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT);
10118 tcp_bbr_tso_size_check(bbr, cts);
10119 /*
10120 * Now call the generic function to start a timer. This will place
10121 * the TCB on the hptsi wheel if a timer is needed with appropriate
10122 * flags.
10123 */
10124 bbr_stop_all_timers(tp, bbr);
10125 /*
10126 * Validate the timers are not in usec, if they are convert.
10127 * BBR should in theory move to USEC and get rid of a
10128 * lot of the TICKS_2 calls.. but for now we stay
10129 * with tick timers.
10130 */
10131 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
10132 TCPT_RANGESET(tp->t_rxtcur,
10133 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
10134 tp->t_rttmin, TCPTV_REXMTMAX);
10135 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0);
10136 return (0);
10137 }
10138
10139 /*
10140 * Return 0 if we can accept the connection. Return
10141 * non-zero if we can't handle the connection. A EAGAIN
10142 * means you need to wait until the connection is up.
10143 * a EADDRNOTAVAIL means we can never handle the connection
10144 * (no SACK).
10145 */
10146 static int
bbr_handoff_ok(struct tcpcb * tp)10147 bbr_handoff_ok(struct tcpcb *tp)
10148 {
10149 if ((tp->t_state == TCPS_CLOSED) ||
10150 (tp->t_state == TCPS_LISTEN)) {
10151 /* Sure no problem though it may not stick */
10152 return (0);
10153 }
10154 if ((tp->t_state == TCPS_SYN_SENT) ||
10155 (tp->t_state == TCPS_SYN_RECEIVED)) {
10156 /*
10157 * We really don't know you have to get to ESTAB or beyond
10158 * to tell.
10159 */
10160 return (EAGAIN);
10161 }
10162 if (tp->t_flags & TF_SENTFIN)
10163 return (EINVAL);
10164 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) {
10165 return (0);
10166 }
10167 /*
10168 * If we reach here we don't do SACK on this connection so we can
10169 * never do rack.
10170 */
10171 return (EINVAL);
10172 }
10173
10174 static void
bbr_fini(struct tcpcb * tp,int32_t tcb_is_purged)10175 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10176 {
10177 if (tp->t_fb_ptr) {
10178 uint32_t calc;
10179 struct tcp_bbr *bbr;
10180 struct bbr_sendmap *rsm;
10181
10182 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10183 if (bbr->r_ctl.crte)
10184 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
10185 bbr_log_flowend(bbr);
10186 bbr->rc_tp = NULL;
10187 if (bbr->bbr_hdrw_pacing)
10188 counter_u64_add(bbr_flows_whdwr_pacing, -1);
10189 else
10190 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
10191 if (bbr->r_ctl.crte != NULL) {
10192 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp);
10193 bbr->r_ctl.crte = NULL;
10194 }
10195 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10196 while (rsm) {
10197 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
10198 uma_zfree(bbr_zone, rsm);
10199 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10200 }
10201 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10202 while (rsm) {
10203 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
10204 uma_zfree(bbr_zone, rsm);
10205 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10206 }
10207 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd;
10208 if (calc > (bbr->r_ctl.rc_init_rwnd / 10))
10209 BBR_STAT_INC(bbr_dynamic_rwnd);
10210 else
10211 BBR_STAT_INC(bbr_static_rwnd);
10212 bbr->r_ctl.rc_free_cnt = 0;
10213 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10214 tp->t_fb_ptr = NULL;
10215 }
10216 /* Make sure snd_nxt is correctly set */
10217 tp->snd_nxt = tp->snd_max;
10218 }
10219
10220 static void
bbr_set_state(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t win)10221 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win)
10222 {
10223 switch (tp->t_state) {
10224 case TCPS_SYN_SENT:
10225 bbr->r_state = TCPS_SYN_SENT;
10226 bbr->r_substate = bbr_do_syn_sent;
10227 break;
10228 case TCPS_SYN_RECEIVED:
10229 bbr->r_state = TCPS_SYN_RECEIVED;
10230 bbr->r_substate = bbr_do_syn_recv;
10231 break;
10232 case TCPS_ESTABLISHED:
10233 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd);
10234 bbr->r_state = TCPS_ESTABLISHED;
10235 bbr->r_substate = bbr_do_established;
10236 break;
10237 case TCPS_CLOSE_WAIT:
10238 bbr->r_state = TCPS_CLOSE_WAIT;
10239 bbr->r_substate = bbr_do_close_wait;
10240 break;
10241 case TCPS_FIN_WAIT_1:
10242 bbr->r_state = TCPS_FIN_WAIT_1;
10243 bbr->r_substate = bbr_do_fin_wait_1;
10244 break;
10245 case TCPS_CLOSING:
10246 bbr->r_state = TCPS_CLOSING;
10247 bbr->r_substate = bbr_do_closing;
10248 break;
10249 case TCPS_LAST_ACK:
10250 bbr->r_state = TCPS_LAST_ACK;
10251 bbr->r_substate = bbr_do_lastack;
10252 break;
10253 case TCPS_FIN_WAIT_2:
10254 bbr->r_state = TCPS_FIN_WAIT_2;
10255 bbr->r_substate = bbr_do_fin_wait_2;
10256 break;
10257 case TCPS_LISTEN:
10258 case TCPS_CLOSED:
10259 case TCPS_TIME_WAIT:
10260 default:
10261 break;
10262 };
10263 }
10264
10265 static void
bbr_substate_change(struct tcp_bbr * bbr,uint32_t cts,int32_t line,int dolog)10266 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog)
10267 {
10268 /*
10269 * Now what state are we going into now? Is there adjustments
10270 * needed?
10271 */
10272 int32_t old_state;
10273
10274 old_state = bbr_state_val(bbr);
10275 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) {
10276 /* Save the lowest srtt we saw in our end of the sub-state */
10277 bbr->rc_hit_state_1 = 0;
10278 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff)
10279 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state;
10280 }
10281 bbr->rc_bbr_substate++;
10282 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) {
10283 /* Cycle back to first state-> gain */
10284 bbr->rc_bbr_substate = 0;
10285 }
10286 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10287 /*
10288 * We enter the gain(5/4) cycle (possibly less if
10289 * shallow buffer detection is enabled)
10290 */
10291 if (bbr->skip_gain) {
10292 /*
10293 * Hardware pacing has set our rate to
10294 * the max and limited our b/w just
10295 * do level i.e. no gain.
10296 */
10297 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1];
10298 } else if (bbr->gain_is_limited &&
10299 bbr->bbr_hdrw_pacing &&
10300 bbr->r_ctl.crte) {
10301 /*
10302 * We can't gain above the hardware pacing
10303 * rate which is less than our rate + the gain
10304 * calculate the gain needed to reach the hardware
10305 * pacing rate..
10306 */
10307 uint64_t bw, rate, gain_calc;
10308
10309 bw = bbr_get_bw(bbr);
10310 rate = bbr->r_ctl.crte->rate;
10311 if ((rate > bw) &&
10312 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) {
10313 gain_calc = (rate * BBR_UNIT) / bw;
10314 if (gain_calc < BBR_UNIT)
10315 gain_calc = BBR_UNIT;
10316 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc;
10317 } else {
10318 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10319 }
10320 } else
10321 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10322 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) {
10323 bbr->r_ctl.rc_bbr_state_atflight = cts;
10324 } else
10325 bbr->r_ctl.rc_bbr_state_atflight = 0;
10326 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10327 bbr->rc_hit_state_1 = 1;
10328 bbr->r_ctl.rc_exta_time_gd = 0;
10329 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10330 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10331 if (bbr_state_drain_2_tar) {
10332 bbr->r_ctl.rc_bbr_state_atflight = 0;
10333 } else
10334 bbr->r_ctl.rc_bbr_state_atflight = cts;
10335 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN];
10336 } else {
10337 /* All other cycles hit here 2-7 */
10338 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) {
10339 if (bbr_sub_drain_slam_cwnd &&
10340 (bbr->rc_use_google == 0) &&
10341 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10342 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10343 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10344 }
10345 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP))
10346 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) -
10347 bbr_get_rtt(bbr, BBR_RTT_PROP));
10348 else
10349 bbr->r_ctl.rc_exta_time_gd = 0;
10350 if (bbr->r_ctl.rc_exta_time_gd) {
10351 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd;
10352 /* Now chop up the time for each state (div by 7) */
10353 bbr->r_ctl.rc_level_state_extra /= 7;
10354 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) {
10355 /* Add a randomization */
10356 bbr_randomize_extra_state_time(bbr);
10357 }
10358 }
10359 }
10360 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10361 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)];
10362 }
10363 if (bbr->rc_use_google) {
10364 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10365 }
10366 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10367 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10368 if (dolog)
10369 bbr_log_type_statechange(bbr, cts, line);
10370
10371 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10372 uint32_t time_in;
10373
10374 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10375 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
10376 counter_u64_add(bbr_state_time[(old_state + 5)], time_in);
10377 } else {
10378 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10379 }
10380 }
10381 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
10382 bbr_set_state_target(bbr, __LINE__);
10383 if (bbr_sub_drain_slam_cwnd &&
10384 (bbr->rc_use_google == 0) &&
10385 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10386 /* Slam down the cwnd */
10387 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10388 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10389 if (bbr_sub_drain_app_limit) {
10390 /* Go app limited if we are on a long drain */
10391 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered +
10392 ctf_flight_size(bbr->rc_tp,
10393 (bbr->r_ctl.rc_sacked +
10394 bbr->r_ctl.rc_lost_bytes)));
10395 }
10396 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10397 }
10398 if (bbr->rc_lt_use_bw) {
10399 /* In policed mode we clamp pacing_gain to BBR_UNIT */
10400 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10401 }
10402 /* Google changes TSO size every cycle */
10403 if (bbr->rc_use_google)
10404 tcp_bbr_tso_size_check(bbr, cts);
10405 bbr->r_ctl.gain_epoch = cts;
10406 bbr->r_ctl.rc_bbr_state_time = cts;
10407 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch;
10408 }
10409
10410 static void
bbr_set_probebw_google_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10411 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10412 {
10413 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) &&
10414 (google_allow_early_out == 1) &&
10415 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) {
10416 /* We have reached out target flight size possibly early */
10417 goto change_state;
10418 }
10419 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10420 return;
10421 }
10422 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) {
10423 /*
10424 * Must be a rttProp movement forward before
10425 * we can change states.
10426 */
10427 return;
10428 }
10429 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10430 /*
10431 * The needed time has passed but for
10432 * the gain cycle extra rules apply:
10433 * 1) If we have seen loss, we exit
10434 * 2) If we have not reached the target
10435 * we stay in GAIN (gain-to-target).
10436 */
10437 if (google_consider_lost && losses)
10438 goto change_state;
10439 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) {
10440 return;
10441 }
10442 }
10443 change_state:
10444 /* For gain we must reach our target, all others last 1 rttProp */
10445 bbr_substate_change(bbr, cts, __LINE__, 1);
10446 }
10447
10448 static void
bbr_set_probebw_gains(struct tcp_bbr * bbr,uint32_t cts,uint32_t losses)10449 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10450 {
10451 uint32_t flight, bbr_cur_cycle_time;
10452
10453 if (bbr->rc_use_google) {
10454 bbr_set_probebw_google_gains(bbr, cts, losses);
10455 return;
10456 }
10457 if (cts == 0) {
10458 /*
10459 * Never alow cts to be 0 we
10460 * do this so we can judge if
10461 * we have set a timestamp.
10462 */
10463 cts = 1;
10464 }
10465 if (bbr_state_is_pkt_epoch)
10466 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
10467 else
10468 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP);
10469
10470 if (bbr->r_ctl.rc_bbr_state_atflight == 0) {
10471 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10472 flight = ctf_flight_size(bbr->rc_tp,
10473 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10474 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) {
10475 /* Keep it slam down */
10476 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) {
10477 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10478 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10479 }
10480 if (bbr_sub_drain_app_limit) {
10481 /* Go app limited if we are on a long drain */
10482 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight);
10483 }
10484 }
10485 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) &&
10486 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) ||
10487 (flight >= bbr->r_ctl.flightsize_at_drain))) {
10488 /*
10489 * Still here after the same time as
10490 * the gain. We need to drain harder
10491 * for the next srtt. Reduce by a set amount
10492 * the gain drop is capped at DRAIN states
10493 * value (88).
10494 */
10495 bbr->r_ctl.flightsize_at_drain = flight;
10496 if (bbr_drain_drop_mul &&
10497 bbr_drain_drop_div &&
10498 (bbr_drain_drop_mul < bbr_drain_drop_div)) {
10499 /* Use your specific drop value (def 4/5 = 20%) */
10500 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul;
10501 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div;
10502 } else {
10503 /* You get drop of 20% */
10504 bbr->r_ctl.rc_bbr_hptsi_gain *= 4;
10505 bbr->r_ctl.rc_bbr_hptsi_gain /= 5;
10506 }
10507 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) {
10508 /* Reduce our gain again to the bottom */
10509 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
10510 }
10511 bbr_log_exit_gain(bbr, cts, 4);
10512 /*
10513 * Extend out so we wait another
10514 * epoch before dropping again.
10515 */
10516 bbr->r_ctl.gain_epoch = cts;
10517 }
10518 if (flight <= bbr->r_ctl.rc_target_at_state) {
10519 if (bbr_sub_drain_slam_cwnd &&
10520 (bbr->rc_use_google == 0) &&
10521 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10522 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10523 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10524 }
10525 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10526 bbr_log_exit_gain(bbr, cts, 3);
10527 }
10528 } else {
10529 /* Its a gain */
10530 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) {
10531 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10532 goto change_state;
10533 }
10534 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) ||
10535 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >=
10536 bbr->rc_tp->snd_wnd)) {
10537 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10538 bbr_log_exit_gain(bbr, cts, 2);
10539 }
10540 }
10541 /**
10542 * We fall through and return always one of two things has
10543 * occurred.
10544 * 1) We are still not at target
10545 * <or>
10546 * 2) We reached the target and set rc_bbr_state_atflight
10547 * which means we no longer hit this block
10548 * next time we are called.
10549 */
10550 return;
10551 }
10552 change_state:
10553 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time))
10554 return;
10555 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) {
10556 /* Less than a full time-period has passed */
10557 return;
10558 }
10559 if (bbr->r_ctl.rc_level_state_extra &&
10560 (bbr_state_val(bbr) > BBR_SUB_DRAIN) &&
10561 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10562 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10563 /* Less than a full time-period + extra has passed */
10564 return;
10565 }
10566 if (bbr_gain_gets_extra_too &&
10567 bbr->r_ctl.rc_level_state_extra &&
10568 (bbr_state_val(bbr) == BBR_SUB_GAIN) &&
10569 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10570 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10571 /* Less than a full time-period + extra has passed */
10572 return;
10573 }
10574 bbr_substate_change(bbr, cts, __LINE__, 1);
10575 }
10576
10577 static uint32_t
bbr_get_a_state_target(struct tcp_bbr * bbr,uint32_t gain)10578 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain)
10579 {
10580 uint32_t mss, tar;
10581
10582 if (bbr->rc_use_google) {
10583 /* Google just uses the cwnd target */
10584 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain);
10585 } else {
10586 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
10587 bbr->r_ctl.rc_pace_max_segs);
10588 /* Get the base cwnd with gain rounded to a mss */
10589 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr),
10590 gain), mss);
10591 /* Make sure it is within our min */
10592 if (tar < get_min_cwnd(bbr))
10593 return (get_min_cwnd(bbr));
10594 }
10595 return (tar);
10596 }
10597
10598 static void
bbr_set_state_target(struct tcp_bbr * bbr,int line)10599 bbr_set_state_target(struct tcp_bbr *bbr, int line)
10600 {
10601 uint32_t tar, meth;
10602
10603 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
10604 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
10605 /* Special case using old probe-rtt method */
10606 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10607 meth = 1;
10608 } else {
10609 /* Non-probe-rtt case and reduced probe-rtt */
10610 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
10611 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) {
10612 /* For gain cycle we use the hptsi gain */
10613 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10614 meth = 2;
10615 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) {
10616 /*
10617 * If configured, or for google all other states
10618 * get BBR_UNIT.
10619 */
10620 tar = bbr_get_a_state_target(bbr, BBR_UNIT);
10621 meth = 3;
10622 } else {
10623 /*
10624 * Or we set a target based on the pacing gain
10625 * for non-google mode and default (non-configured).
10626 * Note we don't set a target goal below drain (192).
10627 */
10628 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) {
10629 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]);
10630 meth = 4;
10631 } else {
10632 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10633 meth = 5;
10634 }
10635 }
10636 }
10637 bbr_log_set_of_state_target(bbr, tar, line, meth);
10638 bbr->r_ctl.rc_target_at_state = tar;
10639 }
10640
10641 static void
bbr_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts,int32_t line)10642 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
10643 {
10644 /* Change to probe_rtt */
10645 uint32_t time_in;
10646
10647 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10648 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10649 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10650 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain
10651 + bbr->r_ctl.rc_delivered);
10652 /* Setup so we force feed the filter */
10653 if (bbr->rc_use_google || bbr_probertt_sets_rtt)
10654 bbr->rc_prtt_set_ts = 1;
10655 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10656 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10657 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10658 }
10659 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0);
10660 bbr->r_ctl.rc_rtt_shrinks = cts;
10661 bbr->r_ctl.last_in_probertt = cts;
10662 bbr->r_ctl.rc_probertt_srttchktim = cts;
10663 bbr->r_ctl.rc_bbr_state_time = cts;
10664 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT;
10665 /* We need to force the filter to update */
10666
10667 if ((bbr_sub_drain_slam_cwnd) &&
10668 bbr->rc_hit_state_1 &&
10669 (bbr->rc_use_google == 0) &&
10670 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10671 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd)
10672 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10673 } else
10674 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10675 /* Update the lost */
10676 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10677 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){
10678 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */
10679 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10680 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10681 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10682 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10683 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6);
10684 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd;
10685 } else {
10686 /*
10687 * We bring it down slowly by using a hptsi gain that is
10688 * probably 75%. This will slowly float down our outstanding
10689 * without tampering with the cwnd.
10690 */
10691 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
10692 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10693 bbr_set_state_target(bbr, __LINE__);
10694 if (bbr_prtt_slam_cwnd &&
10695 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
10696 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10697 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10698 }
10699 }
10700 if (ctf_flight_size(bbr->rc_tp,
10701 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
10702 bbr->r_ctl.rc_target_at_state) {
10703 /* We are at target */
10704 bbr->r_ctl.rc_bbr_enters_probertt = cts;
10705 } else {
10706 /* We need to come down to reach target before our time begins */
10707 bbr->r_ctl.rc_bbr_enters_probertt = 0;
10708 }
10709 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch;
10710 BBR_STAT_INC(bbr_enter_probertt);
10711 bbr_log_exit_gain(bbr, cts, 0);
10712 bbr_log_type_statechange(bbr, cts, line);
10713 }
10714
10715 static void
bbr_check_probe_rtt_limits(struct tcp_bbr * bbr,uint32_t cts)10716 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts)
10717 {
10718 /*
10719 * Sanity check on probe-rtt intervals.
10720 * In crazy situations where we are competing
10721 * against new-reno flows with huge buffers
10722 * our rtt-prop interval could come to dominate
10723 * things if we can't get through a full set
10724 * of cycles, we need to adjust it.
10725 */
10726 if (bbr_can_adjust_probertt &&
10727 (bbr->rc_use_google == 0)) {
10728 uint16_t val = 0;
10729 uint32_t cur_rttp, fval, newval, baseval;
10730
10731 /* Are we to small and go into probe-rtt to often? */
10732 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1));
10733 cur_rttp = roundup(baseval, USECS_IN_SECOND);
10734 fval = bbr_filter_len_sec * USECS_IN_SECOND;
10735 if (bbr_is_ratio == 0) {
10736 if (fval > bbr_rtt_probe_limit)
10737 newval = cur_rttp + (fval - bbr_rtt_probe_limit);
10738 else
10739 newval = cur_rttp;
10740 } else {
10741 int mul;
10742
10743 mul = fval / bbr_rtt_probe_limit;
10744 newval = cur_rttp * mul;
10745 }
10746 if (cur_rttp > bbr->r_ctl.rc_probertt_int) {
10747 bbr->r_ctl.rc_probertt_int = cur_rttp;
10748 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10749 val = 1;
10750 } else {
10751 /*
10752 * No adjustments were made
10753 * do we need to shrink it?
10754 */
10755 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) {
10756 if (cur_rttp <= bbr_rtt_probe_limit) {
10757 /*
10758 * Things have calmed down lets
10759 * shrink all the way to default
10760 */
10761 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10762 reset_time_small(&bbr->r_ctl.rc_rttprop,
10763 (bbr_filter_len_sec * USECS_IN_SECOND));
10764 cur_rttp = bbr_rtt_probe_limit;
10765 newval = (bbr_filter_len_sec * USECS_IN_SECOND);
10766 val = 2;
10767 } else {
10768 /*
10769 * Well does some adjustment make sense?
10770 */
10771 if (cur_rttp < bbr->r_ctl.rc_probertt_int) {
10772 /* We can reduce interval time some */
10773 bbr->r_ctl.rc_probertt_int = cur_rttp;
10774 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10775 val = 3;
10776 }
10777 }
10778 }
10779 }
10780 if (val)
10781 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val);
10782 }
10783 }
10784
10785 static void
bbr_exit_probe_rtt(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t cts)10786 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
10787 {
10788 /* Exit probe-rtt */
10789
10790 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) {
10791 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10792 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10793 }
10794 bbr_log_exit_gain(bbr, cts, 1);
10795 bbr->rc_hit_state_1 = 0;
10796 bbr->r_ctl.rc_rtt_shrinks = cts;
10797 bbr->r_ctl.last_in_probertt = cts;
10798 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0);
10799 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10800 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp,
10801 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
10802 bbr->r_ctl.rc_delivered);
10803 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10804 uint32_t time_in;
10805
10806 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10807 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10808 }
10809 if (bbr->rc_filled_pipe) {
10810 /* Switch to probe_bw */
10811 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
10812 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
10813 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10814 bbr_substate_change(bbr, cts, __LINE__, 0);
10815 bbr_log_type_statechange(bbr, cts, __LINE__);
10816 } else {
10817 /* Back to startup */
10818 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10819 bbr->r_ctl.rc_bbr_state_time = cts;
10820 /*
10821 * We don't want to give a complete free 3
10822 * measurements until we exit, so we use
10823 * the number of pe's we were in probe-rtt
10824 * to add to the startup_epoch. That way
10825 * we will still retain the old state.
10826 */
10827 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt);
10828 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10829 /* Make sure to use the lower pg when shifting back in */
10830 if (bbr->r_ctl.rc_lost &&
10831 bbr_use_lower_gain_in_startup &&
10832 (bbr->rc_use_google == 0))
10833 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
10834 else
10835 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
10836 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
10837 /* Probably not needed but set it anyway */
10838 bbr_set_state_target(bbr, __LINE__);
10839 bbr_log_type_statechange(bbr, cts, __LINE__);
10840 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10841 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0);
10842 }
10843 bbr_check_probe_rtt_limits(bbr, cts);
10844 }
10845
10846 static int32_t inline
bbr_should_enter_probe_rtt(struct tcp_bbr * bbr,uint32_t cts)10847 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts)
10848 {
10849 if ((bbr->rc_past_init_win == 1) &&
10850 (bbr->rc_in_persist == 0) &&
10851 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) {
10852 return (1);
10853 }
10854 if (bbr_can_force_probertt &&
10855 (bbr->rc_in_persist == 0) &&
10856 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
10857 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
10858 return (1);
10859 }
10860 return (0);
10861 }
10862
10863 static int32_t
bbr_google_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t pkt_epoch)10864 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch)
10865 {
10866 uint64_t btlbw, gain;
10867 if (pkt_epoch == 0) {
10868 /*
10869 * Need to be on a pkt-epoch to continue.
10870 */
10871 return (0);
10872 }
10873 btlbw = bbr_get_full_bw(bbr);
10874 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
10875 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
10876 if (btlbw >= gain) {
10877 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
10878 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10879 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
10880 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
10881 }
10882 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)
10883 return (1);
10884 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10885 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
10886 return(0);
10887 }
10888
10889 static int32_t inline
bbr_state_startup(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch)10890 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch)
10891 {
10892 /* Have we gained 25% in the last 3 packet based epoch's? */
10893 uint64_t btlbw, gain;
10894 int do_exit;
10895 int delta, rtt_gain;
10896
10897 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
10898 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
10899 /*
10900 * This qualifies as a RTT_PROBE session since we drop the
10901 * data outstanding to nothing and waited more than
10902 * bbr_rtt_probe_time.
10903 */
10904 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
10905 bbr_set_reduced_rtt(bbr, cts, __LINE__);
10906 }
10907 if (bbr_should_enter_probe_rtt(bbr, cts)) {
10908 bbr_enter_probe_rtt(bbr, cts, __LINE__);
10909 return (0);
10910 }
10911 if (bbr->rc_use_google)
10912 return (bbr_google_startup(bbr, cts, pkt_epoch));
10913
10914 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
10915 (bbr_use_lower_gain_in_startup)) {
10916 /* Drop to a lower gain 1.5 x since we saw loss */
10917 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
10918 }
10919 if (pkt_epoch == 0) {
10920 /*
10921 * Need to be on a pkt-epoch to continue.
10922 */
10923 return (0);
10924 }
10925 if (bbr_rtt_gain_thresh) {
10926 /*
10927 * Do we allow a flow to stay
10928 * in startup with no loss and no
10929 * gain in rtt over a set threshold?
10930 */
10931 if (bbr->r_ctl.rc_pkt_epoch_rtt &&
10932 bbr->r_ctl.startup_last_srtt &&
10933 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) {
10934 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt;
10935 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt;
10936 } else
10937 rtt_gain = 0;
10938 if ((bbr->r_ctl.startup_last_srtt == 0) ||
10939 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt))
10940 /* First time or new lower value */
10941 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
10942
10943 if ((bbr->r_ctl.rc_lost == 0) &&
10944 (rtt_gain < bbr_rtt_gain_thresh)) {
10945 /*
10946 * No loss, and we are under
10947 * our gain threhold for
10948 * increasing RTT.
10949 */
10950 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
10951 bbr->r_ctl.rc_bbr_last_startup_epoch++;
10952 bbr_log_startup_event(bbr, cts, rtt_gain,
10953 delta, bbr->r_ctl.startup_last_srtt, 10);
10954 return (0);
10955 }
10956 }
10957 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) &&
10958 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) &&
10959 (!IN_RECOVERY(bbr->rc_tp->t_flags))) {
10960 /*
10961 * We only assess if we have a new measurement when
10962 * we have no loss and are not in recovery.
10963 * Drag up by one our last_startup epoch so we will hold
10964 * the number of non-gain we have already accumulated.
10965 */
10966 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
10967 bbr->r_ctl.rc_bbr_last_startup_epoch++;
10968 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10969 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9);
10970 return (0);
10971 }
10972 /* Case where we reduced the lost (bad retransmit) */
10973 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost)
10974 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10975 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count;
10976 btlbw = bbr_get_full_bw(bbr);
10977 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower)
10978 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
10979 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
10980 else
10981 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
10982 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
10983 do_exit = 0;
10984 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw)
10985 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
10986 if (btlbw >= gain) {
10987 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
10988 /* Update the lost so we won't exit in next set of tests */
10989 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10990 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
10991 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
10992 }
10993 if ((bbr->rc_loss_exit &&
10994 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
10995 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) &&
10996 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) {
10997 /*
10998 * If we had no gain, we had loss and that loss was above
10999 * our threshould, the rwnd is not constrained, and we have
11000 * had at least 3 packet epochs exit. Note that this is
11001 * switched off by sysctl. Google does not do this by the
11002 * way.
11003 */
11004 if ((ctf_flight_size(bbr->rc_tp,
11005 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11006 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) {
11007 do_exit = 1;
11008 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11009 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4);
11010 } else {
11011 /* Just record an updated loss value */
11012 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11013 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11014 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5);
11015 }
11016 } else
11017 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11018 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) ||
11019 do_exit) {
11020 /* Return 1 to exit the startup state. */
11021 return (1);
11022 }
11023 /* Stay in startup */
11024 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11025 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11026 return (0);
11027 }
11028
11029 static void
bbr_state_change(struct tcp_bbr * bbr,uint32_t cts,int32_t epoch,int32_t pkt_epoch,uint32_t losses)11030 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses)
11031 {
11032 /*
11033 * A tick occurred in the rtt epoch do we need to do anything?
11034 */
11035 #ifdef BBR_INVARIANTS
11036 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
11037 (bbr->rc_bbr_state != BBR_STATE_DRAIN) &&
11038 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) &&
11039 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
11040 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) {
11041 /* Debug code? */
11042 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state);
11043 }
11044 #endif
11045 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
11046 /* Do we exit the startup state? */
11047 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) {
11048 uint32_t time_in;
11049
11050 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11051 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6);
11052 bbr->rc_filled_pipe = 1;
11053 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11054 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11055 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11056 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11057 } else
11058 time_in = 0;
11059 if (bbr->rc_no_pacing)
11060 bbr->rc_no_pacing = 0;
11061 bbr->r_ctl.rc_bbr_state_time = cts;
11062 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg;
11063 bbr->rc_bbr_state = BBR_STATE_DRAIN;
11064 bbr_set_state_target(bbr, __LINE__);
11065 if ((bbr->rc_use_google == 0) &&
11066 bbr_slam_cwnd_in_main_drain) {
11067 /* Here we don't have to worry about probe-rtt */
11068 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
11069 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11070 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11071 }
11072 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
11073 bbr_log_type_statechange(bbr, cts, __LINE__);
11074 if (ctf_flight_size(bbr->rc_tp,
11075 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
11076 bbr->r_ctl.rc_target_at_state) {
11077 /*
11078 * Switch to probe_bw if we are already
11079 * there
11080 */
11081 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11082 bbr_substate_change(bbr, cts, __LINE__, 0);
11083 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11084 bbr_log_type_statechange(bbr, cts, __LINE__);
11085 }
11086 }
11087 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) {
11088 uint32_t inflight;
11089 struct tcpcb *tp;
11090
11091 tp = bbr->rc_tp;
11092 inflight = ctf_flight_size(tp,
11093 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11094 if (inflight >= bbr->r_ctl.rc_target_at_state) {
11095 /* We have reached a flight of the cwnd target */
11096 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11097 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11098 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
11099 bbr_set_state_target(bbr, __LINE__);
11100 /*
11101 * Rig it so we don't do anything crazy and
11102 * start fresh with a new randomization.
11103 */
11104 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
11105 bbr->rc_bbr_substate = BBR_SUB_LEVEL6;
11106 bbr_substate_change(bbr, cts, __LINE__, 1);
11107 }
11108 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) {
11109 /* Has in-flight reached the bdp (or less)? */
11110 uint32_t inflight;
11111 struct tcpcb *tp;
11112
11113 tp = bbr->rc_tp;
11114 inflight = ctf_flight_size(tp,
11115 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11116 if ((bbr->rc_use_google == 0) &&
11117 bbr_slam_cwnd_in_main_drain &&
11118 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11119 /*
11120 * Here we don't have to worry about probe-rtt
11121 * re-slam it, but keep it slammed down.
11122 */
11123 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11124 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11125 }
11126 if (inflight <= bbr->r_ctl.rc_target_at_state) {
11127 /* We have drained */
11128 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11129 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11130 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11131 uint32_t time_in;
11132
11133 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11134 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11135 }
11136 if ((bbr->rc_use_google == 0) &&
11137 bbr_slam_cwnd_in_main_drain &&
11138 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
11139 /* Restore the cwnd */
11140 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11141 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11142 }
11143 /* Setup probe-rtt has being done now RRS-HERE */
11144 bbr->r_ctl.rc_rtt_shrinks = cts;
11145 bbr->r_ctl.last_in_probertt = cts;
11146 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0);
11147 /* Randomly pick a sub-state */
11148 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11149 bbr_substate_change(bbr, cts, __LINE__, 0);
11150 bbr_log_type_statechange(bbr, cts, __LINE__);
11151 }
11152 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) {
11153 uint32_t flight;
11154
11155 flight = ctf_flight_size(bbr->rc_tp,
11156 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11157 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered);
11158 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) &&
11159 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11160 /*
11161 * We must keep cwnd at the desired MSS.
11162 */
11163 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
11164 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11165 } else if ((bbr_prtt_slam_cwnd) &&
11166 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11167 /* Re-slam it */
11168 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11169 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11170 }
11171 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) {
11172 /* Has outstanding reached our target? */
11173 if (flight <= bbr->r_ctl.rc_target_at_state) {
11174 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0);
11175 bbr->r_ctl.rc_bbr_enters_probertt = cts;
11176 /* If time is exactly 0, be 1usec off */
11177 if (bbr->r_ctl.rc_bbr_enters_probertt == 0)
11178 bbr->r_ctl.rc_bbr_enters_probertt = 1;
11179 if (bbr->rc_use_google == 0) {
11180 /*
11181 * Restore any lowering that as occurred to
11182 * reach here
11183 */
11184 if (bbr->r_ctl.bbr_rttprobe_gain_val)
11185 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
11186 else
11187 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11188 }
11189 }
11190 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) &&
11191 (bbr->rc_use_google == 0) &&
11192 bbr->r_ctl.bbr_rttprobe_gain_val &&
11193 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) ||
11194 (flight >= bbr->r_ctl.flightsize_at_drain))) {
11195 /*
11196 * We have doddled with our current hptsi
11197 * gain an srtt and have still not made it
11198 * to target, or we have increased our flight.
11199 * Lets reduce the gain by xx%
11200 * flooring the reduce at DRAIN (based on
11201 * mul/div)
11202 */
11203 int red;
11204
11205 bbr->r_ctl.flightsize_at_drain = flight;
11206 bbr->r_ctl.rc_probertt_srttchktim = cts;
11207 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1);
11208 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) {
11209 /* Reduce our gain again */
11210 bbr->r_ctl.rc_bbr_hptsi_gain -= red;
11211 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0);
11212 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) {
11213 /* one more chance before we give up */
11214 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
11215 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0);
11216 } else {
11217 /* At the very bottom */
11218 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1);
11219 }
11220 }
11221 }
11222 if (bbr->r_ctl.rc_bbr_enters_probertt &&
11223 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) &&
11224 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) {
11225 /* Time to exit probe RTT normally */
11226 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts);
11227 }
11228 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
11229 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11230 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11231 /*
11232 * This qualifies as a RTT_PROBE session since we
11233 * drop the data outstanding to nothing and waited
11234 * more than bbr_rtt_probe_time.
11235 */
11236 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11237 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11238 }
11239 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11240 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11241 } else {
11242 bbr_set_probebw_gains(bbr, cts, losses);
11243 }
11244 }
11245 }
11246
11247 static void
bbr_check_bbr_for_state(struct tcp_bbr * bbr,uint32_t cts,int32_t line,uint32_t losses)11248 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses)
11249 {
11250 int32_t epoch = 0;
11251
11252 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
11253 bbr_set_epoch(bbr, cts, line);
11254 /* At each epoch doe lt bw sampling */
11255 epoch = 1;
11256 }
11257 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses);
11258 }
11259
11260 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)11261 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
11262 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
11263 struct timeval *tv)
11264 {
11265 struct inpcb *inp = tptoinpcb(tp);
11266 struct socket *so = tptosocket(tp);
11267 int32_t thflags, retval;
11268 uint32_t cts, lcts;
11269 uint32_t tiwin;
11270 struct tcpopt to;
11271 struct tcp_bbr *bbr;
11272 struct bbr_sendmap *rsm;
11273 struct timeval ltv;
11274 int32_t did_out = 0;
11275 uint16_t nsegs;
11276 int32_t prev_state;
11277 uint32_t lost;
11278
11279 nsegs = max(1, m->m_pkthdr.lro_nsegs);
11280 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11281 /* add in our stats */
11282 kern_prefetch(bbr, &prev_state);
11283 prev_state = 0;
11284 thflags = tcp_get_flags(th);
11285 /*
11286 * If this is either a state-changing packet or current state isn't
11287 * established, we require a write lock on tcbinfo. Otherwise, we
11288 * allow the tcbinfo to be in either alocked or unlocked, as the
11289 * caller may have unnecessarily acquired a write lock due to a
11290 * race.
11291 */
11292 INP_WLOCK_ASSERT(tptoinpcb(tp));
11293 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
11294 __func__));
11295 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
11296 __func__));
11297
11298 tp->t_rcvtime = ticks;
11299 /*
11300 * Unscale the window into a 32-bit value. For the SYN_SENT state
11301 * the scale is zero.
11302 */
11303 tiwin = th->th_win << tp->snd_scale;
11304 #ifdef STATS
11305 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
11306 #endif
11307
11308 if (m->m_flags & M_TSTMP) {
11309 /* Prefer the hardware timestamp if present */
11310 struct timespec ts;
11311
11312 mbuf_tstmp2timespec(m, &ts);
11313 bbr->rc_tv.tv_sec = ts.tv_sec;
11314 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11315 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11316 } else if (m->m_flags & M_TSTMP_LRO) {
11317 /* Next the arrival timestamp */
11318 struct timespec ts;
11319
11320 mbuf_tstmp2timespec(m, &ts);
11321 bbr->rc_tv.tv_sec = ts.tv_sec;
11322 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11323 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11324 } else {
11325 /*
11326 * Ok just get the current time.
11327 */
11328 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv);
11329 }
11330 /*
11331 * Parse options on any incoming segment.
11332 */
11333 tcp_dooptions(&to, (u_char *)(th + 1),
11334 (th->th_off << 2) - sizeof(struct tcphdr),
11335 (thflags & TH_SYN) ? TO_SYN : 0);
11336 if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) {
11337 /*
11338 * We don't look at sack's from the
11339 * peer because the MSS is too small which
11340 * can subject us to an attack.
11341 */
11342 to.to_flags &= ~TOF_SACK;
11343 }
11344 /*
11345 * If timestamps were negotiated during SYN/ACK and a
11346 * segment without a timestamp is received, silently drop
11347 * the segment, unless it is a RST segment or missing timestamps are
11348 * tolerated.
11349 * See section 3.2 of RFC 7323.
11350 */
11351 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
11352 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
11353 retval = 0;
11354 m_freem(m);
11355 goto done_with_input;
11356 }
11357 /*
11358 * If echoed timestamp is later than the current time, fall back to
11359 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
11360 * were used when this connection was established.
11361 */
11362 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
11363 to.to_tsecr -= tp->ts_offset;
11364 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv)))
11365 to.to_tsecr = 0;
11366 }
11367 /*
11368 * If its the first time in we need to take care of options and
11369 * verify we can do SACK for rack!
11370 */
11371 if (bbr->r_state == 0) {
11372 /*
11373 * Process options only when we get SYN/ACK back. The SYN
11374 * case for incoming connections is handled in tcp_syncache.
11375 * According to RFC1323 the window field in a SYN (i.e., a
11376 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
11377 * this is traditional behavior, may need to be cleaned up.
11378 */
11379 if (bbr->rc_inp == NULL) {
11380 bbr->rc_inp = inp;
11381 }
11382 /*
11383 * We need to init rc_inp here since its not init'd when
11384 * bbr_init is called
11385 */
11386 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
11387 if ((to.to_flags & TOF_SCALE) &&
11388 (tp->t_flags & TF_REQ_SCALE)) {
11389 tp->t_flags |= TF_RCVD_SCALE;
11390 tp->snd_scale = to.to_wscale;
11391 } else
11392 tp->t_flags &= ~TF_REQ_SCALE;
11393 /*
11394 * Initial send window. It will be updated with the
11395 * next incoming segment to the scaled value.
11396 */
11397 tp->snd_wnd = th->th_win;
11398 if ((to.to_flags & TOF_TS) &&
11399 (tp->t_flags & TF_REQ_TSTMP)) {
11400 tp->t_flags |= TF_RCVD_TSTMP;
11401 tp->ts_recent = to.to_tsval;
11402 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
11403 } else
11404 tp->t_flags &= ~TF_REQ_TSTMP;
11405 if (to.to_flags & TOF_MSS)
11406 tcp_mss(tp, to.to_mss);
11407 if ((tp->t_flags & TF_SACK_PERMIT) &&
11408 (to.to_flags & TOF_SACKPERM) == 0)
11409 tp->t_flags &= ~TF_SACK_PERMIT;
11410 if (tp->t_flags & TF_FASTOPEN) {
11411 if (to.to_flags & TOF_FASTOPEN) {
11412 uint16_t mss;
11413
11414 if (to.to_flags & TOF_MSS)
11415 mss = to.to_mss;
11416 else
11417 if ((inp->inp_vflag & INP_IPV6) != 0)
11418 mss = TCP6_MSS;
11419 else
11420 mss = TCP_MSS;
11421 tcp_fastopen_update_cache(tp, mss,
11422 to.to_tfo_len, to.to_tfo_cookie);
11423 } else
11424 tcp_fastopen_disable_path(tp);
11425 }
11426 }
11427 /*
11428 * At this point we are at the initial call. Here we decide
11429 * if we are doing RACK or not. We do this by seeing if
11430 * TF_SACK_PERMIT is set, if not rack is *not* possible and
11431 * we switch to the default code.
11432 */
11433 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
11434 /* Bail */
11435 tcp_switch_back_to_default(tp);
11436 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
11437 tlen, iptos);
11438 return (1);
11439 }
11440 /* Set the flag */
11441 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
11442 tcp_set_hpts(tp);
11443 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack);
11444 }
11445 if (thflags & TH_ACK) {
11446 /* Track ack types */
11447 if (to.to_flags & TOF_SACK)
11448 BBR_STAT_INC(bbr_acks_with_sacks);
11449 else
11450 BBR_STAT_INC(bbr_plain_acks);
11451 }
11452 /*
11453 * This is the one exception case where we set the rack state
11454 * always. All other times (timers etc) we must have a rack-state
11455 * set (so we assure we have done the checks above for SACK).
11456 */
11457 if (thflags & TH_FIN)
11458 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
11459 if (bbr->r_state != tp->t_state)
11460 bbr_set_state(tp, bbr, tiwin);
11461
11462 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL)
11463 kern_prefetch(rsm, &prev_state);
11464 prev_state = bbr->r_state;
11465 bbr->rc_ack_was_delayed = 0;
11466 lost = bbr->r_ctl.rc_lost;
11467 bbr->rc_is_pkt_epoch_now = 0;
11468 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) {
11469 /* Get the real time into lcts and figure the real delay */
11470 lcts = tcp_get_usecs(<v);
11471 if (TSTMP_GT(lcts, cts)) {
11472 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts;
11473 bbr->rc_ack_was_delayed = 1;
11474 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay,
11475 bbr->r_ctl.highest_hdwr_delay))
11476 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay;
11477 } else {
11478 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11479 bbr->rc_ack_was_delayed = 0;
11480 }
11481 } else {
11482 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11483 bbr->rc_ack_was_delayed = 0;
11484 }
11485 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m);
11486 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
11487 retval = 0;
11488 m_freem(m);
11489 goto done_with_input;
11490 }
11491 /*
11492 * If a segment with the ACK-bit set arrives in the SYN-SENT state
11493 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
11494 */
11495 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
11496 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
11497 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11498 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11499 return (1);
11500 }
11501 if (tiwin > bbr->r_ctl.rc_high_rwnd)
11502 bbr->r_ctl.rc_high_rwnd = tiwin;
11503 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp,
11504 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11505 bbr->rtt_valid = 0;
11506 if (to.to_flags & TOF_TS) {
11507 bbr->rc_ts_valid = 1;
11508 bbr->r_ctl.last_inbound_ts = to.to_tsval;
11509 } else {
11510 bbr->rc_ts_valid = 0;
11511 bbr->r_ctl.last_inbound_ts = 0;
11512 }
11513 retval = (*bbr->r_substate) (m, th, so,
11514 tp, &to, drop_hdrlen,
11515 tlen, tiwin, thflags, nxt_pkt, iptos);
11516 if (nxt_pkt == 0)
11517 BBR_STAT_INC(bbr_rlock_left_ret0);
11518 else
11519 BBR_STAT_INC(bbr_rlock_left_ret1);
11520 if (retval == 0) {
11521 /*
11522 * If retval is 1 the tcb is unlocked and most likely the tp
11523 * is gone.
11524 */
11525 INP_WLOCK_ASSERT(inp);
11526 tcp_bbr_xmit_timer_commit(bbr, tp, cts);
11527 if (bbr->rc_is_pkt_epoch_now)
11528 bbr_set_pktepoch(bbr, cts, __LINE__);
11529 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost));
11530 if (nxt_pkt == 0) {
11531 if ((bbr->r_wanted_output != 0) ||
11532 (tp->t_flags & TF_ACKNOW)) {
11533
11534 bbr->rc_output_starts_timer = 0;
11535 did_out = 1;
11536 if (tcp_output(tp) < 0)
11537 return (1);
11538 } else
11539 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0);
11540 }
11541 if ((nxt_pkt == 0) &&
11542 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11543 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11544 (tp->t_flags & TF_DELACK) ||
11545 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11546 (tp->t_state <= TCPS_CLOSING)))) {
11547 /*
11548 * We could not send (probably in the hpts but
11549 * stopped the timer)?
11550 */
11551 if ((tp->snd_max == tp->snd_una) &&
11552 ((tp->t_flags & TF_DELACK) == 0) &&
11553 (tcp_in_hpts(tp)) &&
11554 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11555 /*
11556 * keep alive not needed if we are hptsi
11557 * output yet
11558 */
11559 ;
11560 } else {
11561 if (tcp_in_hpts(tp)) {
11562 tcp_hpts_remove(tp);
11563 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11564 (TSTMP_GT(lcts, bbr->rc_pacer_started))) {
11565 uint32_t del;
11566
11567 del = lcts - bbr->rc_pacer_started;
11568 if (bbr->r_ctl.rc_last_delay_val > del) {
11569 BBR_STAT_INC(bbr_force_timer_start);
11570 bbr->r_ctl.rc_last_delay_val -= del;
11571 bbr->rc_pacer_started = lcts;
11572 } else {
11573 /* We are late */
11574 bbr->r_ctl.rc_last_delay_val = 0;
11575 BBR_STAT_INC(bbr_force_output);
11576 if (tcp_output(tp) < 0)
11577 return (1);
11578 }
11579 }
11580 }
11581 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val,
11582 0);
11583 }
11584 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) {
11585 /* Do we have the correct timer running? */
11586 bbr_timer_audit(tp, bbr, lcts, &so->so_snd);
11587 }
11588 /* Clear the flag, it may have been cleared by output but we may not have */
11589 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
11590 tp->t_flags2 &= ~TF2_HPTS_CALLS;
11591 /* Do we have a new state */
11592 if (bbr->r_state != tp->t_state)
11593 bbr_set_state(tp, bbr, tiwin);
11594 done_with_input:
11595 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out);
11596 if (did_out)
11597 bbr->r_wanted_output = 0;
11598 }
11599 return (retval);
11600 }
11601
11602 static void
bbr_do_segment(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos)11603 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
11604 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11605 {
11606 struct timeval tv;
11607 int retval;
11608
11609 /* First lets see if we have old packets */
11610 if (!STAILQ_EMPTY(&tp->t_inqueue)) {
11611 if (ctf_do_queued_segments(tp, 1)) {
11612 m_freem(m);
11613 return;
11614 }
11615 }
11616 if (m->m_flags & M_TSTMP_LRO) {
11617 mbuf_tstmp2timeval(m, &tv);
11618 } else {
11619 /* Should not be should we kassert instead? */
11620 tcp_get_usecs(&tv);
11621 }
11622 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos,
11623 0, &tv);
11624 if (retval == 0) {
11625 INP_WUNLOCK(tptoinpcb(tp));
11626 }
11627 }
11628
11629 /*
11630 * Return how much data can be sent without violating the
11631 * cwnd or rwnd.
11632 */
11633
11634 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)11635 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin,
11636 uint32_t avail, int32_t sb_offset, uint32_t cts)
11637 {
11638 uint32_t len;
11639
11640 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11641 /* We never want to go over our peers rcv-window */
11642 len = 0;
11643 } else {
11644 uint32_t flight;
11645
11646 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11647 if (flight >= sendwin) {
11648 /*
11649 * We have in flight what we are allowed by cwnd (if
11650 * it was rwnd blocking it would have hit above out
11651 * >= tp->snd_wnd).
11652 */
11653 return (0);
11654 }
11655 len = sendwin - flight;
11656 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11657 /* We would send too much (beyond the rwnd) */
11658 len = tp->snd_wnd - ctf_outstanding(tp);
11659 }
11660 if ((len + sb_offset) > avail) {
11661 /*
11662 * We don't have that much in the SB, how much is
11663 * there?
11664 */
11665 len = avail - sb_offset;
11666 }
11667 }
11668 return (len);
11669 }
11670
11671 static inline void
bbr_do_send_accounting(struct tcpcb * tp,struct tcp_bbr * bbr,struct bbr_sendmap * rsm,int32_t len,int32_t error)11672 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11673 {
11674 if (error) {
11675 return;
11676 }
11677 if (rsm) {
11678 if (rsm->r_flags & BBR_TLP) {
11679 /*
11680 * TLP should not count in retran count, but in its
11681 * own bin
11682 */
11683 KMOD_TCPSTAT_INC(tcps_tlpresends);
11684 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len);
11685 } else {
11686 /* Retransmit */
11687 tp->t_sndrexmitpack++;
11688 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
11689 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
11690 #ifdef STATS
11691 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
11692 len);
11693 #endif
11694 }
11695 /*
11696 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is
11697 * sub-state
11698 */
11699 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len);
11700 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) {
11701 /* Non probe_bw log in 1, 2, or 4. */
11702 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len);
11703 } else {
11704 /*
11705 * Log our probe state 3, and log also 5-13 to show
11706 * us the recovery sub-state for the send. This
11707 * means that 3 == (5+6+7+8+9+10+11+12+13)
11708 */
11709 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len);
11710 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len);
11711 }
11712 /* Place in both 16's the totals of retransmitted */
11713 counter_u64_add(bbr_state_lost[16], len);
11714 counter_u64_add(bbr_state_resend[16], len);
11715 /* Place in 17's the total sent */
11716 counter_u64_add(bbr_state_resend[17], len);
11717 counter_u64_add(bbr_state_lost[17], len);
11718
11719 } else {
11720 /* New sends */
11721 KMOD_TCPSTAT_INC(tcps_sndpack);
11722 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
11723 /* Place in 17's the total sent */
11724 counter_u64_add(bbr_state_resend[17], len);
11725 counter_u64_add(bbr_state_lost[17], len);
11726 #ifdef STATS
11727 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
11728 len);
11729 #endif
11730 }
11731 }
11732
11733 static void
bbr_cwnd_limiting(struct tcpcb * tp,struct tcp_bbr * bbr,uint32_t in_level)11734 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level)
11735 {
11736 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) {
11737 /*
11738 * Limit the cwnd to not be above N x the target plus whats
11739 * is outstanding. The target is based on the current b/w
11740 * estimate.
11741 */
11742 uint32_t target;
11743
11744 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT);
11745 target += ctf_outstanding(tp);
11746 target *= bbr_target_cwnd_mult_limit;
11747 if (tp->snd_cwnd > target)
11748 tp->snd_cwnd = target;
11749 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__);
11750 }
11751 }
11752
11753 static int
bbr_window_update_needed(struct tcpcb * tp,struct socket * so,uint32_t recwin,int32_t maxseg)11754 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg)
11755 {
11756 /*
11757 * "adv" is the amount we could increase the window, taking into
11758 * account that we are limited by TCP_MAXWIN << tp->rcv_scale.
11759 */
11760 int32_t adv;
11761 int32_t oldwin;
11762
11763 adv = recwin;
11764 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
11765 oldwin = (tp->rcv_adv - tp->rcv_nxt);
11766 if (adv > oldwin)
11767 adv -= oldwin;
11768 else {
11769 /* We can't increase the window */
11770 adv = 0;
11771 }
11772 } else
11773 oldwin = 0;
11774
11775 /*
11776 * If the new window size ends up being the same as or less
11777 * than the old size when it is scaled, then don't force
11778 * a window update.
11779 */
11780 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
11781 return (0);
11782
11783 if (adv >= (2 * maxseg) &&
11784 (adv >= (so->so_rcv.sb_hiwat / 4) ||
11785 recwin <= (so->so_rcv.sb_hiwat / 8) ||
11786 so->so_rcv.sb_hiwat <= 8 * maxseg)) {
11787 return (1);
11788 }
11789 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
11790 return (1);
11791 return (0);
11792 }
11793
11794 /*
11795 * Return 0 on success and a errno on failure to send.
11796 * Note that a 0 return may not mean we sent anything
11797 * if the TCB was on the hpts. A non-zero return
11798 * does indicate the error we got from ip[6]_output.
11799 */
11800 static int
bbr_output_wtime(struct tcpcb * tp,const struct timeval * tv)11801 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv)
11802 {
11803 struct socket *so;
11804 int32_t len;
11805 uint32_t cts;
11806 uint32_t recwin, sendwin;
11807 int32_t sb_offset;
11808 int32_t flags, abandon, error = 0;
11809 struct tcp_log_buffer *lgb;
11810 struct mbuf *m;
11811 struct mbuf *mb;
11812 uint32_t if_hw_tsomaxsegcount = 0;
11813 uint32_t if_hw_tsomaxsegsize = 0;
11814 uint32_t if_hw_tsomax = 0;
11815 struct ip *ip = NULL;
11816 struct tcp_bbr *bbr;
11817 struct tcphdr *th;
11818 struct udphdr *udp = NULL;
11819 u_char opt[TCP_MAXOLEN];
11820 unsigned ipoptlen, optlen, hdrlen;
11821 unsigned ulen;
11822 uint32_t bbr_seq;
11823 uint32_t delay_calc=0;
11824 uint8_t doing_tlp = 0;
11825 uint8_t local_options;
11826 #ifdef BBR_INVARIANTS
11827 uint8_t doing_retran_from = 0;
11828 uint8_t picked_up_retran = 0;
11829 #endif
11830 uint8_t wanted_cookie = 0;
11831 uint8_t more_to_rxt=0;
11832 int32_t prefetch_so_done = 0;
11833 int32_t prefetch_rsm = 0;
11834 uint32_t tot_len = 0;
11835 uint32_t maxseg, pace_max_segs, p_maxseg;
11836 int32_t csum_flags = 0;
11837 int32_t hw_tls;
11838 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
11839 unsigned ipsec_optlen = 0;
11840
11841 #endif
11842 volatile int32_t sack_rxmit;
11843 struct bbr_sendmap *rsm = NULL;
11844 int32_t tso, mtu;
11845 struct tcpopt to;
11846 int32_t slot = 0;
11847 struct inpcb *inp;
11848 struct sockbuf *sb;
11849 bool hpts_calling;
11850 #ifdef INET6
11851 struct ip6_hdr *ip6 = NULL;
11852 int32_t isipv6;
11853 #endif
11854 uint8_t app_limited = BBR_JR_SENT_DATA;
11855 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11856 /* We take a cache hit here */
11857 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval));
11858 cts = tcp_tv_to_usectick(&bbr->rc_tv);
11859 inp = bbr->rc_inp;
11860 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
11861 tp->t_flags2 &= ~TF2_HPTS_CALLS;
11862 so = inp->inp_socket;
11863 sb = &so->so_snd;
11864 if (tp->t_nic_ktls_xmit)
11865 hw_tls = 1;
11866 else
11867 hw_tls = 0;
11868 kern_prefetch(sb, &maxseg);
11869 maxseg = tp->t_maxseg - bbr->rc_last_options;
11870 if (bbr_minseg(bbr) < maxseg) {
11871 tcp_bbr_tso_size_check(bbr, cts);
11872 }
11873 /* Remove any flags that indicate we are pacing on the inp */
11874 pace_max_segs = bbr->r_ctl.rc_pace_max_segs;
11875 p_maxseg = min(maxseg, pace_max_segs);
11876 INP_WLOCK_ASSERT(inp);
11877 #ifdef TCP_OFFLOAD
11878 if (tp->t_flags & TF_TOE)
11879 return (tcp_offload_output(tp));
11880 #endif
11881
11882 #ifdef INET6
11883 if (bbr->r_state) {
11884 /* Use the cache line loaded if possible */
11885 isipv6 = bbr->r_is_v6;
11886 } else {
11887 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
11888 }
11889 #endif
11890 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
11891 tcp_in_hpts(tp)) {
11892 /*
11893 * We are on the hpts for some timer but not hptsi output.
11894 * Possibly remove from the hpts so we can send/recv etc.
11895 */
11896 if ((tp->t_flags & TF_ACKNOW) == 0) {
11897 /*
11898 * No immediate demand right now to send an ack, but
11899 * the user may have read, making room for new data
11900 * (a window update). If so we may want to cancel
11901 * whatever timer is running (KEEP/DEL-ACK?) and
11902 * continue to send out a window update. Or we may
11903 * have gotten more data into the socket buffer to
11904 * send.
11905 */
11906 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
11907 (long)TCP_MAXWIN << tp->rcv_scale);
11908 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) &&
11909 ((tcp_outflags[tp->t_state] & TH_RST) == 0) &&
11910 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <=
11911 (tp->snd_max - tp->snd_una))) {
11912 /*
11913 * Nothing new to send and no window update
11914 * is needed to send. Lets just return and
11915 * let the timer-run off.
11916 */
11917 return (0);
11918 }
11919 }
11920 tcp_hpts_remove(tp);
11921 bbr_timer_cancel(bbr, __LINE__, cts);
11922 }
11923 if (bbr->r_ctl.rc_last_delay_val) {
11924 /* Calculate a rough delay for early escape to sending */
11925 if (SEQ_GT(cts, bbr->rc_pacer_started))
11926 delay_calc = cts - bbr->rc_pacer_started;
11927 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
11928 delay_calc -= bbr->r_ctl.rc_last_delay_val;
11929 else
11930 delay_calc = 0;
11931 }
11932 /* Mark that we have called bbr_output(). */
11933 if ((bbr->r_timer_override) ||
11934 (tp->t_state < TCPS_ESTABLISHED)) {
11935 /* Timeouts or early states are exempt */
11936 if (tcp_in_hpts(tp))
11937 tcp_hpts_remove(tp);
11938 } else if (tcp_in_hpts(tp)) {
11939 if ((bbr->r_ctl.rc_last_delay_val) &&
11940 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11941 delay_calc) {
11942 /*
11943 * We were being paced for output and the delay has
11944 * already exceeded when we were supposed to be
11945 * called, lets go ahead and pull out of the hpts
11946 * and call output.
11947 */
11948 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1);
11949 bbr->r_ctl.rc_last_delay_val = 0;
11950 tcp_hpts_remove(tp);
11951 } else if (tp->t_state == TCPS_CLOSED) {
11952 bbr->r_ctl.rc_last_delay_val = 0;
11953 tcp_hpts_remove(tp);
11954 } else {
11955 /*
11956 * On the hpts, you shall not pass! even if ACKNOW
11957 * is on, we will when the hpts fires, unless of
11958 * course we are overdue.
11959 */
11960 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1);
11961 return (0);
11962 }
11963 }
11964 bbr->rc_cwnd_limited = 0;
11965 if (bbr->r_ctl.rc_last_delay_val) {
11966 /* recalculate the real delay and deal with over/under */
11967 if (SEQ_GT(cts, bbr->rc_pacer_started))
11968 delay_calc = cts - bbr->rc_pacer_started;
11969 else
11970 delay_calc = 0;
11971 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
11972 /* Setup the delay which will be added in */
11973 delay_calc -= bbr->r_ctl.rc_last_delay_val;
11974 else {
11975 /*
11976 * We are early setup to adjust
11977 * our slot time.
11978 */
11979 uint64_t merged_val;
11980
11981 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc);
11982 bbr->r_agg_early_set = 1;
11983 if (bbr->r_ctl.rc_hptsi_agg_delay) {
11984 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) {
11985 /* Nope our previous late cancels out the early */
11986 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early;
11987 bbr->r_agg_early_set = 0;
11988 bbr->r_ctl.rc_agg_early = 0;
11989 } else {
11990 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay;
11991 bbr->r_ctl.rc_hptsi_agg_delay = 0;
11992 }
11993 }
11994 merged_val = bbr->rc_pacer_started;
11995 merged_val <<= 32;
11996 merged_val |= bbr->r_ctl.rc_last_delay_val;
11997 bbr_log_pacing_delay_calc(bbr, hpts_calling,
11998 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val,
11999 bbr->r_agg_early_set, 3);
12000 bbr->r_ctl.rc_last_delay_val = 0;
12001 BBR_STAT_INC(bbr_early);
12002 delay_calc = 0;
12003 }
12004 } else {
12005 /* We were not delayed due to hptsi */
12006 if (bbr->r_agg_early_set)
12007 bbr->r_ctl.rc_agg_early = 0;
12008 bbr->r_agg_early_set = 0;
12009 delay_calc = 0;
12010 }
12011 if (delay_calc) {
12012 /*
12013 * We had a hptsi delay which means we are falling behind on
12014 * sending at the expected rate. Calculate an extra amount
12015 * of data we can send, if any, to put us back on track.
12016 */
12017 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay)
12018 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff;
12019 else
12020 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc;
12021 }
12022 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12023 if ((tp->snd_una == tp->snd_max) &&
12024 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
12025 (sbavail(sb))) {
12026 /*
12027 * Ok we have been idle with nothing outstanding
12028 * we possibly need to start fresh with either a new
12029 * suite of states or a fast-ramp up.
12030 */
12031 bbr_restart_after_idle(bbr,
12032 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time));
12033 }
12034 /*
12035 * Now was there a hptsi delay where we are behind? We only count
12036 * being behind if: a) We are not in recovery. b) There was a delay.
12037 * <and> c) We had room to send something.
12038 *
12039 */
12040 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
12041 int retval;
12042
12043 retval = bbr_process_timers(tp, bbr, cts, hpts_calling);
12044 if (retval != 0) {
12045 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1);
12046 /*
12047 * If timers want tcp_drop(), then pass error out,
12048 * otherwise suppress it.
12049 */
12050 return (retval < 0 ? retval : 0);
12051 }
12052 }
12053 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY;
12054 if (hpts_calling &&
12055 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12056 bbr->r_ctl.rc_last_delay_val = 0;
12057 }
12058 bbr->r_timer_override = 0;
12059 bbr->r_wanted_output = 0;
12060 /*
12061 * For TFO connections in SYN_RECEIVED, only allow the initial
12062 * SYN|ACK and those sent by the retransmit timer.
12063 */
12064 if ((tp->t_flags & TF_FASTOPEN) &&
12065 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12066 (tp->t_state == TCPS_SYN_SENT)) &&
12067 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12068 (tp->t_rxtshift == 0)) { /* not a retransmit */
12069 len = 0;
12070 goto just_return_nolock;
12071 }
12072 /*
12073 * Before sending anything check for a state update. For hpts
12074 * calling without input this is important. If its input calling
12075 * then this was already done.
12076 */
12077 if (bbr->rc_use_google == 0)
12078 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12079 again:
12080 /*
12081 * If we've recently taken a timeout, snd_max will be greater than
12082 * snd_max. BBR in general does not pay much attention to snd_nxt
12083 * for historic reasons the persist timer still uses it. This means
12084 * we have to look at it. All retransmissions that are not persits
12085 * use the rsm that needs to be sent so snd_nxt is ignored. At the
12086 * end of this routine we pull snd_nxt always up to snd_max.
12087 */
12088 doing_tlp = 0;
12089 #ifdef BBR_INVARIANTS
12090 doing_retran_from = picked_up_retran = 0;
12091 #endif
12092 error = 0;
12093 tso = 0;
12094 slot = 0;
12095 mtu = 0;
12096 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12097 sb_offset = tp->snd_max - tp->snd_una;
12098 flags = tcp_outflags[tp->t_state];
12099 sack_rxmit = 0;
12100 len = 0;
12101 rsm = NULL;
12102 if (flags & TH_RST) {
12103 SOCKBUF_LOCK(sb);
12104 goto send;
12105 }
12106 recheck_resend:
12107 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
12108 /* We need to always have one in reserve */
12109 rsm = bbr_alloc(bbr);
12110 if (rsm == NULL) {
12111 error = ENOMEM;
12112 /* Lie to get on the hpts */
12113 tot_len = tp->t_maxseg;
12114 if (hpts_calling)
12115 /* Retry in a ms */
12116 slot = 1001;
12117 goto just_return_nolock;
12118 }
12119 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
12120 bbr->r_ctl.rc_free_cnt++;
12121 rsm = NULL;
12122 }
12123 /* What do we send, a resend? */
12124 if (bbr->r_ctl.rc_resend == NULL) {
12125 /* Check for rack timeout */
12126 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
12127 if (bbr->r_ctl.rc_resend) {
12128 #ifdef BBR_INVARIANTS
12129 picked_up_retran = 1;
12130 #endif
12131 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend);
12132 }
12133 }
12134 if (bbr->r_ctl.rc_resend) {
12135 rsm = bbr->r_ctl.rc_resend;
12136 #ifdef BBR_INVARIANTS
12137 doing_retran_from = 1;
12138 #endif
12139 /* Remove any TLP flags its a RACK or T-O */
12140 rsm->r_flags &= ~BBR_TLP;
12141 bbr->r_ctl.rc_resend = NULL;
12142 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12143 #ifdef BBR_INVARIANTS
12144 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n",
12145 tp, bbr, rsm, rsm->r_start, tp->snd_una);
12146 goto recheck_resend;
12147 #else
12148 /* TSNH */
12149 rsm = NULL;
12150 goto recheck_resend;
12151 #endif
12152 }
12153 if (rsm->r_flags & BBR_HAS_SYN) {
12154 /* Only retransmit a SYN by itself */
12155 len = 0;
12156 if ((flags & TH_SYN) == 0) {
12157 /* Huh something is wrong */
12158 rsm->r_start++;
12159 if (rsm->r_start == rsm->r_end) {
12160 /* Clean it up, somehow we missed the ack? */
12161 bbr_log_syn(tp, NULL);
12162 } else {
12163 /* TFO with data? */
12164 rsm->r_flags &= ~BBR_HAS_SYN;
12165 len = rsm->r_end - rsm->r_start;
12166 }
12167 } else {
12168 /* Retransmitting SYN */
12169 rsm = NULL;
12170 SOCKBUF_LOCK(sb);
12171 goto send;
12172 }
12173 } else
12174 len = rsm->r_end - rsm->r_start;
12175 if ((bbr->rc_resends_use_tso == 0) &&
12176 (len > maxseg)) {
12177 len = maxseg;
12178 more_to_rxt = 1;
12179 }
12180 sb_offset = rsm->r_start - tp->snd_una;
12181 if (len > 0) {
12182 sack_rxmit = 1;
12183 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12184 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12185 min(len, maxseg));
12186 } else {
12187 /* I dont think this can happen */
12188 rsm = NULL;
12189 goto recheck_resend;
12190 }
12191 BBR_STAT_INC(bbr_resends_set);
12192 } else if (bbr->r_ctl.rc_tlp_send) {
12193 /*
12194 * Tail loss probe
12195 */
12196 doing_tlp = 1;
12197 rsm = bbr->r_ctl.rc_tlp_send;
12198 bbr->r_ctl.rc_tlp_send = NULL;
12199 sack_rxmit = 1;
12200 len = rsm->r_end - rsm->r_start;
12201 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12202 len = maxseg;
12203
12204 if (SEQ_GT(tp->snd_una, rsm->r_start)) {
12205 #ifdef BBR_INVARIANTS
12206 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u",
12207 tp, bbr, tp->snd_una, rsm, rsm->r_start);
12208 #else
12209 /* TSNH */
12210 rsm = NULL;
12211 goto recheck_resend;
12212 #endif
12213 }
12214 sb_offset = rsm->r_start - tp->snd_una;
12215 BBR_STAT_INC(bbr_tlp_set);
12216 }
12217 /*
12218 * Enforce a connection sendmap count limit if set
12219 * as long as we are not retransmiting.
12220 */
12221 if ((rsm == NULL) &&
12222 (V_tcp_map_entries_limit > 0) &&
12223 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12224 BBR_STAT_INC(bbr_alloc_limited);
12225 if (!bbr->alloc_limit_reported) {
12226 bbr->alloc_limit_reported = 1;
12227 BBR_STAT_INC(bbr_alloc_limited_conns);
12228 }
12229 goto just_return_nolock;
12230 }
12231 #ifdef BBR_INVARIANTS
12232 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) {
12233 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u",
12234 tp, bbr, rsm, sb_offset, len);
12235 }
12236 #endif
12237 /*
12238 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12239 * state flags.
12240 */
12241 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL))
12242 flags |= TH_FIN;
12243 if (tp->t_flags & TF_NEEDSYN)
12244 flags |= TH_SYN;
12245
12246 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) {
12247 /* we are retransmitting the fin */
12248 len--;
12249 if (len) {
12250 /*
12251 * When retransmitting data do *not* include the
12252 * FIN. This could happen from a TLP probe if we
12253 * allowed data with a FIN.
12254 */
12255 flags &= ~TH_FIN;
12256 }
12257 } else if (rsm) {
12258 if (flags & TH_FIN)
12259 flags &= ~TH_FIN;
12260 }
12261 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12262 void *end_rsm;
12263
12264 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
12265 if (end_rsm)
12266 kern_prefetch(end_rsm, &prefetch_rsm);
12267 prefetch_rsm = 1;
12268 }
12269 SOCKBUF_LOCK(sb);
12270 /*
12271 * If snd_nxt == snd_max and we have transmitted a FIN, the
12272 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12273 * negative length. This can also occur when TCP opens up its
12274 * congestion window while receiving additional duplicate acks after
12275 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12276 * the fast-retransmit.
12277 *
12278 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12279 * set to snd_una, the sb_offset will be 0, and the length may wind
12280 * up 0.
12281 *
12282 * If sack_rxmit is true we are retransmitting from the scoreboard
12283 * in which case len is already set.
12284 */
12285 if (sack_rxmit == 0) {
12286 uint32_t avail;
12287
12288 avail = sbavail(sb);
12289 if (SEQ_GT(tp->snd_max, tp->snd_una))
12290 sb_offset = tp->snd_max - tp->snd_una;
12291 else
12292 sb_offset = 0;
12293 if (bbr->rc_tlp_new_data) {
12294 /* TLP is forcing out new data */
12295 uint32_t tlplen;
12296
12297 doing_tlp = 1;
12298 tlplen = maxseg;
12299
12300 if (tlplen > (uint32_t)(avail - sb_offset)) {
12301 tlplen = (uint32_t)(avail - sb_offset);
12302 }
12303 if (tlplen > tp->snd_wnd) {
12304 len = tp->snd_wnd;
12305 } else {
12306 len = tlplen;
12307 }
12308 bbr->rc_tlp_new_data = 0;
12309 } else {
12310 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts);
12311 if ((len < p_maxseg) &&
12312 (bbr->rc_in_persist == 0) &&
12313 (ctf_outstanding(tp) >= (2 * p_maxseg)) &&
12314 ((avail - sb_offset) >= p_maxseg)) {
12315 /*
12316 * We are not completing whats in the socket
12317 * buffer (i.e. there is at least a segment
12318 * waiting to send) and we have 2 or more
12319 * segments outstanding. There is no sense
12320 * of sending a little piece. Lets defer and
12321 * and wait until we can send a whole
12322 * segment.
12323 */
12324 len = 0;
12325 }
12326 if (bbr->rc_in_persist) {
12327 /*
12328 * We are in persists, figure out if
12329 * a retransmit is available (maybe the previous
12330 * persists we sent) or if we have to send new
12331 * data.
12332 */
12333 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
12334 if (rsm) {
12335 len = rsm->r_end - rsm->r_start;
12336 if (rsm->r_flags & BBR_HAS_FIN)
12337 len--;
12338 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12339 len = maxseg;
12340 if (len > 1)
12341 BBR_STAT_INC(bbr_persist_reneg);
12342 /*
12343 * XXXrrs we could force the len to
12344 * 1 byte here to cause the chunk to
12345 * split apart.. but that would then
12346 * mean we always retransmit it as
12347 * one byte even after the window
12348 * opens.
12349 */
12350 sack_rxmit = 1;
12351 sb_offset = rsm->r_start - tp->snd_una;
12352 } else {
12353 /*
12354 * First time through in persists or peer
12355 * acked our one byte. Though we do have
12356 * to have something in the sb.
12357 */
12358 len = 1;
12359 sb_offset = 0;
12360 if (avail == 0)
12361 len = 0;
12362 }
12363 }
12364 }
12365 }
12366 if (prefetch_so_done == 0) {
12367 kern_prefetch(so, &prefetch_so_done);
12368 prefetch_so_done = 1;
12369 }
12370 /*
12371 * Lop off SYN bit if it has already been sent. However, if this is
12372 * SYN-SENT state and if segment contains data and if we don't know
12373 * that foreign host supports TAO, suppress sending segment.
12374 */
12375 if ((flags & TH_SYN) && (rsm == NULL) &&
12376 SEQ_GT(tp->snd_max, tp->snd_una)) {
12377 if (tp->t_state != TCPS_SYN_RECEIVED)
12378 flags &= ~TH_SYN;
12379 /*
12380 * When sending additional segments following a TFO SYN|ACK,
12381 * do not include the SYN bit.
12382 */
12383 if ((tp->t_flags & TF_FASTOPEN) &&
12384 (tp->t_state == TCPS_SYN_RECEIVED))
12385 flags &= ~TH_SYN;
12386 sb_offset--, len++;
12387 if (sbavail(sb) == 0)
12388 len = 0;
12389 } else if ((flags & TH_SYN) && rsm) {
12390 /*
12391 * Subtract one from the len for the SYN being
12392 * retransmitted.
12393 */
12394 len--;
12395 }
12396 /*
12397 * Be careful not to send data and/or FIN on SYN segments. This
12398 * measure is needed to prevent interoperability problems with not
12399 * fully conformant TCP implementations.
12400 */
12401 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12402 len = 0;
12403 flags &= ~TH_FIN;
12404 }
12405 /*
12406 * On TFO sockets, ensure no data is sent in the following cases:
12407 *
12408 * - When retransmitting SYN|ACK on a passively-created socket
12409 * - When retransmitting SYN on an actively created socket
12410 * - When sending a zero-length cookie (cookie request) on an
12411 * actively created socket
12412 * - When the socket is in the CLOSED state (RST is being sent)
12413 */
12414 if ((tp->t_flags & TF_FASTOPEN) &&
12415 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12416 ((tp->t_state == TCPS_SYN_SENT) &&
12417 (tp->t_tfo_client_cookie_len == 0)) ||
12418 (flags & TH_RST))) {
12419 len = 0;
12420 sack_rxmit = 0;
12421 rsm = NULL;
12422 }
12423 /* Without fast-open there should never be data sent on a SYN */
12424 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN))
12425 len = 0;
12426 if (len <= 0) {
12427 /*
12428 * If FIN has been sent but not acked, but we haven't been
12429 * called to retransmit, len will be < 0. Otherwise, window
12430 * shrank after we sent into it. If window shrank to 0,
12431 * cancel pending retransmit, pull snd_nxt back to (closed)
12432 * window, and set the persist timer if it isn't already
12433 * going. If the window didn't close completely, just wait
12434 * for an ACK.
12435 *
12436 * We also do a general check here to ensure that we will
12437 * set the persist timer when we have data to send, but a
12438 * 0-byte window. This makes sure the persist timer is set
12439 * even if the packet hits one of the "goto send" lines
12440 * below.
12441 */
12442 len = 0;
12443 if ((tp->snd_wnd == 0) &&
12444 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12445 (tp->snd_una == tp->snd_max) &&
12446 (sb_offset < (int)sbavail(sb))) {
12447 /*
12448 * Not enough room in the rwnd to send
12449 * a paced segment out.
12450 */
12451 bbr_enter_persist(tp, bbr, cts, __LINE__);
12452 }
12453 } else if ((rsm == NULL) &&
12454 (doing_tlp == 0) &&
12455 (len < bbr->r_ctl.rc_pace_max_segs)) {
12456 /*
12457 * We are not sending a full segment for
12458 * some reason. Should we not send anything (think
12459 * sws or persists)?
12460 */
12461 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12462 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12463 (len < (int)(sbavail(sb) - sb_offset))) {
12464 /*
12465 * Here the rwnd is less than
12466 * the pacing size, this is not a retransmit,
12467 * we are established and
12468 * the send is not the last in the socket buffer
12469 * lets not send, and possibly enter persists.
12470 */
12471 len = 0;
12472 if (tp->snd_max == tp->snd_una)
12473 bbr_enter_persist(tp, bbr, cts, __LINE__);
12474 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) &&
12475 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12476 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12477 (len < (int)(sbavail(sb) - sb_offset)) &&
12478 (len < bbr_minseg(bbr))) {
12479 /*
12480 * Here we are not retransmitting, and
12481 * the cwnd is not so small that we could
12482 * not send at least a min size (rxt timer
12483 * not having gone off), We have 2 segments or
12484 * more already in flight, its not the tail end
12485 * of the socket buffer and the cwnd is blocking
12486 * us from sending out minimum pacing segment size.
12487 * Lets not send anything.
12488 */
12489 bbr->rc_cwnd_limited = 1;
12490 len = 0;
12491 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12492 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
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 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12497 /*
12498 * Here we have a send window but we have
12499 * filled it up and we can't send another pacing segment.
12500 * We also have in flight more than 2 segments
12501 * and we are not completing the sb i.e. we allow
12502 * the last bytes of the sb to go out even if
12503 * its not a full pacing segment.
12504 */
12505 len = 0;
12506 }
12507 }
12508 /* len will be >= 0 after this point. */
12509 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12510 tcp_sndbuf_autoscale(tp, so, sendwin);
12511 /*
12512 *
12513 */
12514 if (bbr->rc_in_persist &&
12515 len &&
12516 (rsm == NULL) &&
12517 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) {
12518 /*
12519 * We are in persist, not doing a retransmit and don't have enough space
12520 * yet to send a full TSO. So is it at the end of the sb
12521 * if so we need to send else nuke to 0 and don't send.
12522 */
12523 int sbleft;
12524 if (sbavail(sb) > sb_offset)
12525 sbleft = sbavail(sb) - sb_offset;
12526 else
12527 sbleft = 0;
12528 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) {
12529 /* not at end of sb lets not send */
12530 len = 0;
12531 }
12532 }
12533 /*
12534 * Decide if we can use TCP Segmentation Offloading (if supported by
12535 * hardware).
12536 *
12537 * TSO may only be used if we are in a pure bulk sending state. The
12538 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12539 * options prevent using TSO. With TSO the TCP header is the same
12540 * (except for the sequence number) for all generated packets. This
12541 * makes it impossible to transmit any options which vary per
12542 * generated segment or packet.
12543 *
12544 * IPv4 handling has a clear separation of ip options and ip header
12545 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen()
12546 * does the right thing below to provide length of just ip options
12547 * and thus checking for ipoptlen is enough to decide if ip options
12548 * are present.
12549 */
12550 #ifdef INET6
12551 if (isipv6)
12552 ipoptlen = ip6_optlen(inp);
12553 else
12554 #endif
12555 if (inp->inp_options)
12556 ipoptlen = inp->inp_options->m_len -
12557 offsetof(struct ipoption, ipopt_list);
12558 else
12559 ipoptlen = 0;
12560 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12561 /*
12562 * Pre-calculate here as we save another lookup into the darknesses
12563 * of IPsec that way and can actually decide if TSO is ok.
12564 */
12565 #ifdef INET6
12566 if (isipv6 && IPSEC_ENABLED(ipv6))
12567 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
12568 #ifdef INET
12569 else
12570 #endif
12571 #endif /* INET6 */
12572 #ifdef INET
12573 if (IPSEC_ENABLED(ipv4))
12574 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
12575 #endif /* INET */
12576 #endif /* IPSEC */
12577 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12578 ipoptlen += ipsec_optlen;
12579 #endif
12580 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
12581 (len > maxseg) &&
12582 (tp->t_port == 0) &&
12583 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12584 tp->rcv_numsacks == 0 &&
12585 ipoptlen == 0)
12586 tso = 1;
12587
12588 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12589 (long)TCP_MAXWIN << tp->rcv_scale);
12590 /*
12591 * Sender silly window avoidance. We transmit under the following
12592 * conditions when len is non-zero:
12593 *
12594 * - We have a full segment (or more with TSO) - This is the last
12595 * buffer in a write()/send() and we are either idle or running
12596 * NODELAY - we've timed out (e.g. persist timer) - we have more
12597 * then 1/2 the maximum send window's worth of data (receiver may be
12598 * limited the window size) - we need to retransmit
12599 */
12600 if (rsm)
12601 goto send;
12602 if (len) {
12603 if (sack_rxmit)
12604 goto send;
12605 if (len >= p_maxseg)
12606 goto send;
12607 /*
12608 * NOTE! on localhost connections an 'ack' from the remote
12609 * end may occur synchronously with the output and cause us
12610 * to flush a buffer queued with moretocome. XXX
12611 *
12612 */
12613 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */
12614 ((tp->t_flags & TF_NODELAY) ||
12615 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) &&
12616 (tp->t_flags & TF_NOPUSH) == 0) {
12617 goto send;
12618 }
12619 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12620 goto send;
12621 }
12622 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12623 goto send;
12624 }
12625 }
12626 /*
12627 * Sending of standalone window updates.
12628 *
12629 * Window updates are important when we close our window due to a
12630 * full socket buffer and are opening it again after the application
12631 * reads data from it. Once the window has opened again and the
12632 * remote end starts to send again the ACK clock takes over and
12633 * provides the most current window information.
12634 *
12635 * We must avoid the silly window syndrome whereas every read from
12636 * the receive buffer, no matter how small, causes a window update
12637 * to be sent. We also should avoid sending a flurry of window
12638 * updates when the socket buffer had queued a lot of data and the
12639 * application is doing small reads.
12640 *
12641 * Prevent a flurry of pointless window updates by only sending an
12642 * update when we can increase the advertized window by more than
12643 * 1/4th of the socket buffer capacity. When the buffer is getting
12644 * full or is very small be more aggressive and send an update
12645 * whenever we can increase by two mss sized segments. In all other
12646 * situations the ACK's to new incoming data will carry further
12647 * window increases.
12648 *
12649 * Don't send an independent window update if a delayed ACK is
12650 * pending (it will get piggy-backed on it) or the remote side
12651 * already has done a half-close and won't send more data. Skip
12652 * this if the connection is in T/TCP half-open state.
12653 */
12654 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
12655 !(tp->t_flags & TF_DELACK) &&
12656 !TCPS_HAVERCVDFIN(tp->t_state)) {
12657 /* Check to see if we should do a window update */
12658 if (bbr_window_update_needed(tp, so, recwin, maxseg))
12659 goto send;
12660 }
12661 /*
12662 * Send if we owe the peer an ACK, RST, SYN. ACKNOW
12663 * is also a catch-all for the retransmit timer timeout case.
12664 */
12665 if (tp->t_flags & TF_ACKNOW) {
12666 goto send;
12667 }
12668 if (flags & TH_RST) {
12669 /* Always send a RST if one is due */
12670 goto send;
12671 }
12672 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) {
12673 goto send;
12674 }
12675 /*
12676 * If our state indicates that FIN should be sent and we have not
12677 * yet done so, then we need to send.
12678 */
12679 if (flags & TH_FIN &&
12680 ((tp->t_flags & TF_SENTFIN) == 0)) {
12681 goto send;
12682 }
12683 /*
12684 * No reason to send a segment, just return.
12685 */
12686 just_return:
12687 SOCKBUF_UNLOCK(sb);
12688 just_return_nolock:
12689 if (tot_len)
12690 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
12691 if (bbr->rc_no_pacing)
12692 slot = 0;
12693 if (tot_len == 0) {
12694 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >=
12695 tp->snd_wnd) {
12696 BBR_STAT_INC(bbr_rwnd_limited);
12697 app_limited = BBR_JR_RWND_LIMITED;
12698 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12699 if ((bbr->rc_in_persist == 0) &&
12700 TCPS_HAVEESTABLISHED(tp->t_state) &&
12701 (tp->snd_max == tp->snd_una) &&
12702 sbavail(&so->so_snd)) {
12703 /* No send window.. we must enter persist */
12704 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
12705 }
12706 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
12707 BBR_STAT_INC(bbr_app_limited);
12708 app_limited = BBR_JR_APP_LIMITED;
12709 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12710 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12711 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) {
12712 BBR_STAT_INC(bbr_cwnd_limited);
12713 app_limited = BBR_JR_CWND_LIMITED;
12714 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12715 bbr->r_ctl.rc_lost_bytes)));
12716 bbr->rc_cwnd_limited = 1;
12717 } else {
12718 BBR_STAT_INC(bbr_app_limited);
12719 app_limited = BBR_JR_APP_LIMITED;
12720 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12721 }
12722 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12723 bbr->r_agg_early_set = 0;
12724 bbr->r_ctl.rc_agg_early = 0;
12725 bbr->r_ctl.rc_last_delay_val = 0;
12726 } else if (bbr->rc_use_google == 0)
12727 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12728 /* Are we app limited? */
12729 if ((app_limited == BBR_JR_APP_LIMITED) ||
12730 (app_limited == BBR_JR_RWND_LIMITED)) {
12731 /**
12732 * We are application limited.
12733 */
12734 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12735 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered);
12736 }
12737 if (tot_len == 0)
12738 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1);
12739 /* Dont update the time if we did not send */
12740 bbr->r_ctl.rc_last_delay_val = 0;
12741 bbr->rc_output_starts_timer = 1;
12742 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len);
12743 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len);
12744 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12745 /* Make sure snd_nxt is drug up */
12746 tp->snd_nxt = tp->snd_max;
12747 }
12748 return (error);
12749
12750 send:
12751 if (doing_tlp == 0) {
12752 /*
12753 * Data not a TLP, and its not the rxt firing. If it is the
12754 * rxt firing, we want to leave the tlp_in_progress flag on
12755 * so we don't send another TLP. It has to be a rack timer
12756 * or normal send (response to acked data) to clear the tlp
12757 * in progress flag.
12758 */
12759 bbr->rc_tlp_in_progress = 0;
12760 bbr->rc_tlp_rtx_out = 0;
12761 } else {
12762 /*
12763 * Its a TLP.
12764 */
12765 bbr->rc_tlp_in_progress = 1;
12766 }
12767 bbr_timer_cancel(bbr, __LINE__, cts);
12768 if (rsm == NULL) {
12769 if (sbused(sb) > 0) {
12770 /*
12771 * This is sub-optimal. We only send a stand alone
12772 * FIN on its own segment.
12773 */
12774 if (flags & TH_FIN) {
12775 flags &= ~TH_FIN;
12776 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) {
12777 /* Lets not send this */
12778 slot = 0;
12779 goto just_return;
12780 }
12781 }
12782 }
12783 } else {
12784 /*
12785 * We do *not* send a FIN on a retransmit if it has data.
12786 * The if clause here where len > 1 should never come true.
12787 */
12788 if ((len > 0) &&
12789 (((rsm->r_flags & BBR_HAS_FIN) == 0) &&
12790 (flags & TH_FIN))) {
12791 flags &= ~TH_FIN;
12792 len--;
12793 }
12794 }
12795 SOCKBUF_LOCK_ASSERT(sb);
12796 if (len > 0) {
12797 if ((tp->snd_una == tp->snd_max) &&
12798 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
12799 /*
12800 * This qualifies as a RTT_PROBE session since we
12801 * drop the data outstanding to nothing and waited
12802 * more than bbr_rtt_probe_time.
12803 */
12804 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
12805 bbr_set_reduced_rtt(bbr, cts, __LINE__);
12806 }
12807 if (len >= maxseg)
12808 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
12809 else
12810 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
12811 }
12812 /*
12813 * Before ESTABLISHED, force sending of initial options unless TCP
12814 * set not to do any options. NOTE: we assume that the IP/TCP header
12815 * plus TCP options always fit in a single mbuf, leaving room for a
12816 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
12817 * + optlen <= MCLBYTES
12818 */
12819 optlen = 0;
12820 #ifdef INET6
12821 if (isipv6)
12822 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12823 else
12824 #endif
12825 hdrlen = sizeof(struct tcpiphdr);
12826
12827 /*
12828 * Compute options for segment. We only have to care about SYN and
12829 * established connection segments. Options for SYN-ACK segments
12830 * are handled in TCP syncache.
12831 */
12832 to.to_flags = 0;
12833 local_options = 0;
12834 if ((tp->t_flags & TF_NOOPT) == 0) {
12835 /* Maximum segment size. */
12836 if (flags & TH_SYN) {
12837 to.to_mss = tcp_mssopt(&inp->inp_inc);
12838 if (tp->t_port)
12839 to.to_mss -= V_tcp_udp_tunneling_overhead;
12840 to.to_flags |= TOF_MSS;
12841 /*
12842 * On SYN or SYN|ACK transmits on TFO connections,
12843 * only include the TFO option if it is not a
12844 * retransmit, as the presence of the TFO option may
12845 * have caused the original SYN or SYN|ACK to have
12846 * been dropped by a middlebox.
12847 */
12848 if ((tp->t_flags & TF_FASTOPEN) &&
12849 (tp->t_rxtshift == 0)) {
12850 if (tp->t_state == TCPS_SYN_RECEIVED) {
12851 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
12852 to.to_tfo_cookie =
12853 (u_int8_t *)&tp->t_tfo_cookie.server;
12854 to.to_flags |= TOF_FASTOPEN;
12855 wanted_cookie = 1;
12856 } else if (tp->t_state == TCPS_SYN_SENT) {
12857 to.to_tfo_len =
12858 tp->t_tfo_client_cookie_len;
12859 to.to_tfo_cookie =
12860 tp->t_tfo_cookie.client;
12861 to.to_flags |= TOF_FASTOPEN;
12862 wanted_cookie = 1;
12863 }
12864 }
12865 }
12866 /* Window scaling. */
12867 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
12868 to.to_wscale = tp->request_r_scale;
12869 to.to_flags |= TOF_SCALE;
12870 }
12871 /* Timestamps. */
12872 if ((tp->t_flags & TF_RCVD_TSTMP) ||
12873 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
12874 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset;
12875 to.to_tsecr = tp->ts_recent;
12876 to.to_flags |= TOF_TS;
12877 local_options += TCPOLEN_TIMESTAMP + 2;
12878 }
12879 /* Set receive buffer autosizing timestamp. */
12880 if (tp->rfbuf_ts == 0 &&
12881 (so->so_rcv.sb_flags & SB_AUTOSIZE))
12882 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv);
12883 /* Selective ACK's. */
12884 if (flags & TH_SYN)
12885 to.to_flags |= TOF_SACKPERM;
12886 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
12887 tp->rcv_numsacks > 0) {
12888 to.to_flags |= TOF_SACK;
12889 to.to_nsacks = tp->rcv_numsacks;
12890 to.to_sacks = (u_char *)tp->sackblks;
12891 }
12892 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
12893 /* TCP-MD5 (RFC2385). */
12894 if (tp->t_flags & TF_SIGNATURE)
12895 to.to_flags |= TOF_SIGNATURE;
12896 #endif /* TCP_SIGNATURE */
12897
12898 /* Processing the options. */
12899 hdrlen += (optlen = tcp_addoptions(&to, opt));
12900 /*
12901 * If we wanted a TFO option to be added, but it was unable
12902 * to fit, ensure no data is sent.
12903 */
12904 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie &&
12905 !(to.to_flags & TOF_FASTOPEN))
12906 len = 0;
12907 }
12908 if (tp->t_port) {
12909 if (V_tcp_udp_tunneling_port == 0) {
12910 /* The port was removed?? */
12911 SOCKBUF_UNLOCK(&so->so_snd);
12912 return (EHOSTUNREACH);
12913 }
12914 hdrlen += sizeof(struct udphdr);
12915 }
12916 #ifdef INET6
12917 if (isipv6)
12918 ipoptlen = ip6_optlen(inp);
12919 else
12920 #endif
12921 if (inp->inp_options)
12922 ipoptlen = inp->inp_options->m_len -
12923 offsetof(struct ipoption, ipopt_list);
12924 else
12925 ipoptlen = 0;
12926 ipoptlen = 0;
12927 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12928 ipoptlen += ipsec_optlen;
12929 #endif
12930 if (bbr->rc_last_options != local_options) {
12931 /*
12932 * Cache the options length this generally does not change
12933 * on a connection. We use this to calculate TSO.
12934 */
12935 bbr->rc_last_options = local_options;
12936 }
12937 maxseg = tp->t_maxseg - (ipoptlen + optlen);
12938 p_maxseg = min(maxseg, pace_max_segs);
12939 /*
12940 * Adjust data length if insertion of options will bump the packet
12941 * length beyond the t_maxseg length. Clear the FIN bit because we
12942 * cut off the tail of the segment.
12943 */
12944 if (len > maxseg) {
12945 if (len != 0 && (flags & TH_FIN)) {
12946 flags &= ~TH_FIN;
12947 }
12948 if (tso) {
12949 uint32_t moff;
12950 int32_t max_len;
12951
12952 /* extract TSO information */
12953 if_hw_tsomax = tp->t_tsomax;
12954 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
12955 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
12956 KASSERT(ipoptlen == 0,
12957 ("%s: TSO can't do IP options", __func__));
12958
12959 /*
12960 * Check if we should limit by maximum payload
12961 * length:
12962 */
12963 if (if_hw_tsomax != 0) {
12964 /* compute maximum TSO length */
12965 max_len = (if_hw_tsomax - hdrlen -
12966 max_linkhdr);
12967 if (max_len <= 0) {
12968 len = 0;
12969 } else if (len > max_len) {
12970 len = max_len;
12971 }
12972 }
12973 /*
12974 * Prevent the last segment from being fractional
12975 * unless the send sockbuf can be emptied:
12976 */
12977 if ((sb_offset + len) < sbavail(sb)) {
12978 moff = len % (uint32_t)maxseg;
12979 if (moff != 0) {
12980 len -= moff;
12981 }
12982 }
12983 /*
12984 * In case there are too many small fragments don't
12985 * use TSO:
12986 */
12987 if (len <= maxseg) {
12988 len = maxseg;
12989 tso = 0;
12990 }
12991 } else {
12992 /* Not doing TSO */
12993 if (optlen + ipoptlen >= tp->t_maxseg) {
12994 /*
12995 * Since we don't have enough space to put
12996 * the IP header chain and the TCP header in
12997 * one packet as required by RFC 7112, don't
12998 * send it. Also ensure that at least one
12999 * byte of the payload can be put into the
13000 * TCP segment.
13001 */
13002 SOCKBUF_UNLOCK(&so->so_snd);
13003 error = EMSGSIZE;
13004 sack_rxmit = 0;
13005 goto out;
13006 }
13007 len = maxseg;
13008 }
13009 } else {
13010 /* Not doing TSO */
13011 if_hw_tsomaxsegcount = 0;
13012 tso = 0;
13013 }
13014 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13015 ("%s: len > IP_MAXPACKET", __func__));
13016 #ifdef DIAGNOSTIC
13017 #ifdef INET6
13018 if (max_linkhdr + hdrlen > MCLBYTES)
13019 #else
13020 if (max_linkhdr + hdrlen > MHLEN)
13021 #endif
13022 panic("tcphdr too big");
13023 #endif
13024 /*
13025 * This KASSERT is here to catch edge cases at a well defined place.
13026 * Before, those had triggered (random) panic conditions further
13027 * down.
13028 */
13029 #ifdef BBR_INVARIANTS
13030 if (sack_rxmit) {
13031 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
13032 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u",
13033 rsm, tp, bbr, rsm->r_start, tp->snd_una);
13034 }
13035 }
13036 #endif
13037 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13038 if ((len == 0) &&
13039 (flags & TH_FIN) &&
13040 (sbused(sb))) {
13041 /*
13042 * We have outstanding data, don't send a fin by itself!.
13043 */
13044 slot = 0;
13045 goto just_return;
13046 }
13047 /*
13048 * Grab a header mbuf, attaching a copy of data to be transmitted,
13049 * and initialize the header from the template for sends on this
13050 * connection.
13051 */
13052 if (len) {
13053 uint32_t moff;
13054
13055 /*
13056 * We place a limit on sending with hptsi.
13057 */
13058 if ((rsm == NULL) && len > pace_max_segs)
13059 len = pace_max_segs;
13060 if (len <= maxseg)
13061 tso = 0;
13062 #ifdef INET6
13063 if (MHLEN < hdrlen + max_linkhdr)
13064 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13065 else
13066 #endif
13067 m = m_gethdr(M_NOWAIT, MT_DATA);
13068
13069 if (m == NULL) {
13070 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13071 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13072 SOCKBUF_UNLOCK(sb);
13073 error = ENOBUFS;
13074 sack_rxmit = 0;
13075 goto out;
13076 }
13077 m->m_data += max_linkhdr;
13078 m->m_len = hdrlen;
13079 /*
13080 * Start the m_copy functions from the closest mbuf to the
13081 * sb_offset in the socket buffer chain.
13082 */
13083 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) {
13084 #ifdef BBR_INVARIANTS
13085 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0)))
13086 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u",
13087 tp, bbr, len, sb_offset, sbavail(sb), rsm,
13088 doing_retran_from,
13089 picked_up_retran,
13090 doing_tlp);
13091
13092 #endif
13093 /*
13094 * In this messed up situation we have two choices,
13095 * a) pretend the send worked, and just start timers
13096 * and what not (not good since that may lead us
13097 * back here a lot). <or> b) Send the lowest segment
13098 * in the map. <or> c) Drop the connection. Lets do
13099 * <b> which if it continues to happen will lead to
13100 * <c> via timeouts.
13101 */
13102 BBR_STAT_INC(bbr_offset_recovery);
13103 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
13104 sb_offset = 0;
13105 if (rsm == NULL) {
13106 sack_rxmit = 0;
13107 len = sbavail(sb);
13108 } else {
13109 sack_rxmit = 1;
13110 if (rsm->r_start != tp->snd_una) {
13111 /*
13112 * Things are really messed up, <c>
13113 * is the only thing to do.
13114 */
13115 BBR_STAT_INC(bbr_offset_drop);
13116 SOCKBUF_UNLOCK(sb);
13117 (void)m_free(m);
13118 return (-EFAULT); /* tcp_drop() */
13119 }
13120 len = rsm->r_end - rsm->r_start;
13121 }
13122 if (len > sbavail(sb))
13123 len = sbavail(sb);
13124 if (len > maxseg)
13125 len = maxseg;
13126 }
13127 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13128 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13129 m_copydata(mb, moff, (int)len,
13130 mtod(m, caddr_t)+hdrlen);
13131 if (rsm == NULL)
13132 sbsndptr_adv(sb, mb, len);
13133 m->m_len += len;
13134 } else {
13135 struct sockbuf *msb;
13136
13137 if (rsm)
13138 msb = NULL;
13139 else
13140 msb = sb;
13141 #ifdef BBR_INVARIANTS
13142 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) {
13143 if (rsm) {
13144 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 ",
13145 tp, bbr, len, moff,
13146 sbavail(sb), rsm,
13147 tp->snd_una, rsm->r_flags, rsm->r_start,
13148 doing_retran_from,
13149 picked_up_retran,
13150 doing_tlp, sack_rxmit);
13151 } else {
13152 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u",
13153 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una);
13154 }
13155 }
13156 #endif
13157 m->m_next = tcp_m_copym(
13158 mb, moff, &len,
13159 if_hw_tsomaxsegcount,
13160 if_hw_tsomaxsegsize, msb,
13161 ((rsm == NULL) ? hw_tls : 0)
13162 #ifdef NETFLIX_COPY_ARGS
13163 , NULL, NULL
13164 #endif
13165 );
13166 if (len <= maxseg) {
13167 /*
13168 * Must have ran out of mbufs for the copy
13169 * shorten it to no longer need tso. Lets
13170 * not put on sendalot since we are low on
13171 * mbufs.
13172 */
13173 tso = 0;
13174 }
13175 if (m->m_next == NULL) {
13176 SOCKBUF_UNLOCK(sb);
13177 (void)m_free(m);
13178 error = ENOBUFS;
13179 sack_rxmit = 0;
13180 goto out;
13181 }
13182 }
13183 #ifdef BBR_INVARIANTS
13184 if (tso && len < maxseg) {
13185 panic("tp:%p tso on, but len:%d < maxseg:%d",
13186 tp, len, maxseg);
13187 }
13188 if (tso && if_hw_tsomaxsegcount) {
13189 int32_t seg_cnt = 0;
13190 struct mbuf *foo;
13191
13192 foo = m;
13193 while (foo) {
13194 seg_cnt++;
13195 foo = foo->m_next;
13196 }
13197 if (seg_cnt > if_hw_tsomaxsegcount) {
13198 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount);
13199 }
13200 }
13201 #endif
13202 /*
13203 * If we're sending everything we've got, set PUSH. (This
13204 * will keep happy those implementations which only give
13205 * data to the user when a buffer fills or a PUSH comes in.)
13206 */
13207 if (sb_offset + len == sbused(sb) &&
13208 sbused(sb) &&
13209 !(flags & TH_SYN)) {
13210 flags |= TH_PUSH;
13211 }
13212 SOCKBUF_UNLOCK(sb);
13213 } else {
13214 SOCKBUF_UNLOCK(sb);
13215 if (tp->t_flags & TF_ACKNOW)
13216 KMOD_TCPSTAT_INC(tcps_sndacks);
13217 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13218 KMOD_TCPSTAT_INC(tcps_sndctrl);
13219 else
13220 KMOD_TCPSTAT_INC(tcps_sndwinup);
13221
13222 m = m_gethdr(M_NOWAIT, MT_DATA);
13223 if (m == NULL) {
13224 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13225 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13226 error = ENOBUFS;
13227 /* Fudge the send time since we could not send */
13228 sack_rxmit = 0;
13229 goto out;
13230 }
13231 #ifdef INET6
13232 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13233 MHLEN >= hdrlen) {
13234 M_ALIGN(m, hdrlen);
13235 } else
13236 #endif
13237 m->m_data += max_linkhdr;
13238 m->m_len = hdrlen;
13239 }
13240 SOCKBUF_UNLOCK_ASSERT(sb);
13241 m->m_pkthdr.rcvif = (struct ifnet *)0;
13242 #ifdef MAC
13243 mac_inpcb_create_mbuf(inp, m);
13244 #endif
13245 #ifdef INET6
13246 if (isipv6) {
13247 ip6 = mtod(m, struct ip6_hdr *);
13248 if (tp->t_port) {
13249 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
13250 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13251 udp->uh_dport = tp->t_port;
13252 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13253 udp->uh_ulen = htons(ulen);
13254 th = (struct tcphdr *)(udp + 1);
13255 } else {
13256 th = (struct tcphdr *)(ip6 + 1);
13257 }
13258 tcpip_fillheaders(inp, tp->t_port, ip6, th);
13259 } else
13260 #endif /* INET6 */
13261 {
13262 ip = mtod(m, struct ip *);
13263 if (tp->t_port) {
13264 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
13265 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13266 udp->uh_dport = tp->t_port;
13267 ulen = hdrlen + len - sizeof(struct ip);
13268 udp->uh_ulen = htons(ulen);
13269 th = (struct tcphdr *)(udp + 1);
13270 } else {
13271 th = (struct tcphdr *)(ip + 1);
13272 }
13273 tcpip_fillheaders(inp, tp->t_port, ip, th);
13274 }
13275 /*
13276 * If we are doing retransmissions, then snd_nxt will not reflect
13277 * the first unsent octet. For ACK only packets, we do not want the
13278 * sequence number of the retransmitted packet, we want the sequence
13279 * number of the next unsent octet. So, if there is no data (and no
13280 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13281 * ti_seq. But if we are in persist state, snd_max might reflect
13282 * one byte beyond the right edge of the window, so use snd_nxt in
13283 * that case, since we know we aren't doing a retransmission.
13284 * (retransmit and persist are mutually exclusive...)
13285 */
13286 if (sack_rxmit == 0) {
13287 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) {
13288 /* New data (including new persists) */
13289 th->th_seq = htonl(tp->snd_max);
13290 bbr_seq = tp->snd_max;
13291 } else if (flags & TH_SYN) {
13292 /* Syn's always send from iss */
13293 th->th_seq = htonl(tp->iss);
13294 bbr_seq = tp->iss;
13295 } else if (flags & TH_FIN) {
13296 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) {
13297 /*
13298 * If we sent the fin already its 1 minus
13299 * snd_max
13300 */
13301 th->th_seq = (htonl(tp->snd_max - 1));
13302 bbr_seq = (tp->snd_max - 1);
13303 } else {
13304 /* First time FIN use snd_max */
13305 th->th_seq = htonl(tp->snd_max);
13306 bbr_seq = tp->snd_max;
13307 }
13308 } else {
13309 /*
13310 * len == 0 and not persist we use snd_max, sending
13311 * an ack unless we have sent the fin then its 1
13312 * minus.
13313 */
13314 /*
13315 * XXXRRS Question if we are in persists and we have
13316 * nothing outstanding to send and we have not sent
13317 * a FIN, we will send an ACK. In such a case it
13318 * might be better to send (tp->snd_una - 1) which
13319 * would force the peer to ack.
13320 */
13321 if (tp->t_flags & TF_SENTFIN) {
13322 th->th_seq = htonl(tp->snd_max - 1);
13323 bbr_seq = (tp->snd_max - 1);
13324 } else {
13325 th->th_seq = htonl(tp->snd_max);
13326 bbr_seq = tp->snd_max;
13327 }
13328 }
13329 } else {
13330 /* All retransmits use the rsm to guide the send */
13331 th->th_seq = htonl(rsm->r_start);
13332 bbr_seq = rsm->r_start;
13333 }
13334 th->th_ack = htonl(tp->rcv_nxt);
13335 if (optlen) {
13336 bcopy(opt, th + 1, optlen);
13337 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13338 }
13339 tcp_set_flags(th, flags);
13340 /*
13341 * Calculate receive window. Don't shrink window, but avoid silly
13342 * window syndrome.
13343 */
13344 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) &&
13345 recwin < maxseg)))
13346 recwin = 0;
13347 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13348 recwin < (tp->rcv_adv - tp->rcv_nxt))
13349 recwin = (tp->rcv_adv - tp->rcv_nxt);
13350 if (recwin > TCP_MAXWIN << tp->rcv_scale)
13351 recwin = TCP_MAXWIN << tp->rcv_scale;
13352
13353 /*
13354 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13355 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13356 * handled in syncache.
13357 */
13358 if (flags & TH_SYN)
13359 th->th_win = htons((u_short)
13360 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13361 else {
13362 /* Avoid shrinking window with window scaling. */
13363 recwin = roundup2(recwin, 1 << tp->rcv_scale);
13364 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13365 }
13366 /*
13367 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13368 * window. This may cause the remote transmitter to stall. This
13369 * flag tells soreceive() to disable delayed acknowledgements when
13370 * draining the buffer. This can occur if the receiver is
13371 * attempting to read more data than can be buffered prior to
13372 * transmitting on the connection.
13373 */
13374 if (th->th_win == 0) {
13375 tp->t_sndzerowin++;
13376 tp->t_flags |= TF_RXWIN0SENT;
13377 } else
13378 tp->t_flags &= ~TF_RXWIN0SENT;
13379 /*
13380 * We don't support urgent data, but drag along
13381 * the pointer in case of a stack switch.
13382 */
13383 tp->snd_up = tp->snd_una;
13384 /*
13385 * Put TCP length in extended header, and then checksum extended
13386 * header and data.
13387 */
13388 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13389
13390 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13391 if (to.to_flags & TOF_SIGNATURE) {
13392 /*
13393 * Calculate MD5 signature and put it into the place
13394 * determined before. NOTE: since TCP options buffer doesn't
13395 * point into mbuf's data, calculate offset and use it.
13396 */
13397 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13398 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13399 /*
13400 * Do not send segment if the calculation of MD5
13401 * digest has failed.
13402 */
13403 goto out;
13404 }
13405 }
13406 #endif
13407
13408 #ifdef INET6
13409 if (isipv6) {
13410 /*
13411 * ip6_plen is not need to be filled now, and will be filled
13412 * in ip6_output.
13413 */
13414 if (tp->t_port) {
13415 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13416 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13417 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13418 th->th_sum = htons(0);
13419 UDPSTAT_INC(udps_opackets);
13420 } else {
13421 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13422 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13423 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
13424 optlen + len, IPPROTO_TCP, 0);
13425 }
13426 }
13427 #endif
13428 #if defined(INET6) && defined(INET)
13429 else
13430 #endif
13431 #ifdef INET
13432 {
13433 if (tp->t_port) {
13434 m->m_pkthdr.csum_flags = CSUM_UDP;
13435 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13436 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13437 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13438 th->th_sum = htons(0);
13439 UDPSTAT_INC(udps_opackets);
13440 } else {
13441 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP;
13442 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13443 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13444 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13445 IPPROTO_TCP + len + optlen));
13446 }
13447 /* IP version must be set here for ipv4/ipv6 checking later */
13448 KASSERT(ip->ip_v == IPVERSION,
13449 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13450 }
13451 #endif
13452
13453 /*
13454 * Enable TSO and specify the size of the segments. The TCP pseudo
13455 * header checksum is always provided. XXX: Fixme: This is currently
13456 * not the case for IPv6.
13457 */
13458 if (tso) {
13459 KASSERT(len > maxseg,
13460 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg));
13461 m->m_pkthdr.csum_flags |= CSUM_TSO;
13462 csum_flags |= CSUM_TSO;
13463 m->m_pkthdr.tso_segsz = maxseg;
13464 }
13465 KASSERT(len + hdrlen == m_length(m, NULL),
13466 ("%s: mbuf chain different than expected: %d + %u != %u",
13467 __func__, len, hdrlen, m_length(m, NULL)));
13468
13469 #ifdef TCP_HHOOK
13470 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */
13471 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13472 #endif
13473
13474 /* Log to the black box */
13475 if (tcp_bblogging_on(tp)) {
13476 union tcp_log_stackspecific log;
13477
13478 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
13479 /* Record info on type of transmission */
13480 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay;
13481 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3);
13482 log.u_bbr.flex3 = maxseg;
13483 log.u_bbr.flex4 = delay_calc;
13484 log.u_bbr.flex5 = bbr->rc_past_init_win;
13485 log.u_bbr.flex5 <<= 1;
13486 log.u_bbr.flex5 |= bbr->rc_no_pacing;
13487 log.u_bbr.flex5 <<= 29;
13488 log.u_bbr.flex5 |= tp->t_maxseg;
13489 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs;
13490 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr);
13491 /* lets poke in the low and the high here for debugging */
13492 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
13493 if (rsm || sack_rxmit) {
13494 if (doing_tlp)
13495 log.u_bbr.flex8 = 2;
13496 else
13497 log.u_bbr.flex8 = 1;
13498 } else {
13499 log.u_bbr.flex8 = 0;
13500 }
13501 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13502 len, &log, false, NULL, NULL, 0, tv);
13503 } else {
13504 lgb = NULL;
13505 }
13506 /*
13507 * Fill in IP length and desired time to live and send to IP level.
13508 * There should be a better way to handle ttl and tos; we could keep
13509 * them in the template, but need a way to checksum without them.
13510 */
13511 /*
13512 * m->m_pkthdr.len should have been set before cksum calcuration,
13513 * because in6_cksum() need it.
13514 */
13515 #ifdef INET6
13516 if (isipv6) {
13517 /*
13518 * we separately set hoplimit for every segment, since the
13519 * user might want to change the value via setsockopt. Also,
13520 * desired default hop limit might be changed via Neighbor
13521 * Discovery.
13522 */
13523 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13524
13525 /*
13526 * Set the packet size here for the benefit of DTrace
13527 * probes. ip6_output() will set it properly; it's supposed
13528 * to include the option header lengths as well.
13529 */
13530 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13531
13532 if (V_path_mtu_discovery && maxseg > V_tcp_minmss)
13533 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13534 else
13535 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13536
13537 if (tp->t_state == TCPS_SYN_SENT)
13538 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13539
13540 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13541 /* TODO: IPv6 IP6TOS_ECT bit on */
13542 error = ip6_output(m, inp->in6p_outputopts,
13543 &inp->inp_route6,
13544 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13545 NULL, NULL, inp);
13546
13547 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
13548 mtu = inp->inp_route6.ro_nh->nh_mtu;
13549 }
13550 #endif /* INET6 */
13551 #if defined(INET) && defined(INET6)
13552 else
13553 #endif
13554 #ifdef INET
13555 {
13556 ip->ip_len = htons(m->m_pkthdr.len);
13557 #ifdef INET6
13558 if (isipv6)
13559 ip->ip_ttl = in6_selecthlim(inp, NULL);
13560 #endif /* INET6 */
13561 /*
13562 * If we do path MTU discovery, then we set DF on every
13563 * packet. This might not be the best thing to do according
13564 * to RFC3390 Section 2. However the tcp hostcache migitates
13565 * the problem so it affects only the first tcp connection
13566 * with a host.
13567 *
13568 * NB: Don't set DF on small MTU/MSS to have a safe
13569 * fallback.
13570 */
13571 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13572 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13573 if (tp->t_port == 0 || len < V_tcp_minmss) {
13574 ip->ip_off |= htons(IP_DF);
13575 }
13576 } else {
13577 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13578 }
13579
13580 if (tp->t_state == TCPS_SYN_SENT)
13581 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
13582
13583 TCP_PROBE5(send, NULL, tp, ip, tp, th);
13584
13585 error = ip_output(m, inp->inp_options, &inp->inp_route,
13586 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
13587 inp);
13588 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
13589 mtu = inp->inp_route.ro_nh->nh_mtu;
13590 }
13591 #endif /* INET */
13592 if (lgb) {
13593 lgb->tlb_errno = error;
13594 lgb = NULL;
13595 }
13596
13597 out:
13598 /*
13599 * In transmit state, time the transmission and arrange for the
13600 * retransmit. In persist state, just set snd_max.
13601 */
13602 if (error == 0) {
13603 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
13604 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13605 (tp->t_flags & TF_SACK_PERMIT) &&
13606 tp->rcv_numsacks > 0)
13607 tcp_clean_dsack_blocks(tp);
13608 /* We sent an ack clear the bbr_segs_rcvd count */
13609 bbr->output_error_seen = 0;
13610 bbr->oerror_cnt = 0;
13611 bbr->bbr_segs_rcvd = 0;
13612 if (len == 0)
13613 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1);
13614 /* Do accounting for new sends */
13615 if ((len > 0) && (rsm == NULL)) {
13616 int idx;
13617 if (tp->snd_una == tp->snd_max) {
13618 /*
13619 * Special case to match google, when
13620 * nothing is in flight the delivered
13621 * time does get updated to the current
13622 * time (see tcp_rate_bsd.c).
13623 */
13624 bbr->r_ctl.rc_del_time = cts;
13625 }
13626 if (len >= maxseg) {
13627 idx = (len / maxseg) + 3;
13628 if (idx >= TCP_MSS_ACCT_ATIMER)
13629 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1);
13630 else
13631 counter_u64_add(bbr_out_size[idx], 1);
13632 } else {
13633 /* smaller than a MSS */
13634 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options);
13635 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV)
13636 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1);
13637 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1);
13638 }
13639 }
13640 }
13641 abandon = 0;
13642 /*
13643 * We must do the send accounting before we log the output,
13644 * otherwise the state of the rsm could change and we account to the
13645 * wrong bucket.
13646 */
13647 if (len > 0) {
13648 bbr_do_send_accounting(tp, bbr, rsm, len, error);
13649 if (error == 0) {
13650 if (tp->snd_una == tp->snd_max)
13651 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
13652 }
13653 }
13654 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error,
13655 cts, mb, &abandon, rsm, 0, sb);
13656 if (abandon) {
13657 /*
13658 * If bbr_log_output destroys the TCB or sees a TH_RST being
13659 * sent we should hit this condition.
13660 */
13661 return (0);
13662 }
13663 if (bbr->rc_in_persist == 0) {
13664 /*
13665 * Advance snd_nxt over sequence space of this segment.
13666 */
13667 if (error)
13668 /* We don't log or do anything with errors */
13669 goto skip_upd;
13670
13671 if (tp->snd_una == tp->snd_max &&
13672 (len || (flags & (TH_SYN | TH_FIN)))) {
13673 /*
13674 * Update the time we just added data since none was
13675 * outstanding.
13676 */
13677 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13678 bbr->rc_tp->t_acktime = ticks;
13679 }
13680 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) {
13681 if (flags & TH_SYN) {
13682 /*
13683 * Smack the snd_max to iss + 1
13684 * if its a FO we will add len below.
13685 */
13686 tp->snd_max = tp->iss + 1;
13687 }
13688 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13689 tp->snd_max++;
13690 tp->t_flags |= TF_SENTFIN;
13691 }
13692 }
13693 if (sack_rxmit == 0)
13694 tp->snd_max += len;
13695 skip_upd:
13696 if ((error == 0) && len)
13697 tot_len += len;
13698 } else {
13699 /* Persists case */
13700 int32_t xlen = len;
13701
13702 if (error)
13703 goto nomore;
13704
13705 if (flags & TH_SYN)
13706 ++xlen;
13707 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
13708 ++xlen;
13709 tp->t_flags |= TF_SENTFIN;
13710 }
13711 if (xlen && (tp->snd_una == tp->snd_max)) {
13712 /*
13713 * Update the time we just added data since none was
13714 * outstanding.
13715 */
13716 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
13717 bbr->rc_tp->t_acktime = ticks;
13718 }
13719 if (sack_rxmit == 0)
13720 tp->snd_max += xlen;
13721 tot_len += (len + optlen + ipoptlen);
13722 }
13723 nomore:
13724 if (error) {
13725 /*
13726 * Failures do not advance the seq counter above. For the
13727 * case of ENOBUFS we will fall out and become ack-clocked.
13728 * capping the cwnd at the current flight.
13729 * Everything else will just have to retransmit with the timer
13730 * (no pacer).
13731 */
13732 SOCKBUF_UNLOCK_ASSERT(sb);
13733 BBR_STAT_INC(bbr_saw_oerr);
13734 /* Clear all delay/early tracks */
13735 bbr->r_ctl.rc_hptsi_agg_delay = 0;
13736 bbr->r_ctl.rc_agg_early = 0;
13737 bbr->r_agg_early_set = 0;
13738 bbr->output_error_seen = 1;
13739 if (bbr->oerror_cnt < 0xf)
13740 bbr->oerror_cnt++;
13741 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) {
13742 /* drop the session */
13743 return (-ENETDOWN);
13744 }
13745 switch (error) {
13746 case ENOBUFS:
13747 /*
13748 * Make this guy have to get ack's to send
13749 * more but lets make sure we don't
13750 * slam him below a T-O (1MSS).
13751 */
13752 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
13753 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13754 bbr->r_ctl.rc_lost_bytes)) - maxseg;
13755 if (tp->snd_cwnd < maxseg)
13756 tp->snd_cwnd = maxseg;
13757 }
13758 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt;
13759 BBR_STAT_INC(bbr_saw_enobuf);
13760 if (bbr->bbr_hdrw_pacing)
13761 counter_u64_add(bbr_hdwr_pacing_enobuf, 1);
13762 else
13763 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1);
13764 /*
13765 * Here even in the enobuf's case we want to do our
13766 * state update. The reason being we may have been
13767 * called by the input function. If so we have had
13768 * things change.
13769 */
13770 error = 0;
13771 goto enobufs;
13772 case EMSGSIZE:
13773 /*
13774 * For some reason the interface we used initially
13775 * to send segments changed to another or lowered
13776 * its MTU. If TSO was active we either got an
13777 * interface without TSO capabilits or TSO was
13778 * turned off. If we obtained mtu from ip_output()
13779 * then update it and try again.
13780 */
13781 /* Turn on tracing (or try to) */
13782 {
13783 int old_maxseg;
13784
13785 old_maxseg = tp->t_maxseg;
13786 BBR_STAT_INC(bbr_saw_emsgsiz);
13787 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts);
13788 if (mtu != 0)
13789 tcp_mss_update(tp, -1, mtu, NULL, NULL);
13790 if (old_maxseg <= tp->t_maxseg) {
13791 /* Huh it did not shrink? */
13792 tp->t_maxseg = old_maxseg - 40;
13793 if (tp->t_maxseg < V_tcp_mssdflt) {
13794 /*
13795 * The MSS is so small we should not
13796 * process incoming SACK's since we are
13797 * subject to attack in such a case.
13798 */
13799 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
13800 } else {
13801 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
13802 }
13803 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts);
13804 }
13805 /*
13806 * Nuke all other things that can interfere
13807 * with slot
13808 */
13809 if ((tot_len + len) && (len >= tp->t_maxseg)) {
13810 slot = bbr_get_pacing_delay(bbr,
13811 bbr->r_ctl.rc_bbr_hptsi_gain,
13812 (tot_len + len), cts, 0);
13813 if (slot < bbr_error_base_paceout)
13814 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
13815 } else
13816 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
13817 bbr->rc_output_starts_timer = 1;
13818 bbr_start_hpts_timer(bbr, tp, cts, 10, slot,
13819 tot_len);
13820 return (error);
13821 }
13822 case EPERM:
13823 case EACCES:
13824 tp->t_softerror = error;
13825 /* FALLTHROUGH */
13826 case EHOSTDOWN:
13827 case EHOSTUNREACH:
13828 case ENETDOWN:
13829 case ENETUNREACH:
13830 if (TCPS_HAVERCVDSYN(tp->t_state)) {
13831 tp->t_softerror = error;
13832 }
13833 /* FALLTHROUGH */
13834 default:
13835 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt;
13836 bbr->rc_output_starts_timer = 1;
13837 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0);
13838 return (error);
13839 }
13840 #ifdef STATS
13841 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) &&
13842 len &&
13843 (rsm == NULL) &&
13844 (bbr->rc_in_persist == 0)) {
13845 tp->gput_seq = bbr_seq;
13846 tp->gput_ack = bbr_seq +
13847 min(sbavail(&so->so_snd) - sb_offset, sendwin);
13848 tp->gput_ts = cts;
13849 tp->t_flags |= TF_GPUTINPROG;
13850 #endif
13851 }
13852 KMOD_TCPSTAT_INC(tcps_sndtotal);
13853 if ((bbr->bbr_hdw_pace_ena) &&
13854 (bbr->bbr_attempt_hdwr_pace == 0) &&
13855 (bbr->rc_past_init_win) &&
13856 (bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
13857 (get_filter_value(&bbr->r_ctl.rc_delrate)) &&
13858 (inp->inp_route.ro_nh &&
13859 inp->inp_route.ro_nh->nh_ifp)) {
13860 /*
13861 * We are past the initial window and
13862 * have at least one measurement so we
13863 * could use hardware pacing if its available.
13864 * We have an interface and we have not attempted
13865 * to setup hardware pacing, lets try to now.
13866 */
13867 uint64_t rate_wanted;
13868 int err = 0;
13869
13870 rate_wanted = bbr_get_hardware_rate(bbr);
13871 bbr->bbr_attempt_hdwr_pace = 1;
13872 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp,
13873 inp->inp_route.ro_nh->nh_ifp,
13874 rate_wanted,
13875 (RS_PACING_GEQ|RS_PACING_SUB_OK),
13876 &err, NULL);
13877 if (bbr->r_ctl.crte) {
13878 bbr_type_log_hdwr_pacing(bbr,
13879 bbr->r_ctl.crte->ptbl->rs_ifp,
13880 rate_wanted,
13881 bbr->r_ctl.crte->rate,
13882 __LINE__, cts, err);
13883 BBR_STAT_INC(bbr_hdwr_rl_add_ok);
13884 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
13885 counter_u64_add(bbr_flows_whdwr_pacing, 1);
13886 bbr->bbr_hdrw_pacing = 1;
13887 /* Now what is our gain status? */
13888 if (bbr->r_ctl.crte->rate < rate_wanted) {
13889 /* We have a problem */
13890 bbr_setup_less_of_rate(bbr, cts,
13891 bbr->r_ctl.crte->rate, rate_wanted);
13892 } else {
13893 /* We are good */
13894 bbr->gain_is_limited = 0;
13895 bbr->skip_gain = 0;
13896 }
13897 tcp_bbr_tso_size_check(bbr, cts);
13898 } else {
13899 bbr_type_log_hdwr_pacing(bbr,
13900 inp->inp_route.ro_nh->nh_ifp,
13901 rate_wanted,
13902 0,
13903 __LINE__, cts, err);
13904 BBR_STAT_INC(bbr_hdwr_rl_add_fail);
13905 }
13906 }
13907 if (bbr->bbr_hdrw_pacing) {
13908 /*
13909 * Worry about cases where the route
13910 * changes or something happened that we
13911 * lost our hardware pacing possibly during
13912 * the last ip_output call.
13913 */
13914 if (inp->inp_snd_tag == NULL) {
13915 /* A change during ip output disabled hw pacing? */
13916 bbr->bbr_hdrw_pacing = 0;
13917 } else if ((inp->inp_route.ro_nh == NULL) ||
13918 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) {
13919 /*
13920 * We had an interface or route change,
13921 * detach from the current hdwr pacing
13922 * and setup to re-attempt next go
13923 * round.
13924 */
13925 bbr->bbr_hdrw_pacing = 0;
13926 bbr->bbr_attempt_hdwr_pace = 0;
13927 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
13928 tcp_bbr_tso_size_check(bbr, cts);
13929 }
13930 }
13931 /*
13932 * Data sent (as far as we can tell). If this advertises a larger
13933 * window than any other segment, then remember the size of the
13934 * advertised window. Any pending ACK has now been sent.
13935 */
13936 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
13937 tp->rcv_adv = tp->rcv_nxt + recwin;
13938
13939 tp->last_ack_sent = tp->rcv_nxt;
13940 if ((error == 0) &&
13941 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) &&
13942 (doing_tlp == 0) &&
13943 (tso == 0) &&
13944 (len > 0) &&
13945 ((flags & TH_RST) == 0) &&
13946 ((flags & TH_SYN) == 0) &&
13947 (IN_RECOVERY(tp->t_flags) == 0) &&
13948 (bbr->rc_in_persist == 0) &&
13949 (tot_len < bbr->r_ctl.rc_pace_max_segs)) {
13950 /*
13951 * For non-tso we need to goto again until we have sent out
13952 * enough data to match what we are hptsi out every hptsi
13953 * interval.
13954 */
13955 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
13956 /* Make sure snd_nxt is drug up */
13957 tp->snd_nxt = tp->snd_max;
13958 }
13959 if (rsm != NULL) {
13960 rsm = NULL;
13961 goto skip_again;
13962 }
13963 rsm = NULL;
13964 sack_rxmit = 0;
13965 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
13966 goto again;
13967 }
13968 skip_again:
13969 if ((error == 0) && (flags & TH_FIN))
13970 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
13971 if ((error == 0) && (flags & TH_RST))
13972 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13973 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) {
13974 /*
13975 * Calculate/Re-Calculate the hptsi slot in usecs based on
13976 * what we have sent so far
13977 */
13978 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
13979 if (bbr->rc_no_pacing)
13980 slot = 0;
13981 }
13982 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
13983 enobufs:
13984 if (bbr->rc_use_google == 0)
13985 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
13986 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13987 bbr->r_ctl.rc_lost_bytes)));
13988 bbr->rc_output_starts_timer = 1;
13989 if (bbr->bbr_use_rack_cheat &&
13990 (more_to_rxt ||
13991 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) {
13992 /* Rack cheats and shotguns out all rxt's 1ms apart */
13993 if (slot > 1000)
13994 slot = 1000;
13995 }
13996 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) {
13997 /*
13998 * We don't change the tso size until some number of sends
13999 * to give the hardware commands time to get down
14000 * to the interface.
14001 */
14002 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++;
14003 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) {
14004 bbr->hw_pacing_set = 1;
14005 tcp_bbr_tso_size_check(bbr, cts);
14006 }
14007 }
14008 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len);
14009 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14010 /* Make sure snd_nxt is drug up */
14011 tp->snd_nxt = tp->snd_max;
14012 }
14013 return (error);
14014
14015 }
14016
14017 /*
14018 * See bbr_output_wtime() for return values.
14019 */
14020 static int
bbr_output(struct tcpcb * tp)14021 bbr_output(struct tcpcb *tp)
14022 {
14023 int32_t ret;
14024 struct timeval tv;
14025
14026 NET_EPOCH_ASSERT();
14027
14028 INP_WLOCK_ASSERT(tptoinpcb(tp));
14029 (void)tcp_get_usecs(&tv);
14030 ret = bbr_output_wtime(tp, &tv);
14031 return (ret);
14032 }
14033
14034 static void
bbr_mtu_chg(struct tcpcb * tp)14035 bbr_mtu_chg(struct tcpcb *tp)
14036 {
14037 struct tcp_bbr *bbr;
14038 struct bbr_sendmap *rsm, *frsm = NULL;
14039 uint32_t maxseg;
14040
14041 /*
14042 * The MTU has changed. a) Clear the sack filter. b) Mark everything
14043 * over the current size as SACK_PASS so a retransmit will occur.
14044 */
14045
14046 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14047 maxseg = tp->t_maxseg - bbr->rc_last_options;
14048 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
14049 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
14050 /* Don't mess with ones acked (by sack?) */
14051 if (rsm->r_flags & BBR_ACKED)
14052 continue;
14053 if ((rsm->r_end - rsm->r_start) > maxseg) {
14054 /*
14055 * We mark sack-passed on all the previous large
14056 * sends we did. This will force them to retransmit.
14057 */
14058 rsm->r_flags |= BBR_SACK_PASSED;
14059 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) &&
14060 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) {
14061 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
14062 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
14063 rsm->r_flags |= BBR_MARKED_LOST;
14064 }
14065 if (frsm == NULL)
14066 frsm = rsm;
14067 }
14068 }
14069 if (frsm) {
14070 bbr->r_ctl.rc_resend = frsm;
14071 }
14072 }
14073
14074 static int
bbr_pru_options(struct tcpcb * tp,int flags)14075 bbr_pru_options(struct tcpcb *tp, int flags)
14076 {
14077 if (flags & PRUS_OOB)
14078 return (EOPNOTSUPP);
14079 return (0);
14080 }
14081
14082 static void
bbr_switch_failed(struct tcpcb * tp)14083 bbr_switch_failed(struct tcpcb *tp)
14084 {
14085 /*
14086 * If a switch fails we only need to
14087 * make sure mbuf_queuing is still in place.
14088 * We also need to make sure we are still in
14089 * ticks granularity (though we should probably
14090 * change bbr to go to USECs).
14091 *
14092 * For timers we need to see if we are still in the
14093 * pacer (if our flags are up) if so we are good, if
14094 * not we need to get back into the pacer.
14095 */
14096 struct timeval tv;
14097 uint32_t cts;
14098 uint32_t toval;
14099 struct tcp_bbr *bbr;
14100 struct hpts_diag diag;
14101
14102 tp->t_flags2 |= TF2_CANNOT_DO_ECN;
14103 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14104 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
14105 if (tp->t_in_hpts > IHPTS_NONE) {
14106 return;
14107 }
14108 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14109 cts = tcp_get_usecs(&tv);
14110 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14111 if (TSTMP_GT(bbr->rc_pacer_started, cts)) {
14112 toval = bbr->rc_pacer_started - cts;
14113 } else {
14114 /* one slot please */
14115 toval = HPTS_TICKS_PER_SLOT;
14116 }
14117 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14118 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
14119 toval = bbr->r_ctl.rc_timer_exp - cts;
14120 } else {
14121 /* one slot please */
14122 toval = HPTS_TICKS_PER_SLOT;
14123 }
14124 } else
14125 toval = HPTS_TICKS_PER_SLOT;
14126 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14127 __LINE__, &diag);
14128 bbr_log_hpts_diag(bbr, cts, &diag);
14129 }
14130
14131 struct tcp_function_block __tcp_bbr = {
14132 .tfb_tcp_block_name = __XSTRING(STACKNAME),
14133 .tfb_tcp_output = bbr_output,
14134 .tfb_do_queued_segments = ctf_do_queued_segments,
14135 .tfb_do_segment_nounlock = bbr_do_segment_nounlock,
14136 .tfb_tcp_do_segment = bbr_do_segment,
14137 .tfb_tcp_ctloutput = bbr_ctloutput,
14138 .tfb_tcp_fb_init = bbr_init,
14139 .tfb_tcp_fb_fini = bbr_fini,
14140 .tfb_tcp_timer_stop_all = bbr_stopall,
14141 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr,
14142 .tfb_tcp_handoff_ok = bbr_handoff_ok,
14143 .tfb_tcp_mtu_chg = bbr_mtu_chg,
14144 .tfb_pru_options = bbr_pru_options,
14145 .tfb_switch_failed = bbr_switch_failed,
14146 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
14147 };
14148
14149 /*
14150 * bbr_ctloutput() must drop the inpcb lock before performing copyin on
14151 * socket option arguments. When it re-acquires the lock after the copy, it
14152 * has to revalidate that the connection is still valid for the socket
14153 * option.
14154 */
14155 static int
bbr_set_sockopt(struct tcpcb * tp,struct sockopt * sopt)14156 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
14157 {
14158 struct epoch_tracker et;
14159 struct inpcb *inp = tptoinpcb(tp);
14160 struct tcp_bbr *bbr;
14161 int32_t error = 0, optval;
14162
14163 switch (sopt->sopt_level) {
14164 case IPPROTO_IPV6:
14165 case IPPROTO_IP:
14166 return (tcp_default_ctloutput(tp, sopt));
14167 }
14168
14169 switch (sopt->sopt_name) {
14170 case TCP_RACK_PACE_MAX_SEG:
14171 case TCP_RACK_MIN_TO:
14172 case TCP_RACK_REORD_THRESH:
14173 case TCP_RACK_REORD_FADE:
14174 case TCP_RACK_TLP_THRESH:
14175 case TCP_RACK_PKT_DELAY:
14176 case TCP_BBR_ALGORITHM:
14177 case TCP_BBR_TSLIMITS:
14178 case TCP_BBR_IWINTSO:
14179 case TCP_BBR_RECFORCE:
14180 case TCP_BBR_STARTUP_PG:
14181 case TCP_BBR_DRAIN_PG:
14182 case TCP_BBR_RWND_IS_APP:
14183 case TCP_BBR_PROBE_RTT_INT:
14184 case TCP_BBR_PROBE_RTT_GAIN:
14185 case TCP_BBR_PROBE_RTT_LEN:
14186 case TCP_BBR_STARTUP_LOSS_EXIT:
14187 case TCP_BBR_USEDEL_RATE:
14188 case TCP_BBR_MIN_RTO:
14189 case TCP_BBR_MAX_RTO:
14190 case TCP_BBR_PACE_PER_SEC:
14191 case TCP_DELACK:
14192 case TCP_BBR_PACE_DEL_TAR:
14193 case TCP_BBR_SEND_IWND_IN_TSO:
14194 case TCP_BBR_EXTRA_STATE:
14195 case TCP_BBR_UTTER_MAX_TSO:
14196 case TCP_BBR_MIN_TOPACEOUT:
14197 case TCP_BBR_FLOOR_MIN_TSO:
14198 case TCP_BBR_TSTMP_RAISES:
14199 case TCP_BBR_POLICER_DETECT:
14200 case TCP_BBR_USE_RACK_CHEAT:
14201 case TCP_DATA_AFTER_CLOSE:
14202 case TCP_BBR_HDWR_PACE:
14203 case TCP_BBR_PACE_SEG_MAX:
14204 case TCP_BBR_PACE_SEG_MIN:
14205 case TCP_BBR_PACE_CROSS:
14206 case TCP_BBR_PACE_OH:
14207 case TCP_BBR_TMR_PACE_OH:
14208 case TCP_BBR_RACK_RTT_USE:
14209 case TCP_BBR_RETRAN_WTSO:
14210 break;
14211 default:
14212 return (tcp_default_ctloutput(tp, sopt));
14213 break;
14214 }
14215 INP_WUNLOCK(inp);
14216 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14217 if (error)
14218 return (error);
14219 INP_WLOCK(inp);
14220 if (inp->inp_flags & INP_DROPPED) {
14221 INP_WUNLOCK(inp);
14222 return (ECONNRESET);
14223 }
14224 if (tp->t_fb != &__tcp_bbr) {
14225 INP_WUNLOCK(inp);
14226 return (ENOPROTOOPT);
14227 }
14228 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14229 switch (sopt->sopt_name) {
14230 case TCP_BBR_PACE_PER_SEC:
14231 BBR_OPTS_INC(tcp_bbr_pace_per_sec);
14232 bbr->r_ctl.bbr_hptsi_per_second = optval;
14233 break;
14234 case TCP_BBR_PACE_DEL_TAR:
14235 BBR_OPTS_INC(tcp_bbr_pace_del_tar);
14236 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval;
14237 break;
14238 case TCP_BBR_PACE_SEG_MAX:
14239 BBR_OPTS_INC(tcp_bbr_pace_seg_max);
14240 bbr->r_ctl.bbr_hptsi_segments_max = optval;
14241 break;
14242 case TCP_BBR_PACE_SEG_MIN:
14243 BBR_OPTS_INC(tcp_bbr_pace_seg_min);
14244 bbr->r_ctl.bbr_hptsi_bytes_min = optval;
14245 break;
14246 case TCP_BBR_PACE_CROSS:
14247 BBR_OPTS_INC(tcp_bbr_pace_cross);
14248 bbr->r_ctl.bbr_cross_over = optval;
14249 break;
14250 case TCP_BBR_ALGORITHM:
14251 BBR_OPTS_INC(tcp_bbr_algorithm);
14252 if (optval && (bbr->rc_use_google == 0)) {
14253 /* Turn on the google mode */
14254 bbr_google_mode_on(bbr);
14255 if ((optval > 3) && (optval < 500)) {
14256 /*
14257 * Must be at least greater than .3%
14258 * and must be less than 50.0%.
14259 */
14260 bbr->r_ctl.bbr_google_discount = optval;
14261 }
14262 } else if ((optval == 0) && (bbr->rc_use_google == 1)) {
14263 /* Turn off the google mode */
14264 bbr_google_mode_off(bbr);
14265 }
14266 break;
14267 case TCP_BBR_TSLIMITS:
14268 BBR_OPTS_INC(tcp_bbr_tslimits);
14269 if (optval == 1)
14270 bbr->rc_use_ts_limit = 1;
14271 else if (optval == 0)
14272 bbr->rc_use_ts_limit = 0;
14273 else
14274 error = EINVAL;
14275 break;
14276
14277 case TCP_BBR_IWINTSO:
14278 BBR_OPTS_INC(tcp_bbr_iwintso);
14279 if ((optval >= 0) && (optval < 128)) {
14280 uint32_t twin;
14281
14282 bbr->rc_init_win = optval;
14283 twin = bbr_initial_cwnd(bbr, tp);
14284 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd))
14285 tp->snd_cwnd = twin;
14286 else
14287 error = EBUSY;
14288 } else
14289 error = EINVAL;
14290 break;
14291 case TCP_BBR_STARTUP_PG:
14292 BBR_OPTS_INC(tcp_bbr_startup_pg);
14293 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) {
14294 bbr->r_ctl.rc_startup_pg = optval;
14295 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
14296 bbr->r_ctl.rc_bbr_hptsi_gain = optval;
14297 }
14298 } else
14299 error = EINVAL;
14300 break;
14301 case TCP_BBR_DRAIN_PG:
14302 BBR_OPTS_INC(tcp_bbr_drain_pg);
14303 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE))
14304 bbr->r_ctl.rc_drain_pg = optval;
14305 else
14306 error = EINVAL;
14307 break;
14308 case TCP_BBR_PROBE_RTT_LEN:
14309 BBR_OPTS_INC(tcp_bbr_probertt_len);
14310 if (optval <= 1)
14311 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND));
14312 else
14313 error = EINVAL;
14314 break;
14315 case TCP_BBR_PROBE_RTT_GAIN:
14316 BBR_OPTS_INC(tcp_bbr_probertt_gain);
14317 if (optval <= BBR_UNIT)
14318 bbr->r_ctl.bbr_rttprobe_gain_val = optval;
14319 else
14320 error = EINVAL;
14321 break;
14322 case TCP_BBR_PROBE_RTT_INT:
14323 BBR_OPTS_INC(tcp_bbr_probe_rtt_int);
14324 if (optval > 1000)
14325 bbr->r_ctl.rc_probertt_int = optval;
14326 else
14327 error = EINVAL;
14328 break;
14329 case TCP_BBR_MIN_TOPACEOUT:
14330 BBR_OPTS_INC(tcp_bbr_topaceout);
14331 if (optval == 0) {
14332 bbr->no_pacing_until = 0;
14333 bbr->rc_no_pacing = 0;
14334 } else if (optval <= 0x00ff) {
14335 bbr->no_pacing_until = optval;
14336 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) &&
14337 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){
14338 /* Turn on no pacing */
14339 bbr->rc_no_pacing = 1;
14340 }
14341 } else
14342 error = EINVAL;
14343 break;
14344 case TCP_BBR_STARTUP_LOSS_EXIT:
14345 BBR_OPTS_INC(tcp_bbr_startup_loss_exit);
14346 bbr->rc_loss_exit = optval;
14347 break;
14348 case TCP_BBR_USEDEL_RATE:
14349 error = EINVAL;
14350 break;
14351 case TCP_BBR_MIN_RTO:
14352 BBR_OPTS_INC(tcp_bbr_min_rto);
14353 bbr->r_ctl.rc_min_rto_ms = optval;
14354 break;
14355 case TCP_BBR_MAX_RTO:
14356 BBR_OPTS_INC(tcp_bbr_max_rto);
14357 bbr->rc_max_rto_sec = optval;
14358 break;
14359 case TCP_RACK_MIN_TO:
14360 /* Minimum time between rack t-o's in ms */
14361 BBR_OPTS_INC(tcp_rack_min_to);
14362 bbr->r_ctl.rc_min_to = optval;
14363 break;
14364 case TCP_RACK_REORD_THRESH:
14365 /* RACK reorder threshold (shift amount) */
14366 BBR_OPTS_INC(tcp_rack_reord_thresh);
14367 if ((optval > 0) && (optval < 31))
14368 bbr->r_ctl.rc_reorder_shift = optval;
14369 else
14370 error = EINVAL;
14371 break;
14372 case TCP_RACK_REORD_FADE:
14373 /* Does reordering fade after ms time */
14374 BBR_OPTS_INC(tcp_rack_reord_fade);
14375 bbr->r_ctl.rc_reorder_fade = optval;
14376 break;
14377 case TCP_RACK_TLP_THRESH:
14378 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14379 BBR_OPTS_INC(tcp_rack_tlp_thresh);
14380 if (optval)
14381 bbr->rc_tlp_threshold = optval;
14382 else
14383 error = EINVAL;
14384 break;
14385 case TCP_BBR_USE_RACK_CHEAT:
14386 BBR_OPTS_INC(tcp_use_rackcheat);
14387 if (bbr->rc_use_google) {
14388 error = EINVAL;
14389 break;
14390 }
14391 BBR_OPTS_INC(tcp_rack_cheat);
14392 if (optval)
14393 bbr->bbr_use_rack_cheat = 1;
14394 else
14395 bbr->bbr_use_rack_cheat = 0;
14396 break;
14397 case TCP_BBR_FLOOR_MIN_TSO:
14398 BBR_OPTS_INC(tcp_utter_max_tso);
14399 if ((optval >= 0) && (optval < 40))
14400 bbr->r_ctl.bbr_hptsi_segments_floor = optval;
14401 else
14402 error = EINVAL;
14403 break;
14404 case TCP_BBR_UTTER_MAX_TSO:
14405 BBR_OPTS_INC(tcp_utter_max_tso);
14406 if ((optval >= 0) && (optval < 0xffff))
14407 bbr->r_ctl.bbr_utter_max = optval;
14408 else
14409 error = EINVAL;
14410 break;
14411
14412 case TCP_BBR_EXTRA_STATE:
14413 BBR_OPTS_INC(tcp_extra_state);
14414 if (optval)
14415 bbr->rc_use_idle_restart = 1;
14416 else
14417 bbr->rc_use_idle_restart = 0;
14418 break;
14419 case TCP_BBR_SEND_IWND_IN_TSO:
14420 BBR_OPTS_INC(tcp_iwnd_tso);
14421 if (optval) {
14422 bbr->bbr_init_win_cheat = 1;
14423 if (bbr->rc_past_init_win == 0) {
14424 uint32_t cts;
14425 cts = tcp_get_usecs(&bbr->rc_tv);
14426 tcp_bbr_tso_size_check(bbr, cts);
14427 }
14428 } else
14429 bbr->bbr_init_win_cheat = 0;
14430 break;
14431 case TCP_BBR_HDWR_PACE:
14432 BBR_OPTS_INC(tcp_hdwr_pacing);
14433 if (optval){
14434 bbr->bbr_hdw_pace_ena = 1;
14435 bbr->bbr_attempt_hdwr_pace = 0;
14436 } else {
14437 bbr->bbr_hdw_pace_ena = 0;
14438 #ifdef RATELIMIT
14439 if (bbr->r_ctl.crte != NULL) {
14440 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp);
14441 bbr->r_ctl.crte = NULL;
14442 }
14443 #endif
14444 }
14445 break;
14446
14447 case TCP_DELACK:
14448 BBR_OPTS_INC(tcp_delack);
14449 if (optval < 100) {
14450 if (optval == 0) /* off */
14451 tp->t_delayed_ack = 0;
14452 else if (optval == 1) /* on which is 2 */
14453 tp->t_delayed_ack = 2;
14454 else /* higher than 2 and less than 100 */
14455 tp->t_delayed_ack = optval;
14456 if (tp->t_flags & TF_DELACK) {
14457 tp->t_flags &= ~TF_DELACK;
14458 tp->t_flags |= TF_ACKNOW;
14459 NET_EPOCH_ENTER(et);
14460 bbr_output(tp);
14461 NET_EPOCH_EXIT(et);
14462 }
14463 } else
14464 error = EINVAL;
14465 break;
14466 case TCP_RACK_PKT_DELAY:
14467 /* RACK added ms i.e. rack-rtt + reord + N */
14468 BBR_OPTS_INC(tcp_rack_pkt_delay);
14469 bbr->r_ctl.rc_pkt_delay = optval;
14470 break;
14471
14472 case TCP_BBR_RETRAN_WTSO:
14473 BBR_OPTS_INC(tcp_retran_wtso);
14474 if (optval)
14475 bbr->rc_resends_use_tso = 1;
14476 else
14477 bbr->rc_resends_use_tso = 0;
14478 break;
14479 case TCP_DATA_AFTER_CLOSE:
14480 BBR_OPTS_INC(tcp_data_ac);
14481 if (optval)
14482 bbr->rc_allow_data_af_clo = 1;
14483 else
14484 bbr->rc_allow_data_af_clo = 0;
14485 break;
14486 case TCP_BBR_POLICER_DETECT:
14487 BBR_OPTS_INC(tcp_policer_det);
14488 if (bbr->rc_use_google == 0)
14489 error = EINVAL;
14490 else if (optval)
14491 bbr->r_use_policer = 1;
14492 else
14493 bbr->r_use_policer = 0;
14494 break;
14495
14496 case TCP_BBR_TSTMP_RAISES:
14497 BBR_OPTS_INC(tcp_ts_raises);
14498 if (optval)
14499 bbr->ts_can_raise = 1;
14500 else
14501 bbr->ts_can_raise = 0;
14502 break;
14503 case TCP_BBR_TMR_PACE_OH:
14504 BBR_OPTS_INC(tcp_pacing_oh_tmr);
14505 if (bbr->rc_use_google) {
14506 error = EINVAL;
14507 } else {
14508 if (optval)
14509 bbr->r_ctl.rc_incr_tmrs = 1;
14510 else
14511 bbr->r_ctl.rc_incr_tmrs = 0;
14512 }
14513 break;
14514 case TCP_BBR_PACE_OH:
14515 BBR_OPTS_INC(tcp_pacing_oh);
14516 if (bbr->rc_use_google) {
14517 error = EINVAL;
14518 } else {
14519 if (optval > (BBR_INCL_TCP_OH|
14520 BBR_INCL_IP_OH|
14521 BBR_INCL_ENET_OH)) {
14522 error = EINVAL;
14523 break;
14524 }
14525 if (optval & BBR_INCL_TCP_OH)
14526 bbr->r_ctl.rc_inc_tcp_oh = 1;
14527 else
14528 bbr->r_ctl.rc_inc_tcp_oh = 0;
14529 if (optval & BBR_INCL_IP_OH)
14530 bbr->r_ctl.rc_inc_ip_oh = 1;
14531 else
14532 bbr->r_ctl.rc_inc_ip_oh = 0;
14533 if (optval & BBR_INCL_ENET_OH)
14534 bbr->r_ctl.rc_inc_enet_oh = 1;
14535 else
14536 bbr->r_ctl.rc_inc_enet_oh = 0;
14537 }
14538 break;
14539 default:
14540 return (tcp_default_ctloutput(tp, sopt));
14541 break;
14542 }
14543 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14544 INP_WUNLOCK(inp);
14545 return (error);
14546 }
14547
14548 /*
14549 * return 0 on success, error-num on failure
14550 */
14551 static int
bbr_get_sockopt(struct tcpcb * tp,struct sockopt * sopt)14552 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
14553 {
14554 struct inpcb *inp = tptoinpcb(tp);
14555 struct tcp_bbr *bbr;
14556 int32_t error, optval;
14557
14558 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14559 if (bbr == NULL) {
14560 INP_WUNLOCK(inp);
14561 return (EINVAL);
14562 }
14563 /*
14564 * Because all our options are either boolean or an int, we can just
14565 * pull everything into optval and then unlock and copy. If we ever
14566 * add a option that is not a int, then this will have quite an
14567 * impact to this routine.
14568 */
14569 switch (sopt->sopt_name) {
14570 case TCP_BBR_PACE_PER_SEC:
14571 optval = bbr->r_ctl.bbr_hptsi_per_second;
14572 break;
14573 case TCP_BBR_PACE_DEL_TAR:
14574 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar;
14575 break;
14576 case TCP_BBR_PACE_SEG_MAX:
14577 optval = bbr->r_ctl.bbr_hptsi_segments_max;
14578 break;
14579 case TCP_BBR_MIN_TOPACEOUT:
14580 optval = bbr->no_pacing_until;
14581 break;
14582 case TCP_BBR_PACE_SEG_MIN:
14583 optval = bbr->r_ctl.bbr_hptsi_bytes_min;
14584 break;
14585 case TCP_BBR_PACE_CROSS:
14586 optval = bbr->r_ctl.bbr_cross_over;
14587 break;
14588 case TCP_BBR_ALGORITHM:
14589 optval = bbr->rc_use_google;
14590 break;
14591 case TCP_BBR_TSLIMITS:
14592 optval = bbr->rc_use_ts_limit;
14593 break;
14594 case TCP_BBR_IWINTSO:
14595 optval = bbr->rc_init_win;
14596 break;
14597 case TCP_BBR_STARTUP_PG:
14598 optval = bbr->r_ctl.rc_startup_pg;
14599 break;
14600 case TCP_BBR_DRAIN_PG:
14601 optval = bbr->r_ctl.rc_drain_pg;
14602 break;
14603 case TCP_BBR_PROBE_RTT_INT:
14604 optval = bbr->r_ctl.rc_probertt_int;
14605 break;
14606 case TCP_BBR_PROBE_RTT_LEN:
14607 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND);
14608 break;
14609 case TCP_BBR_PROBE_RTT_GAIN:
14610 optval = bbr->r_ctl.bbr_rttprobe_gain_val;
14611 break;
14612 case TCP_BBR_STARTUP_LOSS_EXIT:
14613 optval = bbr->rc_loss_exit;
14614 break;
14615 case TCP_BBR_USEDEL_RATE:
14616 error = EINVAL;
14617 break;
14618 case TCP_BBR_MIN_RTO:
14619 optval = bbr->r_ctl.rc_min_rto_ms;
14620 break;
14621 case TCP_BBR_MAX_RTO:
14622 optval = bbr->rc_max_rto_sec;
14623 break;
14624 case TCP_RACK_PACE_MAX_SEG:
14625 /* Max segments in a pace */
14626 optval = bbr->r_ctl.rc_pace_max_segs;
14627 break;
14628 case TCP_RACK_MIN_TO:
14629 /* Minimum time between rack t-o's in ms */
14630 optval = bbr->r_ctl.rc_min_to;
14631 break;
14632 case TCP_RACK_REORD_THRESH:
14633 /* RACK reorder threshold (shift amount) */
14634 optval = bbr->r_ctl.rc_reorder_shift;
14635 break;
14636 case TCP_RACK_REORD_FADE:
14637 /* Does reordering fade after ms time */
14638 optval = bbr->r_ctl.rc_reorder_fade;
14639 break;
14640 case TCP_BBR_USE_RACK_CHEAT:
14641 /* Do we use the rack cheat for rxt */
14642 optval = bbr->bbr_use_rack_cheat;
14643 break;
14644 case TCP_BBR_FLOOR_MIN_TSO:
14645 optval = bbr->r_ctl.bbr_hptsi_segments_floor;
14646 break;
14647 case TCP_BBR_UTTER_MAX_TSO:
14648 optval = bbr->r_ctl.bbr_utter_max;
14649 break;
14650 case TCP_BBR_SEND_IWND_IN_TSO:
14651 /* Do we send TSO size segments initially */
14652 optval = bbr->bbr_init_win_cheat;
14653 break;
14654 case TCP_BBR_EXTRA_STATE:
14655 optval = bbr->rc_use_idle_restart;
14656 break;
14657 case TCP_RACK_TLP_THRESH:
14658 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14659 optval = bbr->rc_tlp_threshold;
14660 break;
14661 case TCP_RACK_PKT_DELAY:
14662 /* RACK added ms i.e. rack-rtt + reord + N */
14663 optval = bbr->r_ctl.rc_pkt_delay;
14664 break;
14665 case TCP_BBR_RETRAN_WTSO:
14666 optval = bbr->rc_resends_use_tso;
14667 break;
14668 case TCP_DATA_AFTER_CLOSE:
14669 optval = bbr->rc_allow_data_af_clo;
14670 break;
14671 case TCP_DELACK:
14672 optval = tp->t_delayed_ack;
14673 break;
14674 case TCP_BBR_HDWR_PACE:
14675 optval = bbr->bbr_hdw_pace_ena;
14676 break;
14677 case TCP_BBR_POLICER_DETECT:
14678 optval = bbr->r_use_policer;
14679 break;
14680 case TCP_BBR_TSTMP_RAISES:
14681 optval = bbr->ts_can_raise;
14682 break;
14683 case TCP_BBR_TMR_PACE_OH:
14684 optval = bbr->r_ctl.rc_incr_tmrs;
14685 break;
14686 case TCP_BBR_PACE_OH:
14687 optval = 0;
14688 if (bbr->r_ctl.rc_inc_tcp_oh)
14689 optval |= BBR_INCL_TCP_OH;
14690 if (bbr->r_ctl.rc_inc_ip_oh)
14691 optval |= BBR_INCL_IP_OH;
14692 if (bbr->r_ctl.rc_inc_enet_oh)
14693 optval |= BBR_INCL_ENET_OH;
14694 break;
14695 default:
14696 return (tcp_default_ctloutput(tp, sopt));
14697 break;
14698 }
14699 INP_WUNLOCK(inp);
14700 error = sooptcopyout(sopt, &optval, sizeof optval);
14701 return (error);
14702 }
14703
14704 /*
14705 * return 0 on success, error-num on failure
14706 */
14707 static int
bbr_ctloutput(struct tcpcb * tp,struct sockopt * sopt)14708 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
14709 {
14710 if (sopt->sopt_dir == SOPT_SET) {
14711 return (bbr_set_sockopt(tp, sopt));
14712 } else if (sopt->sopt_dir == SOPT_GET) {
14713 return (bbr_get_sockopt(tp, sopt));
14714 } else {
14715 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
14716 }
14717 }
14718
14719 static const char *bbr_stack_names[] = {
14720 __XSTRING(STACKNAME),
14721 #ifdef STACKALIAS
14722 __XSTRING(STACKALIAS),
14723 #endif
14724 };
14725
14726 static bool bbr_mod_inited = false;
14727
14728 static int
tcp_addbbr(module_t mod,int32_t type,void * data)14729 tcp_addbbr(module_t mod, int32_t type, void *data)
14730 {
14731 int32_t err = 0;
14732 int num_stacks;
14733
14734 switch (type) {
14735 case MOD_LOAD:
14736 printf("Attempting to load " __XSTRING(MODNAME) "\n");
14737 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
14738 sizeof(struct bbr_sendmap),
14739 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
14740 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
14741 sizeof(struct tcp_bbr),
14742 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
14743 sysctl_ctx_init(&bbr_sysctl_ctx);
14744 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
14745 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
14746 OID_AUTO,
14747 #ifdef STACKALIAS
14748 __XSTRING(STACKALIAS),
14749 #else
14750 __XSTRING(STACKNAME),
14751 #endif
14752 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
14753 "");
14754 if (bbr_sysctl_root == NULL) {
14755 printf("Failed to add sysctl node\n");
14756 err = EFAULT;
14757 goto free_uma;
14758 }
14759 bbr_init_sysctls();
14760 num_stacks = nitems(bbr_stack_names);
14761 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK,
14762 bbr_stack_names, &num_stacks);
14763 if (err) {
14764 printf("Failed to register %s stack name for "
14765 "%s module\n", bbr_stack_names[num_stacks],
14766 __XSTRING(MODNAME));
14767 sysctl_ctx_free(&bbr_sysctl_ctx);
14768 free_uma:
14769 uma_zdestroy(bbr_zone);
14770 uma_zdestroy(bbr_pcb_zone);
14771 bbr_counter_destroy();
14772 printf("Failed to register " __XSTRING(MODNAME)
14773 " module err:%d\n", err);
14774 return (err);
14775 }
14776 tcp_lro_reg_mbufq();
14777 bbr_mod_inited = true;
14778 printf(__XSTRING(MODNAME) " is now available\n");
14779 break;
14780 case MOD_QUIESCE:
14781 err = deregister_tcp_functions(&__tcp_bbr, true, false);
14782 break;
14783 case MOD_UNLOAD:
14784 err = deregister_tcp_functions(&__tcp_bbr, false, true);
14785 if (err == EBUSY)
14786 break;
14787 if (bbr_mod_inited) {
14788 uma_zdestroy(bbr_zone);
14789 uma_zdestroy(bbr_pcb_zone);
14790 sysctl_ctx_free(&bbr_sysctl_ctx);
14791 bbr_counter_destroy();
14792 printf(__XSTRING(MODNAME)
14793 " is now no longer available\n");
14794 bbr_mod_inited = false;
14795 }
14796 tcp_lro_dereg_mbufq();
14797 err = 0;
14798 break;
14799 default:
14800 return (EOPNOTSUPP);
14801 }
14802 return (err);
14803 }
14804
14805 static moduledata_t tcp_bbr = {
14806 .name = __XSTRING(MODNAME),
14807 .evhand = tcp_addbbr,
14808 .priv = 0
14809 };
14810
14811 MODULE_VERSION(MODNAME, 1);
14812 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
14813 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
14814