Linux Audio

Check our new training course

Loading...
v6.13.7
  1#ifndef __NET_SCHED_CODEL_H
  2#define __NET_SCHED_CODEL_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#include <linux/types.h>
 45#include <linux/ktime.h>
 46#include <linux/skbuff.h>
 
 
 47
 48/* Controlling Queue Delay (CoDel) algorithm
 49 * =========================================
 50 * Source : Kathleen Nichols and Van Jacobson
 51 * http://queue.acm.org/detail.cfm?id=2209336
 52 *
 53 * Implemented on linux by Dave Taht and Eric Dumazet
 54 */
 55
 56
 57/* CoDel uses a 1024 nsec clock, encoded in u32
 58 * This gives a range of 2199 seconds, because of signed compares
 59 */
 60typedef u32 codel_time_t;
 61typedef s32 codel_tdiff_t;
 62#define CODEL_SHIFT 10
 63#define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
 64
 65static inline codel_time_t codel_get_time(void)
 66{
 67	u64 ns = ktime_get_ns();
 68
 69	return ns >> CODEL_SHIFT;
 70}
 71
 72/* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
 73 *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
 74 * codel_time_after(a,b) returns true if the time a is after time b.
 75 */
 76#define codel_time_after(a, b)						\
 77	(typecheck(codel_time_t, a) &&					\
 78	 typecheck(codel_time_t, b) &&					\
 79	 ((s32)((a) - (b)) > 0))
 80#define codel_time_before(a, b) 	codel_time_after(b, a)
 81
 82#define codel_time_after_eq(a, b)					\
 83	(typecheck(codel_time_t, a) &&					\
 84	 typecheck(codel_time_t, b) &&					\
 85	 ((s32)((a) - (b)) >= 0))
 86#define codel_time_before_eq(a, b)	codel_time_after_eq(b, a)
 87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 88static inline u32 codel_time_to_us(codel_time_t val)
 89{
 90	u64 valns = ((u64)val << CODEL_SHIFT);
 91
 92	do_div(valns, NSEC_PER_USEC);
 93	return (u32)valns;
 94}
 95
 96/**
 97 * struct codel_params - contains codel parameters
 98 * @target:	target queue size (in time units)
 99 * @ce_threshold:  threshold for marking packets with ECN CE
100 * @interval:	width of moving time window
101 * @mtu:	device mtu, or minimal queue backlog in bytes.
102 * @ecn:	is Explicit Congestion Notification enabled
103 * @ce_threshold_selector: apply ce_threshold to packets matching this value
104 *                         in the diffserv/ECN byte of the IP header
105 * @ce_threshold_mask: mask to apply to ce_threshold_selector comparison
106 */
107struct codel_params {
108	codel_time_t	target;
109	codel_time_t	ce_threshold;
110	codel_time_t	interval;
111	u32		mtu;
112	bool		ecn;
113	u8		ce_threshold_selector;
114	u8		ce_threshold_mask;
115};
116
117/**
118 * struct codel_vars - contains codel variables
119 * @count:		how many drops we've done since the last time we
120 *			entered dropping state
121 * @lastcount:		count at entry to dropping state
122 * @dropping:		set to true if in dropping state
123 * @rec_inv_sqrt:	reciprocal value of sqrt(count) >> 1
124 * @first_above_time:	when we went (or will go) continuously above target
125 *			for interval
126 * @drop_next:		time to drop next packet, or when we dropped last
127 * @ldelay:		sojourn time of last dequeued packet
128 */
129struct codel_vars {
130	u32		count;
131	u32		lastcount;
132	bool		dropping;
133	u16		rec_inv_sqrt;
134	codel_time_t	first_above_time;
135	codel_time_t	drop_next;
136	codel_time_t	ldelay;
137};
138
139#define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
140/* needed shift to get a Q0.32 number from rec_inv_sqrt */
141#define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
142
143/**
144 * struct codel_stats - contains codel shared variables and stats
145 * @maxpacket:	largest packet we've seen so far
146 * @drop_count:	temp count of dropped packets in dequeue()
147 * @drop_len:	bytes of dropped packets in dequeue()
148 * @ecn_mark:	number of packets we ECN marked instead of dropping
149 * @ce_mark:	number of packets CE marked because sojourn time was above ce_threshold
150 */
151struct codel_stats {
152	u32		maxpacket;
153	u32		drop_count;
154	u32		drop_len;
155	u32		ecn_mark;
156	u32		ce_mark;
157};
158
159#define CODEL_DISABLED_THRESHOLD INT_MAX
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
161typedef u32 (*codel_skb_len_t)(const struct sk_buff *skb);
162typedef codel_time_t (*codel_skb_time_t)(const struct sk_buff *skb);
163typedef void (*codel_skb_drop_t)(struct sk_buff *skb, void *ctx);
164typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
165						void *ctx);
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167#endif
v3.15
  1#ifndef __NET_SCHED_CODEL_H
  2#define __NET_SCHED_CODEL_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 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#include <linux/types.h>
 45#include <linux/ktime.h>
 46#include <linux/skbuff.h>
 47#include <net/pkt_sched.h>
 48#include <net/inet_ecn.h>
 49
 50/* Controlling Queue Delay (CoDel) algorithm
 51 * =========================================
 52 * Source : Kathleen Nichols and Van Jacobson
 53 * http://queue.acm.org/detail.cfm?id=2209336
 54 *
 55 * Implemented on linux by Dave Taht and Eric Dumazet
 56 */
 57
 58
 59/* CoDel uses a 1024 nsec clock, encoded in u32
 60 * This gives a range of 2199 seconds, because of signed compares
 61 */
 62typedef u32 codel_time_t;
 63typedef s32 codel_tdiff_t;
 64#define CODEL_SHIFT 10
 65#define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
 66
 67static inline codel_time_t codel_get_time(void)
 68{
 69	u64 ns = ktime_to_ns(ktime_get());
 70
 71	return ns >> CODEL_SHIFT;
 72}
 73
 74/* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
 75 *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
 76 * codel_time_after(a,b) returns true if the time a is after time b.
 77 */
 78#define codel_time_after(a, b)						\
 79	(typecheck(codel_time_t, a) &&					\
 80	 typecheck(codel_time_t, b) &&					\
 81	 ((s32)((a) - (b)) > 0))
 82#define codel_time_before(a, b) 	codel_time_after(b, a)
 83
 84#define codel_time_after_eq(a, b)					\
 85	(typecheck(codel_time_t, a) &&					\
 86	 typecheck(codel_time_t, b) &&					\
 87	 ((s32)((a) - (b)) >= 0))
 88#define codel_time_before_eq(a, b)	codel_time_after_eq(b, a)
 89
 90/* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */
 91struct codel_skb_cb {
 92	codel_time_t enqueue_time;
 93};
 94
 95static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb)
 96{
 97	qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb));
 98	return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data;
 99}
100
101static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb)
102{
103	return get_codel_cb(skb)->enqueue_time;
104}
105
106static void codel_set_enqueue_time(struct sk_buff *skb)
107{
108	get_codel_cb(skb)->enqueue_time = codel_get_time();
109}
110
111static inline u32 codel_time_to_us(codel_time_t val)
112{
113	u64 valns = ((u64)val << CODEL_SHIFT);
114
115	do_div(valns, NSEC_PER_USEC);
116	return (u32)valns;
117}
118
119/**
120 * struct codel_params - contains codel parameters
121 * @target:	target queue size (in time units)
 
122 * @interval:	width of moving time window
 
123 * @ecn:	is Explicit Congestion Notification enabled
 
 
 
124 */
125struct codel_params {
126	codel_time_t	target;
 
127	codel_time_t	interval;
 
128	bool		ecn;
 
 
129};
130
131/**
132 * struct codel_vars - contains codel variables
133 * @count:		how many drops we've done since the last time we
134 *			entered dropping state
135 * @lastcount:		count at entry to dropping state
136 * @dropping:		set to true if in dropping state
137 * @rec_inv_sqrt:	reciprocal value of sqrt(count) >> 1
138 * @first_above_time:	when we went (or will go) continuously above target
139 *			for interval
140 * @drop_next:		time to drop next packet, or when we dropped last
141 * @ldelay:		sojourn time of last dequeued packet
142 */
143struct codel_vars {
144	u32		count;
145	u32		lastcount;
146	bool		dropping;
147	u16		rec_inv_sqrt;
148	codel_time_t	first_above_time;
149	codel_time_t	drop_next;
150	codel_time_t	ldelay;
151};
152
153#define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
154/* needed shift to get a Q0.32 number from rec_inv_sqrt */
155#define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
156
157/**
158 * struct codel_stats - contains codel shared variables and stats
159 * @maxpacket:	largest packet we've seen so far
160 * @drop_count:	temp count of dropped packets in dequeue()
161 * ecn_mark:	number of packets we ECN marked instead of dropping
 
 
162 */
163struct codel_stats {
164	u32		maxpacket;
165	u32		drop_count;
 
166	u32		ecn_mark;
 
167};
168
169static void codel_params_init(struct codel_params *params)
170{
171	params->interval = MS2TIME(100);
172	params->target = MS2TIME(5);
173	params->ecn = false;
174}
175
176static void codel_vars_init(struct codel_vars *vars)
177{
178	memset(vars, 0, sizeof(*vars));
179}
180
181static void codel_stats_init(struct codel_stats *stats)
182{
183	stats->maxpacket = 256;
184}
185
186/*
187 * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
188 * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
189 *
190 * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
191 */
192static void codel_Newton_step(struct codel_vars *vars)
193{
194	u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
195	u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
196	u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
197
198	val >>= 2; /* avoid overflow in following multiply */
199	val = (val * invsqrt) >> (32 - 2 + 1);
200
201	vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
202}
203
204/*
205 * CoDel control_law is t + interval/sqrt(count)
206 * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
207 * both sqrt() and divide operation.
208 */
209static codel_time_t codel_control_law(codel_time_t t,
210				      codel_time_t interval,
211				      u32 rec_inv_sqrt)
212{
213	return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
214}
215
216static bool codel_should_drop(const struct sk_buff *skb,
217			      struct Qdisc *sch,
218			      struct codel_vars *vars,
219			      struct codel_params *params,
220			      struct codel_stats *stats,
221			      codel_time_t now)
222{
223	bool ok_to_drop;
224
225	if (!skb) {
226		vars->first_above_time = 0;
227		return false;
228	}
229
230	vars->ldelay = now - codel_get_enqueue_time(skb);
231	sch->qstats.backlog -= qdisc_pkt_len(skb);
232
233	if (unlikely(qdisc_pkt_len(skb) > stats->maxpacket))
234		stats->maxpacket = qdisc_pkt_len(skb);
235
236	if (codel_time_before(vars->ldelay, params->target) ||
237	    sch->qstats.backlog <= stats->maxpacket) {
238		/* went below - stay below for at least interval */
239		vars->first_above_time = 0;
240		return false;
241	}
242	ok_to_drop = false;
243	if (vars->first_above_time == 0) {
244		/* just went above from below. If we stay above
245		 * for at least interval we'll say it's ok to drop
246		 */
247		vars->first_above_time = now + params->interval;
248	} else if (codel_time_after(now, vars->first_above_time)) {
249		ok_to_drop = true;
250	}
251	return ok_to_drop;
252}
253
 
 
 
254typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
255						struct Qdisc *sch);
256
257static struct sk_buff *codel_dequeue(struct Qdisc *sch,
258				     struct codel_params *params,
259				     struct codel_vars *vars,
260				     struct codel_stats *stats,
261				     codel_skb_dequeue_t dequeue_func)
262{
263	struct sk_buff *skb = dequeue_func(vars, sch);
264	codel_time_t now;
265	bool drop;
266
267	if (!skb) {
268		vars->dropping = false;
269		return skb;
270	}
271	now = codel_get_time();
272	drop = codel_should_drop(skb, sch, vars, params, stats, now);
273	if (vars->dropping) {
274		if (!drop) {
275			/* sojourn time below target - leave dropping state */
276			vars->dropping = false;
277		} else if (codel_time_after_eq(now, vars->drop_next)) {
278			/* It's time for the next drop. Drop the current
279			 * packet and dequeue the next. The dequeue might
280			 * take us out of dropping state.
281			 * If not, schedule the next drop.
282			 * A large backlog might result in drop rates so high
283			 * that the next drop should happen now,
284			 * hence the while loop.
285			 */
286			while (vars->dropping &&
287			       codel_time_after_eq(now, vars->drop_next)) {
288				vars->count++; /* dont care of possible wrap
289						* since there is no more divide
290						*/
291				codel_Newton_step(vars);
292				if (params->ecn && INET_ECN_set_ce(skb)) {
293					stats->ecn_mark++;
294					vars->drop_next =
295						codel_control_law(vars->drop_next,
296								  params->interval,
297								  vars->rec_inv_sqrt);
298					goto end;
299				}
300				qdisc_drop(skb, sch);
301				stats->drop_count++;
302				skb = dequeue_func(vars, sch);
303				if (!codel_should_drop(skb, sch,
304						       vars, params, stats, now)) {
305					/* leave dropping state */
306					vars->dropping = false;
307				} else {
308					/* and schedule the next drop */
309					vars->drop_next =
310						codel_control_law(vars->drop_next,
311								  params->interval,
312								  vars->rec_inv_sqrt);
313				}
314			}
315		}
316	} else if (drop) {
317		u32 delta;
318
319		if (params->ecn && INET_ECN_set_ce(skb)) {
320			stats->ecn_mark++;
321		} else {
322			qdisc_drop(skb, sch);
323			stats->drop_count++;
324
325			skb = dequeue_func(vars, sch);
326			drop = codel_should_drop(skb, sch, vars, params,
327						 stats, now);
328		}
329		vars->dropping = true;
330		/* if min went above target close to when we last went below it
331		 * assume that the drop rate that controlled the queue on the
332		 * last cycle is a good starting point to control it now.
333		 */
334		delta = vars->count - vars->lastcount;
335		if (delta > 1 &&
336		    codel_time_before(now - vars->drop_next,
337				      16 * params->interval)) {
338			vars->count = delta;
339			/* we dont care if rec_inv_sqrt approximation
340			 * is not very precise :
341			 * Next Newton steps will correct it quadratically.
342			 */
343			codel_Newton_step(vars);
344		} else {
345			vars->count = 1;
346			vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
347		}
348		vars->lastcount = vars->count;
349		vars->drop_next = codel_control_law(now, params->interval,
350						    vars->rec_inv_sqrt);
351	}
352end:
353	return skb;
354}
355#endif