red.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <linux/reciprocal_div.h>
  8. /* Random Early Detection (RED) algorithm.
  9. =======================================
  10. Source: Sally Floyd and Van Jacobson, "Random Early Detection Gateways
  11. for Congestion Avoidance", 1993, IEEE/ACM Transactions on Networking.
  12. This file codes a "divisionless" version of RED algorithm
  13. as written down in Fig.17 of the paper.
  14. Short description.
  15. ------------------
  16. When a new packet arrives we calculate the average queue length:
  17. avg = (1-W)*avg + W*current_queue_len,
  18. W is the filter time constant (chosen as 2^(-Wlog)), it controls
  19. the inertia of the algorithm. To allow larger bursts, W should be
  20. decreased.
  21. if (avg > th_max) -> packet marked (dropped).
  22. if (avg < th_min) -> packet passes.
  23. if (th_min < avg < th_max) we calculate probability:
  24. Pb = max_P * (avg - th_min)/(th_max-th_min)
  25. and mark (drop) packet with this probability.
  26. Pb changes from 0 (at avg==th_min) to max_P (avg==th_max).
  27. max_P should be small (not 1), usually 0.01..0.02 is good value.
  28. max_P is chosen as a number, so that max_P/(th_max-th_min)
  29. is a negative power of two in order arithmetics to contain
  30. only shifts.
  31. Parameters, settable by user:
  32. -----------------------------
  33. qth_min - bytes (should be < qth_max/2)
  34. qth_max - bytes (should be at least 2*qth_min and less limit)
  35. Wlog - bits (<32) log(1/W).
  36. Plog - bits (<32)
  37. Plog is related to max_P by formula:
  38. max_P = (qth_max-qth_min)/2^Plog;
  39. F.e. if qth_max=128K and qth_min=32K, then Plog=22
  40. corresponds to max_P=0.02
  41. Scell_log
  42. Stab
  43. Lookup table for log((1-W)^(t/t_ave).
  44. NOTES:
  45. Upper bound on W.
  46. -----------------
  47. If you want to allow bursts of L packets of size S,
  48. you should choose W:
  49. L + 1 - th_min/S < (1-(1-W)^L)/W
  50. th_min/S = 32 th_min/S = 4
  51. log(W) L
  52. -1 33
  53. -2 35
  54. -3 39
  55. -4 46
  56. -5 57
  57. -6 75
  58. -7 101
  59. -8 135
  60. -9 190
  61. etc.
  62. */
  63. /*
  64. * Adaptative RED : An Algorithm for Increasing the Robustness of RED's AQM
  65. * (Sally FLoyd, Ramakrishna Gummadi, and Scott Shenker) August 2001
  66. *
  67. * Every 500 ms:
  68. * if (avg > target and max_p <= 0.5)
  69. * increase max_p : max_p += alpha;
  70. * else if (avg < target and max_p >= 0.01)
  71. * decrease max_p : max_p *= beta;
  72. *
  73. * target :[qth_min + 0.4*(qth_min - qth_max),
  74. * qth_min + 0.6*(qth_min - qth_max)].
  75. * alpha : min(0.01, max_p / 4)
  76. * beta : 0.9
  77. * max_P is a Q0.32 fixed point number (with 32 bits mantissa)
  78. * max_P between 0.01 and 0.5 (1% - 50%) [ Its no longer a negative power of two ]
  79. */
  80. #define RED_ONE_PERCENT ((u32)DIV_ROUND_CLOSEST(1ULL<<32, 100))
  81. #define MAX_P_MIN (1 * RED_ONE_PERCENT)
  82. #define MAX_P_MAX (50 * RED_ONE_PERCENT)
  83. #define MAX_P_ALPHA(val) min(MAX_P_MIN, val / 4)
  84. #define RED_STAB_SIZE 256
  85. #define RED_STAB_MASK (RED_STAB_SIZE - 1)
  86. struct red_stats {
  87. u32 prob_drop; /* Early probability drops */
  88. u32 prob_mark; /* Early probability marks */
  89. u32 forced_drop; /* Forced drops, qavg > max_thresh */
  90. u32 forced_mark; /* Forced marks, qavg > max_thresh */
  91. u32 pdrop; /* Drops due to queue limits */
  92. u32 other; /* Drops due to drop() calls */
  93. };
  94. struct red_parms {
  95. /* Parameters */
  96. u32 qth_min; /* Min avg length threshold: Wlog scaled */
  97. u32 qth_max; /* Max avg length threshold: Wlog scaled */
  98. u32 Scell_max;
  99. u32 max_P; /* probability, [0 .. 1.0] 32 scaled */
  100. u32 max_P_reciprocal; /* reciprocal_value(max_P / qth_delta) */
  101. u32 qth_delta; /* max_th - min_th */
  102. u32 target_min; /* min_th + 0.4*(max_th - min_th) */
  103. u32 target_max; /* min_th + 0.6*(max_th - min_th) */
  104. u8 Scell_log;
  105. u8 Wlog; /* log(W) */
  106. u8 Plog; /* random number bits */
  107. u8 Stab[RED_STAB_SIZE];
  108. /* Variables */
  109. int qcount; /* Number of packets since last random
  110. number generation */
  111. u32 qR; /* Cached random number */
  112. unsigned long qavg; /* Average queue length: Wlog scaled */
  113. ktime_t qidlestart; /* Start of current idle period */
  114. };
  115. static inline u32 red_maxp(u8 Plog)
  116. {
  117. return Plog < 32 ? (~0U >> Plog) : ~0U;
  118. }
  119. static inline void red_set_parms(struct red_parms *p,
  120. u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
  121. u8 Scell_log, u8 *stab, u32 max_P)
  122. {
  123. int delta = qth_max - qth_min;
  124. u32 max_p_delta;
  125. /* Reset average queue length, the value is strictly bound
  126. * to the parameters below, reseting hurts a bit but leaving
  127. * it might result in an unreasonable qavg for a while. --TGR
  128. */
  129. p->qavg = 0;
  130. p->qcount = -1;
  131. p->qth_min = qth_min << Wlog;
  132. p->qth_max = qth_max << Wlog;
  133. p->Wlog = Wlog;
  134. p->Plog = Plog;
  135. if (delta < 0)
  136. delta = 1;
  137. p->qth_delta = delta;
  138. if (!max_P) {
  139. max_P = red_maxp(Plog);
  140. max_P *= delta; /* max_P = (qth_max - qth_min)/2^Plog */
  141. }
  142. p->max_P = max_P;
  143. max_p_delta = max_P / delta;
  144. max_p_delta = max(max_p_delta, 1U);
  145. p->max_P_reciprocal = reciprocal_value(max_p_delta);
  146. /* RED Adaptative target :
  147. * [min_th + 0.4*(min_th - max_th),
  148. * min_th + 0.6*(min_th - max_th)].
  149. */
  150. delta /= 5;
  151. p->target_min = qth_min + 2*delta;
  152. p->target_max = qth_min + 3*delta;
  153. p->Scell_log = Scell_log;
  154. p->Scell_max = (255 << Scell_log);
  155. memcpy(p->Stab, stab, sizeof(p->Stab));
  156. }
  157. static inline int red_is_idling(const struct red_parms *p)
  158. {
  159. return p->qidlestart.tv64 != 0;
  160. }
  161. static inline void red_start_of_idle_period(struct red_parms *p)
  162. {
  163. p->qidlestart = ktime_get();
  164. }
  165. static inline void red_end_of_idle_period(struct red_parms *p)
  166. {
  167. p->qidlestart.tv64 = 0;
  168. }
  169. static inline void red_restart(struct red_parms *p)
  170. {
  171. red_end_of_idle_period(p);
  172. p->qavg = 0;
  173. p->qcount = -1;
  174. }
  175. static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p)
  176. {
  177. s64 delta = ktime_us_delta(ktime_get(), p->qidlestart);
  178. long us_idle = min_t(s64, delta, p->Scell_max);
  179. int shift;
  180. /*
  181. * The problem: ideally, average length queue recalcultion should
  182. * be done over constant clock intervals. This is too expensive, so
  183. * that the calculation is driven by outgoing packets.
  184. * When the queue is idle we have to model this clock by hand.
  185. *
  186. * SF+VJ proposed to "generate":
  187. *
  188. * m = idletime / (average_pkt_size / bandwidth)
  189. *
  190. * dummy packets as a burst after idle time, i.e.
  191. *
  192. * p->qavg *= (1-W)^m
  193. *
  194. * This is an apparently overcomplicated solution (f.e. we have to
  195. * precompute a table to make this calculation in reasonable time)
  196. * I believe that a simpler model may be used here,
  197. * but it is field for experiments.
  198. */
  199. shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
  200. if (shift)
  201. return p->qavg >> shift;
  202. else {
  203. /* Approximate initial part of exponent with linear function:
  204. *
  205. * (1-W)^m ~= 1-mW + ...
  206. *
  207. * Seems, it is the best solution to
  208. * problem of too coarse exponent tabulation.
  209. */
  210. us_idle = (p->qavg * (u64)us_idle) >> p->Scell_log;
  211. if (us_idle < (p->qavg >> 1))
  212. return p->qavg - us_idle;
  213. else
  214. return p->qavg >> 1;
  215. }
  216. }
  217. static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p,
  218. unsigned int backlog)
  219. {
  220. /*
  221. * NOTE: p->qavg is fixed point number with point at Wlog.
  222. * The formula below is equvalent to floating point
  223. * version:
  224. *
  225. * qavg = qavg*(1-W) + backlog*W;
  226. *
  227. * --ANK (980924)
  228. */
  229. return p->qavg + (backlog - (p->qavg >> p->Wlog));
  230. }
  231. static inline unsigned long red_calc_qavg(const struct red_parms *p,
  232. unsigned int backlog)
  233. {
  234. if (!red_is_idling(p))
  235. return red_calc_qavg_no_idle_time(p, backlog);
  236. else
  237. return red_calc_qavg_from_idle_time(p);
  238. }
  239. static inline u32 red_random(const struct red_parms *p)
  240. {
  241. return reciprocal_divide(net_random(), p->max_P_reciprocal);
  242. }
  243. static inline int red_mark_probability(const struct red_parms *p, unsigned long qavg)
  244. {
  245. /* The formula used below causes questions.
  246. OK. qR is random number in the interval
  247. (0..1/max_P)*(qth_max-qth_min)
  248. i.e. 0..(2^Plog). If we used floating point
  249. arithmetics, it would be: (2^Plog)*rnd_num,
  250. where rnd_num is less 1.
  251. Taking into account, that qavg have fixed
  252. point at Wlog, two lines
  253. below have the following floating point equivalent:
  254. max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
  255. Any questions? --ANK (980924)
  256. */
  257. return !(((qavg - p->qth_min) >> p->Wlog) * p->qcount < p->qR);
  258. }
  259. enum {
  260. RED_BELOW_MIN_THRESH,
  261. RED_BETWEEN_TRESH,
  262. RED_ABOVE_MAX_TRESH,
  263. };
  264. static inline int red_cmp_thresh(struct red_parms *p, unsigned long qavg)
  265. {
  266. if (qavg < p->qth_min)
  267. return RED_BELOW_MIN_THRESH;
  268. else if (qavg >= p->qth_max)
  269. return RED_ABOVE_MAX_TRESH;
  270. else
  271. return RED_BETWEEN_TRESH;
  272. }
  273. enum {
  274. RED_DONT_MARK,
  275. RED_PROB_MARK,
  276. RED_HARD_MARK,
  277. };
  278. static inline int red_action(struct red_parms *p, unsigned long qavg)
  279. {
  280. switch (red_cmp_thresh(p, qavg)) {
  281. case RED_BELOW_MIN_THRESH:
  282. p->qcount = -1;
  283. return RED_DONT_MARK;
  284. case RED_BETWEEN_TRESH:
  285. if (++p->qcount) {
  286. if (red_mark_probability(p, qavg)) {
  287. p->qcount = 0;
  288. p->qR = red_random(p);
  289. return RED_PROB_MARK;
  290. }
  291. } else
  292. p->qR = red_random(p);
  293. return RED_DONT_MARK;
  294. case RED_ABOVE_MAX_TRESH:
  295. p->qcount = -1;
  296. return RED_HARD_MARK;
  297. }
  298. BUG();
  299. return RED_DONT_MARK;
  300. }
  301. static inline void red_adaptative_algo(struct red_parms *p)
  302. {
  303. unsigned long qavg;
  304. u32 max_p_delta;
  305. qavg = p->qavg;
  306. if (red_is_idling(p))
  307. qavg = red_calc_qavg_from_idle_time(p);
  308. /* p->qavg is fixed point number with point at Wlog */
  309. qavg >>= p->Wlog;
  310. if (qavg > p->target_max && p->max_P <= MAX_P_MAX)
  311. p->max_P += MAX_P_ALPHA(p->max_P); /* maxp = maxp + alpha */
  312. else if (qavg < p->target_min && p->max_P >= MAX_P_MIN)
  313. p->max_P = (p->max_P/10)*9; /* maxp = maxp * Beta */
  314. max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta);
  315. max_p_delta = max(max_p_delta, 1U);
  316. p->max_P_reciprocal = reciprocal_value(max_p_delta);
  317. }
  318. #endif