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 <asm/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->stats.bytes += skb->len;
111 sch->stats.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->stats.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) {
146 return NET_XMIT_DROP;
150 static struct sk_buff *
151 prio_dequeue(struct Qdisc* sch)
154 struct prio_sched_data *q = qdisc_priv(sch);
158 for (prio = 0; prio < q->bands; prio++) {
159 qdisc = q->queues[prio];
160 skb = qdisc->dequeue(qdisc);
170 static unsigned int prio_drop(struct Qdisc* sch)
172 struct prio_sched_data *q = qdisc_priv(sch);
177 for (prio = q->bands-1; prio >= 0; prio--) {
178 qdisc = q->queues[prio];
179 if ((len = qdisc->ops->drop(qdisc)) != 0) {
189 prio_reset(struct Qdisc* sch)
192 struct prio_sched_data *q = qdisc_priv(sch);
194 for (prio=0; prio<q->bands; prio++)
195 qdisc_reset(q->queues[prio]);
200 prio_destroy(struct Qdisc* sch)
203 struct prio_sched_data *q = qdisc_priv(sch);
204 struct tcf_proto *tp;
206 while ((tp = q->filter_list) != NULL) {
207 q->filter_list = tp->next;
211 for (prio=0; prio<q->bands; prio++)
212 qdisc_destroy(q->queues[prio]);
215 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
217 struct prio_sched_data *q = qdisc_priv(sch);
218 struct tc_prio_qopt *qopt = RTA_DATA(opt);
221 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
223 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
226 for (i=0; i<=TC_PRIO_MAX; i++) {
227 if (qopt->priomap[i] >= qopt->bands)
232 q->bands = qopt->bands;
233 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
235 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
236 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
237 if (child != &noop_qdisc)
238 qdisc_destroy(child);
240 sch_tree_unlock(sch);
242 for (i=0; i<=TC_PRIO_MAX; i++) {
243 int band = q->prio2band[i];
244 if (q->queues[band] == &noop_qdisc) {
246 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
249 child = xchg(&q->queues[band], child);
251 if (child != &noop_qdisc)
252 qdisc_destroy(child);
253 sch_tree_unlock(sch);
260 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
262 struct prio_sched_data *q = qdisc_priv(sch);
265 for (i=0; i<TCQ_PRIO_BANDS; i++)
266 q->queues[i] = &noop_qdisc;
273 if ((err= prio_tune(sch, opt)) != 0)
279 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
281 struct prio_sched_data *q = qdisc_priv(sch);
282 unsigned char *b = skb->tail;
283 struct tc_prio_qopt opt;
285 opt.bands = q->bands;
286 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
287 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
291 skb_trim(skb, b - skb->data);
295 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
298 struct prio_sched_data *q = qdisc_priv(sch);
299 unsigned long band = arg - 1;
301 if (band >= q->bands)
308 *old = q->queues[band];
309 q->queues[band] = new;
310 sch->q.qlen -= (*old)->q.qlen;
312 sch_tree_unlock(sch);
317 static struct Qdisc *
318 prio_leaf(struct Qdisc *sch, unsigned long arg)
320 struct prio_sched_data *q = qdisc_priv(sch);
321 unsigned long band = arg - 1;
323 if (band >= q->bands)
326 return q->queues[band];
329 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
331 struct prio_sched_data *q = qdisc_priv(sch);
332 unsigned long band = TC_H_MIN(classid);
334 if (band - 1 >= q->bands)
339 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
341 return prio_get(sch, classid);
345 static void prio_put(struct Qdisc *q, unsigned long cl)
350 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
352 unsigned long cl = *arg;
353 struct prio_sched_data *q = qdisc_priv(sch);
355 if (cl - 1 > q->bands)
360 static int prio_delete(struct Qdisc *sch, unsigned long cl)
362 struct prio_sched_data *q = qdisc_priv(sch);
363 if (cl - 1 > q->bands)
369 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
372 struct prio_sched_data *q = qdisc_priv(sch);
374 if (cl - 1 > q->bands)
376 tcm->tcm_handle |= TC_H_MIN(cl);
378 tcm->tcm_info = q->queues[cl-1]->handle;
382 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
384 struct prio_sched_data *q = qdisc_priv(sch);
390 for (prio = 0; prio < q->bands; prio++) {
391 if (arg->count < arg->skip) {
395 if (arg->fn(sch, prio+1, arg) < 0) {
403 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
405 struct prio_sched_data *q = qdisc_priv(sch);
409 return &q->filter_list;
412 static struct Qdisc_class_ops prio_class_ops = {
417 .change = prio_change,
418 .delete = prio_delete,
420 .tcf_chain = prio_find_tcf,
421 .bind_tcf = prio_bind,
422 .unbind_tcf = prio_put,
423 .dump = prio_dump_class,
426 static struct Qdisc_ops prio_qdisc_ops = {
428 .cl_ops = &prio_class_ops,
430 .priv_size = sizeof(struct prio_sched_data),
431 .enqueue = prio_enqueue,
432 .dequeue = prio_dequeue,
433 .requeue = prio_requeue,
437 .destroy = prio_destroy,
440 .owner = THIS_MODULE,
443 static int __init prio_module_init(void)
445 return register_qdisc(&prio_qdisc_ops);
448 static void __exit prio_module_exit(void)
450 unregister_qdisc(&prio_qdisc_ops);
453 module_init(prio_module_init)
454 module_exit(prio_module_exit)
456 MODULE_LICENSE("GPL");