Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_drr.c Deficit Round Robin scheduler |
| 3 | * |
| 4 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/pkt_sched.h> |
| 17 | #include <net/sch_generic.h> |
| 18 | #include <net/pkt_sched.h> |
| 19 | #include <net/pkt_cls.h> |
| 20 | |
| 21 | struct drr_class { |
| 22 | struct Qdisc_class_common common; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 23 | unsigned int filter_cnt; |
| 24 | |
Eric Dumazet | c1a8f1f | 2009-08-16 09:36:49 +0000 | [diff] [blame] | 25 | struct gnet_stats_basic_packed bstats; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 26 | struct gnet_stats_queue qstats; |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 27 | struct net_rate_estimator __rcu *rate_est; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 28 | struct list_head alist; |
| 29 | struct Qdisc *qdisc; |
| 30 | |
| 31 | u32 quantum; |
| 32 | u32 deficit; |
| 33 | }; |
| 34 | |
| 35 | struct drr_sched { |
| 36 | struct list_head active; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 37 | struct tcf_proto __rcu *filter_list; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 38 | struct tcf_block *block; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 39 | struct Qdisc_class_hash clhash; |
| 40 | }; |
| 41 | |
| 42 | static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid) |
| 43 | { |
| 44 | struct drr_sched *q = qdisc_priv(sch); |
| 45 | struct Qdisc_class_common *clc; |
| 46 | |
| 47 | clc = qdisc_class_find(&q->clhash, classid); |
| 48 | if (clc == NULL) |
| 49 | return NULL; |
| 50 | return container_of(clc, struct drr_class, common); |
| 51 | } |
| 52 | |
| 53 | static void drr_purge_queue(struct drr_class *cl) |
| 54 | { |
| 55 | unsigned int len = cl->qdisc->q.qlen; |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 56 | unsigned int backlog = cl->qdisc->qstats.backlog; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 57 | |
| 58 | qdisc_reset(cl->qdisc); |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 59 | qdisc_tree_reduce_backlog(cl->qdisc, len, backlog); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = { |
| 63 | [TCA_DRR_QUANTUM] = { .type = NLA_U32 }, |
| 64 | }; |
| 65 | |
| 66 | static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid, |
Alexander Aring | 793d81d | 2017-12-20 12:35:15 -0500 | [diff] [blame^] | 67 | struct nlattr **tca, unsigned long *arg, |
| 68 | struct netlink_ext_ack *extack) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 69 | { |
| 70 | struct drr_sched *q = qdisc_priv(sch); |
| 71 | struct drr_class *cl = (struct drr_class *)*arg; |
Jarek Poplawski | 1844f74 | 2009-02-27 02:42:38 -0800 | [diff] [blame] | 72 | struct nlattr *opt = tca[TCA_OPTIONS]; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 73 | struct nlattr *tb[TCA_DRR_MAX + 1]; |
| 74 | u32 quantum; |
| 75 | int err; |
| 76 | |
Jarek Poplawski | 1844f74 | 2009-02-27 02:42:38 -0800 | [diff] [blame] | 77 | if (!opt) |
| 78 | return -EINVAL; |
| 79 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 80 | err = nla_parse_nested(tb, TCA_DRR_MAX, opt, drr_policy, NULL); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 81 | if (err < 0) |
| 82 | return err; |
| 83 | |
| 84 | if (tb[TCA_DRR_QUANTUM]) { |
| 85 | quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]); |
| 86 | if (quantum == 0) |
| 87 | return -EINVAL; |
| 88 | } else |
| 89 | quantum = psched_mtu(qdisc_dev(sch)); |
| 90 | |
| 91 | if (cl != NULL) { |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 92 | if (tca[TCA_RATE]) { |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 93 | err = gen_replace_estimator(&cl->bstats, NULL, |
| 94 | &cl->rate_est, |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 95 | NULL, |
| 96 | qdisc_root_sleeping_running(sch), |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 97 | tca[TCA_RATE]); |
| 98 | if (err) |
| 99 | return err; |
| 100 | } |
| 101 | |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 102 | sch_tree_lock(sch); |
| 103 | if (tb[TCA_DRR_QUANTUM]) |
| 104 | cl->quantum = quantum; |
| 105 | sch_tree_unlock(sch); |
| 106 | |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL); |
| 111 | if (cl == NULL) |
| 112 | return -ENOBUFS; |
| 113 | |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 114 | cl->common.classid = classid; |
| 115 | cl->quantum = quantum; |
Changli Gao | 3511c91 | 2010-10-16 13:04:08 +0000 | [diff] [blame] | 116 | cl->qdisc = qdisc_create_dflt(sch->dev_queue, |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 117 | &pfifo_qdisc_ops, classid); |
| 118 | if (cl->qdisc == NULL) |
| 119 | cl->qdisc = &noop_qdisc; |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 120 | else |
| 121 | qdisc_hash_add(cl->qdisc, true); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 122 | |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 123 | if (tca[TCA_RATE]) { |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 124 | err = gen_replace_estimator(&cl->bstats, NULL, &cl->rate_est, |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 125 | NULL, |
| 126 | qdisc_root_sleeping_running(sch), |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 127 | tca[TCA_RATE]); |
| 128 | if (err) { |
| 129 | qdisc_destroy(cl->qdisc); |
| 130 | kfree(cl); |
| 131 | return err; |
| 132 | } |
| 133 | } |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 134 | |
| 135 | sch_tree_lock(sch); |
| 136 | qdisc_class_hash_insert(&q->clhash, &cl->common); |
| 137 | sch_tree_unlock(sch); |
| 138 | |
| 139 | qdisc_class_hash_grow(sch, &q->clhash); |
| 140 | |
| 141 | *arg = (unsigned long)cl; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl) |
| 146 | { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 147 | gen_kill_estimator(&cl->rate_est); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 148 | qdisc_destroy(cl->qdisc); |
| 149 | kfree(cl); |
| 150 | } |
| 151 | |
| 152 | static int drr_delete_class(struct Qdisc *sch, unsigned long arg) |
| 153 | { |
| 154 | struct drr_sched *q = qdisc_priv(sch); |
| 155 | struct drr_class *cl = (struct drr_class *)arg; |
| 156 | |
| 157 | if (cl->filter_cnt > 0) |
| 158 | return -EBUSY; |
| 159 | |
| 160 | sch_tree_lock(sch); |
| 161 | |
| 162 | drr_purge_queue(cl); |
| 163 | qdisc_class_hash_remove(&q->clhash, &cl->common); |
| 164 | |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 165 | sch_tree_unlock(sch); |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 166 | |
| 167 | drr_destroy_class(sch, cl); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 171 | static unsigned long drr_search_class(struct Qdisc *sch, u32 classid) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 172 | { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 173 | return (unsigned long)drr_find_class(sch, classid); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 176 | static struct tcf_block *drr_tcf_block(struct Qdisc *sch, unsigned long cl) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 177 | { |
| 178 | struct drr_sched *q = qdisc_priv(sch); |
| 179 | |
| 180 | if (cl) |
| 181 | return NULL; |
| 182 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 183 | return q->block; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent, |
| 187 | u32 classid) |
| 188 | { |
| 189 | struct drr_class *cl = drr_find_class(sch, classid); |
| 190 | |
| 191 | if (cl != NULL) |
| 192 | cl->filter_cnt++; |
| 193 | |
| 194 | return (unsigned long)cl; |
| 195 | } |
| 196 | |
| 197 | static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg) |
| 198 | { |
| 199 | struct drr_class *cl = (struct drr_class *)arg; |
| 200 | |
| 201 | cl->filter_cnt--; |
| 202 | } |
| 203 | |
| 204 | static int drr_graft_class(struct Qdisc *sch, unsigned long arg, |
| 205 | struct Qdisc *new, struct Qdisc **old) |
| 206 | { |
| 207 | struct drr_class *cl = (struct drr_class *)arg; |
| 208 | |
| 209 | if (new == NULL) { |
Changli Gao | 3511c91 | 2010-10-16 13:04:08 +0000 | [diff] [blame] | 210 | new = qdisc_create_dflt(sch->dev_queue, |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 211 | &pfifo_qdisc_ops, cl->common.classid); |
| 212 | if (new == NULL) |
| 213 | new = &noop_qdisc; |
| 214 | } |
| 215 | |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 216 | *old = qdisc_replace(sch, new, &cl->qdisc); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg) |
| 221 | { |
| 222 | struct drr_class *cl = (struct drr_class *)arg; |
| 223 | |
| 224 | return cl->qdisc; |
| 225 | } |
| 226 | |
| 227 | static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg) |
| 228 | { |
| 229 | struct drr_class *cl = (struct drr_class *)arg; |
| 230 | |
Konstantin Khlebnikov | 9594665 | 2017-08-15 16:39:59 +0300 | [diff] [blame] | 231 | list_del(&cl->alist); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int drr_dump_class(struct Qdisc *sch, unsigned long arg, |
| 235 | struct sk_buff *skb, struct tcmsg *tcm) |
| 236 | { |
| 237 | struct drr_class *cl = (struct drr_class *)arg; |
| 238 | struct nlattr *nest; |
| 239 | |
| 240 | tcm->tcm_parent = TC_H_ROOT; |
| 241 | tcm->tcm_handle = cl->common.classid; |
| 242 | tcm->tcm_info = cl->qdisc->handle; |
| 243 | |
| 244 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 245 | if (nest == NULL) |
| 246 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 247 | if (nla_put_u32(skb, TCA_DRR_QUANTUM, cl->quantum)) |
| 248 | goto nla_put_failure; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 249 | return nla_nest_end(skb, nest); |
| 250 | |
| 251 | nla_put_failure: |
| 252 | nla_nest_cancel(skb, nest); |
| 253 | return -EMSGSIZE; |
| 254 | } |
| 255 | |
| 256 | static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg, |
| 257 | struct gnet_dump *d) |
| 258 | { |
| 259 | struct drr_class *cl = (struct drr_class *)arg; |
John Fastabend | 6401585 | 2014-09-28 11:53:57 -0700 | [diff] [blame] | 260 | __u32 qlen = cl->qdisc->q.qlen; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 261 | struct tc_drr_stats xstats; |
| 262 | |
| 263 | memset(&xstats, 0, sizeof(xstats)); |
John Fastabend | 6401585 | 2014-09-28 11:53:57 -0700 | [diff] [blame] | 264 | if (qlen) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 265 | xstats.deficit = cl->deficit; |
| 266 | |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 267 | if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), |
| 268 | d, NULL, &cl->bstats) < 0 || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 269 | gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 || |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 270 | gnet_stats_copy_queue(d, NULL, &cl->qdisc->qstats, qlen) < 0) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 271 | return -1; |
| 272 | |
| 273 | return gnet_stats_copy_app(d, &xstats, sizeof(xstats)); |
| 274 | } |
| 275 | |
| 276 | static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 277 | { |
| 278 | struct drr_sched *q = qdisc_priv(sch); |
| 279 | struct drr_class *cl; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 280 | unsigned int i; |
| 281 | |
| 282 | if (arg->stop) |
| 283 | return; |
| 284 | |
| 285 | for (i = 0; i < q->clhash.hashsize; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 286 | hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) { |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 287 | if (arg->count < arg->skip) { |
| 288 | arg->count++; |
| 289 | continue; |
| 290 | } |
| 291 | if (arg->fn(sch, (unsigned long)cl, arg) < 0) { |
| 292 | arg->stop = 1; |
| 293 | return; |
| 294 | } |
| 295 | arg->count++; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch, |
| 301 | int *qerr) |
| 302 | { |
| 303 | struct drr_sched *q = qdisc_priv(sch); |
| 304 | struct drr_class *cl; |
| 305 | struct tcf_result res; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 306 | struct tcf_proto *fl; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 307 | int result; |
| 308 | |
| 309 | if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) { |
| 310 | cl = drr_find_class(sch, skb->priority); |
| 311 | if (cl != NULL) |
| 312 | return cl; |
| 313 | } |
| 314 | |
| 315 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 316 | fl = rcu_dereference_bh(q->filter_list); |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 317 | result = tcf_classify(skb, fl, &res, false); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 318 | if (result >= 0) { |
| 319 | #ifdef CONFIG_NET_CLS_ACT |
| 320 | switch (result) { |
| 321 | case TC_ACT_QUEUED: |
| 322 | case TC_ACT_STOLEN: |
Jiri Pirko | e25ea21 | 2017-06-06 14:12:02 +0200 | [diff] [blame] | 323 | case TC_ACT_TRAP: |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 324 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
Gustavo A. R. Silva | f3ae608 | 2017-10-19 16:28:24 -0500 | [diff] [blame] | 325 | /* fall through */ |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 326 | case TC_ACT_SHOT: |
| 327 | return NULL; |
| 328 | } |
| 329 | #endif |
| 330 | cl = (struct drr_class *)res.class; |
| 331 | if (cl == NULL) |
| 332 | cl = drr_find_class(sch, res.classid); |
| 333 | return cl; |
| 334 | } |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 338 | static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
| 339 | struct sk_buff **to_free) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 340 | { |
| 341 | struct drr_sched *q = qdisc_priv(sch); |
| 342 | struct drr_class *cl; |
David S. Miller | f54ba77 | 2012-09-27 18:35:47 -0400 | [diff] [blame] | 343 | int err = 0; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 344 | |
| 345 | cl = drr_classify(skb, sch, &err); |
| 346 | if (cl == NULL) { |
| 347 | if (err & __NET_XMIT_BYPASS) |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 348 | qdisc_qstats_drop(sch); |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 349 | __qdisc_drop(skb, to_free); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 350 | return err; |
| 351 | } |
| 352 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 353 | err = qdisc_enqueue(skb, cl->qdisc, to_free); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 354 | if (unlikely(err != NET_XMIT_SUCCESS)) { |
| 355 | if (net_xmit_drop_count(err)) { |
| 356 | cl->qstats.drops++; |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 357 | qdisc_qstats_drop(sch); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 358 | } |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | if (cl->qdisc->q.qlen == 1) { |
| 363 | list_add_tail(&cl->alist, &q->active); |
| 364 | cl->deficit = cl->quantum; |
| 365 | } |
| 366 | |
WANG Cong | 6a73b57 | 2016-06-01 16:15:17 -0700 | [diff] [blame] | 367 | qdisc_qstats_backlog_inc(sch, skb); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 368 | sch->q.qlen++; |
| 369 | return err; |
| 370 | } |
| 371 | |
| 372 | static struct sk_buff *drr_dequeue(struct Qdisc *sch) |
| 373 | { |
| 374 | struct drr_sched *q = qdisc_priv(sch); |
| 375 | struct drr_class *cl; |
| 376 | struct sk_buff *skb; |
| 377 | unsigned int len; |
| 378 | |
Patrick McHardy | 3f0947c | 2008-11-24 15:46:08 -0800 | [diff] [blame] | 379 | if (list_empty(&q->active)) |
| 380 | goto out; |
| 381 | while (1) { |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 382 | cl = list_first_entry(&q->active, struct drr_class, alist); |
| 383 | skb = cl->qdisc->ops->peek(cl->qdisc); |
Florian Westphal | 6e765a0 | 2014-06-11 20:35:18 +0200 | [diff] [blame] | 384 | if (skb == NULL) { |
| 385 | qdisc_warn_nonwc(__func__, cl->qdisc); |
Patrick McHardy | 3f0947c | 2008-11-24 15:46:08 -0800 | [diff] [blame] | 386 | goto out; |
Florian Westphal | 6e765a0 | 2014-06-11 20:35:18 +0200 | [diff] [blame] | 387 | } |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 388 | |
| 389 | len = qdisc_pkt_len(skb); |
| 390 | if (len <= cl->deficit) { |
| 391 | cl->deficit -= len; |
| 392 | skb = qdisc_dequeue_peeked(cl->qdisc); |
Bernie Harris | df3eb6c | 2016-01-28 16:30:51 +1300 | [diff] [blame] | 393 | if (unlikely(skb == NULL)) |
| 394 | goto out; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 395 | if (cl->qdisc->q.qlen == 0) |
| 396 | list_del(&cl->alist); |
Eric Dumazet | 2dd875f | 2012-05-10 05:36:34 +0000 | [diff] [blame] | 397 | |
| 398 | bstats_update(&cl->bstats, skb); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 399 | qdisc_bstats_update(sch, skb); |
WANG Cong | 6a73b57 | 2016-06-01 16:15:17 -0700 | [diff] [blame] | 400 | qdisc_qstats_backlog_dec(sch, skb); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 401 | sch->q.qlen--; |
| 402 | return skb; |
| 403 | } |
| 404 | |
| 405 | cl->deficit += cl->quantum; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 406 | list_move_tail(&cl->alist, &q->active); |
| 407 | } |
Patrick McHardy | 3f0947c | 2008-11-24 15:46:08 -0800 | [diff] [blame] | 408 | out: |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
| 411 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 412 | static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt, |
| 413 | struct netlink_ext_ack *extack) |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 414 | { |
| 415 | struct drr_sched *q = qdisc_priv(sch); |
| 416 | int err; |
| 417 | |
Jiri Pirko | 69d78ef | 2017-10-13 14:00:57 +0200 | [diff] [blame] | 418 | err = tcf_block_get(&q->block, &q->filter_list, sch); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 419 | if (err) |
| 420 | return err; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 421 | err = qdisc_class_hash_init(&q->clhash); |
| 422 | if (err < 0) |
| 423 | return err; |
| 424 | INIT_LIST_HEAD(&q->active); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static void drr_reset_qdisc(struct Qdisc *sch) |
| 429 | { |
| 430 | struct drr_sched *q = qdisc_priv(sch); |
| 431 | struct drr_class *cl; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 432 | unsigned int i; |
| 433 | |
| 434 | for (i = 0; i < q->clhash.hashsize; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 435 | hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) { |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 436 | if (cl->qdisc->q.qlen) |
| 437 | list_del(&cl->alist); |
| 438 | qdisc_reset(cl->qdisc); |
| 439 | } |
| 440 | } |
WANG Cong | 6a73b57 | 2016-06-01 16:15:17 -0700 | [diff] [blame] | 441 | sch->qstats.backlog = 0; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 442 | sch->q.qlen = 0; |
| 443 | } |
| 444 | |
| 445 | static void drr_destroy_qdisc(struct Qdisc *sch) |
| 446 | { |
| 447 | struct drr_sched *q = qdisc_priv(sch); |
| 448 | struct drr_class *cl; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 449 | struct hlist_node *next; |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 450 | unsigned int i; |
| 451 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 452 | tcf_block_put(q->block); |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 453 | |
| 454 | for (i = 0; i < q->clhash.hashsize; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 455 | hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i], |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 456 | common.hnode) |
| 457 | drr_destroy_class(sch, cl); |
| 458 | } |
| 459 | qdisc_class_hash_destroy(&q->clhash); |
| 460 | } |
| 461 | |
| 462 | static const struct Qdisc_class_ops drr_class_ops = { |
| 463 | .change = drr_change_class, |
| 464 | .delete = drr_delete_class, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 465 | .find = drr_search_class, |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 466 | .tcf_block = drr_tcf_block, |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 467 | .bind_tcf = drr_bind_tcf, |
| 468 | .unbind_tcf = drr_unbind_tcf, |
| 469 | .graft = drr_graft_class, |
| 470 | .leaf = drr_class_leaf, |
| 471 | .qlen_notify = drr_qlen_notify, |
| 472 | .dump = drr_dump_class, |
| 473 | .dump_stats = drr_dump_class_stats, |
| 474 | .walk = drr_walk, |
| 475 | }; |
| 476 | |
| 477 | static struct Qdisc_ops drr_qdisc_ops __read_mostly = { |
| 478 | .cl_ops = &drr_class_ops, |
| 479 | .id = "drr", |
| 480 | .priv_size = sizeof(struct drr_sched), |
| 481 | .enqueue = drr_enqueue, |
| 482 | .dequeue = drr_dequeue, |
| 483 | .peek = qdisc_peek_dequeued, |
Patrick McHardy | 13d2a1d | 2008-11-20 04:10:00 -0800 | [diff] [blame] | 484 | .init = drr_init_qdisc, |
| 485 | .reset = drr_reset_qdisc, |
| 486 | .destroy = drr_destroy_qdisc, |
| 487 | .owner = THIS_MODULE, |
| 488 | }; |
| 489 | |
| 490 | static int __init drr_init(void) |
| 491 | { |
| 492 | return register_qdisc(&drr_qdisc_ops); |
| 493 | } |
| 494 | |
| 495 | static void __exit drr_exit(void) |
| 496 | { |
| 497 | unregister_qdisc(&drr_qdisc_ops); |
| 498 | } |
| 499 | |
| 500 | module_init(drr_init); |
| 501 | module_exit(drr_exit); |
| 502 | MODULE_LICENSE("GPL"); |