blob: 404522046289cba0fddcad38dff6a4d7608ba17f [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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070035#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/sock.h>
37#include <net/pkt_sched.h>
38
39
40struct prio_sched_data
41{
42 int bands;
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -070043 int curband; /* for round-robin */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct tcf_proto *filter_list;
45 u8 prio2band[TC_PRIO_MAX+1];
46 struct Qdisc *queues[TCQ_PRIO_BANDS];
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -070047 int mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50
51static struct Qdisc *
52prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
53{
54 struct prio_sched_data *q = qdisc_priv(sch);
55 u32 band = skb->priority;
56 struct tcf_result res;
57
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080058 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (TC_H_MAJ(skb->priority) != sch->handle) {
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;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (!q->filter_list ) {
70#else
71 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
72#endif
73 if (TC_H_MAJ(band))
74 band = 0;
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -070075 band = q->prio2band[band&TC_PRIO_MAX];
76 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 band = res.classid;
79 }
80 band = TC_H_MIN(band) - 1;
Jamal Hadi Salim3e5c2d32007-05-14 02:57:19 -070081 if (band >= q->bands)
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -070082 band = q->prio2band[0];
83out:
84 if (q->mq)
85 skb_set_queue_mapping(skb, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return q->queues[band];
87}
88
89static int
90prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
91{
92 struct Qdisc *qdisc;
93 int ret;
94
95 qdisc = prio_classify(skb, sch, &ret);
96#ifdef CONFIG_NET_CLS_ACT
97 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080098
99 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 sch->qstats.drops++;
101 kfree_skb(skb);
102 return ret;
103 }
104#endif
105
106 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
107 sch->bstats.bytes += skb->len;
108 sch->bstats.packets++;
109 sch->q.qlen++;
110 return NET_XMIT_SUCCESS;
111 }
112 sch->qstats.drops++;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116
117static int
118prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
119{
120 struct Qdisc *qdisc;
121 int ret;
122
123 qdisc = prio_classify(skb, sch, &ret);
124#ifdef CONFIG_NET_CLS_ACT
125 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800126 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 sch->qstats.drops++;
128 kfree_skb(skb);
129 return ret;
130 }
131#endif
132
133 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
134 sch->q.qlen++;
135 sch->qstats.requeues++;
136 return 0;
137 }
138 sch->qstats.drops++;
139 return NET_XMIT_DROP;
140}
141
142
143static struct sk_buff *
144prio_dequeue(struct Qdisc* sch)
145{
146 struct sk_buff *skb;
147 struct prio_sched_data *q = qdisc_priv(sch);
148 int prio;
149 struct Qdisc *qdisc;
150
151 for (prio = 0; prio < q->bands; prio++) {
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700152 /* Check if the target subqueue is available before
153 * pulling an skb. This way we avoid excessive requeues
154 * for slower queues.
155 */
156 if (!netif_subqueue_stopped(sch->dev, (q->mq ? prio : 0))) {
157 qdisc = q->queues[prio];
158 skb = qdisc->dequeue(qdisc);
159 if (skb) {
160 sch->q.qlen--;
161 return skb;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 }
165 return NULL;
166
167}
168
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700169static struct sk_buff *rr_dequeue(struct Qdisc* sch)
170{
171 struct sk_buff *skb;
172 struct prio_sched_data *q = qdisc_priv(sch);
173 struct Qdisc *qdisc;
174 int bandcount;
175
176 /* Only take one pass through the queues. If nothing is available,
177 * return nothing.
178 */
179 for (bandcount = 0; bandcount < q->bands; bandcount++) {
180 /* Check if the target subqueue is available before
181 * pulling an skb. This way we avoid excessive requeues
182 * for slower queues. If the queue is stopped, try the
183 * next queue.
184 */
185 if (!netif_subqueue_stopped(sch->dev,
186 (q->mq ? q->curband : 0))) {
187 qdisc = q->queues[q->curband];
188 skb = qdisc->dequeue(qdisc);
189 if (skb) {
190 sch->q.qlen--;
191 q->curband++;
192 if (q->curband >= q->bands)
193 q->curband = 0;
194 return skb;
195 }
196 }
197 q->curband++;
198 if (q->curband >= q->bands)
199 q->curband = 0;
200 }
201 return NULL;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static unsigned int prio_drop(struct Qdisc* sch)
205{
206 struct prio_sched_data *q = qdisc_priv(sch);
207 int prio;
208 unsigned int len;
209 struct Qdisc *qdisc;
210
211 for (prio = q->bands-1; prio >= 0; prio--) {
212 qdisc = q->queues[prio];
Patrick McHardy6d037a22006-03-20 19:00:49 -0800213 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sch->q.qlen--;
215 return len;
216 }
217 }
218 return 0;
219}
220
221
222static void
223prio_reset(struct Qdisc* sch)
224{
225 int prio;
226 struct prio_sched_data *q = qdisc_priv(sch);
227
228 for (prio=0; prio<q->bands; prio++)
229 qdisc_reset(q->queues[prio]);
230 sch->q.qlen = 0;
231}
232
233static void
234prio_destroy(struct Qdisc* sch)
235{
236 int prio;
237 struct prio_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Patrick McHardya48b5a62007-03-23 11:29:43 -0700239 tcf_destroy_chain(q->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 for (prio=0; prio<q->bands; prio++)
241 qdisc_destroy(q->queues[prio]);
242}
243
244static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
245{
246 struct prio_sched_data *q = qdisc_priv(sch);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700247 struct tc_prio_qopt *qopt;
248 struct rtattr *tb[TCA_PRIO_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int i;
250
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700251 if (rtattr_parse_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
252 sizeof(*qopt)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EINVAL;
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700254 q->bands = qopt->bands;
255 /* If we're multiqueue, make sure the number of incoming bands
256 * matches the number of queues on the device we're associating with.
257 * If the number of bands requested is zero, then set q->bands to
258 * dev->egress_subqueue_count.
259 */
260 q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
261 if (q->mq) {
262 if (sch->handle != TC_H_ROOT)
263 return -EINVAL;
264 if (netif_is_multiqueue(sch->dev)) {
265 if (q->bands == 0)
266 q->bands = sch->dev->egress_subqueue_count;
267 else if (q->bands != sch->dev->egress_subqueue_count)
268 return -EINVAL;
269 } else
270 return -EOPNOTSUPP;
271 }
272
273 if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EINVAL;
275
276 for (i=0; i<=TC_PRIO_MAX; i++) {
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700277 if (qopt->priomap[i] >= q->bands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EINVAL;
279 }
280
281 sch_tree_lock(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
283
284 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
285 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800286 if (child != &noop_qdisc) {
287 qdisc_tree_decrease_qlen(child, child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 sch_tree_unlock(sch);
292
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800293 for (i=0; i<q->bands; i++) {
294 if (q->queues[i] == &noop_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct Qdisc *child;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800296 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
297 TC_H_MAKE(sch->handle, i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (child) {
299 sch_tree_lock(sch);
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800300 child = xchg(&q->queues[i], child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Patrick McHardy5e50da02006-11-29 17:36:20 -0800302 if (child != &noop_qdisc) {
303 qdisc_tree_decrease_qlen(child,
304 child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 sch_tree_unlock(sch);
308 }
309 }
310 }
311 return 0;
312}
313
314static int prio_init(struct Qdisc *sch, struct rtattr *opt)
315{
316 struct prio_sched_data *q = qdisc_priv(sch);
317 int i;
318
319 for (i=0; i<TCQ_PRIO_BANDS; i++)
320 q->queues[i] = &noop_qdisc;
321
322 if (opt == NULL) {
323 return -EINVAL;
324 } else {
325 int err;
326
327 if ((err= prio_tune(sch, opt)) != 0)
328 return err;
329 }
330 return 0;
331}
332
333static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
334{
335 struct prio_sched_data *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700336 unsigned char *b = skb_tail_pointer(skb);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700337 struct rtattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct tc_prio_qopt opt;
339
340 opt.bands = q->bands;
341 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700342
343 nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
344 if (q->mq)
345 RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
346 RTA_NEST_COMPAT_END(skb, nest);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return skb->len;
349
350rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700351 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -1;
353}
354
355static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
356 struct Qdisc **old)
357{
358 struct prio_sched_data *q = qdisc_priv(sch);
359 unsigned long band = arg - 1;
360
361 if (band >= q->bands)
362 return -EINVAL;
363
364 if (new == NULL)
365 new = &noop_qdisc;
366
367 sch_tree_lock(sch);
368 *old = q->queues[band];
369 q->queues[band] = new;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800370 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 qdisc_reset(*old);
372 sch_tree_unlock(sch);
373
374 return 0;
375}
376
377static struct Qdisc *
378prio_leaf(struct Qdisc *sch, unsigned long arg)
379{
380 struct prio_sched_data *q = qdisc_priv(sch);
381 unsigned long band = arg - 1;
382
383 if (band >= q->bands)
384 return NULL;
385
386 return q->queues[band];
387}
388
389static unsigned long prio_get(struct Qdisc *sch, u32 classid)
390{
391 struct prio_sched_data *q = qdisc_priv(sch);
392 unsigned long band = TC_H_MIN(classid);
393
394 if (band - 1 >= q->bands)
395 return 0;
396 return band;
397}
398
399static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
400{
401 return prio_get(sch, classid);
402}
403
404
405static void prio_put(struct Qdisc *q, unsigned long cl)
406{
407 return;
408}
409
410static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
411{
412 unsigned long cl = *arg;
413 struct prio_sched_data *q = qdisc_priv(sch);
414
415 if (cl - 1 > q->bands)
416 return -ENOENT;
417 return 0;
418}
419
420static int prio_delete(struct Qdisc *sch, unsigned long cl)
421{
422 struct prio_sched_data *q = qdisc_priv(sch);
423 if (cl - 1 > q->bands)
424 return -ENOENT;
425 return 0;
426}
427
428
429static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
430 struct tcmsg *tcm)
431{
432 struct prio_sched_data *q = qdisc_priv(sch);
433
434 if (cl - 1 > q->bands)
435 return -ENOENT;
436 tcm->tcm_handle |= TC_H_MIN(cl);
437 if (q->queues[cl-1])
438 tcm->tcm_info = q->queues[cl-1]->handle;
439 return 0;
440}
441
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800442static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
443 struct gnet_dump *d)
444{
445 struct prio_sched_data *q = qdisc_priv(sch);
446 struct Qdisc *cl_q;
447
448 cl_q = q->queues[cl - 1];
449 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
450 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
451 return -1;
452
453 return 0;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
457{
458 struct prio_sched_data *q = qdisc_priv(sch);
459 int prio;
460
461 if (arg->stop)
462 return;
463
464 for (prio = 0; prio < q->bands; prio++) {
465 if (arg->count < arg->skip) {
466 arg->count++;
467 continue;
468 }
469 if (arg->fn(sch, prio+1, arg) < 0) {
470 arg->stop = 1;
471 break;
472 }
473 arg->count++;
474 }
475}
476
477static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
478{
479 struct prio_sched_data *q = qdisc_priv(sch);
480
481 if (cl)
482 return NULL;
483 return &q->filter_list;
484}
485
486static struct Qdisc_class_ops prio_class_ops = {
487 .graft = prio_graft,
488 .leaf = prio_leaf,
489 .get = prio_get,
490 .put = prio_put,
491 .change = prio_change,
492 .delete = prio_delete,
493 .walk = prio_walk,
494 .tcf_chain = prio_find_tcf,
495 .bind_tcf = prio_bind,
496 .unbind_tcf = prio_put,
497 .dump = prio_dump_class,
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800498 .dump_stats = prio_dump_class_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};
500
501static struct Qdisc_ops prio_qdisc_ops = {
502 .next = NULL,
503 .cl_ops = &prio_class_ops,
504 .id = "prio",
505 .priv_size = sizeof(struct prio_sched_data),
506 .enqueue = prio_enqueue,
507 .dequeue = prio_dequeue,
508 .requeue = prio_requeue,
509 .drop = prio_drop,
510 .init = prio_init,
511 .reset = prio_reset,
512 .destroy = prio_destroy,
513 .change = prio_tune,
514 .dump = prio_dump,
515 .owner = THIS_MODULE,
516};
517
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700518static struct Qdisc_ops rr_qdisc_ops = {
519 .next = NULL,
520 .cl_ops = &prio_class_ops,
521 .id = "rr",
522 .priv_size = sizeof(struct prio_sched_data),
523 .enqueue = prio_enqueue,
524 .dequeue = rr_dequeue,
525 .requeue = prio_requeue,
526 .drop = prio_drop,
527 .init = prio_init,
528 .reset = prio_reset,
529 .destroy = prio_destroy,
530 .change = prio_tune,
531 .dump = prio_dump,
532 .owner = THIS_MODULE,
533};
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static int __init prio_module_init(void)
536{
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700537 int err;
538
539 err = register_qdisc(&prio_qdisc_ops);
540 if (err < 0)
541 return err;
542 err = register_qdisc(&rr_qdisc_ops);
543 if (err < 0)
544 unregister_qdisc(&prio_qdisc_ops);
545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900548static void __exit prio_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 unregister_qdisc(&prio_qdisc_ops);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700551 unregister_qdisc(&rr_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554module_init(prio_module_init)
555module_exit(prio_module_exit)
556
557MODULE_LICENSE("GPL");
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700558MODULE_ALIAS("sch_rr");