2 * net/sched/sch_prio.c Simple 3-band priority "scheduler".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.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>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
36 #include <net/pkt_sched.h>
39 struct prio_sched_data
42 struct tcf_proto
*filter_list
;
43 u8 prio2band
[TC_PRIO_MAX
+1];
44 struct Qdisc
*queues
[TCQ_PRIO_BANDS
];
48 static __inline__
unsigned prio_classify(struct sk_buff
*skb
, struct Qdisc
*sch
)
50 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
51 struct tcf_result res
;
55 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
56 if (!q
->filter_list
|| tc_classify(skb
, q
->filter_list
, &res
)) {
59 return q
->prio2band
[band
&TC_PRIO_MAX
];
63 band
= TC_H_MIN(band
) - 1;
64 return band
< q
->bands
? band
: q
->prio2band
[0];
68 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
* sch
)
70 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
74 qdisc
= q
->queues
[prio_classify(skb
, sch
)];
76 if ((ret
= qdisc
->enqueue(skb
, qdisc
)) == 0) {
77 sch
->stats
.bytes
+= skb
->len
;
88 prio_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
90 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
94 qdisc
= q
->queues
[prio_classify(skb
, sch
)];
96 if ((ret
= qdisc
->ops
->requeue(skb
, qdisc
)) == 0) {
105 static struct sk_buff
*
106 prio_dequeue(struct Qdisc
* sch
)
109 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
113 for (prio
= 0; prio
< q
->bands
; prio
++) {
114 qdisc
= q
->queues
[prio
];
115 skb
= qdisc
->dequeue(qdisc
);
126 prio_drop(struct Qdisc
* sch
)
128 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
132 for (prio
= q
->bands
-1; prio
>= 0; prio
--) {
133 qdisc
= q
->queues
[prio
];
134 if (qdisc
->ops
->drop(qdisc
)) {
144 prio_reset(struct Qdisc
* sch
)
147 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
149 for (prio
=0; prio
<q
->bands
; prio
++)
150 qdisc_reset(q
->queues
[prio
]);
155 prio_destroy(struct Qdisc
* sch
)
158 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
160 for (prio
=0; prio
<q
->bands
; prio
++) {
161 qdisc_destroy(q
->queues
[prio
]);
162 q
->queues
[prio
] = &noop_qdisc
;
167 static int prio_tune(struct Qdisc
*sch
, struct rtattr
*opt
)
169 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
170 struct tc_prio_qopt
*qopt
= RTA_DATA(opt
);
173 if (opt
->rta_len
< RTA_LENGTH(sizeof(*qopt
)))
175 if (qopt
->bands
> TCQ_PRIO_BANDS
|| qopt
->bands
< 2)
178 for (i
=0; i
<=TC_PRIO_MAX
; i
++) {
179 if (qopt
->priomap
[i
] >= qopt
->bands
)
184 q
->bands
= qopt
->bands
;
185 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
187 for (i
=q
->bands
; i
<TCQ_PRIO_BANDS
; i
++) {
188 struct Qdisc
*child
= xchg(&q
->queues
[i
], &noop_qdisc
);
189 if (child
!= &noop_qdisc
)
190 qdisc_destroy(child
);
192 sch_tree_unlock(sch
);
194 for (i
=0; i
<=TC_PRIO_MAX
; i
++) {
195 int band
= q
->prio2band
[i
];
196 if (q
->queues
[band
] == &noop_qdisc
) {
198 child
= qdisc_create_dflt(sch
->dev
, &pfifo_qdisc_ops
);
201 child
= xchg(&q
->queues
[band
], child
);
203 if (child
!= &noop_qdisc
)
204 qdisc_destroy(child
);
205 sch_tree_unlock(sch
);
212 static int prio_init(struct Qdisc
*sch
, struct rtattr
*opt
)
214 static const u8 prio2band
[TC_PRIO_MAX
+1] =
215 { 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };
216 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
219 for (i
=0; i
<TCQ_PRIO_BANDS
; i
++)
220 q
->queues
[i
] = &noop_qdisc
;
224 memcpy(q
->prio2band
, prio2band
, sizeof(prio2band
));
225 for (i
=0; i
<3; i
++) {
227 child
= qdisc_create_dflt(sch
->dev
, &pfifo_qdisc_ops
);
229 q
->queues
[i
] = child
;
234 if ((err
= prio_tune(sch
, opt
)) != 0)
241 #ifdef CONFIG_RTNETLINK
242 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
244 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
245 unsigned char *b
= skb
->tail
;
246 struct tc_prio_qopt opt
;
248 opt
.bands
= q
->bands
;
249 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+1);
250 RTA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
254 skb_trim(skb
, b
- skb
->data
);
259 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
262 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
263 unsigned long band
= arg
- 1;
265 if (band
>= q
->bands
)
272 *old
= q
->queues
[band
];
273 q
->queues
[band
] = new;
275 sch_tree_unlock(sch
);
280 static struct Qdisc
*
281 prio_leaf(struct Qdisc
*sch
, unsigned long arg
)
283 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
284 unsigned long band
= arg
- 1;
286 if (band
>= q
->bands
)
289 return q
->queues
[band
];
292 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
294 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
295 unsigned long band
= TC_H_MIN(classid
);
297 if (band
- 1 >= q
->bands
)
302 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
304 return prio_get(sch
, classid
);
308 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
313 static int prio_change(struct Qdisc
*sch
, u32 handle
, u32 parent
, struct rtattr
**tca
, unsigned long *arg
)
315 unsigned long cl
= *arg
;
316 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
318 if (cl
- 1 > q
->bands
)
323 static int prio_delete(struct Qdisc
*sch
, unsigned long cl
)
325 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
326 if (cl
- 1 > q
->bands
)
332 #ifdef CONFIG_RTNETLINK
333 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
336 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
338 if (cl
- 1 > q
->bands
)
341 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
346 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
348 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
354 for (prio
= 0; prio
< q
->bands
; prio
++) {
355 if (arg
->count
< arg
->skip
) {
359 if (arg
->fn(sch
, prio
+1, arg
) < 0) {
367 static struct tcf_proto
** prio_find_tcf(struct Qdisc
*sch
, unsigned long cl
)
369 struct prio_sched_data
*q
= (struct prio_sched_data
*)sch
->data
;
373 return &q
->filter_list
;
376 static struct Qdisc_class_ops prio_class_ops
=
391 #ifdef CONFIG_RTNETLINK
396 struct Qdisc_ops prio_qdisc_ops
=
401 sizeof(struct prio_sched_data
),
413 #ifdef CONFIG_RTNETLINK
420 int init_module(void)
422 return register_qdisc(&prio_qdisc_ops
);
425 void cleanup_module(void)
427 unregister_qdisc(&prio_qdisc_ops
);