sch_codel.c 7.9 KB

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