highmem: Export totalhigh_pages.
[deliverable/linux.git] / net / sched / sch_prio.c
CommitLineData
1da177e4
LT
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>
10297b99 10 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
1da177e4
LT
11 * Init -- EINVAL when opt undefined
12 */
13
1da177e4 14#include <linux/module.h>
1da177e4
LT
15#include <linux/types.h>
16#include <linux/kernel.h>
1da177e4 17#include <linux/string.h>
1da177e4 18#include <linux/errno.h>
1da177e4 19#include <linux/skbuff.h>
dc5fc579 20#include <net/netlink.h>
1da177e4
LT
21#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;
bdba91ec 39 int err;
1da177e4 40
29f1df6c 41 *qerr = NET_XMIT_BYPASS;
1da177e4 42 if (TC_H_MAJ(skb->priority) != sch->handle) {
bdba91ec 43 err = tc_classify(skb, q->filter_list, &res);
1da177e4 44#ifdef CONFIG_NET_CLS_ACT
dbaaa07a 45 switch (err) {
1da177e4
LT
46 case TC_ACT_STOLEN:
47 case TC_ACT_QUEUED:
48 *qerr = NET_XMIT_SUCCESS;
49 case TC_ACT_SHOT:
50 return NULL;
3ff50b79 51 }
1da177e4 52#endif
bdba91ec 53 if (!q->filter_list || err < 0) {
1da177e4
LT
54 if (TC_H_MAJ(band))
55 band = 0;
1d8ae3fd 56 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
1da177e4
LT
57 }
58 band = res.classid;
59 }
60 band = TC_H_MIN(band) - 1;
3e5c2d3b 61 if (band >= q->bands)
1d8ae3fd
DM
62 return q->queues[q->prio2band[0]];
63
1da177e4
LT
64 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) {
29f1df6c
JHS
76
77 if (ret == NET_XMIT_BYPASS)
1da177e4
LT
78 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++;
10297b99 91 return ret;
1da177e4
LT
92}
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) {
29f1df6c 104 if (ret == NET_XMIT_BYPASS)
1da177e4
LT
105 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
1d8ae3fd 121static struct sk_buff *prio_dequeue(struct Qdisc* sch)
1da177e4 122{
1da177e4
LT
123 struct prio_sched_data *q = qdisc_priv(sch);
124 int prio;
1da177e4
LT
125
126 for (prio = 0; prio < q->bands; prio++) {
1d8ae3fd
DM
127 struct Qdisc *qdisc = q->queues[prio];
128 struct sk_buff *skb = qdisc->dequeue(qdisc);
129 if (skb) {
130 sch->q.qlen--;
131 return skb;
1da177e4
LT
132 }
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];
6d037a26 147 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
1da177e4
LT
148 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);
1da177e4 172
ff31ab56 173 tcf_destroy_chain(&q->filter_list);
1da177e4
LT
174 for (prio=0; prio<q->bands; prio++)
175 qdisc_destroy(q->queues[prio]);
176}
177
1e90474c 178static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
1da177e4
LT
179{
180 struct prio_sched_data *q = qdisc_priv(sch);
d62733c8 181 struct tc_prio_qopt *qopt;
1da177e4
LT
182 int i;
183
1d8ae3fd
DM
184 if (nla_len(opt) < sizeof(*qopt))
185 return -EINVAL;
186 qopt = nla_data(opt);
d62733c8 187
1d8ae3fd 188 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
1da177e4
LT
189 return -EINVAL;
190
191 for (i=0; i<=TC_PRIO_MAX; i++) {
1d8ae3fd 192 if (qopt->priomap[i] >= qopt->bands)
1da177e4
LT
193 return -EINVAL;
194 }
195
196 sch_tree_lock(sch);
1d8ae3fd 197 q->bands = qopt->bands;
1da177e4
LT
198 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);
5e50da01
PM
202 if (child != &noop_qdisc) {
203 qdisc_tree_decrease_qlen(child, child->q.qlen);
1da177e4 204 qdisc_destroy(child);
5e50da01 205 }
1da177e4
LT
206 }
207 sch_tree_unlock(sch);
208
dd914b40
AA
209 for (i=0; i<q->bands; i++) {
210 if (q->queues[i] == &noop_qdisc) {
1da177e4 211 struct Qdisc *child;
5ce2d488 212 child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
bb949fbd 213 &pfifo_qdisc_ops,
9f9afec4 214 TC_H_MAKE(sch->handle, i + 1));
1da177e4
LT
215 if (child) {
216 sch_tree_lock(sch);
dd914b40 217 child = xchg(&q->queues[i], child);
1da177e4 218
5e50da01
PM
219 if (child != &noop_qdisc) {
220 qdisc_tree_decrease_qlen(child,
221 child->q.qlen);
1da177e4 222 qdisc_destroy(child);
5e50da01 223 }
1da177e4
LT
224 sch_tree_unlock(sch);
225 }
226 }
227 }
228 return 0;
229}
230
1e90474c 231static int prio_init(struct Qdisc *sch, struct nlattr *opt)
1da177e4
LT
232{
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);
27a884dc 253 unsigned char *b = skb_tail_pointer(skb);
1e90474c 254 struct nlattr *nest;
1da177e4
LT
255 struct tc_prio_qopt opt;
256
257 opt.bands = q->bands;
258 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
d62733c8 259
1e90474c
PM
260 nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
261 if (nest == NULL)
262 goto nla_put_failure;
1e90474c 263 nla_nest_compat_end(skb, nest);
d62733c8 264
1da177e4
LT
265 return skb->len;
266
1e90474c 267nla_put_failure:
dc5fc579 268 nlmsg_trim(skb, b);
1da177e4
LT
269 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;
5e50da01 287 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
1da177e4
LT
288 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
1e90474c 327static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
1da177e4
LT
328{
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
2cf6c36c
JP
359static 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
1da177e4
LT
373static 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
20fea08b 403static const struct Qdisc_class_ops prio_class_ops = {
1da177e4
LT
404 .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,
2cf6c36c 415 .dump_stats = prio_dump_class_stats,
1da177e4
LT
416};
417
20fea08b 418static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
1da177e4
LT
419 .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{
1d8ae3fd 437 return register_qdisc(&prio_qdisc_ops);
1da177e4
LT
438}
439
10297b99 440static void __exit prio_module_exit(void)
1da177e4
LT
441{
442 unregister_qdisc(&prio_qdisc_ops);
443}
444
445module_init(prio_module_init)
446module_exit(prio_module_exit)
447
448MODULE_LICENSE("GPL");
This page took 0.361799 seconds and 5 git commands to generate.