Backport FQ_Codel for Linux 2.6.36
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / sched / sch_fq_codel.c
blobdcd3f24fb5f5fd58003b1435c0cd926c940d7935
1 /*
2 * Fair Queue CoDel discipline
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.
9 * Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
16 #include <linux/string.h>
17 #include <linux/in.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/skbuff.h>
21 #include <linux/jhash.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/codel.h>
28 /* Fair Queue CoDel.
30 * Principles :
31 * Packets are classified (internal classifier or external) on flows.
32 * This is a Stochastic model (as we use a hash, several flows
33 * might be hashed on same slot)
34 * Each flow has a CoDel managed queue.
35 * Flows are linked onto two (Round Robin) lists,
36 * so that new flows have priority on old ones.
38 * For a given flow, packets are not reordered (CoDel uses a FIFO)
39 * head drops only.
40 * ECN capability is on by default.
41 * Low memory footprint (64 bytes per flow)
44 struct fq_codel_flow {
45 struct sk_buff *head;
46 struct sk_buff *tail;
47 struct list_head flowchain;
48 int deficit;
49 u32 dropped; /* number of drops (or ECN marks) on this flow */
50 struct codel_vars cvars;
51 }; /* please try to keep this structure <= 64 bytes */
53 struct fq_codel_sched_data {
54 struct tcf_proto __rcu *filter_list; /* optional external classifier */
55 struct fq_codel_flow *flows; /* Flows table [flows_cnt] */
56 u32 *backlogs; /* backlog table [flows_cnt] */
57 u32 flows_cnt; /* number of flows */
58 u32 perturbation; /* hash perturbation */
59 u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
60 struct codel_params cparams;
61 struct codel_stats cstats;
62 u32 drop_overlimit;
63 u32 new_flow_count;
65 struct list_head new_flows; /* list of new flows */
66 struct list_head old_flows; /* list of old flows */
69 static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
70 const struct sk_buff *skb)
72 unsigned int hash;
74 hash = (__force u32)skb_dst(skb);
76 return reciprocal_scale(hash, q->flows_cnt);
79 static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
80 int *qerr)
82 struct fq_codel_sched_data *q = qdisc_priv(sch);
83 struct tcf_proto *filter;
84 struct tcf_result res;
85 int result;
87 if (TC_H_MAJ(skb->priority) == sch->handle &&
88 TC_H_MIN(skb->priority) > 0 &&
89 TC_H_MIN(skb->priority) <= q->flows_cnt)
90 return TC_H_MIN(skb->priority);
92 filter = rcu_dereference_bh(q->filter_list);
93 if (!filter)
94 return fq_codel_hash(q, skb) + 1;
96 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
97 result = tc_classify(skb, filter, &res);
98 if (result >= 0) {
99 #ifdef CONFIG_NET_CLS_ACT
100 switch (result) {
101 case TC_ACT_STOLEN:
102 case TC_ACT_QUEUED:
103 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
104 case TC_ACT_SHOT:
105 return 0;
107 #endif
108 if (TC_H_MIN(res.classid) <= q->flows_cnt)
109 return TC_H_MIN(res.classid);
111 return 0;
114 /* helper functions : might be changed when/if skb use a standard list_head */
116 /* remove one skb from head of slot queue */
117 static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
119 struct sk_buff *skb = flow->head;
121 flow->head = skb->next;
122 skb->next = NULL;
123 return skb;
126 /* add skb to flow queue (tail add) */
127 static inline void flow_queue_add(struct fq_codel_flow *flow,
128 struct sk_buff *skb)
130 if (flow->head == NULL)
131 flow->head = skb;
132 else
133 flow->tail->next = skb;
134 flow->tail = skb;
135 skb->next = NULL;
138 static unsigned int fq_codel_drop(struct Qdisc *sch)
140 struct fq_codel_sched_data *q = qdisc_priv(sch);
141 struct sk_buff *skb;
142 unsigned int maxbacklog = 0, idx = 0, i, len;
143 struct fq_codel_flow *flow;
145 /* Queue is full! Find the fat flow and drop packet from it.
146 * This might sound expensive, but with 1024 flows, we scan
147 * 4KB of memory, and we dont need to handle a complex tree
148 * in fast path (packet queue/enqueue) with many cache misses.
150 for (i = 0; i < q->flows_cnt; i++) {
151 if (q->backlogs[i] > maxbacklog) {
152 maxbacklog = q->backlogs[i];
153 idx = i;
156 flow = &q->flows[idx];
157 skb = dequeue_head(flow);
158 len = qdisc_pkt_len(skb);
159 q->backlogs[idx] -= len;
160 kfree_skb(skb);
161 sch->q.qlen--;
162 qdisc_qstats_drop(sch);
163 qdisc_qstats_backlog_dec(sch, skb);
164 flow->dropped++;
165 return idx;
168 static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch)
170 struct fq_codel_sched_data *q = qdisc_priv(sch);
171 unsigned int idx;
172 struct fq_codel_flow *flow;
173 int uninitialized_var(ret);
175 idx = fq_codel_classify(skb, sch, &ret);
176 if (idx == 0) {
177 if (ret & __NET_XMIT_BYPASS)
178 qdisc_qstats_drop(sch);
179 kfree_skb(skb);
180 return ret;
182 idx--;
184 codel_set_enqueue_time(skb);
185 flow = &q->flows[idx];
186 flow_queue_add(flow, skb);
187 q->backlogs[idx] += qdisc_pkt_len(skb);
188 qdisc_qstats_backlog_inc(sch, skb);
190 if (list_empty(&flow->flowchain)) {
191 list_add_tail(&flow->flowchain, &q->new_flows);
192 q->new_flow_count++;
193 flow->deficit = q->quantum;
194 flow->dropped = 0;
196 if (++sch->q.qlen <= sch->limit)
197 return NET_XMIT_SUCCESS;
199 q->drop_overlimit++;
200 /* Return Congestion Notification only if we dropped a packet
201 * from this flow.
203 if (fq_codel_drop(sch) == idx)
204 return NET_XMIT_CN;
206 /* As we dropped a packet, better let upper stack know this */
207 qdisc_tree_decrease_qlen(sch, 1);
208 return NET_XMIT_SUCCESS;
211 /* This is the specific function called from codel_dequeue()
212 * to dequeue a packet from queue. Note: backlog is handled in
213 * codel, we dont need to reduce it here.
215 static struct sk_buff *dequeue(struct codel_vars *vars, struct Qdisc *sch)
217 struct fq_codel_sched_data *q = qdisc_priv(sch);
218 struct fq_codel_flow *flow;
219 struct sk_buff *skb = NULL;
221 flow = container_of(vars, struct fq_codel_flow, cvars);
222 if (flow->head) {
223 skb = dequeue_head(flow);
224 q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
225 sch->q.qlen--;
227 return skb;
230 static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
232 struct fq_codel_sched_data *q = qdisc_priv(sch);
233 struct sk_buff *skb;
234 struct fq_codel_flow *flow;
235 struct list_head *head;
236 u32 prev_drop_count, prev_ecn_mark;
238 begin:
239 head = &q->new_flows;
240 if (list_empty(head)) {
241 head = &q->old_flows;
242 if (list_empty(head))
243 return NULL;
245 flow = list_first_entry(head, struct fq_codel_flow, flowchain);
247 if (flow->deficit <= 0) {
248 flow->deficit += q->quantum;
249 list_move_tail(&flow->flowchain, &q->old_flows);
250 goto begin;
253 prev_drop_count = q->cstats.drop_count;
254 prev_ecn_mark = q->cstats.ecn_mark;
256 skb = codel_dequeue(sch, &q->cparams, &flow->cvars, &q->cstats,
257 dequeue);
259 flow->dropped += q->cstats.drop_count - prev_drop_count;
260 flow->dropped += q->cstats.ecn_mark - prev_ecn_mark;
262 if (!skb) {
263 /* force a pass through old_flows to prevent starvation */
264 if ((head == &q->new_flows) && !list_empty(&q->old_flows))
265 list_move_tail(&flow->flowchain, &q->old_flows);
266 else
267 list_del_init(&flow->flowchain);
268 goto begin;
270 qdisc_bstats_update(sch, skb);
271 flow->deficit -= qdisc_pkt_len(skb);
272 /* We cant call qdisc_tree_decrease_qlen() if our qlen is 0,
273 * or HTB crashes. Defer it for next round.
275 if (q->cstats.drop_count && sch->q.qlen) {
276 qdisc_tree_decrease_qlen(sch, q->cstats.drop_count);
277 q->cstats.drop_count = 0;
279 return skb;
282 static void fq_codel_reset(struct Qdisc *sch)
284 struct sk_buff *skb;
286 while ((skb = fq_codel_dequeue(sch)) != NULL)
287 kfree_skb(skb);
290 static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
291 [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
292 [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
293 [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
294 [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
295 [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
296 [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
299 static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
301 struct fq_codel_sched_data *q = qdisc_priv(sch);
302 struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
303 int err;
305 if (!opt)
306 return -EINVAL;
308 err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy);
309 if (err < 0)
310 return err;
311 if (tb[TCA_FQ_CODEL_FLOWS]) {
312 if (q->flows)
313 return -EINVAL;
314 q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
315 if (!q->flows_cnt ||
316 q->flows_cnt > 65536)
317 return -EINVAL;
319 sch_tree_lock(sch);
321 if (tb[TCA_FQ_CODEL_TARGET]) {
322 u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
324 q->cparams.target = (target * NSEC_PER_USEC) >> CODEL_SHIFT;
327 if (tb[TCA_FQ_CODEL_INTERVAL]) {
328 u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
330 q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT;
333 if (tb[TCA_FQ_CODEL_LIMIT])
334 sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
336 if (tb[TCA_FQ_CODEL_ECN])
337 q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
339 if (tb[TCA_FQ_CODEL_QUANTUM])
340 q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
342 while (sch->q.qlen > sch->limit) {
343 struct sk_buff *skb = fq_codel_dequeue(sch);
345 kfree_skb(skb);
346 q->cstats.drop_count++;
348 qdisc_tree_decrease_qlen(sch, q->cstats.drop_count);
349 q->cstats.drop_count = 0;
351 sch_tree_unlock(sch);
352 return 0;
355 static void *fq_codel_zalloc(size_t sz)
357 void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
359 if (!ptr)
360 ptr = vzalloc(sz);
361 return ptr;
364 static void fq_codel_free(void *addr)
366 kvfree(addr);
369 static void fq_codel_destroy(struct Qdisc *sch)
371 struct fq_codel_sched_data *q = qdisc_priv(sch);
373 tcf_destroy_chain(&q->filter_list);
374 fq_codel_free(q->backlogs);
375 fq_codel_free(q->flows);
378 static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt)
380 struct fq_codel_sched_data *q = qdisc_priv(sch);
381 int i;
383 sch->limit = 10*1024;
384 q->flows_cnt = 1024;
385 q->quantum = psched_mtu(qdisc_dev(sch));
386 q->perturbation = prandom_u32();
387 INIT_LIST_HEAD(&q->new_flows);
388 INIT_LIST_HEAD(&q->old_flows);
389 codel_params_init(&q->cparams);
390 codel_stats_init(&q->cstats);
391 q->cparams.ecn = true;
393 if (opt) {
394 int err = fq_codel_change(sch, opt);
395 if (err)
396 return err;
399 if (!q->flows) {
400 q->flows = fq_codel_zalloc(q->flows_cnt *
401 sizeof(struct fq_codel_flow));
402 if (!q->flows)
403 return -ENOMEM;
404 q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32));
405 if (!q->backlogs) {
406 fq_codel_free(q->flows);
407 return -ENOMEM;
409 for (i = 0; i < q->flows_cnt; i++) {
410 struct fq_codel_flow *flow = q->flows + i;
412 INIT_LIST_HEAD(&flow->flowchain);
413 codel_vars_init(&flow->cvars);
416 if (sch->limit >= 1)
417 sch->flags |= TCQ_F_CAN_BYPASS;
418 else
419 sch->flags &= ~TCQ_F_CAN_BYPASS;
420 return 0;
423 static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
425 struct fq_codel_sched_data *q = qdisc_priv(sch);
426 struct nlattr *opts;
428 opts = nla_nest_start(skb, TCA_OPTIONS);
429 if (opts == NULL)
430 goto nla_put_failure;
432 if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
433 codel_time_to_us(q->cparams.target)) ||
434 nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
435 sch->limit) ||
436 nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
437 codel_time_to_us(q->cparams.interval)) ||
438 nla_put_u32(skb, TCA_FQ_CODEL_ECN,
439 q->cparams.ecn) ||
440 nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
441 q->quantum) ||
442 nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
443 q->flows_cnt))
444 goto nla_put_failure;
446 return nla_nest_end(skb, opts);
448 nla_put_failure:
449 return -1;
452 static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
454 struct fq_codel_sched_data *q = qdisc_priv(sch);
455 struct tc_fq_codel_xstats st = {
456 .type = TCA_FQ_CODEL_XSTATS_QDISC,
458 struct list_head *pos;
460 st.qdisc_stats.maxpacket = q->cstats.maxpacket;
461 st.qdisc_stats.drop_overlimit = q->drop_overlimit;
462 st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
463 st.qdisc_stats.new_flow_count = q->new_flow_count;
465 list_for_each(pos, &q->new_flows)
466 st.qdisc_stats.new_flows_len++;
468 list_for_each(pos, &q->old_flows)
469 st.qdisc_stats.old_flows_len++;
471 return gnet_stats_copy_app(d, &st, sizeof(st));
474 static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
476 return NULL;
479 static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid)
481 return 0;
484 static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
485 u32 classid)
487 /* we cannot bypass queue discipline anymore */
488 sch->flags &= ~TCQ_F_CAN_BYPASS;
489 return 0;
492 static void fq_codel_put(struct Qdisc *q, unsigned long cl)
496 static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch,
497 unsigned long cl)
499 struct fq_codel_sched_data *q = qdisc_priv(sch);
501 if (cl)
502 return NULL;
503 return &q->filter_list;
506 static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
507 struct sk_buff *skb, struct tcmsg *tcm)
509 tcm->tcm_handle |= TC_H_MIN(cl);
510 return 0;
513 static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
514 struct gnet_dump *d)
516 struct fq_codel_sched_data *q = qdisc_priv(sch);
517 u32 idx = cl - 1;
518 struct gnet_stats_queue qs = { 0 };
519 struct tc_fq_codel_xstats xstats;
521 if (idx < q->flows_cnt) {
522 const struct fq_codel_flow *flow = &q->flows[idx];
523 const struct sk_buff *skb = flow->head;
525 memset(&xstats, 0, sizeof(xstats));
526 xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
527 xstats.class_stats.deficit = flow->deficit;
528 xstats.class_stats.ldelay =
529 codel_time_to_us(flow->cvars.ldelay);
530 xstats.class_stats.count = flow->cvars.count;
531 xstats.class_stats.lastcount = flow->cvars.lastcount;
532 xstats.class_stats.dropping = flow->cvars.dropping;
533 if (flow->cvars.dropping) {
534 codel_tdiff_t delta = flow->cvars.drop_next -
535 codel_get_time();
537 xstats.class_stats.drop_next = (delta >= 0) ?
538 codel_time_to_us(delta) :
539 -codel_time_to_us(-delta);
541 while (skb) {
542 qs.qlen++;
543 skb = skb->next;
545 qs.backlog = q->backlogs[idx];
546 qs.drops = flow->dropped;
548 if (gnet_stats_copy_queue(d, &qs) < 0)
549 return -1;
550 if (idx < q->flows_cnt)
551 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
552 return 0;
555 static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
557 struct fq_codel_sched_data *q = qdisc_priv(sch);
558 unsigned int i;
560 if (arg->stop)
561 return;
563 for (i = 0; i < q->flows_cnt; i++) {
564 if (list_empty(&q->flows[i].flowchain) ||
565 arg->count < arg->skip) {
566 arg->count++;
567 continue;
569 if (arg->fn(sch, i + 1, arg) < 0) {
570 arg->stop = 1;
571 break;
573 arg->count++;
577 static const struct Qdisc_class_ops fq_codel_class_ops = {
578 .leaf = fq_codel_leaf,
579 .get = fq_codel_get,
580 .put = fq_codel_put,
581 .tcf_chain = fq_codel_find_tcf,
582 .bind_tcf = fq_codel_bind,
583 .unbind_tcf = fq_codel_put,
584 .dump = fq_codel_dump_class,
585 .dump_stats = fq_codel_dump_class_stats,
586 .walk = fq_codel_walk,
589 static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
590 .cl_ops = &fq_codel_class_ops,
591 .id = "fq_codel",
592 .priv_size = sizeof(struct fq_codel_sched_data),
593 .enqueue = fq_codel_enqueue,
594 .dequeue = fq_codel_dequeue,
595 .peek = qdisc_peek_dequeued,
596 .drop = fq_codel_drop,
597 .init = fq_codel_init,
598 .reset = fq_codel_reset,
599 .destroy = fq_codel_destroy,
600 .change = fq_codel_change,
601 .dump = fq_codel_dump,
602 .dump_stats = fq_codel_dump_stats,
603 .owner = THIS_MODULE,
606 static int __init fq_codel_module_init(void)
608 return register_qdisc(&fq_codel_qdisc_ops);
611 static void __exit fq_codel_module_exit(void)
613 unregister_qdisc(&fq_codel_qdisc_ops);
616 module_init(fq_codel_module_init)
617 module_exit(fq_codel_module_exit)
618 MODULE_AUTHOR("Eric Dumazet");
619 MODULE_LICENSE("GPL");