pkt_sched: Remove 'dev' member of struct Qdisc.
[linux-2.6.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/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22
23
24 struct prio_sched_data
25 {
26         int bands;
27         int curband; /* for round-robin */
28         struct tcf_proto *filter_list;
29         u8  prio2band[TC_PRIO_MAX+1];
30         struct Qdisc *queues[TCQ_PRIO_BANDS];
31         int mq;
32 };
33
34
35 static struct Qdisc *
36 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
37 {
38         struct prio_sched_data *q = qdisc_priv(sch);
39         u32 band = skb->priority;
40         struct tcf_result res;
41         int err;
42
43         *qerr = NET_XMIT_BYPASS;
44         if (TC_H_MAJ(skb->priority) != sch->handle) {
45                 err = tc_classify(skb, q->filter_list, &res);
46 #ifdef CONFIG_NET_CLS_ACT
47                 switch (err) {
48                 case TC_ACT_STOLEN:
49                 case TC_ACT_QUEUED:
50                         *qerr = NET_XMIT_SUCCESS;
51                 case TC_ACT_SHOT:
52                         return NULL;
53                 }
54 #endif
55                 if (!q->filter_list || err < 0) {
56                         if (TC_H_MAJ(band))
57                                 band = 0;
58                         band = q->prio2band[band&TC_PRIO_MAX];
59                         goto out;
60                 }
61                 band = res.classid;
62         }
63         band = TC_H_MIN(band) - 1;
64         if (band >= q->bands)
65                 band = q->prio2band[0];
66 out:
67         if (q->mq)
68                 skb_set_queue_mapping(skb, band);
69         return q->queues[band];
70 }
71
72 static int
73 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
74 {
75         struct Qdisc *qdisc;
76         int ret;
77
78         qdisc = prio_classify(skb, sch, &ret);
79 #ifdef CONFIG_NET_CLS_ACT
80         if (qdisc == NULL) {
81
82                 if (ret == NET_XMIT_BYPASS)
83                         sch->qstats.drops++;
84                 kfree_skb(skb);
85                 return ret;
86         }
87 #endif
88
89         if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
90                 sch->bstats.bytes += skb->len;
91                 sch->bstats.packets++;
92                 sch->q.qlen++;
93                 return NET_XMIT_SUCCESS;
94         }
95         sch->qstats.drops++;
96         return ret;
97 }
98
99
100 static int
101 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
102 {
103         struct Qdisc *qdisc;
104         int ret;
105
106         qdisc = prio_classify(skb, sch, &ret);
107 #ifdef CONFIG_NET_CLS_ACT
108         if (qdisc == NULL) {
109                 if (ret == NET_XMIT_BYPASS)
110                         sch->qstats.drops++;
111                 kfree_skb(skb);
112                 return ret;
113         }
114 #endif
115
116         if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
117                 sch->q.qlen++;
118                 sch->qstats.requeues++;
119                 return 0;
120         }
121         sch->qstats.drops++;
122         return NET_XMIT_DROP;
123 }
124
125
126 static struct sk_buff *
127 prio_dequeue(struct Qdisc* sch)
128 {
129         struct sk_buff *skb;
130         struct prio_sched_data *q = qdisc_priv(sch);
131         int prio;
132         struct Qdisc *qdisc;
133
134         for (prio = 0; prio < q->bands; prio++) {
135                 /* Check if the target subqueue is available before
136                  * pulling an skb.  This way we avoid excessive requeues
137                  * for slower queues.
138                  */
139                 if (!__netif_subqueue_stopped(qdisc_dev(sch),
140                                               (q->mq ? prio : 0))) {
141                         qdisc = q->queues[prio];
142                         skb = qdisc->dequeue(qdisc);
143                         if (skb) {
144                                 sch->q.qlen--;
145                                 return skb;
146                         }
147                 }
148         }
149         return NULL;
150
151 }
152
153 static struct sk_buff *rr_dequeue(struct Qdisc* sch)
154 {
155         struct sk_buff *skb;
156         struct prio_sched_data *q = qdisc_priv(sch);
157         struct Qdisc *qdisc;
158         int bandcount;
159
160         /* Only take one pass through the queues.  If nothing is available,
161          * return nothing.
162          */
163         for (bandcount = 0; bandcount < q->bands; bandcount++) {
164                 /* Check if the target subqueue is available before
165                  * pulling an skb.  This way we avoid excessive requeues
166                  * for slower queues.  If the queue is stopped, try the
167                  * next queue.
168                  */
169                 if (!__netif_subqueue_stopped(qdisc_dev(sch),
170                                               (q->mq ? q->curband : 0))) {
171                         qdisc = q->queues[q->curband];
172                         skb = qdisc->dequeue(qdisc);
173                         if (skb) {
174                                 sch->q.qlen--;
175                                 q->curband++;
176                                 if (q->curband >= q->bands)
177                                         q->curband = 0;
178                                 return skb;
179                         }
180                 }
181                 q->curband++;
182                 if (q->curband >= q->bands)
183                         q->curband = 0;
184         }
185         return NULL;
186 }
187
188 static unsigned int prio_drop(struct Qdisc* sch)
189 {
190         struct prio_sched_data *q = qdisc_priv(sch);
191         int prio;
192         unsigned int len;
193         struct Qdisc *qdisc;
194
195         for (prio = q->bands-1; prio >= 0; prio--) {
196                 qdisc = q->queues[prio];
197                 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
198                         sch->q.qlen--;
199                         return len;
200                 }
201         }
202         return 0;
203 }
204
205
206 static void
207 prio_reset(struct Qdisc* sch)
208 {
209         int prio;
210         struct prio_sched_data *q = qdisc_priv(sch);
211
212         for (prio=0; prio<q->bands; prio++)
213                 qdisc_reset(q->queues[prio]);
214         sch->q.qlen = 0;
215 }
216
217 static void
218 prio_destroy(struct Qdisc* sch)
219 {
220         int prio;
221         struct prio_sched_data *q = qdisc_priv(sch);
222
223         tcf_destroy_chain(&q->filter_list);
224         for (prio=0; prio<q->bands; prio++)
225                 qdisc_destroy(q->queues[prio]);
226 }
227
228 static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
229 {
230         struct prio_sched_data *q = qdisc_priv(sch);
231         struct tc_prio_qopt *qopt;
232         struct nlattr *tb[TCA_PRIO_MAX + 1];
233         int err;
234         int i;
235
236         err = nla_parse_nested_compat(tb, TCA_PRIO_MAX, opt, NULL, qopt,
237                                       sizeof(*qopt));
238         if (err < 0)
239                 return err;
240
241         q->bands = qopt->bands;
242         /* If we're multiqueue, make sure the number of incoming bands
243          * matches the number of queues on the device we're associating with.
244          * If the number of bands requested is zero, then set q->bands to
245          * dev->egress_subqueue_count.  Also, the root qdisc must be the
246          * only one that is enabled for multiqueue, since it's the only one
247          * that interacts with the underlying device.
248          */
249         q->mq = nla_get_flag(tb[TCA_PRIO_MQ]);
250         if (q->mq) {
251                 if (sch->parent != TC_H_ROOT)
252                         return -EINVAL;
253                 if (netif_is_multiqueue(qdisc_dev(sch))) {
254                         if (q->bands == 0)
255                                 q->bands = qdisc_dev(sch)->egress_subqueue_count;
256                         else if (q->bands != qdisc_dev(sch)->egress_subqueue_count)
257                                 return -EINVAL;
258                 } else
259                         return -EOPNOTSUPP;
260         }
261
262         if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
263                 return -EINVAL;
264
265         for (i=0; i<=TC_PRIO_MAX; i++) {
266                 if (qopt->priomap[i] >= q->bands)
267                         return -EINVAL;
268         }
269
270         sch_tree_lock(sch);
271         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
272
273         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
274                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
275                 if (child != &noop_qdisc) {
276                         qdisc_tree_decrease_qlen(child, child->q.qlen);
277                         qdisc_destroy(child);
278                 }
279         }
280         sch_tree_unlock(sch);
281
282         for (i=0; i<q->bands; i++) {
283                 if (q->queues[i] == &noop_qdisc) {
284                         struct Qdisc *child;
285                         child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
286                                                   &pfifo_qdisc_ops,
287                                                   TC_H_MAKE(sch->handle, i + 1));
288                         if (child) {
289                                 sch_tree_lock(sch);
290                                 child = xchg(&q->queues[i], child);
291
292                                 if (child != &noop_qdisc) {
293                                         qdisc_tree_decrease_qlen(child,
294                                                                  child->q.qlen);
295                                         qdisc_destroy(child);
296                                 }
297                                 sch_tree_unlock(sch);
298                         }
299                 }
300         }
301         return 0;
302 }
303
304 static int prio_init(struct Qdisc *sch, struct nlattr *opt)
305 {
306         struct prio_sched_data *q = qdisc_priv(sch);
307         int i;
308
309         for (i=0; i<TCQ_PRIO_BANDS; i++)
310                 q->queues[i] = &noop_qdisc;
311
312         if (opt == NULL) {
313                 return -EINVAL;
314         } else {
315                 int err;
316
317                 if ((err= prio_tune(sch, opt)) != 0)
318                         return err;
319         }
320         return 0;
321 }
322
323 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
324 {
325         struct prio_sched_data *q = qdisc_priv(sch);
326         unsigned char *b = skb_tail_pointer(skb);
327         struct nlattr *nest;
328         struct tc_prio_qopt opt;
329
330         opt.bands = q->bands;
331         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
332
333         nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
334         if (nest == NULL)
335                 goto nla_put_failure;
336         if (q->mq) {
337                 if (nla_put_flag(skb, TCA_PRIO_MQ) < 0)
338                         goto nla_put_failure;
339         }
340         nla_nest_compat_end(skb, nest);
341
342         return skb->len;
343
344 nla_put_failure:
345         nlmsg_trim(skb, b);
346         return -1;
347 }
348
349 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
350                       struct Qdisc **old)
351 {
352         struct prio_sched_data *q = qdisc_priv(sch);
353         unsigned long band = arg - 1;
354
355         if (band >= q->bands)
356                 return -EINVAL;
357
358         if (new == NULL)
359                 new = &noop_qdisc;
360
361         sch_tree_lock(sch);
362         *old = q->queues[band];
363         q->queues[band] = new;
364         qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
365         qdisc_reset(*old);
366         sch_tree_unlock(sch);
367
368         return 0;
369 }
370
371 static struct Qdisc *
372 prio_leaf(struct Qdisc *sch, unsigned long arg)
373 {
374         struct prio_sched_data *q = qdisc_priv(sch);
375         unsigned long band = arg - 1;
376
377         if (band >= q->bands)
378                 return NULL;
379
380         return q->queues[band];
381 }
382
383 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
384 {
385         struct prio_sched_data *q = qdisc_priv(sch);
386         unsigned long band = TC_H_MIN(classid);
387
388         if (band - 1 >= q->bands)
389                 return 0;
390         return band;
391 }
392
393 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
394 {
395         return prio_get(sch, classid);
396 }
397
398
399 static void prio_put(struct Qdisc *q, unsigned long cl)
400 {
401         return;
402 }
403
404 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
405 {
406         unsigned long cl = *arg;
407         struct prio_sched_data *q = qdisc_priv(sch);
408
409         if (cl - 1 > q->bands)
410                 return -ENOENT;
411         return 0;
412 }
413
414 static int prio_delete(struct Qdisc *sch, unsigned long cl)
415 {
416         struct prio_sched_data *q = qdisc_priv(sch);
417         if (cl - 1 > q->bands)
418                 return -ENOENT;
419         return 0;
420 }
421
422
423 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
424                            struct tcmsg *tcm)
425 {
426         struct prio_sched_data *q = qdisc_priv(sch);
427
428         if (cl - 1 > q->bands)
429                 return -ENOENT;
430         tcm->tcm_handle |= TC_H_MIN(cl);
431         if (q->queues[cl-1])
432                 tcm->tcm_info = q->queues[cl-1]->handle;
433         return 0;
434 }
435
436 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
437                                  struct gnet_dump *d)
438 {
439         struct prio_sched_data *q = qdisc_priv(sch);
440         struct Qdisc *cl_q;
441
442         cl_q = q->queues[cl - 1];
443         if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
444             gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
445                 return -1;
446
447         return 0;
448 }
449
450 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
451 {
452         struct prio_sched_data *q = qdisc_priv(sch);
453         int prio;
454
455         if (arg->stop)
456                 return;
457
458         for (prio = 0; prio < q->bands; prio++) {
459                 if (arg->count < arg->skip) {
460                         arg->count++;
461                         continue;
462                 }
463                 if (arg->fn(sch, prio+1, arg) < 0) {
464                         arg->stop = 1;
465                         break;
466                 }
467                 arg->count++;
468         }
469 }
470
471 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
472 {
473         struct prio_sched_data *q = qdisc_priv(sch);
474
475         if (cl)
476                 return NULL;
477         return &q->filter_list;
478 }
479
480 static const struct Qdisc_class_ops prio_class_ops = {
481         .graft          =       prio_graft,
482         .leaf           =       prio_leaf,
483         .get            =       prio_get,
484         .put            =       prio_put,
485         .change         =       prio_change,
486         .delete         =       prio_delete,
487         .walk           =       prio_walk,
488         .tcf_chain      =       prio_find_tcf,
489         .bind_tcf       =       prio_bind,
490         .unbind_tcf     =       prio_put,
491         .dump           =       prio_dump_class,
492         .dump_stats     =       prio_dump_class_stats,
493 };
494
495 static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
496         .next           =       NULL,
497         .cl_ops         =       &prio_class_ops,
498         .id             =       "prio",
499         .priv_size      =       sizeof(struct prio_sched_data),
500         .enqueue        =       prio_enqueue,
501         .dequeue        =       prio_dequeue,
502         .requeue        =       prio_requeue,
503         .drop           =       prio_drop,
504         .init           =       prio_init,
505         .reset          =       prio_reset,
506         .destroy        =       prio_destroy,
507         .change         =       prio_tune,
508         .dump           =       prio_dump,
509         .owner          =       THIS_MODULE,
510 };
511
512 static struct Qdisc_ops rr_qdisc_ops __read_mostly = {
513         .next           =       NULL,
514         .cl_ops         =       &prio_class_ops,
515         .id             =       "rr",
516         .priv_size      =       sizeof(struct prio_sched_data),
517         .enqueue        =       prio_enqueue,
518         .dequeue        =       rr_dequeue,
519         .requeue        =       prio_requeue,
520         .drop           =       prio_drop,
521         .init           =       prio_init,
522         .reset          =       prio_reset,
523         .destroy        =       prio_destroy,
524         .change         =       prio_tune,
525         .dump           =       prio_dump,
526         .owner          =       THIS_MODULE,
527 };
528
529 static int __init prio_module_init(void)
530 {
531         int err;
532
533         err = register_qdisc(&prio_qdisc_ops);
534         if (err < 0)
535                 return err;
536         err = register_qdisc(&rr_qdisc_ops);
537         if (err < 0)
538                 unregister_qdisc(&prio_qdisc_ops);
539         return err;
540 }
541
542 static void __exit prio_module_exit(void)
543 {
544         unregister_qdisc(&prio_qdisc_ops);
545         unregister_qdisc(&rr_qdisc_ops);
546 }
547
548 module_init(prio_module_init)
549 module_exit(prio_module_exit)
550
551 MODULE_LICENSE("GPL");
552 MODULE_ALIAS("sch_rr");