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/config.h>
15 #include <linux/module.h>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/bitops.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/if_ether.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/notifier.h>
35 #include <net/route.h>
36 #include <linux/skbuff.h>
38 #include <net/pkt_sched.h>
41 struct prio_sched_data
44 struct tcf_proto *filter_list;
45 u8 prio2band[TC_PRIO_MAX+1];
46 struct Qdisc *queues[TCQ_PRIO_BANDS];
51 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
53 struct prio_sched_data *q = qdisc_priv(sch);
54 u32 band = skb->priority;
55 struct tcf_result res;
57 *qerr = NET_XMIT_BYPASS;
58 if (TC_H_MAJ(skb->priority) != sch->handle) {
59 #ifdef CONFIG_NET_CLS_ACT
60 switch (tc_classify(skb, q->filter_list, &res)) {
63 *qerr = NET_XMIT_SUCCESS;
68 if (!q->filter_list ) {
70 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
74 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
78 band = TC_H_MIN(band) - 1;
80 return q->queues[q->prio2band[0]];
82 return q->queues[band];
86 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
91 qdisc = prio_classify(skb, sch, &ret);
92 #ifdef CONFIG_NET_CLS_ACT
95 if (ret == NET_XMIT_BYPASS)
102 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
103 sch->bstats.bytes += skb->len;
104 sch->bstats.packets++;
106 return NET_XMIT_SUCCESS;
114 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
119 qdisc = prio_classify(skb, sch, &ret);
120 #ifdef CONFIG_NET_CLS_ACT
122 if (ret == NET_XMIT_BYPASS)
129 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
131 sch->qstats.requeues++;
135 return NET_XMIT_DROP;
139 static struct sk_buff *
140 prio_dequeue(struct Qdisc* sch)
143 struct prio_sched_data *q = qdisc_priv(sch);
147 for (prio = 0; prio < q->bands; prio++) {
148 qdisc = q->queues[prio];
149 skb = qdisc->dequeue(qdisc);
159 static unsigned int prio_drop(struct Qdisc* sch)
161 struct prio_sched_data *q = qdisc_priv(sch);
166 for (prio = q->bands-1; prio >= 0; prio--) {
167 qdisc = q->queues[prio];
168 if ((len = qdisc->ops->drop(qdisc)) != 0) {
178 prio_reset(struct Qdisc* sch)
181 struct prio_sched_data *q = qdisc_priv(sch);
183 for (prio=0; prio<q->bands; prio++)
184 qdisc_reset(q->queues[prio]);
189 prio_destroy(struct Qdisc* sch)
192 struct prio_sched_data *q = qdisc_priv(sch);
193 struct tcf_proto *tp;
195 while ((tp = q->filter_list) != NULL) {
196 q->filter_list = tp->next;
200 for (prio=0; prio<q->bands; prio++)
201 qdisc_destroy(q->queues[prio]);
204 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
206 struct prio_sched_data *q = qdisc_priv(sch);
207 struct tc_prio_qopt *qopt = RTA_DATA(opt);
210 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
212 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
215 for (i=0; i<=TC_PRIO_MAX; i++) {
216 if (qopt->priomap[i] >= qopt->bands)
221 q->bands = qopt->bands;
222 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
224 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
225 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
226 if (child != &noop_qdisc)
227 qdisc_destroy(child);
229 sch_tree_unlock(sch);
231 for (i=0; i<=TC_PRIO_MAX; i++) {
232 int band = q->prio2band[i];
233 if (q->queues[band] == &noop_qdisc) {
235 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
238 child = xchg(&q->queues[band], child);
240 if (child != &noop_qdisc)
241 qdisc_destroy(child);
242 sch_tree_unlock(sch);
249 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
251 struct prio_sched_data *q = qdisc_priv(sch);
254 for (i=0; i<TCQ_PRIO_BANDS; i++)
255 q->queues[i] = &noop_qdisc;
262 if ((err= prio_tune(sch, opt)) != 0)
268 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
270 struct prio_sched_data *q = qdisc_priv(sch);
271 unsigned char *b = skb->tail;
272 struct tc_prio_qopt opt;
274 opt.bands = q->bands;
275 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
276 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
280 skb_trim(skb, b - skb->data);
284 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
287 struct prio_sched_data *q = qdisc_priv(sch);
288 unsigned long band = arg - 1;
290 if (band >= q->bands)
297 *old = q->queues[band];
298 q->queues[band] = new;
299 sch->q.qlen -= (*old)->q.qlen;
301 sch_tree_unlock(sch);
306 static struct Qdisc *
307 prio_leaf(struct Qdisc *sch, unsigned long arg)
309 struct prio_sched_data *q = qdisc_priv(sch);
310 unsigned long band = arg - 1;
312 if (band >= q->bands)
315 return q->queues[band];
318 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
320 struct prio_sched_data *q = qdisc_priv(sch);
321 unsigned long band = TC_H_MIN(classid);
323 if (band - 1 >= q->bands)
328 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
330 return prio_get(sch, classid);
334 static void prio_put(struct Qdisc *q, unsigned long cl)
339 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
341 unsigned long cl = *arg;
342 struct prio_sched_data *q = qdisc_priv(sch);
344 if (cl - 1 > q->bands)
349 static int prio_delete(struct Qdisc *sch, unsigned long cl)
351 struct prio_sched_data *q = qdisc_priv(sch);
352 if (cl - 1 > q->bands)
358 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
361 struct prio_sched_data *q = qdisc_priv(sch);
363 if (cl - 1 > q->bands)
365 tcm->tcm_handle |= TC_H_MIN(cl);
367 tcm->tcm_info = q->queues[cl-1]->handle;
371 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
373 struct prio_sched_data *q = qdisc_priv(sch);
379 for (prio = 0; prio < q->bands; prio++) {
380 if (arg->count < arg->skip) {
384 if (arg->fn(sch, prio+1, arg) < 0) {
392 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
394 struct prio_sched_data *q = qdisc_priv(sch);
398 return &q->filter_list;
401 static struct Qdisc_class_ops prio_class_ops = {
406 .change = prio_change,
407 .delete = prio_delete,
409 .tcf_chain = prio_find_tcf,
410 .bind_tcf = prio_bind,
411 .unbind_tcf = prio_put,
412 .dump = prio_dump_class,
415 static struct Qdisc_ops prio_qdisc_ops = {
417 .cl_ops = &prio_class_ops,
419 .priv_size = sizeof(struct prio_sched_data),
420 .enqueue = prio_enqueue,
421 .dequeue = prio_dequeue,
422 .requeue = prio_requeue,
426 .destroy = prio_destroy,
429 .owner = THIS_MODULE,
432 static int __init prio_module_init(void)
434 return register_qdisc(&prio_qdisc_ops);
437 static void __exit prio_module_exit(void)
439 unregister_qdisc(&prio_qdisc_ops);
442 module_init(prio_module_init)
443 module_exit(prio_module_exit)
445 MODULE_LICENSE("GPL");