blob: 5b3a3e48ed92e64d8d45f05fc32e820a5f701024 [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>
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 <linux/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
41struct 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
50static struct Qdisc *
51prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
52{
53 struct prio_sched_data *q = qdisc_priv(sch);
54 u32 band = skb->priority;
55 struct tcf_result res;
56
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080057 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (TC_H_MAJ(skb->priority) != sch->handle) {
59#ifdef CONFIG_NET_CLS_ACT
60 switch (tc_classify(skb, q->filter_list, &res)) {
61 case TC_ACT_STOLEN:
62 case TC_ACT_QUEUED:
63 *qerr = NET_XMIT_SUCCESS;
64 case TC_ACT_SHOT:
65 return NULL;
66 };
67
68 if (!q->filter_list ) {
69#else
70 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
71#endif
72 if (TC_H_MAJ(band))
73 band = 0;
74 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
75 }
76 band = res.classid;
77 }
78 band = TC_H_MIN(band) - 1;
79 if (band > q->bands)
80 return q->queues[q->prio2band[0]];
81
82 return q->queues[band];
83}
84
85static int
86prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
87{
88 struct Qdisc *qdisc;
89 int ret;
90
91 qdisc = prio_classify(skb, sch, &ret);
92#ifdef CONFIG_NET_CLS_ACT
93 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080094
95 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 sch->qstats.drops++;
97 kfree_skb(skb);
98 return ret;
99 }
100#endif
101
102 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
103 sch->bstats.bytes += skb->len;
104 sch->bstats.packets++;
105 sch->q.qlen++;
106 return NET_XMIT_SUCCESS;
107 }
108 sch->qstats.drops++;
109 return ret;
110}
111
112
113static int
114prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
115{
116 struct Qdisc *qdisc;
117 int ret;
118
119 qdisc = prio_classify(skb, sch, &ret);
120#ifdef CONFIG_NET_CLS_ACT
121 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800122 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sch->qstats.drops++;
124 kfree_skb(skb);
125 return ret;
126 }
127#endif
128
129 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
130 sch->q.qlen++;
131 sch->qstats.requeues++;
132 return 0;
133 }
134 sch->qstats.drops++;
135 return NET_XMIT_DROP;
136}
137
138
139static struct sk_buff *
140prio_dequeue(struct Qdisc* sch)
141{
142 struct sk_buff *skb;
143 struct prio_sched_data *q = qdisc_priv(sch);
144 int prio;
145 struct Qdisc *qdisc;
146
147 for (prio = 0; prio < q->bands; prio++) {
148 qdisc = q->queues[prio];
149 skb = qdisc->dequeue(qdisc);
150 if (skb) {
151 sch->q.qlen--;
152 return skb;
153 }
154 }
155 return NULL;
156
157}
158
159static unsigned int prio_drop(struct Qdisc* sch)
160{
161 struct prio_sched_data *q = qdisc_priv(sch);
162 int prio;
163 unsigned int len;
164 struct Qdisc *qdisc;
165
166 for (prio = q->bands-1; prio >= 0; prio--) {
167 qdisc = q->queues[prio];
168 if ((len = qdisc->ops->drop(qdisc)) != 0) {
169 sch->q.qlen--;
170 return len;
171 }
172 }
173 return 0;
174}
175
176
177static void
178prio_reset(struct Qdisc* sch)
179{
180 int prio;
181 struct prio_sched_data *q = qdisc_priv(sch);
182
183 for (prio=0; prio<q->bands; prio++)
184 qdisc_reset(q->queues[prio]);
185 sch->q.qlen = 0;
186}
187
188static void
189prio_destroy(struct Qdisc* sch)
190{
191 int prio;
192 struct prio_sched_data *q = qdisc_priv(sch);
193 struct tcf_proto *tp;
194
195 while ((tp = q->filter_list) != NULL) {
196 q->filter_list = tp->next;
197 tcf_destroy(tp);
198 }
199
200 for (prio=0; prio<q->bands; prio++)
201 qdisc_destroy(q->queues[prio]);
202}
203
204static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
205{
206 struct prio_sched_data *q = qdisc_priv(sch);
207 struct tc_prio_qopt *qopt = RTA_DATA(opt);
208 int i;
209
210 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
211 return -EINVAL;
212 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
213 return -EINVAL;
214
215 for (i=0; i<=TC_PRIO_MAX; i++) {
216 if (qopt->priomap[i] >= qopt->bands)
217 return -EINVAL;
218 }
219
220 sch_tree_lock(sch);
221 q->bands = qopt->bands;
222 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
223
224 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
225 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
226 if (child != &noop_qdisc)
227 qdisc_destroy(child);
228 }
229 sch_tree_unlock(sch);
230
231 for (i=0; i<=TC_PRIO_MAX; i++) {
232 int band = q->prio2band[i];
233 if (q->queues[band] == &noop_qdisc) {
234 struct Qdisc *child;
235 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
236 if (child) {
237 sch_tree_lock(sch);
238 child = xchg(&q->queues[band], child);
239
240 if (child != &noop_qdisc)
241 qdisc_destroy(child);
242 sch_tree_unlock(sch);
243 }
244 }
245 }
246 return 0;
247}
248
249static int prio_init(struct Qdisc *sch, struct rtattr *opt)
250{
251 struct prio_sched_data *q = qdisc_priv(sch);
252 int i;
253
254 for (i=0; i<TCQ_PRIO_BANDS; i++)
255 q->queues[i] = &noop_qdisc;
256
257 if (opt == NULL) {
258 return -EINVAL;
259 } else {
260 int err;
261
262 if ((err= prio_tune(sch, opt)) != 0)
263 return err;
264 }
265 return 0;
266}
267
268static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
269{
270 struct prio_sched_data *q = qdisc_priv(sch);
271 unsigned char *b = skb->tail;
272 struct tc_prio_qopt opt;
273
274 opt.bands = q->bands;
275 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
276 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
277 return skb->len;
278
279rtattr_failure:
280 skb_trim(skb, b - skb->data);
281 return -1;
282}
283
284static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
285 struct Qdisc **old)
286{
287 struct prio_sched_data *q = qdisc_priv(sch);
288 unsigned long band = arg - 1;
289
290 if (band >= q->bands)
291 return -EINVAL;
292
293 if (new == NULL)
294 new = &noop_qdisc;
295
296 sch_tree_lock(sch);
297 *old = q->queues[band];
298 q->queues[band] = new;
299 sch->q.qlen -= (*old)->q.qlen;
300 qdisc_reset(*old);
301 sch_tree_unlock(sch);
302
303 return 0;
304}
305
306static struct Qdisc *
307prio_leaf(struct Qdisc *sch, unsigned long arg)
308{
309 struct prio_sched_data *q = qdisc_priv(sch);
310 unsigned long band = arg - 1;
311
312 if (band >= q->bands)
313 return NULL;
314
315 return q->queues[band];
316}
317
318static unsigned long prio_get(struct Qdisc *sch, u32 classid)
319{
320 struct prio_sched_data *q = qdisc_priv(sch);
321 unsigned long band = TC_H_MIN(classid);
322
323 if (band - 1 >= q->bands)
324 return 0;
325 return band;
326}
327
328static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
329{
330 return prio_get(sch, classid);
331}
332
333
334static void prio_put(struct Qdisc *q, unsigned long cl)
335{
336 return;
337}
338
339static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
340{
341 unsigned long cl = *arg;
342 struct prio_sched_data *q = qdisc_priv(sch);
343
344 if (cl - 1 > q->bands)
345 return -ENOENT;
346 return 0;
347}
348
349static int prio_delete(struct Qdisc *sch, unsigned long cl)
350{
351 struct prio_sched_data *q = qdisc_priv(sch);
352 if (cl - 1 > q->bands)
353 return -ENOENT;
354 return 0;
355}
356
357
358static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
359 struct tcmsg *tcm)
360{
361 struct prio_sched_data *q = qdisc_priv(sch);
362
363 if (cl - 1 > q->bands)
364 return -ENOENT;
365 tcm->tcm_handle |= TC_H_MIN(cl);
366 if (q->queues[cl-1])
367 tcm->tcm_info = q->queues[cl-1]->handle;
368 return 0;
369}
370
371static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
372{
373 struct prio_sched_data *q = qdisc_priv(sch);
374 int prio;
375
376 if (arg->stop)
377 return;
378
379 for (prio = 0; prio < q->bands; prio++) {
380 if (arg->count < arg->skip) {
381 arg->count++;
382 continue;
383 }
384 if (arg->fn(sch, prio+1, arg) < 0) {
385 arg->stop = 1;
386 break;
387 }
388 arg->count++;
389 }
390}
391
392static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
393{
394 struct prio_sched_data *q = qdisc_priv(sch);
395
396 if (cl)
397 return NULL;
398 return &q->filter_list;
399}
400
401static struct Qdisc_class_ops prio_class_ops = {
402 .graft = prio_graft,
403 .leaf = prio_leaf,
404 .get = prio_get,
405 .put = prio_put,
406 .change = prio_change,
407 .delete = prio_delete,
408 .walk = prio_walk,
409 .tcf_chain = prio_find_tcf,
410 .bind_tcf = prio_bind,
411 .unbind_tcf = prio_put,
412 .dump = prio_dump_class,
413};
414
415static struct Qdisc_ops prio_qdisc_ops = {
416 .next = NULL,
417 .cl_ops = &prio_class_ops,
418 .id = "prio",
419 .priv_size = sizeof(struct prio_sched_data),
420 .enqueue = prio_enqueue,
421 .dequeue = prio_dequeue,
422 .requeue = prio_requeue,
423 .drop = prio_drop,
424 .init = prio_init,
425 .reset = prio_reset,
426 .destroy = prio_destroy,
427 .change = prio_tune,
428 .dump = prio_dump,
429 .owner = THIS_MODULE,
430};
431
432static int __init prio_module_init(void)
433{
434 return register_qdisc(&prio_qdisc_ops);
435}
436
437static void __exit prio_module_exit(void)
438{
439 unregister_qdisc(&prio_qdisc_ops);
440}
441
442module_init(prio_module_init)
443module_exit(prio_module_exit)
444
445MODULE_LICENSE("GPL");