sch_red.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * net/sched/sch_red.c Random Early Detection queue.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. *
  11. * Changes:
  12. * J Hadi Salim <hadi@nortel.com> 980914: computation fixes
  13. * Alexey Makarenko <makar@phoenix.kharkov.ua> 990814: qave on idle link was calculated incorrectly.
  14. * J Hadi Salim <hadi@nortelnetworks.com> 980816: ECN support
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/system.h>
  20. #include <linux/bitops.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/string.h>
  25. #include <linux/mm.h>
  26. #include <linux/socket.h>
  27. #include <linux/sockios.h>
  28. #include <linux/in.h>
  29. #include <linux/errno.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/inet.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/notifier.h>
  36. #include <net/ip.h>
  37. #include <net/route.h>
  38. #include <linux/skbuff.h>
  39. #include <net/sock.h>
  40. #include <net/pkt_sched.h>
  41. #include <net/inet_ecn.h>
  42. #include <net/dsfield.h>
  43. /* Random Early Detection (RED) algorithm.
  44. =======================================
  45. Source: Sally Floyd and Van Jacobson, "Random Early Detection Gateways
  46. for Congestion Avoidance", 1993, IEEE/ACM Transactions on Networking.
  47. This file codes a "divisionless" version of RED algorithm
  48. as written down in Fig.17 of the paper.
  49. Short description.
  50. ------------------
  51. When a new packet arrives we calculate the average queue length:
  52. avg = (1-W)*avg + W*current_queue_len,
  53. W is the filter time constant (chosen as 2^(-Wlog)), it controls
  54. the inertia of the algorithm. To allow larger bursts, W should be
  55. decreased.
  56. if (avg > th_max) -> packet marked (dropped).
  57. if (avg < th_min) -> packet passes.
  58. if (th_min < avg < th_max) we calculate probability:
  59. Pb = max_P * (avg - th_min)/(th_max-th_min)
  60. and mark (drop) packet with this probability.
  61. Pb changes from 0 (at avg==th_min) to max_P (avg==th_max).
  62. max_P should be small (not 1), usually 0.01..0.02 is good value.
  63. max_P is chosen as a number, so that max_P/(th_max-th_min)
  64. is a negative power of two in order arithmetics to contain
  65. only shifts.
  66. Parameters, settable by user:
  67. -----------------------------
  68. limit - bytes (must be > qth_max + burst)
  69. Hard limit on queue length, should be chosen >qth_max
  70. to allow packet bursts. This parameter does not
  71. affect the algorithms behaviour and can be chosen
  72. arbitrarily high (well, less than ram size)
  73. Really, this limit will never be reached
  74. if RED works correctly.
  75. qth_min - bytes (should be < qth_max/2)
  76. qth_max - bytes (should be at least 2*qth_min and less limit)
  77. Wlog - bits (<32) log(1/W).
  78. Plog - bits (<32)
  79. Plog is related to max_P by formula:
  80. max_P = (qth_max-qth_min)/2^Plog;
  81. F.e. if qth_max=128K and qth_min=32K, then Plog=22
  82. corresponds to max_P=0.02
  83. Scell_log
  84. Stab
  85. Lookup table for log((1-W)^(t/t_ave).
  86. NOTES:
  87. Upper bound on W.
  88. -----------------
  89. If you want to allow bursts of L packets of size S,
  90. you should choose W:
  91. L + 1 - th_min/S < (1-(1-W)^L)/W
  92. th_min/S = 32 th_min/S = 4
  93. log(W) L
  94. -1 33
  95. -2 35
  96. -3 39
  97. -4 46
  98. -5 57
  99. -6 75
  100. -7 101
  101. -8 135
  102. -9 190
  103. etc.
  104. */
  105. struct red_sched_data
  106. {
  107. /* Parameters */
  108. u32 limit; /* HARD maximal queue length */
  109. u32 qth_min; /* Min average length threshold: A scaled */
  110. u32 qth_max; /* Max average length threshold: A scaled */
  111. u32 Rmask;
  112. u32 Scell_max;
  113. unsigned char flags;
  114. char Wlog; /* log(W) */
  115. char Plog; /* random number bits */
  116. char Scell_log;
  117. u8 Stab[256];
  118. /* Variables */
  119. unsigned long qave; /* Average queue length: A scaled */
  120. int qcount; /* Packets since last random number generation */
  121. u32 qR; /* Cached random number */
  122. psched_time_t qidlestart; /* Start of idle period */
  123. struct tc_red_xstats st;
  124. };
  125. static int red_ecn_mark(struct sk_buff *skb)
  126. {
  127. if (skb->nh.raw + 20 > skb->tail)
  128. return 0;
  129. switch (skb->protocol) {
  130. case __constant_htons(ETH_P_IP):
  131. if (INET_ECN_is_not_ect(skb->nh.iph->tos))
  132. return 0;
  133. IP_ECN_set_ce(skb->nh.iph);
  134. return 1;
  135. case __constant_htons(ETH_P_IPV6):
  136. if (INET_ECN_is_not_ect(ipv6_get_dsfield(skb->nh.ipv6h)))
  137. return 0;
  138. IP6_ECN_set_ce(skb->nh.ipv6h);
  139. return 1;
  140. default:
  141. return 0;
  142. }
  143. }
  144. static int
  145. red_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  146. {
  147. struct red_sched_data *q = qdisc_priv(sch);
  148. psched_time_t now;
  149. if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
  150. long us_idle;
  151. int shift;
  152. PSCHED_GET_TIME(now);
  153. us_idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max);
  154. PSCHED_SET_PASTPERFECT(q->qidlestart);
  155. /*
  156. The problem: ideally, average length queue recalcultion should
  157. be done over constant clock intervals. This is too expensive, so that
  158. the calculation is driven by outgoing packets.
  159. When the queue is idle we have to model this clock by hand.
  160. SF+VJ proposed to "generate" m = idletime/(average_pkt_size/bandwidth)
  161. dummy packets as a burst after idle time, i.e.
  162. q->qave *= (1-W)^m
  163. This is an apparently overcomplicated solution (f.e. we have to precompute
  164. a table to make this calculation in reasonable time)
  165. I believe that a simpler model may be used here,
  166. but it is field for experiments.
  167. */
  168. shift = q->Stab[us_idle>>q->Scell_log];
  169. if (shift) {
  170. q->qave >>= shift;
  171. } else {
  172. /* Approximate initial part of exponent
  173. with linear function:
  174. (1-W)^m ~= 1-mW + ...
  175. Seems, it is the best solution to
  176. problem of too coarce exponent tabulation.
  177. */
  178. us_idle = (q->qave * us_idle)>>q->Scell_log;
  179. if (us_idle < q->qave/2)
  180. q->qave -= us_idle;
  181. else
  182. q->qave >>= 1;
  183. }
  184. } else {
  185. q->qave += sch->qstats.backlog - (q->qave >> q->Wlog);
  186. /* NOTE:
  187. q->qave is fixed point number with point at Wlog.
  188. The formulae above is equvalent to floating point
  189. version:
  190. qave = qave*(1-W) + sch->qstats.backlog*W;
  191. --ANK (980924)
  192. */
  193. }
  194. if (q->qave < q->qth_min) {
  195. q->qcount = -1;
  196. enqueue:
  197. if (sch->qstats.backlog + skb->len <= q->limit) {
  198. __skb_queue_tail(&sch->q, skb);
  199. sch->qstats.backlog += skb->len;
  200. sch->bstats.bytes += skb->len;
  201. sch->bstats.packets++;
  202. return NET_XMIT_SUCCESS;
  203. } else {
  204. q->st.pdrop++;
  205. }
  206. kfree_skb(skb);
  207. sch->qstats.drops++;
  208. return NET_XMIT_DROP;
  209. }
  210. if (q->qave >= q->qth_max) {
  211. q->qcount = -1;
  212. sch->qstats.overlimits++;
  213. mark:
  214. if (!(q->flags&TC_RED_ECN) || !red_ecn_mark(skb)) {
  215. q->st.early++;
  216. goto drop;
  217. }
  218. q->st.marked++;
  219. goto enqueue;
  220. }
  221. if (++q->qcount) {
  222. /* The formula used below causes questions.
  223. OK. qR is random number in the interval 0..Rmask
  224. i.e. 0..(2^Plog). If we used floating point
  225. arithmetics, it would be: (2^Plog)*rnd_num,
  226. where rnd_num is less 1.
  227. Taking into account, that qave have fixed
  228. point at Wlog, and Plog is related to max_P by
  229. max_P = (qth_max-qth_min)/2^Plog; two lines
  230. below have the following floating point equivalent:
  231. max_P*(qave - qth_min)/(qth_max-qth_min) < rnd/qcount
  232. Any questions? --ANK (980924)
  233. */
  234. if (((q->qave - q->qth_min)>>q->Wlog)*q->qcount < q->qR)
  235. goto enqueue;
  236. q->qcount = 0;
  237. q->qR = net_random()&q->Rmask;
  238. sch->qstats.overlimits++;
  239. goto mark;
  240. }
  241. q->qR = net_random()&q->Rmask;
  242. goto enqueue;
  243. drop:
  244. kfree_skb(skb);
  245. sch->qstats.drops++;
  246. return NET_XMIT_CN;
  247. }
  248. static int
  249. red_requeue(struct sk_buff *skb, struct Qdisc* sch)
  250. {
  251. struct red_sched_data *q = qdisc_priv(sch);
  252. PSCHED_SET_PASTPERFECT(q->qidlestart);
  253. __skb_queue_head(&sch->q, skb);
  254. sch->qstats.backlog += skb->len;
  255. sch->qstats.requeues++;
  256. return 0;
  257. }
  258. static struct sk_buff *
  259. red_dequeue(struct Qdisc* sch)
  260. {
  261. struct sk_buff *skb;
  262. struct red_sched_data *q = qdisc_priv(sch);
  263. skb = __skb_dequeue(&sch->q);
  264. if (skb) {
  265. sch->qstats.backlog -= skb->len;
  266. return skb;
  267. }
  268. PSCHED_GET_TIME(q->qidlestart);
  269. return NULL;
  270. }
  271. static unsigned int red_drop(struct Qdisc* sch)
  272. {
  273. struct sk_buff *skb;
  274. struct red_sched_data *q = qdisc_priv(sch);
  275. skb = __skb_dequeue_tail(&sch->q);
  276. if (skb) {
  277. unsigned int len = skb->len;
  278. sch->qstats.backlog -= len;
  279. sch->qstats.drops++;
  280. q->st.other++;
  281. kfree_skb(skb);
  282. return len;
  283. }
  284. PSCHED_GET_TIME(q->qidlestart);
  285. return 0;
  286. }
  287. static void red_reset(struct Qdisc* sch)
  288. {
  289. struct red_sched_data *q = qdisc_priv(sch);
  290. __skb_queue_purge(&sch->q);
  291. sch->qstats.backlog = 0;
  292. PSCHED_SET_PASTPERFECT(q->qidlestart);
  293. q->qave = 0;
  294. q->qcount = -1;
  295. }
  296. static int red_change(struct Qdisc *sch, struct rtattr *opt)
  297. {
  298. struct red_sched_data *q = qdisc_priv(sch);
  299. struct rtattr *tb[TCA_RED_STAB];
  300. struct tc_red_qopt *ctl;
  301. if (opt == NULL ||
  302. rtattr_parse_nested(tb, TCA_RED_STAB, opt) ||
  303. tb[TCA_RED_PARMS-1] == 0 || tb[TCA_RED_STAB-1] == 0 ||
  304. RTA_PAYLOAD(tb[TCA_RED_PARMS-1]) < sizeof(*ctl) ||
  305. RTA_PAYLOAD(tb[TCA_RED_STAB-1]) < 256)
  306. return -EINVAL;
  307. ctl = RTA_DATA(tb[TCA_RED_PARMS-1]);
  308. sch_tree_lock(sch);
  309. q->flags = ctl->flags;
  310. q->Wlog = ctl->Wlog;
  311. q->Plog = ctl->Plog;
  312. q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
  313. q->Scell_log = ctl->Scell_log;
  314. q->Scell_max = (255<<q->Scell_log);
  315. q->qth_min = ctl->qth_min<<ctl->Wlog;
  316. q->qth_max = ctl->qth_max<<ctl->Wlog;
  317. q->limit = ctl->limit;
  318. memcpy(q->Stab, RTA_DATA(tb[TCA_RED_STAB-1]), 256);
  319. q->qcount = -1;
  320. if (skb_queue_len(&sch->q) == 0)
  321. PSCHED_SET_PASTPERFECT(q->qidlestart);
  322. sch_tree_unlock(sch);
  323. return 0;
  324. }
  325. static int red_init(struct Qdisc* sch, struct rtattr *opt)
  326. {
  327. return red_change(sch, opt);
  328. }
  329. static int red_dump(struct Qdisc *sch, struct sk_buff *skb)
  330. {
  331. struct red_sched_data *q = qdisc_priv(sch);
  332. unsigned char *b = skb->tail;
  333. struct rtattr *rta;
  334. struct tc_red_qopt opt;
  335. rta = (struct rtattr*)b;
  336. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  337. opt.limit = q->limit;
  338. opt.qth_min = q->qth_min>>q->Wlog;
  339. opt.qth_max = q->qth_max>>q->Wlog;
  340. opt.Wlog = q->Wlog;
  341. opt.Plog = q->Plog;
  342. opt.Scell_log = q->Scell_log;
  343. opt.flags = q->flags;
  344. RTA_PUT(skb, TCA_RED_PARMS, sizeof(opt), &opt);
  345. rta->rta_len = skb->tail - b;
  346. return skb->len;
  347. rtattr_failure:
  348. skb_trim(skb, b - skb->data);
  349. return -1;
  350. }
  351. static int red_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  352. {
  353. struct red_sched_data *q = qdisc_priv(sch);
  354. return gnet_stats_copy_app(d, &q->st, sizeof(q->st));
  355. }
  356. static struct Qdisc_ops red_qdisc_ops = {
  357. .next = NULL,
  358. .cl_ops = NULL,
  359. .id = "red",
  360. .priv_size = sizeof(struct red_sched_data),
  361. .enqueue = red_enqueue,
  362. .dequeue = red_dequeue,
  363. .requeue = red_requeue,
  364. .drop = red_drop,
  365. .init = red_init,
  366. .reset = red_reset,
  367. .change = red_change,
  368. .dump = red_dump,
  369. .dump_stats = red_dump_stats,
  370. .owner = THIS_MODULE,
  371. };
  372. static int __init red_module_init(void)
  373. {
  374. return register_qdisc(&red_qdisc_ops);
  375. }
  376. static void __exit red_module_exit(void)
  377. {
  378. unregister_qdisc(&red_qdisc_ops);
  379. }
  380. module_init(red_module_init)
  381. module_exit(red_module_exit)
  382. MODULE_LICENSE("GPL");