sch_codel.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  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. #include <linux/module.h>
  43. #include <linux/slab.h>
  44. #include <linux/types.h>
  45. #include <linux/kernel.h>
  46. #include <linux/errno.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/prefetch.h>
  49. #include <net/pkt_sched.h>
  50. #include <net/codel.h>
  51. #include <net/codel_impl.h>
  52. #include <net/codel_qdisc.h>
  53. #define DEFAULT_CODEL_LIMIT 1000
  54. struct codel_sched_data {
  55. struct codel_params params;
  56. struct codel_vars vars;
  57. struct codel_stats stats;
  58. u32 drop_overlimit;
  59. };
  60. /* This is the specific function called from codel_dequeue()
  61. * to dequeue a packet from queue. Note: backlog is handled in
  62. * codel, we dont need to reduce it here.
  63. */
  64. static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
  65. {
  66. struct Qdisc *sch = ctx;
  67. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  68. if (skb) {
  69. sch->qstats.backlog -= qdisc_pkt_len(skb);
  70. prefetch(&skb->end); /* we'll need skb_shinfo() */
  71. }
  72. return skb;
  73. }
  74. static void drop_func(struct sk_buff *skb, void *ctx)
  75. {
  76. struct Qdisc *sch = ctx;
  77. kfree_skb(skb);
  78. qdisc_qstats_drop(sch);
  79. }
  80. static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
  81. {
  82. struct codel_sched_data *q = qdisc_priv(sch);
  83. struct sk_buff *skb;
  84. skb = codel_dequeue(sch, &sch->qstats.backlog, &q->params, &q->vars,
  85. &q->stats, qdisc_pkt_len, codel_get_enqueue_time,
  86. drop_func, dequeue_func);
  87. /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
  88. * or HTB crashes. Defer it for next round.
  89. */
  90. if (q->stats.drop_count && sch->q.qlen) {
  91. qdisc_tree_reduce_backlog(sch, q->stats.drop_count, q->stats.drop_len);
  92. q->stats.drop_count = 0;
  93. q->stats.drop_len = 0;
  94. }
  95. if (skb)
  96. qdisc_bstats_update(sch, skb);
  97. return skb;
  98. }
  99. static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  100. struct sk_buff **to_free)
  101. {
  102. struct codel_sched_data *q;
  103. if (likely(qdisc_qlen(sch) < sch->limit)) {
  104. codel_set_enqueue_time(skb);
  105. return qdisc_enqueue_tail(skb, sch);
  106. }
  107. q = qdisc_priv(sch);
  108. q->drop_overlimit++;
  109. return qdisc_drop(skb, sch, to_free);
  110. }
  111. static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
  112. [TCA_CODEL_TARGET] = { .type = NLA_U32 },
  113. [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
  114. [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
  115. [TCA_CODEL_ECN] = { .type = NLA_U32 },
  116. [TCA_CODEL_CE_THRESHOLD]= { .type = NLA_U32 },
  117. };
  118. static int codel_change(struct Qdisc *sch, struct nlattr *opt,
  119. struct netlink_ext_ack *extack)
  120. {
  121. struct codel_sched_data *q = qdisc_priv(sch);
  122. struct nlattr *tb[TCA_CODEL_MAX + 1];
  123. unsigned int qlen, dropped = 0;
  124. int err;
  125. if (!opt)
  126. return -EINVAL;
  127. err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy, NULL);
  128. if (err < 0)
  129. return err;
  130. sch_tree_lock(sch);
  131. if (tb[TCA_CODEL_TARGET]) {
  132. u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
  133. q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
  134. }
  135. if (tb[TCA_CODEL_CE_THRESHOLD]) {
  136. u64 val = nla_get_u32(tb[TCA_CODEL_CE_THRESHOLD]);
  137. q->params.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
  138. }
  139. if (tb[TCA_CODEL_INTERVAL]) {
  140. u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
  141. q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
  142. }
  143. if (tb[TCA_CODEL_LIMIT])
  144. sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
  145. if (tb[TCA_CODEL_ECN])
  146. q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
  147. qlen = sch->q.qlen;
  148. while (sch->q.qlen > sch->limit) {
  149. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  150. dropped += qdisc_pkt_len(skb);
  151. qdisc_qstats_backlog_dec(sch, skb);
  152. rtnl_qdisc_drop(skb, sch);
  153. }
  154. qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
  155. sch_tree_unlock(sch);
  156. return 0;
  157. }
  158. static int codel_init(struct Qdisc *sch, struct nlattr *opt,
  159. struct netlink_ext_ack *extack)
  160. {
  161. struct codel_sched_data *q = qdisc_priv(sch);
  162. sch->limit = DEFAULT_CODEL_LIMIT;
  163. codel_params_init(&q->params);
  164. codel_vars_init(&q->vars);
  165. codel_stats_init(&q->stats);
  166. q->params.mtu = psched_mtu(qdisc_dev(sch));
  167. if (opt) {
  168. int err = codel_change(sch, opt, extack);
  169. if (err)
  170. return err;
  171. }
  172. if (sch->limit >= 1)
  173. sch->flags |= TCQ_F_CAN_BYPASS;
  174. else
  175. sch->flags &= ~TCQ_F_CAN_BYPASS;
  176. return 0;
  177. }
  178. static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
  179. {
  180. struct codel_sched_data *q = qdisc_priv(sch);
  181. struct nlattr *opts;
  182. opts = nla_nest_start(skb, TCA_OPTIONS);
  183. if (opts == NULL)
  184. goto nla_put_failure;
  185. if (nla_put_u32(skb, TCA_CODEL_TARGET,
  186. codel_time_to_us(q->params.target)) ||
  187. nla_put_u32(skb, TCA_CODEL_LIMIT,
  188. sch->limit) ||
  189. nla_put_u32(skb, TCA_CODEL_INTERVAL,
  190. codel_time_to_us(q->params.interval)) ||
  191. nla_put_u32(skb, TCA_CODEL_ECN,
  192. q->params.ecn))
  193. goto nla_put_failure;
  194. if (q->params.ce_threshold != CODEL_DISABLED_THRESHOLD &&
  195. nla_put_u32(skb, TCA_CODEL_CE_THRESHOLD,
  196. codel_time_to_us(q->params.ce_threshold)))
  197. goto nla_put_failure;
  198. return nla_nest_end(skb, opts);
  199. nla_put_failure:
  200. nla_nest_cancel(skb, opts);
  201. return -1;
  202. }
  203. static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  204. {
  205. const struct codel_sched_data *q = qdisc_priv(sch);
  206. struct tc_codel_xstats st = {
  207. .maxpacket = q->stats.maxpacket,
  208. .count = q->vars.count,
  209. .lastcount = q->vars.lastcount,
  210. .drop_overlimit = q->drop_overlimit,
  211. .ldelay = codel_time_to_us(q->vars.ldelay),
  212. .dropping = q->vars.dropping,
  213. .ecn_mark = q->stats.ecn_mark,
  214. .ce_mark = q->stats.ce_mark,
  215. };
  216. if (q->vars.dropping) {
  217. codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
  218. if (delta >= 0)
  219. st.drop_next = codel_time_to_us(delta);
  220. else
  221. st.drop_next = -codel_time_to_us(-delta);
  222. }
  223. return gnet_stats_copy_app(d, &st, sizeof(st));
  224. }
  225. static void codel_reset(struct Qdisc *sch)
  226. {
  227. struct codel_sched_data *q = qdisc_priv(sch);
  228. qdisc_reset_queue(sch);
  229. codel_vars_init(&q->vars);
  230. }
  231. static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
  232. .id = "codel",
  233. .priv_size = sizeof(struct codel_sched_data),
  234. .enqueue = codel_qdisc_enqueue,
  235. .dequeue = codel_qdisc_dequeue,
  236. .peek = qdisc_peek_dequeued,
  237. .init = codel_init,
  238. .reset = codel_reset,
  239. .change = codel_change,
  240. .dump = codel_dump,
  241. .dump_stats = codel_dump_stats,
  242. .owner = THIS_MODULE,
  243. };
  244. static int __init codel_module_init(void)
  245. {
  246. return register_qdisc(&codel_qdisc_ops);
  247. }
  248. static void __exit codel_module_exit(void)
  249. {
  250. unregister_qdisc(&codel_qdisc_ops);
  251. }
  252. module_init(codel_module_init)
  253. module_exit(codel_module_exit)
  254. MODULE_DESCRIPTION("Controlled Delay queue discipline");
  255. MODULE_AUTHOR("Dave Taht");
  256. MODULE_AUTHOR("Eric Dumazet");
  257. MODULE_LICENSE("Dual BSD/GPL");