xref: /freebsd/sys/net/altq/altq_codel.h (revision 1f40866feb2135a4cf764a07b1b90a8a3398ff0a)
1 /*
2  * CoDel - The Controlled-Delay Active Queue Management algorithm
3  *
4  *  Copyright (C) 2013 Ermal Luçi <eri@FreeBSD.org>
5  *  Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
6  *  Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
7  *  Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
8  *  Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * Alternatively, provided that this notice is retained in full, this
23  * software may be distributed under the terms of the GNU General
24  * Public License ("GPL") version 2, in which case the provisions of the
25  * GPL apply INSTEAD OF those given above.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * $FreeBSD$
41  */
42 
43 #ifndef _ALTQ_ALTQ_CODEL_H_
44 #define	_ALTQ_ALTQ_CODEL_H_
45 
46 struct codel_stats {
47 	u_int32_t	maxpacket;
48 	struct pktcntr	drop_cnt;
49 	u_int		marked_packets;
50 };
51 
52 struct codel_ifstats {
53 	u_int			qlength;
54 	u_int			qlimit;
55 	struct codel_stats	stats;
56 	struct pktcntr	cl_xmitcnt;	/* transmitted packet counter */
57 	struct pktcntr	cl_dropcnt;	/* dropped packet counter */
58 };
59 
60 /*
61  * CBQ_STATS_VERSION is defined in altq.h to work around issues stemming
62  * from mixing of public-API and internal bits in each scheduler-specific
63  * header.
64  */
65 
66 #ifdef _KERNEL
67 #include <net/altq/altq_classq.h>
68 
69 /**
70  * struct codel_params - contains codel parameters
71  *  <at> target:	target queue size (in time units)
72  *  <at> interval:	width of moving time window
73  *  <at> ecn:	is Explicit Congestion Notification enabled
74  */
75 struct codel_params {
76 	u_int64_t	target;
77 	u_int64_t	interval;
78 	int		ecn;
79 };
80 
81 /**
82  * struct codel_vars - contains codel variables
83  *  <at> count:		how many drops we've done since the last time we
84  *			entered dropping state
85  *  <at> lastcount:	count at entry to dropping state
86  *  <at> dropping:	set to true if in dropping state
87  *  <at> rec_inv_sqrt:	reciprocal value of sqrt(count) >> 1
88  *  <at> first_above_time:	when we went (or will go) continuously above
89  *				target for interval
90  *  <at> drop_next:	time to drop next packet, or when we dropped last
91  *  <at> ldelay:	sojourn time of last dequeued packet
92  */
93 struct codel_vars {
94 	u_int32_t	count;
95 	u_int32_t	lastcount;
96 	int		dropping;
97 	u_int16_t	rec_inv_sqrt;
98 	u_int64_t	first_above_time;
99 	u_int64_t	drop_next;
100 	u_int64_t	ldelay;
101 };
102 
103 struct codel {
104 	int			last_pps;
105 	struct codel_params	params;
106 	struct codel_vars	vars;
107 	struct codel_stats	stats;
108 	struct timeval		last_log;
109 	u_int32_t		drop_overlimit;
110 };
111 
112 /*
113  * codel interface state
114  */
115 struct codel_if {
116 	struct codel_if		*cif_next;	/* interface state list */
117 	struct ifaltq		*cif_ifq;	/* backpointer to ifaltq */
118 	u_int			cif_bandwidth;	/* link bandwidth in bps */
119 
120 	class_queue_t	*cl_q;		/* class queue structure */
121 	struct codel	codel;
122 
123 	/* statistics */
124 	struct codel_ifstats cl_stats;
125 };
126 
127 struct codel	*codel_alloc(int, int, int);
128 void		 codel_destroy(struct codel *);
129 int		 codel_addq(struct codel *, class_queue_t *, struct mbuf *);
130 struct mbuf	*codel_getq(struct codel *, class_queue_t *);
131 void		 codel_getstats(struct codel *, struct codel_stats *);
132 
133 #endif /* _KERNEL */
134 
135 #endif /* _ALTQ_ALTQ_CODEL_H_ */
136