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 = (struct prio_sched_data *)sch->data;
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 = (struct prio_sched_data *)sch->data;
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 = (struct prio_sched_data *)sch->data;
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 = (struct prio_sched_data *)sch->data;
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 = (struct prio_sched_data *)sch->data;
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]);
213 q->queues[prio] = &noop_qdisc;
217 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
219 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
220 struct tc_prio_qopt *qopt = RTA_DATA(opt);
223 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
225 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
228 for (i=0; i<=TC_PRIO_MAX; i++) {
229 if (qopt->priomap[i] >= qopt->bands)
234 q->bands = qopt->bands;
235 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
237 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
238 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
239 if (child != &noop_qdisc)
240 qdisc_destroy(child);
242 sch_tree_unlock(sch);
244 for (i=0; i<=TC_PRIO_MAX; i++) {
245 int band = q->prio2band[i];
246 if (q->queues[band] == &noop_qdisc) {
248 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
251 child = xchg(&q->queues[band], child);
253 if (child != &noop_qdisc)
254 qdisc_destroy(child);
255 sch_tree_unlock(sch);
262 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
264 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
267 for (i=0; i<TCQ_PRIO_BANDS; i++)
268 q->queues[i] = &noop_qdisc;
275 if ((err= prio_tune(sch, opt)) != 0)
281 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
283 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
284 unsigned char *b = skb->tail;
285 struct tc_prio_qopt opt;
287 opt.bands = q->bands;
288 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
289 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
293 skb_trim(skb, b - skb->data);
297 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
300 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
301 unsigned long band = arg - 1;
303 if (band >= q->bands)
310 *old = q->queues[band];
311 q->queues[band] = new;
312 sch->q.qlen -= (*old)->q.qlen;
314 sch_tree_unlock(sch);
319 static struct Qdisc *
320 prio_leaf(struct Qdisc *sch, unsigned long arg)
322 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
323 unsigned long band = arg - 1;
325 if (band >= q->bands)
328 return q->queues[band];
331 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
333 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
334 unsigned long band = TC_H_MIN(classid);
336 if (band - 1 >= q->bands)
341 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
343 return prio_get(sch, classid);
347 static void prio_put(struct Qdisc *q, unsigned long cl)
352 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
354 unsigned long cl = *arg;
355 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
357 if (cl - 1 > q->bands)
362 static int prio_delete(struct Qdisc *sch, unsigned long cl)
364 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
365 if (cl - 1 > q->bands)
371 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
374 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
376 if (cl - 1 > q->bands)
378 tcm->tcm_handle |= TC_H_MIN(cl);
380 tcm->tcm_info = q->queues[cl-1]->handle;
384 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
386 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
392 for (prio = 0; prio < q->bands; prio++) {
393 if (arg->count < arg->skip) {
397 if (arg->fn(sch, prio+1, arg) < 0) {
405 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
407 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
411 return &q->filter_list;
414 static struct Qdisc_class_ops prio_class_ops = {
419 .change = prio_change,
420 .delete = prio_delete,
422 .tcf_chain = prio_find_tcf,
423 .bind_tcf = prio_bind,
424 .unbind_tcf = prio_put,
425 .dump = prio_dump_class,
428 static struct Qdisc_ops prio_qdisc_ops = {
430 .cl_ops = &prio_class_ops,
432 .priv_size = sizeof(struct prio_sched_data),
433 .enqueue = prio_enqueue,
434 .dequeue = prio_dequeue,
435 .requeue = prio_requeue,
439 .destroy = prio_destroy,
442 .owner = THIS_MODULE,
445 static int __init prio_module_init(void)
447 return register_qdisc(&prio_qdisc_ops);
450 static void __exit prio_module_exit(void)
452 unregister_qdisc(&prio_qdisc_ops);
455 module_init(prio_module_init)
456 module_exit(prio_module_exit)
458 MODULE_LICENSE("GPL");