codel.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <[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. #include <linux/types.h>
  43. #include <linux/ktime.h>
  44. #include <linux/skbuff.h>
  45. /* Controlling Queue Delay (CoDel) algorithm
  46. * =========================================
  47. * Source : Kathleen Nichols and Van Jacobson
  48. * http://queue.acm.org/detail.cfm?id=2209336
  49. *
  50. * Implemented on linux by Dave Taht and Eric Dumazet
  51. */
  52. /* CoDel uses a 1024 nsec clock, encoded in u32
  53. * This gives a range of 2199 seconds, because of signed compares
  54. */
  55. typedef u32 codel_time_t;
  56. typedef s32 codel_tdiff_t;
  57. #define CODEL_SHIFT 10
  58. #define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
  59. static inline codel_time_t codel_get_time(void)
  60. {
  61. u64 ns = ktime_get_ns();
  62. return ns >> CODEL_SHIFT;
  63. }
  64. /* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
  65. * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
  66. * codel_time_after(a,b) returns true if the time a is after time b.
  67. */
  68. #define codel_time_after(a, b) \
  69. (typecheck(codel_time_t, a) && \
  70. typecheck(codel_time_t, b) && \
  71. ((s32)((a) - (b)) > 0))
  72. #define codel_time_before(a, b) codel_time_after(b, a)
  73. #define codel_time_after_eq(a, b) \
  74. (typecheck(codel_time_t, a) && \
  75. typecheck(codel_time_t, b) && \
  76. ((s32)((a) - (b)) >= 0))
  77. #define codel_time_before_eq(a, b) codel_time_after_eq(b, a)
  78. static inline u32 codel_time_to_us(codel_time_t val)
  79. {
  80. u64 valns = ((u64)val << CODEL_SHIFT);
  81. do_div(valns, NSEC_PER_USEC);
  82. return (u32)valns;
  83. }
  84. /**
  85. * struct codel_params - contains codel parameters
  86. * @target: target queue size (in time units)
  87. * @ce_threshold: threshold for marking packets with ECN CE
  88. * @interval: width of moving time window
  89. * @mtu: device mtu, or minimal queue backlog in bytes.
  90. * @ecn: is Explicit Congestion Notification enabled
  91. * @ce_threshold_selector: apply ce_threshold to packets matching this value
  92. * in the diffserv/ECN byte of the IP header
  93. * @ce_threshold_mask: mask to apply to ce_threshold_selector comparison
  94. */
  95. struct codel_params {
  96. codel_time_t target;
  97. codel_time_t ce_threshold;
  98. codel_time_t interval;
  99. u32 mtu;
  100. bool ecn;
  101. u8 ce_threshold_selector;
  102. u8 ce_threshold_mask;
  103. };
  104. /**
  105. * struct codel_vars - contains codel variables
  106. * @count: how many drops we've done since the last time we
  107. * entered dropping state
  108. * @lastcount: count at entry to dropping state
  109. * @dropping: set to true if in dropping state
  110. * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
  111. * @first_above_time: when we went (or will go) continuously above target
  112. * for interval
  113. * @drop_next: time to drop next packet, or when we dropped last
  114. * @ldelay: sojourn time of last dequeued packet
  115. */
  116. struct codel_vars {
  117. u32 count;
  118. u32 lastcount;
  119. bool dropping;
  120. u16 rec_inv_sqrt;
  121. codel_time_t first_above_time;
  122. codel_time_t drop_next;
  123. codel_time_t ldelay;
  124. };
  125. #define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
  126. /* needed shift to get a Q0.32 number from rec_inv_sqrt */
  127. #define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
  128. /**
  129. * struct codel_stats - contains codel shared variables and stats
  130. * @maxpacket: largest packet we've seen so far
  131. * @drop_count: temp count of dropped packets in dequeue()
  132. * @drop_len: bytes of dropped packets in dequeue()
  133. * ecn_mark: number of packets we ECN marked instead of dropping
  134. * ce_mark: number of packets CE marked because sojourn time was above ce_threshold
  135. */
  136. struct codel_stats {
  137. u32 maxpacket;
  138. u32 drop_count;
  139. u32 drop_len;
  140. u32 ecn_mark;
  141. u32 ce_mark;
  142. };
  143. #define CODEL_DISABLED_THRESHOLD INT_MAX
  144. typedef u32 (*codel_skb_len_t)(const struct sk_buff *skb);
  145. typedef codel_time_t (*codel_skb_time_t)(const struct sk_buff *skb);
  146. typedef void (*codel_skb_drop_t)(struct sk_buff *skb, void *ctx);
  147. typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
  148. void *ctx);
  149. #endif