tfrc_equation.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
  3. * Copyright (c) 2005 Ian McDonald <ian.mcdonald@jandi.co.nz>
  4. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  5. * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include "../../dccp.h"
  14. #include "tfrc.h"
  15. #define TFRC_CALC_X_ARRSIZE 500
  16. #define TFRC_CALC_X_SPLIT 50000 /* 0.05 * 1000000, details below */
  17. #define TFRC_SMALLEST_P (TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE)
  18. /*
  19. TFRC TCP Reno Throughput Equation Lookup Table for f(p)
  20. The following two-column lookup table implements a part of the TCP throughput
  21. equation from [RFC 3448, sec. 3.1]:
  22. s
  23. X_calc = --------------------------------------------------------------
  24. R * sqrt(2*b*p/3) + (3 * t_RTO * sqrt(3*b*p/8) * (p + 32*p^3))
  25. Where:
  26. X is the transmit rate in bytes/second
  27. s is the packet size in bytes
  28. R is the round trip time in seconds
  29. p is the loss event rate, between 0 and 1.0, of the number of loss
  30. events as a fraction of the number of packets transmitted
  31. t_RTO is the TCP retransmission timeout value in seconds
  32. b is the number of packets acknowledged by a single TCP ACK
  33. We can assume that b = 1 and t_RTO is 4 * R. The equation now becomes:
  34. s
  35. X_calc = -------------------------------------------------------
  36. R * sqrt(p*2/3) + (12 * R * sqrt(p*3/8) * (p + 32*p^3))
  37. which we can break down into:
  38. s
  39. X_calc = ---------
  40. R * f(p)
  41. where f(p) is given for 0 < p <= 1 by:
  42. f(p) = sqrt(2*p/3) + 12 * sqrt(3*p/8) * (p + 32*p^3)
  43. Since this is kernel code, floating-point arithmetic is avoided in favour of
  44. integer arithmetic. This means that nearly all fractional parameters are
  45. scaled by 1000000:
  46. * the parameters p and R
  47. * the return result f(p)
  48. The lookup table therefore actually tabulates the following function g(q):
  49. g(q) = 1000000 * f(q/1000000)
  50. Hence, when p <= 1, q must be less than or equal to 1000000. To achieve finer
  51. granularity for the practically more relevant case of small values of p (up to
  52. 5%), the second column is used; the first one ranges up to 100%. This split
  53. corresponds to the value of q = TFRC_CALC_X_SPLIT. At the same time this also
  54. determines the smallest resolution possible with this lookup table:
  55. TFRC_SMALLEST_P = TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE
  56. The entire table is generated by:
  57. for(i=0; i < TFRC_CALC_X_ARRSIZE; i++) {
  58. lookup[i][0] = g((i+1) * 1000000/TFRC_CALC_X_ARRSIZE);
  59. lookup[i][1] = g((i+1) * TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE);
  60. }
  61. With the given configuration, we have, with M = TFRC_CALC_X_ARRSIZE-1,
  62. lookup[0][0] = g(1000000/(M+1)) = 1000000 * f(0.2%)
  63. lookup[M][0] = g(1000000) = 1000000 * f(100%)
  64. lookup[0][1] = g(TFRC_SMALLEST_P) = 1000000 * f(0.01%)
  65. lookup[M][1] = g(TFRC_CALC_X_SPLIT) = 1000000 * f(5%)
  66. In summary, the two columns represent f(p) for the following ranges:
  67. * The first column is for 0.002 <= p <= 1.0
  68. * The second column is for 0.0001 <= p <= 0.05
  69. Where the columns overlap, the second (finer-grained) is given preference,
  70. i.e. the first column is used only for p >= 0.05.
  71. */
  72. static const u32 tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE][2] = {
  73. { 37172, 8172 },
  74. { 53499, 11567 },
  75. { 66664, 14180 },
  76. { 78298, 16388 },
  77. { 89021, 18339 },
  78. { 99147, 20108 },
  79. { 108858, 21738 },
  80. { 118273, 23260 },
  81. { 127474, 24693 },
  82. { 136520, 26052 },
  83. { 145456, 27348 },
  84. { 154316, 28589 },
  85. { 163130, 29783 },
  86. { 171919, 30935 },
  87. { 180704, 32049 },
  88. { 189502, 33130 },
  89. { 198328, 34180 },
  90. { 207194, 35202 },
  91. { 216114, 36198 },
  92. { 225097, 37172 },
  93. { 234153, 38123 },
  94. { 243294, 39055 },
  95. { 252527, 39968 },
  96. { 261861, 40864 },
  97. { 271305, 41743 },
  98. { 280866, 42607 },
  99. { 290553, 43457 },
  100. { 300372, 44293 },
  101. { 310333, 45117 },
  102. { 320441, 45929 },
  103. { 330705, 46729 },
  104. { 341131, 47518 },
  105. { 351728, 48297 },
  106. { 362501, 49066 },
  107. { 373460, 49826 },
  108. { 384609, 50577 },
  109. { 395958, 51320 },
  110. { 407513, 52054 },
  111. { 419281, 52780 },
  112. { 431270, 53499 },
  113. { 443487, 54211 },
  114. { 455940, 54916 },
  115. { 468635, 55614 },
  116. { 481581, 56306 },
  117. { 494785, 56991 },
  118. { 508254, 57671 },
  119. { 521996, 58345 },
  120. { 536019, 59014 },
  121. { 550331, 59677 },
  122. { 564939, 60335 },
  123. { 579851, 60988 },
  124. { 595075, 61636 },
  125. { 610619, 62279 },
  126. { 626491, 62918 },
  127. { 642700, 63553 },
  128. { 659253, 64183 },
  129. { 676158, 64809 },
  130. { 693424, 65431 },
  131. { 711060, 66050 },
  132. { 729073, 66664 },
  133. { 747472, 67275 },
  134. { 766266, 67882 },
  135. { 785464, 68486 },
  136. { 805073, 69087 },
  137. { 825103, 69684 },
  138. { 845562, 70278 },
  139. { 866460, 70868 },
  140. { 887805, 71456 },
  141. { 909606, 72041 },
  142. { 931873, 72623 },
  143. { 954614, 73202 },
  144. { 977839, 73778 },
  145. { 1001557, 74352 },
  146. { 1025777, 74923 },
  147. { 1050508, 75492 },
  148. { 1075761, 76058 },
  149. { 1101544, 76621 },
  150. { 1127867, 77183 },
  151. { 1154739, 77741 },
  152. { 1182172, 78298 },
  153. { 1210173, 78852 },
  154. { 1238753, 79405 },
  155. { 1267922, 79955 },
  156. { 1297689, 80503 },
  157. { 1328066, 81049 },
  158. { 1359060, 81593 },
  159. { 1390684, 82135 },
  160. { 1422947, 82675 },
  161. { 1455859, 83213 },
  162. { 1489430, 83750 },
  163. { 1523671, 84284 },
  164. { 1558593, 84817 },
  165. { 1594205, 85348 },
  166. { 1630518, 85878 },
  167. { 1667543, 86406 },
  168. { 1705290, 86932 },
  169. { 1743770, 87457 },
  170. { 1782994, 87980 },
  171. { 1822973, 88501 },
  172. { 1863717, 89021 },
  173. { 1905237, 89540 },
  174. { 1947545, 90057 },
  175. { 1990650, 90573 },
  176. { 2034566, 91087 },
  177. { 2079301, 91600 },
  178. { 2124869, 92111 },
  179. { 2171279, 92622 },
  180. { 2218543, 93131 },
  181. { 2266673, 93639 },
  182. { 2315680, 94145 },
  183. { 2365575, 94650 },
  184. { 2416371, 95154 },
  185. { 2468077, 95657 },
  186. { 2520707, 96159 },
  187. { 2574271, 96660 },
  188. { 2628782, 97159 },
  189. { 2684250, 97658 },
  190. { 2740689, 98155 },
  191. { 2798110, 98651 },
  192. { 2856524, 99147 },
  193. { 2915944, 99641 },
  194. { 2976382, 100134 },
  195. { 3037850, 100626 },
  196. { 3100360, 101117 },
  197. { 3163924, 101608 },
  198. { 3228554, 102097 },
  199. { 3294263, 102586 },
  200. { 3361063, 103073 },
  201. { 3428966, 103560 },
  202. { 3497984, 104045 },
  203. { 3568131, 104530 },
  204. { 3639419, 105014 },
  205. { 3711860, 105498 },
  206. { 3785467, 105980 },
  207. { 3860253, 106462 },
  208. { 3936229, 106942 },
  209. { 4013410, 107422 },
  210. { 4091808, 107902 },
  211. { 4171435, 108380 },
  212. { 4252306, 108858 },
  213. { 4334431, 109335 },
  214. { 4417825, 109811 },
  215. { 4502501, 110287 },
  216. { 4588472, 110762 },
  217. { 4675750, 111236 },
  218. { 4764349, 111709 },
  219. { 4854283, 112182 },
  220. { 4945564, 112654 },
  221. { 5038206, 113126 },
  222. { 5132223, 113597 },
  223. { 5227627, 114067 },
  224. { 5324432, 114537 },
  225. { 5422652, 115006 },
  226. { 5522299, 115474 },
  227. { 5623389, 115942 },
  228. { 5725934, 116409 },
  229. { 5829948, 116876 },
  230. { 5935446, 117342 },
  231. { 6042439, 117808 },
  232. { 6150943, 118273 },
  233. { 6260972, 118738 },
  234. { 6372538, 119202 },
  235. { 6485657, 119665 },
  236. { 6600342, 120128 },
  237. { 6716607, 120591 },
  238. { 6834467, 121053 },
  239. { 6953935, 121514 },
  240. { 7075025, 121976 },
  241. { 7197752, 122436 },
  242. { 7322131, 122896 },
  243. { 7448175, 123356 },
  244. { 7575898, 123815 },
  245. { 7705316, 124274 },
  246. { 7836442, 124733 },
  247. { 7969291, 125191 },
  248. { 8103877, 125648 },
  249. { 8240216, 126105 },
  250. { 8378321, 126562 },
  251. { 8518208, 127018 },
  252. { 8659890, 127474 },
  253. { 8803384, 127930 },
  254. { 8948702, 128385 },
  255. { 9095861, 128840 },
  256. { 9244875, 129294 },
  257. { 9395760, 129748 },
  258. { 9548529, 130202 },
  259. { 9703198, 130655 },
  260. { 9859782, 131108 },
  261. { 10018296, 131561 },
  262. { 10178755, 132014 },
  263. { 10341174, 132466 },
  264. { 10505569, 132917 },
  265. { 10671954, 133369 },
  266. { 10840345, 133820 },
  267. { 11010757, 134271 },
  268. { 11183206, 134721 },
  269. { 11357706, 135171 },
  270. { 11534274, 135621 },
  271. { 11712924, 136071 },
  272. { 11893673, 136520 },
  273. { 12076536, 136969 },
  274. { 12261527, 137418 },
  275. { 12448664, 137867 },
  276. { 12637961, 138315 },
  277. { 12829435, 138763 },
  278. { 13023101, 139211 },
  279. { 13218974, 139658 },
  280. { 13417071, 140106 },
  281. { 13617407, 140553 },
  282. { 13819999, 140999 },
  283. { 14024862, 141446 },
  284. { 14232012, 141892 },
  285. { 14441465, 142339 },
  286. { 14653238, 142785 },
  287. { 14867346, 143230 },
  288. { 15083805, 143676 },
  289. { 15302632, 144121 },
  290. { 15523842, 144566 },
  291. { 15747453, 145011 },
  292. { 15973479, 145456 },
  293. { 16201939, 145900 },
  294. { 16432847, 146345 },
  295. { 16666221, 146789 },
  296. { 16902076, 147233 },
  297. { 17140429, 147677 },
  298. { 17381297, 148121 },
  299. { 17624696, 148564 },
  300. { 17870643, 149007 },
  301. { 18119154, 149451 },
  302. { 18370247, 149894 },
  303. { 18623936, 150336 },
  304. { 18880241, 150779 },
  305. { 19139176, 151222 },
  306. { 19400759, 151664 },
  307. { 19665007, 152107 },
  308. { 19931936, 152549 },
  309. { 20201564, 152991 },
  310. { 20473907, 153433 },
  311. { 20748982, 153875 },
  312. { 21026807, 154316 },
  313. { 21307399, 154758 },
  314. { 21590773, 155199 },
  315. { 21876949, 155641 },
  316. { 22165941, 156082 },
  317. { 22457769, 156523 },
  318. { 22752449, 156964 },
  319. { 23049999, 157405 },
  320. { 23350435, 157846 },
  321. { 23653774, 158287 },
  322. { 23960036, 158727 },
  323. { 24269236, 159168 },
  324. { 24581392, 159608 },
  325. { 24896521, 160049 },
  326. { 25214642, 160489 },
  327. { 25535772, 160929 },
  328. { 25859927, 161370 },
  329. { 26187127, 161810 },
  330. { 26517388, 162250 },
  331. { 26850728, 162690 },
  332. { 27187165, 163130 },
  333. { 27526716, 163569 },
  334. { 27869400, 164009 },
  335. { 28215234, 164449 },
  336. { 28564236, 164889 },
  337. { 28916423, 165328 },
  338. { 29271815, 165768 },
  339. { 29630428, 166208 },
  340. { 29992281, 166647 },
  341. { 30357392, 167087 },
  342. { 30725779, 167526 },
  343. { 31097459, 167965 },
  344. { 31472452, 168405 },
  345. { 31850774, 168844 },
  346. { 32232445, 169283 },
  347. { 32617482, 169723 },
  348. { 33005904, 170162 },
  349. { 33397730, 170601 },
  350. { 33792976, 171041 },
  351. { 34191663, 171480 },
  352. { 34593807, 171919 },
  353. { 34999428, 172358 },
  354. { 35408544, 172797 },
  355. { 35821174, 173237 },
  356. { 36237335, 173676 },
  357. { 36657047, 174115 },
  358. { 37080329, 174554 },
  359. { 37507197, 174993 },
  360. { 37937673, 175433 },
  361. { 38371773, 175872 },
  362. { 38809517, 176311 },
  363. { 39250924, 176750 },
  364. { 39696012, 177190 },
  365. { 40144800, 177629 },
  366. { 40597308, 178068 },
  367. { 41053553, 178507 },
  368. { 41513554, 178947 },
  369. { 41977332, 179386 },
  370. { 42444904, 179825 },
  371. { 42916290, 180265 },
  372. { 43391509, 180704 },
  373. { 43870579, 181144 },
  374. { 44353520, 181583 },
  375. { 44840352, 182023 },
  376. { 45331092, 182462 },
  377. { 45825761, 182902 },
  378. { 46324378, 183342 },
  379. { 46826961, 183781 },
  380. { 47333531, 184221 },
  381. { 47844106, 184661 },
  382. { 48358706, 185101 },
  383. { 48877350, 185541 },
  384. { 49400058, 185981 },
  385. { 49926849, 186421 },
  386. { 50457743, 186861 },
  387. { 50992759, 187301 },
  388. { 51531916, 187741 },
  389. { 52075235, 188181 },
  390. { 52622735, 188622 },
  391. { 53174435, 189062 },
  392. { 53730355, 189502 },
  393. { 54290515, 189943 },
  394. { 54854935, 190383 },
  395. { 55423634, 190824 },
  396. { 55996633, 191265 },
  397. { 56573950, 191706 },
  398. { 57155606, 192146 },
  399. { 57741621, 192587 },
  400. { 58332014, 193028 },
  401. { 58926806, 193470 },
  402. { 59526017, 193911 },
  403. { 60129666, 194352 },
  404. { 60737774, 194793 },
  405. { 61350361, 195235 },
  406. { 61967446, 195677 },
  407. { 62589050, 196118 },
  408. { 63215194, 196560 },
  409. { 63845897, 197002 },
  410. { 64481179, 197444 },
  411. { 65121061, 197886 },
  412. { 65765563, 198328 },
  413. { 66414705, 198770 },
  414. { 67068508, 199213 },
  415. { 67726992, 199655 },
  416. { 68390177, 200098 },
  417. { 69058085, 200540 },
  418. { 69730735, 200983 },
  419. { 70408147, 201426 },
  420. { 71090343, 201869 },
  421. { 71777343, 202312 },
  422. { 72469168, 202755 },
  423. { 73165837, 203199 },
  424. { 73867373, 203642 },
  425. { 74573795, 204086 },
  426. { 75285124, 204529 },
  427. { 76001380, 204973 },
  428. { 76722586, 205417 },
  429. { 77448761, 205861 },
  430. { 78179926, 206306 },
  431. { 78916102, 206750 },
  432. { 79657310, 207194 },
  433. { 80403571, 207639 },
  434. { 81154906, 208084 },
  435. { 81911335, 208529 },
  436. { 82672880, 208974 },
  437. { 83439562, 209419 },
  438. { 84211402, 209864 },
  439. { 84988421, 210309 },
  440. { 85770640, 210755 },
  441. { 86558080, 211201 },
  442. { 87350762, 211647 },
  443. { 88148708, 212093 },
  444. { 88951938, 212539 },
  445. { 89760475, 212985 },
  446. { 90574339, 213432 },
  447. { 91393551, 213878 },
  448. { 92218133, 214325 },
  449. { 93048107, 214772 },
  450. { 93883493, 215219 },
  451. { 94724314, 215666 },
  452. { 95570590, 216114 },
  453. { 96422343, 216561 },
  454. { 97279594, 217009 },
  455. { 98142366, 217457 },
  456. { 99010679, 217905 },
  457. { 99884556, 218353 },
  458. { 100764018, 218801 },
  459. { 101649086, 219250 },
  460. { 102539782, 219698 },
  461. { 103436128, 220147 },
  462. { 104338146, 220596 },
  463. { 105245857, 221046 },
  464. { 106159284, 221495 },
  465. { 107078448, 221945 },
  466. { 108003370, 222394 },
  467. { 108934074, 222844 },
  468. { 109870580, 223294 },
  469. { 110812910, 223745 },
  470. { 111761087, 224195 },
  471. { 112715133, 224646 },
  472. { 113675069, 225097 },
  473. { 114640918, 225548 },
  474. { 115612702, 225999 },
  475. { 116590442, 226450 },
  476. { 117574162, 226902 },
  477. { 118563882, 227353 },
  478. { 119559626, 227805 },
  479. { 120561415, 228258 },
  480. { 121569272, 228710 },
  481. { 122583219, 229162 },
  482. { 123603278, 229615 },
  483. { 124629471, 230068 },
  484. { 125661822, 230521 },
  485. { 126700352, 230974 },
  486. { 127745083, 231428 },
  487. { 128796039, 231882 },
  488. { 129853241, 232336 },
  489. { 130916713, 232790 },
  490. { 131986475, 233244 },
  491. { 133062553, 233699 },
  492. { 134144966, 234153 },
  493. { 135233739, 234608 },
  494. { 136328894, 235064 },
  495. { 137430453, 235519 },
  496. { 138538440, 235975 },
  497. { 139652876, 236430 },
  498. { 140773786, 236886 },
  499. { 141901190, 237343 },
  500. { 143035113, 237799 },
  501. { 144175576, 238256 },
  502. { 145322604, 238713 },
  503. { 146476218, 239170 },
  504. { 147636442, 239627 },
  505. { 148803298, 240085 },
  506. { 149976809, 240542 },
  507. { 151156999, 241000 },
  508. { 152343890, 241459 },
  509. { 153537506, 241917 },
  510. { 154737869, 242376 },
  511. { 155945002, 242835 },
  512. { 157158929, 243294 },
  513. { 158379673, 243753 },
  514. { 159607257, 244213 },
  515. { 160841704, 244673 },
  516. { 162083037, 245133 },
  517. { 163331279, 245593 },
  518. { 164586455, 246054 },
  519. { 165848586, 246514 },
  520. { 167117696, 246975 },
  521. { 168393810, 247437 },
  522. { 169676949, 247898 },
  523. { 170967138, 248360 },
  524. { 172264399, 248822 },
  525. { 173568757, 249284 },
  526. { 174880235, 249747 },
  527. { 176198856, 250209 },
  528. { 177524643, 250672 },
  529. { 178857621, 251136 },
  530. { 180197813, 251599 },
  531. { 181545242, 252063 },
  532. { 182899933, 252527 },
  533. { 184261908, 252991 },
  534. { 185631191, 253456 },
  535. { 187007807, 253920 },
  536. { 188391778, 254385 },
  537. { 189783129, 254851 },
  538. { 191181884, 255316 },
  539. { 192588065, 255782 },
  540. { 194001698, 256248 },
  541. { 195422805, 256714 },
  542. { 196851411, 257181 },
  543. { 198287540, 257648 },
  544. { 199731215, 258115 },
  545. { 201182461, 258582 },
  546. { 202641302, 259050 },
  547. { 204107760, 259518 },
  548. { 205581862, 259986 },
  549. { 207063630, 260454 },
  550. { 208553088, 260923 },
  551. { 210050262, 261392 },
  552. { 211555174, 261861 },
  553. { 213067849, 262331 },
  554. { 214588312, 262800 },
  555. { 216116586, 263270 },
  556. { 217652696, 263741 },
  557. { 219196666, 264211 },
  558. { 220748520, 264682 },
  559. { 222308282, 265153 },
  560. { 223875978, 265625 },
  561. { 225451630, 266097 },
  562. { 227035265, 266569 },
  563. { 228626905, 267041 },
  564. { 230226576, 267514 },
  565. { 231834302, 267986 },
  566. { 233450107, 268460 },
  567. { 235074016, 268933 },
  568. { 236706054, 269407 },
  569. { 238346244, 269881 },
  570. { 239994613, 270355 },
  571. { 241651183, 270830 },
  572. { 243315981, 271305 }
  573. };
  574. /* return largest index i such that fval <= lookup[i][small] */
  575. static inline u32 tfrc_binsearch(u32 fval, u8 small)
  576. {
  577. u32 try, low = 0, high = TFRC_CALC_X_ARRSIZE - 1;
  578. while (low < high) {
  579. try = (low + high) / 2;
  580. if (fval <= tfrc_calc_x_lookup[try][small])
  581. high = try;
  582. else
  583. low = try + 1;
  584. }
  585. return high;
  586. }
  587. /**
  588. * tfrc_calc_x - Calculate the send rate as per section 3.1 of RFC3448
  589. * @s: packet size in bytes
  590. * @R: RTT scaled by 1000000 (i.e., microseconds)
  591. * @p: loss ratio estimate scaled by 1000000
  592. *
  593. * Returns X_calc in bytes per second (not scaled).
  594. */
  595. u32 tfrc_calc_x(u16 s, u32 R, u32 p)
  596. {
  597. u16 index;
  598. u32 f;
  599. u64 result;
  600. /* check against invalid parameters and divide-by-zero */
  601. BUG_ON(p > 1000000); /* p must not exceed 100% */
  602. BUG_ON(p == 0); /* f(0) = 0, divide by zero */
  603. if (R == 0) { /* possible divide by zero */
  604. DCCP_CRIT("WARNING: RTT is 0, returning maximum X_calc.");
  605. return ~0U;
  606. }
  607. if (p <= TFRC_CALC_X_SPLIT) { /* 0.0000 < p <= 0.05 */
  608. if (p < TFRC_SMALLEST_P) { /* 0.0000 < p < 0.0001 */
  609. DCCP_WARN("Value of p (%d) below resolution. "
  610. "Substituting %d\n", p, TFRC_SMALLEST_P);
  611. index = 0;
  612. } else /* 0.0001 <= p <= 0.05 */
  613. index = p/TFRC_SMALLEST_P - 1;
  614. f = tfrc_calc_x_lookup[index][1];
  615. } else { /* 0.05 < p <= 1.00 */
  616. index = p/(1000000/TFRC_CALC_X_ARRSIZE) - 1;
  617. f = tfrc_calc_x_lookup[index][0];
  618. }
  619. /*
  620. * Compute X = s/(R*f(p)) in bytes per second.
  621. * Since f(p) and R are both scaled by 1000000, we need to multiply by
  622. * 1000000^2. To avoid overflow, the result is computed in two stages.
  623. * This works under almost all reasonable operational conditions, for a
  624. * wide range of parameters. Yet, should some strange combination of
  625. * parameters result in overflow, the use of scaled_div32 will catch
  626. * this and return UINT_MAX - which is a logically adequate consequence.
  627. */
  628. result = scaled_div(s, R);
  629. return scaled_div32(result, f);
  630. }
  631. /**
  632. * tfrc_calc_x_reverse_lookup - try to find p given f(p)
  633. * @fvalue: function value to match, scaled by 1000000
  634. *
  635. * Returns closest match for p, also scaled by 1000000
  636. */
  637. u32 tfrc_calc_x_reverse_lookup(u32 fvalue)
  638. {
  639. int index;
  640. if (fvalue == 0) /* f(p) = 0 whenever p = 0 */
  641. return 0;
  642. /* Error cases. */
  643. if (fvalue < tfrc_calc_x_lookup[0][1]) {
  644. DCCP_WARN("fvalue %u smaller than resolution\n", fvalue);
  645. return TFRC_SMALLEST_P;
  646. }
  647. if (fvalue > tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE - 1][0]) {
  648. DCCP_WARN("fvalue %u exceeds bounds!\n", fvalue);
  649. return 1000000;
  650. }
  651. if (fvalue <= tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE - 1][1]) {
  652. index = tfrc_binsearch(fvalue, 1);
  653. return (index + 1) * TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE;
  654. }
  655. /* else ... it must be in the coarse-grained column */
  656. index = tfrc_binsearch(fvalue, 0);
  657. return (index + 1) * 1000000 / TFRC_CALC_X_ARRSIZE;
  658. }
  659. /**
  660. * tfrc_invert_loss_event_rate - Compute p so that 10^6 corresponds to 100%
  661. * When @loss_event_rate is large, there is a chance that p is truncated to 0.
  662. * To avoid re-entering slow-start in that case, we set p = TFRC_SMALLEST_P > 0.
  663. */
  664. u32 tfrc_invert_loss_event_rate(u32 loss_event_rate)
  665. {
  666. if (loss_event_rate == UINT_MAX) /* see RFC 4342, 8.5 */
  667. return 0;
  668. if (unlikely(loss_event_rate == 0)) /* map 1/0 into 100% */
  669. return 1000000;
  670. return max_t(u32, scaled_div(1, loss_event_rate), TFRC_SMALLEST_P);
  671. }