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