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