blob: de889f23f22a5fdb29481c6696957e0832f95170 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090010 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Init -- EINVAL when opt undefined
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
37
38
39struct prio_sched_data
40{
41 int bands;
42 struct tcf_proto *filter_list;
43 u8 prio2band[TC_PRIO_MAX+1];
44 struct Qdisc *queues[TCQ_PRIO_BANDS];
45};
46
47
48static struct Qdisc *
49prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
50{
51 struct prio_sched_data *q = qdisc_priv(sch);
52 u32 band = skb->priority;
53 struct tcf_result res;
54
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080055 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (TC_H_MAJ(skb->priority) != sch->handle) {
57#ifdef CONFIG_NET_CLS_ACT
58 switch (tc_classify(skb, q->filter_list, &res)) {
59 case TC_ACT_STOLEN:
60 case TC_ACT_QUEUED:
61 *qerr = NET_XMIT_SUCCESS;
62 case TC_ACT_SHOT:
63 return NULL;
64 };
65
66 if (!q->filter_list ) {
67#else
68 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
69#endif
70 if (TC_H_MAJ(band))
71 band = 0;
72 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
73 }
74 band = res.classid;
75 }
76 band = TC_H_MIN(band) - 1;
77 if (band > q->bands)
78 return q->queues[q->prio2band[0]];
79
80 return q->queues[band];
81}
82
83static int
84prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
85{
86 struct Qdisc *qdisc;
87 int ret;
88
89 qdisc = prio_classify(skb, sch, &ret);
90#ifdef CONFIG_NET_CLS_ACT
91 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080092
93 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 sch->qstats.drops++;
95 kfree_skb(skb);
96 return ret;
97 }
98#endif
99
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;
105 }
106 sch->qstats.drops++;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110
111static int
112prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
113{
114 struct Qdisc *qdisc;
115 int ret;
116
117 qdisc = prio_classify(skb, sch, &ret);
118#ifdef CONFIG_NET_CLS_ACT
119 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800120 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 sch->qstats.drops++;
122 kfree_skb(skb);
123 return ret;
124 }
125#endif
126
127 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
128 sch->q.qlen++;
129 sch->qstats.requeues++;
130 return 0;
131 }
132 sch->qstats.drops++;
133 return NET_XMIT_DROP;
134}
135
136
137static struct sk_buff *
138prio_dequeue(struct Qdisc* sch)
139{
140 struct sk_buff *skb;
141 struct prio_sched_data *q = qdisc_priv(sch);
142 int prio;
143 struct Qdisc *qdisc;
144
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;
151 }
152 }
153 return NULL;
154
155}
156
157static unsigned int prio_drop(struct Qdisc* sch)
158{
159 struct prio_sched_data *q = qdisc_priv(sch);
160 int prio;
161 unsigned int len;
162 struct Qdisc *qdisc;
163
164 for (prio = q->bands-1; prio >= 0; prio--) {
165 qdisc = q->queues[prio];
Patrick McHardy6d037a22006-03-20 19:00:49 -0800166 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 sch->q.qlen--;
168 return len;
169 }
170 }
171 return 0;
172}
173
174
175static void
176prio_reset(struct Qdisc* sch)
177{
178 int prio;
179 struct prio_sched_data *q = qdisc_priv(sch);
180
181 for (prio=0; prio<q->bands; prio++)
182 qdisc_reset(q->queues[prio]);
183 sch->q.qlen = 0;
184}
185
186static void
187prio_destroy(struct Qdisc* sch)
188{
189 int prio;
190 struct prio_sched_data *q = qdisc_priv(sch);
191 struct tcf_proto *tp;
192
193 while ((tp = q->filter_list) != NULL) {
194 q->filter_list = tp->next;
195 tcf_destroy(tp);
196 }
197
198 for (prio=0; prio<q->bands; prio++)
199 qdisc_destroy(q->queues[prio]);
200}
201
202static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
203{
204 struct prio_sched_data *q = qdisc_priv(sch);
205 struct tc_prio_qopt *qopt = RTA_DATA(opt);
206 int i;
207
208 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
209 return -EINVAL;
210 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
211 return -EINVAL;
212
213 for (i=0; i<=TC_PRIO_MAX; i++) {
214 if (qopt->priomap[i] >= qopt->bands)
215 return -EINVAL;
216 }
217
218 sch_tree_lock(sch);
219 q->bands = qopt->bands;
220 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
221
222 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
223 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800224 if (child != &noop_qdisc) {
225 qdisc_tree_decrease_qlen(child, child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 sch_tree_unlock(sch);
230
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800231 for (i=0; i<q->bands; i++) {
232 if (q->queues[i] == &noop_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct Qdisc *child;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800234 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
235 TC_H_MAKE(sch->handle, i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (child) {
237 sch_tree_lock(sch);
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800238 child = xchg(&q->queues[i], child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Patrick McHardy5e50da02006-11-29 17:36:20 -0800240 if (child != &noop_qdisc) {
241 qdisc_tree_decrease_qlen(child,
242 child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 sch_tree_unlock(sch);
246 }
247 }
248 }
249 return 0;
250}
251
252static int prio_init(struct Qdisc *sch, struct rtattr *opt)
253{
254 struct prio_sched_data *q = qdisc_priv(sch);
255 int i;
256
257 for (i=0; i<TCQ_PRIO_BANDS; i++)
258 q->queues[i] = &noop_qdisc;
259
260 if (opt == NULL) {
261 return -EINVAL;
262 } else {
263 int err;
264
265 if ((err= prio_tune(sch, opt)) != 0)
266 return err;
267 }
268 return 0;
269}
270
271static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
272{
273 struct prio_sched_data *q = qdisc_priv(sch);
274 unsigned char *b = skb->tail;
275 struct tc_prio_qopt opt;
276
277 opt.bands = q->bands;
278 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
279 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
280 return skb->len;
281
282rtattr_failure:
283 skb_trim(skb, b - skb->data);
284 return -1;
285}
286
287static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
288 struct Qdisc **old)
289{
290 struct prio_sched_data *q = qdisc_priv(sch);
291 unsigned long band = arg - 1;
292
293 if (band >= q->bands)
294 return -EINVAL;
295
296 if (new == NULL)
297 new = &noop_qdisc;
298
299 sch_tree_lock(sch);
300 *old = q->queues[band];
301 q->queues[band] = new;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800302 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 qdisc_reset(*old);
304 sch_tree_unlock(sch);
305
306 return 0;
307}
308
309static struct Qdisc *
310prio_leaf(struct Qdisc *sch, unsigned long arg)
311{
312 struct prio_sched_data *q = qdisc_priv(sch);
313 unsigned long band = arg - 1;
314
315 if (band >= q->bands)
316 return NULL;
317
318 return q->queues[band];
319}
320
321static unsigned long prio_get(struct Qdisc *sch, u32 classid)
322{
323 struct prio_sched_data *q = qdisc_priv(sch);
324 unsigned long band = TC_H_MIN(classid);
325
326 if (band - 1 >= q->bands)
327 return 0;
328 return band;
329}
330
331static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
332{
333 return prio_get(sch, classid);
334}
335
336
337static void prio_put(struct Qdisc *q, unsigned long cl)
338{
339 return;
340}
341
342static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
343{
344 unsigned long cl = *arg;
345 struct prio_sched_data *q = qdisc_priv(sch);
346
347 if (cl - 1 > q->bands)
348 return -ENOENT;
349 return 0;
350}
351
352static int prio_delete(struct Qdisc *sch, unsigned long cl)
353{
354 struct prio_sched_data *q = qdisc_priv(sch);
355 if (cl - 1 > q->bands)
356 return -ENOENT;
357 return 0;
358}
359
360
361static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
362 struct tcmsg *tcm)
363{
364 struct prio_sched_data *q = qdisc_priv(sch);
365
366 if (cl - 1 > q->bands)
367 return -ENOENT;
368 tcm->tcm_handle |= TC_H_MIN(cl);
369 if (q->queues[cl-1])
370 tcm->tcm_info = q->queues[cl-1]->handle;
371 return 0;
372}
373
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800374static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
375 struct gnet_dump *d)
376{
377 struct prio_sched_data *q = qdisc_priv(sch);
378 struct Qdisc *cl_q;
379
380 cl_q = q->queues[cl - 1];
381 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
382 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
383 return -1;
384
385 return 0;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
389{
390 struct prio_sched_data *q = qdisc_priv(sch);
391 int prio;
392
393 if (arg->stop)
394 return;
395
396 for (prio = 0; prio < q->bands; prio++) {
397 if (arg->count < arg->skip) {
398 arg->count++;
399 continue;
400 }
401 if (arg->fn(sch, prio+1, arg) < 0) {
402 arg->stop = 1;
403 break;
404 }
405 arg->count++;
406 }
407}
408
409static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
410{
411 struct prio_sched_data *q = qdisc_priv(sch);
412
413 if (cl)
414 return NULL;
415 return &q->filter_list;
416}
417
418static struct Qdisc_class_ops prio_class_ops = {
419 .graft = prio_graft,
420 .leaf = prio_leaf,
421 .get = prio_get,
422 .put = prio_put,
423 .change = prio_change,
424 .delete = prio_delete,
425 .walk = prio_walk,
426 .tcf_chain = prio_find_tcf,
427 .bind_tcf = prio_bind,
428 .unbind_tcf = prio_put,
429 .dump = prio_dump_class,
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800430 .dump_stats = prio_dump_class_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
433static struct Qdisc_ops prio_qdisc_ops = {
434 .next = NULL,
435 .cl_ops = &prio_class_ops,
436 .id = "prio",
437 .priv_size = sizeof(struct prio_sched_data),
438 .enqueue = prio_enqueue,
439 .dequeue = prio_dequeue,
440 .requeue = prio_requeue,
441 .drop = prio_drop,
442 .init = prio_init,
443 .reset = prio_reset,
444 .destroy = prio_destroy,
445 .change = prio_tune,
446 .dump = prio_dump,
447 .owner = THIS_MODULE,
448};
449
450static int __init prio_module_init(void)
451{
452 return register_qdisc(&prio_qdisc_ops);
453}
454
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900455static void __exit prio_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 unregister_qdisc(&prio_qdisc_ops);
458}
459
460module_init(prio_module_init)
461module_exit(prio_module_exit)
462
463MODULE_LICENSE("GPL");