de492682caeec546e6e40194f1793c81cffce42d
[deliverable/linux.git] / net / sched / sch_prio.c
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>
10 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
11 * Init -- EINVAL when opt undefined
12 */
13
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/skbuff.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23
24
25 struct prio_sched_data {
26 int bands;
27 struct tcf_proto __rcu *filter_list;
28 u8 prio2band[TC_PRIO_MAX+1];
29 struct Qdisc *queues[TCQ_PRIO_BANDS];
30 };
31
32
33 static struct Qdisc *
34 prio_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;
39 struct tcf_proto *fl;
40 int err;
41
42 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
43 if (TC_H_MAJ(skb->priority) != sch->handle) {
44 fl = rcu_dereference_bh(q->filter_list);
45 err = tc_classify(skb, fl, &res, false);
46 #ifdef CONFIG_NET_CLS_ACT
47 switch (err) {
48 case TC_ACT_STOLEN:
49 case TC_ACT_QUEUED:
50 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
51 case TC_ACT_SHOT:
52 return NULL;
53 }
54 #endif
55 if (!fl || err < 0) {
56 if (TC_H_MAJ(band))
57 band = 0;
58 return q->queues[q->prio2band[band & TC_PRIO_MAX]];
59 }
60 band = res.classid;
61 }
62 band = TC_H_MIN(band) - 1;
63 if (band >= q->bands)
64 return q->queues[q->prio2band[0]];
65
66 return q->queues[band];
67 }
68
69 static int
70 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
71 {
72 struct Qdisc *qdisc;
73 int ret;
74
75 qdisc = prio_classify(skb, sch, &ret);
76 #ifdef CONFIG_NET_CLS_ACT
77 if (qdisc == NULL) {
78
79 if (ret & __NET_XMIT_BYPASS)
80 qdisc_qstats_drop(sch);
81 kfree_skb(skb);
82 return ret;
83 }
84 #endif
85
86 ret = qdisc_enqueue(skb, qdisc);
87 if (ret == NET_XMIT_SUCCESS) {
88 qdisc_qstats_backlog_inc(sch, skb);
89 sch->q.qlen++;
90 return NET_XMIT_SUCCESS;
91 }
92 if (net_xmit_drop_count(ret))
93 qdisc_qstats_drop(sch);
94 return ret;
95 }
96
97 static struct sk_buff *prio_peek(struct Qdisc *sch)
98 {
99 struct prio_sched_data *q = qdisc_priv(sch);
100 int prio;
101
102 for (prio = 0; prio < q->bands; prio++) {
103 struct Qdisc *qdisc = q->queues[prio];
104 struct sk_buff *skb = qdisc->ops->peek(qdisc);
105 if (skb)
106 return skb;
107 }
108 return NULL;
109 }
110
111 static struct sk_buff *prio_dequeue(struct Qdisc *sch)
112 {
113 struct prio_sched_data *q = qdisc_priv(sch);
114 int prio;
115
116 for (prio = 0; prio < q->bands; prio++) {
117 struct Qdisc *qdisc = q->queues[prio];
118 struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
119 if (skb) {
120 qdisc_bstats_update(sch, skb);
121 qdisc_qstats_backlog_dec(sch, skb);
122 sch->q.qlen--;
123 return skb;
124 }
125 }
126 return NULL;
127
128 }
129
130 static void
131 prio_reset(struct Qdisc *sch)
132 {
133 int prio;
134 struct prio_sched_data *q = qdisc_priv(sch);
135
136 for (prio = 0; prio < q->bands; prio++)
137 qdisc_reset(q->queues[prio]);
138 sch->qstats.backlog = 0;
139 sch->q.qlen = 0;
140 }
141
142 static void
143 prio_destroy(struct Qdisc *sch)
144 {
145 int prio;
146 struct prio_sched_data *q = qdisc_priv(sch);
147
148 tcf_destroy_chain(&q->filter_list);
149 for (prio = 0; prio < q->bands; prio++)
150 qdisc_destroy(q->queues[prio]);
151 }
152
153 static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
154 {
155 struct prio_sched_data *q = qdisc_priv(sch);
156 struct tc_prio_qopt *qopt;
157 int i;
158
159 if (nla_len(opt) < sizeof(*qopt))
160 return -EINVAL;
161 qopt = nla_data(opt);
162
163 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
164 return -EINVAL;
165
166 for (i = 0; i <= TC_PRIO_MAX; i++) {
167 if (qopt->priomap[i] >= qopt->bands)
168 return -EINVAL;
169 }
170
171 sch_tree_lock(sch);
172 q->bands = qopt->bands;
173 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
174
175 for (i = q->bands; i < TCQ_PRIO_BANDS; i++) {
176 struct Qdisc *child = q->queues[i];
177 q->queues[i] = &noop_qdisc;
178 if (child != &noop_qdisc) {
179 qdisc_tree_reduce_backlog(child, child->q.qlen, child->qstats.backlog);
180 qdisc_destroy(child);
181 }
182 }
183 sch_tree_unlock(sch);
184
185 for (i = 0; i < q->bands; i++) {
186 if (q->queues[i] == &noop_qdisc) {
187 struct Qdisc *child, *old;
188
189 child = qdisc_create_dflt(sch->dev_queue,
190 &pfifo_qdisc_ops,
191 TC_H_MAKE(sch->handle, i + 1));
192 if (child) {
193 sch_tree_lock(sch);
194 old = q->queues[i];
195 q->queues[i] = child;
196
197 if (old != &noop_qdisc) {
198 qdisc_tree_reduce_backlog(old,
199 old->q.qlen,
200 old->qstats.backlog);
201 qdisc_destroy(old);
202 }
203 sch_tree_unlock(sch);
204 }
205 }
206 }
207 return 0;
208 }
209
210 static int prio_init(struct Qdisc *sch, struct nlattr *opt)
211 {
212 struct prio_sched_data *q = qdisc_priv(sch);
213 int i;
214
215 for (i = 0; i < TCQ_PRIO_BANDS; i++)
216 q->queues[i] = &noop_qdisc;
217
218 if (opt == NULL) {
219 return -EINVAL;
220 } else {
221 int err;
222
223 if ((err = prio_tune(sch, opt)) != 0)
224 return err;
225 }
226 return 0;
227 }
228
229 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
230 {
231 struct prio_sched_data *q = qdisc_priv(sch);
232 unsigned char *b = skb_tail_pointer(skb);
233 struct tc_prio_qopt opt;
234
235 opt.bands = q->bands;
236 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
237
238 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
239 goto nla_put_failure;
240
241 return skb->len;
242
243 nla_put_failure:
244 nlmsg_trim(skb, b);
245 return -1;
246 }
247
248 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
249 struct Qdisc **old)
250 {
251 struct prio_sched_data *q = qdisc_priv(sch);
252 unsigned long band = arg - 1;
253
254 if (new == NULL)
255 new = &noop_qdisc;
256
257 *old = qdisc_replace(sch, new, &q->queues[band]);
258 return 0;
259 }
260
261 static struct Qdisc *
262 prio_leaf(struct Qdisc *sch, unsigned long arg)
263 {
264 struct prio_sched_data *q = qdisc_priv(sch);
265 unsigned long band = arg - 1;
266
267 return q->queues[band];
268 }
269
270 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
271 {
272 struct prio_sched_data *q = qdisc_priv(sch);
273 unsigned long band = TC_H_MIN(classid);
274
275 if (band - 1 >= q->bands)
276 return 0;
277 return band;
278 }
279
280 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
281 {
282 return prio_get(sch, classid);
283 }
284
285
286 static void prio_put(struct Qdisc *q, unsigned long cl)
287 {
288 }
289
290 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
291 struct tcmsg *tcm)
292 {
293 struct prio_sched_data *q = qdisc_priv(sch);
294
295 tcm->tcm_handle |= TC_H_MIN(cl);
296 tcm->tcm_info = q->queues[cl-1]->handle;
297 return 0;
298 }
299
300 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
301 struct gnet_dump *d)
302 {
303 struct prio_sched_data *q = qdisc_priv(sch);
304 struct Qdisc *cl_q;
305
306 cl_q = q->queues[cl - 1];
307 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
308 d, NULL, &cl_q->bstats) < 0 ||
309 gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
310 return -1;
311
312 return 0;
313 }
314
315 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
316 {
317 struct prio_sched_data *q = qdisc_priv(sch);
318 int prio;
319
320 if (arg->stop)
321 return;
322
323 for (prio = 0; prio < q->bands; prio++) {
324 if (arg->count < arg->skip) {
325 arg->count++;
326 continue;
327 }
328 if (arg->fn(sch, prio + 1, arg) < 0) {
329 arg->stop = 1;
330 break;
331 }
332 arg->count++;
333 }
334 }
335
336 static struct tcf_proto __rcu **prio_find_tcf(struct Qdisc *sch,
337 unsigned long cl)
338 {
339 struct prio_sched_data *q = qdisc_priv(sch);
340
341 if (cl)
342 return NULL;
343 return &q->filter_list;
344 }
345
346 static const struct Qdisc_class_ops prio_class_ops = {
347 .graft = prio_graft,
348 .leaf = prio_leaf,
349 .get = prio_get,
350 .put = prio_put,
351 .walk = prio_walk,
352 .tcf_chain = prio_find_tcf,
353 .bind_tcf = prio_bind,
354 .unbind_tcf = prio_put,
355 .dump = prio_dump_class,
356 .dump_stats = prio_dump_class_stats,
357 };
358
359 static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
360 .next = NULL,
361 .cl_ops = &prio_class_ops,
362 .id = "prio",
363 .priv_size = sizeof(struct prio_sched_data),
364 .enqueue = prio_enqueue,
365 .dequeue = prio_dequeue,
366 .peek = prio_peek,
367 .init = prio_init,
368 .reset = prio_reset,
369 .destroy = prio_destroy,
370 .change = prio_tune,
371 .dump = prio_dump,
372 .owner = THIS_MODULE,
373 };
374
375 static int __init prio_module_init(void)
376 {
377 return register_qdisc(&prio_qdisc_ops);
378 }
379
380 static void __exit prio_module_exit(void)
381 {
382 unregister_qdisc(&prio_qdisc_ops);
383 }
384
385 module_init(prio_module_init)
386 module_exit(prio_module_exit)
387
388 MODULE_LICENSE("GPL");
This page took 0.04376 seconds and 5 git commands to generate.