blob: a2b81a5b2dfb22c2592409b495ed95fe8f38f1e3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Codel - The Controlled-Delay Active Queue Management algorithm
3 *
4 * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
5 * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
6 *
7 * Implemented on linux by :
8 * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
9 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The names of the authors may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE.
40 *
41 */
42
43#include <linux/module.h>
44#include <linux/slab.h>
45#include <linux/types.h>
46#include <linux/kernel.h>
47#include <linux/errno.h>
48#include <linux/skbuff.h>
49#include <linux/prefetch.h>
50#include <net/pkt_sched.h>
51#include <net/codel.h>
52#include <net/codel_impl.h>
53#include <net/codel_qdisc.h>
54
55
56#define DEFAULT_CODEL_LIMIT 1000
57
58struct codel_sched_data {
59 struct codel_params params;
60 struct codel_vars vars;
61 struct codel_stats stats;
62 u32 drop_overlimit;
63};
64
65/* This is the specific function called from codel_dequeue()
66 * to dequeue a packet from queue. Note: backlog is handled in
67 * codel, we dont need to reduce it here.
68 */
69static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
70{
71 struct Qdisc *sch = ctx;
72 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
73
74 if (skb) {
75 sch->qstats.backlog -= qdisc_pkt_len(skb);
76 prefetch(&skb->end); /* we'll need skb_shinfo() */
77 }
78 return skb;
79}
80
81static void drop_func(struct sk_buff *skb, void *ctx)
82{
83 struct Qdisc *sch = ctx;
84
85 kfree_skb(skb);
86 qdisc_qstats_drop(sch);
87}
88
89static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
90{
91 struct codel_sched_data *q = qdisc_priv(sch);
92 struct sk_buff *skb;
93
94 skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars,
95 &q->stats, qdisc_pkt_len, codel_get_enqueue_time,
96 drop_func, dequeue_func);
97
98 /* If our qlen is 0 qdisc_tree_reduce_backlog() will deactivate
99 * parent class, dequeue in parent qdisc will do the same if we
100 * return skb. Temporary increment qlen if we have skb.
101 */
102 if (q->stats.drop_count) {
103 if (skb)
104 sch->q.qlen++;
105 qdisc_tree_reduce_backlog(sch, q->stats.drop_count,
106 q->stats.drop_len);
107 if (skb)
108 sch->q.qlen--;
109 q->stats.drop_count = 0;
110 q->stats.drop_len = 0;
111 }
112 if (skb)
113 qdisc_bstats_update(sch, skb);
114 return skb;
115}
116
117static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
118 struct sk_buff **to_free)
119{
120 struct codel_sched_data *q;
121
122 if (likely(qdisc_qlen(sch) < sch->limit)) {
123 codel_set_enqueue_time(skb);
124 return qdisc_enqueue_tail(skb, sch);
125 }
126 q = qdisc_priv(sch);
127 q->drop_overlimit++;
128 return qdisc_drop(skb, sch, to_free);
129}
130
131static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
132 [TCA_CODEL_TARGET] = { .type = NLA_U32 },
133 [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
134 [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
135 [TCA_CODEL_ECN] = { .type = NLA_U32 },
136 [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
137};
138
139static int codel_change(struct Qdisc *sch, struct nlattr *opt,
140 struct netlink_ext_ack *extack)
141{
142 struct codel_sched_data *q = qdisc_priv(sch);
143 struct nlattr *tb[TCA_CODEL_MAX + 1];
144 unsigned int qlen, dropped = 0;
145 int err;
146
147 if (!opt)
148 return -EINVAL;
149
150 err = nla_parse_nested_deprecated(tb, TCA_CODEL_MAX, opt,
151 codel_policy, NULL);
152 if (err < 0)
153 return err;
154
155 sch_tree_lock(sch);
156
157 if (tb[TCA_CODEL_TARGET]) {
158 u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
159
160 q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
161 }
162
163 if (tb[TCA_CODEL_CE_THRESHOLD]) {
164 u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
165
166 q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
167 }
168
169 if (tb[TCA_CODEL_INTERVAL]) {
170 u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
171
172 q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
173 }
174
175 if (tb[TCA_CODEL_LIMIT])
176 sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
177
178 if (tb[TCA_CODEL_ECN])
179 q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
180
181 qlen = sch->q.qlen;
182 while (sch->q.qlen > sch->limit) {
183 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
184
185 dropped += qdisc_pkt_len(skb);
186 qdisc_qstats_backlog_dec(sch, skb);
187 rtnl_qdisc_drop(skb, sch);
188 }
189 qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
190
191 sch_tree_unlock(sch);
192 return 0;
193}
194
195static int codel_init(struct Qdisc *sch, struct nlattr *opt,
196 struct netlink_ext_ack *extack)
197{
198 struct codel_sched_data *q = qdisc_priv(sch);
199
200 sch->limit = DEFAULT_CODEL_LIMIT;
201
202 codel_params_init(&q->params);
203 codel_vars_init(&q->vars);
204 codel_stats_init(&q->stats);
205 q->params.mtu = psched_mtu(qdisc_dev(sch));
206
207 if (opt) {
208 int err = codel_change(sch, opt, extack);
209
210 if (err)
211 return err;
212 }
213
214 if (sch->limit >= 1)
215 sch->flags |= TCQ_F_CAN_BYPASS;
216 else
217 sch->flags &= ~TCQ_F_CAN_BYPASS;
218
219 return 0;
220}
221
222static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
223{
224 struct codel_sched_data *q = qdisc_priv(sch);
225 struct nlattr *opts;
226
227 opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
228 if (opts == NULL)
229 goto nla_put_failure;
230
231 if (nla_put_u32(skb, TCA_CODEL_TARGET,
232 codel_time_to_us(q->params.target)) ||
233 nla_put_u32(skb, TCA_CODEL_LIMIT,
234 sch->limit) ||
235 nla_put_u32(skb, TCA_CODEL_INTERVAL,
236 codel_time_to_us(q->params.interval)) ||
237 nla_put_u32(skb, TCA_CODEL_ECN,
238 q->params.ecn))
239 goto nla_put_failure;
240 if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD &&
241 nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
242 codel_time_to_us(q->params.ce_threshold)))
243 goto nla_put_failure;
244 return nla_nest_end(skb, opts);
245
246nla_put_failure:
247 nla_nest_cancel(skb, opts);
248 return -1;
249}
250
251static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
252{
253 const struct codel_sched_data *q = qdisc_priv(sch);
254 struct tc_codel_xstats st = {
255 .maxpacket = q->stats.maxpacket,
256 .count = q->vars.count,
257 .lastcount = q->vars.lastcount,
258 .drop_overlimit = q->drop_overlimit,
259 .ldelay = codel_time_to_us(q->vars.ldelay),
260 .dropping = q->vars.dropping,
261 .ecn_mark = q->stats.ecn_mark,
262 .ce_mark = q->stats.ce_mark,
263 };
264
265 if (q->vars.dropping) {
266 codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
267
268 if (delta >= 0)
269 st.drop_next = codel_time_to_us(delta);
270 else
271 st.drop_next = -codel_time_to_us(-delta);
272 }
273
274 return gnet_stats_copy_app(d, &st, sizeof(st));
275}
276
277static void codel_reset(struct Qdisc *sch)
278{
279 struct codel_sched_data *q = qdisc_priv(sch);
280
281 qdisc_reset_queue(sch);
282 codel_vars_init(&q->vars);
283}
284
285static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
286 .id = "codel",
287 .priv_size = sizeof(struct codel_sched_data),
288
289 .enqueue = codel_qdisc_enqueue,
290 .dequeue = codel_qdisc_dequeue,
291 .peek = qdisc_peek_dequeued,
292 .init = codel_init,
293 .reset = codel_reset,
294 .change = codel_change,
295 .dump = codel_dump,
296 .dump_stats = codel_dump_stats,
297 .owner = THIS_MODULE,
298};
299
300static int __init codel_module_init(void)
301{
302 return register_qdisc(&codel_qdisc_ops);
303}
304
305static void __exit codel_module_exit(void)
306{
307 unregister_qdisc(&codel_qdisc_ops);
308}
309
310module_init(codel_module_init)
311module_exit(codel_module_exit)
312
313MODULE_DESCRIPTION("Controlled Delay queue discipline");
314MODULE_AUTHOR("Dave Taht");
315MODULE_AUTHOR("Eric Dumazet");
316MODULE_LICENSE("Dual BSD/GPL");