xref: /xnu-12377.1.9/bsd/netinet/tcp_ledbat.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2010-2021 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 
29 #include "tcp_includes.h"
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 
35 #include <net/route.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip6.h>
40 #include <netinet/ip_var.h>
41 
42 /* This file implements an alternate TCP congestion control algorithm
43  * for background transport developed by LEDBAT working group at IETF and
44  * described in draft: draft-ietf-ledbat-congestion-02
45  *
46  * Currently, it also implements LEDBAT++ as described in draft
47  * draft-irtf-iccrg-ledbat-plus-plus-01.
48  */
49 
50 #define GAIN_CONSTANT               (16)
51 #define DEFER_SLOWDOWN_DURATION     (30 * 1000) /* 30s */
52 
53 int tcp_ledbat_init(struct tcpcb *tp);
54 int tcp_ledbat_cleanup(struct tcpcb *tp);
55 void tcp_ledbat_cwnd_init(struct tcpcb *tp);
56 void tcp_ledbat_congestion_avd(struct tcpcb *tp, struct tcphdr *th);
57 void tcp_ledbat_ack_rcvd(struct tcpcb *tp, struct tcphdr *th);
58 static void ledbat_pp_ack_rcvd(struct tcpcb *tp, uint32_t bytes_acked);
59 void tcp_ledbat_pre_fr(struct tcpcb *tp);
60 void tcp_ledbat_post_fr(struct tcpcb *tp, struct tcphdr *th);
61 void tcp_ledbat_after_idle(struct tcpcb *tp);
62 void tcp_ledbat_after_timeout(struct tcpcb *tp);
63 static int tcp_ledbat_delay_ack(struct tcpcb *tp, struct tcphdr *th);
64 void tcp_ledbat_switch_cc(struct tcpcb *tp);
65 
66 struct tcp_cc_algo tcp_cc_ledbat = {
67 	.name = "ledbat",
68 	.init = tcp_ledbat_init,
69 	.cleanup = tcp_ledbat_cleanup,
70 	.cwnd_init = tcp_ledbat_cwnd_init,
71 	.congestion_avd = tcp_ledbat_congestion_avd,
72 	.ack_rcvd = tcp_ledbat_ack_rcvd,
73 	.pre_fr = tcp_ledbat_pre_fr,
74 	.post_fr = tcp_ledbat_post_fr,
75 	.after_idle = tcp_ledbat_after_idle,
76 	.after_timeout = tcp_ledbat_after_timeout,
77 	.delay_ack = tcp_ledbat_delay_ack,
78 	.switch_to = tcp_ledbat_switch_cc
79 };
80 
81 static void
update_cwnd(struct tcpcb * tp,uint32_t update,bool is_incr)82 update_cwnd(struct tcpcb *tp, uint32_t update, bool is_incr)
83 {
84 	uint32_t max_allowed_cwnd = 0, flight_size = 0;
85 	uint32_t base_rtt = get_base_rtt(tp);
86 	uint32_t curr_rtt = tcp_use_min_curr_rtt ? tp->curr_rtt_min :
87 	    tp->t_rttcur;
88 
89 	/* If we do not have a good RTT measurement yet, increment
90 	 * congestion window by the default value.
91 	 */
92 	if (base_rtt == 0 || curr_rtt == 0) {
93 		tp->snd_cwnd += update;
94 		goto check_max;
95 	}
96 
97 	if (curr_rtt <= (base_rtt + target_qdelay)) {
98 		/*
99 		 * Delay decreased or remained the same, we can increase
100 		 * the congestion window according to RFC 3465.
101 		 *
102 		 * Move background slow-start threshold to current
103 		 * congestion window so that the next time (after some idle
104 		 * period), we can attempt to do slow-start till here if there
105 		 * is no increase in rtt
106 		 */
107 		if (tp->bg_ssthresh < tp->snd_cwnd) {
108 			tp->bg_ssthresh = tp->snd_cwnd;
109 		}
110 		tp->snd_cwnd += update;
111 		tp->snd_cwnd = tcp_round_to(tp->snd_cwnd, tp->t_maxseg);
112 	} else {
113 		if (tcp_ledbat_plus_plus) {
114 			VERIFY(is_incr == false);
115 			tp->snd_cwnd -= update;
116 		} else {
117 			/* In response to an increase in rtt, reduce the congestion
118 			 * window by one-eighth. This will help to yield immediately
119 			 * to a competing stream.
120 			 */
121 			uint32_t redwin;
122 
123 			redwin = tp->snd_cwnd >> 3;
124 			tp->snd_cwnd -= redwin;
125 		}
126 
127 		if (tp->snd_cwnd < bg_ss_fltsz * tp->t_maxseg) {
128 			tp->snd_cwnd = bg_ss_fltsz * tp->t_maxseg;
129 		}
130 
131 		tp->snd_cwnd = tcp_round_to(tp->snd_cwnd, tp->t_maxseg);
132 		/* Lower background slow-start threshold so that the connection
133 		 * will go into congestion avoidance phase
134 		 */
135 		if (tp->bg_ssthresh > tp->snd_cwnd) {
136 			tp->bg_ssthresh = tp->snd_cwnd;
137 		}
138 	}
139 check_max:
140 	if (!tcp_ledbat_plus_plus) {
141 		/* Calculate the outstanding flight size and restrict the
142 		 * congestion window to a factor of flight size.
143 		 */
144 		flight_size = tp->snd_max - tp->snd_una;
145 
146 		max_allowed_cwnd = (tcp_ledbat_allowed_increase * tp->t_maxseg)
147 		    + (flight_size << tcp_ledbat_tether_shift);
148 		tp->snd_cwnd = min(tp->snd_cwnd, max_allowed_cwnd);
149 	} else {
150 		tp->snd_cwnd = min(tp->snd_cwnd, TCP_MAXWIN << tp->snd_scale);
151 	}
152 }
153 
154 static inline void
tcp_ledbat_clear_state(struct tcpcb * tp)155 tcp_ledbat_clear_state(struct tcpcb *tp)
156 {
157 	tp->t_ccstate->ledbat_slowdown_events = 0;
158 	tp->t_ccstate->ledbat_slowdown_ts = 0;
159 	tp->t_ccstate->ledbat_slowdown_begin = 0;
160 	tp->t_ccstate->ledbat_md_bytes_acked = 0;
161 }
162 
163 int
tcp_ledbat_init(struct tcpcb * tp)164 tcp_ledbat_init(struct tcpcb *tp)
165 {
166 	os_atomic_inc(&tcp_cc_ledbat.num_sockets, relaxed);
167 	tcp_ledbat_clear_state(tp);
168 	return 0;
169 }
170 
171 int
tcp_ledbat_cleanup(struct tcpcb * tp)172 tcp_ledbat_cleanup(struct tcpcb *tp)
173 {
174 #pragma unused(tp)
175 	os_atomic_dec(&tcp_cc_ledbat.num_sockets, relaxed);
176 	return 0;
177 }
178 
179 /*
180  * Initialize the congestion window for a connection
181  */
182 void
tcp_ledbat_cwnd_init(struct tcpcb * tp)183 tcp_ledbat_cwnd_init(struct tcpcb *tp)
184 {
185 	tp->snd_cwnd = tp->t_maxseg * bg_ss_fltsz;
186 	tp->bg_ssthresh = tp->snd_ssthresh;
187 
188 	tcp_update_pacer_state(tp);
189 }
190 
191 /* Function to handle an in-sequence ack which is fast-path processing
192  * of an in sequence ack in tcp_input function (called as header prediction).
193  * This gets called only during congestion avoidance phase.
194  */
195 void
tcp_ledbat_congestion_avd(struct tcpcb * tp,struct tcphdr * th)196 tcp_ledbat_congestion_avd(struct tcpcb *tp, struct tcphdr *th)
197 {
198 	int acked = 0;
199 	uint32_t incr = 0;
200 
201 	acked = BYTES_ACKED(th, tp);
202 
203 	if (tcp_ledbat_plus_plus) {
204 		ledbat_pp_ack_rcvd(tp, acked);
205 	} else {
206 		tp->t_bytes_acked += acked;
207 		if (tp->t_bytes_acked > tp->snd_cwnd) {
208 			tp->t_bytes_acked -= tp->snd_cwnd;
209 			incr = tp->t_maxseg;
210 		}
211 
212 		if (tp->snd_cwnd < tp->snd_wnd && incr > 0) {
213 			update_cwnd(tp, incr, true);
214 		}
215 	}
216 }
217 
218 /*
219  * Compute the denominator
220  * MIN(16, ceil(2 * TARGET / base))
221  */
222 static uint32_t
ledbat_gain(uint32_t base_rtt)223 ledbat_gain(uint32_t base_rtt)
224 {
225 	return MIN(GAIN_CONSTANT, tcp_ceil(2 * target_qdelay /
226 	           (double)base_rtt));
227 }
228 
229 /*
230  * Congestion avoidance for ledbat++
231  */
232 static void
ledbat_pp_congestion_avd(struct tcpcb * tp,uint32_t bytes_acked,uint32_t base_rtt,uint32_t curr_rtt,uint32_t now)233 ledbat_pp_congestion_avd(struct tcpcb *tp, uint32_t bytes_acked,
234     uint32_t base_rtt, uint32_t curr_rtt, uint32_t now)
235 {
236 	uint32_t update = 0;
237 	/*
238 	 * Set the next slowdown time i.e. 9 times the duration
239 	 * of previous slowdown except the initial slowdown.
240 	 */
241 	if (tp->t_ccstate->ledbat_slowdown_ts == 0) {
242 		uint32_t slowdown_duration = 0;
243 		if (tp->t_ccstate->ledbat_slowdown_events > 0) {
244 			slowdown_duration = now -
245 			    tp->t_ccstate->ledbat_slowdown_begin;
246 
247 			if (tp->bg_ssthresh > tp->snd_cwnd) {
248 				/*
249 				 * Special case for slowdowns (other than initial)
250 				 * where cwnd doesn't recover fully to previous
251 				 * ssthresh
252 				 */
253 				slowdown_duration *= 2;
254 			}
255 		}
256 		tp->t_ccstate->ledbat_slowdown_ts = now + (9 * slowdown_duration);
257 		if (slowdown_duration == 0) {
258 			tp->t_ccstate->ledbat_slowdown_ts += (2 * (tp->t_srtt >> TCP_RTT_SHIFT));
259 		}
260 		/* Reset the start */
261 		tp->t_ccstate->ledbat_slowdown_begin = 0;
262 
263 		/* On exit slow start due to higher qdelay, cap the ssthresh */
264 		if (tp->bg_ssthresh > tp->snd_cwnd) {
265 			tp->bg_ssthresh = tp->snd_cwnd;
266 		}
267 	}
268 
269 	if (curr_rtt <= base_rtt + target_qdelay) {
270 		/* Additive increase */
271 		tp->t_bytes_acked += bytes_acked;
272 		if (tp->t_bytes_acked >= tp->snd_cwnd) {
273 			update = tp->t_maxseg;
274 			tp->t_bytes_acked -= tp->snd_cwnd;
275 			update_cwnd(tp, update, true);
276 		}
277 	} else {
278 		/*
279 		 * Multiplicative decrease
280 		 * W -= min(W * (qdelay/target - 1), W/2) (per RTT)
281 		 * To calculate per bytes acked, it becomes
282 		 * W -= min((qdelay/target - 1), 1/2) * bytes_acked
283 		 */
284 		uint32_t qdelay = curr_rtt > base_rtt ?
285 		    (curr_rtt - base_rtt) : 0;
286 
287 		tp->t_ccstate->ledbat_md_bytes_acked += bytes_acked;
288 		if (tp->t_ccstate->ledbat_md_bytes_acked >= tp->snd_cwnd) {
289 			update = (uint32_t)(MIN(((double)qdelay / target_qdelay - 1), 0.5) *
290 			    (double)tp->snd_cwnd);
291 			tp->t_ccstate->ledbat_md_bytes_acked -= tp->snd_cwnd;
292 			update_cwnd(tp, update, false);
293 
294 			if (tp->t_ccstate->ledbat_slowdown_ts != 0) {
295 				/* As the window has been reduced, defer the slowdown. */
296 				tp->t_ccstate->ledbat_slowdown_ts = now + DEFER_SLOWDOWN_DURATION;
297 			}
298 		}
299 	}
300 }
301 
302 /*
303  * Different handling for ack received for ledbat++
304  */
305 static void
ledbat_pp_ack_rcvd(struct tcpcb * tp,uint32_t bytes_acked)306 ledbat_pp_ack_rcvd(struct tcpcb *tp, uint32_t bytes_acked)
307 {
308 	uint32_t update = 0;
309 	const uint32_t base_rtt = get_base_rtt(tp);
310 	const uint32_t curr_rtt = tcp_use_min_curr_rtt ? tp->curr_rtt_min :
311 	    tp->t_rttcur;
312 	const uint32_t ss_target = (uint32_t)(3 * target_qdelay / 4);
313 	struct tcp_globals *globals = tcp_get_globals(tp);
314 
315 	/*
316 	 * Slowdown period - first slowdown
317 	 * is 2RTT after we exit initial slow start.
318 	 * Subsequent slowdowns are after 9 times the
319 	 * previous slow down durations.
320 	 */
321 	if (tp->t_ccstate->ledbat_slowdown_ts != 0 &&
322 	    tcp_globals_now(globals) >= tp->t_ccstate->ledbat_slowdown_ts) {
323 		if (tp->t_ccstate->ledbat_slowdown_begin == 0) {
324 			tp->t_ccstate->ledbat_slowdown_begin = tcp_globals_now(globals);
325 			tp->t_ccstate->ledbat_slowdown_events++;
326 		}
327 		if (tcp_globals_now(globals) < tp->t_ccstate->ledbat_slowdown_ts +
328 		    (2 * (tp->t_srtt >> TCP_RTT_SHIFT))) {
329 			// Set cwnd to 2 packets and return
330 			if (tp->snd_cwnd > bg_ss_fltsz * tp->t_maxseg) {
331 				if (tp->bg_ssthresh < tp->snd_cwnd) {
332 					tp->bg_ssthresh = tp->snd_cwnd;
333 				}
334 				tp->snd_cwnd = bg_ss_fltsz * tp->t_maxseg;
335 				/* Reset total bytes acked */
336 				tp->t_bytes_acked = 0;
337 			}
338 			return;
339 		}
340 	}
341 
342 	if (curr_rtt == 0 || base_rtt == 0) {
343 		update = MIN(bytes_acked, TCP_CC_CWND_INIT_PKTS *
344 		    tp->t_maxseg);
345 		update_cwnd(tp, update, true);
346 	} else if (tp->snd_cwnd < tp->bg_ssthresh &&
347 	    ((tp->t_ccstate->ledbat_slowdown_events > 0 &&
348 	    curr_rtt <= (base_rtt + target_qdelay)) ||
349 	    curr_rtt <= (base_rtt + ss_target))) {
350 		/*
351 		 * Modified slow start with a dynamic GAIN
352 		 * If the queuing delay is larger than 3/4 of the target
353 		 * delay, exit slow start, iff, it is the initial slow start.
354 		 * After the initial slow start, during CA, window growth
355 		 * will be bound by ssthresh.
356 		 */
357 		tp->t_bytes_acked += bytes_acked;
358 		uint32_t gain_factor = ledbat_gain(base_rtt);
359 		if (tp->t_bytes_acked >= tp->t_maxseg * gain_factor) {
360 			update = MIN(tp->t_bytes_acked / gain_factor,
361 			    TCP_CC_CWND_INIT_PKTS * tp->t_maxseg);
362 			tp->t_bytes_acked = 0;
363 			update_cwnd(tp, update, true);
364 		}
365 
366 		/* Reset the next slowdown timestamp */
367 		if (tp->t_ccstate->ledbat_slowdown_ts != 0) {
368 			tp->t_ccstate->ledbat_slowdown_ts = 0;
369 		}
370 	} else {
371 		/* Congestion avoidance */
372 		ledbat_pp_congestion_avd(tp, bytes_acked, base_rtt, curr_rtt, tcp_globals_now(globals));
373 	}
374 
375 	tcp_update_pacer_state(tp);
376 }
377 
378 /* Function to process an ack.
379  */
380 void
tcp_ledbat_ack_rcvd(struct tcpcb * tp,struct tcphdr * th)381 tcp_ledbat_ack_rcvd(struct tcpcb *tp, struct tcphdr *th)
382 {
383 	/*
384 	 * RFC 3465 - Appropriate Byte Counting.
385 	 *
386 	 * If the window is currently less than ssthresh,
387 	 * open the window by the number of bytes ACKed by
388 	 * the last ACK, however clamp the window increase
389 	 * to an upper limit "L".
390 	 *
391 	 * In congestion avoidance phase, open the window by
392 	 * one segment each time "bytes_acked" grows to be
393 	 * greater than or equal to the congestion window.
394 	 */
395 
396 	uint32_t cw = tp->snd_cwnd;
397 	uint32_t incr = tp->t_maxseg;
398 	uint32_t acked = 0;
399 
400 	acked = BYTES_ACKED(th, tp);
401 
402 	if (tcp_ledbat_plus_plus) {
403 		ledbat_pp_ack_rcvd(tp, acked);
404 		return;
405 	}
406 
407 	tp->t_bytes_acked += acked;
408 
409 	if (cw >= tp->bg_ssthresh) {
410 		/* congestion-avoidance */
411 		if (tp->t_bytes_acked < cw) {
412 			/* No need to increase yet. */
413 			incr = 0;
414 		}
415 	} else {
416 		/*
417 		 * If the user explicitly enables RFC3465
418 		 * use 2*SMSS for the "L" param.  Otherwise
419 		 * use the more conservative 1*SMSS.
420 		 *
421 		 * (See RFC 3465 2.3 Choosing the Limit)
422 		 */
423 		u_int abc_lim;
424 
425 		abc_lim = (tp->snd_nxt == tp->snd_max) ? incr * 2 : incr;
426 
427 		incr = ulmin(acked, abc_lim);
428 	}
429 	if (tp->t_bytes_acked >= cw) {
430 		tp->t_bytes_acked -= cw;
431 	}
432 	if (incr > 0) {
433 		update_cwnd(tp, incr, true);
434 	}
435 
436 	tcp_update_pacer_state(tp);
437 }
438 
439 void
tcp_ledbat_pre_fr(struct tcpcb * tp)440 tcp_ledbat_pre_fr(struct tcpcb *tp)
441 {
442 	uint32_t win = min(tp->snd_wnd, tp->snd_cwnd);
443 
444 	if (tp->t_flagsext & TF_CWND_NONVALIDATED) {
445 		tp->t_lossflightsize = tp->snd_max - tp->snd_una;
446 		win = max(tp->t_pipeack, tp->t_lossflightsize);
447 	} else {
448 		tp->t_lossflightsize = 0;
449 	}
450 
451 	win = win / 2;
452 	win = tcp_round_to(win, tp->t_maxseg);
453 	if (win < 2 * tp->t_maxseg) {
454 		win = 2 * tp->t_maxseg;
455 	}
456 	tp->snd_ssthresh = win;
457 	if (tp->bg_ssthresh > tp->snd_ssthresh) {
458 		tp->bg_ssthresh = tp->snd_ssthresh;
459 	}
460 
461 	tcp_cc_resize_sndbuf(tp);
462 }
463 
464 void
tcp_ledbat_post_fr(struct tcpcb * tp,struct tcphdr * th)465 tcp_ledbat_post_fr(struct tcpcb *tp, struct tcphdr *th)
466 {
467 	int32_t ss;
468 
469 	if (th) {
470 		ss = tp->snd_max - th->th_ack;
471 	} else {
472 		ss = tp->snd_max - tp->snd_una;
473 	}
474 
475 	/*
476 	 * Complete ack.  Inflate the congestion window to
477 	 * ssthresh and exit fast recovery.
478 	 *
479 	 * Window inflation should have left us with approx.
480 	 * snd_ssthresh outstanding data.  But in case we
481 	 * would be inclined to send a burst, better to do
482 	 * it via the slow start mechanism.
483 	 *
484 	 * If the flight size is zero, then make congestion
485 	 * window to be worth at least 2 segments to avoid
486 	 * delayed acknowledgement (draft-ietf-tcpm-rfc3782-bis-05).
487 	 */
488 	if (ss < (int32_t)tp->snd_ssthresh) {
489 		tp->snd_cwnd = max(ss, tp->t_maxseg) + tp->t_maxseg;
490 	} else {
491 		tp->snd_cwnd = tp->snd_ssthresh;
492 	}
493 	tp->t_bytes_acked = 0;
494 	tp->t_ccstate->ledbat_md_bytes_acked = 0;
495 
496 	tcp_update_pacer_state(tp);
497 }
498 
499 /*
500  * Function to handle connections that have been idle for
501  * some time. Slow start to get ack "clock" running again.
502  * Clear base history after idle time.
503  */
504 void
tcp_ledbat_after_idle(struct tcpcb * tp)505 tcp_ledbat_after_idle(struct tcpcb *tp)
506 {
507 	tcp_ledbat_clear_state(tp);
508 	/* Reset the congestion window */
509 	tp->snd_cwnd = tp->t_maxseg * bg_ss_fltsz;
510 	tp->t_bytes_acked = 0;
511 	tp->t_ccstate->ledbat_md_bytes_acked = 0;
512 }
513 
514 /* Function to change the congestion window when the retransmit
515  * timer fires. The behavior is the same as that for best-effort
516  * TCP, reduce congestion window to one segment and start probing
517  * the link using "slow start". The slow start threshold is set
518  * to half of the current window. Lower the background slow start
519  * threshold also.
520  */
521 void
tcp_ledbat_after_timeout(struct tcpcb * tp)522 tcp_ledbat_after_timeout(struct tcpcb *tp)
523 {
524 	if (tp->t_state >= TCPS_ESTABLISHED) {
525 		tcp_ledbat_clear_state(tp);
526 		tcp_ledbat_pre_fr(tp);
527 		tp->snd_cwnd = tp->t_maxseg;
528 
529 		tcp_update_pacer_state(tp);
530 	}
531 }
532 
533 /*
534  * Indicate whether this ack should be delayed.
535  * We can delay the ack if:
536  *      - our last ack wasn't a 0-sized window.
537  *      - the peer hasn't sent us a TH_PUSH data packet: if he did, take this
538  *      as a clue that we need to ACK without any delay. This helps higher
539  *	level protocols who won't send us more data even if the window is
540  *      open because their last "segment" hasn't been ACKed
541  * Otherwise the receiver will ack every other full-sized segment or when the
542  * delayed ack timer fires. This will help to generate better rtt estimates for
543  * the other end if it is a ledbat sender.
544  *
545  */
546 
547 static int
tcp_ledbat_delay_ack(struct tcpcb * tp,struct tcphdr * th)548 tcp_ledbat_delay_ack(struct tcpcb *tp, struct tcphdr *th)
549 {
550 	return tcp_cc_delay_ack(tp, th);
551 }
552 
553 /* Change a connection to use ledbat. First, lower bg_ssthresh value
554  * if it needs to be.
555  */
556 void
tcp_ledbat_switch_cc(struct tcpcb * tp)557 tcp_ledbat_switch_cc(struct tcpcb *tp)
558 {
559 	uint32_t cwnd;
560 
561 	tcp_ledbat_clear_state(tp);
562 
563 	if (tp->bg_ssthresh == 0 || tp->bg_ssthresh > tp->snd_ssthresh) {
564 		tp->bg_ssthresh = tp->snd_ssthresh;
565 	}
566 
567 	cwnd = min(tp->snd_wnd, tp->snd_cwnd);
568 
569 	if (tp->snd_cwnd > tp->bg_ssthresh) {
570 		cwnd = cwnd / tp->t_maxseg;
571 	} else {
572 		cwnd = cwnd / 2 / tp->t_maxseg;
573 	}
574 
575 	if (cwnd < bg_ss_fltsz) {
576 		cwnd = bg_ss_fltsz;
577 	}
578 
579 	tp->snd_cwnd = cwnd * tp->t_maxseg;
580 	tp->t_bytes_acked = 0;
581 
582 	os_atomic_inc(&tcp_cc_ledbat.num_sockets, relaxed);
583 }
584