tweak reserved blocks
[linux-2.4.git] / net / sched / sch_prio.c
1 /*
2  * net/sched/sch_prio.c Simple 3-band priority "scheduler".
3  *
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.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  * Fixes:       19990609: J Hadi Salim <hadi@nortelnetworks.com>: 
11  *              Init --  EINVAL when opt undefined
12  */
13
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>
23 #include <linux/mm.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/in.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>
34 #include <net/ip.h>
35 #include <net/route.h>
36 #include <linux/skbuff.h>
37 #include <net/sock.h>
38 #include <net/pkt_sched.h>
39
40
41 struct prio_sched_data
42 {
43         int bands;
44         struct tcf_proto *filter_list;
45         u8  prio2band[TC_PRIO_MAX+1];
46         struct Qdisc *queues[TCQ_PRIO_BANDS];
47 };
48
49
50 static __inline__ unsigned prio_classify(struct sk_buff *skb, struct Qdisc *sch)
51 {
52         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
53         struct tcf_result res;
54         u32 band;
55
56         band = skb->priority;
57         if (TC_H_MAJ(skb->priority) != sch->handle) {
58                 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
59                         if (TC_H_MAJ(band))
60                                 band = 0;
61                         return q->prio2band[band&TC_PRIO_MAX];
62                 }
63                 band = res.classid;
64         }
65         band = TC_H_MIN(band) - 1;
66         return band < q->bands ? band : q->prio2band[0];
67 }
68
69 static int
70 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
71 {
72         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
73         struct Qdisc *qdisc;
74         int ret;
75
76         qdisc = q->queues[prio_classify(skb, sch)];
77
78         if ((ret = qdisc->enqueue(skb, qdisc)) == 0) {
79                 sch->stats.bytes += skb->len;
80                 sch->stats.packets++;
81                 sch->q.qlen++;
82                 return 0;
83         }
84         sch->stats.drops++;
85         return ret;
86 }
87
88
89 static int
90 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
91 {
92         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
93         struct Qdisc *qdisc;
94         int ret;
95
96         qdisc = q->queues[prio_classify(skb, sch)];
97
98         if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
99                 sch->q.qlen++;
100                 return 0;
101         }
102         sch->stats.drops++;
103         return ret;
104 }
105
106
107 static struct sk_buff *
108 prio_dequeue(struct Qdisc* sch)
109 {
110         struct sk_buff *skb;
111         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
112         int prio;
113         struct Qdisc *qdisc;
114
115         for (prio = 0; prio < q->bands; prio++) {
116                 qdisc = q->queues[prio];
117                 skb = qdisc->dequeue(qdisc);
118                 if (skb) {
119                         sch->q.qlen--;
120                         return skb;
121                 }
122         }
123         return NULL;
124
125 }
126
127 static unsigned int prio_drop(struct Qdisc* sch)
128 {
129         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
130         int prio;
131         unsigned int len;
132         struct Qdisc *qdisc;
133
134         for (prio = q->bands-1; prio >= 0; prio--) {
135                 qdisc = q->queues[prio];
136                 if ((len = qdisc->ops->drop(qdisc)) != 0) {
137                         sch->q.qlen--;
138                         return len;
139                 }
140         }
141         return 0;
142 }
143
144
145 static void
146 prio_reset(struct Qdisc* sch)
147 {
148         int prio;
149         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
150
151         for (prio=0; prio<q->bands; prio++)
152                 qdisc_reset(q->queues[prio]);
153         sch->q.qlen = 0;
154 }
155
156 static void
157 prio_destroy(struct Qdisc* sch)
158 {
159         int prio;
160         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
161         struct tcf_proto *tp;
162
163         while ((tp = q->filter_list) != NULL) {
164                 q->filter_list = tp->next;
165                 tcf_destroy(tp);
166         }
167
168         for (prio=0; prio<q->bands; prio++) {
169                 qdisc_destroy(q->queues[prio]);
170                 q->queues[prio] = &noop_qdisc;
171         }
172         MOD_DEC_USE_COUNT;
173 }
174
175 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
176 {
177         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
178         struct tc_prio_qopt *qopt = RTA_DATA(opt);
179         int i;
180
181         if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
182                 return -EINVAL;
183         if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
184                 return -EINVAL;
185
186         for (i=0; i<=TC_PRIO_MAX; i++) {
187                 if (qopt->priomap[i] >= qopt->bands)
188                         return -EINVAL;
189         }
190
191         sch_tree_lock(sch);
192         q->bands = qopt->bands;
193         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
194
195         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
196                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
197                 if (child != &noop_qdisc)
198                         qdisc_destroy(child);
199         }
200         sch_tree_unlock(sch);
201
202         for (i=0; i<=TC_PRIO_MAX; i++) {
203                 int band = q->prio2band[i];
204                 if (q->queues[band] == &noop_qdisc) {
205                         struct Qdisc *child;
206                         child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
207                         if (child) {
208                                 sch_tree_lock(sch);
209                                 child = xchg(&q->queues[band], child);
210
211                                 if (child != &noop_qdisc)
212                                         qdisc_destroy(child);
213                                 sch_tree_unlock(sch);
214                         }
215                 }
216         }
217         return 0;
218 }
219
220 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
221 {
222         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
223         int i;
224
225         for (i=0; i<TCQ_PRIO_BANDS; i++)
226                 q->queues[i] = &noop_qdisc;
227
228         if (opt == NULL) {
229                 return -EINVAL;
230         } else {
231                 int err;
232
233                 if ((err= prio_tune(sch, opt)) != 0)
234                         return err;
235         }
236         MOD_INC_USE_COUNT;
237         return 0;
238 }
239
240 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
241 {
242         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
243         unsigned char    *b = skb->tail;
244         struct tc_prio_qopt opt;
245
246         opt.bands = q->bands;
247         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
248         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
249         return skb->len;
250
251 rtattr_failure:
252         skb_trim(skb, b - skb->data);
253         return -1;
254 }
255
256 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
257                       struct Qdisc **old)
258 {
259         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
260         unsigned long band = arg - 1;
261
262         if (band >= q->bands)
263                 return -EINVAL;
264
265         if (new == NULL)
266                 new = &noop_qdisc;
267
268         sch_tree_lock(sch);
269         *old = q->queues[band];
270         q->queues[band] = new;
271         sch->q.qlen -= (*old)->q.qlen;
272         qdisc_reset(*old);
273         sch_tree_unlock(sch);
274
275         return 0;
276 }
277
278 static struct Qdisc *
279 prio_leaf(struct Qdisc *sch, unsigned long arg)
280 {
281         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
282         unsigned long band = arg - 1;
283
284         if (band >= q->bands)
285                 return NULL;
286
287         return q->queues[band];
288 }
289
290 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
291 {
292         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
293         unsigned long band = TC_H_MIN(classid);
294
295         if (band - 1 >= q->bands)
296                 return 0;
297         return band;
298 }
299
300 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
301 {
302         return prio_get(sch, classid);
303 }
304
305
306 static void prio_put(struct Qdisc *q, unsigned long cl)
307 {
308         return;
309 }
310
311 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
312 {
313         unsigned long cl = *arg;
314         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
315
316         if (cl - 1 > q->bands)
317                 return -ENOENT;
318         return 0;
319 }
320
321 static int prio_delete(struct Qdisc *sch, unsigned long cl)
322 {
323         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
324         if (cl - 1 > q->bands)
325                 return -ENOENT;
326         return 0;
327 }
328
329
330 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
331                            struct tcmsg *tcm)
332 {
333         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
334
335         if (cl - 1 > q->bands)
336                 return -ENOENT;
337         tcm->tcm_handle |= TC_H_MIN(cl);
338         if (q->queues[cl-1])
339                 tcm->tcm_info = q->queues[cl-1]->handle;
340         return 0;
341 }
342
343 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
344 {
345         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
346         int prio;
347
348         if (arg->stop)
349                 return;
350
351         for (prio = 0; prio < q->bands; prio++) {
352                 if (arg->count < arg->skip) {
353                         arg->count++;
354                         continue;
355                 }
356                 if (arg->fn(sch, prio+1, arg) < 0) {
357                         arg->stop = 1;
358                         break;
359                 }
360                 arg->count++;
361         }
362 }
363
364 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
365 {
366         struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
367
368         if (cl)
369                 return NULL;
370         return &q->filter_list;
371 }
372
373 static struct Qdisc_class_ops prio_class_ops =
374 {
375         prio_graft,
376         prio_leaf,
377
378         prio_get,
379         prio_put,
380         prio_change,
381         prio_delete,
382         prio_walk,
383
384         prio_find_tcf,
385         prio_bind,
386         prio_put,
387
388         prio_dump_class,
389 };
390
391 struct Qdisc_ops prio_qdisc_ops =
392 {
393         NULL,
394         &prio_class_ops,
395         "prio",
396         sizeof(struct prio_sched_data),
397
398         prio_enqueue,
399         prio_dequeue,
400         prio_requeue,
401         prio_drop,
402
403         prio_init,
404         prio_reset,
405         prio_destroy,
406         prio_tune,
407
408         prio_dump,
409 };
410
411 #ifdef MODULE
412
413 int init_module(void)
414 {
415         return register_qdisc(&prio_qdisc_ops);
416 }
417
418 void cleanup_module(void) 
419 {
420         unregister_qdisc(&prio_qdisc_ops);
421 }
422
423 #endif
424 MODULE_LICENSE("GPL");