codel_impl.h 8.3 KB

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