2 * net/dccp/ccids/lib/tfrc_equation.c
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005 Ian McDonald <ian.mcdonald@jandi.co.nz>
6 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/module.h>
16 #include "../../dccp.h"
19 #define TFRC_CALC_X_ARRSIZE 500
20 #define TFRC_CALC_X_SPLIT 50000 /* 0.05 * 1000000, details below */
21 #define TFRC_SMALLEST_P (TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE)
24 TFRC TCP Reno Throughput Equation Lookup Table for f(p)
26 The following two-column lookup table implements a part of the TCP throughput
27 equation from [RFC 3448, sec. 3.1]:
30 X_calc = --------------------------------------------------------------
31 R * sqrt(2*b*p/3) + (3 * t_RTO * sqrt(3*b*p/8) * (p + 32*p^3))
34 X is the transmit rate in bytes/second
35 s is the packet size in bytes
36 R is the round trip time in seconds
37 p is the loss event rate, between 0 and 1.0, of the number of loss
38 events as a fraction of the number of packets transmitted
39 t_RTO is the TCP retransmission timeout value in seconds
40 b is the number of packets acknowledged by a single TCP ACK
42 We can assume that b = 1 and t_RTO is 4 * R. The equation now becomes:
45 X_calc = -------------------------------------------------------
46 R * sqrt(p*2/3) + (12 * R * sqrt(p*3/8) * (p + 32*p^3))
48 which we can break down into:
54 where f(p) is given for 0 < p <= 1 by:
56 f(p) = sqrt(2*p/3) + 12 * sqrt(3*p/8) * (p + 32*p^3)
58 Since this is kernel code, floating-point arithmetic is avoided in favour of
59 integer arithmetic. This means that nearly all fractional parameters are
61 * the parameters p and R
62 * the return result f(p)
63 The lookup table therefore actually tabulates the following function g(q):
65 g(q) = 1000000 * f(q/1000000)
67 Hence, when p <= 1, q must be less than or equal to 1000000. To achieve finer
68 granularity for the practically more relevant case of small values of p (up to
69 5%), the second column is used; the first one ranges up to 100%. This split
70 corresponds to the value of q = TFRC_CALC_X_SPLIT. At the same time this also
71 determines the smallest resolution possible with this lookup table:
73 TFRC_SMALLEST_P = TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE
75 The entire table is generated by:
76 for(i=0; i < TFRC_CALC_X_ARRSIZE; i++) {
77 lookup[i][0] = g((i+1) * 1000000/TFRC_CALC_X_ARRSIZE);
78 lookup[i][1] = g((i+1) * TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE);
81 With the given configuration, we have, with M = TFRC_CALC_X_ARRSIZE-1,
82 lookup[0][0] = g(1000000/(M+1)) = 1000000 * f(0.2%)
83 lookup[M][0] = g(1000000) = 1000000 * f(100%)
84 lookup[0][1] = g(TFRC_SMALLEST_P) = 1000000 * f(0.01%)
85 lookup[M][1] = g(TFRC_CALC_X_SPLIT) = 1000000 * f(5%)
87 In summary, the two columns represent f(p) for the following ranges:
88 * The first column is for 0.002 <= p <= 1.0
89 * The second column is for 0.0001 <= p <= 0.05
90 Where the columns overlap, the second (finer-grained) is given preference,
91 i.e. the first column is used only for p >= 0.05.
93 static const u32 tfrc_calc_x_lookup
[TFRC_CALC_X_ARRSIZE
][2] = {
282 { 10018296, 131561 },
283 { 10178755, 132014 },
284 { 10341174, 132466 },
285 { 10505569, 132917 },
286 { 10671954, 133369 },
287 { 10840345, 133820 },
288 { 11010757, 134271 },
289 { 11183206, 134721 },
290 { 11357706, 135171 },
291 { 11534274, 135621 },
292 { 11712924, 136071 },
293 { 11893673, 136520 },
294 { 12076536, 136969 },
295 { 12261527, 137418 },
296 { 12448664, 137867 },
297 { 12637961, 138315 },
298 { 12829435, 138763 },
299 { 13023101, 139211 },
300 { 13218974, 139658 },
301 { 13417071, 140106 },
302 { 13617407, 140553 },
303 { 13819999, 140999 },
304 { 14024862, 141446 },
305 { 14232012, 141892 },
306 { 14441465, 142339 },
307 { 14653238, 142785 },
308 { 14867346, 143230 },
309 { 15083805, 143676 },
310 { 15302632, 144121 },
311 { 15523842, 144566 },
312 { 15747453, 145011 },
313 { 15973479, 145456 },
314 { 16201939, 145900 },
315 { 16432847, 146345 },
316 { 16666221, 146789 },
317 { 16902076, 147233 },
318 { 17140429, 147677 },
319 { 17381297, 148121 },
320 { 17624696, 148564 },
321 { 17870643, 149007 },
322 { 18119154, 149451 },
323 { 18370247, 149894 },
324 { 18623936, 150336 },
325 { 18880241, 150779 },
326 { 19139176, 151222 },
327 { 19400759, 151664 },
328 { 19665007, 152107 },
329 { 19931936, 152549 },
330 { 20201564, 152991 },
331 { 20473907, 153433 },
332 { 20748982, 153875 },
333 { 21026807, 154316 },
334 { 21307399, 154758 },
335 { 21590773, 155199 },
336 { 21876949, 155641 },
337 { 22165941, 156082 },
338 { 22457769, 156523 },
339 { 22752449, 156964 },
340 { 23049999, 157405 },
341 { 23350435, 157846 },
342 { 23653774, 158287 },
343 { 23960036, 158727 },
344 { 24269236, 159168 },
345 { 24581392, 159608 },
346 { 24896521, 160049 },
347 { 25214642, 160489 },
348 { 25535772, 160929 },
349 { 25859927, 161370 },
350 { 26187127, 161810 },
351 { 26517388, 162250 },
352 { 26850728, 162690 },
353 { 27187165, 163130 },
354 { 27526716, 163569 },
355 { 27869400, 164009 },
356 { 28215234, 164449 },
357 { 28564236, 164889 },
358 { 28916423, 165328 },
359 { 29271815, 165768 },
360 { 29630428, 166208 },
361 { 29992281, 166647 },
362 { 30357392, 167087 },
363 { 30725779, 167526 },
364 { 31097459, 167965 },
365 { 31472452, 168405 },
366 { 31850774, 168844 },
367 { 32232445, 169283 },
368 { 32617482, 169723 },
369 { 33005904, 170162 },
370 { 33397730, 170601 },
371 { 33792976, 171041 },
372 { 34191663, 171480 },
373 { 34593807, 171919 },
374 { 34999428, 172358 },
375 { 35408544, 172797 },
376 { 35821174, 173237 },
377 { 36237335, 173676 },
378 { 36657047, 174115 },
379 { 37080329, 174554 },
380 { 37507197, 174993 },
381 { 37937673, 175433 },
382 { 38371773, 175872 },
383 { 38809517, 176311 },
384 { 39250924, 176750 },
385 { 39696012, 177190 },
386 { 40144800, 177629 },
387 { 40597308, 178068 },
388 { 41053553, 178507 },
389 { 41513554, 178947 },
390 { 41977332, 179386 },
391 { 42444904, 179825 },
392 { 42916290, 180265 },
393 { 43391509, 180704 },
394 { 43870579, 181144 },
395 { 44353520, 181583 },
396 { 44840352, 182023 },
397 { 45331092, 182462 },
398 { 45825761, 182902 },
399 { 46324378, 183342 },
400 { 46826961, 183781 },
401 { 47333531, 184221 },
402 { 47844106, 184661 },
403 { 48358706, 185101 },
404 { 48877350, 185541 },
405 { 49400058, 185981 },
406 { 49926849, 186421 },
407 { 50457743, 186861 },
408 { 50992759, 187301 },
409 { 51531916, 187741 },
410 { 52075235, 188181 },
411 { 52622735, 188622 },
412 { 53174435, 189062 },
413 { 53730355, 189502 },
414 { 54290515, 189943 },
415 { 54854935, 190383 },
416 { 55423634, 190824 },
417 { 55996633, 191265 },
418 { 56573950, 191706 },
419 { 57155606, 192146 },
420 { 57741621, 192587 },
421 { 58332014, 193028 },
422 { 58926806, 193470 },
423 { 59526017, 193911 },
424 { 60129666, 194352 },
425 { 60737774, 194793 },
426 { 61350361, 195235 },
427 { 61967446, 195677 },
428 { 62589050, 196118 },
429 { 63215194, 196560 },
430 { 63845897, 197002 },
431 { 64481179, 197444 },
432 { 65121061, 197886 },
433 { 65765563, 198328 },
434 { 66414705, 198770 },
435 { 67068508, 199213 },
436 { 67726992, 199655 },
437 { 68390177, 200098 },
438 { 69058085, 200540 },
439 { 69730735, 200983 },
440 { 70408147, 201426 },
441 { 71090343, 201869 },
442 { 71777343, 202312 },
443 { 72469168, 202755 },
444 { 73165837, 203199 },
445 { 73867373, 203642 },
446 { 74573795, 204086 },
447 { 75285124, 204529 },
448 { 76001380, 204973 },
449 { 76722586, 205417 },
450 { 77448761, 205861 },
451 { 78179926, 206306 },
452 { 78916102, 206750 },
453 { 79657310, 207194 },
454 { 80403571, 207639 },
455 { 81154906, 208084 },
456 { 81911335, 208529 },
457 { 82672880, 208974 },
458 { 83439562, 209419 },
459 { 84211402, 209864 },
460 { 84988421, 210309 },
461 { 85770640, 210755 },
462 { 86558080, 211201 },
463 { 87350762, 211647 },
464 { 88148708, 212093 },
465 { 88951938, 212539 },
466 { 89760475, 212985 },
467 { 90574339, 213432 },
468 { 91393551, 213878 },
469 { 92218133, 214325 },
470 { 93048107, 214772 },
471 { 93883493, 215219 },
472 { 94724314, 215666 },
473 { 95570590, 216114 },
474 { 96422343, 216561 },
475 { 97279594, 217009 },
476 { 98142366, 217457 },
477 { 99010679, 217905 },
478 { 99884556, 218353 },
479 { 100764018, 218801 },
480 { 101649086, 219250 },
481 { 102539782, 219698 },
482 { 103436128, 220147 },
483 { 104338146, 220596 },
484 { 105245857, 221046 },
485 { 106159284, 221495 },
486 { 107078448, 221945 },
487 { 108003370, 222394 },
488 { 108934074, 222844 },
489 { 109870580, 223294 },
490 { 110812910, 223745 },
491 { 111761087, 224195 },
492 { 112715133, 224646 },
493 { 113675069, 225097 },
494 { 114640918, 225548 },
495 { 115612702, 225999 },
496 { 116590442, 226450 },
497 { 117574162, 226902 },
498 { 118563882, 227353 },
499 { 119559626, 227805 },
500 { 120561415, 228258 },
501 { 121569272, 228710 },
502 { 122583219, 229162 },
503 { 123603278, 229615 },
504 { 124629471, 230068 },
505 { 125661822, 230521 },
506 { 126700352, 230974 },
507 { 127745083, 231428 },
508 { 128796039, 231882 },
509 { 129853241, 232336 },
510 { 130916713, 232790 },
511 { 131986475, 233244 },
512 { 133062553, 233699 },
513 { 134144966, 234153 },
514 { 135233739, 234608 },
515 { 136328894, 235064 },
516 { 137430453, 235519 },
517 { 138538440, 235975 },
518 { 139652876, 236430 },
519 { 140773786, 236886 },
520 { 141901190, 237343 },
521 { 143035113, 237799 },
522 { 144175576, 238256 },
523 { 145322604, 238713 },
524 { 146476218, 239170 },
525 { 147636442, 239627 },
526 { 148803298, 240085 },
527 { 149976809, 240542 },
528 { 151156999, 241000 },
529 { 152343890, 241459 },
530 { 153537506, 241917 },
531 { 154737869, 242376 },
532 { 155945002, 242835 },
533 { 157158929, 243294 },
534 { 158379673, 243753 },
535 { 159607257, 244213 },
536 { 160841704, 244673 },
537 { 162083037, 245133 },
538 { 163331279, 245593 },
539 { 164586455, 246054 },
540 { 165848586, 246514 },
541 { 167117696, 246975 },
542 { 168393810, 247437 },
543 { 169676949, 247898 },
544 { 170967138, 248360 },
545 { 172264399, 248822 },
546 { 173568757, 249284 },
547 { 174880235, 249747 },
548 { 176198856, 250209 },
549 { 177524643, 250672 },
550 { 178857621, 251136 },
551 { 180197813, 251599 },
552 { 181545242, 252063 },
553 { 182899933, 252527 },
554 { 184261908, 252991 },
555 { 185631191, 253456 },
556 { 187007807, 253920 },
557 { 188391778, 254385 },
558 { 189783129, 254851 },
559 { 191181884, 255316 },
560 { 192588065, 255782 },
561 { 194001698, 256248 },
562 { 195422805, 256714 },
563 { 196851411, 257181 },
564 { 198287540, 257648 },
565 { 199731215, 258115 },
566 { 201182461, 258582 },
567 { 202641302, 259050 },
568 { 204107760, 259518 },
569 { 205581862, 259986 },
570 { 207063630, 260454 },
571 { 208553088, 260923 },
572 { 210050262, 261392 },
573 { 211555174, 261861 },
574 { 213067849, 262331 },
575 { 214588312, 262800 },
576 { 216116586, 263270 },
577 { 217652696, 263741 },
578 { 219196666, 264211 },
579 { 220748520, 264682 },
580 { 222308282, 265153 },
581 { 223875978, 265625 },
582 { 225451630, 266097 },
583 { 227035265, 266569 },
584 { 228626905, 267041 },
585 { 230226576, 267514 },
586 { 231834302, 267986 },
587 { 233450107, 268460 },
588 { 235074016, 268933 },
589 { 236706054, 269407 },
590 { 238346244, 269881 },
591 { 239994613, 270355 },
592 { 241651183, 270830 },
593 { 243315981, 271305 }
596 /* return largest index i such that fval <= lookup[i][small] */
597 static inline u32
tfrc_binsearch(u32 fval
, u8 small
)
599 u32
try, low
= 0, high
= TFRC_CALC_X_ARRSIZE
- 1;
602 try = (low
+ high
) / 2;
603 if (fval
<= tfrc_calc_x_lookup
[try][small
])
612 * tfrc_calc_x - Calculate the send rate as per section 3.1 of RFC3448
614 * @s: packet size in bytes
615 * @R: RTT scaled by 1000000 (i.e., microseconds)
616 * @p: loss ratio estimate scaled by 1000000
617 * Returns X_calc in bytes per second (not scaled).
619 u32
tfrc_calc_x(u16 s
, u32 R
, u32 p
)
625 /* check against invalid parameters and divide-by-zero */
626 BUG_ON(p
> 1000000); /* p must not exceed 100% */
627 BUG_ON(p
== 0); /* f(0) = 0, divide by zero */
628 if (R
== 0) { /* possible divide by zero */
629 DCCP_CRIT("WARNING: RTT is 0, returning maximum X_calc.");
633 if (p
<= TFRC_CALC_X_SPLIT
) { /* 0.0000 < p <= 0.05 */
634 if (p
< TFRC_SMALLEST_P
) { /* 0.0000 < p < 0.0001 */
635 DCCP_WARN("Value of p (%d) below resolution. "
636 "Substituting %d\n", p
, TFRC_SMALLEST_P
);
638 } else /* 0.0001 <= p <= 0.05 */
639 index
= p
/TFRC_SMALLEST_P
- 1;
641 f
= tfrc_calc_x_lookup
[index
][1];
643 } else { /* 0.05 < p <= 1.00 */
644 index
= p
/(1000000/TFRC_CALC_X_ARRSIZE
) - 1;
646 f
= tfrc_calc_x_lookup
[index
][0];
650 * Compute X = s/(R*f(p)) in bytes per second.
651 * Since f(p) and R are both scaled by 1000000, we need to multiply by
652 * 1000000^2. To avoid overflow, the result is computed in two stages.
653 * This works under almost all reasonable operational conditions, for a
654 * wide range of parameters. Yet, should some strange combination of
655 * parameters result in overflow, the use of scaled_div32 will catch
656 * this and return UINT_MAX - which is a logically adequate consequence.
658 result
= scaled_div(s
, R
);
659 return scaled_div32(result
, f
);
662 EXPORT_SYMBOL_GPL(tfrc_calc_x
);
665 * tfrc_calc_x_reverse_lookup - try to find p given f(p)
667 * @fvalue: function value to match, scaled by 1000000
668 * Returns closest match for p, also scaled by 1000000
670 u32
tfrc_calc_x_reverse_lookup(u32 fvalue
)
674 if (fvalue
== 0) /* f(p) = 0 whenever p = 0 */
678 if (fvalue
< tfrc_calc_x_lookup
[0][1]) {
679 DCCP_WARN("fvalue %d smaller than resolution\n", fvalue
);
680 return tfrc_calc_x_lookup
[0][1];
682 if (fvalue
> tfrc_calc_x_lookup
[TFRC_CALC_X_ARRSIZE
- 1][0]) {
683 DCCP_WARN("fvalue %d exceeds bounds!\n", fvalue
);
687 if (fvalue
<= tfrc_calc_x_lookup
[TFRC_CALC_X_ARRSIZE
- 1][1]) {
688 index
= tfrc_binsearch(fvalue
, 1);
689 return (index
+ 1) * TFRC_CALC_X_SPLIT
/ TFRC_CALC_X_ARRSIZE
;
692 /* else ... it must be in the coarse-grained column */
693 index
= tfrc_binsearch(fvalue
, 0);
694 return (index
+ 1) * 1000000 / TFRC_CALC_X_ARRSIZE
;
697 EXPORT_SYMBOL_GPL(tfrc_calc_x_reverse_lookup
);