Home | History | Annotate | Line # | Download | only in netinet
tcp_congctl.c revision 1.9
      1 /*	$NetBSD: tcp_congctl.c,v 1.9 2006/10/19 11:42:02 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
      9  * Facility, NASA Ames Research Center.
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Charles M. Hannum.
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by Rui Paulo.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *	This product includes software developed by the NetBSD
     26  *	Foundation, Inc. and its contributors.
     27  * 4. Neither the name of The NetBSD Foundation nor the names of its
     28  *    contributors may be used to endorse or promote products derived
     29  *    from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 /*
     45  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     46  * All rights reserved.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the project nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 /*
     74  *      @(#)COPYRIGHT   1.1 (NRL) 17 January 1995
     75  *
     76  * NRL grants permission for redistribution and use in source and binary
     77  * forms, with or without modification, of the software and documentation
     78  * created at NRL provided that the following conditions are met:
     79  *
     80  * 1. Redistributions of source code must retain the above copyright
     81  *    notice, this list of conditions and the following disclaimer.
     82  * 2. Redistributions in binary form must reproduce the above copyright
     83  *    notice, this list of conditions and the following disclaimer in the
     84  *    documentation and/or other materials provided with the distribution.
     85  * 3. All advertising materials mentioning features or use of this software
     86  *    must display the following acknowledgements:
     87  *      This product includes software developed by the University of
     88  *      California, Berkeley and its contributors.
     89  *      This product includes software developed at the Information
     90  *      Technology Division, US Naval Research Laboratory.
     91  * 4. Neither the name of the NRL nor the names of its contributors
     92  *    may be used to endorse or promote products derived from this software
     93  *    without specific prior written permission.
     94  *
     95  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
     96  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     97  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     98  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
     99  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    100  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    101  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    102  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    103  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    104  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    105  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    106  *
    107  * The views and conclusions contained in the software and documentation
    108  * are those of the authors and should not be interpreted as representing
    109  * official policies, either expressed or implied, of the US Naval
    110  * Research Laboratory (NRL).
    111  */
    112 
    113 /*
    114  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
    115  *	The Regents of the University of California.  All rights reserved.
    116  *
    117  * Redistribution and use in source and binary forms, with or without
    118  * modification, are permitted provided that the following conditions
    119  * are met:
    120  * 1. Redistributions of source code must retain the above copyright
    121  *    notice, this list of conditions and the following disclaimer.
    122  * 2. Redistributions in binary form must reproduce the above copyright
    123  *    notice, this list of conditions and the following disclaimer in the
    124  *    documentation and/or other materials provided with the distribution.
    125  * 3. Neither the name of the University nor the names of its contributors
    126  *    may be used to endorse or promote products derived from this software
    127  *    without specific prior written permission.
    128  *
    129  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    130  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    131  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    132  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    133  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    134  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    135  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    136  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    137  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    138  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    139  * SUCH DAMAGE.
    140  *
    141  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
    142  */
    143 
    144 #include <sys/cdefs.h>
    145 __KERNEL_RCSID(0, "$NetBSD: tcp_congctl.c,v 1.9 2006/10/19 11:42:02 yamt Exp $");
    146 
    147 #include "opt_inet.h"
    148 #include "opt_tcp_debug.h"
    149 #include "opt_tcp_congctl.h"
    150 
    151 #include <sys/param.h>
    152 #include <sys/systm.h>
    153 #include <sys/malloc.h>
    154 #include <sys/mbuf.h>
    155 #include <sys/protosw.h>
    156 #include <sys/socket.h>
    157 #include <sys/socketvar.h>
    158 #include <sys/errno.h>
    159 #include <sys/syslog.h>
    160 #include <sys/pool.h>
    161 #include <sys/domain.h>
    162 #include <sys/kernel.h>
    163 #include <sys/lock.h>
    164 
    165 #include <net/if.h>
    166 #include <net/route.h>
    167 
    168 #include <netinet/in.h>
    169 #include <netinet/in_systm.h>
    170 #include <netinet/ip.h>
    171 #include <netinet/in_pcb.h>
    172 #include <netinet/in_var.h>
    173 #include <netinet/ip_var.h>
    174 
    175 #ifdef INET6
    176 #ifndef INET
    177 #include <netinet/in.h>
    178 #endif
    179 #include <netinet/ip6.h>
    180 #include <netinet6/ip6_var.h>
    181 #include <netinet6/in6_pcb.h>
    182 #include <netinet6/ip6_var.h>
    183 #include <netinet6/in6_var.h>
    184 #include <netinet/icmp6.h>
    185 #include <netinet6/nd6.h>
    186 #endif
    187 
    188 #include <netinet/tcp.h>
    189 #include <netinet/tcp_fsm.h>
    190 #include <netinet/tcp_seq.h>
    191 #include <netinet/tcp_timer.h>
    192 #include <netinet/tcp_var.h>
    193 #include <netinet/tcpip.h>
    194 #include <netinet/tcp_congctl.h>
    195 #ifdef TCP_DEBUG
    196 #include <netinet/tcp_debug.h>
    197 #endif
    198 
    199 /*
    200  * TODO:
    201  *   consider separating the actual implementations in another file.
    202  */
    203 
    204 static int  tcp_reno_fast_retransmit(struct tcpcb *, struct tcphdr *);
    205 static void tcp_reno_slow_retransmit(struct tcpcb *);
    206 static void tcp_reno_fast_retransmit_newack(struct tcpcb *, struct tcphdr *);
    207 static void tcp_reno_newack(struct tcpcb *, struct tcphdr *);
    208 static void tcp_reno_congestion_exp(struct tcpcb *tp);
    209 
    210 static int  tcp_newreno_fast_retransmit(struct tcpcb *, struct tcphdr *);
    211 static void tcp_newreno_fast_retransmit_newack(struct tcpcb *,
    212 	struct tcphdr *);
    213 static void tcp_newreno_newack(struct tcpcb *, struct tcphdr *);
    214 
    215 
    216 static void tcp_congctl_fillnames(void);
    217 
    218 extern int tcprexmtthresh;
    219 
    220 MALLOC_DEFINE(M_TCPCONGCTL, "tcpcongctl", "TCP congestion control structures");
    221 
    222 /*
    223  * Used to list the available congestion control algorithms.
    224  */
    225 struct tcp_congctlent {
    226 	TAILQ_ENTRY(tcp_congctlent) congctl_ent;
    227 	char               congctl_name[TCPCC_MAXLEN];
    228 	struct tcp_congctl *congctl_ctl;
    229 };
    230 TAILQ_HEAD(, tcp_congctlent) tcp_congctlhd;
    231 
    232 struct simplelock tcp_congctl_slock;
    233 
    234 void
    235 tcp_congctl_init(void)
    236 {
    237 	int r;
    238 
    239 	TAILQ_INIT(&tcp_congctlhd);
    240 	simple_lock_init(&tcp_congctl_slock);
    241 
    242 	/* Base algorithms. */
    243 	r = tcp_congctl_register("reno", &tcp_reno_ctl);
    244 	KASSERT(r == 0);
    245 	r = tcp_congctl_register("newreno", &tcp_newreno_ctl);
    246 	KASSERT(r == 0);
    247 
    248 	/* NewReno is the default. */
    249 #ifndef TCP_CONGCTL_DEFAULT
    250 #define TCP_CONGCTL_DEFAULT "newreno"
    251 #endif
    252 
    253 	r = tcp_congctl_select(NULL, TCP_CONGCTL_DEFAULT);
    254 	KASSERT(r == 0);
    255 }
    256 
    257 /*
    258  * Register a congestion algorithm and select it if we have none.
    259  */
    260 int
    261 tcp_congctl_register(const char *name, struct tcp_congctl *tcc)
    262 {
    263 	struct tcp_congctlent *ntcc, *tccp;
    264 
    265 	TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
    266 		if (!strcmp(name, tccp->congctl_name)) {
    267 			/* name already registered */
    268 			return EEXIST;
    269 		}
    270 
    271 	ntcc = malloc(sizeof(*ntcc), M_TCPCONGCTL, M_WAITOK);
    272 
    273 	strlcpy(ntcc->congctl_name, name, sizeof(ntcc->congctl_name) - 1);
    274 	ntcc->congctl_ctl = tcc;
    275 
    276 	TAILQ_INSERT_TAIL(&tcp_congctlhd, ntcc, congctl_ent);
    277 	tcp_congctl_fillnames();
    278 
    279 	if (TAILQ_FIRST(&tcp_congctlhd) == ntcc)
    280 		tcp_congctl_select(NULL, name);
    281 
    282 	return 0;
    283 }
    284 
    285 int
    286 tcp_congctl_unregister(const char *name)
    287 {
    288 	struct tcp_congctlent *tccp, *rtccp;
    289 	unsigned int size;
    290 
    291 	rtccp = NULL;
    292 	size = 0;
    293 	TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
    294 		if (!strcmp(name, tccp->congctl_name))
    295 			rtccp = tccp;
    296 		size++;
    297 	}
    298 
    299 	if (!rtccp)
    300 		return ENOENT;
    301 
    302 	if (size <= 1 || tcp_congctl_global == rtccp->congctl_ctl ||
    303 	    rtccp->congctl_ctl->refcnt)
    304 		return EBUSY;
    305 
    306 	TAILQ_REMOVE(&tcp_congctlhd, rtccp, congctl_ent);
    307 	free(rtccp, M_TCPCONGCTL);
    308 	tcp_congctl_fillnames();
    309 
    310 	return 0;
    311 }
    312 
    313 /*
    314  * Select a congestion algorithm by name.
    315  */
    316 int
    317 tcp_congctl_select(struct tcpcb *tp, const char *name)
    318 {
    319 	struct tcp_congctlent *tccp;
    320 
    321 	KASSERT(name);
    322 
    323 	TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
    324 		if (!strcmp(name, tccp->congctl_name)) {
    325 			if (tp) {
    326 				simple_lock(&tcp_congctl_slock);
    327 				tp->t_congctl->refcnt--;
    328 				tp->t_congctl = tccp->congctl_ctl;
    329 				tp->t_congctl->refcnt++;
    330 				simple_unlock(&tcp_congctl_slock);
    331 			} else {
    332 				tcp_congctl_global = tccp->congctl_ctl;
    333 				strlcpy(tcp_congctl_global_name,
    334 				    tccp->congctl_name,
    335 				    sizeof(tcp_congctl_global_name) - 1);
    336 			}
    337 			return 0;
    338 		}
    339 
    340 	return EINVAL;
    341 }
    342 
    343 /*
    344  * Returns the name of a congestion algorithm.
    345  */
    346 const char *
    347 tcp_congctl_bystruct(const struct tcp_congctl *tcc)
    348 {
    349 	struct tcp_congctlent *tccp;
    350 
    351 	KASSERT(tcc);
    352 
    353 	TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
    354 		if (tccp->congctl_ctl == tcc)
    355 			return tccp->congctl_name;
    356 
    357 	return NULL;
    358 }
    359 
    360 static void
    361 tcp_congctl_fillnames(void)
    362 {
    363 	struct tcp_congctlent *tccp;
    364 	const char *delim = " ";
    365 
    366 	tcp_congctl_avail[0] = '\0';
    367 	TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
    368 		strlcat(tcp_congctl_avail, tccp->congctl_name,
    369 		    sizeof(tcp_congctl_avail) - 1);
    370 		if (TAILQ_NEXT(tccp, congctl_ent))
    371 			strlcat(tcp_congctl_avail, delim,
    372 			    sizeof(tcp_congctl_avail) - 1);
    373 	}
    374 
    375 }
    376 
    377 /* ------------------------------------------------------------------------ */
    378 
    379 /*
    380  * TCP/Reno congestion control.
    381  */
    382 static void
    383 tcp_reno_congestion_exp(struct tcpcb *tp)
    384 {
    385 	u_int win;
    386 
    387 	/*
    388 	 * Halve the congestion window and reduce the
    389 	 * slow start threshold.
    390 	 */
    391 	win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
    392 	if (win < 2)
    393 		win = 2;
    394 
    395 	tp->snd_ssthresh = win * tp->t_segsz;
    396 	tp->snd_recover = tp->snd_max;
    397 	tp->snd_cwnd = tp->snd_ssthresh;
    398 
    399 	/*
    400 	 * When using TCP ECN, notify the peer that
    401 	 * we reduced the cwnd.
    402 	 */
    403 	if (TCP_ECN_ALLOWED(tp))
    404 		tp->t_flags |= TF_ECN_SND_CWR;
    405 }
    406 
    407 
    408 
    409 static int
    410 tcp_reno_fast_retransmit(struct tcpcb *tp, struct tcphdr *th)
    411 {
    412 	/*
    413 	 * We know we're losing at the current
    414 	 * window size so do congestion avoidance
    415 	 * (set ssthresh to half the current window
    416 	 * and pull our congestion window back to
    417 	 * the new ssthresh).
    418 	 *
    419 	 * Dup acks mean that packets have left the
    420 	 * network (they're now cached at the receiver)
    421 	 * so bump cwnd by the amount in the receiver
    422 	 * to keep a constant cwnd packets in the
    423 	 * network.
    424 	 *
    425 	 * If we are using TCP/SACK, then enter
    426 	 * Fast Recovery if the receiver SACKs
    427 	 * data that is tcprexmtthresh * MSS
    428 	 * bytes past the last ACKed segment,
    429 	 * irrespective of the number of DupAcks.
    430 	 */
    431 
    432 	tcp_seq onxt;
    433 
    434 	onxt = tp->snd_nxt;
    435 	tcp_reno_congestion_exp(tp);
    436 	tp->t_partialacks = 0;
    437 	TCP_TIMER_DISARM(tp, TCPT_REXMT);
    438 	tp->t_rtttime = 0;
    439 	if (TCP_SACK_ENABLED(tp)) {
    440 		tp->t_dupacks = tcprexmtthresh;
    441 		tp->sack_newdata = tp->snd_nxt;
    442 		tp->snd_cwnd = tp->t_segsz;
    443 		(void) tcp_output(tp);
    444 		return 0;
    445 	}
    446 	tp->snd_nxt = th->th_ack;
    447 	tp->snd_cwnd = tp->t_segsz;
    448 	(void) tcp_output(tp);
    449 	tp->snd_cwnd = tp->snd_ssthresh + tp->t_segsz * tp->t_dupacks;
    450 	if (SEQ_GT(onxt, tp->snd_nxt))
    451 		tp->snd_nxt = onxt;
    452 
    453 	return 0;
    454 }
    455 
    456 static void
    457 tcp_reno_slow_retransmit(struct tcpcb *tp)
    458 {
    459 	u_int win;
    460 
    461 	/*
    462 	 * Close the congestion window down to one segment
    463 	 * (we'll open it by one segment for each ack we get).
    464 	 * Since we probably have a window's worth of unacked
    465 	 * data accumulated, this "slow start" keeps us from
    466 	 * dumping all that data as back-to-back packets (which
    467 	 * might overwhelm an intermediate gateway).
    468 	 *
    469 	 * There are two phases to the opening: Initially we
    470 	 * open by one mss on each ack.  This makes the window
    471 	 * size increase exponentially with time.  If the
    472 	 * window is larger than the path can handle, this
    473 	 * exponential growth results in dropped packet(s)
    474 	 * almost immediately.  To get more time between
    475 	 * drops but still "push" the network to take advantage
    476 	 * of improving conditions, we switch from exponential
    477 	 * to linear window opening at some threshhold size.
    478 	 * For a threshhold, we use half the current window
    479 	 * size, truncated to a multiple of the mss.
    480 	 *
    481 	 * (the minimum cwnd that will give us exponential
    482 	 * growth is 2 mss.  We don't allow the threshhold
    483 	 * to go below this.)
    484 	 */
    485 
    486 	win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
    487 	if (win < 2)
    488 		win = 2;
    489 	/* Loss Window MUST be one segment. */
    490 	tp->snd_cwnd = tp->t_segsz;
    491 	tp->snd_ssthresh = win * tp->t_segsz;
    492 	tp->t_partialacks = -1;
    493 	tp->t_dupacks = 0;
    494 	tp->t_bytes_acked = 0;
    495 }
    496 
    497 static void
    498 tcp_reno_fast_retransmit_newack(struct tcpcb *tp, struct tcphdr *th __unused)
    499 {
    500 	if (tp->t_partialacks < 0) {
    501 		/*
    502 		 * We were not in fast recovery.  Reset the duplicate ack
    503 		 * counter.
    504 		 */
    505 		tp->t_dupacks = 0;
    506 	} else {
    507 		/*
    508 		 * Clamp the congestion window to the crossover point and
    509 		 * exit fast recovery.
    510 		 */
    511 		if (tp->snd_cwnd > tp->snd_ssthresh)
    512 			tp->snd_cwnd = tp->snd_ssthresh;
    513 		tp->t_partialacks = -1;
    514 		tp->t_dupacks = 0;
    515 		tp->t_bytes_acked = 0;
    516 	}
    517 }
    518 
    519 static void
    520 tcp_reno_newack(struct tcpcb *tp, struct tcphdr *th __unused)
    521 {
    522 	/*
    523 	 * When new data is acked, open the congestion window.
    524 	 */
    525 
    526 	u_int cw = tp->snd_cwnd;
    527 	u_int incr = tp->t_segsz;
    528 
    529 	if (tcp_do_abc) {
    530 
    531 		/*
    532 		 * RFC 3465 Appropriate Byte Counting (ABC)
    533 		 */
    534 
    535 		int acked = th->th_ack - tp->snd_una;
    536 
    537 		if (cw >= tp->snd_ssthresh) {
    538 			tp->t_bytes_acked += acked;
    539 			if (tp->t_bytes_acked >= cw) {
    540 				/* Time to increase the window. */
    541 				tp->t_bytes_acked -= cw;
    542 			} else {
    543 				/* No need to increase yet. */
    544 				incr = 0;
    545 			}
    546 		} else {
    547 			/*
    548 			 * use 2*SMSS or 1*SMSS for the "L" param,
    549 			 * depending on sysctl setting.
    550 			 *
    551 			 * (See RFC 3465 2.3 Choosing the Limit)
    552 			 */
    553 			u_int abc_lim;
    554 
    555 			abc_lim = (tcp_abc_aggressive == 0 ||
    556 			    tp->snd_nxt != tp->snd_max) ? incr : incr * 2;
    557 			incr = min(acked, abc_lim);
    558 		}
    559 	} else {
    560 
    561 		/*
    562 		 * If the window gives us less than ssthresh packets
    563 		 * in flight, open exponentially (segsz per packet).
    564 		 * Otherwise open linearly: segsz per window
    565 		 * (segsz^2 / cwnd per packet).
    566 		 */
    567 
    568 		if (cw >= tp->snd_ssthresh) {
    569 			incr = incr * incr / cw;
    570 		}
    571 	}
    572 
    573 	tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale);
    574 }
    575 
    576 struct tcp_congctl tcp_reno_ctl = {
    577 	.fast_retransmit = tcp_reno_fast_retransmit,
    578 	.slow_retransmit = tcp_reno_slow_retransmit,
    579 	.fast_retransmit_newack = tcp_reno_fast_retransmit_newack,
    580 	.newack = tcp_reno_newack,
    581 	.cong_exp = tcp_reno_congestion_exp,
    582 };
    583 
    584 /*
    585  * TCP/NewReno Congestion control.
    586  */
    587 static int
    588 tcp_newreno_fast_retransmit(struct tcpcb *tp, struct tcphdr *th)
    589 {
    590 	if (SEQ_LT(th->th_ack, tp->snd_high)) {
    591 		/*
    592 		 * False fast retransmit after timeout.
    593 		 * Do not enter fast recovery
    594 		 */
    595 		tp->t_dupacks = 0;
    596 		return 1;
    597 	} else {
    598 		/*
    599 		 * Fast retransmit is same as reno.
    600 		 */
    601 		return tcp_reno_fast_retransmit(tp, th);
    602 	}
    603 
    604 	return 0;
    605 }
    606 
    607 /*
    608  * Implement the NewReno response to a new ack, checking for partial acks in
    609  * fast recovery.
    610  */
    611 static void
    612 tcp_newreno_fast_retransmit_newack(struct tcpcb *tp, struct tcphdr *th)
    613 {
    614 	if (tp->t_partialacks < 0) {
    615 		/*
    616 		 * We were not in fast recovery.  Reset the duplicate ack
    617 		 * counter.
    618 		 */
    619 		tp->t_dupacks = 0;
    620 	} else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
    621 		/*
    622 		 * This is a partial ack.  Retransmit the first unacknowledged
    623 		 * segment and deflate the congestion window by the amount of
    624 		 * acknowledged data.  Do not exit fast recovery.
    625 		 */
    626 		tcp_seq onxt = tp->snd_nxt;
    627 		u_long ocwnd = tp->snd_cwnd;
    628 
    629 		/*
    630 		 * snd_una has not yet been updated and the socket's send
    631 		 * buffer has not yet drained off the ACK'd data, so we
    632 		 * have to leave snd_una as it was to get the correct data
    633 		 * offset in tcp_output().
    634 		 */
    635 		if (++tp->t_partialacks == 1)
    636 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    637 		tp->t_rtttime = 0;
    638 		tp->snd_nxt = th->th_ack;
    639 		/*
    640 		 * Set snd_cwnd to one segment beyond ACK'd offset.  snd_una
    641 		 * is not yet updated when we're called.
    642 		 */
    643 		tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
    644 		(void) tcp_output(tp);
    645 		tp->snd_cwnd = ocwnd;
    646 		if (SEQ_GT(onxt, tp->snd_nxt))
    647 			tp->snd_nxt = onxt;
    648 		/*
    649 		 * Partial window deflation.  Relies on fact that tp->snd_una
    650 		 * not updated yet.
    651 		 */
    652 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz);
    653 	} else {
    654 		/*
    655 		 * Complete ack.  Inflate the congestion window to ssthresh
    656 		 * and exit fast recovery.
    657 		 *
    658 		 * Window inflation should have left us with approx.
    659 		 * snd_ssthresh outstanding data.  But in case we
    660 		 * would be inclined to send a burst, better to do
    661 		 * it via the slow start mechanism.
    662 		 */
    663 		if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
    664 			tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
    665 			    + tp->t_segsz;
    666 		else
    667 			tp->snd_cwnd = tp->snd_ssthresh;
    668 		tp->t_partialacks = -1;
    669 		tp->t_dupacks = 0;
    670 		tp->t_bytes_acked = 0;
    671 	}
    672 }
    673 
    674 static void
    675 tcp_newreno_newack(struct tcpcb *tp, struct tcphdr *th)
    676 {
    677 	/*
    678 	 * If we are still in fast recovery (meaning we are using
    679 	 * NewReno and we have only received partial acks), do not
    680 	 * inflate the window yet.
    681 	 */
    682 	if (tp->t_partialacks < 0)
    683 		tcp_reno_newack(tp, th);
    684 }
    685 
    686 
    687 struct tcp_congctl tcp_newreno_ctl = {
    688 	.fast_retransmit = tcp_newreno_fast_retransmit,
    689 	.slow_retransmit = tcp_reno_slow_retransmit,
    690 	.fast_retransmit_newack = tcp_newreno_fast_retransmit_newack,
    691 	.newack = tcp_newreno_newack,
    692 	.cong_exp = tcp_reno_congestion_exp,
    693 };
    694 
    695 
    696