sch_codel.c 7.4 KB

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