| 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 | |
| 41 | #ifndef _ALTQ_ALTQ_CODEL_H_ |
| 42 | #define	_ALTQ_ALTQ_CODEL_H_ |
| 43 | |
| 44 | struct codel_stats { |
| 45 | 	u_int32_t	maxpacket; |
| 46 | 	struct pktcntr	drop_cnt; |
| 47 | 	u_int		marked_packets; |
| 48 | }; |
| 49 | |
| 50 | struct codel_ifstats { |
| 51 | 	u_int			qlength; |
| 52 | 	u_int			qlimit; |
| 53 | 	struct codel_stats	stats; |
| 54 | 	struct pktcntr	cl_xmitcnt;	/* transmitted packet counter */ |
| 55 | 	struct pktcntr	cl_dropcnt;	/* dropped packet counter */ |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * CBQ_STATS_VERSION is defined in altq.h to work around issues stemming |
| 60 | * from mixing of public-API and internal bits in each scheduler-specific |
| 61 | * header. |
| 62 | */ |
| 63 | |
| 64 | #ifdef _KERNEL |
| 65 | #include <net/altq/altq_classq.h> |
| 66 | |
| 67 | /** |
| 68 | * struct codel_params - contains codel parameters |
| 69 | * <at> target:	target queue size (in time units) |
| 70 | * <at> interval:	width of moving time window |
| 71 | * <at> ecn:	is Explicit Congestion Notification enabled |
| 72 | */ |
| 73 | struct codel_params { |
| 74 | 	u_int64_t	target; |
| 75 | 	u_int64_t	interval; |
| 76 | 	int		ecn; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * struct codel_vars - contains codel variables |
| 81 | * <at> count:		how many drops we've done since the last time we |
| 82 | *			entered dropping state |
| 83 | * <at> lastcount:	count at entry to dropping state |
| 84 | * <at> dropping:	set to true if in dropping state |
| 85 | * <at> rec_inv_sqrt:	reciprocal value of sqrt(count) >> 1 |
| 86 | * <at> first_above_time:	when we went (or will go) continuously above |
| 87 | *				target for interval |
| 88 | * <at> drop_next:	time to drop next packet, or when we dropped last |
| 89 | * <at> ldelay:	sojourn time of last dequeued packet |
| 90 | */ |
| 91 | struct codel_vars { |
| 92 | 	u_int32_t	count; |
| 93 | 	u_int32_t	lastcount; |
| 94 | 	int		dropping; |
| 95 | 	u_int16_t	rec_inv_sqrt; |
| 96 | 	u_int64_t	first_above_time; |
| 97 | 	u_int64_t	drop_next; |
| 98 | 	u_int64_t	ldelay; |
| 99 | }; |
| 100 | |
| 101 | struct codel { |
| 102 | 	int			last_pps; |
| 103 | 	struct codel_params	params; |
| 104 | 	struct codel_vars	vars; |
| 105 | 	struct codel_stats	stats; |
| 106 | 	struct timeval		last_log; |
| 107 | 	u_int32_t		drop_overlimit; |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * codel interface state |
| 112 | */ |
| 113 | struct codel_if { |
| 114 | 	struct codel_if		*cif_next;	/* interface state list */ |
| 115 | 	struct ifaltq		*cif_ifq;	/* backpointer to ifaltq */ |
| 116 | 	u_int			cif_bandwidth;	/* link bandwidth in bps */ |
| 117 | |
| 118 | 	class_queue_t	*cl_q;		/* class queue structure */ |
| 119 | 	struct codel	codel; |
| 120 | |
| 121 | 	/* statistics */ |
| 122 | 	struct codel_ifstats cl_stats; |
| 123 | }; |
| 124 | |
| 125 | struct codel	*codel_alloc(int, int, int); |
| 126 | void		 codel_destroy(struct codel *); |
| 127 | int		 codel_addq(struct codel *, class_queue_t *, struct mbuf *); |
| 128 | struct mbuf	*codel_getq(struct codel *, class_queue_t *); |
| 129 | void		 codel_getstats(struct codel *, struct codel_stats *); |
| 130 | |
| 131 | #endif /* _KERNEL */ |
| 132 | |
| 133 | #endif /* _ALTQ_ALTQ_CODEL_H_ */ |