1215166Slstewart/*-
2215166Slstewart * Copyright (c) 2007-2008
3215166Slstewart * 	Swinburne University of Technology, Melbourne, Australia.
4215166Slstewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5215166Slstewart * Copyright (c) 2010 The FreeBSD Foundation
6215166Slstewart * All rights reserved.
7215166Slstewart *
8215166Slstewart * This software was developed at the Centre for Advanced Internet
9220560Slstewart * Architectures, Swinburne University of Technology, by Lawrence Stewart and
10220560Slstewart * James Healy, made possible in part by a grant from the Cisco University
11220560Slstewart * Research Program Fund at Community Foundation Silicon Valley.
12215166Slstewart *
13215166Slstewart * Portions of this software were developed at the Centre for Advanced
14215166Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne,
15215166Slstewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16215166Slstewart *
17215166Slstewart * Redistribution and use in source and binary forms, with or without
18215166Slstewart * modification, are permitted provided that the following conditions
19215166Slstewart * are met:
20215166Slstewart * 1. Redistributions of source code must retain the above copyright
21215166Slstewart *    notice, this list of conditions and the following disclaimer.
22215166Slstewart * 2. Redistributions in binary form must reproduce the above copyright
23215166Slstewart *    notice, this list of conditions and the following disclaimer in the
24215166Slstewart *    documentation and/or other materials provided with the distribution.
25215166Slstewart *
26215166Slstewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27215166Slstewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28215166Slstewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29215166Slstewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30215166Slstewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31215166Slstewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32215166Slstewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33215166Slstewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34215166Slstewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35215166Slstewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36215166Slstewart * SUCH DAMAGE.
37215166Slstewart *
38215166Slstewart * $FreeBSD$
39215166Slstewart */
40215166Slstewart
41215166Slstewart/*
42215166Slstewart * This software was first released in 2007 by James Healy and Lawrence Stewart
43220560Slstewart * whilst working on the NewTCP research project at Swinburne University of
44220560Slstewart * Technology's Centre for Advanced Internet Architectures, Melbourne,
45220560Slstewart * Australia, which was made possible in part by a grant from the Cisco
46220560Slstewart * University Research Program Fund at Community Foundation Silicon Valley.
47220560Slstewart * More details are available at:
48215166Slstewart *   http://caia.swin.edu.au/urp/newtcp/
49215166Slstewart */
50215166Slstewart
51215166Slstewart#ifndef _NETINET_CC_H_
52215166Slstewart#define _NETINET_CC_H_
53215166Slstewart
54215166Slstewart/* XXX: TCP_CA_NAME_MAX define lives in tcp.h for compat reasons. */
55215166Slstewart#include <netinet/tcp.h>
56215166Slstewart
57215166Slstewart/* Global CC vars. */
58215166Slstewartextern STAILQ_HEAD(cc_head, cc_algo) cc_list;
59215166Slstewartextern const int tcprexmtthresh;
60215166Slstewartextern struct cc_algo newreno_cc_algo;
61215166Slstewart
62215395Slstewart/* Per-netstack bits. */
63215395SlstewartVNET_DECLARE(struct cc_algo *, default_cc_ptr);
64215395Slstewart#define	V_default_cc_ptr VNET(default_cc_ptr)
65215395Slstewart
66215166Slstewart/* Define the new net.inet.tcp.cc sysctl tree. */
67215166SlstewartSYSCTL_DECL(_net_inet_tcp_cc);
68215166Slstewart
69215166Slstewart/* CC housekeeping functions. */
70215166Slstewartint	cc_register_algo(struct cc_algo *add_cc);
71215166Slstewartint	cc_deregister_algo(struct cc_algo *remove_cc);
72215166Slstewart
73215166Slstewart/*
74215166Slstewart * Wrapper around transport structs that contain same-named congestion
75215166Slstewart * control variables. Allows algos to be shared amongst multiple CC aware
76215166Slstewart * transprots.
77215166Slstewart */
78215166Slstewartstruct cc_var {
79215166Slstewart	void		*cc_data; /* Per-connection private CC algorithm data. */
80215166Slstewart	int		bytes_this_ack; /* # bytes acked by the current ACK. */
81215166Slstewart	tcp_seq		curack; /* Most recent ACK. */
82215166Slstewart	uint32_t	flags; /* Flags for cc_var (see below) */
83215166Slstewart	int		type; /* Indicates which ptr is valid in ccvc. */
84215166Slstewart	union ccv_container {
85215166Slstewart		struct tcpcb		*tcp;
86215166Slstewart		struct sctp_nets	*sctp;
87215166Slstewart	} ccvc;
88215166Slstewart};
89215166Slstewart
90215166Slstewart/* cc_var flags. */
91215166Slstewart#define	CCF_ABC_SENTAWND	0x0001	/* ABC counted cwnd worth of bytes? */
92215166Slstewart#define	CCF_CWND_LIMITED	0x0002	/* Are we currently cwnd limited? */
93215166Slstewart
94215166Slstewart/* ACK types passed to the ack_received() hook. */
95215166Slstewart#define	CC_ACK		0x0001	/* Regular in sequence ACK. */
96215166Slstewart#define	CC_DUPACK	0x0002	/* Duplicate ACK. */
97215166Slstewart#define	CC_PARTIALACK	0x0004	/* Not yet. */
98215166Slstewart#define	CC_SACK		0x0008	/* Not yet. */
99215166Slstewart
100215166Slstewart/*
101215166Slstewart * Congestion signal types passed to the cong_signal() hook. The highest order 8
102215166Slstewart * bits (0x01000000 - 0x80000000) are reserved for CC algos to declare their own
103215166Slstewart * congestion signal types.
104215166Slstewart */
105218167Slstewart#define	CC_ECN		0x00000001	/* ECN marked packet received. */
106218167Slstewart#define	CC_RTO		0x00000002	/* RTO fired. */
107218167Slstewart#define	CC_RTO_ERR	0x00000004	/* RTO fired in error. */
108218167Slstewart#define	CC_NDUPACK	0x00000008	/* Threshold of dupack's reached. */
109215166Slstewart
110218167Slstewart#define	CC_SIGPRIVMASK	0xFF000000	/* Mask to check if sig is private. */
111218167Slstewart
112215166Slstewart/*
113215166Slstewart * Structure to hold data and function pointers that together represent a
114215166Slstewart * congestion control algorithm.
115215166Slstewart */
116215166Slstewartstruct cc_algo {
117215166Slstewart	char	name[TCP_CA_NAME_MAX];
118215166Slstewart
119215166Slstewart	/* Init global module state on kldload. */
120215166Slstewart	int	(*mod_init)(void);
121215166Slstewart
122215166Slstewart	/* Cleanup global module state on kldunload. */
123215166Slstewart	int	(*mod_destroy)(void);
124215166Slstewart
125215166Slstewart	/* Init CC state for a new control block. */
126215166Slstewart	int	(*cb_init)(struct cc_var *ccv);
127215166Slstewart
128215166Slstewart	/* Cleanup CC state for a terminating control block. */
129215166Slstewart	void	(*cb_destroy)(struct cc_var *ccv);
130215166Slstewart
131215166Slstewart	/* Init variables for a newly established connection. */
132215166Slstewart	void	(*conn_init)(struct cc_var *ccv);
133215166Slstewart
134215166Slstewart	/* Called on receipt of an ack. */
135215166Slstewart	void	(*ack_received)(struct cc_var *ccv, uint16_t type);
136215166Slstewart
137215166Slstewart	/* Called on detection of a congestion signal. */
138215166Slstewart	void	(*cong_signal)(struct cc_var *ccv, uint32_t type);
139215166Slstewart
140215166Slstewart	/* Called after exiting congestion recovery. */
141215166Slstewart	void	(*post_recovery)(struct cc_var *ccv);
142215166Slstewart
143215166Slstewart	/* Called when data transfer resumes after an idle period. */
144215166Slstewart	void	(*after_idle)(struct cc_var *ccv);
145215166Slstewart
146215166Slstewart	STAILQ_ENTRY (cc_algo) entries;
147215166Slstewart};
148215166Slstewart
149215166Slstewart/* Macro to obtain the CC algo's struct ptr. */
150215166Slstewart#define	CC_ALGO(tp)	((tp)->cc_algo)
151215166Slstewart
152215166Slstewart/* Macro to obtain the CC algo's data ptr. */
153215166Slstewart#define	CC_DATA(tp)	((tp)->ccv->cc_data)
154215166Slstewart
155215166Slstewart/* Macro to obtain the system default CC algo's struct ptr. */
156215395Slstewart#define	CC_DEFAULT()	V_default_cc_ptr
157215166Slstewart
158215166Slstewartextern struct rwlock cc_list_lock;
159215166Slstewart#define	CC_LIST_LOCK_INIT()	rw_init(&cc_list_lock, "cc_list")
160215166Slstewart#define	CC_LIST_LOCK_DESTROY()	rw_destroy(&cc_list_lock)
161215166Slstewart#define	CC_LIST_RLOCK()		rw_rlock(&cc_list_lock)
162215166Slstewart#define	CC_LIST_RUNLOCK()	rw_runlock(&cc_list_lock)
163215166Slstewart#define	CC_LIST_WLOCK()		rw_wlock(&cc_list_lock)
164215166Slstewart#define	CC_LIST_WUNLOCK()	rw_wunlock(&cc_list_lock)
165215395Slstewart#define	CC_LIST_LOCK_ASSERT()	rw_assert(&cc_list_lock, RA_LOCKED)
166215166Slstewart
167215166Slstewart#endif /* _NETINET_CC_H_ */
168