pkt_sched: sch_drr: fix drr_dequeue loop()
[safe/jmp/linux-2.6] / net / sched / sch_drr.c
1 /*
2  * net/sched/sch_drr.c         Deficit Round Robin scheduler
3  *
4  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/netdevice.h>
15 #include <linux/pkt_sched.h>
16 #include <net/sch_generic.h>
17 #include <net/pkt_sched.h>
18 #include <net/pkt_cls.h>
19
20 struct drr_class {
21         struct Qdisc_class_common       common;
22         unsigned int                    refcnt;
23         unsigned int                    filter_cnt;
24
25         struct gnet_stats_basic         bstats;
26         struct gnet_stats_queue         qstats;
27         struct gnet_stats_rate_est      rate_est;
28         struct list_head                alist;
29         struct Qdisc                    *qdisc;
30
31         u32                             quantum;
32         u32                             deficit;
33 };
34
35 struct drr_sched {
36         struct list_head                active;
37         struct tcf_proto                *filter_list;
38         struct Qdisc_class_hash         clhash;
39 };
40
41 static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid)
42 {
43         struct drr_sched *q = qdisc_priv(sch);
44         struct Qdisc_class_common *clc;
45
46         clc = qdisc_class_find(&q->clhash, classid);
47         if (clc == NULL)
48                 return NULL;
49         return container_of(clc, struct drr_class, common);
50 }
51
52 static void drr_purge_queue(struct drr_class *cl)
53 {
54         unsigned int len = cl->qdisc->q.qlen;
55
56         qdisc_reset(cl->qdisc);
57         qdisc_tree_decrease_qlen(cl->qdisc, len);
58 }
59
60 static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = {
61         [TCA_DRR_QUANTUM]       = { .type = NLA_U32 },
62 };
63
64 static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
65                             struct nlattr **tca, unsigned long *arg)
66 {
67         struct drr_sched *q = qdisc_priv(sch);
68         struct drr_class *cl = (struct drr_class *)*arg;
69         struct nlattr *tb[TCA_DRR_MAX + 1];
70         u32 quantum;
71         int err;
72
73         err = nla_parse_nested(tb, TCA_DRR_MAX, tca[TCA_OPTIONS], drr_policy);
74         if (err < 0)
75                 return err;
76
77         if (tb[TCA_DRR_QUANTUM]) {
78                 quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
79                 if (quantum == 0)
80                         return -EINVAL;
81         } else
82                 quantum = psched_mtu(qdisc_dev(sch));
83
84         if (cl != NULL) {
85                 sch_tree_lock(sch);
86                 if (tb[TCA_DRR_QUANTUM])
87                         cl->quantum = quantum;
88                 sch_tree_unlock(sch);
89
90                 if (tca[TCA_RATE])
91                         gen_replace_estimator(&cl->bstats, &cl->rate_est,
92                                               qdisc_root_sleeping_lock(sch),
93                                               tca[TCA_RATE]);
94                 return 0;
95         }
96
97         cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
98         if (cl == NULL)
99                 return -ENOBUFS;
100
101         cl->refcnt         = 1;
102         cl->common.classid = classid;
103         cl->quantum        = quantum;
104         cl->qdisc          = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
105                                                &pfifo_qdisc_ops, classid);
106         if (cl->qdisc == NULL)
107                 cl->qdisc = &noop_qdisc;
108
109         if (tca[TCA_RATE])
110                 gen_replace_estimator(&cl->bstats, &cl->rate_est,
111                                       qdisc_root_sleeping_lock(sch),
112                                       tca[TCA_RATE]);
113
114         sch_tree_lock(sch);
115         qdisc_class_hash_insert(&q->clhash, &cl->common);
116         sch_tree_unlock(sch);
117
118         qdisc_class_hash_grow(sch, &q->clhash);
119
120         *arg = (unsigned long)cl;
121         return 0;
122 }
123
124 static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
125 {
126         gen_kill_estimator(&cl->bstats, &cl->rate_est);
127         qdisc_destroy(cl->qdisc);
128         kfree(cl);
129 }
130
131 static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
132 {
133         struct drr_sched *q = qdisc_priv(sch);
134         struct drr_class *cl = (struct drr_class *)arg;
135
136         if (cl->filter_cnt > 0)
137                 return -EBUSY;
138
139         sch_tree_lock(sch);
140
141         drr_purge_queue(cl);
142         qdisc_class_hash_remove(&q->clhash, &cl->common);
143
144         if (--cl->refcnt == 0)
145                 drr_destroy_class(sch, cl);
146
147         sch_tree_unlock(sch);
148         return 0;
149 }
150
151 static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
152 {
153         struct drr_class *cl = drr_find_class(sch, classid);
154
155         if (cl != NULL)
156                 cl->refcnt++;
157
158         return (unsigned long)cl;
159 }
160
161 static void drr_put_class(struct Qdisc *sch, unsigned long arg)
162 {
163         struct drr_class *cl = (struct drr_class *)arg;
164
165         if (--cl->refcnt == 0)
166                 drr_destroy_class(sch, cl);
167 }
168
169 static struct tcf_proto **drr_tcf_chain(struct Qdisc *sch, unsigned long cl)
170 {
171         struct drr_sched *q = qdisc_priv(sch);
172
173         if (cl)
174                 return NULL;
175
176         return &q->filter_list;
177 }
178
179 static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
180                                   u32 classid)
181 {
182         struct drr_class *cl = drr_find_class(sch, classid);
183
184         if (cl != NULL)
185                 cl->filter_cnt++;
186
187         return (unsigned long)cl;
188 }
189
190 static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
191 {
192         struct drr_class *cl = (struct drr_class *)arg;
193
194         cl->filter_cnt--;
195 }
196
197 static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
198                            struct Qdisc *new, struct Qdisc **old)
199 {
200         struct drr_class *cl = (struct drr_class *)arg;
201
202         if (new == NULL) {
203                 new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
204                                         &pfifo_qdisc_ops, cl->common.classid);
205                 if (new == NULL)
206                         new = &noop_qdisc;
207         }
208
209         sch_tree_lock(sch);
210         drr_purge_queue(cl);
211         *old = cl->qdisc;
212         cl->qdisc = new;
213         sch_tree_unlock(sch);
214         return 0;
215 }
216
217 static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
218 {
219         struct drr_class *cl = (struct drr_class *)arg;
220
221         return cl->qdisc;
222 }
223
224 static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
225 {
226         struct drr_class *cl = (struct drr_class *)arg;
227
228         if (cl->qdisc->q.qlen == 0)
229                 list_del(&cl->alist);
230 }
231
232 static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
233                           struct sk_buff *skb, struct tcmsg *tcm)
234 {
235         struct drr_class *cl = (struct drr_class *)arg;
236         struct nlattr *nest;
237
238         tcm->tcm_parent = TC_H_ROOT;
239         tcm->tcm_handle = cl->common.classid;
240         tcm->tcm_info   = cl->qdisc->handle;
241
242         nest = nla_nest_start(skb, TCA_OPTIONS);
243         if (nest == NULL)
244                 goto nla_put_failure;
245         NLA_PUT_U32(skb, TCA_DRR_QUANTUM, cl->quantum);
246         return nla_nest_end(skb, nest);
247
248 nla_put_failure:
249         nla_nest_cancel(skb, nest);
250         return -EMSGSIZE;
251 }
252
253 static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
254                                 struct gnet_dump *d)
255 {
256         struct drr_class *cl = (struct drr_class *)arg;
257         struct tc_drr_stats xstats;
258
259         memset(&xstats, 0, sizeof(xstats));
260         if (cl->qdisc->q.qlen)
261                 xstats.deficit = cl->deficit;
262
263         if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
264             gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
265             gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
266                 return -1;
267
268         return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
269 }
270
271 static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
272 {
273         struct drr_sched *q = qdisc_priv(sch);
274         struct drr_class *cl;
275         struct hlist_node *n;
276         unsigned int i;
277
278         if (arg->stop)
279                 return;
280
281         for (i = 0; i < q->clhash.hashsize; i++) {
282                 hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
283                         if (arg->count < arg->skip) {
284                                 arg->count++;
285                                 continue;
286                         }
287                         if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
288                                 arg->stop = 1;
289                                 return;
290                         }
291                         arg->count++;
292                 }
293         }
294 }
295
296 static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
297                                       int *qerr)
298 {
299         struct drr_sched *q = qdisc_priv(sch);
300         struct drr_class *cl;
301         struct tcf_result res;
302         int result;
303
304         if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
305                 cl = drr_find_class(sch, skb->priority);
306                 if (cl != NULL)
307                         return cl;
308         }
309
310         *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
311         result = tc_classify(skb, q->filter_list, &res);
312         if (result >= 0) {
313 #ifdef CONFIG_NET_CLS_ACT
314                 switch (result) {
315                 case TC_ACT_QUEUED:
316                 case TC_ACT_STOLEN:
317                         *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
318                 case TC_ACT_SHOT:
319                         return NULL;
320                 }
321 #endif
322                 cl = (struct drr_class *)res.class;
323                 if (cl == NULL)
324                         cl = drr_find_class(sch, res.classid);
325                 return cl;
326         }
327         return NULL;
328 }
329
330 static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch)
331 {
332         struct drr_sched *q = qdisc_priv(sch);
333         struct drr_class *cl;
334         unsigned int len;
335         int err;
336
337         cl = drr_classify(skb, sch, &err);
338         if (cl == NULL) {
339                 if (err & __NET_XMIT_BYPASS)
340                         sch->qstats.drops++;
341                 kfree_skb(skb);
342                 return err;
343         }
344
345         len = qdisc_pkt_len(skb);
346         err = qdisc_enqueue(skb, cl->qdisc);
347         if (unlikely(err != NET_XMIT_SUCCESS)) {
348                 if (net_xmit_drop_count(err)) {
349                         cl->qstats.drops++;
350                         sch->qstats.drops++;
351                 }
352                 return err;
353         }
354
355         if (cl->qdisc->q.qlen == 1) {
356                 list_add_tail(&cl->alist, &q->active);
357                 cl->deficit = cl->quantum;
358         }
359
360         cl->bstats.packets++;
361         cl->bstats.bytes += len;
362         sch->bstats.packets++;
363         sch->bstats.bytes += len;
364
365         sch->q.qlen++;
366         return err;
367 }
368
369 static struct sk_buff *drr_dequeue(struct Qdisc *sch)
370 {
371         struct drr_sched *q = qdisc_priv(sch);
372         struct drr_class *cl;
373         struct sk_buff *skb;
374         unsigned int len;
375
376         if (list_empty(&q->active))
377                 goto out;
378         while (1) {
379                 cl = list_first_entry(&q->active, struct drr_class, alist);
380                 skb = cl->qdisc->ops->peek(cl->qdisc);
381                 if (skb == NULL)
382                         goto out;
383
384                 len = qdisc_pkt_len(skb);
385                 if (len <= cl->deficit) {
386                         cl->deficit -= len;
387                         skb = qdisc_dequeue_peeked(cl->qdisc);
388                         if (cl->qdisc->q.qlen == 0)
389                                 list_del(&cl->alist);
390                         sch->q.qlen--;
391                         return skb;
392                 }
393
394                 cl->deficit += cl->quantum;
395                 list_move_tail(&cl->alist, &q->active);
396         }
397 out:
398         return NULL;
399 }
400
401 static unsigned int drr_drop(struct Qdisc *sch)
402 {
403         struct drr_sched *q = qdisc_priv(sch);
404         struct drr_class *cl;
405         unsigned int len;
406
407         list_for_each_entry(cl, &q->active, alist) {
408                 if (cl->qdisc->ops->drop) {
409                         len = cl->qdisc->ops->drop(cl->qdisc);
410                         if (len > 0) {
411                                 sch->q.qlen--;
412                                 if (cl->qdisc->q.qlen == 0)
413                                         list_del(&cl->alist);
414                                 return len;
415                         }
416                 }
417         }
418         return 0;
419 }
420
421 static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
422 {
423         struct drr_sched *q = qdisc_priv(sch);
424         int err;
425
426         err = qdisc_class_hash_init(&q->clhash);
427         if (err < 0)
428                 return err;
429         INIT_LIST_HEAD(&q->active);
430         return 0;
431 }
432
433 static void drr_reset_qdisc(struct Qdisc *sch)
434 {
435         struct drr_sched *q = qdisc_priv(sch);
436         struct drr_class *cl;
437         struct hlist_node *n;
438         unsigned int i;
439
440         for (i = 0; i < q->clhash.hashsize; i++) {
441                 hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
442                         if (cl->qdisc->q.qlen)
443                                 list_del(&cl->alist);
444                         qdisc_reset(cl->qdisc);
445                 }
446         }
447         sch->q.qlen = 0;
448 }
449
450 static void drr_destroy_qdisc(struct Qdisc *sch)
451 {
452         struct drr_sched *q = qdisc_priv(sch);
453         struct drr_class *cl;
454         struct hlist_node *n, *next;
455         unsigned int i;
456
457         tcf_destroy_chain(&q->filter_list);
458
459         for (i = 0; i < q->clhash.hashsize; i++) {
460                 hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
461                                           common.hnode)
462                         drr_destroy_class(sch, cl);
463         }
464         qdisc_class_hash_destroy(&q->clhash);
465 }
466
467 static const struct Qdisc_class_ops drr_class_ops = {
468         .change         = drr_change_class,
469         .delete         = drr_delete_class,
470         .get            = drr_get_class,
471         .put            = drr_put_class,
472         .tcf_chain      = drr_tcf_chain,
473         .bind_tcf       = drr_bind_tcf,
474         .unbind_tcf     = drr_unbind_tcf,
475         .graft          = drr_graft_class,
476         .leaf           = drr_class_leaf,
477         .qlen_notify    = drr_qlen_notify,
478         .dump           = drr_dump_class,
479         .dump_stats     = drr_dump_class_stats,
480         .walk           = drr_walk,
481 };
482
483 static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
484         .cl_ops         = &drr_class_ops,
485         .id             = "drr",
486         .priv_size      = sizeof(struct drr_sched),
487         .enqueue        = drr_enqueue,
488         .dequeue        = drr_dequeue,
489         .peek           = qdisc_peek_dequeued,
490         .drop           = drr_drop,
491         .init           = drr_init_qdisc,
492         .reset          = drr_reset_qdisc,
493         .destroy        = drr_destroy_qdisc,
494         .owner          = THIS_MODULE,
495 };
496
497 static int __init drr_init(void)
498 {
499         return register_qdisc(&drr_qdisc_ops);
500 }
501
502 static void __exit drr_exit(void)
503 {
504         unregister_qdisc(&drr_qdisc_ops);
505 }
506
507 module_init(drr_init);
508 module_exit(drr_exit);
509 MODULE_LICENSE("GPL");