Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 12 | * J Hadi Salim 980914: computation fixes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Alexey Makarenko <makar@phoenix.kharkov.ua> 990814: qave on idle link was calculated incorrectly. |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 14 | * J Hadi Salim 980816: ECN support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <net/pkt_sched.h> |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 22 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/inet_ecn.h> |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 24 | #include <net/red.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 27 | /* Parameters, settable by user: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | ----------------------------- |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 40 | struct red_sched_data { |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 41 | u32 limit; /* HARD maximal queue length */ |
| 42 | unsigned char flags; |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 43 | struct timer_list adapt_timer; |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 44 | struct Qdisc *sch; |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 45 | struct red_parms parms; |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 46 | struct red_vars vars; |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 47 | struct red_stats stats; |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 48 | struct Qdisc *qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 51 | static inline int red_use_ecn(struct red_sched_data *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 53 | return q->flags & TC_RED_ECN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Thomas Graf | bdc450a | 2005-11-05 21:14:28 +0100 | [diff] [blame] | 56 | static inline int red_use_harddrop(struct red_sched_data *q) |
| 57 | { |
| 58 | return q->flags & TC_RED_HARDDROP; |
| 59 | } |
| 60 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 61 | static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
| 62 | struct sk_buff **to_free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 65 | struct Qdisc *child = q->qdisc; |
| 66 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 68 | q->vars.qavg = red_calc_qavg(&q->parms, |
| 69 | &q->vars, |
| 70 | child->qstats.backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 72 | if (red_is_idling(&q->vars)) |
| 73 | red_end_of_idle_period(&q->vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 75 | switch (red_action(&q->parms, &q->vars, q->vars.qavg)) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 76 | case RED_DONT_MARK: |
| 77 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 79 | case RED_PROB_MARK: |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 80 | qdisc_qstats_overlimit(sch); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 81 | if (!red_use_ecn(q) || !INET_ECN_set_ce(skb)) { |
| 82 | q->stats.prob_drop++; |
| 83 | goto congestion_drop; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 86 | q->stats.prob_mark++; |
| 87 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 89 | case RED_HARD_MARK: |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 90 | qdisc_qstats_overlimit(sch); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 91 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 97 | q->stats.forced_mark++; |
| 98 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 101 | ret = qdisc_enqueue(skb, child, to_free); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 102 | if (likely(ret == NET_XMIT_SUCCESS)) { |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 103 | qdisc_qstats_backlog_inc(sch, skb); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 104 | sch->q.qlen++; |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 105 | } else if (net_xmit_drop_count(ret)) { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 106 | q->stats.pdrop++; |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 107 | qdisc_qstats_drop(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 108 | } |
| 109 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 111 | congestion_drop: |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 112 | qdisc_drop(skb, sch, to_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return NET_XMIT_CN; |
| 114 | } |
| 115 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 116 | static struct sk_buff *red_dequeue(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct sk_buff *skb; |
| 119 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 120 | struct Qdisc *child = q->qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 122 | skb = child->dequeue(child); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 123 | if (skb) { |
| 124 | qdisc_bstats_update(sch, skb); |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 125 | qdisc_qstats_backlog_dec(sch, skb); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 126 | sch->q.qlen--; |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 127 | } else { |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 128 | if (!red_is_idling(&q->vars)) |
| 129 | red_start_of_idle_period(&q->vars); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 130 | } |
Thomas Graf | 9e178ff | 2005-11-05 21:14:06 +0100 | [diff] [blame] | 131 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 134 | static struct sk_buff *red_peek(struct Qdisc *sch) |
Jarek Poplawski | 8e3af97 | 2008-10-31 00:45:55 -0700 | [diff] [blame] | 135 | { |
| 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 Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 142 | static void red_reset(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct red_sched_data *q = qdisc_priv(sch); |
| 145 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 146 | qdisc_reset(q->qdisc); |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 147 | sch->qstats.backlog = 0; |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 148 | sch->q.qlen = 0; |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 149 | red_restart(&q->vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 152 | static 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; |
| 169 | opt.set.is_ecn = red_use_ecn(q); |
Jakub Kicinski | 416ef9b | 2018-01-14 20:01:26 -0800 | [diff] [blame] | 170 | opt.set.qstats = &sch->qstats; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 171 | } else { |
| 172 | opt.command = TC_RED_DESTROY; |
| 173 | } |
| 174 | |
Nogah Frankel | 8234af2 | 2017-12-25 10:51:41 +0200 | [diff] [blame] | 175 | return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED, &opt); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 178 | static void red_destroy(struct Qdisc *sch) |
| 179 | { |
| 180 | struct red_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 181 | |
| 182 | del_timer_sync(&q->adapt_timer); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 183 | red_offload(sch, false); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 184 | qdisc_destroy(q->qdisc); |
| 185 | } |
| 186 | |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 187 | static const struct nla_policy red_policy[TCA_RED_MAX + 1] = { |
| 188 | [TCA_RED_PARMS] = { .len = sizeof(struct tc_red_qopt) }, |
| 189 | [TCA_RED_STAB] = { .len = RED_STAB_SIZE }, |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 190 | [TCA_RED_MAX_P] = { .type = NLA_U32 }, |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
Alexander Aring | 2030721 | 2017-12-20 12:35:14 -0500 | [diff] [blame] | 193 | static int red_change(struct Qdisc *sch, struct nlattr *opt, |
| 194 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 197 | struct nlattr *tb[TCA_RED_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct tc_red_qopt *ctl; |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 199 | struct Qdisc *child = NULL; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 200 | int err; |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 201 | u32 max_P; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 203 | if (opt == NULL) |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 206 | err = nla_parse_nested(tb, TCA_RED_MAX, opt, red_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 207 | if (err < 0) |
| 208 | return err; |
| 209 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 210 | if (tb[TCA_RED_PARMS] == NULL || |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 211 | tb[TCA_RED_STAB] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 214 | max_P = tb[TCA_RED_MAX_P] ? nla_get_u32(tb[TCA_RED_MAX_P]) : 0; |
| 215 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 216 | ctl = nla_data(tb[TCA_RED_PARMS]); |
Nogah Frankel | 8afa10c | 2017-12-04 13:31:11 +0200 | [diff] [blame] | 217 | if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog)) |
| 218 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 220 | if (ctl->limit > 0) { |
Alexander Aring | a38a988 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 221 | child = fifo_create_dflt(sch, &bfifo_qdisc_ops, ctl->limit, |
| 222 | extack); |
Patrick McHardy | fb0305c | 2008-07-05 23:40:21 -0700 | [diff] [blame] | 223 | if (IS_ERR(child)) |
| 224 | return PTR_ERR(child); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 227 | if (child != &noop_qdisc) |
| 228 | qdisc_hash_add(child, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | sch_tree_lock(sch); |
| 230 | q->flags = ctl->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | q->limit = ctl->limit; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 232 | if (child) { |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 233 | qdisc_tree_reduce_backlog(q->qdisc, q->qdisc->q.qlen, |
| 234 | q->qdisc->qstats.backlog); |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 235 | qdisc_destroy(q->qdisc); |
| 236 | q->qdisc = child; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 239 | red_set_parms(&q->parms, |
| 240 | ctl->qth_min, ctl->qth_max, ctl->Wlog, |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 241 | ctl->Plog, ctl->Scell_log, |
| 242 | nla_data(tb[TCA_RED_STAB]), |
| 243 | max_P); |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 244 | red_set_vars(&q->vars); |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 245 | |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 246 | del_timer(&q->adapt_timer); |
| 247 | if (ctl->flags & TC_RED_ADAPTATIVE) |
| 248 | mod_timer(&q->adapt_timer, jiffies + HZ/2); |
| 249 | |
Eric Dumazet | 1ee5fa1 | 2011-12-01 11:06:34 +0000 | [diff] [blame] | 250 | if (!q->qdisc->q.qlen) |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 251 | red_start_of_idle_period(&q->vars); |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | sch_tree_unlock(sch); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 254 | red_offload(sch, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 258 | static inline void red_adaptative_timer(struct timer_list *t) |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 259 | { |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 260 | struct red_sched_data *q = from_timer(q, t, adapt_timer); |
| 261 | struct Qdisc *sch = q->sch; |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 262 | spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch)); |
| 263 | |
| 264 | spin_lock(root_lock); |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 265 | red_adaptative_algo(&q->parms, &q->vars); |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 266 | mod_timer(&q->adapt_timer, jiffies + HZ/2); |
| 267 | spin_unlock(root_lock); |
| 268 | } |
| 269 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 270 | static int red_init(struct Qdisc *sch, struct nlattr *opt, |
| 271 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 273 | struct red_sched_data *q = qdisc_priv(sch); |
| 274 | |
| 275 | q->qdisc = &noop_qdisc; |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 276 | q->sch = sch; |
| 277 | timer_setup(&q->adapt_timer, red_adaptative_timer, 0); |
Alexander Aring | 2030721 | 2017-12-20 12:35:14 -0500 | [diff] [blame] | 278 | return red_change(sch, opt, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yuval Mintz | 428a68a | 2017-12-14 15:54:30 +0200 | [diff] [blame] | 281 | static int red_dump_offload_stats(struct Qdisc *sch, struct tc_red_qopt *opt) |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 282 | { |
| 283 | struct net_device *dev = qdisc_dev(sch); |
| 284 | struct tc_red_qopt_offload hw_stats = { |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 285 | .command = TC_RED_STATS, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 286 | .handle = sch->handle, |
| 287 | .parent = sch->parent, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 288 | { |
| 289 | .stats.bstats = &sch->bstats, |
| 290 | .stats.qstats = &sch->qstats, |
| 291 | }, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 292 | }; |
Nogah Frankel | 8234af2 | 2017-12-25 10:51:41 +0200 | [diff] [blame] | 293 | int err; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 294 | |
Nogah Frankel | 8234af2 | 2017-12-25 10:51:41 +0200 | [diff] [blame] | 295 | sch->flags &= ~TCQ_F_OFFLOADED; |
| 296 | |
| 297 | if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | |
Nogah Frankel | 8234af2 | 2017-12-25 10:51:41 +0200 | [diff] [blame] | 300 | err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED, |
| 301 | &hw_stats); |
| 302 | if (err == -EOPNOTSUPP) |
| 303 | return 0; |
| 304 | |
| 305 | if (!err) |
| 306 | sch->flags |= TCQ_F_OFFLOADED; |
| 307 | |
| 308 | return err; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | static int red_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 312 | { |
| 313 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 314 | struct nlattr *opts = NULL; |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 315 | struct tc_red_qopt opt = { |
| 316 | .limit = q->limit, |
| 317 | .flags = q->flags, |
| 318 | .qth_min = q->parms.qth_min >> q->parms.Wlog, |
| 319 | .qth_max = q->parms.qth_max >> q->parms.Wlog, |
| 320 | .Wlog = q->parms.Wlog, |
| 321 | .Plog = q->parms.Plog, |
| 322 | .Scell_log = q->parms.Scell_log, |
| 323 | }; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 324 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Yuval Mintz | 428a68a | 2017-12-14 15:54:30 +0200 | [diff] [blame] | 326 | err = red_dump_offload_stats(sch, &opt); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 327 | if (err) |
| 328 | goto nla_put_failure; |
| 329 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 330 | opts = nla_nest_start(skb, TCA_OPTIONS); |
| 331 | if (opts == NULL) |
| 332 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 333 | if (nla_put(skb, TCA_RED_PARMS, sizeof(opt), &opt) || |
| 334 | nla_put_u32(skb, TCA_RED_MAX_P, q->parms.max_P)) |
| 335 | goto nla_put_failure; |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 336 | return nla_nest_end(skb, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 338 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 339 | nla_nest_cancel(skb, opts); |
| 340 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int red_dump_stats(struct Qdisc *sch, struct gnet_dump *d) |
| 344 | { |
| 345 | struct red_sched_data *q = qdisc_priv(sch); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 346 | struct net_device *dev = qdisc_dev(sch); |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 347 | struct tc_red_xstats st = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Yuval Mintz | 428a68a | 2017-12-14 15:54:30 +0200 | [diff] [blame] | 349 | if (sch->flags & TCQ_F_OFFLOADED) { |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 350 | struct tc_red_qopt_offload hw_stats_request = { |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 351 | .command = TC_RED_XSTATS, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 352 | .handle = sch->handle, |
| 353 | .parent = sch->parent, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 354 | { |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 355 | .xstats = &q->stats, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 356 | }, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 357 | }; |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 358 | dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED, |
| 359 | &hw_stats_request); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 360 | } |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 361 | st.early = q->stats.prob_drop + q->stats.forced_drop; |
| 362 | st.pdrop = q->stats.pdrop; |
| 363 | st.other = q->stats.other; |
| 364 | st.marked = q->stats.prob_mark + q->stats.forced_mark; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 365 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 366 | return gnet_stats_copy_app(d, &st, sizeof(st)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 369 | static int red_dump_class(struct Qdisc *sch, unsigned long cl, |
| 370 | struct sk_buff *skb, struct tcmsg *tcm) |
| 371 | { |
| 372 | struct red_sched_data *q = qdisc_priv(sch); |
| 373 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 374 | tcm->tcm_handle |= TC_H_MIN(1); |
| 375 | tcm->tcm_info = q->qdisc->handle; |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int red_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, |
Alexander Aring | 653d6fd | 2017-12-20 12:35:17 -0500 | [diff] [blame] | 380 | struct Qdisc **old, struct netlink_ext_ack *extack) |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 381 | { |
| 382 | struct red_sched_data *q = qdisc_priv(sch); |
| 383 | |
| 384 | if (new == NULL) |
| 385 | new = &noop_qdisc; |
| 386 | |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 387 | *old = qdisc_replace(sch, new, &q->qdisc); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static struct Qdisc *red_leaf(struct Qdisc *sch, unsigned long arg) |
| 392 | { |
| 393 | struct red_sched_data *q = qdisc_priv(sch); |
| 394 | return q->qdisc; |
| 395 | } |
| 396 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 397 | static unsigned long red_find(struct Qdisc *sch, u32 classid) |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 398 | { |
| 399 | return 1; |
| 400 | } |
| 401 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 402 | static void red_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
| 403 | { |
| 404 | if (!walker->stop) { |
| 405 | if (walker->count >= walker->skip) |
| 406 | if (walker->fn(sch, 1, walker) < 0) { |
| 407 | walker->stop = 1; |
| 408 | return; |
| 409 | } |
| 410 | walker->count++; |
| 411 | } |
| 412 | } |
| 413 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 414 | static const struct Qdisc_class_ops red_class_ops = { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 415 | .graft = red_graft, |
| 416 | .leaf = red_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 417 | .find = red_find, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 418 | .walk = red_walk, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 419 | .dump = red_dump_class, |
| 420 | }; |
| 421 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 422 | static struct Qdisc_ops red_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | .id = "red", |
| 424 | .priv_size = sizeof(struct red_sched_data), |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 425 | .cl_ops = &red_class_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | .enqueue = red_enqueue, |
| 427 | .dequeue = red_dequeue, |
Jarek Poplawski | 8e3af97 | 2008-10-31 00:45:55 -0700 | [diff] [blame] | 428 | .peek = red_peek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | .init = red_init, |
| 430 | .reset = red_reset, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 431 | .destroy = red_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | .change = red_change, |
| 433 | .dump = red_dump, |
| 434 | .dump_stats = red_dump_stats, |
| 435 | .owner = THIS_MODULE, |
| 436 | }; |
| 437 | |
| 438 | static int __init red_module_init(void) |
| 439 | { |
| 440 | return register_qdisc(&red_qdisc_ops); |
| 441 | } |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 442 | |
| 443 | static void __exit red_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | unregister_qdisc(&red_qdisc_ops); |
| 446 | } |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | module_init(red_module_init) |
| 449 | module_exit(red_module_exit) |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | MODULE_LICENSE("GPL"); |