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