xref: /openbmc/linux/include/net/codel_impl.h (revision b4a6aaea)
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 static void codel_params_init(struct codel_params *params)
53 {
54 	params->interval = MS2TIME(100);
55 	params->target = MS2TIME(5);
56 	params->ce_threshold = CODEL_DISABLED_THRESHOLD;
57 	params->ce_threshold_mask = 0;
58 	params->ce_threshold_selector = 0;
59 	params->ecn = false;
60 }
61 
62 static void codel_vars_init(struct codel_vars *vars)
63 {
64 	memset(vars, 0, sizeof(*vars));
65 }
66 
67 static void codel_stats_init(struct codel_stats *stats)
68 {
69 	stats->maxpacket = 0;
70 }
71 
72 /*
73  * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
74  * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
75  *
76  * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
77  */
78 static void codel_Newton_step(struct codel_vars *vars)
79 {
80 	u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
81 	u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
82 	u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
83 
84 	val >>= 2; /* avoid overflow in following multiply */
85 	val = (val * invsqrt) >> (32 - 2 + 1);
86 
87 	vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
88 }
89 
90 /*
91  * CoDel control_law is t + interval/sqrt(count)
92  * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
93  * both sqrt() and divide operation.
94  */
95 static codel_time_t codel_control_law(codel_time_t t,
96 				      codel_time_t interval,
97 				      u32 rec_inv_sqrt)
98 {
99 	return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
100 }
101 
102 static bool codel_should_drop(const struct sk_buff *skb,
103 			      void *ctx,
104 			      struct codel_vars *vars,
105 			      struct codel_params *params,
106 			      struct codel_stats *stats,
107 			      codel_skb_len_t skb_len_func,
108 			      codel_skb_time_t skb_time_func,
109 			      u32 *backlog,
110 			      codel_time_t now)
111 {
112 	bool ok_to_drop;
113 	u32 skb_len;
114 
115 	if (!skb) {
116 		vars->first_above_time = 0;
117 		return false;
118 	}
119 
120 	skb_len = skb_len_func(skb);
121 	vars->ldelay = now - skb_time_func(skb);
122 
123 	if (unlikely(skb_len > stats->maxpacket))
124 		stats->maxpacket = skb_len;
125 
126 	if (codel_time_before(vars->ldelay, params->target) ||
127 	    *backlog <= params->mtu) {
128 		/* went below - stay below for at least interval */
129 		vars->first_above_time = 0;
130 		return false;
131 	}
132 	ok_to_drop = false;
133 	if (vars->first_above_time == 0) {
134 		/* just went above from below. If we stay above
135 		 * for at least interval we'll say it's ok to drop
136 		 */
137 		vars->first_above_time = now + params->interval;
138 	} else if (codel_time_after(now, vars->first_above_time)) {
139 		ok_to_drop = true;
140 	}
141 	return ok_to_drop;
142 }
143 
144 static struct sk_buff *codel_dequeue(void *ctx,
145 				     u32 *backlog,
146 				     struct codel_params *params,
147 				     struct codel_vars *vars,
148 				     struct codel_stats *stats,
149 				     codel_skb_len_t skb_len_func,
150 				     codel_skb_time_t skb_time_func,
151 				     codel_skb_drop_t drop_func,
152 				     codel_skb_dequeue_t dequeue_func)
153 {
154 	struct sk_buff *skb = dequeue_func(vars, ctx);
155 	codel_time_t now;
156 	bool drop;
157 
158 	if (!skb) {
159 		vars->dropping = false;
160 		return skb;
161 	}
162 	now = codel_get_time();
163 	drop = codel_should_drop(skb, ctx, vars, params, stats,
164 				 skb_len_func, skb_time_func, backlog, now);
165 	if (vars->dropping) {
166 		if (!drop) {
167 			/* sojourn time below target - leave dropping state */
168 			vars->dropping = false;
169 		} else if (codel_time_after_eq(now, vars->drop_next)) {
170 			/* It's time for the next drop. Drop the current
171 			 * packet and dequeue the next. The dequeue might
172 			 * take us out of dropping state.
173 			 * If not, schedule the next drop.
174 			 * A large backlog might result in drop rates so high
175 			 * that the next drop should happen now,
176 			 * hence the while loop.
177 			 */
178 			while (vars->dropping &&
179 			       codel_time_after_eq(now, vars->drop_next)) {
180 				vars->count++; /* dont care of possible wrap
181 						* since there is no more divide
182 						*/
183 				codel_Newton_step(vars);
184 				if (params->ecn && INET_ECN_set_ce(skb)) {
185 					stats->ecn_mark++;
186 					vars->drop_next =
187 						codel_control_law(vars->drop_next,
188 								  params->interval,
189 								  vars->rec_inv_sqrt);
190 					goto end;
191 				}
192 				stats->drop_len += skb_len_func(skb);
193 				drop_func(skb, ctx);
194 				stats->drop_count++;
195 				skb = dequeue_func(vars, ctx);
196 				if (!codel_should_drop(skb, ctx,
197 						       vars, params, stats,
198 						       skb_len_func,
199 						       skb_time_func,
200 						       backlog, now)) {
201 					/* leave dropping state */
202 					vars->dropping = false;
203 				} else {
204 					/* and schedule the next drop */
205 					vars->drop_next =
206 						codel_control_law(vars->drop_next,
207 								  params->interval,
208 								  vars->rec_inv_sqrt);
209 				}
210 			}
211 		}
212 	} else if (drop) {
213 		u32 delta;
214 
215 		if (params->ecn && INET_ECN_set_ce(skb)) {
216 			stats->ecn_mark++;
217 		} else {
218 			stats->drop_len += skb_len_func(skb);
219 			drop_func(skb, ctx);
220 			stats->drop_count++;
221 
222 			skb = dequeue_func(vars, ctx);
223 			drop = codel_should_drop(skb, ctx, vars, params,
224 						 stats, skb_len_func,
225 						 skb_time_func, backlog, now);
226 		}
227 		vars->dropping = true;
228 		/* if min went above target close to when we last went below it
229 		 * assume that the drop rate that controlled the queue on the
230 		 * last cycle is a good starting point to control it now.
231 		 */
232 		delta = vars->count - vars->lastcount;
233 		if (delta > 1 &&
234 		    codel_time_before(now - vars->drop_next,
235 				      16 * params->interval)) {
236 			vars->count = delta;
237 			/* we dont care if rec_inv_sqrt approximation
238 			 * is not very precise :
239 			 * Next Newton steps will correct it quadratically.
240 			 */
241 			codel_Newton_step(vars);
242 		} else {
243 			vars->count = 1;
244 			vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
245 		}
246 		vars->lastcount = vars->count;
247 		vars->drop_next = codel_control_law(now, params->interval,
248 						    vars->rec_inv_sqrt);
249 	}
250 end:
251 	if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) {
252 		bool set_ce = true;
253 
254 		if (params->ce_threshold_mask) {
255 			int dsfield = skb_get_dsfield(skb);
256 
257 			set_ce = (dsfield >= 0 &&
258 				  (((u8)dsfield & params->ce_threshold_mask) ==
259 				   params->ce_threshold_selector));
260 		}
261 		if (set_ce && INET_ECN_set_ce(skb))
262 			stats->ce_mark++;
263 	}
264 	return skb;
265 }
266 
267 #endif
268