blob: 9df9942340eaaa30ed38fc3345649f287a373bee [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>
Nogah Frankel602f3ba2017-11-06 07:23:41 +010022#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/inet_ecn.h>
Thomas Graf6b31b282005-11-05 21:14:05 +010024#include <net/red.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26
Thomas Graf6b31b282005-11-05 21:14:05 +010027/* Parameters, settable by user:
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 -----------------------------
29
30 limit - bytes (must be > qth_max + burst)
31
32 Hard limit on queue length, should be chosen >qth_max
33 to allow packet bursts. This parameter does not
34 affect the algorithms behaviour and can be chosen
35 arbitrarily high (well, less than ram size)
36 Really, this limit will never be reached
37 if RED works correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Eric Dumazetcc7ec452011-01-19 19:26:56 +000040struct red_sched_data {
Thomas Graf6b31b282005-11-05 21:14:05 +010041 u32 limit; /* HARD maximal queue length */
42 unsigned char flags;
Eric Dumazet8af2a212011-12-08 06:06:03 +000043 struct timer_list adapt_timer;
Kees Cookcdeabbb2017-10-16 17:29:17 -070044 struct Qdisc *sch;
Thomas Graf6b31b282005-11-05 21:14:05 +010045 struct red_parms parms;
Eric Dumazeteeca6682012-01-05 02:25:16 +000046 struct red_vars vars;
Thomas Graf6b31b282005-11-05 21:14:05 +010047 struct red_stats stats;
Patrick McHardyf38c39d2006-03-20 19:20:44 -080048 struct Qdisc *qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Thomas Graf6b31b282005-11-05 21:14:05 +010051static inline int red_use_ecn(struct red_sched_data *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Thomas Graf6b31b282005-11-05 21:14:05 +010053 return q->flags & TC_RED_ECN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Thomas Grafbdc450a2005-11-05 21:14:28 +010056static inline int red_use_harddrop(struct red_sched_data *q)
57{
58 return q->flags & TC_RED_HARDDROP;
59}
60
Eric Dumazet520ac302016-06-21 23:16:49 -070061static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch,
62 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -080065 struct Qdisc *child = q->qdisc;
66 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Dumazeteeca6682012-01-05 02:25:16 +000068 q->vars.qavg = red_calc_qavg(&q->parms,
69 &q->vars,
70 child->qstats.backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Dumazeteeca6682012-01-05 02:25:16 +000072 if (red_is_idling(&q->vars))
73 red_end_of_idle_period(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazeteeca6682012-01-05 02:25:16 +000075 switch (red_action(&q->parms, &q->vars, q->vars.qavg)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +000076 case RED_DONT_MARK:
77 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Eric Dumazetcc7ec452011-01-19 19:26:56 +000079 case RED_PROB_MARK:
John Fastabend25331d62014-09-28 11:53:29 -070080 qdisc_qstats_overlimit(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +000081 if (!red_use_ecn(q) || !INET_ECN_set_ce(skb)) {
82 q->stats.prob_drop++;
83 goto congestion_drop;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Eric Dumazetcc7ec452011-01-19 19:26:56 +000086 q->stats.prob_mark++;
87 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric Dumazetcc7ec452011-01-19 19:26:56 +000089 case RED_HARD_MARK:
John Fastabend25331d62014-09-28 11:53:29 -070090 qdisc_qstats_overlimit(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +000091 if (red_use_harddrop(q) || !red_use_ecn(q) ||
92 !INET_ECN_set_ce(skb)) {
93 q->stats.forced_drop++;
94 goto congestion_drop;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric Dumazetcc7ec452011-01-19 19:26:56 +000097 q->stats.forced_mark++;
98 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Eric Dumazet520ac302016-06-21 23:16:49 -0700101 ret = qdisc_enqueue(skb, child, to_free);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800102 if (likely(ret == NET_XMIT_SUCCESS)) {
WANG Congd7f4f332016-06-01 16:15:18 -0700103 qdisc_qstats_backlog_inc(sch, skb);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800104 sch->q.qlen++;
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700105 } else if (net_xmit_drop_count(ret)) {
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800106 q->stats.pdrop++;
John Fastabend25331d62014-09-28 11:53:29 -0700107 qdisc_qstats_drop(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800108 }
109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Thomas Graf6b31b282005-11-05 21:14:05 +0100111congestion_drop:
Eric Dumazet520ac302016-06-21 23:16:49 -0700112 qdisc_drop(skb, sch, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return NET_XMIT_CN;
114}
115
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000116static struct sk_buff *red_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct sk_buff *skb;
119 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800120 struct Qdisc *child = q->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800122 skb = child->dequeue(child);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800123 if (skb) {
124 qdisc_bstats_update(sch, skb);
WANG Congd7f4f332016-06-01 16:15:18 -0700125 qdisc_qstats_backlog_dec(sch, skb);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800126 sch->q.qlen--;
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800127 } else {
Eric Dumazeteeca6682012-01-05 02:25:16 +0000128 if (!red_is_idling(&q->vars))
129 red_start_of_idle_period(&q->vars);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800130 }
Thomas Graf9e178ff2005-11-05 21:14:06 +0100131 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000134static struct sk_buff *red_peek(struct Qdisc *sch)
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700135{
136 struct red_sched_data *q = qdisc_priv(sch);
137 struct Qdisc *child = q->qdisc;
138
139 return child->ops->peek(child);
140}
141
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000142static void red_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct red_sched_data *q = qdisc_priv(sch);
145
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800146 qdisc_reset(q->qdisc);
WANG Congd7f4f332016-06-01 16:15:18 -0700147 sch->qstats.backlog = 0;
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800148 sch->q.qlen = 0;
Eric Dumazeteeca6682012-01-05 02:25:16 +0000149 red_restart(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100152static int red_offload(struct Qdisc *sch, bool enable)
153{
154 struct red_sched_data *q = qdisc_priv(sch);
155 struct net_device *dev = qdisc_dev(sch);
156 struct tc_red_qopt_offload opt = {
157 .handle = sch->handle,
158 .parent = sch->parent,
159 };
160
161 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
162 return -EOPNOTSUPP;
163
164 if (enable) {
165 opt.command = TC_RED_REPLACE;
166 opt.set.min = q->parms.qth_min >> q->parms.Wlog;
167 opt.set.max = q->parms.qth_max >> q->parms.Wlog;
168 opt.set.probability = q->parms.max_P;
Jakub Kicinskic0b74902018-11-12 14:58:16 -0800169 opt.set.limit = q->limit;
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100170 opt.set.is_ecn = red_use_ecn(q);
Jakub Kicinski190852a2018-11-08 19:50:38 -0800171 opt.set.is_harddrop = red_use_harddrop(q);
Jakub Kicinski416ef9b2018-01-14 20:01:26 -0800172 opt.set.qstats = &sch->qstats;
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100173 } else {
174 opt.command = TC_RED_DESTROY;
175 }
176
Nogah Frankel8234af22017-12-25 10:51:41 +0200177 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED, &opt);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100178}
179
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800180static void red_destroy(struct Qdisc *sch)
181{
182 struct red_sched_data *q = qdisc_priv(sch);
Eric Dumazet8af2a212011-12-08 06:06:03 +0000183
184 del_timer_sync(&q->adapt_timer);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100185 red_offload(sch, false);
Vlad Buslov86bd4462018-09-24 19:22:50 +0300186 qdisc_put(q->qdisc);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800187}
188
Patrick McHardy27a34212008-01-23 20:35:39 -0800189static const struct nla_policy red_policy[TCA_RED_MAX + 1] = {
190 [TCA_RED_PARMS] = { .len = sizeof(struct tc_red_qopt) },
191 [TCA_RED_STAB] = { .len = RED_STAB_SIZE },
Eric Dumazeta73ed262011-12-09 02:46:45 +0000192 [TCA_RED_MAX_P] = { .type = NLA_U32 },
Patrick McHardy27a34212008-01-23 20:35:39 -0800193};
194
Alexander Aring20307212017-12-20 12:35:14 -0500195static int red_change(struct Qdisc *sch, struct nlattr *opt,
196 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Jakub Kicinski0c8d13a2018-11-07 17:33:39 -0800198 struct Qdisc *old_child = NULL, *child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800200 struct nlattr *tb[TCA_RED_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct tc_red_qopt *ctl;
Patrick McHardycee63722008-01-23 20:33:32 -0800202 int err;
Eric Dumazeta73ed262011-12-09 02:46:45 +0000203 u32 max_P;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Patrick McHardycee63722008-01-23 20:33:32 -0800205 if (opt == NULL)
Thomas Grafdba051f2005-11-05 21:14:08 +0100206 return -EINVAL;
207
Johannes Bergfceb6432017-04-12 14:34:07 +0200208 err = nla_parse_nested(tb, TCA_RED_MAX, opt, red_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800209 if (err < 0)
210 return err;
211
Patrick McHardy1e904742008-01-22 22:11:17 -0800212 if (tb[TCA_RED_PARMS] == NULL ||
Patrick McHardy27a34212008-01-23 20:35:39 -0800213 tb[TCA_RED_STAB] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215
Eric Dumazeta73ed262011-12-09 02:46:45 +0000216 max_P = tb[TCA_RED_MAX_P] ? nla_get_u32(tb[TCA_RED_MAX_P]) : 0;
217
Patrick McHardy1e904742008-01-22 22:11:17 -0800218 ctl = nla_data(tb[TCA_RED_PARMS]);
Nogah Frankel8afa10c2017-12-04 13:31:11 +0200219 if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog))
220 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800222 if (ctl->limit > 0) {
Alexander Aringa38a9882017-12-20 12:35:21 -0500223 child = fifo_create_dflt(sch, &bfifo_qdisc_ops, ctl->limit,
224 extack);
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700225 if (IS_ERR(child))
226 return PTR_ERR(child);
Paolo Abeni44a63b132018-05-18 14:51:44 +0200227
228 /* child is fifo, no need to check for noop_qdisc */
229 qdisc_hash_add(child, true);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sch_tree_lock(sch);
233 q->flags = ctl->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 q->limit = ctl->limit;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800235 if (child) {
WANG Cong2ccccf52016-02-25 14:55:01 -0800236 qdisc_tree_reduce_backlog(q->qdisc, q->qdisc->q.qlen,
237 q->qdisc->qstats.backlog);
Jakub Kicinski0c8d13a2018-11-07 17:33:39 -0800238 old_child = q->qdisc;
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800239 q->qdisc = child;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazeteeca6682012-01-05 02:25:16 +0000242 red_set_parms(&q->parms,
243 ctl->qth_min, ctl->qth_max, ctl->Wlog,
Eric Dumazeta73ed262011-12-09 02:46:45 +0000244 ctl->Plog, ctl->Scell_log,
245 nla_data(tb[TCA_RED_STAB]),
246 max_P);
Eric Dumazeteeca6682012-01-05 02:25:16 +0000247 red_set_vars(&q->vars);
Thomas Graf6b31b282005-11-05 21:14:05 +0100248
Eric Dumazet8af2a212011-12-08 06:06:03 +0000249 del_timer(&q->adapt_timer);
250 if (ctl->flags & TC_RED_ADAPTATIVE)
251 mod_timer(&q->adapt_timer, jiffies + HZ/2);
252
Eric Dumazet1ee5fa12011-12-01 11:06:34 +0000253 if (!q->qdisc->q.qlen)
Eric Dumazeteeca6682012-01-05 02:25:16 +0000254 red_start_of_idle_period(&q->vars);
Thomas Grafdba051f2005-11-05 21:14:08 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 sch_tree_unlock(sch);
Jakub Kicinski0c8d13a2018-11-07 17:33:39 -0800257
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100258 red_offload(sch, true);
Jakub Kicinski0c8d13a2018-11-07 17:33:39 -0800259
260 if (old_child)
261 qdisc_put(old_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
Kees Cookcdeabbb2017-10-16 17:29:17 -0700265static inline void red_adaptative_timer(struct timer_list *t)
Eric Dumazet8af2a212011-12-08 06:06:03 +0000266{
Kees Cookcdeabbb2017-10-16 17:29:17 -0700267 struct red_sched_data *q = from_timer(q, t, adapt_timer);
268 struct Qdisc *sch = q->sch;
Eric Dumazet8af2a212011-12-08 06:06:03 +0000269 spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
270
271 spin_lock(root_lock);
Eric Dumazeteeca6682012-01-05 02:25:16 +0000272 red_adaptative_algo(&q->parms, &q->vars);
Eric Dumazet8af2a212011-12-08 06:06:03 +0000273 mod_timer(&q->adapt_timer, jiffies + HZ/2);
274 spin_unlock(root_lock);
275}
276
Alexander Aringe63d7df2017-12-20 12:35:13 -0500277static int red_init(struct Qdisc *sch, struct nlattr *opt,
278 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800280 struct red_sched_data *q = qdisc_priv(sch);
281
282 q->qdisc = &noop_qdisc;
Kees Cookcdeabbb2017-10-16 17:29:17 -0700283 q->sch = sch;
284 timer_setup(&q->adapt_timer, red_adaptative_timer, 0);
Alexander Aring20307212017-12-20 12:35:14 -0500285 return red_change(sch, opt, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Jakub Kicinskidad54c02018-11-07 17:33:35 -0800288static int red_dump_offload_stats(struct Qdisc *sch)
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100289{
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100290 struct tc_red_qopt_offload hw_stats = {
Andrew Mortonee9d3422017-11-10 15:09:53 -0800291 .command = TC_RED_STATS,
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100292 .handle = sch->handle,
293 .parent = sch->parent,
Andrew Mortonee9d3422017-11-10 15:09:53 -0800294 {
295 .stats.bstats = &sch->bstats,
296 .stats.qstats = &sch->qstats,
297 },
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100298 };
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100299
Jakub Kicinskib5928432018-11-07 17:33:34 -0800300 return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_RED, &hw_stats);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static int red_dump(struct Qdisc *sch, struct sk_buff *skb)
304{
305 struct red_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800306 struct nlattr *opts = NULL;
Thomas Graf6b31b282005-11-05 21:14:05 +0100307 struct tc_red_qopt opt = {
308 .limit = q->limit,
309 .flags = q->flags,
310 .qth_min = q->parms.qth_min >> q->parms.Wlog,
311 .qth_max = q->parms.qth_max >> q->parms.Wlog,
312 .Wlog = q->parms.Wlog,
313 .Plog = q->parms.Plog,
314 .Scell_log = q->parms.Scell_log,
315 };
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100316 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jakub Kicinskidad54c02018-11-07 17:33:35 -0800318 err = red_dump_offload_stats(sch);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100319 if (err)
320 goto nla_put_failure;
321
Patrick McHardy1e904742008-01-22 22:11:17 -0800322 opts = nla_nest_start(skb, TCA_OPTIONS);
323 if (opts == NULL)
324 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400325 if (nla_put(skb, TCA_RED_PARMS, sizeof(opt), &opt) ||
326 nla_put_u32(skb, TCA_RED_MAX_P, q->parms.max_P))
327 goto nla_put_failure;
Patrick McHardy1e904742008-01-22 22:11:17 -0800328 return nla_nest_end(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Patrick McHardy1e904742008-01-22 22:11:17 -0800330nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700331 nla_nest_cancel(skb, opts);
332 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static int red_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
336{
337 struct red_sched_data *q = qdisc_priv(sch);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100338 struct net_device *dev = qdisc_dev(sch);
Nogah Frankelf8253df2018-01-10 14:59:59 +0100339 struct tc_red_xstats st = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Yuval Mintz428a68a2017-12-14 15:54:30 +0200341 if (sch->flags & TCQ_F_OFFLOADED) {
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100342 struct tc_red_qopt_offload hw_stats_request = {
Andrew Mortonee9d3422017-11-10 15:09:53 -0800343 .command = TC_RED_XSTATS,
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100344 .handle = sch->handle,
345 .parent = sch->parent,
Andrew Mortonee9d3422017-11-10 15:09:53 -0800346 {
Nogah Frankelf8253df2018-01-10 14:59:59 +0100347 .xstats = &q->stats,
Andrew Mortonee9d3422017-11-10 15:09:53 -0800348 },
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100349 };
Nogah Frankelf8253df2018-01-10 14:59:59 +0100350 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED,
351 &hw_stats_request);
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100352 }
Nogah Frankelf8253df2018-01-10 14:59:59 +0100353 st.early = q->stats.prob_drop + q->stats.forced_drop;
354 st.pdrop = q->stats.pdrop;
355 st.other = q->stats.other;
356 st.marked = q->stats.prob_mark + q->stats.forced_mark;
Nogah Frankel602f3ba2017-11-06 07:23:41 +0100357
Thomas Graf6b31b282005-11-05 21:14:05 +0100358 return gnet_stats_copy_app(d, &st, sizeof(st));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800361static int red_dump_class(struct Qdisc *sch, unsigned long cl,
362 struct sk_buff *skb, struct tcmsg *tcm)
363{
364 struct red_sched_data *q = qdisc_priv(sch);
365
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800366 tcm->tcm_handle |= TC_H_MIN(1);
367 tcm->tcm_info = q->qdisc->handle;
368 return 0;
369}
370
Jakub Kicinskibf2a7522018-11-12 14:58:13 -0800371static void red_graft_offload(struct Qdisc *sch,
372 struct Qdisc *new, struct Qdisc *old,
373 struct netlink_ext_ack *extack)
374{
375 struct tc_red_qopt_offload graft_offload = {
376 .handle = sch->handle,
377 .parent = sch->parent,
378 .child_handle = new->handle,
379 .command = TC_RED_GRAFT,
380 };
381
382 qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, old,
383 TC_SETUP_QDISC_RED, &graft_offload, extack);
384}
385
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800386static int red_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Alexander Aring653d6fd2017-12-20 12:35:17 -0500387 struct Qdisc **old, struct netlink_ext_ack *extack)
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800388{
389 struct red_sched_data *q = qdisc_priv(sch);
390
391 if (new == NULL)
392 new = &noop_qdisc;
393
WANG Cong86a79962016-02-25 14:55:00 -0800394 *old = qdisc_replace(sch, new, &q->qdisc);
Jakub Kicinskibf2a7522018-11-12 14:58:13 -0800395
396 red_graft_offload(sch, new, *old, extack);
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800397 return 0;
398}
399
400static struct Qdisc *red_leaf(struct Qdisc *sch, unsigned long arg)
401{
402 struct red_sched_data *q = qdisc_priv(sch);
403 return q->qdisc;
404}
405
WANG Cong143976c2017-08-24 16:51:29 -0700406static unsigned long red_find(struct Qdisc *sch, u32 classid)
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800407{
408 return 1;
409}
410
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800411static void red_walk(struct Qdisc *sch, struct qdisc_walker *walker)
412{
413 if (!walker->stop) {
414 if (walker->count >= walker->skip)
415 if (walker->fn(sch, 1, walker) < 0) {
416 walker->stop = 1;
417 return;
418 }
419 walker->count++;
420 }
421}
422
Eric Dumazet20fea082007-11-14 01:44:41 -0800423static const struct Qdisc_class_ops red_class_ops = {
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800424 .graft = red_graft,
425 .leaf = red_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700426 .find = red_find,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800427 .walk = red_walk,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800428 .dump = red_dump_class,
429};
430
Eric Dumazet20fea082007-11-14 01:44:41 -0800431static struct Qdisc_ops red_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .id = "red",
433 .priv_size = sizeof(struct red_sched_data),
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800434 .cl_ops = &red_class_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .enqueue = red_enqueue,
436 .dequeue = red_dequeue,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700437 .peek = red_peek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 .init = red_init,
439 .reset = red_reset,
Patrick McHardyf38c39d2006-03-20 19:20:44 -0800440 .destroy = red_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .change = red_change,
442 .dump = red_dump,
443 .dump_stats = red_dump_stats,
444 .owner = THIS_MODULE,
445};
446
447static int __init red_module_init(void)
448{
449 return register_qdisc(&red_qdisc_ops);
450}
Thomas Grafdba051f2005-11-05 21:14:08 +0100451
452static void __exit red_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 unregister_qdisc(&red_qdisc_ops);
455}
Thomas Grafdba051f2005-11-05 21:14:08 +0100456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457module_init(red_module_init)
458module_exit(red_module_exit)
Thomas Grafdba051f2005-11-05 21:14:08 +0100459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460MODULE_LICENSE("GPL");