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];
50 struct Qdisc *prio_classify(struct sk_buff *skb, struct Qdisc *sch,int *r)
52 struct prio_sched_data *q = qdisc_priv(sch);
53 u32 band = skb->priority;
54 struct tcf_result res;
56 if (TC_H_MAJ(skb->priority) != sch->handle) {
57 #ifdef CONFIG_NET_CLS_ACT
58 int result = 0, terminal = 0;
59 result = tc_classify(skb, q->filter_list, &res);
70 case TC_ACT_RECLASSIFY:
81 if (!q->filter_list ) {
83 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
87 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
91 band = TC_H_MIN(band) - 1;
93 return q->queues[q->prio2band[0]];
95 return q->queues[band];
99 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
102 int ret = NET_XMIT_SUCCESS;
104 qdisc = prio_classify(skb, sch, &ret);
109 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
110 sch->bstats.bytes += skb->len;
111 sch->bstats.packets++;
113 return NET_XMIT_SUCCESS;
117 #ifdef CONFIG_NET_CLS_ACT
118 if (NET_XMIT_DROP == ret) {
121 #ifdef CONFIG_NET_CLS_ACT
123 sch->qstats.overlimits++; /* abuse, but noone uses it */
131 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
134 int ret = NET_XMIT_DROP;
136 qdisc = prio_classify(skb, sch, &ret);
140 if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
142 sch->qstats.requeues++;
147 return NET_XMIT_DROP;
151 static struct sk_buff *
152 prio_dequeue(struct Qdisc* sch)
155 struct prio_sched_data *q = qdisc_priv(sch);
159 for (prio = 0; prio < q->bands; prio++) {
160 qdisc = q->queues[prio];
161 skb = qdisc->dequeue(qdisc);
171 static unsigned int prio_drop(struct Qdisc* sch)
173 struct prio_sched_data *q = qdisc_priv(sch);
178 for (prio = q->bands-1; prio >= 0; prio--) {
179 qdisc = q->queues[prio];
180 if ((len = qdisc->ops->drop(qdisc)) != 0) {
190 prio_reset(struct Qdisc* sch)
193 struct prio_sched_data *q = qdisc_priv(sch);
195 for (prio=0; prio<q->bands; prio++)
196 qdisc_reset(q->queues[prio]);
201 prio_destroy(struct Qdisc* sch)
204 struct prio_sched_data *q = qdisc_priv(sch);
205 struct tcf_proto *tp;
207 while ((tp = q->filter_list) != NULL) {
208 q->filter_list = tp->next;
212 for (prio=0; prio<q->bands; prio++)
213 qdisc_destroy(q->queues[prio]);
216 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
218 struct prio_sched_data *q = qdisc_priv(sch);
219 struct tc_prio_qopt *qopt = RTA_DATA(opt);
222 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
224 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
227 for (i=0; i<=TC_PRIO_MAX; i++) {
228 if (qopt->priomap[i] >= qopt->bands)
233 q->bands = qopt->bands;
234 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
236 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
237 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
238 if (child != &noop_qdisc)
239 qdisc_destroy(child);
241 sch_tree_unlock(sch);
243 for (i=0; i<=TC_PRIO_MAX; i++) {
244 int band = q->prio2band[i];
245 if (q->queues[band] == &noop_qdisc) {
247 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
250 child = xchg(&q->queues[band], child);
252 if (child != &noop_qdisc)
253 qdisc_destroy(child);
254 sch_tree_unlock(sch);
261 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
263 struct prio_sched_data *q = qdisc_priv(sch);
266 for (i=0; i<TCQ_PRIO_BANDS; i++)
267 q->queues[i] = &noop_qdisc;
274 if ((err= prio_tune(sch, opt)) != 0)
280 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
282 struct prio_sched_data *q = qdisc_priv(sch);
283 unsigned char *b = skb->tail;
284 struct tc_prio_qopt opt;
286 opt.bands = q->bands;
287 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
288 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
292 skb_trim(skb, b - skb->data);
296 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
299 struct prio_sched_data *q = qdisc_priv(sch);
300 unsigned long band = arg - 1;
302 if (band >= q->bands)
309 *old = q->queues[band];
310 q->queues[band] = new;
311 sch->q.qlen -= (*old)->q.qlen;
313 sch_tree_unlock(sch);
318 static struct Qdisc *
319 prio_leaf(struct Qdisc *sch, unsigned long arg)
321 struct prio_sched_data *q = qdisc_priv(sch);
322 unsigned long band = arg - 1;
324 if (band >= q->bands)
327 return q->queues[band];
330 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
332 struct prio_sched_data *q = qdisc_priv(sch);
333 unsigned long band = TC_H_MIN(classid);
335 if (band - 1 >= q->bands)
340 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
342 return prio_get(sch, classid);
346 static void prio_put(struct Qdisc *q, unsigned long cl)
351 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
353 unsigned long cl = *arg;
354 struct prio_sched_data *q = qdisc_priv(sch);
356 if (cl - 1 > q->bands)
361 static int prio_delete(struct Qdisc *sch, unsigned long cl)
363 struct prio_sched_data *q = qdisc_priv(sch);
364 if (cl - 1 > q->bands)
370 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
373 struct prio_sched_data *q = qdisc_priv(sch);
375 if (cl - 1 > q->bands)
377 tcm->tcm_handle |= TC_H_MIN(cl);
379 tcm->tcm_info = q->queues[cl-1]->handle;
383 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
385 struct prio_sched_data *q = qdisc_priv(sch);
391 for (prio = 0; prio < q->bands; prio++) {
392 if (arg->count < arg->skip) {
396 if (arg->fn(sch, prio+1, arg) < 0) {
404 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
406 struct prio_sched_data *q = qdisc_priv(sch);
410 return &q->filter_list;
413 static struct Qdisc_class_ops prio_class_ops = {
418 .change = prio_change,
419 .delete = prio_delete,
421 .tcf_chain = prio_find_tcf,
422 .bind_tcf = prio_bind,
423 .unbind_tcf = prio_put,
424 .dump = prio_dump_class,
427 static struct Qdisc_ops prio_qdisc_ops = {
429 .cl_ops = &prio_class_ops,
431 .priv_size = sizeof(struct prio_sched_data),
432 .enqueue = prio_enqueue,
433 .dequeue = prio_dequeue,
434 .requeue = prio_requeue,
438 .destroy = prio_destroy,
441 .owner = THIS_MODULE,
444 static int __init prio_module_init(void)
446 return register_qdisc(&prio_qdisc_ops);
449 static void __exit prio_module_exit(void)
451 unregister_qdisc(&prio_qdisc_ops);
454 module_init(prio_module_init)
455 module_exit(prio_module_exit)
457 MODULE_LICENSE("GPL");