]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2010-2020 Apple Inc. All rights reserved. |
6d2010ae A |
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/mcache.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 | ||
6d2010ae | 40 | #include <netinet/ip6.h> |
6d2010ae A |
41 | #include <netinet/ip_var.h> |
42 | #include <netinet/tcp.h> | |
43 | #include <netinet/tcp_fsm.h> | |
44 | #include <netinet/tcp_timer.h> | |
45 | #include <netinet/tcp_var.h> | |
46 | #include <netinet/tcpip.h> | |
47 | #include <netinet/tcp_cc.h> | |
48 | ||
49 | #include <libkern/OSAtomic.h> | |
50 | ||
51 | /* This file implements an alternate TCP congestion control algorithm | |
52 | * for background transport developed by LEDBAT working group at IETF and | |
53 | * described in draft: draft-ietf-ledbat-congestion-02 | |
54 | */ | |
55 | ||
56 | int tcp_ledbat_init(struct tcpcb *tp); | |
57 | int tcp_ledbat_cleanup(struct tcpcb *tp); | |
58 | void tcp_ledbat_cwnd_init(struct tcpcb *tp); | |
fe8ab488 | 59 | void tcp_ledbat_congestion_avd(struct tcpcb *tp, struct tcphdr *th); |
6d2010ae | 60 | void tcp_ledbat_ack_rcvd(struct tcpcb *tp, struct tcphdr *th); |
316670eb | 61 | void tcp_ledbat_pre_fr(struct tcpcb *tp); |
6d2010ae A |
62 | void tcp_ledbat_post_fr(struct tcpcb *tp, struct tcphdr *th); |
63 | void tcp_ledbat_after_idle(struct tcpcb *tp); | |
64 | void tcp_ledbat_after_timeout(struct tcpcb *tp); | |
f427ee49 | 65 | static int tcp_ledbat_delay_ack(struct tcpcb *tp, struct tcphdr *th); |
6d2010ae A |
66 | void tcp_ledbat_switch_cc(struct tcpcb *tp, uint16_t old_cc_index); |
67 | ||
68 | struct tcp_cc_algo tcp_cc_ledbat = { | |
69 | .name = "ledbat", | |
70 | .init = tcp_ledbat_init, | |
71 | .cleanup = tcp_ledbat_cleanup, | |
72 | .cwnd_init = tcp_ledbat_cwnd_init, | |
fe8ab488 | 73 | .congestion_avd = tcp_ledbat_congestion_avd, |
6d2010ae A |
74 | .ack_rcvd = tcp_ledbat_ack_rcvd, |
75 | .pre_fr = tcp_ledbat_pre_fr, | |
76 | .post_fr = tcp_ledbat_post_fr, | |
77 | .after_idle = tcp_ledbat_after_idle, | |
78 | .after_timeout = tcp_ledbat_after_timeout, | |
79 | .delay_ack = tcp_ledbat_delay_ack, | |
80 | .switch_to = tcp_ledbat_switch_cc | |
81 | }; | |
82 | ||
0a7de745 A |
83 | /* Target queuing delay in milliseconds. This includes the processing |
84 | * and scheduling delay on both of the end-hosts. A LEDBAT sender tries | |
6d2010ae | 85 | * to keep queuing delay below this limit. When the queuing delay |
0a7de745 | 86 | * goes above this limit, a LEDBAT sender will start reducing the |
6d2010ae A |
87 | * congestion window. |
88 | * | |
0a7de745 | 89 | * The LEDBAT draft says that target queue delay MUST be 100 ms for |
6d2010ae A |
90 | * inter-operability. |
91 | */ | |
5ba3f43e | 92 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, bg_target_qdelay, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 93 | int, target_qdelay, 100, "Target queuing delay"); |
6d2010ae A |
94 | |
95 | /* Allowed increase and tether are used to place an upper bound on | |
96 | * congestion window based on the amount of data that is outstanding. | |
0a7de745 | 97 | * This will limit the congestion window when the amount of data in |
6d2010ae | 98 | * flight is little because the application is writing to the socket |
0a7de745 | 99 | * intermittently and is preventing the connection from becoming idle . |
6d2010ae A |
100 | * |
101 | * max_allowed_cwnd = allowed_increase + (tether * flight_size) | |
102 | * cwnd = min(cwnd, max_allowed_cwnd) | |
103 | * | |
4b17d6b6 A |
104 | * 'Allowed_increase' parameter is set to 8. If the flight size is zero, then |
105 | * we want the congestion window to be at least 8 packets to reduce the | |
0a7de745 | 106 | * delay induced by delayed ack. This helps when the receiver is acking |
4b17d6b6 | 107 | * more than 2 packets at a time (stretching acks for better performance). |
0a7de745 | 108 | * |
6d2010ae A |
109 | * 'Tether' is also set to 2. We do not want this to limit the growth of cwnd |
110 | * during slow-start. | |
0a7de745 | 111 | */ |
5ba3f43e | 112 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, bg_allowed_increase, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 A |
113 | int, allowed_increase, 8, |
114 | "Additive constant used to calculate max allowed congestion window"); | |
6d2010ae A |
115 | |
116 | /* Left shift for cwnd to get tether value of 2 */ | |
5ba3f43e | 117 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, bg_tether_shift, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 118 | int, tether_shift, 1, "Tether shift for max allowed congestion window"); |
6d2010ae | 119 | |
0a7de745 | 120 | /* Start with an initial window of 2. This will help to get more accurate |
6d2010ae A |
121 | * minimum RTT measurement in the beginning. It will help to probe |
122 | * the path slowly and will not add to the existing delay if the path is | |
123 | * already congested. Using 2 packets will reduce the delay induced by delayed-ack. | |
124 | */ | |
5ba3f43e | 125 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, bg_ss_fltsz, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 126 | uint32_t, bg_ss_fltsz, 2, "Initial congestion window for background transport"); |
6d2010ae A |
127 | |
128 | extern int rtt_samples_per_slot; | |
129 | ||
0a7de745 A |
130 | static void |
131 | update_cwnd(struct tcpcb *tp, uint32_t incr) | |
132 | { | |
6d2010ae | 133 | uint32_t max_allowed_cwnd = 0, flight_size = 0; |
ecc0ceb4 | 134 | uint32_t base_rtt; |
6d2010ae A |
135 | |
136 | base_rtt = get_base_rtt(tp); | |
137 | ||
138 | /* If we do not have a good RTT measurement yet, increment | |
0a7de745 | 139 | * congestion window by the default value. |
6d2010ae A |
140 | */ |
141 | if (base_rtt == 0 || tp->t_rttcur == 0) { | |
142 | tp->snd_cwnd += incr; | |
143 | goto check_max; | |
144 | } | |
6d2010ae | 145 | |
ecc0ceb4 A |
146 | if (tp->t_rttcur <= (base_rtt + target_qdelay)) { |
147 | /* | |
148 | * Delay decreased or remained the same, we can increase | |
6d2010ae A |
149 | * the congestion window according to RFC 3465. |
150 | * | |
151 | * Move background slow-start threshold to current | |
152 | * congestion window so that the next time (after some idle | |
0a7de745 | 153 | * period), we can attempt to do slow-start till here if there |
6d2010ae A |
154 | * is no increase in rtt |
155 | */ | |
0a7de745 | 156 | if (tp->bg_ssthresh < tp->snd_cwnd) { |
6d2010ae | 157 | tp->bg_ssthresh = tp->snd_cwnd; |
0a7de745 A |
158 | } |
159 | tp->snd_cwnd += incr; | |
6d2010ae | 160 | } else { |
0a7de745 A |
161 | /* In response to an increase in rtt, reduce the congestion |
162 | * window by one-eighth. This will help to yield immediately | |
6d2010ae A |
163 | * to a competing stream. |
164 | */ | |
165 | uint32_t redwin; | |
166 | ||
0a7de745 | 167 | redwin = tp->snd_cwnd >> 3; |
6d2010ae | 168 | tp->snd_cwnd -= redwin; |
0a7de745 | 169 | if (tp->snd_cwnd < bg_ss_fltsz * tp->t_maxseg) { |
6d2010ae | 170 | tp->snd_cwnd = bg_ss_fltsz * tp->t_maxseg; |
0a7de745 | 171 | } |
6d2010ae | 172 | |
0a7de745 | 173 | /* Lower background slow-start threshold so that the connection |
6d2010ae A |
174 | * will go into congestion avoidance phase |
175 | */ | |
0a7de745 | 176 | if (tp->bg_ssthresh > tp->snd_cwnd) { |
6d2010ae | 177 | tp->bg_ssthresh = tp->snd_cwnd; |
0a7de745 | 178 | } |
6d2010ae A |
179 | } |
180 | check_max: | |
181 | /* Calculate the outstanding flight size and restrict the | |
182 | * congestion window to a factor of flight size. | |
183 | */ | |
184 | flight_size = tp->snd_max - tp->snd_una; | |
185 | ||
0a7de745 A |
186 | max_allowed_cwnd = (allowed_increase * tp->t_maxseg) |
187 | + (flight_size << tether_shift); | |
6d2010ae A |
188 | tp->snd_cwnd = min(tp->snd_cwnd, max_allowed_cwnd); |
189 | return; | |
190 | } | |
191 | ||
0a7de745 A |
192 | int |
193 | tcp_ledbat_init(struct tcpcb *tp) | |
194 | { | |
6d2010ae A |
195 | #pragma unused(tp) |
196 | OSIncrementAtomic((volatile SInt32 *)&tcp_cc_ledbat.num_sockets); | |
197 | return 0; | |
198 | } | |
199 | ||
0a7de745 A |
200 | int |
201 | tcp_ledbat_cleanup(struct tcpcb *tp) | |
202 | { | |
6d2010ae A |
203 | #pragma unused(tp) |
204 | OSDecrementAtomic((volatile SInt32 *)&tcp_cc_ledbat.num_sockets); | |
205 | return 0; | |
206 | } | |
207 | ||
0a7de745 A |
208 | /* Initialize the congestion window for a connection |
209 | * | |
6d2010ae A |
210 | */ |
211 | ||
212 | void | |
0a7de745 A |
213 | tcp_ledbat_cwnd_init(struct tcpcb *tp) |
214 | { | |
6d2010ae A |
215 | tp->snd_cwnd = tp->t_maxseg * bg_ss_fltsz; |
216 | tp->bg_ssthresh = tp->snd_ssthresh; | |
217 | } | |
218 | ||
0a7de745 A |
219 | /* Function to handle an in-sequence ack which is fast-path processing |
220 | * of an in sequence ack in tcp_input function (called as header prediction). | |
6d2010ae A |
221 | * This gets called only during congestion avoidance phase. |
222 | */ | |
223 | void | |
0a7de745 A |
224 | tcp_ledbat_congestion_avd(struct tcpcb *tp, struct tcphdr *th) |
225 | { | |
6d2010ae A |
226 | int acked = 0; |
227 | u_int32_t incr = 0; | |
228 | ||
39236c6e | 229 | acked = BYTES_ACKED(th, tp); |
6d2010ae A |
230 | tp->t_bytes_acked += acked; |
231 | if (tp->t_bytes_acked > tp->snd_cwnd) { | |
232 | tp->t_bytes_acked -= tp->snd_cwnd; | |
233 | incr = tp->t_maxseg; | |
234 | } | |
235 | ||
236 | if (tp->snd_cwnd < tp->snd_wnd && incr > 0) { | |
237 | update_cwnd(tp, incr); | |
238 | } | |
239 | } | |
240 | /* Function to process an ack. | |
241 | */ | |
242 | void | |
0a7de745 A |
243 | tcp_ledbat_ack_rcvd(struct tcpcb *tp, struct tcphdr *th) |
244 | { | |
6d2010ae A |
245 | /* |
246 | * RFC 3465 - Appropriate Byte Counting. | |
247 | * | |
248 | * If the window is currently less than ssthresh, | |
249 | * open the window by the number of bytes ACKed by | |
250 | * the last ACK, however clamp the window increase | |
251 | * to an upper limit "L". | |
252 | * | |
253 | * In congestion avoidance phase, open the window by | |
254 | * one segment each time "bytes_acked" grows to be | |
255 | * greater than or equal to the congestion window. | |
256 | */ | |
257 | ||
f427ee49 A |
258 | uint32_t cw = tp->snd_cwnd; |
259 | uint32_t incr = tp->t_maxseg; | |
260 | uint32_t acked = 0; | |
6d2010ae | 261 | |
39236c6e | 262 | acked = BYTES_ACKED(th, tp); |
6d2010ae A |
263 | tp->t_bytes_acked += acked; |
264 | if (cw >= tp->bg_ssthresh) { | |
265 | /* congestion-avoidance */ | |
266 | if (tp->t_bytes_acked < cw) { | |
267 | /* No need to increase yet. */ | |
268 | incr = 0; | |
269 | } | |
270 | } else { | |
271 | /* | |
272 | * If the user explicitly enables RFC3465 | |
273 | * use 2*SMSS for the "L" param. Otherwise | |
274 | * use the more conservative 1*SMSS. | |
275 | * | |
276 | * (See RFC 3465 2.3 Choosing the Limit) | |
277 | */ | |
278 | u_int abc_lim; | |
279 | ||
f427ee49 | 280 | abc_lim = (tp->snd_nxt == tp->snd_max) ? incr * 2 : incr; |
6d2010ae | 281 | |
f427ee49 | 282 | incr = ulmin(acked, abc_lim); |
6d2010ae | 283 | } |
0a7de745 | 284 | if (tp->t_bytes_acked >= cw) { |
6d2010ae | 285 | tp->t_bytes_acked -= cw; |
0a7de745 A |
286 | } |
287 | if (incr > 0) { | |
6d2010ae | 288 | update_cwnd(tp, incr); |
0a7de745 | 289 | } |
6d2010ae A |
290 | } |
291 | ||
292 | void | |
0a7de745 A |
293 | tcp_ledbat_pre_fr(struct tcpcb *tp) |
294 | { | |
6d2010ae A |
295 | uint32_t win; |
296 | ||
0a7de745 A |
297 | win = min(tp->snd_wnd, tp->snd_cwnd) / |
298 | 2 / tp->t_maxseg; | |
299 | if (win < 2) { | |
6d2010ae | 300 | win = 2; |
0a7de745 A |
301 | } |
302 | tp->snd_ssthresh = win * tp->t_maxseg; | |
303 | if (tp->bg_ssthresh > tp->snd_ssthresh) { | |
6d2010ae | 304 | tp->bg_ssthresh = tp->snd_ssthresh; |
0a7de745 | 305 | } |
316670eb A |
306 | |
307 | tcp_cc_resize_sndbuf(tp); | |
6d2010ae A |
308 | } |
309 | ||
310 | void | |
0a7de745 A |
311 | tcp_ledbat_post_fr(struct tcpcb *tp, struct tcphdr *th) |
312 | { | |
6d2010ae A |
313 | int32_t ss; |
314 | ||
f427ee49 A |
315 | if (th) { |
316 | ss = tp->snd_max - th->th_ack; | |
317 | } else { | |
318 | ss = tp->snd_max - tp->snd_una; | |
319 | } | |
6d2010ae A |
320 | |
321 | /* | |
322 | * Complete ack. Inflate the congestion window to | |
323 | * ssthresh and exit fast recovery. | |
324 | * | |
325 | * Window inflation should have left us with approx. | |
326 | * snd_ssthresh outstanding data. But in case we | |
327 | * would be inclined to send a burst, better to do | |
328 | * it via the slow start mechanism. | |
39236c6e | 329 | * |
0a7de745 A |
330 | * If the flight size is zero, then make congestion |
331 | * window to be worth at least 2 segments to avoid | |
39236c6e | 332 | * delayed acknowledgement (draft-ietf-tcpm-rfc3782-bis-05). |
6d2010ae | 333 | */ |
0a7de745 | 334 | if (ss < (int32_t)tp->snd_ssthresh) { |
39236c6e | 335 | tp->snd_cwnd = max(ss, tp->t_maxseg) + tp->t_maxseg; |
0a7de745 | 336 | } else { |
6d2010ae | 337 | tp->snd_cwnd = tp->snd_ssthresh; |
0a7de745 | 338 | } |
6d2010ae A |
339 | tp->t_bytes_acked = 0; |
340 | } | |
341 | ||
342 | /* | |
343 | * Function to handle connections that have been idle for | |
344 | * some time. Slow start to get ack "clock" running again. | |
345 | * Clear base history after idle time. | |
346 | */ | |
347 | void | |
0a7de745 A |
348 | tcp_ledbat_after_idle(struct tcpcb *tp) |
349 | { | |
6d2010ae A |
350 | /* Reset the congestion window */ |
351 | tp->snd_cwnd = tp->t_maxseg * bg_ss_fltsz; | |
352 | } | |
353 | ||
0a7de745 | 354 | /* Function to change the congestion window when the retransmit |
6d2010ae A |
355 | * timer fires. The behavior is the same as that for best-effort |
356 | * TCP, reduce congestion window to one segment and start probing | |
357 | * the link using "slow start". The slow start threshold is set | |
358 | * to half of the current window. Lower the background slow start | |
359 | * threshold also. | |
360 | */ | |
361 | void | |
0a7de745 A |
362 | tcp_ledbat_after_timeout(struct tcpcb *tp) |
363 | { | |
364 | if (tp->t_state >= TCPS_ESTABLISHED) { | |
6d2010ae | 365 | u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; |
0a7de745 | 366 | if (win < 2) { |
6d2010ae | 367 | win = 2; |
0a7de745 | 368 | } |
6d2010ae | 369 | tp->snd_ssthresh = win * tp->t_maxseg; |
6d2010ae | 370 | |
0a7de745 | 371 | if (tp->bg_ssthresh > tp->snd_ssthresh) { |
6d2010ae | 372 | tp->bg_ssthresh = tp->snd_ssthresh; |
0a7de745 | 373 | } |
316670eb | 374 | |
39236c6e | 375 | tp->snd_cwnd = tp->t_maxseg; |
316670eb | 376 | tcp_cc_resize_sndbuf(tp); |
6d2010ae A |
377 | } |
378 | } | |
379 | ||
380 | /* | |
381 | * Indicate whether this ack should be delayed. | |
382 | * We can delay the ack if: | |
383 | * - our last ack wasn't a 0-sized window. | |
0a7de745 A |
384 | * - the peer hasn't sent us a TH_PUSH data packet: if he did, take this |
385 | * as a clue that we need to ACK without any delay. This helps higher | |
386 | * level protocols who won't send us more data even if the window is | |
387 | * open because their last "segment" hasn't been ACKed | |
6d2010ae | 388 | * Otherwise the receiver will ack every other full-sized segment or when the |
0a7de745 | 389 | * delayed ack timer fires. This will help to generate better rtt estimates for |
6d2010ae | 390 | * the other end if it is a ledbat sender. |
0a7de745 | 391 | * |
6d2010ae A |
392 | */ |
393 | ||
f427ee49 | 394 | static int |
0a7de745 A |
395 | tcp_ledbat_delay_ack(struct tcpcb *tp, struct tcphdr *th) |
396 | { | |
f427ee49 A |
397 | if (tcp_ack_strategy == TCP_ACK_STRATEGY_MODERN) { |
398 | return tcp_cc_delay_ack(tp, th); | |
399 | } else { | |
400 | if ((tp->t_flags & TF_RXWIN0SENT) == 0 && | |
401 | (th->th_flags & TH_PUSH) == 0 && (tp->t_unacksegs == 1)) { | |
402 | return 1; | |
403 | } | |
404 | return 0; | |
0a7de745 | 405 | } |
6d2010ae A |
406 | } |
407 | ||
408 | /* Change a connection to use ledbat. First, lower bg_ssthresh value | |
0a7de745 | 409 | * if it needs to be. |
6d2010ae A |
410 | */ |
411 | void | |
0a7de745 A |
412 | tcp_ledbat_switch_cc(struct tcpcb *tp, uint16_t old_cc_index) |
413 | { | |
6d2010ae A |
414 | #pragma unused(old_cc_index) |
415 | uint32_t cwnd; | |
416 | ||
0a7de745 | 417 | if (tp->bg_ssthresh == 0 || tp->bg_ssthresh > tp->snd_ssthresh) { |
6d2010ae | 418 | tp->bg_ssthresh = tp->snd_ssthresh; |
0a7de745 | 419 | } |
6d2010ae A |
420 | |
421 | cwnd = min(tp->snd_wnd, tp->snd_cwnd); | |
422 | ||
0a7de745 | 423 | if (tp->snd_cwnd > tp->bg_ssthresh) { |
6d2010ae | 424 | cwnd = cwnd / tp->t_maxseg; |
0a7de745 | 425 | } else { |
6d2010ae | 426 | cwnd = cwnd / 2 / tp->t_maxseg; |
0a7de745 | 427 | } |
6d2010ae | 428 | |
0a7de745 | 429 | if (cwnd < bg_ss_fltsz) { |
6d2010ae | 430 | cwnd = bg_ss_fltsz; |
0a7de745 | 431 | } |
6d2010ae A |
432 | |
433 | tp->snd_cwnd = cwnd * tp->t_maxseg; | |
434 | tp->t_bytes_acked = 0; | |
435 | ||
436 | OSIncrementAtomic((volatile SInt32 *)&tcp_cc_ledbat.num_sockets); | |
437 | } |