blob: a5e420b3d4abfb841f2533e2d8f1bedf2aa1c632 [file] [log] [blame]
Eric Dumazet4b549a22012-05-11 09:30:50 +00001/*
2 * Fair Queue CoDel discipline
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 *
Eric Dumazet80ba92f2015-05-08 15:05:12 -07009 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
Eric Dumazet4b549a22012-05-11 09:30:50 +000010 */
11
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>
Eric Dumazet4b549a22012-05-11 09:30:50 +000026#include <net/codel.h>
Michal Kaziord068ca22016-04-22 14:15:59 +020027#include <net/codel_impl.h>
28#include <net/codel_qdisc.h>
Eric Dumazet4b549a22012-05-11 09:30:50 +000029
30/* Fair Queue CoDel.
31 *
32 * Principles :
33 * Packets are classified (internal classifier or external) on flows.
34 * This is a Stochastic model (as we use a hash, several flows
35 * might be hashed on same slot)
36 * Each flow has a CoDel managed queue.
37 * Flows are linked onto two (Round Robin) lists,
38 * so that new flows have priority on old ones.
39 *
40 * For a given flow, packets are not reordered (CoDel uses a FIFO)
41 * head drops only.
42 * ECN capability is on by default.
43 * Low memory footprint (64 bytes per flow)
44 */
45
46struct fq_codel_flow {
47 struct sk_buff *head;
48 struct sk_buff *tail;
49 struct list_head flowchain;
50 int deficit;
51 u32 dropped; /* number of drops (or ECN marks) on this flow */
52 struct codel_vars cvars;
53}; /* please try to keep this structure <= 64 bytes */
54
55struct fq_codel_sched_data {
John Fastabend25d8c0d2014-09-12 20:05:27 -070056 struct tcf_proto __rcu *filter_list; /* optional external classifier */
Eric Dumazet4b549a22012-05-11 09:30:50 +000057 struct fq_codel_flow *flows; /* Flows table [flows_cnt] */
58 u32 *backlogs; /* backlog table [flows_cnt] */
59 u32 flows_cnt; /* number of flows */
60 u32 perturbation; /* hash perturbation */
61 u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
62 struct codel_params cparams;
63 struct codel_stats cstats;
64 u32 drop_overlimit;
65 u32 new_flow_count;
66
67 struct list_head new_flows; /* list of new flows */
68 struct list_head old_flows; /* list of old flows */
69};
70
71static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
Tom Herbert342db222015-05-01 11:30:13 -070072 struct sk_buff *skb)
Eric Dumazet4b549a22012-05-11 09:30:50 +000073{
Tom Herbert342db222015-05-01 11:30:13 -070074 u32 hash = skb_get_hash_perturb(skb, q->perturbation);
Daniel Borkmann8fc54f62014-08-23 20:58:54 +020075
76 return reciprocal_scale(hash, q->flows_cnt);
Eric Dumazet4b549a22012-05-11 09:30:50 +000077}
78
79static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
80 int *qerr)
81{
82 struct fq_codel_sched_data *q = qdisc_priv(sch);
John Fastabend25d8c0d2014-09-12 20:05:27 -070083 struct tcf_proto *filter;
Eric Dumazet4b549a22012-05-11 09:30:50 +000084 struct tcf_result res;
85 int result;
86
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);
91
Valdis.Kletnieks@vt.edu69204cf2014-12-09 16:15:50 -050092 filter = rcu_dereference_bh(q->filter_list);
John Fastabend25d8c0d2014-09-12 20:05:27 -070093 if (!filter)
Eric Dumazet4b549a22012-05-11 09:30:50 +000094 return fq_codel_hash(q, skb) + 1;
95
96 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Daniel Borkmann3b3ae882015-08-26 23:00:06 +020097 result = tc_classify(skb, filter, &res, false);
Eric Dumazet4b549a22012-05-11 09:30:50 +000098 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;
106 }
107#endif
108 if (TC_H_MIN(res.classid) <= q->flows_cnt)
109 return TC_H_MIN(res.classid);
110 }
111 return 0;
112}
113
114/* helper functions : might be changed when/if skb use a standard list_head */
115
116/* remove one skb from head of slot queue */
117static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
118{
119 struct sk_buff *skb = flow->head;
120
121 flow->head = skb->next;
122 skb->next = NULL;
123 return skb;
124}
125
126/* add skb to flow queue (tail add) */
127static inline void flow_queue_add(struct fq_codel_flow *flow,
128 struct sk_buff *skb)
129{
130 if (flow->head == NULL)
131 flow->head = skb;
132 else
133 flow->tail->next = skb;
134 flow->tail = skb;
135 skb->next = NULL;
136}
137
138static unsigned int fq_codel_drop(struct Qdisc *sch)
139{
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;
144
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.
149 */
150 for (i = 0; i < q->flows_cnt; i++) {
151 if (q->backlogs[i] > maxbacklog) {
152 maxbacklog = q->backlogs[i];
153 idx = i;
154 }
155 }
156 flow = &q->flows[idx];
157 skb = dequeue_head(flow);
158 len = qdisc_pkt_len(skb);
159 q->backlogs[idx] -= len;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000160 sch->q.qlen--;
John Fastabend25331d62014-09-28 11:53:29 -0700161 qdisc_qstats_drop(sch);
162 qdisc_qstats_backlog_dec(sch, skb);
WANG Cong052cbda2015-07-13 12:30:07 -0700163 kfree_skb(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000164 flow->dropped++;
165 return idx;
166}
167
WANG Congc0afd9c2015-07-14 11:21:58 -0700168static unsigned int fq_codel_qdisc_drop(struct Qdisc *sch)
169{
170 unsigned int prev_backlog;
171
172 prev_backlog = sch->qstats.backlog;
173 fq_codel_drop(sch);
174 return prev_backlog - sch->qstats.backlog;
175}
176
Eric Dumazet4b549a22012-05-11 09:30:50 +0000177static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch)
178{
179 struct fq_codel_sched_data *q = qdisc_priv(sch);
WANG Cong2ccccf52016-02-25 14:55:01 -0800180 unsigned int idx, prev_backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000181 struct fq_codel_flow *flow;
182 int uninitialized_var(ret);
183
184 idx = fq_codel_classify(skb, sch, &ret);
185 if (idx == 0) {
186 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -0700187 qdisc_qstats_drop(sch);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000188 kfree_skb(skb);
189 return ret;
190 }
191 idx--;
192
193 codel_set_enqueue_time(skb);
194 flow = &q->flows[idx];
195 flow_queue_add(flow, skb);
196 q->backlogs[idx] += qdisc_pkt_len(skb);
John Fastabend25331d62014-09-28 11:53:29 -0700197 qdisc_qstats_backlog_inc(sch, skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000198
199 if (list_empty(&flow->flowchain)) {
200 list_add_tail(&flow->flowchain, &q->new_flows);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000201 q->new_flow_count++;
202 flow->deficit = q->quantum;
203 flow->dropped = 0;
204 }
Vijay Subramaniancd68ddd2013-03-28 13:52:00 +0000205 if (++sch->q.qlen <= sch->limit)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000206 return NET_XMIT_SUCCESS;
207
WANG Cong2ccccf52016-02-25 14:55:01 -0800208 prev_backlog = sch->qstats.backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000209 q->drop_overlimit++;
210 /* Return Congestion Notification only if we dropped a packet
211 * from this flow.
212 */
213 if (fq_codel_drop(sch) == idx)
214 return NET_XMIT_CN;
215
216 /* As we dropped a packet, better let upper stack know this */
WANG Cong2ccccf52016-02-25 14:55:01 -0800217 qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000218 return NET_XMIT_SUCCESS;
219}
220
221/* This is the specific function called from codel_dequeue()
222 * to dequeue a packet from queue. Note: backlog is handled in
223 * codel, we dont need to reduce it here.
224 */
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200225static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000226{
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200227 struct Qdisc *sch = ctx;
Eric Dumazet865ec552012-05-16 04:39:09 +0000228 struct fq_codel_sched_data *q = qdisc_priv(sch);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000229 struct fq_codel_flow *flow;
230 struct sk_buff *skb = NULL;
231
232 flow = container_of(vars, struct fq_codel_flow, cvars);
233 if (flow->head) {
234 skb = dequeue_head(flow);
Eric Dumazet865ec552012-05-16 04:39:09 +0000235 q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000236 sch->q.qlen--;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200237 sch->qstats.backlog -= qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000238 }
239 return skb;
240}
241
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200242static void drop_func(struct sk_buff *skb, void *ctx)
243{
244 struct Qdisc *sch = ctx;
245
246 qdisc_drop(skb, sch);
247}
248
Eric Dumazet4b549a22012-05-11 09:30:50 +0000249static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
250{
251 struct fq_codel_sched_data *q = qdisc_priv(sch);
252 struct sk_buff *skb;
253 struct fq_codel_flow *flow;
254 struct list_head *head;
255 u32 prev_drop_count, prev_ecn_mark;
WANG Cong2ccccf52016-02-25 14:55:01 -0800256 unsigned int prev_backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000257
258begin:
259 head = &q->new_flows;
260 if (list_empty(head)) {
261 head = &q->old_flows;
262 if (list_empty(head))
263 return NULL;
264 }
265 flow = list_first_entry(head, struct fq_codel_flow, flowchain);
266
267 if (flow->deficit <= 0) {
268 flow->deficit += q->quantum;
269 list_move_tail(&flow->flowchain, &q->old_flows);
270 goto begin;
271 }
272
273 prev_drop_count = q->cstats.drop_count;
274 prev_ecn_mark = q->cstats.ecn_mark;
WANG Cong2ccccf52016-02-25 14:55:01 -0800275 prev_backlog = sch->qstats.backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000276
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200277 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams,
278 &flow->cvars, &q->cstats, qdisc_pkt_len,
279 codel_get_enqueue_time, drop_func, dequeue_func);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000280
281 flow->dropped += q->cstats.drop_count - prev_drop_count;
282 flow->dropped += q->cstats.ecn_mark - prev_ecn_mark;
283
284 if (!skb) {
285 /* force a pass through old_flows to prevent starvation */
286 if ((head == &q->new_flows) && !list_empty(&q->old_flows))
287 list_move_tail(&flow->flowchain, &q->old_flows);
288 else
289 list_del_init(&flow->flowchain);
290 goto begin;
291 }
292 qdisc_bstats_update(sch, skb);
293 flow->deficit -= qdisc_pkt_len(skb);
WANG Cong2ccccf52016-02-25 14:55:01 -0800294 /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000295 * or HTB crashes. Defer it for next round.
296 */
297 if (q->cstats.drop_count && sch->q.qlen) {
WANG Cong2ccccf52016-02-25 14:55:01 -0800298 qdisc_tree_reduce_backlog(sch, q->cstats.drop_count,
299 q->cstats.drop_len);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000300 q->cstats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800301 q->cstats.drop_len = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000302 }
303 return skb;
304}
305
306static void fq_codel_reset(struct Qdisc *sch)
307{
Eric Dumazet3d0e0af2015-07-31 17:53:39 -0700308 struct fq_codel_sched_data *q = qdisc_priv(sch);
309 int i;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000310
Eric Dumazet3d0e0af2015-07-31 17:53:39 -0700311 INIT_LIST_HEAD(&q->new_flows);
312 INIT_LIST_HEAD(&q->old_flows);
313 for (i = 0; i < q->flows_cnt; i++) {
314 struct fq_codel_flow *flow = q->flows + i;
315
316 while (flow->head) {
317 struct sk_buff *skb = dequeue_head(flow);
318
319 qdisc_qstats_backlog_dec(sch, skb);
320 kfree_skb(skb);
321 }
322
323 INIT_LIST_HEAD(&flow->flowchain);
324 codel_vars_init(&flow->cvars);
325 }
326 memset(q->backlogs, 0, q->flows_cnt * sizeof(u32));
327 sch->q.qlen = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000328}
329
330static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
331 [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
332 [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
333 [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
334 [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
335 [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
336 [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700337 [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
Eric Dumazet4b549a22012-05-11 09:30:50 +0000338};
339
340static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
341{
342 struct fq_codel_sched_data *q = qdisc_priv(sch);
343 struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
344 int err;
345
346 if (!opt)
347 return -EINVAL;
348
349 err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy);
350 if (err < 0)
351 return err;
352 if (tb[TCA_FQ_CODEL_FLOWS]) {
353 if (q->flows)
354 return -EINVAL;
355 q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
356 if (!q->flows_cnt ||
357 q->flows_cnt > 65536)
358 return -EINVAL;
359 }
360 sch_tree_lock(sch);
361
362 if (tb[TCA_FQ_CODEL_TARGET]) {
363 u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
364
365 q->cparams.target = (target * NSEC_PER_USEC) >> CODEL_SHIFT;
366 }
367
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700368 if (tb[TCA_FQ_CODEL_CE_THRESHOLD]) {
369 u64 val = nla_get_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]);
370
371 q->cparams.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
372 }
373
Eric Dumazet4b549a22012-05-11 09:30:50 +0000374 if (tb[TCA_FQ_CODEL_INTERVAL]) {
375 u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
376
377 q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT;
378 }
379
380 if (tb[TCA_FQ_CODEL_LIMIT])
381 sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
382
383 if (tb[TCA_FQ_CODEL_ECN])
384 q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
385
386 if (tb[TCA_FQ_CODEL_QUANTUM])
387 q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
388
389 while (sch->q.qlen > sch->limit) {
390 struct sk_buff *skb = fq_codel_dequeue(sch);
391
WANG Cong2ccccf52016-02-25 14:55:01 -0800392 q->cstats.drop_len += qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000393 kfree_skb(skb);
394 q->cstats.drop_count++;
395 }
WANG Cong2ccccf52016-02-25 14:55:01 -0800396 qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000397 q->cstats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800398 q->cstats.drop_len = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000399
400 sch_tree_unlock(sch);
401 return 0;
402}
403
404static void *fq_codel_zalloc(size_t sz)
405{
406 void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
407
408 if (!ptr)
409 ptr = vzalloc(sz);
410 return ptr;
411}
412
413static void fq_codel_free(void *addr)
414{
WANG Cong4cb28972014-06-02 15:55:22 -0700415 kvfree(addr);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000416}
417
418static void fq_codel_destroy(struct Qdisc *sch)
419{
420 struct fq_codel_sched_data *q = qdisc_priv(sch);
421
422 tcf_destroy_chain(&q->filter_list);
423 fq_codel_free(q->backlogs);
424 fq_codel_free(q->flows);
425}
426
427static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt)
428{
429 struct fq_codel_sched_data *q = qdisc_priv(sch);
430 int i;
431
432 sch->limit = 10*1024;
433 q->flows_cnt = 1024;
434 q->quantum = psched_mtu(qdisc_dev(sch));
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500435 q->perturbation = prandom_u32();
Eric Dumazet4b549a22012-05-11 09:30:50 +0000436 INIT_LIST_HEAD(&q->new_flows);
437 INIT_LIST_HEAD(&q->old_flows);
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200438 codel_params_init(&q->cparams);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000439 codel_stats_init(&q->cstats);
440 q->cparams.ecn = true;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200441 q->cparams.mtu = psched_mtu(qdisc_dev(sch));
Eric Dumazet4b549a22012-05-11 09:30:50 +0000442
443 if (opt) {
444 int err = fq_codel_change(sch, opt);
445 if (err)
446 return err;
447 }
448
449 if (!q->flows) {
450 q->flows = fq_codel_zalloc(q->flows_cnt *
451 sizeof(struct fq_codel_flow));
452 if (!q->flows)
453 return -ENOMEM;
454 q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32));
455 if (!q->backlogs) {
456 fq_codel_free(q->flows);
457 return -ENOMEM;
458 }
459 for (i = 0; i < q->flows_cnt; i++) {
460 struct fq_codel_flow *flow = q->flows + i;
461
462 INIT_LIST_HEAD(&flow->flowchain);
Eric Dumazetb3791352012-09-01 03:19:57 +0000463 codel_vars_init(&flow->cvars);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000464 }
465 }
466 if (sch->limit >= 1)
467 sch->flags |= TCQ_F_CAN_BYPASS;
468 else
469 sch->flags &= ~TCQ_F_CAN_BYPASS;
470 return 0;
471}
472
473static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
474{
475 struct fq_codel_sched_data *q = qdisc_priv(sch);
476 struct nlattr *opts;
477
478 opts = nla_nest_start(skb, TCA_OPTIONS);
479 if (opts == NULL)
480 goto nla_put_failure;
481
482 if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
483 codel_time_to_us(q->cparams.target)) ||
484 nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
485 sch->limit) ||
486 nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
487 codel_time_to_us(q->cparams.interval)) ||
488 nla_put_u32(skb, TCA_FQ_CODEL_ECN,
489 q->cparams.ecn) ||
490 nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
491 q->quantum) ||
492 nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
493 q->flows_cnt))
494 goto nla_put_failure;
495
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700496 if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD &&
497 nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
498 codel_time_to_us(q->cparams.ce_threshold)))
499 goto nla_put_failure;
500
Yang Yingliangd59b7d82014-03-12 10:20:32 +0800501 return nla_nest_end(skb, opts);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000502
503nla_put_failure:
504 return -1;
505}
506
507static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
508{
509 struct fq_codel_sched_data *q = qdisc_priv(sch);
510 struct tc_fq_codel_xstats st = {
511 .type = TCA_FQ_CODEL_XSTATS_QDISC,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000512 };
513 struct list_head *pos;
514
Sasha Levin669d67b2012-05-14 11:57:06 +0000515 st.qdisc_stats.maxpacket = q->cstats.maxpacket;
516 st.qdisc_stats.drop_overlimit = q->drop_overlimit;
517 st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
518 st.qdisc_stats.new_flow_count = q->new_flow_count;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700519 st.qdisc_stats.ce_mark = q->cstats.ce_mark;
Sasha Levin669d67b2012-05-14 11:57:06 +0000520
Eric Dumazet4b549a22012-05-11 09:30:50 +0000521 list_for_each(pos, &q->new_flows)
522 st.qdisc_stats.new_flows_len++;
523
524 list_for_each(pos, &q->old_flows)
525 st.qdisc_stats.old_flows_len++;
526
527 return gnet_stats_copy_app(d, &st, sizeof(st));
528}
529
530static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
531{
532 return NULL;
533}
534
535static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid)
536{
537 return 0;
538}
539
540static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
541 u32 classid)
542{
543 /* we cannot bypass queue discipline anymore */
544 sch->flags &= ~TCQ_F_CAN_BYPASS;
545 return 0;
546}
547
548static void fq_codel_put(struct Qdisc *q, unsigned long cl)
549{
550}
551
John Fastabend25d8c0d2014-09-12 20:05:27 -0700552static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch,
553 unsigned long cl)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000554{
555 struct fq_codel_sched_data *q = qdisc_priv(sch);
556
557 if (cl)
558 return NULL;
559 return &q->filter_list;
560}
561
562static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
563 struct sk_buff *skb, struct tcmsg *tcm)
564{
565 tcm->tcm_handle |= TC_H_MIN(cl);
566 return 0;
567}
568
569static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
570 struct gnet_dump *d)
571{
572 struct fq_codel_sched_data *q = qdisc_priv(sch);
573 u32 idx = cl - 1;
574 struct gnet_stats_queue qs = { 0 };
575 struct tc_fq_codel_xstats xstats;
576
577 if (idx < q->flows_cnt) {
578 const struct fq_codel_flow *flow = &q->flows[idx];
579 const struct sk_buff *skb = flow->head;
580
581 memset(&xstats, 0, sizeof(xstats));
582 xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
583 xstats.class_stats.deficit = flow->deficit;
584 xstats.class_stats.ldelay =
585 codel_time_to_us(flow->cvars.ldelay);
586 xstats.class_stats.count = flow->cvars.count;
587 xstats.class_stats.lastcount = flow->cvars.lastcount;
588 xstats.class_stats.dropping = flow->cvars.dropping;
589 if (flow->cvars.dropping) {
590 codel_tdiff_t delta = flow->cvars.drop_next -
591 codel_get_time();
592
593 xstats.class_stats.drop_next = (delta >= 0) ?
594 codel_time_to_us(delta) :
595 -codel_time_to_us(-delta);
596 }
597 while (skb) {
598 qs.qlen++;
599 skb = skb->next;
600 }
601 qs.backlog = q->backlogs[idx];
602 qs.drops = flow->dropped;
603 }
John Fastabendb0ab6f92014-09-28 11:54:24 -0700604 if (gnet_stats_copy_queue(d, NULL, &qs, 0) < 0)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000605 return -1;
606 if (idx < q->flows_cnt)
607 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
608 return 0;
609}
610
611static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
612{
613 struct fq_codel_sched_data *q = qdisc_priv(sch);
614 unsigned int i;
615
616 if (arg->stop)
617 return;
618
619 for (i = 0; i < q->flows_cnt; i++) {
620 if (list_empty(&q->flows[i].flowchain) ||
621 arg->count < arg->skip) {
622 arg->count++;
623 continue;
624 }
625 if (arg->fn(sch, i + 1, arg) < 0) {
626 arg->stop = 1;
627 break;
628 }
629 arg->count++;
630 }
631}
632
633static const struct Qdisc_class_ops fq_codel_class_ops = {
634 .leaf = fq_codel_leaf,
635 .get = fq_codel_get,
636 .put = fq_codel_put,
637 .tcf_chain = fq_codel_find_tcf,
638 .bind_tcf = fq_codel_bind,
639 .unbind_tcf = fq_codel_put,
640 .dump = fq_codel_dump_class,
641 .dump_stats = fq_codel_dump_class_stats,
642 .walk = fq_codel_walk,
643};
644
645static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
646 .cl_ops = &fq_codel_class_ops,
647 .id = "fq_codel",
648 .priv_size = sizeof(struct fq_codel_sched_data),
649 .enqueue = fq_codel_enqueue,
650 .dequeue = fq_codel_dequeue,
651 .peek = qdisc_peek_dequeued,
WANG Congc0afd9c2015-07-14 11:21:58 -0700652 .drop = fq_codel_qdisc_drop,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000653 .init = fq_codel_init,
654 .reset = fq_codel_reset,
655 .destroy = fq_codel_destroy,
656 .change = fq_codel_change,
657 .dump = fq_codel_dump,
658 .dump_stats = fq_codel_dump_stats,
659 .owner = THIS_MODULE,
660};
661
662static int __init fq_codel_module_init(void)
663{
664 return register_qdisc(&fq_codel_qdisc_ops);
665}
666
667static void __exit fq_codel_module_exit(void)
668{
669 unregister_qdisc(&fq_codel_qdisc_ops);
670}
671
672module_init(fq_codel_module_init)
673module_exit(fq_codel_module_exit)
674MODULE_AUTHOR("Eric Dumazet");
675MODULE_LICENSE("GPL");