xref: /xnu-8020.101.4/bsd/netinet/tcp_cubic.c (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 /*
2  * Copyright (c) 2013-2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/protosw.h>
32 #include <sys/socketvar.h>
33 #include <sys/syslog.h>
34 
35 #include <net/route.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 
40 #include <netinet/ip6.h>
41 
42 #include <netinet/ip_var.h>
43 #include <netinet/tcp.h>
44 #include <netinet/tcp_timer.h>
45 #include <netinet/tcp_var.h>
46 #include <netinet/tcp_fsm.h>
47 #include <netinet/tcp_var.h>
48 #include <netinet/tcp_cc.h>
49 #include <netinet/tcpip.h>
50 #include <netinet/tcp_seq.h>
51 #include <kern/task.h>
52 #include <libkern/OSAtomic.h>
53 
54 static int tcp_cubic_init(struct tcpcb *tp);
55 static int tcp_cubic_cleanup(struct tcpcb *tp);
56 static void tcp_cubic_cwnd_init_or_reset(struct tcpcb *tp);
57 static void tcp_cubic_congestion_avd(struct tcpcb *tp, struct tcphdr *th);
58 static void tcp_cubic_ack_rcvd(struct tcpcb *tp, struct tcphdr *th);
59 static void tcp_cubic_pre_fr(struct tcpcb *tp);
60 static void tcp_cubic_post_fr(struct tcpcb *tp, struct tcphdr *th);
61 static void tcp_cubic_after_timeout(struct tcpcb *tp);
62 static int tcp_cubic_delay_ack(struct tcpcb *tp, struct tcphdr *th);
63 static void tcp_cubic_switch_cc(struct tcpcb *tp);
64 static uint32_t tcp_cubic_update(struct tcpcb *tp, uint32_t rtt);
65 static inline void tcp_cubic_clear_state(struct tcpcb *tp);
66 
67 
68 extern float cbrtf(float x);
69 
70 struct tcp_cc_algo tcp_cc_cubic = {
71 	.name = "cubic",
72 	.init = tcp_cubic_init,
73 	.cleanup = tcp_cubic_cleanup,
74 	.cwnd_init = tcp_cubic_cwnd_init_or_reset,
75 	.congestion_avd = tcp_cubic_congestion_avd,
76 	.ack_rcvd = tcp_cubic_ack_rcvd,
77 	.pre_fr = tcp_cubic_pre_fr,
78 	.post_fr = tcp_cubic_post_fr,
79 	.after_idle = tcp_cubic_cwnd_init_or_reset,
80 	.after_timeout = tcp_cubic_after_timeout,
81 	.delay_ack = tcp_cubic_delay_ack,
82 	.switch_to = tcp_cubic_switch_cc
83 };
84 
85 static float tcp_cubic_backoff = 0.2f; /* multiplicative decrease factor */
86 static float tcp_cubic_coeff = 0.4f;
87 static float tcp_cubic_fast_convergence_factor = 0.875f;
88 
89 static float tcp_cubic_beta = 0.8f;
90 
91 SYSCTL_SKMEM_TCP_INT(OID_AUTO, cubic_tcp_friendliness, CTLFLAG_RW | CTLFLAG_LOCKED,
92     static int, tcp_cubic_tcp_friendliness, 0, "Enable TCP friendliness");
93 
94 SYSCTL_SKMEM_TCP_INT(OID_AUTO, cubic_fast_convergence, CTLFLAG_RW | CTLFLAG_LOCKED,
95     static int, tcp_cubic_fast_convergence, 0, "Enable fast convergence");
96 
97 SYSCTL_SKMEM_TCP_INT(OID_AUTO, cubic_use_minrtt, CTLFLAG_RW | CTLFLAG_LOCKED,
98     static int, tcp_cubic_use_minrtt, 0, "use a min of 5 sec rtt");
99 
100 SYSCTL_SKMEM_TCP_INT(OID_AUTO, cubic_minor_fixes, CTLFLAG_RW | CTLFLAG_LOCKED,
101     int, tcp_cubic_minor_fixes, 1, "Minor fixes to TCP Cubic");
102 
103 SYSCTL_SKMEM_TCP_INT(OID_AUTO, cubic_rfc_compliant, CTLFLAG_RW | CTLFLAG_LOCKED,
104     int, tcp_cubic_rfc_compliant, 1, "RFC Compliance for TCP Cubic");
105 
106 static int
tcp_cubic_init(struct tcpcb * tp)107 tcp_cubic_init(struct tcpcb *tp)
108 {
109 	OSIncrementAtomic((volatile SInt32 *)&tcp_cc_cubic.num_sockets);
110 
111 	if (tcp_cubic_rfc_compliant) {
112 		tcp_cubic_backoff = 0.3f; /* multiplicative decrease factor */
113 		tcp_cubic_fast_convergence_factor = 0.85f;
114 		tcp_cubic_beta = 0.7f;
115 	} else {
116 		tcp_cubic_backoff = 0.2f; /* multiplicative decrease factor */
117 		tcp_cubic_fast_convergence_factor = 0.875f;
118 		tcp_cubic_beta = 0.8f;
119 	}
120 
121 	VERIFY(tp->t_ccstate != NULL);
122 	tcp_cubic_clear_state(tp);
123 	return 0;
124 }
125 
126 static int
tcp_cubic_cleanup(struct tcpcb * tp)127 tcp_cubic_cleanup(struct tcpcb *tp)
128 {
129 #pragma unused(tp)
130 	OSDecrementAtomic((volatile SInt32 *)&tcp_cc_cubic.num_sockets);
131 	return 0;
132 }
133 
134 /*
135  * Initialize the congestion window at the beginning of a connection or
136  * after idle time
137  */
138 static void
tcp_cubic_cwnd_init_or_reset(struct tcpcb * tp)139 tcp_cubic_cwnd_init_or_reset(struct tcpcb *tp)
140 {
141 	VERIFY(tp->t_ccstate != NULL);
142 
143 	tcp_cubic_clear_state(tp);
144 	tcp_cc_cwnd_init_or_reset(tp);
145 	tp->t_pipeack = 0;
146 	tcp_clear_pipeack_state(tp);
147 
148 	/* Start counting bytes for RFC 3465 again */
149 	tp->t_bytes_acked = 0;
150 
151 	/*
152 	 * slow start threshold could get initialized to a lower value
153 	 * when there is a cached value in the route metrics. In this case,
154 	 * the connection can enter congestion avoidance without any packet
155 	 * loss and Cubic will enter steady-state too early. It is better
156 	 * to always probe to find the initial slow-start threshold.
157 	 */
158 	if (tp->t_inpcb->inp_stat->txbytes <= tcp_initial_cwnd(tp) &&
159 	    tp->snd_ssthresh < (TCP_MAXWIN << TCP_MAX_WINSHIFT)) {
160 		tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
161 	}
162 
163 	/* Initialize cubic last max to be same as ssthresh */
164 	tp->t_ccstate->cub_last_max = tp->snd_ssthresh;
165 }
166 
167 /*
168  * Compute the target congestion window for the next RTT according to
169  * cubic equation when an ack is received.
170  *
171  * W(t) = C(t-K)^3 + W(last_max)
172  */
173 static uint32_t
tcp_cubic_update(struct tcpcb * tp,u_int32_t rtt)174 tcp_cubic_update(struct tcpcb *tp, u_int32_t rtt)
175 {
176 	float K, var;
177 	u_int32_t elapsed_time, win;
178 
179 	win = min(tp->snd_cwnd, tp->snd_wnd);
180 	if (tp->t_ccstate->cub_last_max == 0) {
181 		tp->t_ccstate->cub_last_max = tp->snd_ssthresh;
182 	}
183 
184 	if (tp->t_ccstate->cub_epoch_start == 0) {
185 		/*
186 		 * This is the beginning of a new epoch, initialize some of
187 		 * the variables that we need to use for computing the
188 		 * congestion window later.
189 		 */
190 		tp->t_ccstate->cub_epoch_start = tcp_now;
191 		if (tp->t_ccstate->cub_epoch_start == 0) {
192 			tp->t_ccstate->cub_epoch_start = 1;
193 		}
194 		if (win < tp->t_ccstate->cub_last_max) {
195 			/*
196 			 * Compute cubic epoch period, this is the time
197 			 * period that the window will take to increase to
198 			 * last_max again after backoff due to loss.
199 			 */
200 			if (tcp_cubic_minor_fixes) {
201 				K = ((float)tp->t_ccstate->cub_last_max - win) / tp->t_maxseg / tcp_cubic_coeff;
202 			} else {
203 				K = (tp->t_ccstate->cub_last_max - win) / tp->t_maxseg / tcp_cubic_coeff;
204 			}
205 			K = cbrtf(K);
206 			tp->t_ccstate->cub_epoch_period = K * TCP_RETRANSHZ;
207 			/* Origin point */
208 			tp->t_ccstate->cub_origin_point = tp->t_ccstate->cub_last_max;
209 		} else {
210 			tp->t_ccstate->cub_epoch_period = 0;
211 			tp->t_ccstate->cub_origin_point = win;
212 		}
213 	}
214 
215 	VERIFY(tp->t_ccstate->cub_origin_point > 0);
216 	/*
217 	 * Compute the target window for the next RTT using smoothed RTT
218 	 * as an estimate for next RTT.
219 	 */
220 	elapsed_time = timer_diff(tcp_now, 0, tp->t_ccstate->cub_epoch_start, 0);
221 
222 	if (tcp_cubic_use_minrtt) {
223 		elapsed_time += max(tcp_cubic_use_minrtt, rtt);
224 	} else {
225 		elapsed_time += rtt;
226 	}
227 	var = (elapsed_time  - tp->t_ccstate->cub_epoch_period) / TCP_RETRANSHZ;
228 	var = var * var * var * (tcp_cubic_coeff * tp->t_maxseg);
229 
230 	return (u_int32_t)(tp->t_ccstate->cub_origin_point + var);
231 }
232 
233 /*
234  * Standard TCP utilizes bandwidth well in low RTT and low BDP connections
235  * even when there is some packet loss. Enabling TCP mode will help Cubic
236  * to achieve this kind of utilization.
237  *
238  * But if there is a bottleneck link in the path with a fixed size queue
239  * and fixed bandwidth, TCP Cubic will help to reduce packet loss at this
240  * link because of the steady-state behavior. Using average and mean
241  * absolute deviation of W(lastmax), we try to detect if the congestion
242  * window is close to the bottleneck bandwidth. In that case, disabling
243  * TCP mode will help to minimize packet loss at this link.
244  *
245  * Disable TCP mode if the W(lastmax) (the window where previous packet
246  * loss happened) is within a small range from the average last max
247  * calculated.
248  */
249 #define TCP_CUBIC_ENABLE_TCPMODE(_tp_) \
250 	((!soissrcrealtime((_tp_)->t_inpcb->inp_socket) && \
251 	(_tp_)->t_ccstate->cub_mean_dev > (tp->t_maxseg << 1)) ? 1 : 0)
252 
253 /*
254  * Compute the window growth if standard TCP (AIMD) was used with
255  * a backoff of 0.5 and additive increase of 1 packet per RTT.
256  *
257  * TCP window at time t can be calculated using the following equation
258  * with tcp_beta_cubic
259  *
260  * W(t) <- Wmax * tcp_beta_cubic + 3 * ((1 - tcp_beta_cubic)/(1 + tcp_beta_cubic)) * t/RTT
261  *
262  */
263 static uint32_t
tcp_cubic_tcpwin(struct tcpcb * tp,struct tcphdr * th)264 tcp_cubic_tcpwin(struct tcpcb *tp, struct tcphdr *th)
265 {
266 	if (tp->t_ccstate->cub_tcp_win == 0) {
267 		/* Start of the epoch, we set the tcp_win to whatever Cubic decided
268 		 * at the beginning of the epoch.
269 		 */
270 		tp->t_ccstate->cub_tcp_win = min(tp->snd_cwnd, tp->snd_wnd);
271 		if (tcp_cubic_minor_fixes) {
272 			tp->t_ccstate->cub_tcp_bytes_acked = BYTES_ACKED(th, tp);
273 		} else {
274 			tp->t_ccstate->cub_tcp_bytes_acked = 0;
275 		}
276 	} else {
277 		tp->t_ccstate->cub_tcp_bytes_acked += BYTES_ACKED(th, tp);
278 
279 		if (tcp_cubic_minor_fixes) {
280 			/*
281 			 * Increase by ai_factor * MSS, once per RTT. Counting bytes_acked
282 			 * against the snd_cwnd represents exactly one RTT at full rate.
283 			 */
284 			while (tp->t_ccstate->cub_tcp_bytes_acked >= tp->snd_cwnd) {
285 				/* Enough bytes have been ACK'd for TCP to do AIMD*/
286 				tp->t_ccstate->cub_tcp_bytes_acked -= tp->snd_cwnd;
287 
288 				if (tp->snd_cwnd >= tp->t_ccstate->cub_last_max || !tcp_cubic_rfc_compliant) {
289 					tp->t_ccstate->cub_tcp_win += tp->t_maxseg;
290 				} else {
291 					/* Increase-rate from Section 4.2, RFC 8312 */
292 					float ai_factor = (float)3 * (1 - tcp_cubic_beta) / (1 + tcp_cubic_beta);
293 
294 					tp->t_ccstate->cub_tcp_win += (uint32_t)(tp->t_maxseg * ai_factor);
295 				}
296 			}
297 		} else {
298 			if (tp->t_ccstate->cub_tcp_bytes_acked >= tp->t_ccstate->cub_tcp_win) {
299 				tp->t_ccstate->cub_tcp_bytes_acked -= tp->t_ccstate->cub_tcp_win;
300 				tp->t_ccstate->cub_tcp_win += tp->t_maxseg;
301 			}
302 		}
303 	}
304 	return tp->t_ccstate->cub_tcp_win;
305 }
306 
307 /*
308  * Handle an in-sequence ack during congestion avoidance phase.
309  */
310 static void
tcp_cubic_congestion_avd(struct tcpcb * tp,struct tcphdr * th)311 tcp_cubic_congestion_avd(struct tcpcb *tp, struct tcphdr *th)
312 {
313 	u_int32_t cubic_target_win, tcp_win, rtt;
314 	u_int64_t incr_win = UINT32_MAX;
315 
316 	/* Do not increase congestion window in non-validated phase */
317 	if (tcp_cc_is_cwnd_nonvalidated(tp) != 0) {
318 		return;
319 	}
320 
321 	tp->t_bytes_acked += BYTES_ACKED(th, tp);
322 
323 	rtt = get_base_rtt(tp);
324 	/*
325 	 * First compute cubic window. If cubic variables are not
326 	 * initialized (after coming out of recovery), this call will
327 	 * initialize them.
328 	 */
329 	cubic_target_win = tcp_cubic_update(tp, rtt);
330 
331 	/* Compute TCP window if a multiplicative decrease of 0.2 is used */
332 	tcp_win = tcp_cubic_tcpwin(tp, th);
333 
334 	if (tp->snd_cwnd < tcp_win && tcp_cubic_minor_fixes == 0 && TCP_CUBIC_ENABLE_TCPMODE(tp)) {
335 		/* this connection is in TCP-friendly region */
336 		if (tp->t_bytes_acked >= tp->snd_cwnd) {
337 			tp->t_bytes_acked -= tp->snd_cwnd;
338 			tp->snd_cwnd = min(tcp_win, TCP_MAXWIN << tp->snd_scale);
339 		}
340 	} else {
341 		if (cubic_target_win > tp->snd_cwnd) {
342 			/*
343 			 * The target win is computed for the next RTT.
344 			 * To reach this value, cwnd will have to be updated
345 			 * one segment at a time. Compute how many bytes
346 			 * need to be acknowledged before we can increase
347 			 * the cwnd by one segment.
348 			 */
349 			incr_win = (uint64_t)tp->snd_cwnd * tp->t_maxseg;
350 			incr_win /= (cubic_target_win - tp->snd_cwnd);
351 			if (!tcp_cubic_minor_fixes) {
352 				if (incr_win > 0 &&
353 				    tp->t_bytes_acked >= incr_win) {
354 					tp->t_bytes_acked -= incr_win;
355 					tp->snd_cwnd =
356 					    min((tp->snd_cwnd + tp->t_maxseg),
357 					    TCP_MAXWIN << tp->snd_scale);
358 				}
359 			}
360 		}
361 	}
362 
363 	if (tcp_cubic_minor_fixes) {
364 		tcp_win = tcp_round_to(tcp_win, tp->t_maxseg);
365 
366 		if (tp->snd_cwnd < tcp_win) {
367 			uint64_t tcp_incr_win;
368 
369 			tcp_incr_win = (uint64_t)tp->snd_cwnd * tp->t_maxseg;
370 			tcp_incr_win /= (tcp_win - tp->snd_cwnd);
371 
372 			if (tcp_incr_win < incr_win) {
373 				/* this connection is in TCP-friendly region */
374 				incr_win = tcp_incr_win;
375 			}
376 		}
377 
378 		if (incr_win > 0 && tp->t_bytes_acked >= incr_win) {
379 			tp->t_bytes_acked -= incr_win;
380 			tp->snd_cwnd = min(tp->snd_cwnd + tp->t_maxseg, TCP_MAXWIN << tp->snd_scale);
381 		}
382 	}
383 }
384 
385 static void
tcp_cubic_ack_rcvd(struct tcpcb * tp,struct tcphdr * th)386 tcp_cubic_ack_rcvd(struct tcpcb *tp, struct tcphdr *th)
387 {
388 	/* Do not increase the congestion window in non-validated phase */
389 	if (tcp_cc_is_cwnd_nonvalidated(tp) != 0) {
390 		return;
391 	}
392 
393 	if (tp->snd_cwnd >= tp->snd_ssthresh) {
394 		/* Congestion avoidance phase */
395 		tcp_cubic_congestion_avd(tp, th);
396 	} else {
397 		/*
398 		 * Use 2*SMSS as limit on increment as suggested
399 		 * by RFC 3465 section 2.3
400 		 */
401 		uint32_t acked, abc_lim, incr;
402 
403 		acked = BYTES_ACKED(th, tp);
404 		if (tcp_cubic_minor_fixes) {
405 			/*
406 			 * Maximum burst-size is limited to the initial congestion-window.
407 			 * We know that the network can survive this kind of burst.
408 			 */
409 			abc_lim = tcp_initial_cwnd(tp);
410 		} else {
411 			abc_lim = (tp->snd_nxt == tp->snd_max) ? 2 * tp->t_maxseg : tp->t_maxseg;
412 		}
413 		incr = min(acked, abc_lim);
414 
415 		tp->snd_cwnd += incr;
416 		tp->snd_cwnd = min(tp->snd_cwnd, TCP_MAXWIN << tp->snd_scale);
417 	}
418 }
419 
420 static void
tcp_cubic_pre_fr(struct tcpcb * tp)421 tcp_cubic_pre_fr(struct tcpcb *tp)
422 {
423 	u_int32_t win, avg;
424 	int32_t dev;
425 	tp->t_ccstate->cub_epoch_start = 0;
426 	tp->t_ccstate->cub_tcp_win = 0;
427 	tp->t_ccstate->cub_tcp_bytes_acked = 0;
428 
429 	win = min(tp->snd_cwnd, tp->snd_wnd);
430 	if (tp->t_flagsext & TF_CWND_NONVALIDATED) {
431 		tp->t_lossflightsize = tp->snd_max - tp->snd_una;
432 		if (tcp_flow_control_response) {
433 			win = max(tp->t_pipeack, tp->t_lossflightsize);
434 		} else {
435 			win = (max(tp->t_pipeack, tp->t_lossflightsize)) >> 1;
436 		}
437 	} else {
438 		tp->t_lossflightsize = 0;
439 	}
440 	/*
441 	 * Note the congestion window at which packet loss occurred as
442 	 * cub_last_max.
443 	 *
444 	 * If the congestion window is less than the last max window when
445 	 * loss occurred, it indicates that capacity available in the
446 	 * network has gone down. This can happen if a new flow has started
447 	 * and it is capturing some of the bandwidth. To reach convergence
448 	 * quickly, backoff a little more.
449 	 */
450 	if (win < tp->t_ccstate->cub_last_max && tcp_cubic_minor_fixes) {
451 		tp->t_ccstate->cub_last_max = (uint32_t)((float)win * tcp_cubic_fast_convergence_factor);
452 	} else {
453 		tp->t_ccstate->cub_last_max = win;
454 	}
455 
456 	if (tp->t_ccstate->cub_last_max == 0) {
457 		/*
458 		 * If last_max is zero because snd_wnd is zero or for
459 		 * any other reason, initialize it to the amount of data
460 		 * in flight
461 		 */
462 		tp->t_ccstate->cub_last_max = tp->snd_max - tp->snd_una;
463 	}
464 
465 	/*
466 	 * Compute average and mean absolute deviation of the
467 	 * window at which packet loss occurred.
468 	 */
469 	if (tp->t_ccstate->cub_avg_lastmax == 0) {
470 		tp->t_ccstate->cub_avg_lastmax = tp->t_ccstate->cub_last_max;
471 	} else {
472 		/*
473 		 * Average is computed by taking 63 parts of
474 		 * history and one part of the most recent value
475 		 */
476 		avg = tp->t_ccstate->cub_avg_lastmax;
477 		avg = (avg << 6) - avg;
478 		tp->t_ccstate->cub_avg_lastmax =
479 		    (avg + tp->t_ccstate->cub_last_max) >> 6;
480 	}
481 
482 	/* caluclate deviation from average */
483 	dev = tp->t_ccstate->cub_avg_lastmax - tp->t_ccstate->cub_last_max;
484 
485 	/* Take the absolute value */
486 	if (dev < 0) {
487 		dev = -dev;
488 	}
489 
490 	if (tp->t_ccstate->cub_mean_dev == 0) {
491 		tp->t_ccstate->cub_mean_dev = dev;
492 	} else {
493 		dev = dev + ((tp->t_ccstate->cub_mean_dev << 4)
494 		    - tp->t_ccstate->cub_mean_dev);
495 		tp->t_ccstate->cub_mean_dev = dev >> 4;
496 	}
497 
498 	/* Backoff congestion window by tcp_cubic_backoff factor */
499 	win = (u_int32_t)(win - (win * tcp_cubic_backoff));
500 	win = tcp_round_to(win, tp->t_maxseg);
501 	if (win < 2 * tp->t_maxseg) {
502 		win =  2 * tp->t_maxseg;
503 	}
504 	tp->snd_ssthresh = win;
505 	tcp_cc_resize_sndbuf(tp);
506 }
507 
508 static void
tcp_cubic_post_fr(struct tcpcb * tp,struct tcphdr * th)509 tcp_cubic_post_fr(struct tcpcb *tp, struct tcphdr *th)
510 {
511 	uint32_t flight_size = 0;
512 	uint32_t ack;
513 
514 	if (th != NULL) {
515 		ack = th->th_ack;
516 	} else {
517 		ack = tp->snd_una;
518 	}
519 
520 	if (SEQ_LEQ(ack, tp->snd_max) && (!tcp_cubic_minor_fixes || tcp_flow_control_response)) {
521 		flight_size = tp->snd_max - ack;
522 	} else if (tcp_cubic_minor_fixes) {
523 		/*
524 		 * Cubic Minor Fixes: snd_max - th_ack is a very very bad estimate
525 		 * of the flight size. Either the app is sending at full speed and
526 		 * flight_size *is* snd_sshtresh, or the app is not sending at full
527 		 * speed and congestion-window validation would have kicked in earlier.
528 		 *
529 		 * Except that for the latter, snd_ssthresh is way too high.
530 		 * When we exit recovery we will burst a lot of data out...
531 		 *
532 		 * So, tcp_flow_control_response brings us back to the old behavior.
533 		 * Too many feature-flags...
534 		 */
535 		flight_size = tp->snd_ssthresh;
536 	}
537 
538 	/*
539 	 * Cubic Minor Fixes: t_lossflightsize is always 0, because of
540 	 * EXIT_FASTRECOVERY. This here is basically dead code...
541 	 */
542 	if (SACK_ENABLED(tp) && tp->t_lossflightsize > 0 && !tcp_cubic_minor_fixes) {
543 		u_int32_t total_rxt_size = 0, ncwnd;
544 		/*
545 		 * When SACK is enabled, the number of retransmitted bytes
546 		 * can be counted more accurately.
547 		 */
548 		total_rxt_size = tcp_rxtseg_total_size(tp);
549 		ncwnd = max(tp->t_pipeack, tp->t_lossflightsize);
550 		if (total_rxt_size <= ncwnd) {
551 			ncwnd = ncwnd - total_rxt_size;
552 		}
553 
554 		/*
555 		 * To avoid sending a large burst at the end of recovery
556 		 * set a max limit on ncwnd
557 		 */
558 		ncwnd = min(ncwnd, (tp->t_maxseg << 6));
559 		ncwnd = ncwnd >> 1;
560 		flight_size = max(ncwnd, flight_size);
561 	}
562 	/*
563 	 * Complete ack. The current window was inflated for fast recovery.
564 	 * It has to be deflated post recovery.
565 	 *
566 	 * Window inflation should have left us with approx snd_ssthresh
567 	 * outstanding data. If the flight size is zero or one segment,
568 	 * make congestion window to be at least as big as 2 segments to
569 	 * avoid delayed acknowledgements. This is according to RFC 6582.
570 	 */
571 	if (flight_size < tp->snd_ssthresh) {
572 		tp->snd_cwnd = max(flight_size, tp->t_maxseg)
573 		    + tp->t_maxseg;
574 	} else {
575 		tp->snd_cwnd = tp->snd_ssthresh;
576 	}
577 	tp->t_ccstate->cub_tcp_win = 0;
578 	tp->t_ccstate->cub_tcp_bytes_acked = 0;
579 }
580 
581 static void
tcp_cubic_after_timeout(struct tcpcb * tp)582 tcp_cubic_after_timeout(struct tcpcb *tp)
583 {
584 	VERIFY(tp->t_ccstate != NULL);
585 
586 	/*
587 	 * Avoid adjusting congestion window due to SYN retransmissions.
588 	 * If more than one byte (SYN) is outstanding then it is still
589 	 * needed to adjust the window.
590 	 */
591 	if (tp->t_state < TCPS_ESTABLISHED &&
592 	    ((int)(tp->snd_max - tp->snd_una) <= 1)) {
593 		return;
594 	}
595 
596 	if (!IN_FASTRECOVERY(tp)) {
597 		tcp_cubic_clear_state(tp);
598 		tcp_cubic_pre_fr(tp);
599 	}
600 
601 	/*
602 	 * Close the congestion window down to one segment as a retransmit
603 	 * timeout might indicate severe congestion.
604 	 */
605 	tp->snd_cwnd = tp->t_maxseg;
606 }
607 
608 static int
tcp_cubic_delay_ack(struct tcpcb * tp,struct tcphdr * th)609 tcp_cubic_delay_ack(struct tcpcb *tp, struct tcphdr *th)
610 {
611 	return tcp_cc_delay_ack(tp, th);
612 }
613 
614 /*
615  * When switching from a different CC it is better for Cubic to start
616  * fresh. The state required for Cubic calculation might be stale and it
617  * might not represent the current state of the network. If it starts as
618  * a new connection it will probe and learn the existing network conditions.
619  */
620 static void
tcp_cubic_switch_cc(struct tcpcb * tp)621 tcp_cubic_switch_cc(struct tcpcb *tp)
622 {
623 	tcp_cubic_cwnd_init_or_reset(tp);
624 
625 	OSIncrementAtomic((volatile SInt32 *)&tcp_cc_cubic.num_sockets);
626 }
627 
628 static inline void
tcp_cubic_clear_state(struct tcpcb * tp)629 tcp_cubic_clear_state(struct tcpcb *tp)
630 {
631 	tp->t_ccstate->cub_last_max = 0;
632 	tp->t_ccstate->cub_epoch_start = 0;
633 	tp->t_ccstate->cub_origin_point = 0;
634 	tp->t_ccstate->cub_tcp_win = 0;
635 	tp->t_ccstate->cub_tcp_bytes_acked = 0;
636 	tp->t_ccstate->cub_epoch_period = 0;
637 }
638