[NETFILTER]: xt_limit: add compat conversion functions
[hh.org.git] / net / sched / sch_prio.c
bloba5fa03c0c19b6c8167f2e68a4e4d3b052c0b0bcc
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/sched.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/in.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/if_ether.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/notifier.h>
33 #include <net/ip.h>
34 #include <net/route.h>
35 #include <linux/skbuff.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;
56 *qerr = NET_XMIT_BYPASS;
57 if (TC_H_MAJ(skb->priority) != sch->handle) {
58 #ifdef CONFIG_NET_CLS_ACT
59 switch (tc_classify(skb, q->filter_list, &res)) {
60 case TC_ACT_STOLEN:
61 case TC_ACT_QUEUED:
62 *qerr = NET_XMIT_SUCCESS;
63 case TC_ACT_SHOT:
64 return NULL;
67 if (!q->filter_list ) {
68 #else
69 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
70 #endif
71 if (TC_H_MAJ(band))
72 band = 0;
73 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
75 band = res.classid;
77 band = TC_H_MIN(band) - 1;
78 if (band > q->bands)
79 return q->queues[q->prio2band[0]];
81 return q->queues[band];
84 static int
85 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
87 struct Qdisc *qdisc;
88 int ret;
90 qdisc = prio_classify(skb, sch, &ret);
91 #ifdef CONFIG_NET_CLS_ACT
92 if (qdisc == NULL) {
94 if (ret == NET_XMIT_BYPASS)
95 sch->qstats.drops++;
96 kfree_skb(skb);
97 return ret;
99 #endif
101 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
102 sch->bstats.bytes += skb->len;
103 sch->bstats.packets++;
104 sch->q.qlen++;
105 return NET_XMIT_SUCCESS;
107 sch->qstats.drops++;
108 return ret;
112 static int
113 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
115 struct Qdisc *qdisc;
116 int ret;
118 qdisc = prio_classify(skb, sch, &ret);
119 #ifdef CONFIG_NET_CLS_ACT
120 if (qdisc == NULL) {
121 if (ret == NET_XMIT_BYPASS)
122 sch->qstats.drops++;
123 kfree_skb(skb);
124 return ret;
126 #endif
128 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
129 sch->q.qlen++;
130 sch->qstats.requeues++;
131 return 0;
133 sch->qstats.drops++;
134 return NET_XMIT_DROP;
138 static struct sk_buff *
139 prio_dequeue(struct Qdisc* sch)
141 struct sk_buff *skb;
142 struct prio_sched_data *q = qdisc_priv(sch);
143 int prio;
144 struct Qdisc *qdisc;
146 for (prio = 0; prio < q->bands; prio++) {
147 qdisc = q->queues[prio];
148 skb = qdisc->dequeue(qdisc);
149 if (skb) {
150 sch->q.qlen--;
151 return skb;
154 return NULL;
158 static unsigned int prio_drop(struct Qdisc* sch)
160 struct prio_sched_data *q = qdisc_priv(sch);
161 int prio;
162 unsigned int len;
163 struct Qdisc *qdisc;
165 for (prio = q->bands-1; prio >= 0; prio--) {
166 qdisc = q->queues[prio];
167 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
168 sch->q.qlen--;
169 return len;
172 return 0;
176 static void
177 prio_reset(struct Qdisc* sch)
179 int prio;
180 struct prio_sched_data *q = qdisc_priv(sch);
182 for (prio=0; prio<q->bands; prio++)
183 qdisc_reset(q->queues[prio]);
184 sch->q.qlen = 0;
187 static void
188 prio_destroy(struct Qdisc* sch)
190 int prio;
191 struct prio_sched_data *q = qdisc_priv(sch);
192 struct tcf_proto *tp;
194 while ((tp = q->filter_list) != NULL) {
195 q->filter_list = tp->next;
196 tcf_destroy(tp);
199 for (prio=0; prio<q->bands; prio++)
200 qdisc_destroy(q->queues[prio]);
203 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
205 struct prio_sched_data *q = qdisc_priv(sch);
206 struct tc_prio_qopt *qopt = RTA_DATA(opt);
207 int i;
209 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
210 return -EINVAL;
211 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
212 return -EINVAL;
214 for (i=0; i<=TC_PRIO_MAX; i++) {
215 if (qopt->priomap[i] >= qopt->bands)
216 return -EINVAL;
219 sch_tree_lock(sch);
220 q->bands = qopt->bands;
221 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
223 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
224 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
225 if (child != &noop_qdisc)
226 qdisc_destroy(child);
228 sch_tree_unlock(sch);
230 for (i=0; i<q->bands; i++) {
231 if (q->queues[i] == &noop_qdisc) {
232 struct Qdisc *child;
233 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
234 if (child) {
235 sch_tree_lock(sch);
236 child = xchg(&q->queues[i], child);
238 if (child != &noop_qdisc)
239 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;
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 skb_trim(skb, b - skb->data);
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 sch->q.qlen -= (*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 void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
371 struct prio_sched_data *q = qdisc_priv(sch);
372 int prio;
374 if (arg->stop)
375 return;
377 for (prio = 0; prio < q->bands; prio++) {
378 if (arg->count < arg->skip) {
379 arg->count++;
380 continue;
382 if (arg->fn(sch, prio+1, arg) < 0) {
383 arg->stop = 1;
384 break;
386 arg->count++;
390 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
392 struct prio_sched_data *q = qdisc_priv(sch);
394 if (cl)
395 return NULL;
396 return &q->filter_list;
399 static struct Qdisc_class_ops prio_class_ops = {
400 .graft = prio_graft,
401 .leaf = prio_leaf,
402 .get = prio_get,
403 .put = prio_put,
404 .change = prio_change,
405 .delete = prio_delete,
406 .walk = prio_walk,
407 .tcf_chain = prio_find_tcf,
408 .bind_tcf = prio_bind,
409 .unbind_tcf = prio_put,
410 .dump = prio_dump_class,
413 static struct Qdisc_ops prio_qdisc_ops = {
414 .next = NULL,
415 .cl_ops = &prio_class_ops,
416 .id = "prio",
417 .priv_size = sizeof(struct prio_sched_data),
418 .enqueue = prio_enqueue,
419 .dequeue = prio_dequeue,
420 .requeue = prio_requeue,
421 .drop = prio_drop,
422 .init = prio_init,
423 .reset = prio_reset,
424 .destroy = prio_destroy,
425 .change = prio_tune,
426 .dump = prio_dump,
427 .owner = THIS_MODULE,
430 static int __init prio_module_init(void)
432 return register_qdisc(&prio_qdisc_ops);
435 static void __exit prio_module_exit(void)
437 unregister_qdisc(&prio_qdisc_ops);
440 module_init(prio_module_init)
441 module_exit(prio_module_exit)
443 MODULE_LICENSE("GPL");