Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/sch_red.c Random Early Detection queue. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 6 | * |
| 7 | * Changes: |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 8 | * J Hadi Salim 980914: computation fixes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * 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] | 10 | * J Hadi Salim 980816: ECN support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/pkt_sched.h> |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 18 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/inet_ecn.h> |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 20 | #include <net/red.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 23 | /* Parameters, settable by user: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | ----------------------------- |
| 25 | |
| 26 | limit - bytes (must be > qth_max + burst) |
| 27 | |
| 28 | Hard limit on queue length, should be chosen >qth_max |
| 29 | to allow packet bursts. This parameter does not |
| 30 | affect the algorithms behaviour and can be chosen |
| 31 | arbitrarily high (well, less than ram size) |
| 32 | Really, this limit will never be reached |
| 33 | if RED works correctly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 36 | struct red_sched_data { |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 37 | u32 limit; /* HARD maximal queue length */ |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 38 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 39 | unsigned char flags; |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 40 | /* Non-flags in tc_red_qopt.flags. */ |
| 41 | unsigned char userbits; |
| 42 | |
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; |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 49 | struct tcf_qevent qe_early_drop; |
| 50 | struct tcf_qevent qe_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 53 | #define TC_RED_SUPPORTED_FLAGS (TC_RED_HISTORIC_FLAGS | TC_RED_NODROP) |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 54 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 55 | static inline int red_use_ecn(struct red_sched_data *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 57 | return q->flags & TC_RED_ECN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Graf | bdc450a | 2005-11-05 21:14:28 +0100 | [diff] [blame] | 60 | static inline int red_use_harddrop(struct red_sched_data *q) |
| 61 | { |
| 62 | return q->flags & TC_RED_HARDDROP; |
| 63 | } |
| 64 | |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 65 | static int red_use_nodrop(struct red_sched_data *q) |
| 66 | { |
| 67 | return q->flags & TC_RED_NODROP; |
| 68 | } |
| 69 | |
Petr Machata | ac5c66f | 2020-07-14 20:03:08 +0300 | [diff] [blame] | 70 | static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 71 | struct sk_buff **to_free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 74 | struct Qdisc *child = q->qdisc; |
| 75 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 77 | q->vars.qavg = red_calc_qavg(&q->parms, |
| 78 | &q->vars, |
| 79 | child->qstats.backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 81 | if (red_is_idling(&q->vars)) |
| 82 | red_end_of_idle_period(&q->vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 84 | switch (red_action(&q->parms, &q->vars, q->vars.qavg)) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 85 | case RED_DONT_MARK: |
| 86 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 88 | case RED_PROB_MARK: |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 89 | qdisc_qstats_overlimit(sch); |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 90 | if (!red_use_ecn(q)) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 91 | q->stats.prob_drop++; |
| 92 | goto congestion_drop; |
| 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 95 | if (INET_ECN_set_ce(skb)) { |
| 96 | q->stats.prob_mark++; |
Petr Machata | 55f656c | 2020-07-14 20:03:07 +0300 | [diff] [blame] | 97 | skb = tcf_qevent_handle(&q->qe_mark, sch, skb, to_free, &ret); |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 98 | if (!skb) |
| 99 | return NET_XMIT_CN | ret; |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 100 | } else if (!red_use_nodrop(q)) { |
| 101 | q->stats.prob_drop++; |
| 102 | goto congestion_drop; |
| 103 | } |
| 104 | |
| 105 | /* Non-ECT packet in ECN nodrop mode: queue it. */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 106 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 108 | case RED_HARD_MARK: |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 109 | qdisc_qstats_overlimit(sch); |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 110 | if (red_use_harddrop(q) || !red_use_ecn(q)) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 111 | q->stats.forced_drop++; |
| 112 | goto congestion_drop; |
| 113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 115 | if (INET_ECN_set_ce(skb)) { |
| 116 | q->stats.forced_mark++; |
Petr Machata | 55f656c | 2020-07-14 20:03:07 +0300 | [diff] [blame] | 117 | skb = tcf_qevent_handle(&q->qe_mark, sch, skb, to_free, &ret); |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 118 | if (!skb) |
| 119 | return NET_XMIT_CN | ret; |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 120 | } else if (!red_use_nodrop(q)) { |
| 121 | q->stats.forced_drop++; |
| 122 | goto congestion_drop; |
| 123 | } |
| 124 | |
| 125 | /* Non-ECT packet in ECN nodrop mode: queue it. */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 126 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Petr Machata | ac5c66f | 2020-07-14 20:03:08 +0300 | [diff] [blame] | 129 | ret = qdisc_enqueue(skb, child, to_free); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 130 | if (likely(ret == NET_XMIT_SUCCESS)) { |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 131 | qdisc_qstats_backlog_inc(sch, skb); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 132 | sch->q.qlen++; |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 133 | } else if (net_xmit_drop_count(ret)) { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 134 | q->stats.pdrop++; |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 135 | qdisc_qstats_drop(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 136 | } |
| 137 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 139 | congestion_drop: |
Petr Machata | 55f656c | 2020-07-14 20:03:07 +0300 | [diff] [blame] | 140 | skb = tcf_qevent_handle(&q->qe_early_drop, sch, skb, to_free, &ret); |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 141 | if (!skb) |
| 142 | return NET_XMIT_CN | ret; |
| 143 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 144 | qdisc_drop(skb, sch, to_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return NET_XMIT_CN; |
| 146 | } |
| 147 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 148 | static struct sk_buff *red_dequeue(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | struct sk_buff *skb; |
| 151 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 152 | struct Qdisc *child = q->qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 154 | skb = child->dequeue(child); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 155 | if (skb) { |
| 156 | qdisc_bstats_update(sch, skb); |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 157 | qdisc_qstats_backlog_dec(sch, skb); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 158 | sch->q.qlen--; |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 159 | } else { |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 160 | if (!red_is_idling(&q->vars)) |
| 161 | red_start_of_idle_period(&q->vars); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 162 | } |
Thomas Graf | 9e178ff | 2005-11-05 21:14:06 +0100 | [diff] [blame] | 163 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 166 | static struct sk_buff *red_peek(struct Qdisc *sch) |
Jarek Poplawski | 8e3af97 | 2008-10-31 00:45:55 -0700 | [diff] [blame] | 167 | { |
| 168 | struct red_sched_data *q = qdisc_priv(sch); |
| 169 | struct Qdisc *child = q->qdisc; |
| 170 | |
| 171 | return child->ops->peek(child); |
| 172 | } |
| 173 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 174 | static void red_reset(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct red_sched_data *q = qdisc_priv(sch); |
| 177 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 178 | qdisc_reset(q->qdisc); |
WANG Cong | d7f4f33 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 179 | sch->qstats.backlog = 0; |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 180 | sch->q.qlen = 0; |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 181 | red_restart(&q->vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 184 | static int red_offload(struct Qdisc *sch, bool enable) |
| 185 | { |
| 186 | struct red_sched_data *q = qdisc_priv(sch); |
| 187 | struct net_device *dev = qdisc_dev(sch); |
| 188 | struct tc_red_qopt_offload opt = { |
| 189 | .handle = sch->handle, |
| 190 | .parent = sch->parent, |
| 191 | }; |
| 192 | |
| 193 | if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) |
| 194 | return -EOPNOTSUPP; |
| 195 | |
| 196 | if (enable) { |
| 197 | opt.command = TC_RED_REPLACE; |
| 198 | opt.set.min = q->parms.qth_min >> q->parms.Wlog; |
| 199 | opt.set.max = q->parms.qth_max >> q->parms.Wlog; |
| 200 | opt.set.probability = q->parms.max_P; |
Jakub Kicinski | c0b7490 | 2018-11-12 14:58:16 -0800 | [diff] [blame] | 201 | opt.set.limit = q->limit; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 202 | opt.set.is_ecn = red_use_ecn(q); |
Jakub Kicinski | 190852a | 2018-11-08 19:50:38 -0800 | [diff] [blame] | 203 | opt.set.is_harddrop = red_use_harddrop(q); |
Petr Machata | 0a7fad2 | 2020-03-13 01:10:57 +0200 | [diff] [blame] | 204 | opt.set.is_nodrop = red_use_nodrop(q); |
Jakub Kicinski | 416ef9b | 2018-01-14 20:01:26 -0800 | [diff] [blame] | 205 | opt.set.qstats = &sch->qstats; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 206 | } else { |
| 207 | opt.command = TC_RED_DESTROY; |
| 208 | } |
| 209 | |
Nogah Frankel | 8234af2 | 2017-12-25 10:51:41 +0200 | [diff] [blame] | 210 | 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] | 211 | } |
| 212 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 213 | static void red_destroy(struct Qdisc *sch) |
| 214 | { |
| 215 | struct red_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 216 | |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 217 | tcf_qevent_destroy(&q->qe_mark, sch); |
| 218 | tcf_qevent_destroy(&q->qe_early_drop, sch); |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 219 | del_timer_sync(&q->adapt_timer); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 220 | red_offload(sch, false); |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 221 | qdisc_put(q->qdisc); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 224 | static const struct nla_policy red_policy[TCA_RED_MAX + 1] = { |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 225 | [TCA_RED_UNSPEC] = { .strict_start_type = TCA_RED_FLAGS }, |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 226 | [TCA_RED_PARMS] = { .len = sizeof(struct tc_red_qopt) }, |
| 227 | [TCA_RED_STAB] = { .len = RED_STAB_SIZE }, |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 228 | [TCA_RED_MAX_P] = { .type = NLA_U32 }, |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 229 | [TCA_RED_FLAGS] = NLA_POLICY_BITFIELD32(TC_RED_SUPPORTED_FLAGS), |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 230 | [TCA_RED_EARLY_DROP_BLOCK] = { .type = NLA_U32 }, |
| 231 | [TCA_RED_MARK_BLOCK] = { .type = NLA_U32 }, |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 234 | static int __red_change(struct Qdisc *sch, struct nlattr **tb, |
| 235 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Jakub Kicinski | 0c8d13a | 2018-11-07 17:33:39 -0800 | [diff] [blame] | 237 | struct Qdisc *old_child = NULL, *child = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | struct red_sched_data *q = qdisc_priv(sch); |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 239 | struct nla_bitfield32 flags_bf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | struct tc_red_qopt *ctl; |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 241 | unsigned char userbits; |
| 242 | unsigned char flags; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 243 | int err; |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 244 | u32 max_P; |
Eric Dumazet | e323d86 | 2021-03-10 08:26:41 -0800 | [diff] [blame] | 245 | u8 *stab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 247 | if (tb[TCA_RED_PARMS] == NULL || |
Patrick McHardy | 27a3421 | 2008-01-23 20:35:39 -0800 | [diff] [blame] | 248 | tb[TCA_RED_STAB] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 251 | max_P = tb[TCA_RED_MAX_P] ? nla_get_u32(tb[TCA_RED_MAX_P]) : 0; |
| 252 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 253 | ctl = nla_data(tb[TCA_RED_PARMS]); |
Eric Dumazet | e323d86 | 2021-03-10 08:26:41 -0800 | [diff] [blame] | 254 | stab = nla_data(tb[TCA_RED_STAB]); |
| 255 | if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog, |
| 256 | ctl->Scell_log, stab)) |
Nogah Frankel | 8afa10c | 2017-12-04 13:31:11 +0200 | [diff] [blame] | 257 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 259 | err = red_get_flags(ctl->flags, TC_RED_HISTORIC_FLAGS, |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 260 | tb[TCA_RED_FLAGS], TC_RED_SUPPORTED_FLAGS, |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 261 | &flags_bf, &userbits, extack); |
| 262 | if (err) |
| 263 | return err; |
| 264 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 265 | if (ctl->limit > 0) { |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 266 | child = fifo_create_dflt(sch, &bfifo_qdisc_ops, ctl->limit, |
| 267 | extack); |
Patrick McHardy | fb0305c | 2008-07-05 23:40:21 -0700 | [diff] [blame] | 268 | if (IS_ERR(child)) |
| 269 | return PTR_ERR(child); |
Paolo Abeni | 44a63b13 | 2018-05-18 14:51:44 +0200 | [diff] [blame] | 270 | |
| 271 | /* child is fifo, no need to check for noop_qdisc */ |
| 272 | qdisc_hash_add(child, true); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | sch_tree_lock(sch); |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 276 | |
| 277 | flags = (q->flags & ~flags_bf.selector) | flags_bf.value; |
| 278 | err = red_validate_flags(flags, extack); |
| 279 | if (err) |
| 280 | goto unlock_out; |
| 281 | |
| 282 | q->flags = flags; |
| 283 | q->userbits = userbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | q->limit = ctl->limit; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 285 | if (child) { |
Paolo Abeni | e5f0e8f | 2019-03-28 16:53:13 +0100 | [diff] [blame] | 286 | qdisc_tree_flush_backlog(q->qdisc); |
Jakub Kicinski | 0c8d13a | 2018-11-07 17:33:39 -0800 | [diff] [blame] | 287 | old_child = q->qdisc; |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 288 | q->qdisc = child; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 291 | red_set_parms(&q->parms, |
| 292 | ctl->qth_min, ctl->qth_max, ctl->Wlog, |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 293 | ctl->Plog, ctl->Scell_log, |
Eric Dumazet | e323d86 | 2021-03-10 08:26:41 -0800 | [diff] [blame] | 294 | stab, |
Eric Dumazet | a73ed26 | 2011-12-09 02:46:45 +0000 | [diff] [blame] | 295 | max_P); |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 296 | red_set_vars(&q->vars); |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 297 | |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 298 | del_timer(&q->adapt_timer); |
| 299 | if (ctl->flags & TC_RED_ADAPTATIVE) |
| 300 | mod_timer(&q->adapt_timer, jiffies + HZ/2); |
| 301 | |
Eric Dumazet | 1ee5fa1 | 2011-12-01 11:06:34 +0000 | [diff] [blame] | 302 | if (!q->qdisc->q.qlen) |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 303 | red_start_of_idle_period(&q->vars); |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | sch_tree_unlock(sch); |
Jakub Kicinski | 0c8d13a | 2018-11-07 17:33:39 -0800 | [diff] [blame] | 306 | |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 307 | red_offload(sch, true); |
Jakub Kicinski | 0c8d13a | 2018-11-07 17:33:39 -0800 | [diff] [blame] | 308 | |
| 309 | if (old_child) |
| 310 | qdisc_put(old_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return 0; |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 312 | |
| 313 | unlock_out: |
| 314 | sch_tree_unlock(sch); |
| 315 | if (child) |
| 316 | qdisc_put(child); |
| 317 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 320 | static inline void red_adaptative_timer(struct timer_list *t) |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 321 | { |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 322 | struct red_sched_data *q = from_timer(q, t, adapt_timer); |
| 323 | struct Qdisc *sch = q->sch; |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 324 | spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch)); |
| 325 | |
| 326 | spin_lock(root_lock); |
Eric Dumazet | eeca668 | 2012-01-05 02:25:16 +0000 | [diff] [blame] | 327 | red_adaptative_algo(&q->parms, &q->vars); |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 328 | mod_timer(&q->adapt_timer, jiffies + HZ/2); |
| 329 | spin_unlock(root_lock); |
| 330 | } |
| 331 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 332 | static int red_init(struct Qdisc *sch, struct nlattr *opt, |
| 333 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 335 | struct red_sched_data *q = qdisc_priv(sch); |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 336 | struct nlattr *tb[TCA_RED_MAX + 1]; |
| 337 | int err; |
| 338 | |
Cong Wang | 608b4ad | 2020-07-25 13:17:07 -0700 | [diff] [blame] | 339 | q->qdisc = &noop_qdisc; |
| 340 | q->sch = sch; |
| 341 | timer_setup(&q->adapt_timer, red_adaptative_timer, 0); |
| 342 | |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 343 | if (!opt) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | err = nla_parse_nested_deprecated(tb, TCA_RED_MAX, opt, red_policy, |
| 347 | extack); |
| 348 | if (err < 0) |
| 349 | return err; |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 350 | |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 351 | err = __red_change(sch, tb, extack); |
| 352 | if (err) |
| 353 | return err; |
| 354 | |
| 355 | err = tcf_qevent_init(&q->qe_early_drop, sch, |
| 356 | FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP, |
| 357 | tb[TCA_RED_EARLY_DROP_BLOCK], extack); |
| 358 | if (err) |
Cong Wang | 5438dd4 | 2020-08-27 10:40:41 -0700 | [diff] [blame] | 359 | return err; |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 360 | |
Cong Wang | 5438dd4 | 2020-08-27 10:40:41 -0700 | [diff] [blame] | 361 | return tcf_qevent_init(&q->qe_mark, sch, |
| 362 | FLOW_BLOCK_BINDER_TYPE_RED_MARK, |
| 363 | tb[TCA_RED_MARK_BLOCK], extack); |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static int red_change(struct Qdisc *sch, struct nlattr *opt, |
| 367 | struct netlink_ext_ack *extack) |
| 368 | { |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 369 | struct red_sched_data *q = qdisc_priv(sch); |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 370 | struct nlattr *tb[TCA_RED_MAX + 1]; |
| 371 | int err; |
| 372 | |
| 373 | if (!opt) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | err = nla_parse_nested_deprecated(tb, TCA_RED_MAX, opt, red_policy, |
| 377 | extack); |
| 378 | if (err < 0) |
| 379 | return err; |
| 380 | |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 381 | err = tcf_qevent_validate_change(&q->qe_early_drop, |
| 382 | tb[TCA_RED_EARLY_DROP_BLOCK], extack); |
| 383 | if (err) |
| 384 | return err; |
| 385 | |
| 386 | err = tcf_qevent_validate_change(&q->qe_mark, |
| 387 | tb[TCA_RED_MARK_BLOCK], extack); |
| 388 | if (err) |
| 389 | return err; |
| 390 | |
Petr Machata | 65545ea2 | 2020-06-27 01:45:27 +0300 | [diff] [blame] | 391 | return __red_change(sch, tb, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jakub Kicinski | dad54c0 | 2018-11-07 17:33:35 -0800 | [diff] [blame] | 394 | static int red_dump_offload_stats(struct Qdisc *sch) |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 395 | { |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 396 | struct tc_red_qopt_offload hw_stats = { |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 397 | .command = TC_RED_STATS, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 398 | .handle = sch->handle, |
| 399 | .parent = sch->parent, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 400 | { |
| 401 | .stats.bstats = &sch->bstats, |
| 402 | .stats.qstats = &sch->qstats, |
| 403 | }, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 404 | }; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 405 | |
Jakub Kicinski | b592843 | 2018-11-07 17:33:34 -0800 | [diff] [blame] | 406 | return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_RED, &hw_stats); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | static int red_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 410 | { |
| 411 | struct red_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 412 | struct nlattr *opts = NULL; |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 413 | struct tc_red_qopt opt = { |
| 414 | .limit = q->limit, |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 415 | .flags = (q->flags & TC_RED_HISTORIC_FLAGS) | |
| 416 | q->userbits, |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 417 | .qth_min = q->parms.qth_min >> q->parms.Wlog, |
| 418 | .qth_max = q->parms.qth_max >> q->parms.Wlog, |
| 419 | .Wlog = q->parms.Wlog, |
| 420 | .Plog = q->parms.Plog, |
| 421 | .Scell_log = q->parms.Scell_log, |
| 422 | }; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 423 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Jakub Kicinski | dad54c0 | 2018-11-07 17:33:35 -0800 | [diff] [blame] | 425 | err = red_dump_offload_stats(sch); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 426 | if (err) |
| 427 | goto nla_put_failure; |
| 428 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 429 | opts = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 430 | if (opts == NULL) |
| 431 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 432 | if (nla_put(skb, TCA_RED_PARMS, sizeof(opt), &opt) || |
Petr Machata | 14bc175 | 2020-03-13 01:10:56 +0200 | [diff] [blame] | 433 | nla_put_u32(skb, TCA_RED_MAX_P, q->parms.max_P) || |
Jiri Pirko | 8953b07 | 2020-03-28 16:37:42 +0100 | [diff] [blame] | 434 | nla_put_bitfield32(skb, TCA_RED_FLAGS, |
Petr Machata | aee9caa | 2020-06-27 01:45:28 +0300 | [diff] [blame] | 435 | q->flags, TC_RED_SUPPORTED_FLAGS) || |
| 436 | tcf_qevent_dump(skb, TCA_RED_MARK_BLOCK, &q->qe_mark) || |
| 437 | tcf_qevent_dump(skb, TCA_RED_EARLY_DROP_BLOCK, &q->qe_early_drop)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 438 | goto nla_put_failure; |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 439 | return nla_nest_end(skb, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 441 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 442 | nla_nest_cancel(skb, opts); |
| 443 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int red_dump_stats(struct Qdisc *sch, struct gnet_dump *d) |
| 447 | { |
| 448 | struct red_sched_data *q = qdisc_priv(sch); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 449 | struct net_device *dev = qdisc_dev(sch); |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 450 | struct tc_red_xstats st = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Yuval Mintz | 428a68a | 2017-12-14 15:54:30 +0200 | [diff] [blame] | 452 | if (sch->flags & TCQ_F_OFFLOADED) { |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 453 | struct tc_red_qopt_offload hw_stats_request = { |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 454 | .command = TC_RED_XSTATS, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 455 | .handle = sch->handle, |
| 456 | .parent = sch->parent, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 457 | { |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 458 | .xstats = &q->stats, |
Andrew Morton | ee9d342 | 2017-11-10 15:09:53 -0800 | [diff] [blame] | 459 | }, |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 460 | }; |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 461 | dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_RED, |
| 462 | &hw_stats_request); |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 463 | } |
Nogah Frankel | f8253df | 2018-01-10 14:59:59 +0100 | [diff] [blame] | 464 | st.early = q->stats.prob_drop + q->stats.forced_drop; |
| 465 | st.pdrop = q->stats.pdrop; |
| 466 | st.other = q->stats.other; |
| 467 | st.marked = q->stats.prob_mark + q->stats.forced_mark; |
Nogah Frankel | 602f3ba | 2017-11-06 07:23:41 +0100 | [diff] [blame] | 468 | |
Thomas Graf | 6b31b28 | 2005-11-05 21:14:05 +0100 | [diff] [blame] | 469 | return gnet_stats_copy_app(d, &st, sizeof(st)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 472 | static int red_dump_class(struct Qdisc *sch, unsigned long cl, |
| 473 | struct sk_buff *skb, struct tcmsg *tcm) |
| 474 | { |
| 475 | struct red_sched_data *q = qdisc_priv(sch); |
| 476 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 477 | tcm->tcm_handle |= TC_H_MIN(1); |
| 478 | tcm->tcm_info = q->qdisc->handle; |
| 479 | return 0; |
| 480 | } |
| 481 | |
Jakub Kicinski | bf2a752 | 2018-11-12 14:58:13 -0800 | [diff] [blame] | 482 | static void red_graft_offload(struct Qdisc *sch, |
| 483 | struct Qdisc *new, struct Qdisc *old, |
| 484 | struct netlink_ext_ack *extack) |
| 485 | { |
| 486 | struct tc_red_qopt_offload graft_offload = { |
| 487 | .handle = sch->handle, |
| 488 | .parent = sch->parent, |
| 489 | .child_handle = new->handle, |
| 490 | .command = TC_RED_GRAFT, |
| 491 | }; |
| 492 | |
| 493 | qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, old, |
| 494 | TC_SETUP_QDISC_RED, &graft_offload, extack); |
| 495 | } |
| 496 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 497 | 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] | 498 | struct Qdisc **old, struct netlink_ext_ack *extack) |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 499 | { |
| 500 | struct red_sched_data *q = qdisc_priv(sch); |
| 501 | |
| 502 | if (new == NULL) |
| 503 | new = &noop_qdisc; |
| 504 | |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 505 | *old = qdisc_replace(sch, new, &q->qdisc); |
Jakub Kicinski | bf2a752 | 2018-11-12 14:58:13 -0800 | [diff] [blame] | 506 | |
| 507 | red_graft_offload(sch, new, *old, extack); |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static struct Qdisc *red_leaf(struct Qdisc *sch, unsigned long arg) |
| 512 | { |
| 513 | struct red_sched_data *q = qdisc_priv(sch); |
| 514 | return q->qdisc; |
| 515 | } |
| 516 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 517 | static unsigned long red_find(struct Qdisc *sch, u32 classid) |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 518 | { |
| 519 | return 1; |
| 520 | } |
| 521 | |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 522 | static void red_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
| 523 | { |
| 524 | if (!walker->stop) { |
| 525 | if (walker->count >= walker->skip) |
| 526 | if (walker->fn(sch, 1, walker) < 0) { |
| 527 | walker->stop = 1; |
| 528 | return; |
| 529 | } |
| 530 | walker->count++; |
| 531 | } |
| 532 | } |
| 533 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 534 | static const struct Qdisc_class_ops red_class_ops = { |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 535 | .graft = red_graft, |
| 536 | .leaf = red_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 537 | .find = red_find, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 538 | .walk = red_walk, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 539 | .dump = red_dump_class, |
| 540 | }; |
| 541 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 542 | static struct Qdisc_ops red_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | .id = "red", |
| 544 | .priv_size = sizeof(struct red_sched_data), |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 545 | .cl_ops = &red_class_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | .enqueue = red_enqueue, |
| 547 | .dequeue = red_dequeue, |
Jarek Poplawski | 8e3af97 | 2008-10-31 00:45:55 -0700 | [diff] [blame] | 548 | .peek = red_peek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | .init = red_init, |
| 550 | .reset = red_reset, |
Patrick McHardy | f38c39d | 2006-03-20 19:20:44 -0800 | [diff] [blame] | 551 | .destroy = red_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | .change = red_change, |
| 553 | .dump = red_dump, |
| 554 | .dump_stats = red_dump_stats, |
| 555 | .owner = THIS_MODULE, |
| 556 | }; |
| 557 | |
| 558 | static int __init red_module_init(void) |
| 559 | { |
| 560 | return register_qdisc(&red_qdisc_ops); |
| 561 | } |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 562 | |
| 563 | static void __exit red_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | unregister_qdisc(&red_qdisc_ops); |
| 566 | } |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | module_init(red_module_init) |
| 569 | module_exit(red_module_exit) |
Thomas Graf | dba051f | 2005-11-05 21:14:08 +0100 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | MODULE_LICENSE("GPL"); |