codel.h 11 KB

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