071718bccdab65da685eae239816ac07715b139d
[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 unsigned int prio_drop(struct Qdisc *sch)
131 {
132 struct prio_sched_data *q = qdisc_priv(sch);
133 int prio;
134 unsigned int len;
135 struct Qdisc *qdisc;
136
137 for (prio = q->bands-1; prio >= 0; prio--) {
138 qdisc = q->queues[prio];
139 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
140 sch->qstats.backlog -= len;
141 sch->q.qlen--;
142 return len;
143 }
144 }
145 return 0;
146 }
147
148
149 static void
150 prio_reset(struct Qdisc *sch)
151 {
152 int prio;
153 struct prio_sched_data *q = qdisc_priv(sch);
154
155 for (prio = 0; prio < q->bands; prio++)
156 qdisc_reset(q->queues[prio]);
157 sch->qstats.backlog = 0;
158 sch->q.qlen = 0;
159 }
160
161 static void
162 prio_destroy(struct Qdisc *sch)
163 {
164 int prio;
165 struct prio_sched_data *q = qdisc_priv(sch);
166
167 tcf_destroy_chain(&q->filter_list);
168 for (prio = 0; prio < q->bands; prio++)
169 qdisc_destroy(q->queues[prio]);
170 }
171
172 static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
173 {
174 struct prio_sched_data *q = qdisc_priv(sch);
175 struct tc_prio_qopt *qopt;
176 int i;
177
178 if (nla_len(opt) < sizeof(*qopt))
179 return -EINVAL;
180 qopt = nla_data(opt);
181
182 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
183 return -EINVAL;
184
185 for (i = 0; i <= TC_PRIO_MAX; i++) {
186 if (qopt->priomap[i] >= qopt->bands)
187 return -EINVAL;
188 }
189
190 sch_tree_lock(sch);
191 q->bands = qopt->bands;
192 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
193
194 for (i = q->bands; i < TCQ_PRIO_BANDS; i++) {
195 struct Qdisc *child = q->queues[i];
196 q->queues[i] = &noop_qdisc;
197 if (child != &noop_qdisc) {
198 qdisc_tree_reduce_backlog(child, child->q.qlen, child->qstats.backlog);
199 qdisc_destroy(child);
200 }
201 }
202 sch_tree_unlock(sch);
203
204 for (i = 0; i < q->bands; i++) {
205 struct Qdisc *child;
206
207 if (q->queues[i] != &noop_qdisc)
208 continue;
209
210 child = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
211 TC_H_MAKE(sch->handle, i + 1));
212 if (!child)
213 return -ENOMEM;
214 sch_tree_lock(sch);
215 q->queues[i] = child;
216 sch_tree_unlock(sch);
217 }
218 return 0;
219 }
220
221 static int prio_init(struct Qdisc *sch, struct nlattr *opt)
222 {
223 struct prio_sched_data *q = qdisc_priv(sch);
224 int i;
225
226 for (i = 0; i < TCQ_PRIO_BANDS; i++)
227 q->queues[i] = &noop_qdisc;
228
229 if (opt == NULL) {
230 return -EINVAL;
231 } else {
232 int err;
233
234 if ((err = prio_tune(sch, opt)) != 0)
235 return err;
236 }
237 return 0;
238 }
239
240 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
241 {
242 struct prio_sched_data *q = qdisc_priv(sch);
243 unsigned char *b = skb_tail_pointer(skb);
244 struct tc_prio_qopt opt;
245
246 opt.bands = q->bands;
247 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
248
249 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
250 goto nla_put_failure;
251
252 return skb->len;
253
254 nla_put_failure:
255 nlmsg_trim(skb, b);
256 return -1;
257 }
258
259 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
260 struct Qdisc **old)
261 {
262 struct prio_sched_data *q = qdisc_priv(sch);
263 unsigned long band = arg - 1;
264
265 if (new == NULL)
266 new = &noop_qdisc;
267
268 *old = qdisc_replace(sch, new, &q->queues[band]);
269 return 0;
270 }
271
272 static struct Qdisc *
273 prio_leaf(struct Qdisc *sch, unsigned long arg)
274 {
275 struct prio_sched_data *q = qdisc_priv(sch);
276 unsigned long band = arg - 1;
277
278 return q->queues[band];
279 }
280
281 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
282 {
283 struct prio_sched_data *q = qdisc_priv(sch);
284 unsigned long band = TC_H_MIN(classid);
285
286 if (band - 1 >= q->bands)
287 return 0;
288 return band;
289 }
290
291 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
292 {
293 return prio_get(sch, classid);
294 }
295
296
297 static void prio_put(struct Qdisc *q, unsigned long cl)
298 {
299 }
300
301 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
302 struct tcmsg *tcm)
303 {
304 struct prio_sched_data *q = qdisc_priv(sch);
305
306 tcm->tcm_handle |= TC_H_MIN(cl);
307 tcm->tcm_info = q->queues[cl-1]->handle;
308 return 0;
309 }
310
311 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
312 struct gnet_dump *d)
313 {
314 struct prio_sched_data *q = qdisc_priv(sch);
315 struct Qdisc *cl_q;
316
317 cl_q = q->queues[cl - 1];
318 if (gnet_stats_copy_basic(d, NULL, &cl_q->bstats) < 0 ||
319 gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
320 return -1;
321
322 return 0;
323 }
324
325 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
326 {
327 struct prio_sched_data *q = qdisc_priv(sch);
328 int prio;
329
330 if (arg->stop)
331 return;
332
333 for (prio = 0; prio < q->bands; prio++) {
334 if (arg->count < arg->skip) {
335 arg->count++;
336 continue;
337 }
338 if (arg->fn(sch, prio + 1, arg) < 0) {
339 arg->stop = 1;
340 break;
341 }
342 arg->count++;
343 }
344 }
345
346 static struct tcf_proto __rcu **prio_find_tcf(struct Qdisc *sch,
347 unsigned long cl)
348 {
349 struct prio_sched_data *q = qdisc_priv(sch);
350
351 if (cl)
352 return NULL;
353 return &q->filter_list;
354 }
355
356 static const struct Qdisc_class_ops prio_class_ops = {
357 .graft = prio_graft,
358 .leaf = prio_leaf,
359 .get = prio_get,
360 .put = prio_put,
361 .walk = prio_walk,
362 .tcf_chain = prio_find_tcf,
363 .bind_tcf = prio_bind,
364 .unbind_tcf = prio_put,
365 .dump = prio_dump_class,
366 .dump_stats = prio_dump_class_stats,
367 };
368
369 static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
370 .next = NULL,
371 .cl_ops = &prio_class_ops,
372 .id = "prio",
373 .priv_size = sizeof(struct prio_sched_data),
374 .enqueue = prio_enqueue,
375 .dequeue = prio_dequeue,
376 .peek = prio_peek,
377 .drop = prio_drop,
378 .init = prio_init,
379 .reset = prio_reset,
380 .destroy = prio_destroy,
381 .change = prio_tune,
382 .dump = prio_dump,
383 .owner = THIS_MODULE,
384 };
385
386 static int __init prio_module_init(void)
387 {
388 return register_qdisc(&prio_qdisc_ops);
389 }
390
391 static void __exit prio_module_exit(void)
392 {
393 unregister_qdisc(&prio_qdisc_ops);
394 }
395
396 module_init(prio_module_init)
397 module_exit(prio_module_exit)
398
399 MODULE_LICENSE("GPL");
This page took 0.053301 seconds and 4 git commands to generate.