codel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #ifndef __NET_SCHED_CODEL_H
  2. #define __NET_SCHED_CODEL_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 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/types.h>
  43. #include <linux/ktime.h>
  44. #include <linux/skbuff.h>
  45. #include <net/pkt_sched.h>
  46. #include <net/inet_ecn.h>
  47. #include <linux/reciprocal_div.h>
  48. /* Controlling Queue Delay (CoDel) algorithm
  49. * =========================================
  50. * Source : Kathleen Nichols and Van Jacobson
  51. * http://queue.acm.org/detail.cfm?id=2209336
  52. *
  53. * Implemented on linux by Dave Taht and Eric Dumazet
  54. */
  55. /* CoDel uses a 1024 nsec clock, encoded in u32
  56. * This gives a range of 2199 seconds, because of signed compares
  57. */
  58. typedef u32 codel_time_t;
  59. typedef s32 codel_tdiff_t;
  60. #define CODEL_SHIFT 10
  61. #define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
  62. static inline codel_time_t codel_get_time(void)
  63. {
  64. u64 ns = ktime_to_ns(ktime_get());
  65. return ns >> CODEL_SHIFT;
  66. }
  67. #define codel_time_after(a, b) ((s32)(a) - (s32)(b) > 0)
  68. #define codel_time_after_eq(a, b) ((s32)(a) - (s32)(b) >= 0)
  69. #define codel_time_before(a, b) ((s32)(a) - (s32)(b) < 0)
  70. #define codel_time_before_eq(a, b) ((s32)(a) - (s32)(b) <= 0)
  71. /* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */
  72. struct codel_skb_cb {
  73. codel_time_t enqueue_time;
  74. };
  75. static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb)
  76. {
  77. qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb));
  78. return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data;
  79. }
  80. static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb)
  81. {
  82. return get_codel_cb(skb)->enqueue_time;
  83. }
  84. static void codel_set_enqueue_time(struct sk_buff *skb)
  85. {
  86. get_codel_cb(skb)->enqueue_time = codel_get_time();
  87. }
  88. static inline u32 codel_time_to_us(codel_time_t val)
  89. {
  90. u64 valns = ((u64)val << CODEL_SHIFT);
  91. do_div(valns, NSEC_PER_USEC);
  92. return (u32)valns;
  93. }
  94. /**
  95. * struct codel_params - contains codel parameters
  96. * @target: target queue size (in time units)
  97. * @interval: width of moving time window
  98. * @ecn: is Explicit Congestion Notification enabled
  99. */
  100. struct codel_params {
  101. codel_time_t target;
  102. codel_time_t interval;
  103. bool ecn;
  104. };
  105. /**
  106. * struct codel_vars - contains codel variables
  107. * @count: how many drops we've done since the last time we
  108. * entered dropping state
  109. * @lastcount: count at entry to dropping state
  110. * @dropping: set to true if in dropping state
  111. * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
  112. * @first_above_time: when we went (or will go) continuously above target
  113. * for interval
  114. * @drop_next: time to drop next packet, or when we dropped last
  115. * @ldelay: sojourn time of last dequeued packet
  116. */
  117. struct codel_vars {
  118. u32 count;
  119. u32 lastcount;
  120. bool dropping;
  121. u16 rec_inv_sqrt;
  122. codel_time_t first_above_time;
  123. codel_time_t drop_next;
  124. codel_time_t ldelay;
  125. };
  126. #define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
  127. /* needed shift to get a Q0.32 number from rec_inv_sqrt */
  128. #define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
  129. /**
  130. * struct codel_stats - contains codel shared variables and stats
  131. * @maxpacket: largest packet we've seen so far
  132. * @drop_count: temp count of dropped packets in dequeue()
  133. * ecn_mark: number of packets we ECN marked instead of dropping
  134. */
  135. struct codel_stats {
  136. u32 maxpacket;
  137. u32 drop_count;
  138. u32 ecn_mark;
  139. };
  140. static void codel_params_init(struct codel_params *params)
  141. {
  142. params->interval = MS2TIME(100);
  143. params->target = MS2TIME(5);
  144. params->ecn = false;
  145. }
  146. static void codel_vars_init(struct codel_vars *vars)
  147. {
  148. memset(vars, 0, sizeof(*vars));
  149. }
  150. static void codel_stats_init(struct codel_stats *stats)
  151. {
  152. stats->maxpacket = 256;
  153. }
  154. /*
  155. * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
  156. * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
  157. *
  158. * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
  159. */
  160. static void codel_Newton_step(struct codel_vars *vars)
  161. {
  162. u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
  163. u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
  164. u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
  165. val >>= 2; /* avoid overflow in following multiply */
  166. val = (val * invsqrt) >> (32 - 2 + 1);
  167. vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
  168. }
  169. /*
  170. * CoDel control_law is t + interval/sqrt(count)
  171. * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
  172. * both sqrt() and divide operation.
  173. */
  174. static codel_time_t codel_control_law(codel_time_t t,
  175. codel_time_t interval,
  176. u32 rec_inv_sqrt)
  177. {
  178. return t + reciprocal_divide(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
  179. }
  180. static bool codel_should_drop(const struct sk_buff *skb,
  181. struct Qdisc *sch,
  182. struct codel_vars *vars,
  183. struct codel_params *params,
  184. struct codel_stats *stats,
  185. codel_time_t now)
  186. {
  187. bool ok_to_drop;
  188. if (!skb) {
  189. vars->first_above_time = 0;
  190. return false;
  191. }
  192. vars->ldelay = now - codel_get_enqueue_time(skb);
  193. sch->qstats.backlog -= qdisc_pkt_len(skb);
  194. if (unlikely(qdisc_pkt_len(skb) > stats->maxpacket))
  195. stats->maxpacket = qdisc_pkt_len(skb);
  196. if (codel_time_before(vars->ldelay, params->target) ||
  197. sch->qstats.backlog <= stats->maxpacket) {
  198. /* went below - stay below for at least interval */
  199. vars->first_above_time = 0;
  200. return false;
  201. }
  202. ok_to_drop = false;
  203. if (vars->first_above_time == 0) {
  204. /* just went above from below. If we stay above
  205. * for at least interval we'll say it's ok to drop
  206. */
  207. vars->first_above_time = now + params->interval;
  208. } else if (codel_time_after(now, vars->first_above_time)) {
  209. ok_to_drop = true;
  210. }
  211. return ok_to_drop;
  212. }
  213. typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
  214. struct Qdisc *sch);
  215. static struct sk_buff *codel_dequeue(struct Qdisc *sch,
  216. struct codel_params *params,
  217. struct codel_vars *vars,
  218. struct codel_stats *stats,
  219. codel_skb_dequeue_t dequeue_func)
  220. {
  221. struct sk_buff *skb = dequeue_func(vars, sch);
  222. codel_time_t now;
  223. bool drop;
  224. if (!skb) {
  225. vars->dropping = false;
  226. return skb;
  227. }
  228. now = codel_get_time();
  229. drop = codel_should_drop(skb, sch, vars, params, stats, now);
  230. if (vars->dropping) {
  231. if (!drop) {
  232. /* sojourn time below target - leave dropping state */
  233. vars->dropping = false;
  234. } else if (codel_time_after_eq(now, vars->drop_next)) {
  235. /* It's time for the next drop. Drop the current
  236. * packet and dequeue the next. The dequeue might
  237. * take us out of dropping state.
  238. * If not, schedule the next drop.
  239. * A large backlog might result in drop rates so high
  240. * that the next drop should happen now,
  241. * hence the while loop.
  242. */
  243. while (vars->dropping &&
  244. codel_time_after_eq(now, vars->drop_next)) {
  245. vars->count++; /* dont care of possible wrap
  246. * since there is no more divide
  247. */
  248. codel_Newton_step(vars);
  249. if (params->ecn && INET_ECN_set_ce(skb)) {
  250. stats->ecn_mark++;
  251. vars->drop_next =
  252. codel_control_law(vars->drop_next,
  253. params->interval,
  254. vars->rec_inv_sqrt);
  255. goto end;
  256. }
  257. qdisc_drop(skb, sch);
  258. stats->drop_count++;
  259. skb = dequeue_func(vars, sch);
  260. if (!codel_should_drop(skb, sch,
  261. vars, params, stats, now)) {
  262. /* leave dropping state */
  263. vars->dropping = false;
  264. } else {
  265. /* and schedule the next drop */
  266. vars->drop_next =
  267. codel_control_law(vars->drop_next,
  268. params->interval,
  269. vars->rec_inv_sqrt);
  270. }
  271. }
  272. }
  273. } else if (drop) {
  274. if (params->ecn && INET_ECN_set_ce(skb)) {
  275. stats->ecn_mark++;
  276. } else {
  277. qdisc_drop(skb, sch);
  278. stats->drop_count++;
  279. skb = dequeue_func(vars, sch);
  280. drop = codel_should_drop(skb, sch, vars, params,
  281. stats, now);
  282. }
  283. vars->dropping = true;
  284. /* if min went above target close to when we last went below it
  285. * assume that the drop rate that controlled the queue on the
  286. * last cycle is a good starting point to control it now.
  287. */
  288. if (codel_time_before(now - vars->drop_next,
  289. 16 * params->interval)) {
  290. vars->count = (vars->count - vars->lastcount) | 1;
  291. /* we dont care if rec_inv_sqrt approximation
  292. * is not very precise :
  293. * Next Newton steps will correct it quadratically.
  294. */
  295. codel_Newton_step(vars);
  296. } else {
  297. vars->count = 1;
  298. vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
  299. }
  300. vars->lastcount = vars->count;
  301. vars->drop_next = codel_control_law(now, params->interval,
  302. vars->rec_inv_sqrt);
  303. }
  304. end:
  305. return skb;
  306. }
  307. #endif