blob: 536ca474dc69bc86d00f6da516e41b1a925fa97d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070020#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/pkt_sched.h>
22
23
24struct prio_sched_data
25{
26 int bands;
27 struct tcf_proto *filter_list;
28 u8 prio2band[TC_PRIO_MAX+1];
29 struct Qdisc *queues[TCQ_PRIO_BANDS];
30};
31
32
33static struct Qdisc *
34prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
35{
36 struct prio_sched_data *q = qdisc_priv(sch);
37 u32 band = skb->priority;
38 struct tcf_result res;
Patrick McHardybdba91e2007-07-30 17:07:14 -070039 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080041 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (TC_H_MAJ(skb->priority) != sch->handle) {
Patrick McHardybdba91e2007-07-30 17:07:14 -070043 err = tc_classify(skb, q->filter_list, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef CONFIG_NET_CLS_ACT
Lucas Nussbaumdbaaa072007-08-30 22:35:46 -070045 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 case TC_ACT_STOLEN:
47 case TC_ACT_QUEUED:
48 *qerr = NET_XMIT_SUCCESS;
49 case TC_ACT_SHOT:
50 return NULL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
Patrick McHardybdba91e2007-07-30 17:07:14 -070053 if (!q->filter_list || err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (TC_H_MAJ(band))
55 band = 0;
David S. Miller1d8ae3f2008-07-15 02:52:19 -070056 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58 band = res.classid;
59 }
60 band = TC_H_MIN(band) - 1;
Jamal Hadi Salim3e5c2d32007-05-14 02:57:19 -070061 if (band >= q->bands)
David S. Miller1d8ae3f2008-07-15 02:52:19 -070062 return q->queues[q->prio2band[0]];
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return q->queues[band];
65}
66
67static int
68prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
69{
70 struct Qdisc *qdisc;
71 int ret;
72
73 qdisc = prio_classify(skb, sch, &ret);
74#ifdef CONFIG_NET_CLS_ACT
75 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080076
77 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 sch->qstats.drops++;
79 kfree_skb(skb);
80 return ret;
81 }
82#endif
83
84 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
85 sch->bstats.bytes += skb->len;
86 sch->bstats.packets++;
87 sch->q.qlen++;
88 return NET_XMIT_SUCCESS;
89 }
90 sch->qstats.drops++;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94
95static int
96prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
97{
98 struct Qdisc *qdisc;
99 int ret;
100
101 qdisc = prio_classify(skb, sch, &ret);
102#ifdef CONFIG_NET_CLS_ACT
103 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800104 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 sch->qstats.drops++;
106 kfree_skb(skb);
107 return ret;
108 }
109#endif
110
111 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
112 sch->q.qlen++;
113 sch->qstats.requeues++;
114 return 0;
115 }
116 sch->qstats.drops++;
117 return NET_XMIT_DROP;
118}
119
120
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700121static struct sk_buff *prio_dequeue(struct Qdisc* sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct prio_sched_data *q = qdisc_priv(sch);
124 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 for (prio = 0; prio < q->bands; prio++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700127 struct Qdisc *qdisc = q->queues[prio];
128 struct sk_buff *skb = qdisc->dequeue(qdisc);
129 if (skb) {
130 sch->q.qlen--;
131 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 }
134 return NULL;
135
136}
137
138static unsigned int prio_drop(struct Qdisc* sch)
139{
140 struct prio_sched_data *q = qdisc_priv(sch);
141 int prio;
142 unsigned int len;
143 struct Qdisc *qdisc;
144
145 for (prio = q->bands-1; prio >= 0; prio--) {
146 qdisc = q->queues[prio];
Patrick McHardy6d037a22006-03-20 19:00:49 -0800147 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sch->q.qlen--;
149 return len;
150 }
151 }
152 return 0;
153}
154
155
156static void
157prio_reset(struct Qdisc* sch)
158{
159 int prio;
160 struct prio_sched_data *q = qdisc_priv(sch);
161
162 for (prio=0; prio<q->bands; prio++)
163 qdisc_reset(q->queues[prio]);
164 sch->q.qlen = 0;
165}
166
167static void
168prio_destroy(struct Qdisc* sch)
169{
170 int prio;
171 struct prio_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Patrick McHardyff31ab52008-07-01 19:52:38 -0700173 tcf_destroy_chain(&q->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 for (prio=0; prio<q->bands; prio++)
175 qdisc_destroy(q->queues[prio]);
176}
177
Patrick McHardy1e904742008-01-22 22:11:17 -0800178static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct prio_sched_data *q = qdisc_priv(sch);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700181 struct tc_prio_qopt *qopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int i;
183
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700184 if (nla_len(opt) < sizeof(*qopt))
185 return -EINVAL;
186 qopt = nla_data(opt);
Patrick McHardycee63722008-01-23 20:33:32 -0800187
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700188 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190
191 for (i=0; i<=TC_PRIO_MAX; i++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700192 if (qopt->priomap[i] >= qopt->bands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EINVAL;
194 }
195
196 sch_tree_lock(sch);
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700197 q->bands = qopt->bands;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
199
200 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
201 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800202 if (child != &noop_qdisc) {
203 qdisc_tree_decrease_qlen(child, child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 sch_tree_unlock(sch);
208
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800209 for (i=0; i<q->bands; i++) {
210 if (q->queues[i] == &noop_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct Qdisc *child;
David S. Miller5ce2d482008-07-08 17:06:30 -0700212 child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700213 &pfifo_qdisc_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800214 TC_H_MAKE(sch->handle, i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (child) {
216 sch_tree_lock(sch);
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800217 child = xchg(&q->queues[i], child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Patrick McHardy5e50da02006-11-29 17:36:20 -0800219 if (child != &noop_qdisc) {
220 qdisc_tree_decrease_qlen(child,
221 child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sch_tree_unlock(sch);
225 }
226 }
227 }
228 return 0;
229}
230
Patrick McHardy1e904742008-01-22 22:11:17 -0800231static int prio_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct prio_sched_data *q = qdisc_priv(sch);
234 int i;
235
236 for (i=0; i<TCQ_PRIO_BANDS; i++)
237 q->queues[i] = &noop_qdisc;
238
239 if (opt == NULL) {
240 return -EINVAL;
241 } else {
242 int err;
243
244 if ((err= prio_tune(sch, opt)) != 0)
245 return err;
246 }
247 return 0;
248}
249
250static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
251{
252 struct prio_sched_data *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700253 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy1e904742008-01-22 22:11:17 -0800254 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct tc_prio_qopt opt;
256
257 opt.bands = q->bands;
258 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700259
Patrick McHardy1e904742008-01-22 22:11:17 -0800260 nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
261 if (nest == NULL)
262 goto nla_put_failure;
Patrick McHardy1e904742008-01-22 22:11:17 -0800263 nla_nest_compat_end(skb, nest);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return skb->len;
266
Patrick McHardy1e904742008-01-22 22:11:17 -0800267nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700268 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -1;
270}
271
272static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
273 struct Qdisc **old)
274{
275 struct prio_sched_data *q = qdisc_priv(sch);
276 unsigned long band = arg - 1;
277
278 if (band >= q->bands)
279 return -EINVAL;
280
281 if (new == NULL)
282 new = &noop_qdisc;
283
284 sch_tree_lock(sch);
285 *old = q->queues[band];
286 q->queues[band] = new;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800287 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 qdisc_reset(*old);
289 sch_tree_unlock(sch);
290
291 return 0;
292}
293
294static struct Qdisc *
295prio_leaf(struct Qdisc *sch, unsigned long arg)
296{
297 struct prio_sched_data *q = qdisc_priv(sch);
298 unsigned long band = arg - 1;
299
300 if (band >= q->bands)
301 return NULL;
302
303 return q->queues[band];
304}
305
306static unsigned long prio_get(struct Qdisc *sch, u32 classid)
307{
308 struct prio_sched_data *q = qdisc_priv(sch);
309 unsigned long band = TC_H_MIN(classid);
310
311 if (band - 1 >= q->bands)
312 return 0;
313 return band;
314}
315
316static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
317{
318 return prio_get(sch, classid);
319}
320
321
322static void prio_put(struct Qdisc *q, unsigned long cl)
323{
324 return;
325}
326
Patrick McHardy1e904742008-01-22 22:11:17 -0800327static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned long cl = *arg;
330 struct prio_sched_data *q = qdisc_priv(sch);
331
332 if (cl - 1 > q->bands)
333 return -ENOENT;
334 return 0;
335}
336
337static int prio_delete(struct Qdisc *sch, unsigned long cl)
338{
339 struct prio_sched_data *q = qdisc_priv(sch);
340 if (cl - 1 > q->bands)
341 return -ENOENT;
342 return 0;
343}
344
345
346static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
347 struct tcmsg *tcm)
348{
349 struct prio_sched_data *q = qdisc_priv(sch);
350
351 if (cl - 1 > q->bands)
352 return -ENOENT;
353 tcm->tcm_handle |= TC_H_MIN(cl);
354 if (q->queues[cl-1])
355 tcm->tcm_info = q->queues[cl-1]->handle;
356 return 0;
357}
358
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800359static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
360 struct gnet_dump *d)
361{
362 struct prio_sched_data *q = qdisc_priv(sch);
363 struct Qdisc *cl_q;
364
365 cl_q = q->queues[cl - 1];
366 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
367 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
368 return -1;
369
370 return 0;
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
374{
375 struct prio_sched_data *q = qdisc_priv(sch);
376 int prio;
377
378 if (arg->stop)
379 return;
380
381 for (prio = 0; prio < q->bands; prio++) {
382 if (arg->count < arg->skip) {
383 arg->count++;
384 continue;
385 }
386 if (arg->fn(sch, prio+1, arg) < 0) {
387 arg->stop = 1;
388 break;
389 }
390 arg->count++;
391 }
392}
393
394static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
395{
396 struct prio_sched_data *q = qdisc_priv(sch);
397
398 if (cl)
399 return NULL;
400 return &q->filter_list;
401}
402
Eric Dumazet20fea082007-11-14 01:44:41 -0800403static const struct Qdisc_class_ops prio_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .graft = prio_graft,
405 .leaf = prio_leaf,
406 .get = prio_get,
407 .put = prio_put,
408 .change = prio_change,
409 .delete = prio_delete,
410 .walk = prio_walk,
411 .tcf_chain = prio_find_tcf,
412 .bind_tcf = prio_bind,
413 .unbind_tcf = prio_put,
414 .dump = prio_dump_class,
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800415 .dump_stats = prio_dump_class_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416};
417
Eric Dumazet20fea082007-11-14 01:44:41 -0800418static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .next = NULL,
420 .cl_ops = &prio_class_ops,
421 .id = "prio",
422 .priv_size = sizeof(struct prio_sched_data),
423 .enqueue = prio_enqueue,
424 .dequeue = prio_dequeue,
425 .requeue = prio_requeue,
426 .drop = prio_drop,
427 .init = prio_init,
428 .reset = prio_reset,
429 .destroy = prio_destroy,
430 .change = prio_tune,
431 .dump = prio_dump,
432 .owner = THIS_MODULE,
433};
434
435static int __init prio_module_init(void)
436{
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700437 return register_qdisc(&prio_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900440static void __exit prio_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 unregister_qdisc(&prio_qdisc_ops);
443}
444
445module_init(prio_module_init)
446module_exit(prio_module_exit)
447
448MODULE_LICENSE("GPL");