blob: 17cd81f84b5de9d9f53acf2c9078a7618927ddc5 [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;
Florian Westphaled760cb2016-09-18 00:57:33 +020072 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
Eric Dumazet76e3cc12012-05-10 07:51:25 +000073
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
Eric Dumazet520ac302016-06-21 23:16:49 -070085 kfree_skb(skb);
86 qdisc_qstats_drop(sch);
Michal Kazior79bdc4c2016-04-22 14:15:58 +020087}
88
Eric Dumazet76e3cc12012-05-10 07:51:25 +000089static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
90{
91 struct codel_sched_data *q = qdisc_priv(sch);
92 struct sk_buff *skb;
93
Michal Kazior79bdc4c2016-04-22 14:15:58 +020094 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars,
95 &q->stats, qdisc_pkt_len, codel_get_enqueue_time,
96 drop_func, dequeue_func);
Eric Dumazet865ec552012-05-16 04:39:09 +000097
WANG Cong2ccccf52016-02-25 14:55:01 -080098 /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
Eric Dumazet76e3cc12012-05-10 07:51:25 +000099 * or HTB crashes. Defer it for next round.
100 */
101 if (q->stats.drop_count && sch->q.qlen) {
WANG Cong2ccccf52016-02-25 14:55:01 -0800102 qdisc_tree_reduce_backlog(sch, q->stats.drop_count, q->stats.drop_len);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000103 q->stats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800104 q->stats.drop_len = 0;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000105 }
106 if (skb)
107 qdisc_bstats_update(sch, skb);
108 return skb;
109}
110
Eric Dumazet520ac302016-06-21 23:16:49 -0700111static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
112 struct sk_buff **to_free)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000113{
114 struct codel_sched_data *q;
115
116 if (likely(qdisc_qlen(sch) < sch->limit)) {
117 codel_set_enqueue_time(skb);
118 return qdisc_enqueue_tail(skb, sch);
119 }
120 q = qdisc_priv(sch);
121 q->drop_overlimit++;
Eric Dumazet520ac302016-06-21 23:16:49 -0700122 return qdisc_drop(skb, sch, to_free);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000123}
124
125static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
126 [TCA_CODEL_TARGET] = { .type = NLA_U32 },
127 [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
128 [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
129 [TCA_CODEL_ECN] = { .type = NLA_U32 },
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700130 [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000131};
132
Alexander Aring20307212017-12-20 12:35:14 -0500133static int codel_change(struct Qdisc *sch, struct nlattr *opt,
134 struct netlink_ext_ack *extack)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000135{
136 struct codel_sched_data *q = qdisc_priv(sch);
137 struct nlattr *tb[TCA_CODEL_MAX + 1];
WANG Cong2ccccf52016-02-25 14:55:01 -0800138 unsigned int qlen, dropped = 0;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000139 int err;
140
141 if (!opt)
142 return -EINVAL;
143
Johannes Bergfceb6432017-04-12 14:34:07 +0200144 err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy, NULL);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000145 if (err < 0)
146 return err;
147
148 sch_tree_lock(sch);
149
150 if (tb[TCA_CODEL_TARGET]) {
151 u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
152
153 q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
154 }
155
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700156 if (tb[TCA_CODEL_CE_THRESHOLD]) {
157 u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
158
159 q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
160 }
161
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000162 if (tb[TCA_CODEL_INTERVAL]) {
163 u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
164
165 q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
166 }
167
168 if (tb[TCA_CODEL_LIMIT])
169 sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
170
171 if (tb[TCA_CODEL_ECN])
172 q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
173
174 qlen = sch->q.qlen;
175 while (sch->q.qlen > sch->limit) {
Florian Westphaled760cb2016-09-18 00:57:33 +0200176 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000177
WANG Cong2ccccf52016-02-25 14:55:01 -0800178 dropped += qdisc_pkt_len(skb);
John Fastabend25331d62014-09-28 11:53:29 -0700179 qdisc_qstats_backlog_dec(sch, skb);
Eric Dumazetb3d7e2b2016-06-13 20:21:52 -0700180 rtnl_qdisc_drop(skb, sch);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000181 }
WANG Cong2ccccf52016-02-25 14:55:01 -0800182 qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000183
184 sch_tree_unlock(sch);
185 return 0;
186}
187
Alexander Aringe63d7df2017-12-20 12:35:13 -0500188static int codel_init(struct Qdisc *sch, struct nlattr *opt,
189 struct netlink_ext_ack *extack)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000190{
191 struct codel_sched_data *q = qdisc_priv(sch);
192
193 sch->limit = DEFAULT_CODEL_LIMIT;
194
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200195 codel_params_init(&q->params);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000196 codel_vars_init(&q->vars);
197 codel_stats_init(&q->stats);
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200198 q->params.mtu = psched_mtu(qdisc_dev(sch));
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000199
200 if (opt) {
Alexander Aring20307212017-12-20 12:35:14 -0500201 int err = codel_change(sch, opt, extack);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000202
203 if (err)
204 return err;
205 }
206
207 if (sch->limit >= 1)
208 sch->flags |= TCQ_F_CAN_BYPASS;
209 else
210 sch->flags &= ~TCQ_F_CAN_BYPASS;
211
212 return 0;
213}
214
215static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
216{
217 struct codel_sched_data *q = qdisc_priv(sch);
218 struct nlattr *opts;
219
220 opts = nla_nest_start(skb, TCA_OPTIONS);
221 if (opts == NULL)
222 goto nla_put_failure;
223
224 if (nla_put_u32(skb, TCA_CODEL_TARGET,
225 codel_time_to_us(q->params.target)) ||
226 nla_put_u32(skb, TCA_CODEL_LIMIT,
227 sch->limit) ||
228 nla_put_u32(skb, TCA_CODEL_INTERVAL,
229 codel_time_to_us(q->params.interval)) ||
230 nla_put_u32(skb, TCA_CODEL_ECN,
231 q->params.ecn))
232 goto nla_put_failure;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700233 if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD &&
234 nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
235 codel_time_to_us(q->params.ce_threshold)))
236 goto nla_put_failure;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000237 return nla_nest_end(skb, opts);
238
239nla_put_failure:
240 nla_nest_cancel(skb, opts);
241 return -1;
242}
243
244static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
245{
246 const struct codel_sched_data *q = qdisc_priv(sch);
247 struct tc_codel_xstats st = {
248 .maxpacket = q->stats.maxpacket,
249 .count = q->vars.count,
250 .lastcount = q->vars.lastcount,
251 .drop_overlimit = q->drop_overlimit,
252 .ldelay = codel_time_to_us(q->vars.ldelay),
253 .dropping = q->vars.dropping,
254 .ecn_mark = q->stats.ecn_mark,
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700255 .ce_mark = q->stats.ce_mark,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000256 };
257
258 if (q->vars.dropping) {
259 codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
260
261 if (delta >= 0)
262 st.drop_next = codel_time_to_us(delta);
263 else
264 st.drop_next = -codel_time_to_us(-delta);
265 }
266
267 return gnet_stats_copy_app(d, &st, sizeof(st));
268}
269
270static void codel_reset(struct Qdisc *sch)
271{
272 struct codel_sched_data *q = qdisc_priv(sch);
273
274 qdisc_reset_queue(sch);
275 codel_vars_init(&q->vars);
276}
277
278static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
279 .id = "codel",
280 .priv_size = sizeof(struct codel_sched_data),
281
282 .enqueue = codel_qdisc_enqueue,
283 .dequeue = codel_qdisc_dequeue,
284 .peek = qdisc_peek_dequeued,
285 .init = codel_init,
286 .reset = codel_reset,
287 .change = codel_change,
288 .dump = codel_dump,
289 .dump_stats = codel_dump_stats,
290 .owner = THIS_MODULE,
291};
292
293static int __init codel_module_init(void)
294{
295 return register_qdisc(&codel_qdisc_ops);
296}
297
298static void __exit codel_module_exit(void)
299{
300 unregister_qdisc(&codel_qdisc_ops);
301}
302
303module_init(codel_module_init)
304module_exit(codel_module_exit)
305
306MODULE_DESCRIPTION("Controlled Delay queue discipline");
307MODULE_AUTHOR("Dave Taht");
308MODULE_AUTHOR("Eric Dumazet");
309MODULE_LICENSE("Dual BSD/GPL");