xref: /freebsd/sys/netinet/cc/cc.h (revision 4644fda3f7a455e47f45a51a2e986d6b1fd6d0f9)
1*4644fda3SGleb Smirnoff /*-
2*4644fda3SGleb Smirnoff  * Copyright (c) 2007-2008
3*4644fda3SGleb Smirnoff  * 	Swinburne University of Technology, Melbourne, Australia.
4*4644fda3SGleb Smirnoff  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5*4644fda3SGleb Smirnoff  * Copyright (c) 2010 The FreeBSD Foundation
6*4644fda3SGleb Smirnoff  * All rights reserved.
7*4644fda3SGleb Smirnoff  *
8*4644fda3SGleb Smirnoff  * This software was developed at the Centre for Advanced Internet
9*4644fda3SGleb Smirnoff  * Architectures, Swinburne University of Technology, by Lawrence Stewart and
10*4644fda3SGleb Smirnoff  * James Healy, made possible in part by a grant from the Cisco University
11*4644fda3SGleb Smirnoff  * Research Program Fund at Community Foundation Silicon Valley.
12*4644fda3SGleb Smirnoff  *
13*4644fda3SGleb Smirnoff  * Portions of this software were developed at the Centre for Advanced
14*4644fda3SGleb Smirnoff  * Internet Architectures, Swinburne University of Technology, Melbourne,
15*4644fda3SGleb Smirnoff  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16*4644fda3SGleb Smirnoff  *
17*4644fda3SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
18*4644fda3SGleb Smirnoff  * modification, are permitted provided that the following conditions
19*4644fda3SGleb Smirnoff  * are met:
20*4644fda3SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
21*4644fda3SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
22*4644fda3SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
23*4644fda3SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
24*4644fda3SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
25*4644fda3SGleb Smirnoff  *
26*4644fda3SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27*4644fda3SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*4644fda3SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*4644fda3SGleb Smirnoff  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30*4644fda3SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*4644fda3SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*4644fda3SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*4644fda3SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*4644fda3SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*4644fda3SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*4644fda3SGleb Smirnoff  * SUCH DAMAGE.
37*4644fda3SGleb Smirnoff  *
38*4644fda3SGleb Smirnoff  * $FreeBSD$
39*4644fda3SGleb Smirnoff  */
40*4644fda3SGleb Smirnoff 
41*4644fda3SGleb Smirnoff /*
42*4644fda3SGleb Smirnoff  * This software was first released in 2007 by James Healy and Lawrence Stewart
43*4644fda3SGleb Smirnoff  * whilst working on the NewTCP research project at Swinburne University of
44*4644fda3SGleb Smirnoff  * Technology's Centre for Advanced Internet Architectures, Melbourne,
45*4644fda3SGleb Smirnoff  * Australia, which was made possible in part by a grant from the Cisco
46*4644fda3SGleb Smirnoff  * University Research Program Fund at Community Foundation Silicon Valley.
47*4644fda3SGleb Smirnoff  * More details are available at:
48*4644fda3SGleb Smirnoff  *   http://caia.swin.edu.au/urp/newtcp/
49*4644fda3SGleb Smirnoff  */
50*4644fda3SGleb Smirnoff 
51*4644fda3SGleb Smirnoff #ifndef _NETINET_CC_CC_H_
52*4644fda3SGleb Smirnoff #define _NETINET_CC_CC_H_
53*4644fda3SGleb Smirnoff 
54*4644fda3SGleb Smirnoff #if !defined(_KERNEL)
55*4644fda3SGleb Smirnoff #error "no user-servicable parts inside"
56*4644fda3SGleb Smirnoff #endif
57*4644fda3SGleb Smirnoff 
58*4644fda3SGleb Smirnoff /* Global CC vars. */
59*4644fda3SGleb Smirnoff extern STAILQ_HEAD(cc_head, cc_algo) cc_list;
60*4644fda3SGleb Smirnoff extern const int tcprexmtthresh;
61*4644fda3SGleb Smirnoff extern struct cc_algo newreno_cc_algo;
62*4644fda3SGleb Smirnoff 
63*4644fda3SGleb Smirnoff /* Per-netstack bits. */
64*4644fda3SGleb Smirnoff VNET_DECLARE(struct cc_algo *, default_cc_ptr);
65*4644fda3SGleb Smirnoff #define	V_default_cc_ptr VNET(default_cc_ptr)
66*4644fda3SGleb Smirnoff 
67*4644fda3SGleb Smirnoff /* Define the new net.inet.tcp.cc sysctl tree. */
68*4644fda3SGleb Smirnoff SYSCTL_DECL(_net_inet_tcp_cc);
69*4644fda3SGleb Smirnoff 
70*4644fda3SGleb Smirnoff /* CC housekeeping functions. */
71*4644fda3SGleb Smirnoff int	cc_register_algo(struct cc_algo *add_cc);
72*4644fda3SGleb Smirnoff int	cc_deregister_algo(struct cc_algo *remove_cc);
73*4644fda3SGleb Smirnoff 
74*4644fda3SGleb Smirnoff /*
75*4644fda3SGleb Smirnoff  * Wrapper around transport structs that contain same-named congestion
76*4644fda3SGleb Smirnoff  * control variables. Allows algos to be shared amongst multiple CC aware
77*4644fda3SGleb Smirnoff  * transprots.
78*4644fda3SGleb Smirnoff  */
79*4644fda3SGleb Smirnoff struct cc_var {
80*4644fda3SGleb Smirnoff 	void		*cc_data; /* Per-connection private CC algorithm data. */
81*4644fda3SGleb Smirnoff 	int		bytes_this_ack; /* # bytes acked by the current ACK. */
82*4644fda3SGleb Smirnoff 	tcp_seq		curack; /* Most recent ACK. */
83*4644fda3SGleb Smirnoff 	uint32_t	flags; /* Flags for cc_var (see below) */
84*4644fda3SGleb Smirnoff 	int		type; /* Indicates which ptr is valid in ccvc. */
85*4644fda3SGleb Smirnoff 	union ccv_container {
86*4644fda3SGleb Smirnoff 		struct tcpcb		*tcp;
87*4644fda3SGleb Smirnoff 		struct sctp_nets	*sctp;
88*4644fda3SGleb Smirnoff 	} ccvc;
89*4644fda3SGleb Smirnoff };
90*4644fda3SGleb Smirnoff 
91*4644fda3SGleb Smirnoff /* cc_var flags. */
92*4644fda3SGleb Smirnoff #define	CCF_ABC_SENTAWND	0x0001	/* ABC counted cwnd worth of bytes? */
93*4644fda3SGleb Smirnoff #define	CCF_CWND_LIMITED	0x0002	/* Are we currently cwnd limited? */
94*4644fda3SGleb Smirnoff #define	CCF_DELACK		0x0004	/* Is this ack delayed? */
95*4644fda3SGleb Smirnoff #define	CCF_ACKNOW		0x0008	/* Will this ack be sent now? */
96*4644fda3SGleb Smirnoff #define	CCF_IPHDR_CE		0x0010	/* Does this packet set CE bit? */
97*4644fda3SGleb Smirnoff #define	CCF_TCPHDR_CWR		0x0020	/* Does this packet set CWR bit? */
98*4644fda3SGleb Smirnoff 
99*4644fda3SGleb Smirnoff /* ACK types passed to the ack_received() hook. */
100*4644fda3SGleb Smirnoff #define	CC_ACK		0x0001	/* Regular in sequence ACK. */
101*4644fda3SGleb Smirnoff #define	CC_DUPACK	0x0002	/* Duplicate ACK. */
102*4644fda3SGleb Smirnoff #define	CC_PARTIALACK	0x0004	/* Not yet. */
103*4644fda3SGleb Smirnoff #define	CC_SACK		0x0008	/* Not yet. */
104*4644fda3SGleb Smirnoff 
105*4644fda3SGleb Smirnoff /*
106*4644fda3SGleb Smirnoff  * Congestion signal types passed to the cong_signal() hook. The highest order 8
107*4644fda3SGleb Smirnoff  * bits (0x01000000 - 0x80000000) are reserved for CC algos to declare their own
108*4644fda3SGleb Smirnoff  * congestion signal types.
109*4644fda3SGleb Smirnoff  */
110*4644fda3SGleb Smirnoff #define	CC_ECN		0x00000001	/* ECN marked packet received. */
111*4644fda3SGleb Smirnoff #define	CC_RTO		0x00000002	/* RTO fired. */
112*4644fda3SGleb Smirnoff #define	CC_RTO_ERR	0x00000004	/* RTO fired in error. */
113*4644fda3SGleb Smirnoff #define	CC_NDUPACK	0x00000008	/* Threshold of dupack's reached. */
114*4644fda3SGleb Smirnoff 
115*4644fda3SGleb Smirnoff #define	CC_SIGPRIVMASK	0xFF000000	/* Mask to check if sig is private. */
116*4644fda3SGleb Smirnoff 
117*4644fda3SGleb Smirnoff /*
118*4644fda3SGleb Smirnoff  * Structure to hold data and function pointers that together represent a
119*4644fda3SGleb Smirnoff  * congestion control algorithm.
120*4644fda3SGleb Smirnoff  */
121*4644fda3SGleb Smirnoff struct cc_algo {
122*4644fda3SGleb Smirnoff 	char	name[TCP_CA_NAME_MAX];
123*4644fda3SGleb Smirnoff 
124*4644fda3SGleb Smirnoff 	/* Init global module state on kldload. */
125*4644fda3SGleb Smirnoff 	int	(*mod_init)(void);
126*4644fda3SGleb Smirnoff 
127*4644fda3SGleb Smirnoff 	/* Cleanup global module state on kldunload. */
128*4644fda3SGleb Smirnoff 	int	(*mod_destroy)(void);
129*4644fda3SGleb Smirnoff 
130*4644fda3SGleb Smirnoff 	/* Init CC state for a new control block. */
131*4644fda3SGleb Smirnoff 	int	(*cb_init)(struct cc_var *ccv);
132*4644fda3SGleb Smirnoff 
133*4644fda3SGleb Smirnoff 	/* Cleanup CC state for a terminating control block. */
134*4644fda3SGleb Smirnoff 	void	(*cb_destroy)(struct cc_var *ccv);
135*4644fda3SGleb Smirnoff 
136*4644fda3SGleb Smirnoff 	/* Init variables for a newly established connection. */
137*4644fda3SGleb Smirnoff 	void	(*conn_init)(struct cc_var *ccv);
138*4644fda3SGleb Smirnoff 
139*4644fda3SGleb Smirnoff 	/* Called on receipt of an ack. */
140*4644fda3SGleb Smirnoff 	void	(*ack_received)(struct cc_var *ccv, uint16_t type);
141*4644fda3SGleb Smirnoff 
142*4644fda3SGleb Smirnoff 	/* Called on detection of a congestion signal. */
143*4644fda3SGleb Smirnoff 	void	(*cong_signal)(struct cc_var *ccv, uint32_t type);
144*4644fda3SGleb Smirnoff 
145*4644fda3SGleb Smirnoff 	/* Called after exiting congestion recovery. */
146*4644fda3SGleb Smirnoff 	void	(*post_recovery)(struct cc_var *ccv);
147*4644fda3SGleb Smirnoff 
148*4644fda3SGleb Smirnoff 	/* Called when data transfer resumes after an idle period. */
149*4644fda3SGleb Smirnoff 	void	(*after_idle)(struct cc_var *ccv);
150*4644fda3SGleb Smirnoff 
151*4644fda3SGleb Smirnoff 	/* Called for an additional ECN processing apart from RFC3168. */
152*4644fda3SGleb Smirnoff 	void	(*ecnpkt_handler)(struct cc_var *ccv);
153*4644fda3SGleb Smirnoff 
154*4644fda3SGleb Smirnoff 	/* Called for {get|set}sockopt() on a TCP socket with TCP_CCALGOOPT. */
155*4644fda3SGleb Smirnoff 	int     (*ctl_output)(struct cc_var *, struct sockopt *, void *);
156*4644fda3SGleb Smirnoff 
157*4644fda3SGleb Smirnoff 	STAILQ_ENTRY (cc_algo) entries;
158*4644fda3SGleb Smirnoff };
159*4644fda3SGleb Smirnoff 
160*4644fda3SGleb Smirnoff /* Macro to obtain the CC algo's struct ptr. */
161*4644fda3SGleb Smirnoff #define	CC_ALGO(tp)	((tp)->cc_algo)
162*4644fda3SGleb Smirnoff 
163*4644fda3SGleb Smirnoff /* Macro to obtain the CC algo's data ptr. */
164*4644fda3SGleb Smirnoff #define	CC_DATA(tp)	((tp)->ccv->cc_data)
165*4644fda3SGleb Smirnoff 
166*4644fda3SGleb Smirnoff /* Macro to obtain the system default CC algo's struct ptr. */
167*4644fda3SGleb Smirnoff #define	CC_DEFAULT()	V_default_cc_ptr
168*4644fda3SGleb Smirnoff 
169*4644fda3SGleb Smirnoff extern struct rwlock cc_list_lock;
170*4644fda3SGleb Smirnoff #define	CC_LIST_LOCK_INIT()	rw_init(&cc_list_lock, "cc_list")
171*4644fda3SGleb Smirnoff #define	CC_LIST_LOCK_DESTROY()	rw_destroy(&cc_list_lock)
172*4644fda3SGleb Smirnoff #define	CC_LIST_RLOCK()		rw_rlock(&cc_list_lock)
173*4644fda3SGleb Smirnoff #define	CC_LIST_RUNLOCK()	rw_runlock(&cc_list_lock)
174*4644fda3SGleb Smirnoff #define	CC_LIST_WLOCK()		rw_wlock(&cc_list_lock)
175*4644fda3SGleb Smirnoff #define	CC_LIST_WUNLOCK()	rw_wunlock(&cc_list_lock)
176*4644fda3SGleb Smirnoff #define	CC_LIST_LOCK_ASSERT()	rw_assert(&cc_list_lock, RA_LOCKED)
177*4644fda3SGleb Smirnoff 
178*4644fda3SGleb Smirnoff #endif /* _NETINET_CC_CC_H_ */
179