]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_newreno.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_newreno.c
CommitLineData
6d2010ae 1/*
fe8ab488 2 * Copyright (c) 2010-2014 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 */
316670eb
A
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.16 2001/08/22 00:59:12 silby Exp $
62 */
6d2010ae
A
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/kernel.h>
66#include <sys/protosw.h>
316670eb 67#include <sys/socketvar.h>
6d2010ae
A
68
69#include <net/route.h>
70#include <netinet/in.h>
71#include <netinet/in_systm.h>
72#include <netinet/ip.h>
73
74#if INET6
75#include <netinet/ip6.h>
76#endif
77#include <netinet/ip_var.h>
78#include <netinet/tcp.h>
79#include <netinet/tcp_fsm.h>
80#include <netinet/tcp_timer.h>
81#include <netinet/tcp_var.h>
82#include <netinet/tcpip.h>
83#include <netinet/tcp_cc.h>
84#include <libkern/OSAtomic.h>
85
86int tcp_newreno_init(struct tcpcb *tp);
87int tcp_newreno_cleanup(struct tcpcb *tp);
88void tcp_newreno_cwnd_init_or_reset(struct tcpcb *tp);
fe8ab488 89void tcp_newreno_congestion_avd(struct tcpcb *tp, struct tcphdr *th);
6d2010ae 90void tcp_newreno_ack_rcvd(struct tcpcb *tp, struct tcphdr *th);
316670eb 91void tcp_newreno_pre_fr(struct tcpcb *tp);
6d2010ae
A
92void tcp_newreno_post_fr(struct tcpcb *tp, struct tcphdr *th);
93void tcp_newreno_after_idle(struct tcpcb *tp);
94void tcp_newreno_after_timeout(struct tcpcb *tp);
95int tcp_newreno_delay_ack(struct tcpcb *tp, struct tcphdr *th);
96void tcp_newreno_switch_cc(struct tcpcb *tp, uint16_t old_index);
97
98struct tcp_cc_algo tcp_cc_newreno = {
99 .name = "newreno",
100 .init = tcp_newreno_init,
101 .cleanup = tcp_newreno_cleanup,
102 .cwnd_init = tcp_newreno_cwnd_init_or_reset,
fe8ab488 103 .congestion_avd = tcp_newreno_congestion_avd,
6d2010ae
A
104 .ack_rcvd = tcp_newreno_ack_rcvd,
105 .pre_fr = tcp_newreno_pre_fr,
106 .post_fr = tcp_newreno_post_fr,
107 .after_idle = tcp_newreno_cwnd_init_or_reset,
108 .after_timeout = tcp_newreno_after_timeout,
109 .delay_ack = tcp_newreno_delay_ack,
110 .switch_to = tcp_newreno_switch_cc
111};
112
0a7de745
A
113int
114tcp_newreno_init(struct tcpcb *tp)
115{
6d2010ae
A
116#pragma unused(tp)
117 OSIncrementAtomic((volatile SInt32 *)&tcp_cc_newreno.num_sockets);
118 return 0;
119}
120
0a7de745
A
121int
122tcp_newreno_cleanup(struct tcpcb *tp)
123{
6d2010ae
A
124#pragma unused(tp)
125 OSDecrementAtomic((volatile SInt32 *)&tcp_cc_newreno.num_sockets);
126 return 0;
127}
128
129/* Initialize the congestion window for a connection or
130 * handles connections that have been idle for
131 * some time. In this state, no acks are
132 * expected to clock out any data we send --
133 * slow start to get ack "clock" running again.
134 *
135 * Set the slow-start flight size depending on whether
136 * this is a local network or not.
137 */
138void
0a7de745
A
139tcp_newreno_cwnd_init_or_reset(struct tcpcb *tp)
140{
fe8ab488 141 tcp_cc_cwnd_init_or_reset(tp);
6d2010ae
A
142}
143
144
145/* Function to handle an in-sequence ack during congestion avoidance phase.
146 * This will get called from header prediction code.
147 */
148void
0a7de745
A
149tcp_newreno_congestion_avd(struct tcpcb *tp, struct tcphdr *th)
150{
fe8ab488 151 uint32_t acked = 0;
39236c6e 152 acked = BYTES_ACKED(th, tp);
6d2010ae
A
153 /*
154 * Grow the congestion window, if the
155 * connection is cwnd bound.
156 */
157 if (tp->snd_cwnd < tp->snd_wnd) {
158 tp->t_bytes_acked += acked;
159 if (tp->t_bytes_acked > tp->snd_cwnd) {
160 tp->t_bytes_acked -= tp->snd_cwnd;
161 tp->snd_cwnd += tp->t_maxseg;
162 }
163 }
164}
165/* Function to process an ack.
166 */
167void
0a7de745
A
168tcp_newreno_ack_rcvd(struct tcpcb *tp, struct tcphdr *th)
169{
6d2010ae
A
170 /*
171 * RFC 3465 - Appropriate Byte Counting.
172 *
173 * If the window is currently less than ssthresh,
174 * open the window by the number of bytes ACKed by
175 * the last ACK, however clamp the window increase
176 * to an upper limit "L".
177 *
178 * In congestion avoidance phase, open the window by
179 * one segment each time "bytes_acked" grows to be
180 * greater than or equal to the congestion window.
181 */
182
39037602
A
183 u_int cw = tp->snd_cwnd;
184 u_int incr = tp->t_maxseg;
6d2010ae
A
185 int acked = 0;
186
39236c6e 187 acked = BYTES_ACKED(th, tp);
6d2010ae 188 if (tcp_do_rfc3465) {
6d2010ae
A
189 if (cw >= tp->snd_ssthresh) {
190 tp->t_bytes_acked += acked;
191 if (tp->t_bytes_acked >= cw) {
192 /* Time to increase the window. */
193 tp->t_bytes_acked -= cw;
194 } else {
195 /* No need to increase yet. */
196 incr = 0;
197 }
198 } else {
199 /*
200 * If the user explicitly enables RFC3465
201 * use 2*SMSS for the "L" param. Otherwise
202 * use the more conservative 1*SMSS.
203 *
204 * (See RFC 3465 2.3 Choosing the Limit)
205 */
fe8ab488 206 uint32_t abc_lim;
6d2010ae 207 abc_lim = (tcp_do_rfc3465_lim2 &&
0a7de745
A
208 tp->snd_nxt == tp->snd_max) ? incr * 2
209 : incr;
6d2010ae
A
210
211 incr = lmin(acked, abc_lim);
212 }
213 } else {
214 /*
215 * If the window gives us less than ssthresh packets
216 * in flight, open exponentially (segsz per packet).
217 * Otherwise open linearly: segsz per window
218 * (segsz^2 / cwnd per packet).
219 */
220
0a7de745 221 if (cw >= tp->snd_ssthresh) {
6d2010ae 222 incr = max((incr * incr / cw), 1);
0a7de745 223 }
6d2010ae 224 }
0a7de745 225 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale);
6d2010ae
A
226}
227
228void
0a7de745
A
229tcp_newreno_pre_fr(struct tcpcb *tp)
230{
6d2010ae
A
231 uint32_t win;
232
0a7de745
A
233 win = min(tp->snd_wnd, tp->snd_cwnd) /
234 2 / tp->t_maxseg;
235 if (win < 2) {
6d2010ae 236 win = 2;
0a7de745
A
237 }
238 tp->snd_ssthresh = win * tp->t_maxseg;
316670eb 239 tcp_cc_resize_sndbuf(tp);
6d2010ae
A
240}
241
242void
0a7de745
A
243tcp_newreno_post_fr(struct tcpcb *tp, struct tcphdr *th)
244{
6d2010ae
A
245 int32_t ss;
246
247 ss = tp->snd_max - th->th_ack;
248
249 /*
250 * Complete ack. Inflate the congestion window to
251 * ssthresh and exit fast recovery.
252 *
253 * Window inflation should have left us with approx.
254 * snd_ssthresh outstanding data. But in case we
255 * would be inclined to send a burst, better to do
256 * it via the slow start mechanism.
39236c6e 257 *
0a7de745
A
258 * If the flight size is zero, then make congestion
259 * window to be worth at least 2 segments to avoid
39236c6e 260 * delayed acknowledgement (draft-ietf-tcpm-rfc3782-bis-05).
6d2010ae 261 */
0a7de745 262 if (ss < (int32_t)tp->snd_ssthresh) {
39236c6e 263 tp->snd_cwnd = max(ss, tp->t_maxseg) + tp->t_maxseg;
0a7de745 264 } else {
6d2010ae 265 tp->snd_cwnd = tp->snd_ssthresh;
0a7de745 266 }
6d2010ae
A
267 tp->t_bytes_acked = 0;
268}
269
0a7de745 270/* Function to change the congestion window when the retransmit
6d2010ae
A
271 * timer fires.
272 */
273void
0a7de745
A
274tcp_newreno_after_timeout(struct tcpcb *tp)
275{
6d2010ae
A
276 /*
277 * Close the congestion window down to one segment
278 * (we'll open it by one segment for each ack we get).
279 * Since we probably have a window's worth of unacked
280 * data accumulated, this "slow start" keeps us from
281 * dumping all that data as back-to-back packets (which
282 * might overwhelm an intermediate gateway).
283 *
284 * There are two phases to the opening: Initially we
285 * open by one mss on each ack. This makes the window
286 * size increase exponentially with time. If the
287 * window is larger than the path can handle, this
288 * exponential growth results in dropped packet(s)
289 * almost immediately. To get more time between
290 * drops but still "push" the network to take advantage
291 * of improving conditions, we switch from exponential
292 * to linear window opening at some threshhold size.
293 * For a threshhold, we use half the current window
294 * size, truncated to a multiple of the mss.
295 *
296 * (the minimum cwnd that will give us exponential
297 * growth is 2 mss. We don't allow the threshhold
298 * to go below this.)
299 */
0a7de745 300 if (tp->t_state >= TCPS_ESTABLISHED) {
6d2010ae 301 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
0a7de745 302 if (win < 2) {
6d2010ae 303 win = 2;
0a7de745 304 }
6d2010ae 305 tp->snd_ssthresh = win * tp->t_maxseg;
316670eb 306
39236c6e 307 tp->snd_cwnd = tp->t_maxseg;
316670eb 308 tcp_cc_resize_sndbuf(tp);
6d2010ae
A
309 }
310}
311
312/*
313 * Indicate whether this ack should be delayed.
314 * We can delay the ack if:
0a7de745 315 * - delayed acks are enabled and set to 1, same as when value is set to 2.
6d2010ae
A
316 * We kept this for binary compatibility.
317 * - delayed acks are enabled and set to 2, will "ack every other packet"
318 * - if our last ack wasn't a 0-sized window.
0a7de745
A
319 * - if the peer hasn't sent us a TH_PUSH data packet (this solves 3649245).
320 * If TH_PUSH is set, take this as a clue that we need to ACK
321 * with no delay. This helps higher level protocols who won't send
322 * us more data even if the window is open because their
6d2010ae 323 * last "segment" hasn't been ACKed
0a7de745 324 * - delayed acks are enabled and set to 3, will do "streaming detection"
6d2010ae
A
325 * (see the comment in tcp_input.c) and
326 * - if we receive more than "maxseg_unacked" full packets in the last 100ms
0a7de745 327 * - if the connection is not in slow-start or idle or loss/recovery states
6d2010ae
A
328 * - if those criteria aren't met, it will ack every other packet.
329 */
330
331int
0a7de745
A
332tcp_newreno_delay_ack(struct tcpcb *tp, struct tcphdr *th)
333{
334 return tcp_cc_delay_ack(tp, th);
6d2010ae
A
335}
336
337/* Switch to newreno from a different CC. If the connection is in
338 * congestion avoidance state, it can continue to use the current
339 * congestion window because it is going to be conservative. But
340 * if the connection is in slow-start, we will halve the congestion
0a7de745 341 * window and let newreno work from there.
6d2010ae
A
342 */
343void
0a7de745
A
344tcp_newreno_switch_cc(struct tcpcb *tp, uint16_t old_index)
345{
6d2010ae
A
346#pragma unused(old_index)
347
348 uint32_t cwnd = min(tp->snd_wnd, tp->snd_cwnd);
349 if (tp->snd_cwnd >= tp->snd_ssthresh) {
350 cwnd = cwnd / tp->t_maxseg;
0a7de745 351 } else {
6d2010ae
A
352 cwnd = cwnd / 2 / tp->t_maxseg;
353 }
fe8ab488 354 tp->snd_cwnd = max(TCP_CC_CWND_INIT_BYTES, cwnd * tp->t_maxseg);
6d2010ae
A
355
356 /* Start counting bytes for RFC 3465 again */
357 tp->t_bytes_acked = 0;
358
359 OSIncrementAtomic((volatile SInt32 *)&tcp_cc_newreno.num_sockets);
360}