Merge branches 'tracing/profiling', 'tracing/options' and 'tracing/urgent' into traci...
[linux-2.6] / net / sched / sch_multiq.c
1 /*
2  * Copyright (c) 2008, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
18  */
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/skbuff.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
28
29
30 struct multiq_sched_data {
31         u16 bands;
32         u16 max_bands;
33         u16 curband;
34         struct tcf_proto *filter_list;
35         struct Qdisc **queues;
36 };
37
38
39 static struct Qdisc *
40 multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
41 {
42         struct multiq_sched_data *q = qdisc_priv(sch);
43         u32 band;
44         struct tcf_result res;
45         int err;
46
47         *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
48         err = tc_classify(skb, q->filter_list, &res);
49 #ifdef CONFIG_NET_CLS_ACT
50         switch (err) {
51         case TC_ACT_STOLEN:
52         case TC_ACT_QUEUED:
53                 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
54         case TC_ACT_SHOT:
55                 return NULL;
56         }
57 #endif
58         band = skb_get_queue_mapping(skb);
59
60         if (band >= q->bands)
61                 return q->queues[0];
62
63         return q->queues[band];
64 }
65
66 static int
67 multiq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
68 {
69         struct Qdisc *qdisc;
70         int ret;
71
72         qdisc = multiq_classify(skb, sch, &ret);
73 #ifdef CONFIG_NET_CLS_ACT
74         if (qdisc == NULL) {
75
76                 if (ret & __NET_XMIT_BYPASS)
77                         sch->qstats.drops++;
78                 kfree_skb(skb);
79                 return ret;
80         }
81 #endif
82
83         ret = qdisc_enqueue(skb, qdisc);
84         if (ret == NET_XMIT_SUCCESS) {
85                 sch->bstats.bytes += qdisc_pkt_len(skb);
86                 sch->bstats.packets++;
87                 sch->q.qlen++;
88                 return NET_XMIT_SUCCESS;
89         }
90         if (net_xmit_drop_count(ret))
91                 sch->qstats.drops++;
92         return ret;
93 }
94
95
96 static int
97 multiq_requeue(struct sk_buff *skb, struct Qdisc *sch)
98 {
99         struct Qdisc *qdisc;
100         struct multiq_sched_data *q = qdisc_priv(sch);
101         int ret;
102
103         qdisc = multiq_classify(skb, sch, &ret);
104 #ifdef CONFIG_NET_CLS_ACT
105         if (qdisc == NULL) {
106                 if (ret & __NET_XMIT_BYPASS)
107                         sch->qstats.drops++;
108                 kfree_skb(skb);
109                 return ret;
110         }
111 #endif
112
113         ret = qdisc->ops->requeue(skb, qdisc);
114         if (ret == NET_XMIT_SUCCESS) {
115                 sch->q.qlen++;
116                 sch->qstats.requeues++;
117                 if (q->curband)
118                         q->curband--;
119                 else
120                         q->curband = q->bands - 1;
121                 return NET_XMIT_SUCCESS;
122         }
123         if (net_xmit_drop_count(ret))
124                 sch->qstats.drops++;
125         return ret;
126 }
127
128
129 static struct sk_buff *multiq_dequeue(struct Qdisc *sch)
130 {
131         struct multiq_sched_data *q = qdisc_priv(sch);
132         struct Qdisc *qdisc;
133         struct sk_buff *skb;
134         int band;
135
136         for (band = 0; band < q->bands; band++) {
137                 /* cycle through bands to ensure fairness */
138                 q->curband++;
139                 if (q->curband >= q->bands)
140                         q->curband = 0;
141
142                 /* Check that target subqueue is available before
143                  * pulling an skb to avoid excessive requeues
144                  */
145                 if (!__netif_subqueue_stopped(qdisc_dev(sch), q->curband)) {
146                         qdisc = q->queues[q->curband];
147                         skb = qdisc->dequeue(qdisc);
148                         if (skb) {
149                                 sch->q.qlen--;
150                                 return skb;
151                         }
152                 }
153         }
154         return NULL;
155
156 }
157
158 static unsigned int multiq_drop(struct Qdisc *sch)
159 {
160         struct multiq_sched_data *q = qdisc_priv(sch);
161         int band;
162         unsigned int len;
163         struct Qdisc *qdisc;
164
165         for (band = q->bands-1; band >= 0; band--) {
166                 qdisc = q->queues[band];
167                 if (qdisc->ops->drop) {
168                         len = qdisc->ops->drop(qdisc);
169                         if (len != 0) {
170                                 sch->q.qlen--;
171                                 return len;
172                         }
173                 }
174         }
175         return 0;
176 }
177
178
179 static void
180 multiq_reset(struct Qdisc *sch)
181 {
182         u16 band;
183         struct multiq_sched_data *q = qdisc_priv(sch);
184
185         for (band = 0; band < q->bands; band++)
186                 qdisc_reset(q->queues[band]);
187         sch->q.qlen = 0;
188         q->curband = 0;
189 }
190
191 static void
192 multiq_destroy(struct Qdisc *sch)
193 {
194         int band;
195         struct multiq_sched_data *q = qdisc_priv(sch);
196
197         tcf_destroy_chain(&q->filter_list);
198         for (band = 0; band < q->bands; band++)
199                 qdisc_destroy(q->queues[band]);
200
201         kfree(q->queues);
202 }
203
204 static int multiq_tune(struct Qdisc *sch, struct nlattr *opt)
205 {
206         struct multiq_sched_data *q = qdisc_priv(sch);
207         struct tc_multiq_qopt *qopt;
208         int i;
209
210         if (!netif_is_multiqueue(qdisc_dev(sch)))
211                 return -EINVAL;
212         if (nla_len(opt) < sizeof(*qopt))
213                 return -EINVAL;
214
215         qopt = nla_data(opt);
216
217         qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
218
219         sch_tree_lock(sch);
220         q->bands = qopt->bands;
221         for (i = q->bands; i < q->max_bands; i++) {
222                 if (q->queues[i] != &noop_qdisc) {
223                         struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
224                         qdisc_tree_decrease_qlen(child, child->q.qlen);
225                         qdisc_destroy(child);
226                 }
227         }
228
229         sch_tree_unlock(sch);
230
231         for (i = 0; i < q->bands; i++) {
232                 if (q->queues[i] == &noop_qdisc) {
233                         struct Qdisc *child;
234                         child = qdisc_create_dflt(qdisc_dev(sch),
235                                                   sch->dev_queue,
236                                                   &pfifo_qdisc_ops,
237                                                   TC_H_MAKE(sch->handle,
238                                                             i + 1));
239                         if (child) {
240                                 sch_tree_lock(sch);
241                                 child = xchg(&q->queues[i], child);
242
243                                 if (child != &noop_qdisc) {
244                                         qdisc_tree_decrease_qlen(child,
245                                                                  child->q.qlen);
246                                         qdisc_destroy(child);
247                                 }
248                                 sch_tree_unlock(sch);
249                         }
250                 }
251         }
252         return 0;
253 }
254
255 static int multiq_init(struct Qdisc *sch, struct nlattr *opt)
256 {
257         struct multiq_sched_data *q = qdisc_priv(sch);
258         int i, err;
259
260         q->queues = NULL;
261
262         if (opt == NULL)
263                 return -EINVAL;
264
265         q->max_bands = qdisc_dev(sch)->num_tx_queues;
266
267         q->queues = kcalloc(q->max_bands, sizeof(struct Qdisc *), GFP_KERNEL);
268         if (!q->queues)
269                 return -ENOBUFS;
270         for (i = 0; i < q->max_bands; i++)
271                 q->queues[i] = &noop_qdisc;
272
273         err = multiq_tune(sch,opt);
274
275         if (err)
276                 kfree(q->queues);
277
278         return err;
279 }
280
281 static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
282 {
283         struct multiq_sched_data *q = qdisc_priv(sch);
284         unsigned char *b = skb_tail_pointer(skb);
285         struct tc_multiq_qopt opt;
286
287         opt.bands = q->bands;
288         opt.max_bands = q->max_bands;
289
290         NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
291
292         return skb->len;
293
294 nla_put_failure:
295         nlmsg_trim(skb, b);
296         return -1;
297 }
298
299 static int multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
300                       struct Qdisc **old)
301 {
302         struct multiq_sched_data *q = qdisc_priv(sch);
303         unsigned long band = arg - 1;
304
305         if (band >= q->bands)
306                 return -EINVAL;
307
308         if (new == NULL)
309                 new = &noop_qdisc;
310
311         sch_tree_lock(sch);
312         *old = q->queues[band];
313         q->queues[band] = new;
314         qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
315         qdisc_reset(*old);
316         sch_tree_unlock(sch);
317
318         return 0;
319 }
320
321 static struct Qdisc *
322 multiq_leaf(struct Qdisc *sch, unsigned long arg)
323 {
324         struct multiq_sched_data *q = qdisc_priv(sch);
325         unsigned long band = arg - 1;
326
327         if (band >= q->bands)
328                 return NULL;
329
330         return q->queues[band];
331 }
332
333 static unsigned long multiq_get(struct Qdisc *sch, u32 classid)
334 {
335         struct multiq_sched_data *q = qdisc_priv(sch);
336         unsigned long band = TC_H_MIN(classid);
337
338         if (band - 1 >= q->bands)
339                 return 0;
340         return band;
341 }
342
343 static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
344                                  u32 classid)
345 {
346         return multiq_get(sch, classid);
347 }
348
349
350 static void multiq_put(struct Qdisc *q, unsigned long cl)
351 {
352         return;
353 }
354
355 static int multiq_change(struct Qdisc *sch, u32 handle, u32 parent,
356                          struct nlattr **tca, unsigned long *arg)
357 {
358         unsigned long cl = *arg;
359         struct multiq_sched_data *q = qdisc_priv(sch);
360
361         if (cl - 1 > q->bands)
362                 return -ENOENT;
363         return 0;
364 }
365
366 static int multiq_delete(struct Qdisc *sch, unsigned long cl)
367 {
368         struct multiq_sched_data *q = qdisc_priv(sch);
369         if (cl - 1 > q->bands)
370                 return -ENOENT;
371         return 0;
372 }
373
374
375 static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
376                              struct sk_buff *skb, struct tcmsg *tcm)
377 {
378         struct multiq_sched_data *q = qdisc_priv(sch);
379
380         if (cl - 1 > q->bands)
381                 return -ENOENT;
382         tcm->tcm_handle |= TC_H_MIN(cl);
383         if (q->queues[cl-1])
384                 tcm->tcm_info = q->queues[cl-1]->handle;
385         return 0;
386 }
387
388 static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
389                                  struct gnet_dump *d)
390 {
391         struct multiq_sched_data *q = qdisc_priv(sch);
392         struct Qdisc *cl_q;
393
394         cl_q = q->queues[cl - 1];
395         if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
396             gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
397                 return -1;
398
399         return 0;
400 }
401
402 static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
403 {
404         struct multiq_sched_data *q = qdisc_priv(sch);
405         int band;
406
407         if (arg->stop)
408                 return;
409
410         for (band = 0; band < q->bands; band++) {
411                 if (arg->count < arg->skip) {
412                         arg->count++;
413                         continue;
414                 }
415                 if (arg->fn(sch, band+1, arg) < 0) {
416                         arg->stop = 1;
417                         break;
418                 }
419                 arg->count++;
420         }
421 }
422
423 static struct tcf_proto **multiq_find_tcf(struct Qdisc *sch, unsigned long cl)
424 {
425         struct multiq_sched_data *q = qdisc_priv(sch);
426
427         if (cl)
428                 return NULL;
429         return &q->filter_list;
430 }
431
432 static const struct Qdisc_class_ops multiq_class_ops = {
433         .graft          =       multiq_graft,
434         .leaf           =       multiq_leaf,
435         .get            =       multiq_get,
436         .put            =       multiq_put,
437         .change         =       multiq_change,
438         .delete         =       multiq_delete,
439         .walk           =       multiq_walk,
440         .tcf_chain      =       multiq_find_tcf,
441         .bind_tcf       =       multiq_bind,
442         .unbind_tcf     =       multiq_put,
443         .dump           =       multiq_dump_class,
444         .dump_stats     =       multiq_dump_class_stats,
445 };
446
447 static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
448         .next           =       NULL,
449         .cl_ops         =       &multiq_class_ops,
450         .id             =       "multiq",
451         .priv_size      =       sizeof(struct multiq_sched_data),
452         .enqueue        =       multiq_enqueue,
453         .dequeue        =       multiq_dequeue,
454         .requeue        =       multiq_requeue,
455         .drop           =       multiq_drop,
456         .init           =       multiq_init,
457         .reset          =       multiq_reset,
458         .destroy        =       multiq_destroy,
459         .change         =       multiq_tune,
460         .dump           =       multiq_dump,
461         .owner          =       THIS_MODULE,
462 };
463
464 static int __init multiq_module_init(void)
465 {
466         return register_qdisc(&multiq_qdisc_ops);
467 }
468
469 static void __exit multiq_module_exit(void)
470 {
471         unregister_qdisc(&multiq_qdisc_ops);
472 }
473
474 module_init(multiq_module_init)
475 module_exit(multiq_module_exit)
476
477 MODULE_LICENSE("GPL");