allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / sched / sch_prio.c
blob81df2153ec5225d1570a0862e58f387f4f8e40df
1 /*
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 <asm/uaccess.h>
16 #include <asm/system.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/in.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
32 #include <net/ip.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
35 #include <net/netlink.h>
36 #include <net/sock.h>
37 #include <net/pkt_sched.h>
40 struct prio_sched_data
42 int bands;
43 struct tcf_proto *filter_list;
44 u8 prio2band[TC_PRIO_MAX+1];
45 struct Qdisc *queues[TCQ_PRIO_BANDS];
49 static struct Qdisc *
50 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
52 struct prio_sched_data *q = qdisc_priv(sch);
53 u32 band = skb->priority;
54 struct tcf_result res;
55 int err;
57 *qerr = NET_XMIT_BYPASS;
58 if (TC_H_MAJ(skb->priority) != sch->handle) {
59 err = tc_classify(skb, q->filter_list, &res);
60 #ifdef CONFIG_NET_CLS_ACT
61 switch (tc_classify(skb, q->filter_list, &res)) {
62 case TC_ACT_STOLEN:
63 case TC_ACT_QUEUED:
64 *qerr = NET_XMIT_SUCCESS;
65 case TC_ACT_SHOT:
66 return NULL;
68 #endif
69 if (!q->filter_list || err < 0) {
70 if (TC_H_MAJ(band))
71 band = 0;
72 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
74 band = res.classid;
76 band = TC_H_MIN(band) - 1;
77 if (band >= q->bands)
78 return q->queues[q->prio2band[0]];
80 return q->queues[band];
83 static int
84 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
86 struct Qdisc *qdisc;
87 int ret;
89 qdisc = prio_classify(skb, sch, &ret);
90 #ifdef CONFIG_NET_CLS_ACT
91 if (qdisc == NULL) {
93 if (ret == NET_XMIT_BYPASS)
94 sch->qstats.drops++;
95 kfree_skb(skb);
96 return ret;
98 #endif
100 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
101 sch->bstats.bytes += skb->len;
102 sch->bstats.packets++;
103 sch->q.qlen++;
104 return NET_XMIT_SUCCESS;
106 sch->qstats.drops++;
107 return ret;
111 static int
112 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
114 struct Qdisc *qdisc;
115 int ret;
117 qdisc = prio_classify(skb, sch, &ret);
118 #ifdef CONFIG_NET_CLS_ACT
119 if (qdisc == NULL) {
120 if (ret == NET_XMIT_BYPASS)
121 sch->qstats.drops++;
122 kfree_skb(skb);
123 return ret;
125 #endif
127 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
128 sch->q.qlen++;
129 sch->qstats.requeues++;
130 return 0;
132 sch->qstats.drops++;
133 return NET_XMIT_DROP;
137 static struct sk_buff *
138 prio_dequeue(struct Qdisc* sch)
140 struct sk_buff *skb;
141 struct prio_sched_data *q = qdisc_priv(sch);
142 int prio;
143 struct Qdisc *qdisc;
145 for (prio = 0; prio < q->bands; prio++) {
146 qdisc = q->queues[prio];
147 skb = qdisc->dequeue(qdisc);
148 if (skb) {
149 sch->q.qlen--;
150 return skb;
153 return NULL;
157 static unsigned int prio_drop(struct Qdisc* sch)
159 struct prio_sched_data *q = qdisc_priv(sch);
160 int prio;
161 unsigned int len;
162 struct Qdisc *qdisc;
164 for (prio = q->bands-1; prio >= 0; prio--) {
165 qdisc = q->queues[prio];
166 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
167 sch->q.qlen--;
168 return len;
171 return 0;
175 static void
176 prio_reset(struct Qdisc* sch)
178 int prio;
179 struct prio_sched_data *q = qdisc_priv(sch);
181 for (prio=0; prio<q->bands; prio++)
182 qdisc_reset(q->queues[prio]);
183 sch->q.qlen = 0;
186 static void
187 prio_destroy(struct Qdisc* sch)
189 int prio;
190 struct prio_sched_data *q = qdisc_priv(sch);
192 tcf_destroy_chain(q->filter_list);
193 for (prio=0; prio<q->bands; prio++)
194 qdisc_destroy(q->queues[prio]);
197 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
199 struct prio_sched_data *q = qdisc_priv(sch);
200 struct tc_prio_qopt *qopt = RTA_DATA(opt);
201 int i;
203 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
204 return -EINVAL;
205 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
206 return -EINVAL;
208 for (i=0; i<=TC_PRIO_MAX; i++) {
209 if (qopt->priomap[i] >= qopt->bands)
210 return -EINVAL;
213 sch_tree_lock(sch);
214 q->bands = qopt->bands;
215 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
217 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
218 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
219 if (child != &noop_qdisc) {
220 qdisc_tree_decrease_qlen(child, child->q.qlen);
221 qdisc_destroy(child);
224 sch_tree_unlock(sch);
226 for (i=0; i<q->bands; i++) {
227 if (q->queues[i] == &noop_qdisc) {
228 struct Qdisc *child;
229 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
230 TC_H_MAKE(sch->handle, i + 1));
231 if (child) {
232 sch_tree_lock(sch);
233 child = xchg(&q->queues[i], child);
235 if (child != &noop_qdisc) {
236 qdisc_tree_decrease_qlen(child,
237 child->q.qlen);
238 qdisc_destroy(child);
240 sch_tree_unlock(sch);
244 return 0;
247 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
249 struct prio_sched_data *q = qdisc_priv(sch);
250 int i;
252 for (i=0; i<TCQ_PRIO_BANDS; i++)
253 q->queues[i] = &noop_qdisc;
255 if (opt == NULL) {
256 return -EINVAL;
257 } else {
258 int err;
260 if ((err= prio_tune(sch, opt)) != 0)
261 return err;
263 return 0;
266 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
268 struct prio_sched_data *q = qdisc_priv(sch);
269 unsigned char *b = skb_tail_pointer(skb);
270 struct tc_prio_qopt opt;
272 opt.bands = q->bands;
273 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
274 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
275 return skb->len;
277 rtattr_failure:
278 nlmsg_trim(skb, b);
279 return -1;
282 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
283 struct Qdisc **old)
285 struct prio_sched_data *q = qdisc_priv(sch);
286 unsigned long band = arg - 1;
288 if (band >= q->bands)
289 return -EINVAL;
291 if (new == NULL)
292 new = &noop_qdisc;
294 sch_tree_lock(sch);
295 *old = q->queues[band];
296 q->queues[band] = new;
297 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
298 qdisc_reset(*old);
299 sch_tree_unlock(sch);
301 return 0;
304 static struct Qdisc *
305 prio_leaf(struct Qdisc *sch, unsigned long arg)
307 struct prio_sched_data *q = qdisc_priv(sch);
308 unsigned long band = arg - 1;
310 if (band >= q->bands)
311 return NULL;
313 return q->queues[band];
316 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
318 struct prio_sched_data *q = qdisc_priv(sch);
319 unsigned long band = TC_H_MIN(classid);
321 if (band - 1 >= q->bands)
322 return 0;
323 return band;
326 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
328 return prio_get(sch, classid);
332 static void prio_put(struct Qdisc *q, unsigned long cl)
334 return;
337 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
339 unsigned long cl = *arg;
340 struct prio_sched_data *q = qdisc_priv(sch);
342 if (cl - 1 > q->bands)
343 return -ENOENT;
344 return 0;
347 static int prio_delete(struct Qdisc *sch, unsigned long cl)
349 struct prio_sched_data *q = qdisc_priv(sch);
350 if (cl - 1 > q->bands)
351 return -ENOENT;
352 return 0;
356 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
357 struct tcmsg *tcm)
359 struct prio_sched_data *q = qdisc_priv(sch);
361 if (cl - 1 > q->bands)
362 return -ENOENT;
363 tcm->tcm_handle |= TC_H_MIN(cl);
364 if (q->queues[cl-1])
365 tcm->tcm_info = q->queues[cl-1]->handle;
366 return 0;
369 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
370 struct gnet_dump *d)
372 struct prio_sched_data *q = qdisc_priv(sch);
373 struct Qdisc *cl_q;
375 cl_q = q->queues[cl - 1];
376 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
377 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
378 return -1;
380 return 0;
383 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
385 struct prio_sched_data *q = qdisc_priv(sch);
386 int prio;
388 if (arg->stop)
389 return;
391 for (prio = 0; prio < q->bands; prio++) {
392 if (arg->count < arg->skip) {
393 arg->count++;
394 continue;
396 if (arg->fn(sch, prio+1, arg) < 0) {
397 arg->stop = 1;
398 break;
400 arg->count++;
404 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
406 struct prio_sched_data *q = qdisc_priv(sch);
408 if (cl)
409 return NULL;
410 return &q->filter_list;
413 static struct Qdisc_class_ops prio_class_ops = {
414 .graft = prio_graft,
415 .leaf = prio_leaf,
416 .get = prio_get,
417 .put = prio_put,
418 .change = prio_change,
419 .delete = prio_delete,
420 .walk = prio_walk,
421 .tcf_chain = prio_find_tcf,
422 .bind_tcf = prio_bind,
423 .unbind_tcf = prio_put,
424 .dump = prio_dump_class,
425 .dump_stats = prio_dump_class_stats,
428 static struct Qdisc_ops prio_qdisc_ops = {
429 .next = NULL,
430 .cl_ops = &prio_class_ops,
431 .id = "prio",
432 .priv_size = sizeof(struct prio_sched_data),
433 .enqueue = prio_enqueue,
434 .dequeue = prio_dequeue,
435 .requeue = prio_requeue,
436 .drop = prio_drop,
437 .init = prio_init,
438 .reset = prio_reset,
439 .destroy = prio_destroy,
440 .change = prio_tune,
441 .dump = prio_dump,
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");