xref: /linux/include/net/codel_impl.h (revision 815980fe6dbb01ad4007e8b260a45617f598b76d)
1 #ifndef __NET_SCHED_CODEL_IMPL_H
2 #define __NET_SCHED_CODEL_IMPL_H
3 
4 /*
5  * Codel - The Controlled-Delay Active Queue Management algorithm
6  *
7  *  Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
8  *  Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
9  *  Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
10  *  Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * Alternatively, provided that this notice is retained in full, this
25  * software may be distributed under the terms of the GNU General
26  * Public License ("GPL") version 2, in which case the provisions of the
27  * GPL apply INSTEAD OF those given above.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40  * DAMAGE.
41  *
42  */
43 
44 /* Controlling Queue Delay (CoDel) algorithm
45  * =========================================
46  * Source : Kathleen Nichols and Van Jacobson
47  * http://queue.acm.org/detail.cfm?id=2209336
48  *
49  * Implemented on linux by Dave Taht and Eric Dumazet
50  */
51 
52 #include <net/inet_ecn.h>
53 
54 static void codel_params_init(struct codel_params *params)
55 {
56 	params->interval = MS2TIME(100);
57 	params->target = MS2TIME(5);
58 	params->ce_threshold = CODEL_DISABLED_THRESHOLD;
59 	params->ce_threshold_mask = 0;
60 	params->ce_threshold_selector = 0;
61 	params->ecn = false;
62 }
63 
64 static void codel_vars_init(struct codel_vars *vars)
65 {
66 	memset(vars, 0, sizeof(*vars));
67 }
68 
69 static void codel_stats_init(struct codel_stats *stats)
70 {
71 	stats->maxpacket = 0;
72 }
73 
74 /*
75  * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
76  * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
77  *
78  * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
79  */
80 static void codel_Newton_step(struct codel_vars *vars)
81 {
82 	u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
83 	u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
84 	u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
85 
86 	val >>= 2; /* avoid overflow in following multiply */
87 	val = (val * invsqrt) >> (32 - 2 + 1);
88 
89 	vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
90 }
91 
92 /*
93  * CoDel control_law is t + interval/sqrt(count)
94  * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
95  * both sqrt() and divide operation.
96  */
97 static codel_time_t codel_control_law(codel_time_t t,
98 				      codel_time_t interval,
99 				      u32 rec_inv_sqrt)
100 {
101 	return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
102 }
103 
104 static bool codel_should_drop(const struct sk_buff *skb,
105 			      void *ctx,
106 			      struct codel_vars *vars,
107 			      struct codel_params *params,
108 			      struct codel_stats *stats,
109 			      codel_skb_len_t skb_len_func,
110 			      codel_skb_time_t skb_time_func,
111 			      u32 *backlog,
112 			      codel_time_t now)
113 {
114 	bool ok_to_drop;
115 	u32 skb_len;
116 
117 	if (!skb) {
118 		vars->first_above_time = 0;
119 		return false;
120 	}
121 
122 	skb_len = skb_len_func(skb);
123 	vars->ldelay = now - skb_time_func(skb);
124 
125 	if (unlikely(skb_len > stats->maxpacket))
126 		stats->maxpacket = skb_len;
127 
128 	if (codel_time_before(vars->ldelay, params->target) ||
129 	    *backlog <= params->mtu) {
130 		/* went below - stay below for at least interval */
131 		vars->first_above_time = 0;
132 		return false;
133 	}
134 	ok_to_drop = false;
135 	if (vars->first_above_time == 0) {
136 		/* just went above from below. If we stay above
137 		 * for at least interval we'll say it's ok to drop
138 		 */
139 		vars->first_above_time = now + params->interval;
140 	} else if (codel_time_after(now, vars->first_above_time)) {
141 		ok_to_drop = true;
142 	}
143 	return ok_to_drop;
144 }
145 
146 static struct sk_buff *codel_dequeue(void *ctx,
147 				     u32 *backlog,
148 				     struct codel_params *params,
149 				     struct codel_vars *vars,
150 				     struct codel_stats *stats,
151 				     codel_skb_len_t skb_len_func,
152 				     codel_skb_time_t skb_time_func,
153 				     codel_skb_drop_t drop_func,
154 				     codel_skb_dequeue_t dequeue_func)
155 {
156 	struct sk_buff *skb = dequeue_func(vars, ctx);
157 	codel_time_t now;
158 	bool drop;
159 
160 	if (!skb) {
161 		vars->first_above_time = 0;
162 		vars->dropping = false;
163 		return skb;
164 	}
165 	now = codel_get_time();
166 	drop = codel_should_drop(skb, ctx, vars, params, stats,
167 				 skb_len_func, skb_time_func, backlog, now);
168 	if (vars->dropping) {
169 		if (!drop) {
170 			/* sojourn time below target - leave dropping state */
171 			vars->dropping = false;
172 		} else if (codel_time_after_eq(now, vars->drop_next)) {
173 			/* It's time for the next drop. Drop the current
174 			 * packet and dequeue the next. The dequeue might
175 			 * take us out of dropping state.
176 			 * If not, schedule the next drop.
177 			 * A large backlog might result in drop rates so high
178 			 * that the next drop should happen now,
179 			 * hence the while loop.
180 			 */
181 			while (vars->dropping &&
182 			       codel_time_after_eq(now, vars->drop_next)) {
183 				vars->count++; /* dont care of possible wrap
184 						* since there is no more divide
185 						*/
186 				codel_Newton_step(vars);
187 				if (params->ecn && INET_ECN_set_ce(skb)) {
188 					stats->ecn_mark++;
189 					vars->drop_next =
190 						codel_control_law(vars->drop_next,
191 								  params->interval,
192 								  vars->rec_inv_sqrt);
193 					goto end;
194 				}
195 				stats->drop_len += skb_len_func(skb);
196 				drop_func(skb, ctx);
197 				stats->drop_count++;
198 				skb = dequeue_func(vars, ctx);
199 				if (!codel_should_drop(skb, ctx,
200 						       vars, params, stats,
201 						       skb_len_func,
202 						       skb_time_func,
203 						       backlog, now)) {
204 					/* leave dropping state */
205 					vars->dropping = false;
206 				} else {
207 					/* and schedule the next drop */
208 					vars->drop_next =
209 						codel_control_law(vars->drop_next,
210 								  params->interval,
211 								  vars->rec_inv_sqrt);
212 				}
213 			}
214 		}
215 	} else if (drop) {
216 		u32 delta;
217 
218 		if (params->ecn && INET_ECN_set_ce(skb)) {
219 			stats->ecn_mark++;
220 		} else {
221 			stats->drop_len += skb_len_func(skb);
222 			drop_func(skb, ctx);
223 			stats->drop_count++;
224 
225 			skb = dequeue_func(vars, ctx);
226 			drop = codel_should_drop(skb, ctx, vars, params,
227 						 stats, skb_len_func,
228 						 skb_time_func, backlog, now);
229 		}
230 		vars->dropping = true;
231 		/* if min went above target close to when we last went below it
232 		 * assume that the drop rate that controlled the queue on the
233 		 * last cycle is a good starting point to control it now.
234 		 */
235 		delta = vars->count - vars->lastcount;
236 		if (delta > 1 &&
237 		    codel_time_before(now - vars->drop_next,
238 				      16 * params->interval)) {
239 			vars->count = delta;
240 			/* we dont care if rec_inv_sqrt approximation
241 			 * is not very precise :
242 			 * Next Newton steps will correct it quadratically.
243 			 */
244 			codel_Newton_step(vars);
245 		} else {
246 			vars->count = 1;
247 			vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
248 		}
249 		vars->lastcount = vars->count;
250 		vars->drop_next = codel_control_law(now, params->interval,
251 						    vars->rec_inv_sqrt);
252 	}
253 end:
254 	if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) {
255 		bool set_ce = true;
256 
257 		if (params->ce_threshold_mask) {
258 			int dsfield = skb_get_dsfield(skb);
259 
260 			set_ce = (dsfield >= 0 &&
261 				  (((u8)dsfield & params->ce_threshold_mask) ==
262 				   params->ce_threshold_selector));
263 		}
264 		if (set_ce && INET_ECN_set_ce(skb))
265 			stats->ce_mark++;
266 	}
267 	return skb;
268 }
269 
270 #endif
271