1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sched/sch_drr.c Deficit Round Robin scheduler
5 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/netdevice.h>
13 #include <linux/pkt_sched.h>
14 #include <net/sch_generic.h>
15 #include <net/pkt_sched.h>
16 #include <net/pkt_cls.h>
19 struct Qdisc_class_common common;
21 struct gnet_stats_basic_sync bstats;
22 struct gnet_stats_queue qstats;
23 struct net_rate_estimator __rcu *rate_est;
24 struct list_head alist;
32 struct list_head active;
33 struct tcf_proto __rcu *filter_list;
34 struct tcf_block *block;
35 struct Qdisc_class_hash clhash;
38 static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid)
40 struct drr_sched *q = qdisc_priv(sch);
41 struct Qdisc_class_common *clc;
43 clc = qdisc_class_find(&q->clhash, classid);
46 return container_of(clc, struct drr_class, common);
49 static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = {
50 [TCA_DRR_QUANTUM] = { .type = NLA_U32 },
53 static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
54 struct nlattr **tca, unsigned long *arg,
55 struct netlink_ext_ack *extack)
57 struct drr_sched *q = qdisc_priv(sch);
58 struct drr_class *cl = (struct drr_class *)*arg;
59 struct nlattr *opt = tca[TCA_OPTIONS];
60 struct nlattr *tb[TCA_DRR_MAX + 1];
65 NL_SET_ERR_MSG(extack, "DRR options are required for this operation");
69 err = nla_parse_nested_deprecated(tb, TCA_DRR_MAX, opt, drr_policy,
74 if (tb[TCA_DRR_QUANTUM]) {
75 quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
77 NL_SET_ERR_MSG(extack, "Specified DRR quantum cannot be zero");
81 quantum = psched_mtu(qdisc_dev(sch));
85 err = gen_replace_estimator(&cl->bstats, NULL,
90 NL_SET_ERR_MSG(extack, "Failed to replace estimator");
96 if (tb[TCA_DRR_QUANTUM])
97 cl->quantum = quantum;
103 cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
107 gnet_stats_basic_sync_init(&cl->bstats);
108 cl->common.classid = classid;
109 cl->quantum = quantum;
110 cl->qdisc = qdisc_create_dflt(sch->dev_queue,
111 &pfifo_qdisc_ops, classid,
113 if (cl->qdisc == NULL)
114 cl->qdisc = &noop_qdisc;
116 qdisc_hash_add(cl->qdisc, true);
119 err = gen_replace_estimator(&cl->bstats, NULL, &cl->rate_est,
120 NULL, true, tca[TCA_RATE]);
122 NL_SET_ERR_MSG(extack, "Failed to replace estimator");
123 qdisc_put(cl->qdisc);
130 qdisc_class_hash_insert(&q->clhash, &cl->common);
131 sch_tree_unlock(sch);
133 qdisc_class_hash_grow(sch, &q->clhash);
135 *arg = (unsigned long)cl;
139 static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
141 gen_kill_estimator(&cl->rate_est);
142 qdisc_put(cl->qdisc);
146 static int drr_delete_class(struct Qdisc *sch, unsigned long arg,
147 struct netlink_ext_ack *extack)
149 struct drr_sched *q = qdisc_priv(sch);
150 struct drr_class *cl = (struct drr_class *)arg;
152 if (qdisc_class_in_use(&cl->common)) {
153 NL_SET_ERR_MSG(extack, "DRR class is in use");
159 qdisc_purge_queue(cl->qdisc);
160 qdisc_class_hash_remove(&q->clhash, &cl->common);
162 sch_tree_unlock(sch);
164 drr_destroy_class(sch, cl);
168 static unsigned long drr_search_class(struct Qdisc *sch, u32 classid)
170 return (unsigned long)drr_find_class(sch, classid);
173 static struct tcf_block *drr_tcf_block(struct Qdisc *sch, unsigned long cl,
174 struct netlink_ext_ack *extack)
176 struct drr_sched *q = qdisc_priv(sch);
179 NL_SET_ERR_MSG(extack, "DRR classid must be zero");
186 static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
189 struct drr_class *cl = drr_find_class(sch, classid);
192 qdisc_class_get(&cl->common);
194 return (unsigned long)cl;
197 static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
199 struct drr_class *cl = (struct drr_class *)arg;
201 qdisc_class_put(&cl->common);
204 static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
205 struct Qdisc *new, struct Qdisc **old,
206 struct netlink_ext_ack *extack)
208 struct drr_class *cl = (struct drr_class *)arg;
211 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
212 cl->common.classid, NULL);
217 *old = qdisc_replace(sch, new, &cl->qdisc);
221 static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
223 struct drr_class *cl = (struct drr_class *)arg;
228 static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
230 struct drr_class *cl = (struct drr_class *)arg;
232 list_del(&cl->alist);
235 static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
236 struct sk_buff *skb, struct tcmsg *tcm)
238 struct drr_class *cl = (struct drr_class *)arg;
241 tcm->tcm_parent = TC_H_ROOT;
242 tcm->tcm_handle = cl->common.classid;
243 tcm->tcm_info = cl->qdisc->handle;
245 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
247 goto nla_put_failure;
248 if (nla_put_u32(skb, TCA_DRR_QUANTUM, cl->quantum))
249 goto nla_put_failure;
250 return nla_nest_end(skb, nest);
253 nla_nest_cancel(skb, nest);
257 static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
260 struct drr_class *cl = (struct drr_class *)arg;
261 __u32 qlen = qdisc_qlen_sum(cl->qdisc);
262 struct Qdisc *cl_q = cl->qdisc;
263 struct tc_drr_stats xstats;
265 memset(&xstats, 0, sizeof(xstats));
267 xstats.deficit = cl->deficit;
269 if (gnet_stats_copy_basic(d, NULL, &cl->bstats, true) < 0 ||
270 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
271 gnet_stats_copy_queue(d, cl_q->cpu_qstats, &cl_q->qstats, qlen) < 0)
274 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
277 static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
279 struct drr_sched *q = qdisc_priv(sch);
280 struct drr_class *cl;
286 for (i = 0; i < q->clhash.hashsize; i++) {
287 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
288 if (!tc_qdisc_stats_dump(sch, (unsigned long)cl, arg))
294 static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
297 struct drr_sched *q = qdisc_priv(sch);
298 struct drr_class *cl;
299 struct tcf_result res;
300 struct tcf_proto *fl;
303 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
304 cl = drr_find_class(sch, skb->priority);
309 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
310 fl = rcu_dereference_bh(q->filter_list);
311 result = tcf_classify(skb, NULL, fl, &res, false);
313 #ifdef CONFIG_NET_CLS_ACT
318 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
324 cl = (struct drr_class *)res.class;
326 cl = drr_find_class(sch, res.classid);
332 static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch,
333 struct sk_buff **to_free)
335 unsigned int len = qdisc_pkt_len(skb);
336 struct drr_sched *q = qdisc_priv(sch);
337 struct drr_class *cl;
341 cl = drr_classify(skb, sch, &err);
343 if (err & __NET_XMIT_BYPASS)
344 qdisc_qstats_drop(sch);
345 __qdisc_drop(skb, to_free);
349 first = !cl->qdisc->q.qlen;
350 err = qdisc_enqueue(skb, cl->qdisc, to_free);
351 if (unlikely(err != NET_XMIT_SUCCESS)) {
352 if (net_xmit_drop_count(err)) {
354 qdisc_qstats_drop(sch);
360 list_add_tail(&cl->alist, &q->active);
361 cl->deficit = cl->quantum;
364 sch->qstats.backlog += len;
369 static struct sk_buff *drr_dequeue(struct Qdisc *sch)
371 struct drr_sched *q = qdisc_priv(sch);
372 struct drr_class *cl;
376 if (list_empty(&q->active))
379 cl = list_first_entry(&q->active, struct drr_class, alist);
380 skb = cl->qdisc->ops->peek(cl->qdisc);
382 qdisc_warn_nonwc(__func__, cl->qdisc);
386 len = qdisc_pkt_len(skb);
387 if (len <= cl->deficit) {
389 skb = qdisc_dequeue_peeked(cl->qdisc);
390 if (unlikely(skb == NULL))
392 if (cl->qdisc->q.qlen == 0)
393 list_del(&cl->alist);
395 bstats_update(&cl->bstats, skb);
396 qdisc_bstats_update(sch, skb);
397 qdisc_qstats_backlog_dec(sch, skb);
402 cl->deficit += cl->quantum;
403 list_move_tail(&cl->alist, &q->active);
409 static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt,
410 struct netlink_ext_ack *extack)
412 struct drr_sched *q = qdisc_priv(sch);
415 err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
418 err = qdisc_class_hash_init(&q->clhash);
421 INIT_LIST_HEAD(&q->active);
425 static void drr_reset_qdisc(struct Qdisc *sch)
427 struct drr_sched *q = qdisc_priv(sch);
428 struct drr_class *cl;
431 for (i = 0; i < q->clhash.hashsize; i++) {
432 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
433 if (cl->qdisc->q.qlen)
434 list_del(&cl->alist);
435 qdisc_reset(cl->qdisc);
440 static void drr_destroy_qdisc(struct Qdisc *sch)
442 struct drr_sched *q = qdisc_priv(sch);
443 struct drr_class *cl;
444 struct hlist_node *next;
447 tcf_block_put(q->block);
449 for (i = 0; i < q->clhash.hashsize; i++) {
450 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
452 drr_destroy_class(sch, cl);
454 qdisc_class_hash_destroy(&q->clhash);
457 static const struct Qdisc_class_ops drr_class_ops = {
458 .change = drr_change_class,
459 .delete = drr_delete_class,
460 .find = drr_search_class,
461 .tcf_block = drr_tcf_block,
462 .bind_tcf = drr_bind_tcf,
463 .unbind_tcf = drr_unbind_tcf,
464 .graft = drr_graft_class,
465 .leaf = drr_class_leaf,
466 .qlen_notify = drr_qlen_notify,
467 .dump = drr_dump_class,
468 .dump_stats = drr_dump_class_stats,
472 static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
473 .cl_ops = &drr_class_ops,
475 .priv_size = sizeof(struct drr_sched),
476 .enqueue = drr_enqueue,
477 .dequeue = drr_dequeue,
478 .peek = qdisc_peek_dequeued,
479 .init = drr_init_qdisc,
480 .reset = drr_reset_qdisc,
481 .destroy = drr_destroy_qdisc,
482 .owner = THIS_MODULE,
485 static int __init drr_init(void)
487 return register_qdisc(&drr_qdisc_ops);
490 static void __exit drr_exit(void)
492 unregister_qdisc(&drr_qdisc_ops);
495 module_init(drr_init);
496 module_exit(drr_exit);
497 MODULE_LICENSE("GPL");