red.h 10 KB

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