]> Pileus Git - ~andy/linux/blob - net/sched/sch_prio.c
71bafde353a6f038b083994aff7c67b4d6d040c5
[~andy/linux] / 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 (tc_classify(skb, q->filter_list, &res)) {
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(sch->dev, (q->mq ? prio : 0))) {
140                         qdisc = q->queues[prio];
141                         skb = qdisc->dequeue(qdisc);
142                         if (skb) {
143                                 sch->q.qlen--;
144                                 return skb;
145                         }
146                 }
147         }
148         return NULL;
149
150 }
151
152 static struct sk_buff *rr_dequeue(struct Qdisc* sch)
153 {
154         struct sk_buff *skb;
155         struct prio_sched_data *q = qdisc_priv(sch);
156         struct Qdisc *qdisc;
157         int bandcount;
158
159         /* Only take one pass through the queues.  If nothing is available,
160          * return nothing.
161          */
162         for (bandcount = 0; bandcount < q->bands; bandcount++) {
163                 /* Check if the target subqueue is available before
164                  * pulling an skb.  This way we avoid excessive requeues
165                  * for slower queues.  If the queue is stopped, try the
166                  * next queue.
167                  */
168                 if (!netif_subqueue_stopped(sch->dev,
169                                             (q->mq ? q->curband : 0))) {
170                         qdisc = q->queues[q->curband];
171                         skb = qdisc->dequeue(qdisc);
172                         if (skb) {
173                                 sch->q.qlen--;
174                                 q->curband++;
175                                 if (q->curband >= q->bands)
176                                         q->curband = 0;
177                                 return skb;
178                         }
179                 }
180                 q->curband++;
181                 if (q->curband >= q->bands)
182                         q->curband = 0;
183         }
184         return NULL;
185 }
186
187 static unsigned int prio_drop(struct Qdisc* sch)
188 {
189         struct prio_sched_data *q = qdisc_priv(sch);
190         int prio;
191         unsigned int len;
192         struct Qdisc *qdisc;
193
194         for (prio = q->bands-1; prio >= 0; prio--) {
195                 qdisc = q->queues[prio];
196                 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
197                         sch->q.qlen--;
198                         return len;
199                 }
200         }
201         return 0;
202 }
203
204
205 static void
206 prio_reset(struct Qdisc* sch)
207 {
208         int prio;
209         struct prio_sched_data *q = qdisc_priv(sch);
210
211         for (prio=0; prio<q->bands; prio++)
212                 qdisc_reset(q->queues[prio]);
213         sch->q.qlen = 0;
214 }
215
216 static void
217 prio_destroy(struct Qdisc* sch)
218 {
219         int prio;
220         struct prio_sched_data *q = qdisc_priv(sch);
221
222         tcf_destroy_chain(q->filter_list);
223         for (prio=0; prio<q->bands; prio++)
224                 qdisc_destroy(q->queues[prio]);
225 }
226
227 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
228 {
229         struct prio_sched_data *q = qdisc_priv(sch);
230         struct tc_prio_qopt *qopt;
231         struct rtattr *tb[TCA_PRIO_MAX];
232         int i;
233
234         if (rtattr_parse_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
235                                        sizeof(*qopt)))
236                 return -EINVAL;
237         q->bands = qopt->bands;
238         /* If we're multiqueue, make sure the number of incoming bands
239          * matches the number of queues on the device we're associating with.
240          * If the number of bands requested is zero, then set q->bands to
241          * dev->egress_subqueue_count.
242          */
243         q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
244         if (q->mq) {
245                 if (sch->handle != TC_H_ROOT)
246                         return -EINVAL;
247                 if (netif_is_multiqueue(sch->dev)) {
248                         if (q->bands == 0)
249                                 q->bands = sch->dev->egress_subqueue_count;
250                         else if (q->bands != sch->dev->egress_subqueue_count)
251                                 return -EINVAL;
252                 } else
253                         return -EOPNOTSUPP;
254         }
255
256         if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
257                 return -EINVAL;
258
259         for (i=0; i<=TC_PRIO_MAX; i++) {
260                 if (qopt->priomap[i] >= q->bands)
261                         return -EINVAL;
262         }
263
264         sch_tree_lock(sch);
265         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
266
267         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
268                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
269                 if (child != &noop_qdisc) {
270                         qdisc_tree_decrease_qlen(child, child->q.qlen);
271                         qdisc_destroy(child);
272                 }
273         }
274         sch_tree_unlock(sch);
275
276         for (i=0; i<q->bands; i++) {
277                 if (q->queues[i] == &noop_qdisc) {
278                         struct Qdisc *child;
279                         child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
280                                                   TC_H_MAKE(sch->handle, i + 1));
281                         if (child) {
282                                 sch_tree_lock(sch);
283                                 child = xchg(&q->queues[i], child);
284
285                                 if (child != &noop_qdisc) {
286                                         qdisc_tree_decrease_qlen(child,
287                                                                  child->q.qlen);
288                                         qdisc_destroy(child);
289                                 }
290                                 sch_tree_unlock(sch);
291                         }
292                 }
293         }
294         return 0;
295 }
296
297 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
298 {
299         struct prio_sched_data *q = qdisc_priv(sch);
300         int i;
301
302         for (i=0; i<TCQ_PRIO_BANDS; i++)
303                 q->queues[i] = &noop_qdisc;
304
305         if (opt == NULL) {
306                 return -EINVAL;
307         } else {
308                 int err;
309
310                 if ((err= prio_tune(sch, opt)) != 0)
311                         return err;
312         }
313         return 0;
314 }
315
316 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
317 {
318         struct prio_sched_data *q = qdisc_priv(sch);
319         unsigned char *b = skb_tail_pointer(skb);
320         struct rtattr *nest;
321         struct tc_prio_qopt opt;
322
323         opt.bands = q->bands;
324         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
325
326         nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
327         if (q->mq)
328                 RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
329         RTA_NEST_COMPAT_END(skb, nest);
330
331         return skb->len;
332
333 rtattr_failure:
334         nlmsg_trim(skb, b);
335         return -1;
336 }
337
338 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
339                       struct Qdisc **old)
340 {
341         struct prio_sched_data *q = qdisc_priv(sch);
342         unsigned long band = arg - 1;
343
344         if (band >= q->bands)
345                 return -EINVAL;
346
347         if (new == NULL)
348                 new = &noop_qdisc;
349
350         sch_tree_lock(sch);
351         *old = q->queues[band];
352         q->queues[band] = new;
353         qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
354         qdisc_reset(*old);
355         sch_tree_unlock(sch);
356
357         return 0;
358 }
359
360 static struct Qdisc *
361 prio_leaf(struct Qdisc *sch, unsigned long arg)
362 {
363         struct prio_sched_data *q = qdisc_priv(sch);
364         unsigned long band = arg - 1;
365
366         if (band >= q->bands)
367                 return NULL;
368
369         return q->queues[band];
370 }
371
372 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
373 {
374         struct prio_sched_data *q = qdisc_priv(sch);
375         unsigned long band = TC_H_MIN(classid);
376
377         if (band - 1 >= q->bands)
378                 return 0;
379         return band;
380 }
381
382 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
383 {
384         return prio_get(sch, classid);
385 }
386
387
388 static void prio_put(struct Qdisc *q, unsigned long cl)
389 {
390         return;
391 }
392
393 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
394 {
395         unsigned long cl = *arg;
396         struct prio_sched_data *q = qdisc_priv(sch);
397
398         if (cl - 1 > q->bands)
399                 return -ENOENT;
400         return 0;
401 }
402
403 static int prio_delete(struct Qdisc *sch, unsigned long cl)
404 {
405         struct prio_sched_data *q = qdisc_priv(sch);
406         if (cl - 1 > q->bands)
407                 return -ENOENT;
408         return 0;
409 }
410
411
412 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
413                            struct tcmsg *tcm)
414 {
415         struct prio_sched_data *q = qdisc_priv(sch);
416
417         if (cl - 1 > q->bands)
418                 return -ENOENT;
419         tcm->tcm_handle |= TC_H_MIN(cl);
420         if (q->queues[cl-1])
421                 tcm->tcm_info = q->queues[cl-1]->handle;
422         return 0;
423 }
424
425 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
426                                  struct gnet_dump *d)
427 {
428         struct prio_sched_data *q = qdisc_priv(sch);
429         struct Qdisc *cl_q;
430
431         cl_q = q->queues[cl - 1];
432         if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
433             gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
434                 return -1;
435
436         return 0;
437 }
438
439 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
440 {
441         struct prio_sched_data *q = qdisc_priv(sch);
442         int prio;
443
444         if (arg->stop)
445                 return;
446
447         for (prio = 0; prio < q->bands; prio++) {
448                 if (arg->count < arg->skip) {
449                         arg->count++;
450                         continue;
451                 }
452                 if (arg->fn(sch, prio+1, arg) < 0) {
453                         arg->stop = 1;
454                         break;
455                 }
456                 arg->count++;
457         }
458 }
459
460 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
461 {
462         struct prio_sched_data *q = qdisc_priv(sch);
463
464         if (cl)
465                 return NULL;
466         return &q->filter_list;
467 }
468
469 static struct Qdisc_class_ops prio_class_ops = {
470         .graft          =       prio_graft,
471         .leaf           =       prio_leaf,
472         .get            =       prio_get,
473         .put            =       prio_put,
474         .change         =       prio_change,
475         .delete         =       prio_delete,
476         .walk           =       prio_walk,
477         .tcf_chain      =       prio_find_tcf,
478         .bind_tcf       =       prio_bind,
479         .unbind_tcf     =       prio_put,
480         .dump           =       prio_dump_class,
481         .dump_stats     =       prio_dump_class_stats,
482 };
483
484 static struct Qdisc_ops prio_qdisc_ops = {
485         .next           =       NULL,
486         .cl_ops         =       &prio_class_ops,
487         .id             =       "prio",
488         .priv_size      =       sizeof(struct prio_sched_data),
489         .enqueue        =       prio_enqueue,
490         .dequeue        =       prio_dequeue,
491         .requeue        =       prio_requeue,
492         .drop           =       prio_drop,
493         .init           =       prio_init,
494         .reset          =       prio_reset,
495         .destroy        =       prio_destroy,
496         .change         =       prio_tune,
497         .dump           =       prio_dump,
498         .owner          =       THIS_MODULE,
499 };
500
501 static struct Qdisc_ops rr_qdisc_ops = {
502         .next           =       NULL,
503         .cl_ops         =       &prio_class_ops,
504         .id             =       "rr",
505         .priv_size      =       sizeof(struct prio_sched_data),
506         .enqueue        =       prio_enqueue,
507         .dequeue        =       rr_dequeue,
508         .requeue        =       prio_requeue,
509         .drop           =       prio_drop,
510         .init           =       prio_init,
511         .reset          =       prio_reset,
512         .destroy        =       prio_destroy,
513         .change         =       prio_tune,
514         .dump           =       prio_dump,
515         .owner          =       THIS_MODULE,
516 };
517
518 static int __init prio_module_init(void)
519 {
520         int err;
521
522         err = register_qdisc(&prio_qdisc_ops);
523         if (err < 0)
524                 return err;
525         err = register_qdisc(&rr_qdisc_ops);
526         if (err < 0)
527                 unregister_qdisc(&prio_qdisc_ops);
528         return err;
529 }
530
531 static void __exit prio_module_exit(void)
532 {
533         unregister_qdisc(&prio_qdisc_ops);
534         unregister_qdisc(&rr_qdisc_ops);
535 }
536
537 module_init(prio_module_init)
538 module_exit(prio_module_exit)
539
540 MODULE_LICENSE("GPL");
541 MODULE_ALIAS("sch_rr");