blob: 689157555fa4095e987164bd7925c40b03af00ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_red.c Random Early Detection queue.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
Thomas Grafdba051f2005-11-05 21:14:08 +010012 * J Hadi Salim 980914: computation fixes
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alexey Makarenko <makar@phoenix.kharkov.ua> 990814: qave on idle link was calculated incorrectly.
Thomas Grafdba051f2005-11-05 21:14:08 +010014 * J Hadi Salim 980816: ECN support
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/pkt_sched.h>
22#include <net/inet_ecn.h>
Thomas Graf6b31b282005-11-05 21:14:05 +010023#include <net/red.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25
Thomas Graf6b31b282005-11-05 21:14:05 +010026/* Parameters, settable by user:
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 -----------------------------
28
29 limit - bytes (must be > qth_max + burst)
30
31 Hard limit on queue length, should be chosen >qth_max
32 to allow packet bursts. This parameter does not
33 affect the algorithms behaviour and can be chosen
34 arbitrarily high (well, less than ram size)
35 Really, this limit will never be reached
36 if RED works correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Eric Dumazetcc7ec452011-01-19 19:26:56 +000039struct red_sched_data {
Thomas Graf6b31b282005-11-05 21:14:05 +010040 u32 limit; /* HARD maximal queue length */
41 unsigned char flags;
42 struct red_parms parms;
43 struct red_stats stats;
Patrick McHardyf38c39d2006-03-20 19:20:44 -080044 struct Qdisc *qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Thomas Graf6b31b282005-11-05 21:14:05 +010047static inline int red_use_ecn(struct red_sched_data *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Thomas Graf6b31b282005-11-05 21:14:05 +010049 return q->flags & TC_RED_ECN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Thomas Grafbdc450a2005-11-05 21:14:28 +010052static inline int red_use_harddrop(struct red_sched_data *q)
53{
54 return q->flags & TC_RED_HARDDROP;
55}
56
Eric Dumazetcc7ec452011-01-19 19:26:56 +000057static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -080060 struct Qdisc *child = q->qdisc;
61 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Patrick McHardyf38c39d2006-03-20 19:20:44 -080063 q->parms.qavg = red_calc_qavg(&q->parms, child->qstats.backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Thomas Graf6b31b282005-11-05 21:14:05 +010065 if (red_is_idling(&q->parms))
66 red_end_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Thomas Graf6b31b282005-11-05 21:14:05 +010068 switch (red_action(&q->parms, q->parms.qavg)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +000069 case RED_DONT_MARK:
70 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Dumazetcc7ec452011-01-19 19:26:56 +000072 case RED_PROB_MARK:
73 sch->qstats.overlimits++;
74 if (!red_use_ecn(q) || !INET_ECN_set_ce(skb)) {
75 q->stats.prob_drop++;
76 goto congestion_drop;
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Eric Dumazetcc7ec452011-01-19 19:26:56 +000079 q->stats.prob_mark++;
80 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric Dumazetcc7ec452011-01-19 19:26:56 +000082 case RED_HARD_MARK:
83 sch->qstats.overlimits++;
84 if (red_use_harddrop(q) || !red_use_ecn(q) ||
85 !INET_ECN_set_ce(skb)) {
86 q->stats.forced_drop++;
87 goto congestion_drop;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric Dumazetcc7ec452011-01-19 19:26:56 +000090 q->stats.forced_mark++;
91 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Jussi Kivilinna5f861732008-07-20 00:08:04 -070094 ret = qdisc_enqueue(skb, child);
Patrick McHardyf38c39d2006-03-20 19:20:44 -080095 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazetbfe0d022011-01-09 08:30:54 +000096 qdisc_bstats_update(sch, skb);
Patrick McHardyf38c39d2006-03-20 19:20:44 -080097 sch->q.qlen++;
Jarek Poplawski378a2f02008-08-04 22:31:03 -070098 } else if (net_xmit_drop_count(ret)) {
Patrick McHardyf38c39d2006-03-20 19:20:44 -080099 q->stats.pdrop++;
100 sch->qstats.drops++;
101 }
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Thomas Graf6b31b282005-11-05 21:14:05 +0100104congestion_drop:
Thomas Graf9e178ff2005-11-05 21:14:06 +0100105 qdisc_drop(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return NET_XMIT_CN;
107}
108
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000109static struct sk_buff *red_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct sk_buff *skb;
112 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800113 struct Qdisc *child = q->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800115 skb = child->dequeue(child);
116 if (skb)
117 sch->q.qlen--;
118 else if (!red_is_idling(&q->parms))
Thomas Graf9e178ff2005-11-05 21:14:06 +0100119 red_start_of_idle_period(&q->parms);
120
121 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000124static struct sk_buff *red_peek(struct Qdisc *sch)
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700125{
126 struct red_sched_data *q = qdisc_priv(sch);
127 struct Qdisc *child = q->qdisc;
128
129 return child->ops->peek(child);
130}
131
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000132static unsigned int red_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800135 struct Qdisc *child = q->qdisc;
136 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800138 if (child->ops->drop && (len = child->ops->drop(child)) > 0) {
Thomas Graf6b31b282005-11-05 21:14:05 +0100139 q->stats.other++;
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800140 sch->qstats.drops++;
141 sch->q.qlen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return len;
143 }
Thomas Graf6b31b282005-11-05 21:14:05 +0100144
Thomas Graf6a1b63d2005-11-05 21:14:07 +0100145 if (!red_is_idling(&q->parms))
146 red_start_of_idle_period(&q->parms);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149}
150
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000151static void red_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct red_sched_data *q = qdisc_priv(sch);
154
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800155 qdisc_reset(q->qdisc);
156 sch->q.qlen = 0;
Thomas Graf6b31b282005-11-05 21:14:05 +0100157 red_restart(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800160static void red_destroy(struct Qdisc *sch)
161{
162 struct red_sched_data *q = qdisc_priv(sch);
163 qdisc_destroy(q->qdisc);
164}
165
Patrick McHardy27a34212008-01-23 20:35:39 -0800166static const struct nla_policy red_policy[TCA_RED_MAX + 1] = {
167 [TCA_RED_PARMS] = { .len = sizeof(struct tc_red_qopt) },
168 [TCA_RED_STAB] = { .len = RED_STAB_SIZE },
169};
170
Patrick McHardy1e904742008-01-22 22:11:17 -0800171static int red_change(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800174 struct nlattr *tb[TCA_RED_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct tc_red_qopt *ctl;
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800176 struct Qdisc *child = NULL;
Patrick McHardycee63722008-01-23 20:33:32 -0800177 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Patrick McHardycee63722008-01-23 20:33:32 -0800179 if (opt == NULL)
Thomas Grafdba051f2005-11-05 21:14:08 +0100180 return -EINVAL;
181
Patrick McHardy27a34212008-01-23 20:35:39 -0800182 err = nla_parse_nested(tb, TCA_RED_MAX, opt, red_policy);
Patrick McHardycee63722008-01-23 20:33:32 -0800183 if (err < 0)
184 return err;
185
Patrick McHardy1e904742008-01-22 22:11:17 -0800186 if (tb[TCA_RED_PARMS] == NULL ||
Patrick McHardy27a34212008-01-23 20:35:39 -0800187 tb[TCA_RED_STAB] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189
Patrick McHardy1e904742008-01-22 22:11:17 -0800190 ctl = nla_data(tb[TCA_RED_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800192 if (ctl->limit > 0) {
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700193 child = fifo_create_dflt(sch, &bfifo_qdisc_ops, ctl->limit);
194 if (IS_ERR(child))
195 return PTR_ERR(child);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sch_tree_lock(sch);
199 q->flags = ctl->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 q->limit = ctl->limit;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800201 if (child) {
202 qdisc_tree_decrease_qlen(q->qdisc, q->qdisc->q.qlen);
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800203 qdisc_destroy(q->qdisc);
204 q->qdisc = child;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Thomas Graf6b31b282005-11-05 21:14:05 +0100207 red_set_parms(&q->parms, ctl->qth_min, ctl->qth_max, ctl->Wlog,
208 ctl->Plog, ctl->Scell_log,
Patrick McHardy1e904742008-01-22 22:11:17 -0800209 nla_data(tb[TCA_RED_STAB]));
Thomas Graf6b31b282005-11-05 21:14:05 +0100210
David S. Millerb03efcf2005-07-08 14:57:23 -0700211 if (skb_queue_empty(&sch->q))
Thomas Graf6b31b282005-11-05 21:14:05 +0100212 red_end_of_idle_period(&q->parms);
Thomas Grafdba051f2005-11-05 21:14:08 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sch_tree_unlock(sch);
215 return 0;
216}
217
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000218static int red_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800220 struct red_sched_data *q = qdisc_priv(sch);
221
222 q->qdisc = &noop_qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return red_change(sch, opt);
224}
225
226static int red_dump(struct Qdisc *sch, struct sk_buff *skb)
227{
228 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800229 struct nlattr *opts = NULL;
Thomas Graf6b31b282005-11-05 21:14:05 +0100230 struct tc_red_qopt opt = {
231 .limit = q->limit,
232 .flags = q->flags,
233 .qth_min = q->parms.qth_min >> q->parms.Wlog,
234 .qth_max = q->parms.qth_max >> q->parms.Wlog,
235 .Wlog = q->parms.Wlog,
236 .Plog = q->parms.Plog,
237 .Scell_log = q->parms.Scell_log,
238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric Dumazet0dfb33a2011-01-03 08:11:38 +0000240 sch->qstats.backlog = q->qdisc->qstats.backlog;
Patrick McHardy1e904742008-01-22 22:11:17 -0800241 opts = nla_nest_start(skb, TCA_OPTIONS);
242 if (opts == NULL)
243 goto nla_put_failure;
244 NLA_PUT(skb, TCA_RED_PARMS, sizeof(opt), &opt);
245 return nla_nest_end(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Patrick McHardy1e904742008-01-22 22:11:17 -0800247nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700248 nla_nest_cancel(skb, opts);
249 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static int red_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
253{
254 struct red_sched_data *q = qdisc_priv(sch);
Thomas Graf6b31b282005-11-05 21:14:05 +0100255 struct tc_red_xstats st = {
256 .early = q->stats.prob_drop + q->stats.forced_drop,
257 .pdrop = q->stats.pdrop,
258 .other = q->stats.other,
259 .marked = q->stats.prob_mark + q->stats.forced_mark,
260 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Thomas Graf6b31b282005-11-05 21:14:05 +0100262 return gnet_stats_copy_app(d, &st, sizeof(st));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800265static int red_dump_class(struct Qdisc *sch, unsigned long cl,
266 struct sk_buff *skb, struct tcmsg *tcm)
267{
268 struct red_sched_data *q = qdisc_priv(sch);
269
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800270 tcm->tcm_handle |= TC_H_MIN(1);
271 tcm->tcm_info = q->qdisc->handle;
272 return 0;
273}
274
275static int red_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
276 struct Qdisc **old)
277{
278 struct red_sched_data *q = qdisc_priv(sch);
279
280 if (new == NULL)
281 new = &noop_qdisc;
282
283 sch_tree_lock(sch);
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800284 *old = q->qdisc;
285 q->qdisc = new;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800286 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800287 qdisc_reset(*old);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800288 sch_tree_unlock(sch);
289 return 0;
290}
291
292static struct Qdisc *red_leaf(struct Qdisc *sch, unsigned long arg)
293{
294 struct red_sched_data *q = qdisc_priv(sch);
295 return q->qdisc;
296}
297
298static unsigned long red_get(struct Qdisc *sch, u32 classid)
299{
300 return 1;
301}
302
303static void red_put(struct Qdisc *sch, unsigned long arg)
304{
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800305}
306
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800307static void red_walk(struct Qdisc *sch, struct qdisc_walker *walker)
308{
309 if (!walker->stop) {
310 if (walker->count >= walker->skip)
311 if (walker->fn(sch, 1, walker) < 0) {
312 walker->stop = 1;
313 return;
314 }
315 walker->count++;
316 }
317}
318
Eric Dumazet20fea082007-11-14 01:44:41 -0800319static const struct Qdisc_class_ops red_class_ops = {
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800320 .graft = red_graft,
321 .leaf = red_leaf,
322 .get = red_get,
323 .put = red_put,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800324 .walk = red_walk,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800325 .dump = red_dump_class,
326};
327
Eric Dumazet20fea082007-11-14 01:44:41 -0800328static struct Qdisc_ops red_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .id = "red",
330 .priv_size = sizeof(struct red_sched_data),
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800331 .cl_ops = &red_class_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .enqueue = red_enqueue,
333 .dequeue = red_dequeue,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700334 .peek = red_peek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 .drop = red_drop,
336 .init = red_init,
337 .reset = red_reset,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800338 .destroy = red_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 .change = red_change,
340 .dump = red_dump,
341 .dump_stats = red_dump_stats,
342 .owner = THIS_MODULE,
343};
344
345static int __init red_module_init(void)
346{
347 return register_qdisc(&red_qdisc_ops);
348}
Thomas Grafdba051f2005-11-05 21:14:08 +0100349
350static void __exit red_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unregister_qdisc(&red_qdisc_ops);
353}
Thomas Grafdba051f2005-11-05 21:14:08 +0100354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355module_init(red_module_init)
356module_exit(red_module_exit)
Thomas Grafdba051f2005-11-05 21:14:08 +0100357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358MODULE_LICENSE("GPL");