[Codel] [PATCH v10] codel: Controlled Delay AQM

Eric Dumazet eric.dumazet at gmail.com
Mon May 7 09:57:23 EDT 2012


On Sun, 2012-05-06 at 22:35 -0700, Dave Täht wrote:
> This version (9) adds support for various forms of decrease in s3,
> in the form of the module parameters gentle and decrease_method.
> 
> It defaults to the algorithm as described in the original presentation.
> 
> v1: Original implementation - Dave Taht
> v2: Working code - Corrections for ktime - Dave Taht
> v3: 32 bit support and port to net-next - Eric Dumazet
> v4: 16 bit precision for inv sqrt and cache - Dave Taht
> v5: Kernel cleanup and full precision - Eric Dumazet
> v6: Dump Stats support added - Eric Dumazet
> v7: Complete rewrite for u32 values - Eric Dumazet
> v8: Stats and timing added, 64 bit prescale improved - Eric Dumazet
> v9: debated functionality moved to isolated routine - Dave Taht

Please find v10 :

-  ECN support.

-  refactorize code to let codel be plugged in SFQ (or other qdiscs)
with minimal memory costs (separate data into three subsets : params,
vars, stats). Each Qdisc provides its own 'dequeue packet from raw
queue', since it might be specific (in SFQ we have of course one
separate queue per flow, and it has to update 2 backlogs, not only one)
  - add codel_ prefixes to cleanup code.
  - Qdisc wanting to plug codel must include the room for skb timestamp
in their private skb->cb[] (if they use/have one)

   I plan to add codel to SFQ in a very near future (so that you can
optionally select RED or Codel for SFQ flows)

About the choice of q->count decrease, I think its better to wait for
Kathleen & Van input on this subject.

 include/linux/pkt_sched.h |   27 +++
 include/net/codel.h       |  275 ++++++++++++++++++++++++++++++++++++
 net/sched/Kconfig         |   11 +
 net/sched/Makefile        |    1 
 net/sched/sch_codel.c     |  252 ++++++++++++++++++++++++++++++++
 5 files changed, 566 insertions(+)

diff --git a/include/linux/pkt_sched.h b/include/linux/pkt_sched.h
index ffe975c..45a1abe 100644
--- a/include/linux/pkt_sched.h
+++ b/include/linux/pkt_sched.h
@@ -655,4 +655,31 @@ struct tc_qfq_stats {
 	__u32 lmax;
 };
 
+/* CODEL */
+
+enum {
+	TCA_CODEL_UNSPEC,
+	TCA_CODEL_TARGET,
+	TCA_CODEL_LIMIT,
+	TCA_CODEL_MINBYTES,
+	TCA_CODEL_INTERVAL,
+	TCA_CODEL_ECN,
+	__TCA_CODEL_MAX
+};
+
+#define TCA_CODEL_MAX	(__TCA_CODEL_MAX - 1)
+
+struct tc_codel_xstats {
+	__u32		count;
+	__u32		delay; /* time elapsed since next packet was queued (in us) */
+	__u32		drop_next;
+	__u32		drop_overlimit;
+	__u32		ecn_mark;
+	__u32		dropping;
+	__u32		state1;
+	__u32		state2;
+	__u32		state3;
+	__u32		states;
+};
+
 #endif
diff --git a/include/net/codel.h b/include/net/codel.h
new file mode 100644
index 0000000..aed7ee9
--- /dev/null
+++ b/include/net/codel.h
@@ -0,0 +1,275 @@
+#ifndef __NET_SCHED_CODEL_H
+#define __NET_SCHED_CODEL_H
+
+#include <linux/types.h>
+#include <linux/bug.h>
+#include <linux/ktime.h>
+#include <net/inet_ecn.h>
+
+/* Controlling Queue Delay (Codel) algorithm 
+ * =========================================
+ * Source : Kathleen Nichols and Van Jacobson
+ */
+
+
+/*
+ * codel uses a 1024 nsec clock, encoded in u32
+ */
+typedef u32 codel_time_t;
+#define CODEL_SHIFT 10
+#define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
+
+static inline codel_time_t codel_get_time(void)
+{
+	u64 ns = ktime_to_ns(ktime_get());
+
+	return ns >> CODEL_SHIFT;
+}
+
+#define codel_time_after(a, b)	 ((int)(a) - (int)(b) > 0)
+#define codel_time_after_eq(a, b) ((int)(a) - (int)(b) >= 0)
+#define codel_time_before(a, b)	 ((int)(a) - (int)(b) < 0)
+#define codel_time_before_eq(a, b) ((int)(a) - (int)(b) <= 0)
+
+struct codel_skb_cb {
+	codel_time_t enqueue_time;
+};
+
+static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb)
+{
+	qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb));
+	return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data;
+}
+
+static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb)
+{
+	return get_codel_cb(skb)->enqueue_time;
+}
+
+static void codel_set_enqueue_time(struct sk_buff *skb)
+{
+	get_codel_cb(skb)->enqueue_time = codel_get_time();
+}
+
+static u32 codel_time_to_us(codel_time_t val)
+{
+	u64 valns = ((u64)val << CODEL_SHIFT);
+
+	do_div(valns, NSEC_PER_USEC);
+	return (u32)valns;
+}
+
+struct codel_params {
+	u32		minbytes; /* 1500, or interface MTU */
+	codel_time_t	interval; /* MS2TIME(100) */
+	codel_time_t	target;	  /* MS2TIME(5) */
+	bool		ecn;	  /* is ECN enabled */
+};
+
+struct codel_vars {
+	u32		count; /* packets dropped since we went into drop state */
+	bool		dropping;
+	/* time to declare above q->target (0 if below)*/
+	codel_time_t	first_above_time;
+	codel_time_t	drop_next; /* time to drop next packet */
+};
+
+/* contains stats and some shared info */
+struct codel_stats {
+	struct Qdisc 	*sch;
+	u32		drop_count; /* temp count of dropped packets in dequeue() */
+
+	u32		ecn_mark;
+	u32		state1;
+	u32		state2;
+	u32		state3;
+	u32		states;
+};
+
+static void codel_params_init(struct codel_params *params,
+			      const struct Qdisc *sch)
+{
+	params->minbytes = psched_mtu(qdisc_dev(sch));
+	params->interval = MS2TIME(100);
+	params->target = MS2TIME(5);
+	params->ecn = false;
+}
+
+static void codel_vars_init(struct codel_vars *vars)
+{
+	vars->drop_next = 0;
+	vars->first_above_time = 0;
+	vars->dropping = false; /* exit dropping state */
+	vars->count = 1;
+}
+
+static void codel_stats_init(struct codel_stats *stats,
+			     struct Qdisc *sch)
+{
+	stats->sch = sch; /* back pointer for qdisc_drop() calls */
+}
+
+/* return interval/sqrt(x) with good precision */
+static u32 codel_inv_sqrt(u32 _interval, u32 _x)
+{
+	u64 interval = _interval;
+	unsigned long x = _x;
+
+	/* Scale operands for max precision.
+	 * On 64bit arches, we can prescale x by 32bits
+	 */
+	if (BITS_PER_LONG == 64) {
+		x <<= 32;
+		interval <<= 16;
+	}
+	while (x < (1UL << (BITS_PER_LONG - 2))) {
+		x <<= 2;
+		interval <<= 1;
+	}
+	do_div(interval, int_sqrt(x));
+	return (u32)interval;
+}
+
+static codel_time_t codel_control_law(codel_time_t t,
+				      codel_time_t interval,
+				      u32 count)
+{
+	return t + codel_inv_sqrt(interval, count);
+}
+
+
+static bool codel_should_drop(struct sk_buff *skb,
+			      unsigned int *backlog,
+			      struct codel_vars *vars,
+			      const struct codel_params *params,
+			      struct codel_stats *stats,
+			      codel_time_t now)
+{
+	codel_time_t sojourn_time;
+	bool drop;
+
+	if (!skb) {
+		vars->first_above_time = 0;
+		return false;
+	}
+
+	sojourn_time = now - codel_get_enqueue_time(skb);
+	*backlog -= qdisc_pkt_len(skb);
+
+	if (codel_time_before(sojourn_time, params->target) || 
+	    *backlog < params->minbytes) {
+		/* went below so we'll stay below for at least q->interval */
+		vars->first_above_time = 0;
+		return false;
+	}
+	drop = false;
+	if (vars->first_above_time == 0) {
+		/* just went above from below. If we stay above
+		 * for at least q->interval we'll say it's ok to drop
+		 */
+		vars->first_above_time = now + params->interval;
+	} else if (codel_time_after(now, vars->first_above_time)) {
+		drop = true;
+		stats->state1++;
+	}
+	return drop;
+}
+
+typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars);
+
+static struct sk_buff *codel_dequeue(const struct codel_params *params,
+				     struct codel_vars *vars,
+				     struct codel_stats *stats,
+				     codel_skb_dequeue_t dequeue_func,
+				     u32 *backlog)
+{
+	struct sk_buff *skb = dequeue_func(vars);
+	codel_time_t now;
+	bool drop;
+
+	if (!skb) {
+		vars->dropping = false;
+		return skb;
+	}
+	now = codel_get_time();
+	drop = codel_should_drop(skb, backlog,
+				vars, params, stats,
+				now);
+	if (vars->dropping) {
+		if (!drop) {
+			/* sojourn time below target - leave dropping state */
+			vars->dropping = false;
+		} else if (codel_time_after_eq(now, vars->drop_next)) {
+			stats->state2++;
+			/* It's time for the next drop. Drop the current
+			 * packet and dequeue the next. The dequeue might 
+			 * take us out of dropping state. 
+			 * If not, schedule the next drop.
+			 * A large backlog might result in drop rates so high
+			 * that the next drop should happen now, 
+			 * hence the while loop.
+			 */  
+			while (vars->dropping && 
+			       codel_time_after_eq(now, vars->drop_next)) {
+				vars->count++;
+				if (params->ecn && INET_ECN_set_ce(skb)) {
+					stats->ecn_mark++;
+					vars->drop_next =
+						codel_control_law(vars->drop_next,
+								  params->interval,
+								  vars->count);
+					goto end;
+				}
+				qdisc_drop(skb, stats->sch);
+				stats->drop_count++;
+				skb = dequeue_func(vars);
+				if (!codel_should_drop(skb, backlog,
+						       vars, params, stats, now)) {
+					/* leave dropping state */
+					vars->dropping = false;
+				} else {
+					/* and schedule the next drop */
+					vars->drop_next =
+						codel_control_law(vars->drop_next,
+								  params->interval,
+								  vars->count);
+				}
+			}
+		}
+	} else if (drop &&
+		   (codel_time_before(now - vars->drop_next,
+				      16 * params->interval) ||
+		    codel_time_after_eq(now - vars->first_above_time,
+					2 * params->interval))) {
+		if (params->ecn && INET_ECN_set_ce(skb)) {
+			stats->ecn_mark++;
+		} else {
+			qdisc_drop(skb, stats->sch);
+			stats->drop_count++;
+
+			skb = dequeue_func(vars);
+			drop = codel_should_drop(skb, backlog, vars, params, stats, now);
+		}
+		vars->dropping = true;
+		stats->state3++;
+		/* 
+		 * if min went above target close to when we last went below it
+		 * assume that the drop rate that controlled the queue on the
+		 * last cycle is a good starting point to control it now.
+		 */
+		if (codel_time_before(now - vars->drop_next,
+				      16 * params->interval)) {
+//			u32 c = min(q->count - 1, q->count - (q->count >> 4));
+			u32 c = vars->count - 1;
+			vars->count = max(1U, c);
+		} else {
+			vars->count = 1;
+		}
+		vars->drop_next = codel_control_law(now, params->interval,
+						    vars->count);
+	}
+end:
+	stats->states++;
+	return skb;
+}
+#endif
diff --git a/net/sched/Kconfig b/net/sched/Kconfig
index 75b58f8..fadd252 100644
--- a/net/sched/Kconfig
+++ b/net/sched/Kconfig
@@ -250,6 +250,17 @@ config NET_SCH_QFQ
 
 	  If unsure, say N.
 
+config NET_SCH_CODEL
+	tristate "Controlled Delay AQM (CODEL)"
+	help
+	  Say Y here if you want to use the Controlled Delay (CODEL)
+	  packet scheduling algorithm.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called sch_codel.
+
+	  If unsure, say N.
+
 config NET_SCH_INGRESS
 	tristate "Ingress Qdisc"
 	depends on NET_CLS_ACT
diff --git a/net/sched/Makefile b/net/sched/Makefile
index 8cdf4e2..30fab03 100644
--- a/net/sched/Makefile
+++ b/net/sched/Makefile
@@ -37,6 +37,7 @@ obj-$(CONFIG_NET_SCH_PLUG)	+= sch_plug.o
 obj-$(CONFIG_NET_SCH_MQPRIO)	+= sch_mqprio.o
 obj-$(CONFIG_NET_SCH_CHOKE)	+= sch_choke.o
 obj-$(CONFIG_NET_SCH_QFQ)	+= sch_qfq.o
+obj-$(CONFIG_NET_SCH_CODEL)	+= sch_codel.o
 
 obj-$(CONFIG_NET_CLS_U32)	+= cls_u32.o
 obj-$(CONFIG_NET_CLS_ROUTE4)	+= cls_route.o
diff --git a/net/sched/sch_codel.c b/net/sched/sch_codel.c
new file mode 100644
index 0000000..fa36dd2
--- /dev/null
+++ b/net/sched/sch_codel.c
@@ -0,0 +1,252 @@
+/*
+ * net/sched/sch_codel.c	A Codel implementation
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License
+ *	as published by the Free Software Foundation; either version
+ *	2 of the License, or (at your option) any later version.
+ * 
+ * Codel, the COntrolled DELay Queueing discipline
+ * Based on ns2 simulation code presented by Kathie Nichols
+ *
+ * Authors:	Dave Täht <d at taht.net>
+ *		Eric Dumazet <edumazet at google.com>
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/skbuff.h>
+#include <net/pkt_sched.h>
+#include <net/codel.h>
+
+
+#define DEFAULT_CODEL_LIMIT 1000
+
+struct codel_sched_data {
+	struct codel_params	params;
+	struct codel_vars	vars;
+	struct codel_stats	stats;
+	u32			drop_overlimit;
+};
+
+/* This is the specific function called from codel_dequeue()
+ * to dequeue a packet from queue.
+ */
+static struct sk_buff *dequeue(struct codel_vars *vars)
+{
+	struct codel_sched_data *q;
+	struct Qdisc *sch;
+
+	q = container_of(vars, struct codel_sched_data, vars);
+	sch = (struct Qdisc *)((void *)q - QDISC_ALIGN(sizeof(struct Qdisc)));
+	return __skb_dequeue(&sch->q);
+}
+
+static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+	struct sk_buff *skb;
+
+	skb = codel_dequeue(&q->params, &q->vars, &q->stats,
+			    dequeue, &sch->qstats.backlog);
+	/* We cant call qdisc_tree_decrease_qlen() if our qlen is 0,
+	 * or HTB crashes. Defer it for next round.
+	 */
+	if (q->stats.drop_count && sch->q.qlen) {
+		qdisc_tree_decrease_qlen(sch, q->stats.drop_count);
+		q->stats.drop_count = 0;
+	}
+	if (skb)
+		qdisc_bstats_update(sch, skb);
+	return skb;
+}
+
+static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
+{
+	struct codel_sched_data *q;
+
+	if (likely(qdisc_qlen(sch) < sch->limit)) {
+		codel_set_enqueue_time(skb);
+		return qdisc_enqueue_tail(skb, sch);
+	}
+	q = qdisc_priv(sch);
+	q->drop_overlimit++;
+	return qdisc_drop(skb, sch);
+}
+
+static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
+	[TCA_CODEL_TARGET]	= { .type = NLA_U32 },
+	[TCA_CODEL_LIMIT]	= { .type = NLA_U32 },
+	[TCA_CODEL_MINBYTES]	= { .type = NLA_U32 },
+	[TCA_CODEL_INTERVAL]	= { .type = NLA_U32 },
+	[TCA_CODEL_ECN]		= { .type = NLA_U32 },
+};
+
+static int codel_change(struct Qdisc *sch, struct nlattr *opt)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+	struct nlattr *tb[TCA_CODEL_MAX + 1];
+	unsigned int qlen;
+	int err;
+
+	if (!opt)
+		return -EINVAL;
+
+	err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy);
+	if (err < 0)
+		return err;
+
+	sch_tree_lock(sch);
+	if (tb[TCA_CODEL_TARGET]) {
+		u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
+
+		q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
+	}
+	if (tb[TCA_CODEL_INTERVAL]) {
+		u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
+
+		q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
+	}
+	if (tb[TCA_CODEL_LIMIT])
+		sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
+
+	if (tb[TCA_CODEL_MINBYTES])
+		q->params.minbytes = nla_get_u32(tb[TCA_CODEL_MINBYTES]);
+
+	if (tb[TCA_CODEL_ECN])
+		q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
+
+	qlen = sch->q.qlen;
+	while (sch->q.qlen > sch->limit) {
+		struct sk_buff *skb = __skb_dequeue(&sch->q);
+
+		sch->qstats.backlog -= qdisc_pkt_len(skb);
+		qdisc_drop(skb, sch);
+	}
+	qdisc_tree_decrease_qlen(sch, qlen - sch->q.qlen);
+
+//	q->drop_next = q->first_above_time = 0;
+//	q->dropping = false;
+	sch_tree_unlock(sch);
+	return 0;
+}
+
+static int codel_init(struct Qdisc *sch, struct nlattr *opt)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+
+	/* It should be possible to run with no limit,
+	 * with infinite memory :)
+	 */
+	sch->limit = DEFAULT_CODEL_LIMIT;
+
+	codel_params_init(&q->params, sch);
+	codel_vars_init(&q->vars);
+	codel_stats_init(&q->stats, sch);
+
+	if (opt) {
+		int err = codel_change(sch, opt);
+
+		if (err)
+			return err;
+	}
+
+	if (sch->limit >= 1)
+		sch->flags |= TCQ_F_CAN_BYPASS;
+	else
+		sch->flags &= ~TCQ_F_CAN_BYPASS;
+
+	return 0;
+}
+
+static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+	struct nlattr *opts;
+
+	opts = nla_nest_start(skb, TCA_OPTIONS);
+	if (opts == NULL)
+		goto nla_put_failure;
+
+	if (nla_put_u32(skb, TCA_CODEL_TARGET,
+			codel_time_to_us(q->params.target)) ||
+	    nla_put_u32(skb, TCA_CODEL_LIMIT,
+			sch->limit) ||
+	    nla_put_u32(skb, TCA_CODEL_INTERVAL,
+			codel_time_to_us(q->params.interval)) ||
+	    nla_put_u32(skb, TCA_CODEL_MINBYTES,
+			q->params.minbytes) ||
+	    nla_put_u32(skb, TCA_CODEL_ECN, q->params.ecn))
+		goto nla_put_failure;
+
+	return nla_nest_end(skb, opts);
+
+nla_put_failure:
+	nla_nest_cancel(skb, opts);
+	return -1;
+}
+
+static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+	struct sk_buff *skb = skb_peek(&sch->q);
+	codel_time_t now = codel_get_time();
+	struct tc_codel_xstats st = {
+		.count	= q->vars.count,
+		.state1 = q->stats.state1,
+		.state2 = q->stats.state2,
+		.state3 = q->stats.state3,
+		.states = q->stats.states,
+		.drop_overlimit = q->drop_overlimit,
+		.delay = skb ? now - codel_get_enqueue_time(skb) : 0,
+		.drop_next = (q->vars.dropping && q->vars.drop_next) ?
+				q->vars.drop_next - now : 0,
+		.dropping = q->vars.dropping,
+		.ecn_mark = q->stats.ecn_mark,
+	};
+
+	return gnet_stats_copy_app(d, &st, sizeof(st));
+}
+
+static void codel_reset(struct Qdisc *sch)
+{
+	struct codel_sched_data *q = qdisc_priv(sch);
+
+	qdisc_reset_queue(sch);
+	sch->q.qlen = 0;
+	codel_vars_init(&q->vars);
+}
+
+static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
+	.id		=	"codel",
+	.priv_size	=	sizeof(struct codel_sched_data),
+
+	.enqueue	=	codel_qdisc_enqueue,
+	.dequeue	=	codel_qdisc_dequeue,
+	.peek		=	qdisc_peek_dequeued,
+	.init		=	codel_init,
+	.reset		=	codel_reset,
+	.change 	=	codel_change,
+	.dump		=	codel_dump,
+	.dump_stats	=	codel_dump_stats,
+	.owner		=	THIS_MODULE,
+};
+
+static int __init codel_module_init(void)
+{
+	return register_qdisc(&codel_qdisc_ops);
+}
+static void __exit codel_module_exit(void)
+{
+	unregister_qdisc(&codel_qdisc_ops);
+}
+module_init(codel_module_init)
+module_exit(codel_module_exit)
+
+MODULE_DESCRIPTION("Controlled Delay queue discipline");
+MODULE_AUTHOR("Dave Taht");
+MODULE_AUTHOR("Eric Dumazet");
+MODULE_LICENSE("GPL");





More information about the Codel mailing list