[SCHED]: Qdisc changes and sch_rr added for multiqueue
[linux-2.6/btrfs-unstable.git] / net / sched / sch_prio.c
blob404522046289cba0fddcad38dff6a4d7608ba17f
1 /*
2 * net/sched/sch_prio.c Simple 3-band priority "scheduler".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
11 * Init -- EINVAL when opt undefined
14 #include <linux/module.h>
15 #include <asm/uaccess.h>
16 #include <asm/system.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/in.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
32 #include <net/ip.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
35 #include <net/netlink.h>
36 #include <net/sock.h>
37 #include <net/pkt_sched.h>
40 struct prio_sched_data
42 int bands;
43 int curband; /* for round-robin */
44 struct tcf_proto *filter_list;
45 u8 prio2band[TC_PRIO_MAX+1];
46 struct Qdisc *queues[TCQ_PRIO_BANDS];
47 int mq;
51 static struct Qdisc *
52 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
54 struct prio_sched_data *q = qdisc_priv(sch);
55 u32 band = skb->priority;
56 struct tcf_result res;
58 *qerr = NET_XMIT_BYPASS;
59 if (TC_H_MAJ(skb->priority) != sch->handle) {
60 #ifdef CONFIG_NET_CLS_ACT
61 switch (tc_classify(skb, q->filter_list, &res)) {
62 case TC_ACT_STOLEN:
63 case TC_ACT_QUEUED:
64 *qerr = NET_XMIT_SUCCESS;
65 case TC_ACT_SHOT:
66 return NULL;
69 if (!q->filter_list ) {
70 #else
71 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
72 #endif
73 if (TC_H_MAJ(band))
74 band = 0;
75 band = q->prio2band[band&TC_PRIO_MAX];
76 goto out;
78 band = res.classid;
80 band = TC_H_MIN(band) - 1;
81 if (band >= q->bands)
82 band = q->prio2band[0];
83 out:
84 if (q->mq)
85 skb_set_queue_mapping(skb, band);
86 return q->queues[band];
89 static int
90 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
92 struct Qdisc *qdisc;
93 int ret;
95 qdisc = prio_classify(skb, sch, &ret);
96 #ifdef CONFIG_NET_CLS_ACT
97 if (qdisc == NULL) {
99 if (ret == NET_XMIT_BYPASS)
100 sch->qstats.drops++;
101 kfree_skb(skb);
102 return ret;
104 #endif
106 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
107 sch->bstats.bytes += skb->len;
108 sch->bstats.packets++;
109 sch->q.qlen++;
110 return NET_XMIT_SUCCESS;
112 sch->qstats.drops++;
113 return ret;
117 static int
118 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
120 struct Qdisc *qdisc;
121 int ret;
123 qdisc = prio_classify(skb, sch, &ret);
124 #ifdef CONFIG_NET_CLS_ACT
125 if (qdisc == NULL) {
126 if (ret == NET_XMIT_BYPASS)
127 sch->qstats.drops++;
128 kfree_skb(skb);
129 return ret;
131 #endif
133 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
134 sch->q.qlen++;
135 sch->qstats.requeues++;
136 return 0;
138 sch->qstats.drops++;
139 return NET_XMIT_DROP;
143 static struct sk_buff *
144 prio_dequeue(struct Qdisc* sch)
146 struct sk_buff *skb;
147 struct prio_sched_data *q = qdisc_priv(sch);
148 int prio;
149 struct Qdisc *qdisc;
151 for (prio = 0; prio < q->bands; prio++) {
152 /* Check if the target subqueue is available before
153 * pulling an skb. This way we avoid excessive requeues
154 * for slower queues.
156 if (!netif_subqueue_stopped(sch->dev, (q->mq ? prio : 0))) {
157 qdisc = q->queues[prio];
158 skb = qdisc->dequeue(qdisc);
159 if (skb) {
160 sch->q.qlen--;
161 return skb;
165 return NULL;
169 static struct sk_buff *rr_dequeue(struct Qdisc* sch)
171 struct sk_buff *skb;
172 struct prio_sched_data *q = qdisc_priv(sch);
173 struct Qdisc *qdisc;
174 int bandcount;
176 /* Only take one pass through the queues. If nothing is available,
177 * return nothing.
179 for (bandcount = 0; bandcount < q->bands; bandcount++) {
180 /* Check if the target subqueue is available before
181 * pulling an skb. This way we avoid excessive requeues
182 * for slower queues. If the queue is stopped, try the
183 * next queue.
185 if (!netif_subqueue_stopped(sch->dev,
186 (q->mq ? q->curband : 0))) {
187 qdisc = q->queues[q->curband];
188 skb = qdisc->dequeue(qdisc);
189 if (skb) {
190 sch->q.qlen--;
191 q->curband++;
192 if (q->curband >= q->bands)
193 q->curband = 0;
194 return skb;
197 q->curband++;
198 if (q->curband >= q->bands)
199 q->curband = 0;
201 return NULL;
204 static unsigned int prio_drop(struct Qdisc* sch)
206 struct prio_sched_data *q = qdisc_priv(sch);
207 int prio;
208 unsigned int len;
209 struct Qdisc *qdisc;
211 for (prio = q->bands-1; prio >= 0; prio--) {
212 qdisc = q->queues[prio];
213 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
214 sch->q.qlen--;
215 return len;
218 return 0;
222 static void
223 prio_reset(struct Qdisc* sch)
225 int prio;
226 struct prio_sched_data *q = qdisc_priv(sch);
228 for (prio=0; prio<q->bands; prio++)
229 qdisc_reset(q->queues[prio]);
230 sch->q.qlen = 0;
233 static void
234 prio_destroy(struct Qdisc* sch)
236 int prio;
237 struct prio_sched_data *q = qdisc_priv(sch);
239 tcf_destroy_chain(q->filter_list);
240 for (prio=0; prio<q->bands; prio++)
241 qdisc_destroy(q->queues[prio]);
244 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
246 struct prio_sched_data *q = qdisc_priv(sch);
247 struct tc_prio_qopt *qopt;
248 struct rtattr *tb[TCA_PRIO_MAX];
249 int i;
251 if (rtattr_parse_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
252 sizeof(*qopt)))
253 return -EINVAL;
254 q->bands = qopt->bands;
255 /* If we're multiqueue, make sure the number of incoming bands
256 * matches the number of queues on the device we're associating with.
257 * If the number of bands requested is zero, then set q->bands to
258 * dev->egress_subqueue_count.
260 q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
261 if (q->mq) {
262 if (sch->handle != TC_H_ROOT)
263 return -EINVAL;
264 if (netif_is_multiqueue(sch->dev)) {
265 if (q->bands == 0)
266 q->bands = sch->dev->egress_subqueue_count;
267 else if (q->bands != sch->dev->egress_subqueue_count)
268 return -EINVAL;
269 } else
270 return -EOPNOTSUPP;
273 if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
274 return -EINVAL;
276 for (i=0; i<=TC_PRIO_MAX; i++) {
277 if (qopt->priomap[i] >= q->bands)
278 return -EINVAL;
281 sch_tree_lock(sch);
282 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
284 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
285 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
286 if (child != &noop_qdisc) {
287 qdisc_tree_decrease_qlen(child, child->q.qlen);
288 qdisc_destroy(child);
291 sch_tree_unlock(sch);
293 for (i=0; i<q->bands; i++) {
294 if (q->queues[i] == &noop_qdisc) {
295 struct Qdisc *child;
296 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
297 TC_H_MAKE(sch->handle, i + 1));
298 if (child) {
299 sch_tree_lock(sch);
300 child = xchg(&q->queues[i], child);
302 if (child != &noop_qdisc) {
303 qdisc_tree_decrease_qlen(child,
304 child->q.qlen);
305 qdisc_destroy(child);
307 sch_tree_unlock(sch);
311 return 0;
314 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
316 struct prio_sched_data *q = qdisc_priv(sch);
317 int i;
319 for (i=0; i<TCQ_PRIO_BANDS; i++)
320 q->queues[i] = &noop_qdisc;
322 if (opt == NULL) {
323 return -EINVAL;
324 } else {
325 int err;
327 if ((err= prio_tune(sch, opt)) != 0)
328 return err;
330 return 0;
333 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
335 struct prio_sched_data *q = qdisc_priv(sch);
336 unsigned char *b = skb_tail_pointer(skb);
337 struct rtattr *nest;
338 struct tc_prio_qopt opt;
340 opt.bands = q->bands;
341 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
343 nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
344 if (q->mq)
345 RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
346 RTA_NEST_COMPAT_END(skb, nest);
348 return skb->len;
350 rtattr_failure:
351 nlmsg_trim(skb, b);
352 return -1;
355 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
356 struct Qdisc **old)
358 struct prio_sched_data *q = qdisc_priv(sch);
359 unsigned long band = arg - 1;
361 if (band >= q->bands)
362 return -EINVAL;
364 if (new == NULL)
365 new = &noop_qdisc;
367 sch_tree_lock(sch);
368 *old = q->queues[band];
369 q->queues[band] = new;
370 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
371 qdisc_reset(*old);
372 sch_tree_unlock(sch);
374 return 0;
377 static struct Qdisc *
378 prio_leaf(struct Qdisc *sch, unsigned long arg)
380 struct prio_sched_data *q = qdisc_priv(sch);
381 unsigned long band = arg - 1;
383 if (band >= q->bands)
384 return NULL;
386 return q->queues[band];
389 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
391 struct prio_sched_data *q = qdisc_priv(sch);
392 unsigned long band = TC_H_MIN(classid);
394 if (band - 1 >= q->bands)
395 return 0;
396 return band;
399 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
401 return prio_get(sch, classid);
405 static void prio_put(struct Qdisc *q, unsigned long cl)
407 return;
410 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
412 unsigned long cl = *arg;
413 struct prio_sched_data *q = qdisc_priv(sch);
415 if (cl - 1 > q->bands)
416 return -ENOENT;
417 return 0;
420 static int prio_delete(struct Qdisc *sch, unsigned long cl)
422 struct prio_sched_data *q = qdisc_priv(sch);
423 if (cl - 1 > q->bands)
424 return -ENOENT;
425 return 0;
429 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
430 struct tcmsg *tcm)
432 struct prio_sched_data *q = qdisc_priv(sch);
434 if (cl - 1 > q->bands)
435 return -ENOENT;
436 tcm->tcm_handle |= TC_H_MIN(cl);
437 if (q->queues[cl-1])
438 tcm->tcm_info = q->queues[cl-1]->handle;
439 return 0;
442 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
443 struct gnet_dump *d)
445 struct prio_sched_data *q = qdisc_priv(sch);
446 struct Qdisc *cl_q;
448 cl_q = q->queues[cl - 1];
449 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
450 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
451 return -1;
453 return 0;
456 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
458 struct prio_sched_data *q = qdisc_priv(sch);
459 int prio;
461 if (arg->stop)
462 return;
464 for (prio = 0; prio < q->bands; prio++) {
465 if (arg->count < arg->skip) {
466 arg->count++;
467 continue;
469 if (arg->fn(sch, prio+1, arg) < 0) {
470 arg->stop = 1;
471 break;
473 arg->count++;
477 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
479 struct prio_sched_data *q = qdisc_priv(sch);
481 if (cl)
482 return NULL;
483 return &q->filter_list;
486 static struct Qdisc_class_ops prio_class_ops = {
487 .graft = prio_graft,
488 .leaf = prio_leaf,
489 .get = prio_get,
490 .put = prio_put,
491 .change = prio_change,
492 .delete = prio_delete,
493 .walk = prio_walk,
494 .tcf_chain = prio_find_tcf,
495 .bind_tcf = prio_bind,
496 .unbind_tcf = prio_put,
497 .dump = prio_dump_class,
498 .dump_stats = prio_dump_class_stats,
501 static struct Qdisc_ops prio_qdisc_ops = {
502 .next = NULL,
503 .cl_ops = &prio_class_ops,
504 .id = "prio",
505 .priv_size = sizeof(struct prio_sched_data),
506 .enqueue = prio_enqueue,
507 .dequeue = prio_dequeue,
508 .requeue = prio_requeue,
509 .drop = prio_drop,
510 .init = prio_init,
511 .reset = prio_reset,
512 .destroy = prio_destroy,
513 .change = prio_tune,
514 .dump = prio_dump,
515 .owner = THIS_MODULE,
518 static struct Qdisc_ops rr_qdisc_ops = {
519 .next = NULL,
520 .cl_ops = &prio_class_ops,
521 .id = "rr",
522 .priv_size = sizeof(struct prio_sched_data),
523 .enqueue = prio_enqueue,
524 .dequeue = rr_dequeue,
525 .requeue = prio_requeue,
526 .drop = prio_drop,
527 .init = prio_init,
528 .reset = prio_reset,
529 .destroy = prio_destroy,
530 .change = prio_tune,
531 .dump = prio_dump,
532 .owner = THIS_MODULE,
535 static int __init prio_module_init(void)
537 int err;
539 err = register_qdisc(&prio_qdisc_ops);
540 if (err < 0)
541 return err;
542 err = register_qdisc(&rr_qdisc_ops);
543 if (err < 0)
544 unregister_qdisc(&prio_qdisc_ops);
545 return err;
548 static void __exit prio_module_exit(void)
550 unregister_qdisc(&prio_qdisc_ops);
551 unregister_qdisc(&rr_qdisc_ops);
554 module_init(prio_module_init)
555 module_exit(prio_module_exit)
557 MODULE_LICENSE("GPL");
558 MODULE_ALIAS("sch_rr");