xref: /freebsd/sys/net/altq/altq_codel.h (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
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 #ifdef _KERNEL
61 #include <net/altq/altq_classq.h>
62 
63 /**
64  * struct codel_params - contains codel parameters
65  *  <at> target:	target queue size (in time units)
66  *  <at> interval:	width of moving time window
67  *  <at> ecn:	is Explicit Congestion Notification enabled
68  */
69 struct codel_params {
70 	u_int64_t	target;
71 	u_int64_t	interval;
72 	int		ecn;
73 };
74 
75 /**
76  * struct codel_vars - contains codel variables
77  *  <at> count:		how many drops we've done since the last time we
78  *			entered dropping state
79  *  <at> lastcount:	count at entry to dropping state
80  *  <at> dropping:	set to true if in dropping state
81  *  <at> rec_inv_sqrt:	reciprocal value of sqrt(count) >> 1
82  *  <at> first_above_time:	when we went (or will go) continuously above
83  *				target for interval
84  *  <at> drop_next:	time to drop next packet, or when we dropped last
85  *  <at> ldelay:	sojourn time of last dequeued packet
86  */
87 struct codel_vars {
88 	u_int32_t	count;
89 	u_int32_t	lastcount;
90 	int		dropping;
91 	u_int16_t	rec_inv_sqrt;
92 	u_int64_t	first_above_time;
93 	u_int64_t	drop_next;
94 	u_int64_t	ldelay;
95 };
96 
97 struct codel {
98 	int			last_pps;
99 	struct codel_params	params;
100 	struct codel_vars	vars;
101 	struct codel_stats	stats;
102 	struct timeval		last_log;
103 	u_int32_t		drop_overlimit;
104 };
105 
106 /*
107  * codel interface state
108  */
109 struct codel_if {
110 	struct codel_if		*cif_next;	/* interface state list */
111 	struct ifaltq		*cif_ifq;	/* backpointer to ifaltq */
112 	u_int			cif_bandwidth;	/* link bandwidth in bps */
113 
114 	class_queue_t	*cl_q;		/* class queue structure */
115 	struct codel	codel;
116 
117 	/* statistics */
118 	struct codel_ifstats cl_stats;
119 };
120 
121 struct codel	*codel_alloc(int, int, int);
122 void		 codel_destroy(struct codel *);
123 int		 codel_addq(struct codel *, class_queue_t *, struct mbuf *);
124 struct mbuf	*codel_getq(struct codel *, class_queue_t *);
125 void		 codel_getstats(struct codel *, struct codel_stats *);
126 
127 #endif /* _KERNEL */
128 
129 #endif /* _ALTQ_ALTQ_CODEL_H_ */
130