ACPI: Enable bit 11 in _PDC to advertise hw coord
[linux-2.6/mini2440.git] / net / sched / sch_drr.c
blobf6b4fa97df70e353b58ed04ba4a74f50f4c8fbcf
1 /*
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.
9 */
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>
20 struct drr_class {
21 struct Qdisc_class_common common;
22 unsigned int refcnt;
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;
29 struct Qdisc *qdisc;
31 u32 quantum;
32 u32 deficit;
35 struct drr_sched {
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);
47 if (clc == NULL)
48 return NULL;
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];
70 u32 quantum;
71 int err;
73 err = nla_parse_nested(tb, TCA_DRR_MAX, tca[TCA_OPTIONS], drr_policy);
74 if (err < 0)
75 return err;
77 if (tb[TCA_DRR_QUANTUM]) {
78 quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
79 if (quantum == 0)
80 return -EINVAL;
81 } else
82 quantum = psched_mtu(qdisc_dev(sch));
84 if (cl != NULL) {
85 if (tca[TCA_RATE]) {
86 err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
87 qdisc_root_sleeping_lock(sch),
88 tca[TCA_RATE]);
89 if (err)
90 return err;
93 sch_tree_lock(sch);
94 if (tb[TCA_DRR_QUANTUM])
95 cl->quantum = quantum;
96 sch_tree_unlock(sch);
98 return 0;
101 cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
102 if (cl == NULL)
103 return -ENOBUFS;
105 cl->refcnt = 1;
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;
113 if (tca[TCA_RATE]) {
114 err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
115 qdisc_root_sleeping_lock(sch),
116 tca[TCA_RATE]);
117 if (err) {
118 qdisc_destroy(cl->qdisc);
119 kfree(cl);
120 return err;
124 sch_tree_lock(sch);
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;
131 return 0;
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);
138 kfree(cl);
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)
147 return -EBUSY;
149 sch_tree_lock(sch);
151 drr_purge_queue(cl);
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);
158 return 0;
161 static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
163 struct drr_class *cl = drr_find_class(sch, classid);
165 if (cl != NULL)
166 cl->refcnt++;
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);
183 if (cl)
184 return NULL;
186 return &q->filter_list;
189 static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
190 u32 classid)
192 struct drr_class *cl = drr_find_class(sch, classid);
194 if (cl != NULL)
195 cl->filter_cnt++;
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;
204 cl->filter_cnt--;
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;
212 if (new == NULL) {
213 new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
214 &pfifo_qdisc_ops, cl->common.classid);
215 if (new == NULL)
216 new = &noop_qdisc;
219 sch_tree_lock(sch);
220 drr_purge_queue(cl);
221 *old = cl->qdisc;
222 cl->qdisc = new;
223 sch_tree_unlock(sch);
224 return 0;
227 static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
229 struct drr_class *cl = (struct drr_class *)arg;
231 return cl->qdisc;
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;
246 struct nlattr *nest;
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);
253 if (nest == NULL)
254 goto nla_put_failure;
255 NLA_PUT_U32(skb, TCA_DRR_QUANTUM, cl->quantum);
256 return nla_nest_end(skb, nest);
258 nla_put_failure:
259 nla_nest_cancel(skb, nest);
260 return -EMSGSIZE;
263 static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
264 struct gnet_dump *d)
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)
276 return -1;
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;
286 unsigned int i;
288 if (arg->stop)
289 return;
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) {
294 arg->count++;
295 continue;
297 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
298 arg->stop = 1;
299 return;
301 arg->count++;
306 static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
307 int *qerr)
309 struct drr_sched *q = qdisc_priv(sch);
310 struct drr_class *cl;
311 struct tcf_result res;
312 int result;
314 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
315 cl = drr_find_class(sch, skb->priority);
316 if (cl != NULL)
317 return cl;
320 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
321 result = tc_classify(skb, q->filter_list, &res);
322 if (result >= 0) {
323 #ifdef CONFIG_NET_CLS_ACT
324 switch (result) {
325 case TC_ACT_QUEUED:
326 case TC_ACT_STOLEN:
327 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
328 case TC_ACT_SHOT:
329 return NULL;
331 #endif
332 cl = (struct drr_class *)res.class;
333 if (cl == NULL)
334 cl = drr_find_class(sch, res.classid);
335 return cl;
337 return NULL;
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;
344 unsigned int len;
345 int err;
347 cl = drr_classify(skb, sch, &err);
348 if (cl == NULL) {
349 if (err & __NET_XMIT_BYPASS)
350 sch->qstats.drops++;
351 kfree_skb(skb);
352 return err;
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)) {
359 cl->qstats.drops++;
360 sch->qstats.drops++;
362 return 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;
375 sch->q.qlen++;
376 return err;
379 static struct sk_buff *drr_dequeue(struct Qdisc *sch)
381 struct drr_sched *q = qdisc_priv(sch);
382 struct drr_class *cl;
383 struct sk_buff *skb;
384 unsigned int len;
386 if (list_empty(&q->active))
387 goto out;
388 while (1) {
389 cl = list_first_entry(&q->active, struct drr_class, alist);
390 skb = cl->qdisc->ops->peek(cl->qdisc);
391 if (skb == NULL)
392 goto out;
394 len = qdisc_pkt_len(skb);
395 if (len <= cl->deficit) {
396 cl->deficit -= len;
397 skb = qdisc_dequeue_peeked(cl->qdisc);
398 if (cl->qdisc->q.qlen == 0)
399 list_del(&cl->alist);
400 sch->q.qlen--;
401 return skb;
404 cl->deficit += cl->quantum;
405 list_move_tail(&cl->alist, &q->active);
407 out:
408 return NULL;
411 static unsigned int drr_drop(struct Qdisc *sch)
413 struct drr_sched *q = qdisc_priv(sch);
414 struct drr_class *cl;
415 unsigned int len;
417 list_for_each_entry(cl, &q->active, alist) {
418 if (cl->qdisc->ops->drop) {
419 len = cl->qdisc->ops->drop(cl->qdisc);
420 if (len > 0) {
421 sch->q.qlen--;
422 if (cl->qdisc->q.qlen == 0)
423 list_del(&cl->alist);
424 return len;
428 return 0;
431 static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
433 struct drr_sched *q = qdisc_priv(sch);
434 int err;
436 err = qdisc_class_hash_init(&q->clhash);
437 if (err < 0)
438 return err;
439 INIT_LIST_HEAD(&q->active);
440 return 0;
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;
448 unsigned int i;
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);
457 sch->q.qlen = 0;
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;
465 unsigned int i;
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],
471 common.hnode)
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,
490 .walk = drr_walk,
493 static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
494 .cl_ops = &drr_class_ops,
495 .id = "drr",
496 .priv_size = sizeof(struct drr_sched),
497 .enqueue = drr_enqueue,
498 .dequeue = drr_dequeue,
499 .peek = qdisc_peek_dequeued,
500 .drop = drr_drop,
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");