1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Codel - The Controlled-Delay Active Queue Management algorithm
4 *
5 * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
6 * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
7 *
8 * Implemented on linux by :
9 * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
10 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
11 */
12
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
19 #include <linux/prefetch.h>
20 #include <net/pkt_sched.h>
21 #include <net/codel.h>
22 #include <net/codel_impl.h>
23 #include <net/codel_qdisc.h>
24
25
26 #define DEFAULT_CODEL_LIMIT 1000
27
28 struct codel_sched_data {
29 struct codel_params params;
30 struct codel_vars vars;
31 struct codel_stats stats;
32 u32 drop_overlimit;
33 };
34
35 /* This is the specific function called from codel_dequeue()
36 * to dequeue a packet from queue. Note: backlog is handled in
37 * codel, we dont need to reduce it here.
38 */
dequeue_func(struct codel_vars * vars,void * ctx)39 static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
40 {
41 struct Qdisc *sch = ctx;
42 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
43
44 if (skb) {
45 qstats_backlog_sub(sch, qdisc_pkt_len(skb));
46 prefetch(&skb->end); /* we'll need skb_shinfo() */
47 }
48 return skb;
49 }
50
drop_func(struct sk_buff * skb,void * ctx)51 static void drop_func(struct sk_buff *skb, void *ctx)
52 {
53 struct Qdisc *sch = ctx;
54
55 qdisc_dequeue_drop(sch, skb, QDISC_DROP_CONGESTED);
56 qdisc_qstats_drop(sch);
57 }
58
__codel_qdisc_dequeue(struct Qdisc * sch)59 static struct sk_buff *__codel_qdisc_dequeue(struct Qdisc *sch)
60 {
61 struct codel_sched_data *q = qdisc_priv(sch);
62 struct sk_buff *skb;
63
64 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars,
65 &q->stats, qdisc_pkt_len, codel_get_enqueue_time,
66 drop_func, dequeue_func);
67
68 if (skb)
69 qdisc_bstats_update(sch, skb);
70 return skb;
71 }
72
codel_dequeue_drop(struct Qdisc * sch)73 static void codel_dequeue_drop(struct Qdisc *sch)
74 {
75 struct codel_sched_data *q = qdisc_priv(sch);
76
77 if (q->stats.drop_count) {
78 qdisc_tree_reduce_backlog(sch, q->stats.drop_count,
79 q->stats.drop_len);
80 q->stats.drop_count = 0;
81 q->stats.drop_len = 0;
82 }
83 }
84
codel_qdisc_dequeue(struct Qdisc * sch)85 static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
86 {
87 struct sk_buff *skb;
88
89 skb = __codel_qdisc_dequeue(sch);
90
91 codel_dequeue_drop(sch);
92
93 return skb;
94 }
95
codel_peek(struct Qdisc * sch)96 static struct sk_buff *codel_peek(struct Qdisc *sch)
97 {
98 struct sk_buff *skb = skb_peek(&sch->gso_skb);
99
100 if (!skb) {
101 skb = __codel_qdisc_dequeue(sch);
102
103 if (skb) {
104 __skb_queue_head(&sch->gso_skb, skb);
105 /* it's still part of the queue */
106 qdisc_qstats_backlog_inc(sch, skb);
107 sch->q.qlen++;
108 }
109
110 codel_dequeue_drop(sch);
111 }
112
113 return skb;
114 }
115
codel_qdisc_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)116 static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
117 struct sk_buff **to_free)
118 {
119 struct codel_sched_data *q;
120
121 if (likely(qdisc_qlen(sch) < sch->limit)) {
122 codel_set_enqueue_time(skb);
123 return qdisc_enqueue_tail(skb, sch);
124 }
125 q = qdisc_priv(sch);
126 WRITE_ONCE(q->drop_overlimit, q->drop_overlimit + 1);
127 return qdisc_drop_reason(skb, sch, to_free, QDISC_DROP_OVERLIMIT);
128 }
129
130 static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
131 [TCA_CODEL_TARGET] = { .type = NLA_U32 },
132 [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
133 [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
134 [TCA_CODEL_ECN] = { .type = NLA_U32 },
135 [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
136 };
137
codel_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)138 static int codel_change(struct Qdisc *sch, struct nlattr *opt,
139 struct netlink_ext_ack *extack)
140 {
141 unsigned int dropped_pkts = 0, dropped_bytes = 0;
142 struct codel_sched_data *q = qdisc_priv(sch);
143 struct nlattr *tb[TCA_CODEL_MAX + 1];
144 int err;
145
146 err = nla_parse_nested_deprecated(tb, TCA_CODEL_MAX, opt,
147 codel_policy, NULL);
148 if (err < 0)
149 return err;
150
151 sch_tree_lock(sch);
152
153 if (tb[TCA_CODEL_TARGET]) {
154 u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
155
156 WRITE_ONCE(q->params.target,
157 ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT);
158 }
159
160 if (tb[TCA_CODEL_CE_THRESHOLD]) {
161 u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
162
163 WRITE_ONCE(q->params.ce_threshold,
164 (val * NSEC_PER_USEC) >> CODEL_SHIFT);
165 }
166
167 if (tb[TCA_CODEL_INTERVAL]) {
168 u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
169
170 WRITE_ONCE(q->params.interval,
171 ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT);
172 }
173
174 if (tb[TCA_CODEL_LIMIT])
175 WRITE_ONCE(sch->limit,
176 nla_get_u32(tb[TCA_CODEL_LIMIT]));
177
178 if (tb[TCA_CODEL_ECN])
179 WRITE_ONCE(q->params.ecn,
180 !!nla_get_u32(tb[TCA_CODEL_ECN]));
181
182 while (sch->q.qlen > sch->limit) {
183 struct sk_buff *skb = qdisc_dequeue_internal(sch, true);
184
185 if (!skb)
186 break;
187
188 dropped_pkts++;
189 dropped_bytes += qdisc_pkt_len(skb);
190 rtnl_qdisc_drop(skb, sch);
191 }
192 qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
193
194 sch_tree_unlock(sch);
195 return 0;
196 }
197
codel_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)198 static int codel_init(struct Qdisc *sch, struct nlattr *opt,
199 struct netlink_ext_ack *extack)
200 {
201 struct codel_sched_data *q = qdisc_priv(sch);
202
203 sch->limit = DEFAULT_CODEL_LIMIT;
204
205 codel_params_init(&q->params);
206 codel_vars_init(&q->vars);
207 codel_stats_init(&q->stats);
208 q->params.mtu = min_t(u32, psched_mtu(qdisc_dev(sch)), 1 << 20);
209
210 if (opt) {
211 int err = codel_change(sch, opt, extack);
212
213 if (err)
214 return err;
215 }
216
217 if (sch->limit >= 1)
218 sch->flags |= TCQ_F_CAN_BYPASS;
219 else
220 sch->flags &= ~TCQ_F_CAN_BYPASS;
221
222 sch->flags |= TCQ_F_DEQUEUE_DROPS;
223
224 return 0;
225 }
226
codel_dump(struct Qdisc * sch,struct sk_buff * skb)227 static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
228 {
229 struct codel_sched_data *q = qdisc_priv(sch);
230 codel_time_t ce_threshold;
231 struct nlattr *opts;
232
233 opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
234 if (opts == NULL)
235 goto nla_put_failure;
236
237 if (nla_put_u32(skb, TCA_CODEL_TARGET,
238 codel_time_to_us(READ_ONCE(q->params.target))) ||
239 nla_put_u32(skb, TCA_CODEL_LIMIT,
240 READ_ONCE(sch->limit)) ||
241 nla_put_u32(skb, TCA_CODEL_INTERVAL,
242 codel_time_to_us(READ_ONCE(q->params.interval))) ||
243 nla_put_u32(skb, TCA_CODEL_ECN,
244 READ_ONCE(q->params.ecn)))
245 goto nla_put_failure;
246 ce_threshold = READ_ONCE(q->params.ce_threshold);
247 if (ce_threshold != CODEL_DISABLED_THRESHOLD &&
248 nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
249 codel_time_to_us(ce_threshold)))
250 goto nla_put_failure;
251 return nla_nest_end(skb, opts);
252
253 nla_put_failure:
254 nla_nest_cancel(skb, opts);
255 return -1;
256 }
257
codel_dump_stats(struct Qdisc * sch,struct gnet_dump * d)258 static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
259 {
260 const struct codel_sched_data *q = qdisc_priv(sch);
261 struct tc_codel_xstats st = {
262 .maxpacket = READ_ONCE(q->stats.maxpacket),
263 .count = READ_ONCE(q->vars.count),
264 .lastcount = READ_ONCE(q->vars.lastcount),
265 .drop_overlimit = READ_ONCE(q->drop_overlimit),
266 .ldelay = codel_time_to_us(READ_ONCE(q->vars.ldelay)),
267 .dropping = READ_ONCE(q->vars.dropping),
268 .ecn_mark = READ_ONCE(q->stats.ecn_mark),
269 .ce_mark = READ_ONCE(q->stats.ce_mark),
270 };
271
272 if (st.dropping) {
273 codel_tdiff_t delta = READ_ONCE(q->vars.drop_next) - codel_get_time();
274
275 if (delta >= 0)
276 st.drop_next = codel_time_to_us(delta);
277 else
278 st.drop_next = -codel_time_to_us(-delta);
279 }
280
281 return gnet_stats_copy_app(d, &st, sizeof(st));
282 }
283
codel_reset(struct Qdisc * sch)284 static void codel_reset(struct Qdisc *sch)
285 {
286 struct codel_sched_data *q = qdisc_priv(sch);
287
288 qdisc_reset_queue(sch);
289 codel_vars_init(&q->vars);
290 }
291
292 static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
293 .id = "codel",
294 .priv_size = sizeof(struct codel_sched_data),
295
296 .enqueue = codel_qdisc_enqueue,
297 .dequeue = codel_qdisc_dequeue,
298 .peek = codel_peek,
299 .init = codel_init,
300 .reset = codel_reset,
301 .change = codel_change,
302 .dump = codel_dump,
303 .dump_stats = codel_dump_stats,
304 .owner = THIS_MODULE,
305 };
306 MODULE_ALIAS_NET_SCH("codel");
307
codel_module_init(void)308 static int __init codel_module_init(void)
309 {
310 return register_qdisc(&codel_qdisc_ops);
311 }
312
codel_module_exit(void)313 static void __exit codel_module_exit(void)
314 {
315 unregister_qdisc(&codel_qdisc_ops);
316 }
317
318 module_init(codel_module_init)
319 module_exit(codel_module_exit)
320
321 MODULE_DESCRIPTION("Controlled Delay queue discipline");
322 MODULE_AUTHOR("Dave Taht");
323 MODULE_AUTHOR("Eric Dumazet");
324 MODULE_LICENSE("Dual BSD/GPL");
325