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 <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
24 struct prio_sched_data
27 int curband; /* for round-robin */
28 struct tcf_proto *filter_list;
29 u8 prio2band[TC_PRIO_MAX+1];
30 struct Qdisc *queues[TCQ_PRIO_BANDS];
36 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
38 struct prio_sched_data *q = qdisc_priv(sch);
39 u32 band = skb->priority;
40 struct tcf_result res;
43 *qerr = NET_XMIT_BYPASS;
44 if (TC_H_MAJ(skb->priority) != sch->handle) {
45 err = tc_classify(skb, q->filter_list, &res);
46 #ifdef CONFIG_NET_CLS_ACT
50 *qerr = NET_XMIT_SUCCESS;
55 if (!q->filter_list || err < 0) {
58 band = q->prio2band[band&TC_PRIO_MAX];
63 band = TC_H_MIN(band) - 1;
65 band = q->prio2band[0];
68 skb_set_queue_mapping(skb, band);
69 return q->queues[band];
73 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
78 qdisc = prio_classify(skb, sch, &ret);
79 #ifdef CONFIG_NET_CLS_ACT
82 if (ret == NET_XMIT_BYPASS)
89 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
90 sch->bstats.bytes += skb->len;
91 sch->bstats.packets++;
93 return NET_XMIT_SUCCESS;
101 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
106 qdisc = prio_classify(skb, sch, &ret);
107 #ifdef CONFIG_NET_CLS_ACT
109 if (ret == NET_XMIT_BYPASS)
116 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
118 sch->qstats.requeues++;
122 return NET_XMIT_DROP;
126 static struct sk_buff *
127 prio_dequeue(struct Qdisc* sch)
130 struct prio_sched_data *q = qdisc_priv(sch);
134 for (prio = 0; prio < q->bands; prio++) {
135 /* Check if the target subqueue is available before
136 * pulling an skb. This way we avoid excessive requeues
139 if (!netif_subqueue_stopped(sch->dev, (q->mq ? prio : 0))) {
140 qdisc = q->queues[prio];
141 skb = qdisc->dequeue(qdisc);
152 static struct sk_buff *rr_dequeue(struct Qdisc* sch)
155 struct prio_sched_data *q = qdisc_priv(sch);
159 /* Only take one pass through the queues. If nothing is available,
162 for (bandcount = 0; bandcount < q->bands; bandcount++) {
163 /* Check if the target subqueue is available before
164 * pulling an skb. This way we avoid excessive requeues
165 * for slower queues. If the queue is stopped, try the
168 if (!netif_subqueue_stopped(sch->dev,
169 (q->mq ? q->curband : 0))) {
170 qdisc = q->queues[q->curband];
171 skb = qdisc->dequeue(qdisc);
175 if (q->curband >= q->bands)
181 if (q->curband >= q->bands)
187 static unsigned int prio_drop(struct Qdisc* sch)
189 struct prio_sched_data *q = qdisc_priv(sch);
194 for (prio = q->bands-1; prio >= 0; prio--) {
195 qdisc = q->queues[prio];
196 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
206 prio_reset(struct Qdisc* sch)
209 struct prio_sched_data *q = qdisc_priv(sch);
211 for (prio=0; prio<q->bands; prio++)
212 qdisc_reset(q->queues[prio]);
217 prio_destroy(struct Qdisc* sch)
220 struct prio_sched_data *q = qdisc_priv(sch);
222 tcf_destroy_chain(q->filter_list);
223 for (prio=0; prio<q->bands; prio++)
224 qdisc_destroy(q->queues[prio]);
227 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
229 struct prio_sched_data *q = qdisc_priv(sch);
230 struct tc_prio_qopt *qopt;
231 struct rtattr *tb[TCA_PRIO_MAX];
234 if (rtattr_parse_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
237 q->bands = qopt->bands;
238 /* If we're multiqueue, make sure the number of incoming bands
239 * matches the number of queues on the device we're associating with.
240 * If the number of bands requested is zero, then set q->bands to
241 * dev->egress_subqueue_count. Also, the root qdisc must be the
242 * only one that is enabled for multiqueue, since it's the only one
243 * that interacts with the underlying device.
245 q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
247 if (sch->parent != TC_H_ROOT)
249 if (netif_is_multiqueue(sch->dev)) {
251 q->bands = sch->dev->egress_subqueue_count;
252 else if (q->bands != sch->dev->egress_subqueue_count)
258 if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
261 for (i=0; i<=TC_PRIO_MAX; i++) {
262 if (qopt->priomap[i] >= q->bands)
267 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
269 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
270 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
271 if (child != &noop_qdisc) {
272 qdisc_tree_decrease_qlen(child, child->q.qlen);
273 qdisc_destroy(child);
276 sch_tree_unlock(sch);
278 for (i=0; i<q->bands; i++) {
279 if (q->queues[i] == &noop_qdisc) {
281 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
282 TC_H_MAKE(sch->handle, i + 1));
285 child = xchg(&q->queues[i], child);
287 if (child != &noop_qdisc) {
288 qdisc_tree_decrease_qlen(child,
290 qdisc_destroy(child);
292 sch_tree_unlock(sch);
299 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
301 struct prio_sched_data *q = qdisc_priv(sch);
304 for (i=0; i<TCQ_PRIO_BANDS; i++)
305 q->queues[i] = &noop_qdisc;
312 if ((err= prio_tune(sch, opt)) != 0)
318 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
320 struct prio_sched_data *q = qdisc_priv(sch);
321 unsigned char *b = skb_tail_pointer(skb);
323 struct tc_prio_qopt opt;
325 opt.bands = q->bands;
326 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
328 nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
330 RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
331 RTA_NEST_COMPAT_END(skb, nest);
340 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
343 struct prio_sched_data *q = qdisc_priv(sch);
344 unsigned long band = arg - 1;
346 if (band >= q->bands)
353 *old = q->queues[band];
354 q->queues[band] = new;
355 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
357 sch_tree_unlock(sch);
362 static struct Qdisc *
363 prio_leaf(struct Qdisc *sch, unsigned long arg)
365 struct prio_sched_data *q = qdisc_priv(sch);
366 unsigned long band = arg - 1;
368 if (band >= q->bands)
371 return q->queues[band];
374 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
376 struct prio_sched_data *q = qdisc_priv(sch);
377 unsigned long band = TC_H_MIN(classid);
379 if (band - 1 >= q->bands)
384 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
386 return prio_get(sch, classid);
390 static void prio_put(struct Qdisc *q, unsigned long cl)
395 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
397 unsigned long cl = *arg;
398 struct prio_sched_data *q = qdisc_priv(sch);
400 if (cl - 1 > q->bands)
405 static int prio_delete(struct Qdisc *sch, unsigned long cl)
407 struct prio_sched_data *q = qdisc_priv(sch);
408 if (cl - 1 > q->bands)
414 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
417 struct prio_sched_data *q = qdisc_priv(sch);
419 if (cl - 1 > q->bands)
421 tcm->tcm_handle |= TC_H_MIN(cl);
423 tcm->tcm_info = q->queues[cl-1]->handle;
427 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
430 struct prio_sched_data *q = qdisc_priv(sch);
433 cl_q = q->queues[cl - 1];
434 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
435 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
441 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
443 struct prio_sched_data *q = qdisc_priv(sch);
449 for (prio = 0; prio < q->bands; prio++) {
450 if (arg->count < arg->skip) {
454 if (arg->fn(sch, prio+1, arg) < 0) {
462 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
464 struct prio_sched_data *q = qdisc_priv(sch);
468 return &q->filter_list;
471 static struct Qdisc_class_ops prio_class_ops = {
476 .change = prio_change,
477 .delete = prio_delete,
479 .tcf_chain = prio_find_tcf,
480 .bind_tcf = prio_bind,
481 .unbind_tcf = prio_put,
482 .dump = prio_dump_class,
483 .dump_stats = prio_dump_class_stats,
486 static struct Qdisc_ops prio_qdisc_ops = {
488 .cl_ops = &prio_class_ops,
490 .priv_size = sizeof(struct prio_sched_data),
491 .enqueue = prio_enqueue,
492 .dequeue = prio_dequeue,
493 .requeue = prio_requeue,
497 .destroy = prio_destroy,
500 .owner = THIS_MODULE,
503 static struct Qdisc_ops rr_qdisc_ops = {
505 .cl_ops = &prio_class_ops,
507 .priv_size = sizeof(struct prio_sched_data),
508 .enqueue = prio_enqueue,
509 .dequeue = rr_dequeue,
510 .requeue = prio_requeue,
514 .destroy = prio_destroy,
517 .owner = THIS_MODULE,
520 static int __init prio_module_init(void)
524 err = register_qdisc(&prio_qdisc_ops);
527 err = register_qdisc(&rr_qdisc_ops);
529 unregister_qdisc(&prio_qdisc_ops);
533 static void __exit prio_module_exit(void)
535 unregister_qdisc(&prio_qdisc_ops);
536 unregister_qdisc(&rr_qdisc_ops);
539 module_init(prio_module_init)
540 module_exit(prio_module_exit)
542 MODULE_LICENSE("GPL");
543 MODULE_ALIAS("sch_rr");