2 * net/sched/sch_drr.c Deficit Round Robin scheduler
4 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
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.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/netdevice.h>
15 #include <linux/pkt_sched.h>
16 #include <net/sch_generic.h>
17 #include <net/pkt_sched.h>
18 #include <net/pkt_cls.h>
21 struct Qdisc_class_common common;
23 unsigned int filter_cnt;
25 struct gnet_stats_basic bstats;
26 struct gnet_stats_queue qstats;
27 struct gnet_stats_rate_est rate_est;
28 struct list_head alist;
36 struct list_head active;
37 struct tcf_proto *filter_list;
38 struct Qdisc_class_hash clhash;
41 static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid)
43 struct drr_sched *q = qdisc_priv(sch);
44 struct Qdisc_class_common *clc;
46 clc = qdisc_class_find(&q->clhash, classid);
49 return container_of(clc, struct drr_class, common);
52 static void drr_purge_queue(struct drr_class *cl)
54 unsigned int len = cl->qdisc->q.qlen;
56 qdisc_reset(cl->qdisc);
57 qdisc_tree_decrease_qlen(cl->qdisc, len);
60 static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = {
61 [TCA_DRR_QUANTUM] = { .type = NLA_U32 },
64 static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
65 struct nlattr **tca, unsigned long *arg)
67 struct drr_sched *q = qdisc_priv(sch);
68 struct drr_class *cl = (struct drr_class *)*arg;
69 struct nlattr *tb[TCA_DRR_MAX + 1];
73 err = nla_parse_nested(tb, TCA_DRR_MAX, tca[TCA_OPTIONS], drr_policy);
77 if (tb[TCA_DRR_QUANTUM]) {
78 quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
82 quantum = psched_mtu(qdisc_dev(sch));
86 err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
87 qdisc_root_sleeping_lock(sch),
94 if (tb[TCA_DRR_QUANTUM])
95 cl->quantum = quantum;
101 cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
106 cl->common.classid = classid;
107 cl->quantum = quantum;
108 cl->qdisc = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
109 &pfifo_qdisc_ops, classid);
110 if (cl->qdisc == NULL)
111 cl->qdisc = &noop_qdisc;
114 err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
115 qdisc_root_sleeping_lock(sch),
118 qdisc_destroy(cl->qdisc);
125 qdisc_class_hash_insert(&q->clhash, &cl->common);
126 sch_tree_unlock(sch);
128 qdisc_class_hash_grow(sch, &q->clhash);
130 *arg = (unsigned long)cl;
134 static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
136 gen_kill_estimator(&cl->bstats, &cl->rate_est);
137 qdisc_destroy(cl->qdisc);
141 static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
143 struct drr_sched *q = qdisc_priv(sch);
144 struct drr_class *cl = (struct drr_class *)arg;
146 if (cl->filter_cnt > 0)
152 qdisc_class_hash_remove(&q->clhash, &cl->common);
154 if (--cl->refcnt == 0)
155 drr_destroy_class(sch, cl);
157 sch_tree_unlock(sch);
161 static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
163 struct drr_class *cl = drr_find_class(sch, classid);
168 return (unsigned long)cl;
171 static void drr_put_class(struct Qdisc *sch, unsigned long arg)
173 struct drr_class *cl = (struct drr_class *)arg;
175 if (--cl->refcnt == 0)
176 drr_destroy_class(sch, cl);
179 static struct tcf_proto **drr_tcf_chain(struct Qdisc *sch, unsigned long cl)
181 struct drr_sched *q = qdisc_priv(sch);
186 return &q->filter_list;
189 static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
192 struct drr_class *cl = drr_find_class(sch, classid);
197 return (unsigned long)cl;
200 static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
202 struct drr_class *cl = (struct drr_class *)arg;
207 static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
208 struct Qdisc *new, struct Qdisc **old)
210 struct drr_class *cl = (struct drr_class *)arg;
213 new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
214 &pfifo_qdisc_ops, cl->common.classid);
223 sch_tree_unlock(sch);
227 static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
229 struct drr_class *cl = (struct drr_class *)arg;
234 static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
236 struct drr_class *cl = (struct drr_class *)arg;
238 if (cl->qdisc->q.qlen == 0)
239 list_del(&cl->alist);
242 static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
243 struct sk_buff *skb, struct tcmsg *tcm)
245 struct drr_class *cl = (struct drr_class *)arg;
248 tcm->tcm_parent = TC_H_ROOT;
249 tcm->tcm_handle = cl->common.classid;
250 tcm->tcm_info = cl->qdisc->handle;
252 nest = nla_nest_start(skb, TCA_OPTIONS);
254 goto nla_put_failure;
255 NLA_PUT_U32(skb, TCA_DRR_QUANTUM, cl->quantum);
256 return nla_nest_end(skb, nest);
259 nla_nest_cancel(skb, nest);
263 static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
266 struct drr_class *cl = (struct drr_class *)arg;
267 struct tc_drr_stats xstats;
269 memset(&xstats, 0, sizeof(xstats));
270 if (cl->qdisc->q.qlen)
271 xstats.deficit = cl->deficit;
273 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
274 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
275 gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
278 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
281 static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
283 struct drr_sched *q = qdisc_priv(sch);
284 struct drr_class *cl;
285 struct hlist_node *n;
291 for (i = 0; i < q->clhash.hashsize; i++) {
292 hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
293 if (arg->count < arg->skip) {
297 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
306 static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
309 struct drr_sched *q = qdisc_priv(sch);
310 struct drr_class *cl;
311 struct tcf_result res;
314 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
315 cl = drr_find_class(sch, skb->priority);
320 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
321 result = tc_classify(skb, q->filter_list, &res);
323 #ifdef CONFIG_NET_CLS_ACT
327 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
332 cl = (struct drr_class *)res.class;
334 cl = drr_find_class(sch, res.classid);
340 static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch)
342 struct drr_sched *q = qdisc_priv(sch);
343 struct drr_class *cl;
347 cl = drr_classify(skb, sch, &err);
349 if (err & __NET_XMIT_BYPASS)
355 len = qdisc_pkt_len(skb);
356 err = qdisc_enqueue(skb, cl->qdisc);
357 if (unlikely(err != NET_XMIT_SUCCESS)) {
358 if (net_xmit_drop_count(err)) {
365 if (cl->qdisc->q.qlen == 1) {
366 list_add_tail(&cl->alist, &q->active);
367 cl->deficit = cl->quantum;
370 cl->bstats.packets++;
371 cl->bstats.bytes += len;
372 sch->bstats.packets++;
373 sch->bstats.bytes += len;
379 static struct sk_buff *drr_dequeue(struct Qdisc *sch)
381 struct drr_sched *q = qdisc_priv(sch);
382 struct drr_class *cl;
386 if (list_empty(&q->active))
389 cl = list_first_entry(&q->active, struct drr_class, alist);
390 skb = cl->qdisc->ops->peek(cl->qdisc);
394 len = qdisc_pkt_len(skb);
395 if (len <= cl->deficit) {
397 skb = qdisc_dequeue_peeked(cl->qdisc);
398 if (cl->qdisc->q.qlen == 0)
399 list_del(&cl->alist);
404 cl->deficit += cl->quantum;
405 list_move_tail(&cl->alist, &q->active);
411 static unsigned int drr_drop(struct Qdisc *sch)
413 struct drr_sched *q = qdisc_priv(sch);
414 struct drr_class *cl;
417 list_for_each_entry(cl, &q->active, alist) {
418 if (cl->qdisc->ops->drop) {
419 len = cl->qdisc->ops->drop(cl->qdisc);
422 if (cl->qdisc->q.qlen == 0)
423 list_del(&cl->alist);
431 static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
433 struct drr_sched *q = qdisc_priv(sch);
436 err = qdisc_class_hash_init(&q->clhash);
439 INIT_LIST_HEAD(&q->active);
443 static void drr_reset_qdisc(struct Qdisc *sch)
445 struct drr_sched *q = qdisc_priv(sch);
446 struct drr_class *cl;
447 struct hlist_node *n;
450 for (i = 0; i < q->clhash.hashsize; i++) {
451 hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
452 if (cl->qdisc->q.qlen)
453 list_del(&cl->alist);
454 qdisc_reset(cl->qdisc);
460 static void drr_destroy_qdisc(struct Qdisc *sch)
462 struct drr_sched *q = qdisc_priv(sch);
463 struct drr_class *cl;
464 struct hlist_node *n, *next;
467 tcf_destroy_chain(&q->filter_list);
469 for (i = 0; i < q->clhash.hashsize; i++) {
470 hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
472 drr_destroy_class(sch, cl);
474 qdisc_class_hash_destroy(&q->clhash);
477 static const struct Qdisc_class_ops drr_class_ops = {
478 .change = drr_change_class,
479 .delete = drr_delete_class,
480 .get = drr_get_class,
481 .put = drr_put_class,
482 .tcf_chain = drr_tcf_chain,
483 .bind_tcf = drr_bind_tcf,
484 .unbind_tcf = drr_unbind_tcf,
485 .graft = drr_graft_class,
486 .leaf = drr_class_leaf,
487 .qlen_notify = drr_qlen_notify,
488 .dump = drr_dump_class,
489 .dump_stats = drr_dump_class_stats,
493 static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
494 .cl_ops = &drr_class_ops,
496 .priv_size = sizeof(struct drr_sched),
497 .enqueue = drr_enqueue,
498 .dequeue = drr_dequeue,
499 .peek = qdisc_peek_dequeued,
501 .init = drr_init_qdisc,
502 .reset = drr_reset_qdisc,
503 .destroy = drr_destroy_qdisc,
504 .owner = THIS_MODULE,
507 static int __init drr_init(void)
509 return register_qdisc(&drr_qdisc_ops);
512 static void __exit drr_exit(void)
514 unregister_qdisc(&drr_qdisc_ops);
517 module_init(drr_init);
518 module_exit(drr_exit);
519 MODULE_LICENSE("GPL");