blob: dddf3bb65a328a2de51a5eb9f6f2945b68cc9d11 [file] [log] [blame]
Eric Dumazet76e3cc12012-05-10 07:51:25 +00001/*
2 * Codel - The Controlled-Delay Active Queue Management algorithm
3 *
4 * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
5 * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
6 *
7 * Implemented on linux by :
8 * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
Eric Dumazet80ba92f2015-05-08 15:05:12 -07009 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
Eric Dumazet76e3cc12012-05-10 07:51:25 +000010 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The names of the authors may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE.
40 *
41 */
42
43#include <linux/module.h>
44#include <linux/slab.h>
45#include <linux/types.h>
46#include <linux/kernel.h>
47#include <linux/errno.h>
48#include <linux/skbuff.h>
Geert Uytterhoevence5b4b92012-05-14 09:47:05 +000049#include <linux/prefetch.h>
Eric Dumazet76e3cc12012-05-10 07:51:25 +000050#include <net/pkt_sched.h>
51#include <net/codel.h>
Michal Kaziord068ca22016-04-22 14:15:59 +020052#include <net/codel_impl.h>
53#include <net/codel_qdisc.h>
Eric Dumazet76e3cc12012-05-10 07:51:25 +000054
55
56#define DEFAULT_CODEL_LIMIT 1000
57
58struct codel_sched_data {
59 struct codel_params params;
60 struct codel_vars vars;
61 struct codel_stats stats;
62 u32 drop_overlimit;
63};
64
65/* This is the specific function called from codel_dequeue()
66 * to dequeue a packet from queue. Note: backlog is handled in
67 * codel, we dont need to reduce it here.
68 */
Michal Kazior79bdc4c2016-04-22 14:15:58 +020069static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
Eric Dumazet76e3cc12012-05-10 07:51:25 +000070{
Michal Kazior79bdc4c2016-04-22 14:15:58 +020071 struct Qdisc *sch = ctx;
Eric Dumazet76e3cc12012-05-10 07:51:25 +000072 struct sk_buff *skb = __skb_dequeue(&sch->q);
73
Michal Kazior79bdc4c2016-04-22 14:15:58 +020074 if (skb)
75 sch->qstats.backlog -= qdisc_pkt_len(skb);
76
Eric Dumazet76e3cc12012-05-10 07:51:25 +000077 prefetch(&skb->end); /* we'll need skb_shinfo() */
78 return skb;
79}
80
Michal Kazior79bdc4c2016-04-22 14:15:58 +020081static void drop_func(struct sk_buff *skb, void *ctx)
82{
83 struct Qdisc *sch = ctx;
84
85 qdisc_drop(skb, sch);
86}
87
Eric Dumazet76e3cc12012-05-10 07:51:25 +000088static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
89{
90 struct codel_sched_data *q = qdisc_priv(sch);
91 struct sk_buff *skb;
92
Michal Kazior79bdc4c2016-04-22 14:15:58 +020093 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars,
94 &q->stats, qdisc_pkt_len, codel_get_enqueue_time,
95 drop_func, dequeue_func);
Eric Dumazet865ec552012-05-16 04:39:09 +000096
WANG Cong2ccccf52016-02-25 14:55:01 -080097 /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
Eric Dumazet76e3cc12012-05-10 07:51:25 +000098 * or HTB crashes. Defer it for next round.
99 */
100 if (q->stats.drop_count && sch->q.qlen) {
WANG Cong2ccccf52016-02-25 14:55:01 -0800101 qdisc_tree_reduce_backlog(sch, q->stats.drop_count, q->stats.drop_len);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000102 q->stats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800103 q->stats.drop_len = 0;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000104 }
105 if (skb)
106 qdisc_bstats_update(sch, skb);
107 return skb;
108}
109
110static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
111{
112 struct codel_sched_data *q;
113
114 if (likely(qdisc_qlen(sch) < sch->limit)) {
115 codel_set_enqueue_time(skb);
116 return qdisc_enqueue_tail(skb, sch);
117 }
118 q = qdisc_priv(sch);
119 q->drop_overlimit++;
120 return qdisc_drop(skb, sch);
121}
122
123static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
124 [TCA_CODEL_TARGET] = { .type = NLA_U32 },
125 [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
126 [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
127 [TCA_CODEL_ECN] = { .type = NLA_U32 },
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700128 [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000129};
130
131static int codel_change(struct Qdisc *sch, struct nlattr *opt)
132{
133 struct codel_sched_data *q = qdisc_priv(sch);
134 struct nlattr *tb[TCA_CODEL_MAX + 1];
WANG Cong2ccccf52016-02-25 14:55:01 -0800135 unsigned int qlen, dropped = 0;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000136 int err;
137
138 if (!opt)
139 return -EINVAL;
140
141 err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy);
142 if (err < 0)
143 return err;
144
145 sch_tree_lock(sch);
146
147 if (tb[TCA_CODEL_TARGET]) {
148 u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
149
150 q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
151 }
152
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700153 if (tb[TCA_CODEL_CE_THRESHOLD]) {
154 u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
155
156 q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
157 }
158
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000159 if (tb[TCA_CODEL_INTERVAL]) {
160 u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
161
162 q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
163 }
164
165 if (tb[TCA_CODEL_LIMIT])
166 sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
167
168 if (tb[TCA_CODEL_ECN])
169 q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
170
171 qlen = sch->q.qlen;
172 while (sch->q.qlen > sch->limit) {
173 struct sk_buff *skb = __skb_dequeue(&sch->q);
174
WANG Cong2ccccf52016-02-25 14:55:01 -0800175 dropped += qdisc_pkt_len(skb);
John Fastabend25331d62014-09-28 11:53:29 -0700176 qdisc_qstats_backlog_dec(sch, skb);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000177 qdisc_drop(skb, sch);
178 }
WANG Cong2ccccf52016-02-25 14:55:01 -0800179 qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000180
181 sch_tree_unlock(sch);
182 return 0;
183}
184
185static int codel_init(struct Qdisc *sch, struct nlattr *opt)
186{
187 struct codel_sched_data *q = qdisc_priv(sch);
188
189 sch->limit = DEFAULT_CODEL_LIMIT;
190
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200191 codel_params_init(&q->params);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000192 codel_vars_init(&q->vars);
193 codel_stats_init(&q->stats);
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200194 q->params.mtu = psched_mtu(qdisc_dev(sch));
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000195
196 if (opt) {
197 int err = codel_change(sch, opt);
198
199 if (err)
200 return err;
201 }
202
203 if (sch->limit >= 1)
204 sch->flags |= TCQ_F_CAN_BYPASS;
205 else
206 sch->flags &= ~TCQ_F_CAN_BYPASS;
207
208 return 0;
209}
210
211static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
212{
213 struct codel_sched_data *q = qdisc_priv(sch);
214 struct nlattr *opts;
215
216 opts = nla_nest_start(skb, TCA_OPTIONS);
217 if (opts == NULL)
218 goto nla_put_failure;
219
220 if (nla_put_u32(skb, TCA_CODEL_TARGET,
221 codel_time_to_us(q->params.target)) ||
222 nla_put_u32(skb, TCA_CODEL_LIMIT,
223 sch->limit) ||
224 nla_put_u32(skb, TCA_CODEL_INTERVAL,
225 codel_time_to_us(q->params.interval)) ||
226 nla_put_u32(skb, TCA_CODEL_ECN,
227 q->params.ecn))
228 goto nla_put_failure;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700229 if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD &&
230 nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
231 codel_time_to_us(q->params.ce_threshold)))
232 goto nla_put_failure;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000233 return nla_nest_end(skb, opts);
234
235nla_put_failure:
236 nla_nest_cancel(skb, opts);
237 return -1;
238}
239
240static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
241{
242 const struct codel_sched_data *q = qdisc_priv(sch);
243 struct tc_codel_xstats st = {
244 .maxpacket = q->stats.maxpacket,
245 .count = q->vars.count,
246 .lastcount = q->vars.lastcount,
247 .drop_overlimit = q->drop_overlimit,
248 .ldelay = codel_time_to_us(q->vars.ldelay),
249 .dropping = q->vars.dropping,
250 .ecn_mark = q->stats.ecn_mark,
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700251 .ce_mark = q->stats.ce_mark,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000252 };
253
254 if (q->vars.dropping) {
255 codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
256
257 if (delta >= 0)
258 st.drop_next = codel_time_to_us(delta);
259 else
260 st.drop_next = -codel_time_to_us(-delta);
261 }
262
263 return gnet_stats_copy_app(d, &st, sizeof(st));
264}
265
266static void codel_reset(struct Qdisc *sch)
267{
268 struct codel_sched_data *q = qdisc_priv(sch);
269
270 qdisc_reset_queue(sch);
271 codel_vars_init(&q->vars);
272}
273
274static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
275 .id = "codel",
276 .priv_size = sizeof(struct codel_sched_data),
277
278 .enqueue = codel_qdisc_enqueue,
279 .dequeue = codel_qdisc_dequeue,
280 .peek = qdisc_peek_dequeued,
281 .init = codel_init,
282 .reset = codel_reset,
283 .change = codel_change,
284 .dump = codel_dump,
285 .dump_stats = codel_dump_stats,
286 .owner = THIS_MODULE,
287};
288
289static int __init codel_module_init(void)
290{
291 return register_qdisc(&codel_qdisc_ops);
292}
293
294static void __exit codel_module_exit(void)
295{
296 unregister_qdisc(&codel_qdisc_ops);
297}
298
299module_init(codel_module_init)
300module_exit(codel_module_exit)
301
302MODULE_DESCRIPTION("Controlled Delay queue discipline");
303MODULE_AUTHOR("Dave Taht");
304MODULE_AUTHOR("Eric Dumazet");
305MODULE_LICENSE("Dual BSD/GPL");