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. u32 backlog;
  72. };
  73. struct red_parms {
  74. /* Parameters */
  75. u32 qth_min; /* Min avg length threshold: A scaled */
  76. u32 qth_max; /* Max avg length threshold: A scaled */
  77. u32 Scell_max;
  78. u32 Rmask; /* Cached random mask, see red_rmask */
  79. u8 Scell_log;
  80. u8 Wlog; /* log(W) */
  81. u8 Plog; /* random number bits */
  82. u8 Stab[RED_STAB_SIZE];
  83. /* Variables */
  84. int qcount; /* Number of packets since last random
  85. number generation */
  86. u32 qR; /* Cached random number */
  87. unsigned long qavg; /* Average queue length: A scaled */
  88. psched_time_t qidlestart; /* Start of current idle period */
  89. };
  90. static inline u32 red_rmask(u8 Plog)
  91. {
  92. return Plog < 32 ? ((1 << Plog) - 1) : ~0UL;
  93. }
  94. static inline void red_set_parms(struct red_parms *p,
  95. u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
  96. u8 Scell_log, u8 *stab)
  97. {
  98. /* Reset average queue length, the value is strictly bound
  99. * to the parameters below, reseting hurts a bit but leaving
  100. * it might result in an unreasonable qavg for a while. --TGR
  101. */
  102. p->qavg = 0;
  103. p->qcount = -1;
  104. p->qth_min = qth_min << Wlog;
  105. p->qth_max = qth_max << Wlog;
  106. p->Wlog = Wlog;
  107. p->Plog = Plog;
  108. p->Rmask = red_rmask(Plog);
  109. p->Scell_log = Scell_log;
  110. p->Scell_max = (255 << Scell_log);
  111. memcpy(p->Stab, stab, sizeof(p->Stab));
  112. }
  113. static inline int red_is_idling(struct red_parms *p)
  114. {
  115. return p->qidlestart != PSCHED_PASTPERFECT;
  116. }
  117. static inline void red_start_of_idle_period(struct red_parms *p)
  118. {
  119. p->qidlestart = psched_get_time();
  120. }
  121. static inline void red_end_of_idle_period(struct red_parms *p)
  122. {
  123. p->qidlestart = PSCHED_PASTPERFECT;
  124. }
  125. static inline void red_restart(struct red_parms *p)
  126. {
  127. red_end_of_idle_period(p);
  128. p->qavg = 0;
  129. p->qcount = -1;
  130. }
  131. static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p)
  132. {
  133. psched_time_t now;
  134. long us_idle;
  135. int shift;
  136. now = psched_get_time();
  137. us_idle = psched_tdiff_bounded(now, p->qidlestart, p->Scell_max);
  138. /*
  139. * The problem: ideally, average length queue recalcultion should
  140. * be done over constant clock intervals. This is too expensive, so
  141. * that the calculation is driven by outgoing packets.
  142. * When the queue is idle we have to model this clock by hand.
  143. *
  144. * SF+VJ proposed to "generate":
  145. *
  146. * m = idletime / (average_pkt_size / bandwidth)
  147. *
  148. * dummy packets as a burst after idle time, i.e.
  149. *
  150. * p->qavg *= (1-W)^m
  151. *
  152. * This is an apparently overcomplicated solution (f.e. we have to
  153. * precompute a table to make this calculation in reasonable time)
  154. * I believe that a simpler model may be used here,
  155. * but it is field for experiments.
  156. */
  157. shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
  158. if (shift)
  159. return p->qavg >> shift;
  160. else {
  161. /* Approximate initial part of exponent with linear function:
  162. *
  163. * (1-W)^m ~= 1-mW + ...
  164. *
  165. * Seems, it is the best solution to
  166. * problem of too coarse exponent tabulation.
  167. */
  168. us_idle = (p->qavg * (u64)us_idle) >> p->Scell_log;
  169. if (us_idle < (p->qavg >> 1))
  170. return p->qavg - us_idle;
  171. else
  172. return p->qavg >> 1;
  173. }
  174. }
  175. static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p,
  176. unsigned int backlog)
  177. {
  178. /*
  179. * NOTE: p->qavg is fixed point number with point at Wlog.
  180. * The formula below is equvalent to floating point
  181. * version:
  182. *
  183. * qavg = qavg*(1-W) + backlog*W;
  184. *
  185. * --ANK (980924)
  186. */
  187. return p->qavg + (backlog - (p->qavg >> p->Wlog));
  188. }
  189. static inline unsigned long red_calc_qavg(struct red_parms *p,
  190. unsigned int backlog)
  191. {
  192. if (!red_is_idling(p))
  193. return red_calc_qavg_no_idle_time(p, backlog);
  194. else
  195. return red_calc_qavg_from_idle_time(p);
  196. }
  197. static inline u32 red_random(struct red_parms *p)
  198. {
  199. return net_random() & p->Rmask;
  200. }
  201. static inline int red_mark_probability(struct red_parms *p, unsigned long qavg)
  202. {
  203. /* The formula used below causes questions.
  204. OK. qR is random number in the interval 0..Rmask
  205. i.e. 0..(2^Plog). If we used floating point
  206. arithmetics, it would be: (2^Plog)*rnd_num,
  207. where rnd_num is less 1.
  208. Taking into account, that qavg have fixed
  209. point at Wlog, and Plog is related to max_P by
  210. max_P = (qth_max-qth_min)/2^Plog; two lines
  211. below have the following floating point equivalent:
  212. max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
  213. Any questions? --ANK (980924)
  214. */
  215. return !(((qavg - p->qth_min) >> p->Wlog) * p->qcount < p->qR);
  216. }
  217. enum {
  218. RED_BELOW_MIN_THRESH,
  219. RED_BETWEEN_TRESH,
  220. RED_ABOVE_MAX_TRESH,
  221. };
  222. static inline int red_cmp_thresh(struct red_parms *p, unsigned long qavg)
  223. {
  224. if (qavg < p->qth_min)
  225. return RED_BELOW_MIN_THRESH;
  226. else if (qavg >= p->qth_max)
  227. return RED_ABOVE_MAX_TRESH;
  228. else
  229. return RED_BETWEEN_TRESH;
  230. }
  231. enum {
  232. RED_DONT_MARK,
  233. RED_PROB_MARK,
  234. RED_HARD_MARK,
  235. };
  236. static inline int red_action(struct red_parms *p, unsigned long qavg)
  237. {
  238. switch (red_cmp_thresh(p, qavg)) {
  239. case RED_BELOW_MIN_THRESH:
  240. p->qcount = -1;
  241. return RED_DONT_MARK;
  242. case RED_BETWEEN_TRESH:
  243. if (++p->qcount) {
  244. if (red_mark_probability(p, qavg)) {
  245. p->qcount = 0;
  246. p->qR = red_random(p);
  247. return RED_PROB_MARK;
  248. }
  249. } else
  250. p->qR = red_random(p);
  251. return RED_DONT_MARK;
  252. case RED_ABOVE_MAX_TRESH:
  253. p->qcount = -1;
  254. return RED_HARD_MARK;
  255. }
  256. BUG();
  257. return RED_DONT_MARK;
  258. }
  259. #endif