codel_impl.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifndef __NET_SCHED_CODEL_IMPL_H
  2. #define __NET_SCHED_CODEL_IMPL_H
  3. /*
  4. * Codel - The Controlled-Delay Active Queue Management algorithm
  5. *
  6. * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
  7. * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
  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. /* Controlling Queue Delay (CoDel) algorithm
  43. * =========================================
  44. * Source : Kathleen Nichols and Van Jacobson
  45. * http://queue.acm.org/detail.cfm?id=2209336
  46. *
  47. * Implemented on linux by Dave Taht and Eric Dumazet
  48. */
  49. static void codel_params_init(struct codel_params *params)
  50. {
  51. params->interval = MS2TIME(100);
  52. params->target = MS2TIME(5);
  53. params->ce_threshold = CODEL_DISABLED_THRESHOLD;
  54. params->ecn = false;
  55. }
  56. static void codel_vars_init(struct codel_vars *vars)
  57. {
  58. memset(vars, 0, sizeof(*vars));
  59. }
  60. static void codel_stats_init(struct codel_stats *stats)
  61. {
  62. stats->maxpacket = 0;
  63. }
  64. /*
  65. * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
  66. * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
  67. *
  68. * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
  69. */
  70. static void codel_Newton_step(struct codel_vars *vars)
  71. {
  72. u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
  73. u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
  74. u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
  75. val >>= 2; /* avoid overflow in following multiply */
  76. val = (val * invsqrt) >> (32 - 2 + 1);
  77. vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
  78. }
  79. /*
  80. * CoDel control_law is t + interval/sqrt(count)
  81. * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
  82. * both sqrt() and divide operation.
  83. */
  84. static codel_time_t codel_control_law(codel_time_t t,
  85. codel_time_t interval,
  86. u32 rec_inv_sqrt)
  87. {
  88. return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
  89. }
  90. static bool codel_should_drop(const struct sk_buff *skb,
  91. void *ctx,
  92. struct codel_vars *vars,
  93. struct codel_params *params,
  94. struct codel_stats *stats,
  95. codel_skb_len_t skb_len_func,
  96. codel_skb_time_t skb_time_func,
  97. u32 *backlog,
  98. codel_time_t now)
  99. {
  100. bool ok_to_drop;
  101. u32 skb_len;
  102. if (!skb) {
  103. vars->first_above_time = 0;
  104. return false;
  105. }
  106. skb_len = skb_len_func(skb);
  107. vars->ldelay = now - skb_time_func(skb);
  108. if (unlikely(skb_len > stats->maxpacket))
  109. stats->maxpacket = skb_len;
  110. if (codel_time_before(vars->ldelay, params->target) ||
  111. *backlog <= params->mtu) {
  112. /* went below - stay below for at least interval */
  113. vars->first_above_time = 0;
  114. return false;
  115. }
  116. ok_to_drop = false;
  117. if (vars->first_above_time == 0) {
  118. /* just went above from below. If we stay above
  119. * for at least interval we'll say it's ok to drop
  120. */
  121. vars->first_above_time = now + params->interval;
  122. } else if (codel_time_after(now, vars->first_above_time)) {
  123. ok_to_drop = true;
  124. }
  125. return ok_to_drop;
  126. }
  127. static struct sk_buff *codel_dequeue(void *ctx,
  128. u32 *backlog,
  129. struct codel_params *params,
  130. struct codel_vars *vars,
  131. struct codel_stats *stats,
  132. codel_skb_len_t skb_len_func,
  133. codel_skb_time_t skb_time_func,
  134. codel_skb_drop_t drop_func,
  135. codel_skb_dequeue_t dequeue_func)
  136. {
  137. struct sk_buff *skb = dequeue_func(vars, ctx);
  138. codel_time_t now;
  139. bool drop;
  140. if (!skb) {
  141. vars->dropping = false;
  142. return skb;
  143. }
  144. now = codel_get_time();
  145. drop = codel_should_drop(skb, ctx, vars, params, stats,
  146. skb_len_func, skb_time_func, backlog, now);
  147. if (vars->dropping) {
  148. if (!drop) {
  149. /* sojourn time below target - leave dropping state */
  150. vars->dropping = false;
  151. } else if (codel_time_after_eq(now, vars->drop_next)) {
  152. /* It's time for the next drop. Drop the current
  153. * packet and dequeue the next. The dequeue might
  154. * take us out of dropping state.
  155. * If not, schedule the next drop.
  156. * A large backlog might result in drop rates so high
  157. * that the next drop should happen now,
  158. * hence the while loop.
  159. */
  160. while (vars->dropping &&
  161. codel_time_after_eq(now, vars->drop_next)) {
  162. vars->count++; /* dont care of possible wrap
  163. * since there is no more divide
  164. */
  165. codel_Newton_step(vars);
  166. if (params->ecn && INET_ECN_set_ce(skb)) {
  167. stats->ecn_mark++;
  168. vars->drop_next =
  169. codel_control_law(vars->drop_next,
  170. params->interval,
  171. vars->rec_inv_sqrt);
  172. goto end;
  173. }
  174. stats->drop_len += skb_len_func(skb);
  175. drop_func(skb, ctx);
  176. stats->drop_count++;
  177. skb = dequeue_func(vars, ctx);
  178. if (!codel_should_drop(skb, ctx,
  179. vars, params, stats,
  180. skb_len_func,
  181. skb_time_func,
  182. backlog, now)) {
  183. /* leave dropping state */
  184. vars->dropping = false;
  185. } else {
  186. /* and schedule the next drop */
  187. vars->drop_next =
  188. codel_control_law(vars->drop_next,
  189. params->interval,
  190. vars->rec_inv_sqrt);
  191. }
  192. }
  193. }
  194. } else if (drop) {
  195. u32 delta;
  196. if (params->ecn && INET_ECN_set_ce(skb)) {
  197. stats->ecn_mark++;
  198. } else {
  199. stats->drop_len += skb_len_func(skb);
  200. drop_func(skb, ctx);
  201. stats->drop_count++;
  202. skb = dequeue_func(vars, ctx);
  203. drop = codel_should_drop(skb, ctx, vars, params,
  204. stats, skb_len_func,
  205. skb_time_func, backlog, now);
  206. }
  207. vars->dropping = true;
  208. /* if min went above target close to when we last went below it
  209. * assume that the drop rate that controlled the queue on the
  210. * last cycle is a good starting point to control it now.
  211. */
  212. delta = vars->count - vars->lastcount;
  213. if (delta > 1 &&
  214. codel_time_before(now - vars->drop_next,
  215. 16 * params->interval)) {
  216. vars->count = delta;
  217. /* we dont care if rec_inv_sqrt approximation
  218. * is not very precise :
  219. * Next Newton steps will correct it quadratically.
  220. */
  221. codel_Newton_step(vars);
  222. } else {
  223. vars->count = 1;
  224. vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
  225. }
  226. vars->lastcount = vars->count;
  227. vars->drop_next = codel_control_law(now, params->interval,
  228. vars->rec_inv_sqrt);
  229. }
  230. end:
  231. if (skb && codel_time_after(vars->ldelay, params->ce_threshold) &&
  232. INET_ECN_set_ce(skb))
  233. stats->ce_mark++;
  234. return skb;
  235. }
  236. #endif