GNU Linux-libre 4.4.289-gnu1
[releases.git] / net / sched / sch_codel.c
1 /*
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>
9  *  Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
10  *
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>
49 #include <linux/prefetch.h>
50 #include <net/pkt_sched.h>
51 #include <net/codel.h>
52
53
54 #define DEFAULT_CODEL_LIMIT 1000
55
56 struct codel_sched_data {
57         struct codel_params     params;
58         struct codel_vars       vars;
59         struct codel_stats      stats;
60         u32                     drop_overlimit;
61 };
62
63 /* This is the specific function called from codel_dequeue()
64  * to dequeue a packet from queue. Note: backlog is handled in
65  * codel, we dont need to reduce it here.
66  */
67 static struct sk_buff *dequeue(struct codel_vars *vars, struct Qdisc *sch)
68 {
69         struct sk_buff *skb = __skb_dequeue(&sch->q);
70
71         if (skb)
72                 prefetch(&skb->end); /* we'll need skb_shinfo() */
73         return skb;
74 }
75
76 static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
77 {
78         struct codel_sched_data *q = qdisc_priv(sch);
79         struct sk_buff *skb;
80
81         skb = codel_dequeue(sch, &q->params, &q->vars, &q->stats, dequeue);
82
83         /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
84          * or HTB crashes. Defer it for next round.
85          */
86         if (q->stats.drop_count && sch->q.qlen) {
87                 qdisc_tree_reduce_backlog(sch, q->stats.drop_count, q->stats.drop_len);
88                 q->stats.drop_count = 0;
89                 q->stats.drop_len = 0;
90         }
91         if (skb)
92                 qdisc_bstats_update(sch, skb);
93         return skb;
94 }
95
96 static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
97 {
98         struct codel_sched_data *q;
99
100         if (likely(qdisc_qlen(sch) < sch->limit)) {
101                 codel_set_enqueue_time(skb);
102                 return qdisc_enqueue_tail(skb, sch);
103         }
104         q = qdisc_priv(sch);
105         q->drop_overlimit++;
106         return qdisc_drop(skb, sch);
107 }
108
109 static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
110         [TCA_CODEL_TARGET]      = { .type = NLA_U32 },
111         [TCA_CODEL_LIMIT]       = { .type = NLA_U32 },
112         [TCA_CODEL_INTERVAL]    = { .type = NLA_U32 },
113         [TCA_CODEL_ECN]         = { .type = NLA_U32 },
114         [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
115 };
116
117 static int codel_change(struct Qdisc *sch, struct nlattr *opt)
118 {
119         struct codel_sched_data *q = qdisc_priv(sch);
120         struct nlattr *tb[TCA_CODEL_MAX + 1];
121         unsigned int qlen, dropped = 0;
122         int err;
123
124         if (!opt)
125                 return -EINVAL;
126
127         err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy);
128         if (err < 0)
129                 return err;
130
131         sch_tree_lock(sch);
132
133         if (tb[TCA_CODEL_TARGET]) {
134                 u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
135
136                 q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
137         }
138
139         if (tb[TCA_CODEL_CE_THRESHOLD]) {
140                 u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
141
142                 q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
143         }
144
145         if (tb[TCA_CODEL_INTERVAL]) {
146                 u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
147
148                 q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
149         }
150
151         if (tb[TCA_CODEL_LIMIT])
152                 sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
153
154         if (tb[TCA_CODEL_ECN])
155                 q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
156
157         qlen = sch->q.qlen;
158         while (sch->q.qlen > sch->limit) {
159                 struct sk_buff *skb = __skb_dequeue(&sch->q);
160
161                 dropped += qdisc_pkt_len(skb);
162                 qdisc_qstats_backlog_dec(sch, skb);
163                 qdisc_drop(skb, sch);
164         }
165         qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
166
167         sch_tree_unlock(sch);
168         return 0;
169 }
170
171 static int codel_init(struct Qdisc *sch, struct nlattr *opt)
172 {
173         struct codel_sched_data *q = qdisc_priv(sch);
174
175         sch->limit = DEFAULT_CODEL_LIMIT;
176
177         codel_params_init(&q->params, sch);
178         codel_vars_init(&q->vars);
179         codel_stats_init(&q->stats);
180
181         if (opt) {
182                 int err = codel_change(sch, opt);
183
184                 if (err)
185                         return err;
186         }
187
188         if (sch->limit >= 1)
189                 sch->flags |= TCQ_F_CAN_BYPASS;
190         else
191                 sch->flags &= ~TCQ_F_CAN_BYPASS;
192
193         return 0;
194 }
195
196 static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
197 {
198         struct codel_sched_data *q = qdisc_priv(sch);
199         struct nlattr *opts;
200
201         opts = nla_nest_start(skb, TCA_OPTIONS);
202         if (opts == NULL)
203                 goto nla_put_failure;
204
205         if (nla_put_u32(skb, TCA_CODEL_TARGET,
206                         codel_time_to_us(q->params.target)) ||
207             nla_put_u32(skb, TCA_CODEL_LIMIT,
208                         sch->limit) ||
209             nla_put_u32(skb, TCA_CODEL_INTERVAL,
210                         codel_time_to_us(q->params.interval)) ||
211             nla_put_u32(skb, TCA_CODEL_ECN,
212                         q->params.ecn))
213                 goto nla_put_failure;
214         if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD &&
215             nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
216                         codel_time_to_us(q->params.ce_threshold)))
217                 goto nla_put_failure;
218         return nla_nest_end(skb, opts);
219
220 nla_put_failure:
221         nla_nest_cancel(skb, opts);
222         return -1;
223 }
224
225 static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
226 {
227         const struct codel_sched_data *q = qdisc_priv(sch);
228         struct tc_codel_xstats st = {
229                 .maxpacket      = q->stats.maxpacket,
230                 .count          = q->vars.count,
231                 .lastcount      = q->vars.lastcount,
232                 .drop_overlimit = q->drop_overlimit,
233                 .ldelay         = codel_time_to_us(q->vars.ldelay),
234                 .dropping       = q->vars.dropping,
235                 .ecn_mark       = q->stats.ecn_mark,
236                 .ce_mark        = q->stats.ce_mark,
237         };
238
239         if (q->vars.dropping) {
240                 codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
241
242                 if (delta >= 0)
243                         st.drop_next = codel_time_to_us(delta);
244                 else
245                         st.drop_next = -codel_time_to_us(-delta);
246         }
247
248         return gnet_stats_copy_app(d, &st, sizeof(st));
249 }
250
251 static void codel_reset(struct Qdisc *sch)
252 {
253         struct codel_sched_data *q = qdisc_priv(sch);
254
255         qdisc_reset_queue(sch);
256         codel_vars_init(&q->vars);
257 }
258
259 static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
260         .id             =       "codel",
261         .priv_size      =       sizeof(struct codel_sched_data),
262
263         .enqueue        =       codel_qdisc_enqueue,
264         .dequeue        =       codel_qdisc_dequeue,
265         .peek           =       qdisc_peek_dequeued,
266         .init           =       codel_init,
267         .reset          =       codel_reset,
268         .change         =       codel_change,
269         .dump           =       codel_dump,
270         .dump_stats     =       codel_dump_stats,
271         .owner          =       THIS_MODULE,
272 };
273
274 static int __init codel_module_init(void)
275 {
276         return register_qdisc(&codel_qdisc_ops);
277 }
278
279 static void __exit codel_module_exit(void)
280 {
281         unregister_qdisc(&codel_qdisc_ops);
282 }
283
284 module_init(codel_module_init)
285 module_exit(codel_module_exit)
286
287 MODULE_DESCRIPTION("Controlled Delay queue discipline");
288 MODULE_AUTHOR("Dave Taht");
289 MODULE_AUTHOR("Eric Dumazet");
290 MODULE_LICENSE("Dual BSD/GPL");