* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / sched / sch_prio.c
blob0fc53b4c9a655fbf70ba2fdd85e8c410fc4a1bf3
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>
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.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/sock.h>
36 #include <net/pkt_sched.h>
39 struct prio_sched_data
41 int bands;
42 struct tcf_proto *filter_list;
43 u8 prio2band[TC_PRIO_MAX+1];
44 struct Qdisc *queues[TCQ_PRIO_BANDS];
48 static __inline__ unsigned prio_classify(struct sk_buff *skb, struct Qdisc *sch)
50 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
51 struct tcf_result res;
52 u32 band;
54 band = skb->priority;
55 if (TC_H_MAJ(skb->priority) != sch->handle) {
56 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
57 if (TC_H_MAJ(band))
58 band = 0;
59 return q->prio2band[band&TC_PRIO_MAX];
61 band = res.classid;
63 band = TC_H_MIN(band) - 1;
64 return band < q->bands ? band : q->prio2band[0];
67 static int
68 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
70 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
71 struct Qdisc *qdisc;
72 int ret;
74 qdisc = q->queues[prio_classify(skb, sch)];
76 if ((ret = qdisc->enqueue(skb, qdisc)) == 0) {
77 sch->stats.bytes += skb->len;
78 sch->stats.packets++;
79 sch->q.qlen++;
80 return 0;
82 sch->stats.drops++;
83 return ret;
87 static int
88 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
90 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
91 struct Qdisc *qdisc;
92 int ret;
94 qdisc = q->queues[prio_classify(skb, sch)];
96 if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
97 sch->q.qlen++;
98 return 0;
100 sch->stats.drops++;
101 return ret;
105 static struct sk_buff *
106 prio_dequeue(struct Qdisc* sch)
108 struct sk_buff *skb;
109 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
110 int prio;
111 struct Qdisc *qdisc;
113 for (prio = 0; prio < q->bands; prio++) {
114 qdisc = q->queues[prio];
115 skb = qdisc->dequeue(qdisc);
116 if (skb) {
117 sch->q.qlen--;
118 return skb;
121 return NULL;
125 static int
126 prio_drop(struct Qdisc* sch)
128 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
129 int prio;
130 struct Qdisc *qdisc;
132 for (prio = q->bands-1; prio >= 0; prio--) {
133 qdisc = q->queues[prio];
134 if (qdisc->ops->drop(qdisc)) {
135 sch->q.qlen--;
136 return 1;
139 return 0;
143 static void
144 prio_reset(struct Qdisc* sch)
146 int prio;
147 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
149 for (prio=0; prio<q->bands; prio++)
150 qdisc_reset(q->queues[prio]);
151 sch->q.qlen = 0;
154 static void
155 prio_destroy(struct Qdisc* sch)
157 int prio;
158 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
160 for (prio=0; prio<q->bands; prio++) {
161 qdisc_destroy(q->queues[prio]);
162 q->queues[prio] = &noop_qdisc;
164 MOD_DEC_USE_COUNT;
167 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
169 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
170 struct tc_prio_qopt *qopt = RTA_DATA(opt);
171 int i;
173 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
174 return -EINVAL;
175 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
176 return -EINVAL;
178 for (i=0; i<=TC_PRIO_MAX; i++) {
179 if (qopt->priomap[i] >= qopt->bands)
180 return -EINVAL;
183 sch_tree_lock(sch);
184 q->bands = qopt->bands;
185 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
187 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
188 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
189 if (child != &noop_qdisc)
190 qdisc_destroy(child);
192 sch_tree_unlock(sch);
194 for (i=0; i<=TC_PRIO_MAX; i++) {
195 int band = q->prio2band[i];
196 if (q->queues[band] == &noop_qdisc) {
197 struct Qdisc *child;
198 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
199 if (child) {
200 sch_tree_lock(sch);
201 child = xchg(&q->queues[band], child);
203 if (child != &noop_qdisc)
204 qdisc_destroy(child);
205 sch_tree_unlock(sch);
209 return 0;
212 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
214 static const u8 prio2band[TC_PRIO_MAX+1] =
215 { 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };
216 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
217 int i;
219 for (i=0; i<TCQ_PRIO_BANDS; i++)
220 q->queues[i] = &noop_qdisc;
222 if (opt == NULL) {
223 q->bands = 3;
224 memcpy(q->prio2band, prio2band, sizeof(prio2band));
225 for (i=0; i<3; i++) {
226 struct Qdisc *child;
227 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
228 if (child)
229 q->queues[i] = child;
231 } else {
232 int err;
234 if ((err= prio_tune(sch, opt)) != 0)
235 return err;
237 MOD_INC_USE_COUNT;
238 return 0;
241 #ifdef CONFIG_RTNETLINK
242 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
244 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
245 unsigned char *b = skb->tail;
246 struct tc_prio_qopt opt;
248 opt.bands = q->bands;
249 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
250 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
251 return skb->len;
253 rtattr_failure:
254 skb_trim(skb, b - skb->data);
255 return -1;
257 #endif
259 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
260 struct Qdisc **old)
262 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
263 unsigned long band = arg - 1;
265 if (band >= q->bands)
266 return -EINVAL;
268 if (new == NULL)
269 new = &noop_qdisc;
271 sch_tree_lock(sch);
272 *old = q->queues[band];
273 q->queues[band] = new;
274 qdisc_reset(*old);
275 sch_tree_unlock(sch);
277 return 0;
280 static struct Qdisc *
281 prio_leaf(struct Qdisc *sch, unsigned long arg)
283 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
284 unsigned long band = arg - 1;
286 if (band >= q->bands)
287 return NULL;
289 return q->queues[band];
292 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
294 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
295 unsigned long band = TC_H_MIN(classid);
297 if (band - 1 >= q->bands)
298 return 0;
299 return band;
302 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
304 return prio_get(sch, classid);
308 static void prio_put(struct Qdisc *q, unsigned long cl)
310 return;
313 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
315 unsigned long cl = *arg;
316 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
318 if (cl - 1 > q->bands)
319 return -ENOENT;
320 return 0;
323 static int prio_delete(struct Qdisc *sch, unsigned long cl)
325 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
326 if (cl - 1 > q->bands)
327 return -ENOENT;
328 return 0;
332 #ifdef CONFIG_RTNETLINK
333 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
334 struct tcmsg *tcm)
336 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
338 if (cl - 1 > q->bands)
339 return -ENOENT;
340 if (q->queues[cl-1])
341 tcm->tcm_info = q->queues[cl-1]->handle;
342 return 0;
344 #endif
346 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
348 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
349 int prio;
351 if (arg->stop)
352 return;
354 for (prio = 0; prio < q->bands; prio++) {
355 if (arg->count < arg->skip) {
356 arg->count++;
357 continue;
359 if (arg->fn(sch, prio+1, arg) < 0) {
360 arg->stop = 1;
361 break;
363 arg->count++;
367 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
369 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
371 if (cl)
372 return NULL;
373 return &q->filter_list;
376 static struct Qdisc_class_ops prio_class_ops =
378 prio_graft,
379 prio_leaf,
381 prio_get,
382 prio_put,
383 prio_change,
384 prio_delete,
385 prio_walk,
387 prio_find_tcf,
388 prio_bind,
389 prio_put,
391 #ifdef CONFIG_RTNETLINK
392 prio_dump_class,
393 #endif
396 struct Qdisc_ops prio_qdisc_ops =
398 NULL,
399 &prio_class_ops,
400 "prio",
401 sizeof(struct prio_sched_data),
403 prio_enqueue,
404 prio_dequeue,
405 prio_requeue,
406 prio_drop,
408 prio_init,
409 prio_reset,
410 prio_destroy,
411 prio_tune,
413 #ifdef CONFIG_RTNETLINK
414 prio_dump,
415 #endif
418 #ifdef MODULE
420 int init_module(void)
422 return register_qdisc(&prio_qdisc_ops);
425 void cleanup_module(void)
427 unregister_qdisc(&prio_qdisc_ops);
430 #endif