red.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #ifndef __NET_SCHED_RED_H
  2. #define __NET_SCHED_RED_H
  3. #include <linux/types.h>
  4. #include <net/pkt_sched.h>
  5. #include <net/inet_ecn.h>
  6. #include <net/dsfield.h>
  7. /* Random Early Detection (RED) algorithm.
  8. =======================================
  9. Source: Sally Floyd and Van Jacobson, "Random Early Detection Gateways
  10. for Congestion Avoidance", 1993, IEEE/ACM Transactions on Networking.
  11. This file codes a "divisionless" version of RED algorithm
  12. as written down in Fig.17 of the paper.
  13. Short description.
  14. ------------------
  15. When a new packet arrives we calculate the average queue length:
  16. avg = (1-W)*avg + W*current_queue_len,
  17. W is the filter time constant (chosen as 2^(-Wlog)), it controls
  18. the inertia of the algorithm. To allow larger bursts, W should be
  19. decreased.
  20. if (avg > th_max) -> packet marked (dropped).
  21. if (avg < th_min) -> packet passes.
  22. if (th_min < avg < th_max) we calculate probability:
  23. Pb = max_P * (avg - th_min)/(th_max-th_min)
  24. and mark (drop) packet with this probability.
  25. Pb changes from 0 (at avg==th_min) to max_P (avg==th_max).
  26. max_P should be small (not 1), usually 0.01..0.02 is good value.
  27. max_P is chosen as a number, so that max_P/(th_max-th_min)
  28. is a negative power of two in order arithmetics to contain
  29. only shifts.
  30. Parameters, settable by user:
  31. -----------------------------
  32. qth_min - bytes (should be < qth_max/2)
  33. qth_max - bytes (should be at least 2*qth_min and less limit)
  34. Wlog - bits (<32) log(1/W).
  35. Plog - bits (<32)
  36. Plog is related to max_P by formula:
  37. max_P = (qth_max-qth_min)/2^Plog;
  38. F.e. if qth_max=128K and qth_min=32K, then Plog=22
  39. corresponds to max_P=0.02
  40. Scell_log
  41. Stab
  42. Lookup table for log((1-W)^(t/t_ave).
  43. NOTES:
  44. Upper bound on W.
  45. -----------------
  46. If you want to allow bursts of L packets of size S,
  47. you should choose W:
  48. L + 1 - th_min/S < (1-(1-W)^L)/W
  49. th_min/S = 32 th_min/S = 4
  50. log(W) L
  51. -1 33
  52. -2 35
  53. -3 39
  54. -4 46
  55. -5 57
  56. -6 75
  57. -7 101
  58. -8 135
  59. -9 190
  60. etc.
  61. */
  62. #define RED_STAB_SIZE 256
  63. #define RED_STAB_MASK (RED_STAB_SIZE - 1)
  64. struct red_stats {
  65. u32 prob_drop; /* Early probability drops */
  66. u32 prob_mark; /* Early probability marks */
  67. u32 forced_drop; /* Forced drops, qavg > max_thresh */
  68. u32 forced_mark; /* Forced marks, qavg > max_thresh */
  69. u32 pdrop; /* Drops due to queue limits */
  70. u32 other; /* Drops due to drop() calls */
  71. };
  72. struct red_parms {
  73. /* Parameters */
  74. u32 qth_min; /* Min avg length threshold: A scaled */
  75. u32 qth_max; /* Max avg length threshold: A scaled */
  76. u32 Scell_max;
  77. u32 Rmask; /* Cached random mask, see red_rmask */
  78. u8 Scell_log;
  79. u8 Wlog; /* log(W) */
  80. u8 Plog; /* random number bits */
  81. u8 Stab[RED_STAB_SIZE];
  82. /* Variables */
  83. int qcount; /* Number of packets since last random
  84. number generation */
  85. u32 qR; /* Cached random number */
  86. unsigned long qavg; /* Average queue length: A scaled */
  87. psched_time_t qidlestart; /* Start of current idle period */
  88. };
  89. static inline u32 red_rmask(u8 Plog)
  90. {
  91. return Plog < 32 ? ((1 << Plog) - 1) : ~0UL;
  92. }
  93. static inline void red_set_parms(struct red_parms *p,
  94. u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
  95. u8 Scell_log, u8 *stab)
  96. {
  97. /* Reset average queue length, the value is strictly bound
  98. * to the parameters below, reseting hurts a bit but leaving
  99. * it might result in an unreasonable qavg for a while. --TGR
  100. */
  101. p->qavg = 0;
  102. p->qcount = -1;
  103. p->qth_min = qth_min << Wlog;
  104. p->qth_max = qth_max << Wlog;
  105. p->Wlog = Wlog;
  106. p->Plog = Plog;
  107. p->Rmask = red_rmask(Plog);
  108. p->Scell_log = Scell_log;
  109. p->Scell_max = (255 << Scell_log);
  110. memcpy(p->Stab, stab, sizeof(p->Stab));
  111. }
  112. static inline int red_is_idling(struct red_parms *p)
  113. {
  114. return p->qidlestart != PSCHED_PASTPERFECT;
  115. }
  116. static inline void red_start_of_idle_period(struct red_parms *p)
  117. {
  118. p->qidlestart = psched_get_time();
  119. }
  120. static inline void red_end_of_idle_period(struct red_parms *p)
  121. {
  122. p->qidlestart = PSCHED_PASTPERFECT;
  123. }
  124. static inline void red_restart(struct red_parms *p)
  125. {
  126. red_end_of_idle_period(p);
  127. p->qavg = 0;
  128. p->qcount = -1;
  129. }
  130. static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p)
  131. {
  132. psched_time_t now;
  133. long us_idle;
  134. int shift;
  135. now = psched_get_time();
  136. us_idle = psched_tdiff_bounded(now, p->qidlestart, p->Scell_max);
  137. /*
  138. * The problem: ideally, average length queue recalcultion should
  139. * be done over constant clock intervals. This is too expensive, so
  140. * that the calculation is driven by outgoing packets.
  141. * When the queue is idle we have to model this clock by hand.
  142. *
  143. * SF+VJ proposed to "generate":
  144. *
  145. * m = idletime / (average_pkt_size / bandwidth)
  146. *
  147. * dummy packets as a burst after idle time, i.e.
  148. *
  149. * p->qavg *= (1-W)^m
  150. *
  151. * This is an apparently overcomplicated solution (f.e. we have to
  152. * precompute a table to make this calculation in reasonable time)
  153. * I believe that a simpler model may be used here,
  154. * but it is field for experiments.
  155. */
  156. shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
  157. if (shift)
  158. return p->qavg >> shift;
  159. else {
  160. /* Approximate initial part of exponent with linear function:
  161. *
  162. * (1-W)^m ~= 1-mW + ...
  163. *
  164. * Seems, it is the best solution to
  165. * problem of too coarse exponent tabulation.
  166. */
  167. us_idle = (p->qavg * (u64)us_idle) >> p->Scell_log;
  168. if (us_idle < (p->qavg >> 1))
  169. return p->qavg - us_idle;
  170. else
  171. return p->qavg >> 1;
  172. }
  173. }
  174. static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p,
  175. unsigned int backlog)
  176. {
  177. /*
  178. * NOTE: p->qavg is fixed point number with point at Wlog.
  179. * The formula below is equvalent to floating point
  180. * version:
  181. *
  182. * qavg = qavg*(1-W) + backlog*W;
  183. *
  184. * --ANK (980924)
  185. */
  186. return p->qavg + (backlog - (p->qavg >> p->Wlog));
  187. }
  188. static inline unsigned long red_calc_qavg(struct red_parms *p,
  189. unsigned int backlog)
  190. {
  191. if (!red_is_idling(p))
  192. return red_calc_qavg_no_idle_time(p, backlog);
  193. else
  194. return red_calc_qavg_from_idle_time(p);
  195. }
  196. static inline u32 red_random(struct red_parms *p)
  197. {
  198. return net_random() & p->Rmask;
  199. }
  200. static inline int red_mark_probability(struct red_parms *p, unsigned long qavg)
  201. {
  202. /* The formula used below causes questions.
  203. OK. qR is random number in the interval 0..Rmask
  204. i.e. 0..(2^Plog). If we used floating point
  205. arithmetics, it would be: (2^Plog)*rnd_num,
  206. where rnd_num is less 1.
  207. Taking into account, that qavg have fixed
  208. point at Wlog, and Plog is related to max_P by
  209. max_P = (qth_max-qth_min)/2^Plog; two lines
  210. below have the following floating point equivalent:
  211. max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
  212. Any questions? --ANK (980924)
  213. */
  214. return !(((qavg - p->qth_min) >> p->Wlog) * p->qcount < p->qR);
  215. }
  216. enum {
  217. RED_BELOW_MIN_THRESH,
  218. RED_BETWEEN_TRESH,
  219. RED_ABOVE_MAX_TRESH,
  220. };
  221. static inline int red_cmp_thresh(struct red_parms *p, unsigned long qavg)
  222. {
  223. if (qavg < p->qth_min)
  224. return RED_BELOW_MIN_THRESH;
  225. else if (qavg >= p->qth_max)
  226. return RED_ABOVE_MAX_TRESH;
  227. else
  228. return RED_BETWEEN_TRESH;
  229. }
  230. enum {
  231. RED_DONT_MARK,
  232. RED_PROB_MARK,
  233. RED_HARD_MARK,
  234. };
  235. static inline int red_action(struct red_parms *p, unsigned long qavg)
  236. {
  237. switch (red_cmp_thresh(p, qavg)) {
  238. case RED_BELOW_MIN_THRESH:
  239. p->qcount = -1;
  240. return RED_DONT_MARK;
  241. case RED_BETWEEN_TRESH:
  242. if (++p->qcount) {
  243. if (red_mark_probability(p, qavg)) {
  244. p->qcount = 0;
  245. p->qR = red_random(p);
  246. return RED_PROB_MARK;
  247. }
  248. } else
  249. p->qR = red_random(p);
  250. return RED_DONT_MARK;
  251. case RED_ABOVE_MAX_TRESH:
  252. p->qcount = -1;
  253. return RED_HARD_MARK;
  254. }
  255. BUG();
  256. return RED_DONT_MARK;
  257. }
  258. #endif