blob: dcf7266e6901abac8681b01d732094972c5731d1 [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>
27
28/* Fair Queue CoDel.
29 *
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.
37 *
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)
42 */
43
44struct 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 */
52
53struct fq_codel_sched_data {
John Fastabend25d8c0d2014-09-12 20:05:27 -070054 struct tcf_proto __rcu *filter_list; /* optional external classifier */
Eric Dumazet4b549a22012-05-11 09:30:50 +000055 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;
64
65 struct list_head new_flows; /* list of new flows */
66 struct list_head old_flows; /* list of old flows */
67};
68
69static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
Tom Herbert342db222015-05-01 11:30:13 -070070 struct sk_buff *skb)
Eric Dumazet4b549a22012-05-11 09:30:50 +000071{
Tom Herbert342db222015-05-01 11:30:13 -070072 u32 hash = skb_get_hash_perturb(skb, q->perturbation);
Daniel Borkmann8fc54f62014-08-23 20:58:54 +020073
74 return reciprocal_scale(hash, q->flows_cnt);
Eric Dumazet4b549a22012-05-11 09:30:50 +000075}
76
77static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
78 int *qerr)
79{
80 struct fq_codel_sched_data *q = qdisc_priv(sch);
John Fastabend25d8c0d2014-09-12 20:05:27 -070081 struct tcf_proto *filter;
Eric Dumazet4b549a22012-05-11 09:30:50 +000082 struct tcf_result res;
83 int result;
84
85 if (TC_H_MAJ(skb->priority) == sch->handle &&
86 TC_H_MIN(skb->priority) > 0 &&
87 TC_H_MIN(skb->priority) <= q->flows_cnt)
88 return TC_H_MIN(skb->priority);
89
Valdis.Kletnieks@vt.edu69204cf2014-12-09 16:15:50 -050090 filter = rcu_dereference_bh(q->filter_list);
John Fastabend25d8c0d2014-09-12 20:05:27 -070091 if (!filter)
Eric Dumazet4b549a22012-05-11 09:30:50 +000092 return fq_codel_hash(q, skb) + 1;
93
94 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Daniel Borkmann3b3ae882015-08-26 23:00:06 +020095 result = tc_classify(skb, filter, &res, false);
Eric Dumazet4b549a22012-05-11 09:30:50 +000096 if (result >= 0) {
97#ifdef CONFIG_NET_CLS_ACT
98 switch (result) {
99 case TC_ACT_STOLEN:
100 case TC_ACT_QUEUED:
101 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
102 case TC_ACT_SHOT:
103 return 0;
104 }
105#endif
106 if (TC_H_MIN(res.classid) <= q->flows_cnt)
107 return TC_H_MIN(res.classid);
108 }
109 return 0;
110}
111
112/* helper functions : might be changed when/if skb use a standard list_head */
113
114/* remove one skb from head of slot queue */
115static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
116{
117 struct sk_buff *skb = flow->head;
118
119 flow->head = skb->next;
120 skb->next = NULL;
121 return skb;
122}
123
124/* add skb to flow queue (tail add) */
125static inline void flow_queue_add(struct fq_codel_flow *flow,
126 struct sk_buff *skb)
127{
128 if (flow->head == NULL)
129 flow->head = skb;
130 else
131 flow->tail->next = skb;
132 flow->tail = skb;
133 skb->next = NULL;
134}
135
136static unsigned int fq_codel_drop(struct Qdisc *sch)
137{
138 struct fq_codel_sched_data *q = qdisc_priv(sch);
139 struct sk_buff *skb;
140 unsigned int maxbacklog = 0, idx = 0, i, len;
141 struct fq_codel_flow *flow;
142
143 /* Queue is full! Find the fat flow and drop packet from it.
144 * This might sound expensive, but with 1024 flows, we scan
145 * 4KB of memory, and we dont need to handle a complex tree
146 * in fast path (packet queue/enqueue) with many cache misses.
147 */
148 for (i = 0; i < q->flows_cnt; i++) {
149 if (q->backlogs[i] > maxbacklog) {
150 maxbacklog = q->backlogs[i];
151 idx = i;
152 }
153 }
154 flow = &q->flows[idx];
155 skb = dequeue_head(flow);
156 len = qdisc_pkt_len(skb);
157 q->backlogs[idx] -= len;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000158 sch->q.qlen--;
John Fastabend25331d62014-09-28 11:53:29 -0700159 qdisc_qstats_drop(sch);
160 qdisc_qstats_backlog_dec(sch, skb);
WANG Cong052cbda2015-07-13 12:30:07 -0700161 kfree_skb(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000162 flow->dropped++;
163 return idx;
164}
165
WANG Congc0afd9c2015-07-14 11:21:58 -0700166static unsigned int fq_codel_qdisc_drop(struct Qdisc *sch)
167{
168 unsigned int prev_backlog;
169
170 prev_backlog = sch->qstats.backlog;
171 fq_codel_drop(sch);
172 return prev_backlog - sch->qstats.backlog;
173}
174
Eric Dumazet4b549a22012-05-11 09:30:50 +0000175static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch)
176{
177 struct fq_codel_sched_data *q = qdisc_priv(sch);
WANG Cong2ccccf52016-02-25 14:55:01 -0800178 unsigned int idx, prev_backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000179 struct fq_codel_flow *flow;
180 int uninitialized_var(ret);
181
182 idx = fq_codel_classify(skb, sch, &ret);
183 if (idx == 0) {
184 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -0700185 qdisc_qstats_drop(sch);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000186 kfree_skb(skb);
187 return ret;
188 }
189 idx--;
190
191 codel_set_enqueue_time(skb);
192 flow = &q->flows[idx];
193 flow_queue_add(flow, skb);
194 q->backlogs[idx] += qdisc_pkt_len(skb);
John Fastabend25331d62014-09-28 11:53:29 -0700195 qdisc_qstats_backlog_inc(sch, skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000196
197 if (list_empty(&flow->flowchain)) {
198 list_add_tail(&flow->flowchain, &q->new_flows);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000199 q->new_flow_count++;
200 flow->deficit = q->quantum;
201 flow->dropped = 0;
202 }
Vijay Subramaniancd68ddd2013-03-28 13:52:00 +0000203 if (++sch->q.qlen <= sch->limit)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000204 return NET_XMIT_SUCCESS;
205
WANG Cong2ccccf52016-02-25 14:55:01 -0800206 prev_backlog = sch->qstats.backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000207 q->drop_overlimit++;
208 /* Return Congestion Notification only if we dropped a packet
209 * from this flow.
210 */
211 if (fq_codel_drop(sch) == idx)
212 return NET_XMIT_CN;
213
214 /* As we dropped a packet, better let upper stack know this */
WANG Cong2ccccf52016-02-25 14:55:01 -0800215 qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000216 return NET_XMIT_SUCCESS;
217}
218
219/* This is the specific function called from codel_dequeue()
220 * to dequeue a packet from queue. Note: backlog is handled in
221 * codel, we dont need to reduce it here.
222 */
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200223static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000224{
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200225 struct Qdisc *sch = ctx;
Eric Dumazet865ec552012-05-16 04:39:09 +0000226 struct fq_codel_sched_data *q = qdisc_priv(sch);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000227 struct fq_codel_flow *flow;
228 struct sk_buff *skb = NULL;
229
230 flow = container_of(vars, struct fq_codel_flow, cvars);
231 if (flow->head) {
232 skb = dequeue_head(flow);
Eric Dumazet865ec552012-05-16 04:39:09 +0000233 q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000234 sch->q.qlen--;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200235 sch->qstats.backlog -= qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000236 }
237 return skb;
238}
239
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200240static void drop_func(struct sk_buff *skb, void *ctx)
241{
242 struct Qdisc *sch = ctx;
243
244 qdisc_drop(skb, sch);
245}
246
Eric Dumazet4b549a22012-05-11 09:30:50 +0000247static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
248{
249 struct fq_codel_sched_data *q = qdisc_priv(sch);
250 struct sk_buff *skb;
251 struct fq_codel_flow *flow;
252 struct list_head *head;
253 u32 prev_drop_count, prev_ecn_mark;
WANG Cong2ccccf52016-02-25 14:55:01 -0800254 unsigned int prev_backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000255
256begin:
257 head = &q->new_flows;
258 if (list_empty(head)) {
259 head = &q->old_flows;
260 if (list_empty(head))
261 return NULL;
262 }
263 flow = list_first_entry(head, struct fq_codel_flow, flowchain);
264
265 if (flow->deficit <= 0) {
266 flow->deficit += q->quantum;
267 list_move_tail(&flow->flowchain, &q->old_flows);
268 goto begin;
269 }
270
271 prev_drop_count = q->cstats.drop_count;
272 prev_ecn_mark = q->cstats.ecn_mark;
WANG Cong2ccccf52016-02-25 14:55:01 -0800273 prev_backlog = sch->qstats.backlog;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000274
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200275 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams,
276 &flow->cvars, &q->cstats, qdisc_pkt_len,
277 codel_get_enqueue_time, drop_func, dequeue_func);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000278
279 flow->dropped += q->cstats.drop_count - prev_drop_count;
280 flow->dropped += q->cstats.ecn_mark - prev_ecn_mark;
281
282 if (!skb) {
283 /* force a pass through old_flows to prevent starvation */
284 if ((head == &q->new_flows) && !list_empty(&q->old_flows))
285 list_move_tail(&flow->flowchain, &q->old_flows);
286 else
287 list_del_init(&flow->flowchain);
288 goto begin;
289 }
290 qdisc_bstats_update(sch, skb);
291 flow->deficit -= qdisc_pkt_len(skb);
WANG Cong2ccccf52016-02-25 14:55:01 -0800292 /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000293 * or HTB crashes. Defer it for next round.
294 */
295 if (q->cstats.drop_count && sch->q.qlen) {
WANG Cong2ccccf52016-02-25 14:55:01 -0800296 qdisc_tree_reduce_backlog(sch, q->cstats.drop_count,
297 q->cstats.drop_len);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000298 q->cstats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800299 q->cstats.drop_len = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000300 }
301 return skb;
302}
303
304static void fq_codel_reset(struct Qdisc *sch)
305{
Eric Dumazet3d0e0af2015-07-31 17:53:39 -0700306 struct fq_codel_sched_data *q = qdisc_priv(sch);
307 int i;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000308
Eric Dumazet3d0e0af2015-07-31 17:53:39 -0700309 INIT_LIST_HEAD(&q->new_flows);
310 INIT_LIST_HEAD(&q->old_flows);
311 for (i = 0; i < q->flows_cnt; i++) {
312 struct fq_codel_flow *flow = q->flows + i;
313
314 while (flow->head) {
315 struct sk_buff *skb = dequeue_head(flow);
316
317 qdisc_qstats_backlog_dec(sch, skb);
318 kfree_skb(skb);
319 }
320
321 INIT_LIST_HEAD(&flow->flowchain);
322 codel_vars_init(&flow->cvars);
323 }
324 memset(q->backlogs, 0, q->flows_cnt * sizeof(u32));
325 sch->q.qlen = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000326}
327
328static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
329 [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
330 [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
331 [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
332 [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
333 [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
334 [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700335 [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
Eric Dumazet4b549a22012-05-11 09:30:50 +0000336};
337
338static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
339{
340 struct fq_codel_sched_data *q = qdisc_priv(sch);
341 struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
342 int err;
343
344 if (!opt)
345 return -EINVAL;
346
347 err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy);
348 if (err < 0)
349 return err;
350 if (tb[TCA_FQ_CODEL_FLOWS]) {
351 if (q->flows)
352 return -EINVAL;
353 q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
354 if (!q->flows_cnt ||
355 q->flows_cnt > 65536)
356 return -EINVAL;
357 }
358 sch_tree_lock(sch);
359
360 if (tb[TCA_FQ_CODEL_TARGET]) {
361 u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
362
363 q->cparams.target = (target * NSEC_PER_USEC) >> CODEL_SHIFT;
364 }
365
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700366 if (tb[TCA_FQ_CODEL_CE_THRESHOLD]) {
367 u64 val = nla_get_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]);
368
369 q->cparams.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
370 }
371
Eric Dumazet4b549a22012-05-11 09:30:50 +0000372 if (tb[TCA_FQ_CODEL_INTERVAL]) {
373 u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
374
375 q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT;
376 }
377
378 if (tb[TCA_FQ_CODEL_LIMIT])
379 sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
380
381 if (tb[TCA_FQ_CODEL_ECN])
382 q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
383
384 if (tb[TCA_FQ_CODEL_QUANTUM])
385 q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
386
387 while (sch->q.qlen > sch->limit) {
388 struct sk_buff *skb = fq_codel_dequeue(sch);
389
WANG Cong2ccccf52016-02-25 14:55:01 -0800390 q->cstats.drop_len += qdisc_pkt_len(skb);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000391 kfree_skb(skb);
392 q->cstats.drop_count++;
393 }
WANG Cong2ccccf52016-02-25 14:55:01 -0800394 qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000395 q->cstats.drop_count = 0;
WANG Cong2ccccf52016-02-25 14:55:01 -0800396 q->cstats.drop_len = 0;
Eric Dumazet4b549a22012-05-11 09:30:50 +0000397
398 sch_tree_unlock(sch);
399 return 0;
400}
401
402static void *fq_codel_zalloc(size_t sz)
403{
404 void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
405
406 if (!ptr)
407 ptr = vzalloc(sz);
408 return ptr;
409}
410
411static void fq_codel_free(void *addr)
412{
WANG Cong4cb28972014-06-02 15:55:22 -0700413 kvfree(addr);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000414}
415
416static void fq_codel_destroy(struct Qdisc *sch)
417{
418 struct fq_codel_sched_data *q = qdisc_priv(sch);
419
420 tcf_destroy_chain(&q->filter_list);
421 fq_codel_free(q->backlogs);
422 fq_codel_free(q->flows);
423}
424
425static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt)
426{
427 struct fq_codel_sched_data *q = qdisc_priv(sch);
428 int i;
429
430 sch->limit = 10*1024;
431 q->flows_cnt = 1024;
432 q->quantum = psched_mtu(qdisc_dev(sch));
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500433 q->perturbation = prandom_u32();
Eric Dumazet4b549a22012-05-11 09:30:50 +0000434 INIT_LIST_HEAD(&q->new_flows);
435 INIT_LIST_HEAD(&q->old_flows);
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200436 codel_params_init(&q->cparams);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000437 codel_stats_init(&q->cstats);
438 q->cparams.ecn = true;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200439 q->cparams.mtu = psched_mtu(qdisc_dev(sch));
Eric Dumazet4b549a22012-05-11 09:30:50 +0000440
441 if (opt) {
442 int err = fq_codel_change(sch, opt);
443 if (err)
444 return err;
445 }
446
447 if (!q->flows) {
448 q->flows = fq_codel_zalloc(q->flows_cnt *
449 sizeof(struct fq_codel_flow));
450 if (!q->flows)
451 return -ENOMEM;
452 q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32));
453 if (!q->backlogs) {
454 fq_codel_free(q->flows);
455 return -ENOMEM;
456 }
457 for (i = 0; i < q->flows_cnt; i++) {
458 struct fq_codel_flow *flow = q->flows + i;
459
460 INIT_LIST_HEAD(&flow->flowchain);
Eric Dumazetb3791352012-09-01 03:19:57 +0000461 codel_vars_init(&flow->cvars);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000462 }
463 }
464 if (sch->limit >= 1)
465 sch->flags |= TCQ_F_CAN_BYPASS;
466 else
467 sch->flags &= ~TCQ_F_CAN_BYPASS;
468 return 0;
469}
470
471static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
472{
473 struct fq_codel_sched_data *q = qdisc_priv(sch);
474 struct nlattr *opts;
475
476 opts = nla_nest_start(skb, TCA_OPTIONS);
477 if (opts == NULL)
478 goto nla_put_failure;
479
480 if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
481 codel_time_to_us(q->cparams.target)) ||
482 nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
483 sch->limit) ||
484 nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
485 codel_time_to_us(q->cparams.interval)) ||
486 nla_put_u32(skb, TCA_FQ_CODEL_ECN,
487 q->cparams.ecn) ||
488 nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
489 q->quantum) ||
490 nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
491 q->flows_cnt))
492 goto nla_put_failure;
493
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700494 if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD &&
495 nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
496 codel_time_to_us(q->cparams.ce_threshold)))
497 goto nla_put_failure;
498
Yang Yingliangd59b7d82014-03-12 10:20:32 +0800499 return nla_nest_end(skb, opts);
Eric Dumazet4b549a22012-05-11 09:30:50 +0000500
501nla_put_failure:
502 return -1;
503}
504
505static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
506{
507 struct fq_codel_sched_data *q = qdisc_priv(sch);
508 struct tc_fq_codel_xstats st = {
509 .type = TCA_FQ_CODEL_XSTATS_QDISC,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000510 };
511 struct list_head *pos;
512
Sasha Levin669d67b2012-05-14 11:57:06 +0000513 st.qdisc_stats.maxpacket = q->cstats.maxpacket;
514 st.qdisc_stats.drop_overlimit = q->drop_overlimit;
515 st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
516 st.qdisc_stats.new_flow_count = q->new_flow_count;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700517 st.qdisc_stats.ce_mark = q->cstats.ce_mark;
Sasha Levin669d67b2012-05-14 11:57:06 +0000518
Eric Dumazet4b549a22012-05-11 09:30:50 +0000519 list_for_each(pos, &q->new_flows)
520 st.qdisc_stats.new_flows_len++;
521
522 list_for_each(pos, &q->old_flows)
523 st.qdisc_stats.old_flows_len++;
524
525 return gnet_stats_copy_app(d, &st, sizeof(st));
526}
527
528static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
529{
530 return NULL;
531}
532
533static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid)
534{
535 return 0;
536}
537
538static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
539 u32 classid)
540{
541 /* we cannot bypass queue discipline anymore */
542 sch->flags &= ~TCQ_F_CAN_BYPASS;
543 return 0;
544}
545
546static void fq_codel_put(struct Qdisc *q, unsigned long cl)
547{
548}
549
John Fastabend25d8c0d2014-09-12 20:05:27 -0700550static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch,
551 unsigned long cl)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000552{
553 struct fq_codel_sched_data *q = qdisc_priv(sch);
554
555 if (cl)
556 return NULL;
557 return &q->filter_list;
558}
559
560static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
561 struct sk_buff *skb, struct tcmsg *tcm)
562{
563 tcm->tcm_handle |= TC_H_MIN(cl);
564 return 0;
565}
566
567static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
568 struct gnet_dump *d)
569{
570 struct fq_codel_sched_data *q = qdisc_priv(sch);
571 u32 idx = cl - 1;
572 struct gnet_stats_queue qs = { 0 };
573 struct tc_fq_codel_xstats xstats;
574
575 if (idx < q->flows_cnt) {
576 const struct fq_codel_flow *flow = &q->flows[idx];
577 const struct sk_buff *skb = flow->head;
578
579 memset(&xstats, 0, sizeof(xstats));
580 xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
581 xstats.class_stats.deficit = flow->deficit;
582 xstats.class_stats.ldelay =
583 codel_time_to_us(flow->cvars.ldelay);
584 xstats.class_stats.count = flow->cvars.count;
585 xstats.class_stats.lastcount = flow->cvars.lastcount;
586 xstats.class_stats.dropping = flow->cvars.dropping;
587 if (flow->cvars.dropping) {
588 codel_tdiff_t delta = flow->cvars.drop_next -
589 codel_get_time();
590
591 xstats.class_stats.drop_next = (delta >= 0) ?
592 codel_time_to_us(delta) :
593 -codel_time_to_us(-delta);
594 }
595 while (skb) {
596 qs.qlen++;
597 skb = skb->next;
598 }
599 qs.backlog = q->backlogs[idx];
600 qs.drops = flow->dropped;
601 }
John Fastabendb0ab6f92014-09-28 11:54:24 -0700602 if (gnet_stats_copy_queue(d, NULL, &qs, 0) < 0)
Eric Dumazet4b549a22012-05-11 09:30:50 +0000603 return -1;
604 if (idx < q->flows_cnt)
605 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
606 return 0;
607}
608
609static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
610{
611 struct fq_codel_sched_data *q = qdisc_priv(sch);
612 unsigned int i;
613
614 if (arg->stop)
615 return;
616
617 for (i = 0; i < q->flows_cnt; i++) {
618 if (list_empty(&q->flows[i].flowchain) ||
619 arg->count < arg->skip) {
620 arg->count++;
621 continue;
622 }
623 if (arg->fn(sch, i + 1, arg) < 0) {
624 arg->stop = 1;
625 break;
626 }
627 arg->count++;
628 }
629}
630
631static const struct Qdisc_class_ops fq_codel_class_ops = {
632 .leaf = fq_codel_leaf,
633 .get = fq_codel_get,
634 .put = fq_codel_put,
635 .tcf_chain = fq_codel_find_tcf,
636 .bind_tcf = fq_codel_bind,
637 .unbind_tcf = fq_codel_put,
638 .dump = fq_codel_dump_class,
639 .dump_stats = fq_codel_dump_class_stats,
640 .walk = fq_codel_walk,
641};
642
643static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
644 .cl_ops = &fq_codel_class_ops,
645 .id = "fq_codel",
646 .priv_size = sizeof(struct fq_codel_sched_data),
647 .enqueue = fq_codel_enqueue,
648 .dequeue = fq_codel_dequeue,
649 .peek = qdisc_peek_dequeued,
WANG Congc0afd9c2015-07-14 11:21:58 -0700650 .drop = fq_codel_qdisc_drop,
Eric Dumazet4b549a22012-05-11 09:30:50 +0000651 .init = fq_codel_init,
652 .reset = fq_codel_reset,
653 .destroy = fq_codel_destroy,
654 .change = fq_codel_change,
655 .dump = fq_codel_dump,
656 .dump_stats = fq_codel_dump_stats,
657 .owner = THIS_MODULE,
658};
659
660static int __init fq_codel_module_init(void)
661{
662 return register_qdisc(&fq_codel_qdisc_ops);
663}
664
665static void __exit fq_codel_module_exit(void)
666{
667 unregister_qdisc(&fq_codel_qdisc_ops);
668}
669
670module_init(fq_codel_module_init)
671module_exit(fq_codel_module_exit)
672MODULE_AUTHOR("Eric Dumazet");
673MODULE_LICENSE("GPL");