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>
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>
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)
40 * ECN capability is on by default.
41 * Low memory footprint (64 bytes per flow)
44 struct fq_codel_flow
{
47 struct list_head flowchain
;
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
;
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
)
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
,
82 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
83 struct tcf_proto
*filter
;
84 struct tcf_result res
;
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
);
94 return fq_codel_hash(q
, skb
) + 1;
96 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_BYPASS
;
97 result
= tc_classify(skb
, filter
, &res
);
99 #ifdef CONFIG_NET_CLS_ACT
103 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_STOLEN
;
108 if (TC_H_MIN(res
.classid
) <= q
->flows_cnt
)
109 return TC_H_MIN(res
.classid
);
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
;
126 /* add skb to flow queue (tail add) */
127 static inline void flow_queue_add(struct fq_codel_flow
*flow
,
130 if (flow
->head
== NULL
)
133 flow
->tail
->next
= skb
;
138 static unsigned int fq_codel_drop(struct Qdisc
*sch
)
140 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
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
];
156 flow
= &q
->flows
[idx
];
157 skb
= dequeue_head(flow
);
158 len
= qdisc_pkt_len(skb
);
159 q
->backlogs
[idx
] -= len
;
162 qdisc_qstats_drop(sch
);
163 qdisc_qstats_backlog_dec(sch
, skb
);
168 static int fq_codel_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
170 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
172 struct fq_codel_flow
*flow
;
173 int uninitialized_var(ret
);
175 idx
= fq_codel_classify(skb
, sch
, &ret
);
177 if (ret
& __NET_XMIT_BYPASS
)
178 qdisc_qstats_drop(sch
);
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
);
193 flow
->deficit
= q
->quantum
;
196 if (++sch
->q
.qlen
<= sch
->limit
)
197 return NET_XMIT_SUCCESS
;
200 /* Return Congestion Notification only if we dropped a packet
203 if (fq_codel_drop(sch
) == idx
)
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
);
223 skb
= dequeue_head(flow
);
224 q
->backlogs
[flow
- q
->flows
] -= qdisc_pkt_len(skb
);
230 static struct sk_buff
*fq_codel_dequeue(struct Qdisc
*sch
)
232 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
234 struct fq_codel_flow
*flow
;
235 struct list_head
*head
;
236 u32 prev_drop_count
, prev_ecn_mark
;
239 head
= &q
->new_flows
;
240 if (list_empty(head
)) {
241 head
= &q
->old_flows
;
242 if (list_empty(head
))
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
);
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
,
259 flow
->dropped
+= q
->cstats
.drop_count
- prev_drop_count
;
260 flow
->dropped
+= q
->cstats
.ecn_mark
- prev_ecn_mark
;
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
);
267 list_del_init(&flow
->flowchain
);
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;
282 static void fq_codel_reset(struct Qdisc
*sch
)
286 while ((skb
= fq_codel_dequeue(sch
)) != NULL
)
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];
308 err
= nla_parse_nested(tb
, TCA_FQ_CODEL_MAX
, opt
, fq_codel_policy
);
311 if (tb
[TCA_FQ_CODEL_FLOWS
]) {
314 q
->flows_cnt
= nla_get_u32(tb
[TCA_FQ_CODEL_FLOWS
]);
316 q
->flows_cnt
> 65536)
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
);
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
);
355 static void *fq_codel_zalloc(size_t sz
)
357 void *ptr
= kzalloc(sz
, GFP_KERNEL
| __GFP_NOWARN
);
364 static void fq_codel_free(void *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
);
383 sch
->limit
= 10*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;
394 int err
= fq_codel_change(sch
, opt
);
400 q
->flows
= fq_codel_zalloc(q
->flows_cnt
*
401 sizeof(struct fq_codel_flow
));
404 q
->backlogs
= fq_codel_zalloc(q
->flows_cnt
* sizeof(u32
));
406 fq_codel_free(q
->flows
);
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
);
417 sch
->flags
|= TCQ_F_CAN_BYPASS
;
419 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
423 static int fq_codel_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
425 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
428 opts
= nla_nest_start(skb
, TCA_OPTIONS
);
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
,
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
,
440 nla_put_u32(skb
, TCA_FQ_CODEL_QUANTUM
,
442 nla_put_u32(skb
, TCA_FQ_CODEL_FLOWS
,
444 goto nla_put_failure
;
446 return nla_nest_end(skb
, opts
);
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
)
479 static unsigned long fq_codel_get(struct Qdisc
*sch
, u32 classid
)
484 static unsigned long fq_codel_bind(struct Qdisc
*sch
, unsigned long parent
,
487 /* we cannot bypass queue discipline anymore */
488 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
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
,
499 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
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
);
513 static int fq_codel_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
516 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
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
-
537 xstats
.class_stats
.drop_next
= (delta
>= 0) ?
538 codel_time_to_us(delta
) :
539 -codel_time_to_us(-delta
);
545 qs
.backlog
= q
->backlogs
[idx
];
546 qs
.drops
= flow
->dropped
;
548 if (gnet_stats_copy_queue(d
, &qs
) < 0)
550 if (idx
< q
->flows_cnt
)
551 return gnet_stats_copy_app(d
, &xstats
, sizeof(xstats
));
555 static void fq_codel_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
557 struct fq_codel_sched_data
*q
= qdisc_priv(sch
);
563 for (i
= 0; i
< q
->flows_cnt
; i
++) {
564 if (list_empty(&q
->flows
[i
].flowchain
) ||
565 arg
->count
< arg
->skip
) {
569 if (arg
->fn(sch
, i
+ 1, arg
) < 0) {
577 static const struct Qdisc_class_ops fq_codel_class_ops
= {
578 .leaf
= fq_codel_leaf
,
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
,
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");