18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Codel - The Controlled-Delay Active Queue Management algorithm 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com> 58c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Implemented on linux by : 88c2ecf20Sopenharmony_ci * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net> 98c2ecf20Sopenharmony_ci * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com> 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 128c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 138c2ecf20Sopenharmony_ci * are met: 148c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 158c2ecf20Sopenharmony_ci * notice, this list of conditions, and the following disclaimer, 168c2ecf20Sopenharmony_ci * without modification. 178c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 188c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 198c2ecf20Sopenharmony_ci * documentation and/or other materials provided with the distribution. 208c2ecf20Sopenharmony_ci * 3. The names of the authors may not be used to endorse or promote products 218c2ecf20Sopenharmony_ci * derived from this software without specific prior written permission. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * Alternatively, provided that this notice is retained in full, this 248c2ecf20Sopenharmony_ci * software may be distributed under the terms of the GNU General 258c2ecf20Sopenharmony_ci * Public License ("GPL") version 2, in which case the provisions of the 268c2ecf20Sopenharmony_ci * GPL apply INSTEAD OF those given above. 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 298c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 308c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 318c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 328c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 338c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 348c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 358c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 368c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 378c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 388c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 398c2ecf20Sopenharmony_ci * DAMAGE. 408c2ecf20Sopenharmony_ci * 418c2ecf20Sopenharmony_ci */ 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#include <linux/module.h> 448c2ecf20Sopenharmony_ci#include <linux/slab.h> 458c2ecf20Sopenharmony_ci#include <linux/types.h> 468c2ecf20Sopenharmony_ci#include <linux/kernel.h> 478c2ecf20Sopenharmony_ci#include <linux/errno.h> 488c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 498c2ecf20Sopenharmony_ci#include <linux/prefetch.h> 508c2ecf20Sopenharmony_ci#include <net/pkt_sched.h> 518c2ecf20Sopenharmony_ci#include <net/codel.h> 528c2ecf20Sopenharmony_ci#include <net/codel_impl.h> 538c2ecf20Sopenharmony_ci#include <net/codel_qdisc.h> 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define DEFAULT_CODEL_LIMIT 1000 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistruct codel_sched_data { 598c2ecf20Sopenharmony_ci struct codel_params params; 608c2ecf20Sopenharmony_ci struct codel_vars vars; 618c2ecf20Sopenharmony_ci struct codel_stats stats; 628c2ecf20Sopenharmony_ci u32 drop_overlimit; 638c2ecf20Sopenharmony_ci}; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* This is the specific function called from codel_dequeue() 668c2ecf20Sopenharmony_ci * to dequeue a packet from queue. Note: backlog is handled in 678c2ecf20Sopenharmony_ci * codel, we dont need to reduce it here. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_cistatic struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci struct Qdisc *sch = ctx; 728c2ecf20Sopenharmony_ci struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (skb) { 758c2ecf20Sopenharmony_ci sch->qstats.backlog -= qdisc_pkt_len(skb); 768c2ecf20Sopenharmony_ci prefetch(&skb->end); /* we'll need skb_shinfo() */ 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci return skb; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic void drop_func(struct sk_buff *skb, void *ctx) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci struct Qdisc *sch = ctx; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci kfree_skb(skb); 868c2ecf20Sopenharmony_ci qdisc_qstats_drop(sch); 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci struct codel_sched_data *q = qdisc_priv(sch); 928c2ecf20Sopenharmony_ci struct sk_buff *skb; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars, 958c2ecf20Sopenharmony_ci &q->stats, qdisc_pkt_len, codel_get_enqueue_time, 968c2ecf20Sopenharmony_ci drop_func, dequeue_func); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0, 998c2ecf20Sopenharmony_ci * or HTB crashes. Defer it for next round. 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_ci if (q->stats.drop_count && sch->q.qlen) { 1028c2ecf20Sopenharmony_ci qdisc_tree_reduce_backlog(sch, q->stats.drop_count, q->stats.drop_len); 1038c2ecf20Sopenharmony_ci q->stats.drop_count = 0; 1048c2ecf20Sopenharmony_ci q->stats.drop_len = 0; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci if (skb) 1078c2ecf20Sopenharmony_ci qdisc_bstats_update(sch, skb); 1088c2ecf20Sopenharmony_ci return skb; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, 1128c2ecf20Sopenharmony_ci struct sk_buff **to_free) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci struct codel_sched_data *q; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (likely(qdisc_qlen(sch) < sch->limit)) { 1178c2ecf20Sopenharmony_ci codel_set_enqueue_time(skb); 1188c2ecf20Sopenharmony_ci return qdisc_enqueue_tail(skb, sch); 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci q = qdisc_priv(sch); 1218c2ecf20Sopenharmony_ci q->drop_overlimit++; 1228c2ecf20Sopenharmony_ci return qdisc_drop(skb, sch, to_free); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = { 1268c2ecf20Sopenharmony_ci [TCA_CODEL_TARGET] = { .type = NLA_U32 }, 1278c2ecf20Sopenharmony_ci [TCA_CODEL_LIMIT] = { .type = NLA_U32 }, 1288c2ecf20Sopenharmony_ci [TCA_CODEL_INTERVAL] = { .type = NLA_U32 }, 1298c2ecf20Sopenharmony_ci [TCA_CODEL_ECN] = { .type = NLA_U32 }, 1308c2ecf20Sopenharmony_ci [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 }, 1318c2ecf20Sopenharmony_ci}; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic int codel_change(struct Qdisc *sch, struct nlattr *opt, 1348c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct codel_sched_data *q = qdisc_priv(sch); 1378c2ecf20Sopenharmony_ci struct nlattr *tb[TCA_CODEL_MAX + 1]; 1388c2ecf20Sopenharmony_ci unsigned int qlen, dropped = 0; 1398c2ecf20Sopenharmony_ci int err; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (!opt) 1428c2ecf20Sopenharmony_ci return -EINVAL; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci err = nla_parse_nested_deprecated(tb, TCA_CODEL_MAX, opt, 1458c2ecf20Sopenharmony_ci codel_policy, NULL); 1468c2ecf20Sopenharmony_ci if (err < 0) 1478c2ecf20Sopenharmony_ci return err; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci sch_tree_lock(sch); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (tb[TCA_CODEL_TARGET]) { 1528c2ecf20Sopenharmony_ci u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (tb[TCA_CODEL_CE_THRESHOLD]) { 1588c2ecf20Sopenharmony_ci u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT; 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (tb[TCA_CODEL_INTERVAL]) { 1648c2ecf20Sopenharmony_ci u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (tb[TCA_CODEL_LIMIT]) 1708c2ecf20Sopenharmony_ci sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci if (tb[TCA_CODEL_ECN]) 1738c2ecf20Sopenharmony_ci q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci qlen = sch->q.qlen; 1768c2ecf20Sopenharmony_ci while (sch->q.qlen > sch->limit) { 1778c2ecf20Sopenharmony_ci struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci dropped += qdisc_pkt_len(skb); 1808c2ecf20Sopenharmony_ci qdisc_qstats_backlog_dec(sch, skb); 1818c2ecf20Sopenharmony_ci rtnl_qdisc_drop(skb, sch); 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci sch_tree_unlock(sch); 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic int codel_init(struct Qdisc *sch, struct nlattr *opt, 1908c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci struct codel_sched_data *q = qdisc_priv(sch); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci sch->limit = DEFAULT_CODEL_LIMIT; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci codel_params_init(&q->params); 1978c2ecf20Sopenharmony_ci codel_vars_init(&q->vars); 1988c2ecf20Sopenharmony_ci codel_stats_init(&q->stats); 1998c2ecf20Sopenharmony_ci q->params.mtu = psched_mtu(qdisc_dev(sch)); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci if (opt) { 2028c2ecf20Sopenharmony_ci int err = codel_change(sch, opt, extack); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci if (err) 2058c2ecf20Sopenharmony_ci return err; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (sch->limit >= 1) 2098c2ecf20Sopenharmony_ci sch->flags |= TCQ_F_CAN_BYPASS; 2108c2ecf20Sopenharmony_ci else 2118c2ecf20Sopenharmony_ci sch->flags &= ~TCQ_F_CAN_BYPASS; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci return 0; 2148c2ecf20Sopenharmony_ci} 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_cistatic int codel_dump(struct Qdisc *sch, struct sk_buff *skb) 2178c2ecf20Sopenharmony_ci{ 2188c2ecf20Sopenharmony_ci struct codel_sched_data *q = qdisc_priv(sch); 2198c2ecf20Sopenharmony_ci struct nlattr *opts; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci opts = nla_nest_start_noflag(skb, TCA_OPTIONS); 2228c2ecf20Sopenharmony_ci if (opts == NULL) 2238c2ecf20Sopenharmony_ci goto nla_put_failure; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (nla_put_u32(skb, TCA_CODEL_TARGET, 2268c2ecf20Sopenharmony_ci codel_time_to_us(q->params.target)) || 2278c2ecf20Sopenharmony_ci nla_put_u32(skb, TCA_CODEL_LIMIT, 2288c2ecf20Sopenharmony_ci sch->limit) || 2298c2ecf20Sopenharmony_ci nla_put_u32(skb, TCA_CODEL_INTERVAL, 2308c2ecf20Sopenharmony_ci codel_time_to_us(q->params.interval)) || 2318c2ecf20Sopenharmony_ci nla_put_u32(skb, TCA_CODEL_ECN, 2328c2ecf20Sopenharmony_ci q->params.ecn)) 2338c2ecf20Sopenharmony_ci goto nla_put_failure; 2348c2ecf20Sopenharmony_ci if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD && 2358c2ecf20Sopenharmony_ci nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD, 2368c2ecf20Sopenharmony_ci codel_time_to_us(q->params.ce_threshold))) 2378c2ecf20Sopenharmony_ci goto nla_put_failure; 2388c2ecf20Sopenharmony_ci return nla_nest_end(skb, opts); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cinla_put_failure: 2418c2ecf20Sopenharmony_ci nla_nest_cancel(skb, opts); 2428c2ecf20Sopenharmony_ci return -1; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci const struct codel_sched_data *q = qdisc_priv(sch); 2488c2ecf20Sopenharmony_ci struct tc_codel_xstats st = { 2498c2ecf20Sopenharmony_ci .maxpacket = q->stats.maxpacket, 2508c2ecf20Sopenharmony_ci .count = q->vars.count, 2518c2ecf20Sopenharmony_ci .lastcount = q->vars.lastcount, 2528c2ecf20Sopenharmony_ci .drop_overlimit = q->drop_overlimit, 2538c2ecf20Sopenharmony_ci .ldelay = codel_time_to_us(q->vars.ldelay), 2548c2ecf20Sopenharmony_ci .dropping = q->vars.dropping, 2558c2ecf20Sopenharmony_ci .ecn_mark = q->stats.ecn_mark, 2568c2ecf20Sopenharmony_ci .ce_mark = q->stats.ce_mark, 2578c2ecf20Sopenharmony_ci }; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (q->vars.dropping) { 2608c2ecf20Sopenharmony_ci codel_tdiff_t delta = q->vars.drop_next - codel_get_time(); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci if (delta >= 0) 2638c2ecf20Sopenharmony_ci st.drop_next = codel_time_to_us(delta); 2648c2ecf20Sopenharmony_ci else 2658c2ecf20Sopenharmony_ci st.drop_next = -codel_time_to_us(-delta); 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci return gnet_stats_copy_app(d, &st, sizeof(st)); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic void codel_reset(struct Qdisc *sch) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci struct codel_sched_data *q = qdisc_priv(sch); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci qdisc_reset_queue(sch); 2768c2ecf20Sopenharmony_ci codel_vars_init(&q->vars); 2778c2ecf20Sopenharmony_ci} 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_cistatic struct Qdisc_ops codel_qdisc_ops __read_mostly = { 2808c2ecf20Sopenharmony_ci .id = "codel", 2818c2ecf20Sopenharmony_ci .priv_size = sizeof(struct codel_sched_data), 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci .enqueue = codel_qdisc_enqueue, 2848c2ecf20Sopenharmony_ci .dequeue = codel_qdisc_dequeue, 2858c2ecf20Sopenharmony_ci .peek = qdisc_peek_dequeued, 2868c2ecf20Sopenharmony_ci .init = codel_init, 2878c2ecf20Sopenharmony_ci .reset = codel_reset, 2888c2ecf20Sopenharmony_ci .change = codel_change, 2898c2ecf20Sopenharmony_ci .dump = codel_dump, 2908c2ecf20Sopenharmony_ci .dump_stats = codel_dump_stats, 2918c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 2928c2ecf20Sopenharmony_ci}; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_cistatic int __init codel_module_init(void) 2958c2ecf20Sopenharmony_ci{ 2968c2ecf20Sopenharmony_ci return register_qdisc(&codel_qdisc_ops); 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic void __exit codel_module_exit(void) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci unregister_qdisc(&codel_qdisc_ops); 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cimodule_init(codel_module_init) 3058c2ecf20Sopenharmony_cimodule_exit(codel_module_exit) 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Controlled Delay queue discipline"); 3088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dave Taht"); 3098c2ecf20Sopenharmony_ciMODULE_AUTHOR("Eric Dumazet"); 3108c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 311