sch_codel.c 7.8 KB

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