]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * tc_red.c RED maintanance routines. | |
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 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
aba5acdf SH |
16 | #include <fcntl.h> |
17 | #include <math.h> | |
18 | #include <sys/socket.h> | |
19 | #include <netinet/in.h> | |
20 | #include <arpa/inet.h> | |
21 | #include <string.h> | |
22 | ||
23 | #include "tc_core.h" | |
24 | #include "tc_red.h" | |
25 | ||
26 | /* | |
27 | Plog = log(prob/(qmax - qmin)) | |
28 | */ | |
32a121cb | 29 | int tc_red_eval_P(unsigned int qmin, unsigned int qmax, double prob) |
aba5acdf SH |
30 | { |
31 | int i = qmax - qmin; | |
32 | ||
33 | if (i <= 0) | |
34 | return -1; | |
35 | ||
36 | prob /= i; | |
37 | ||
32a121cb | 38 | for (i = 0; i < 32; i++) { |
aba5acdf SH |
39 | if (prob > 1.0) |
40 | break; | |
41 | prob *= 2; | |
42 | } | |
32a121cb | 43 | if (i >= 32) |
aba5acdf SH |
44 | return -1; |
45 | return i; | |
46 | } | |
47 | ||
48 | /* | |
49 | burst + 1 - qmin/avpkt < (1-(1-W)^burst)/W | |
50 | */ | |
51 | ||
32a121cb | 52 | int tc_red_eval_ewma(unsigned int qmin, unsigned int burst, unsigned int avpkt) |
aba5acdf SH |
53 | { |
54 | int wlog = 1; | |
55 | double W = 0.5; | |
56 | double a = (double)burst + 1 - (double)qmin/avpkt; | |
57 | ||
0cf67ead | 58 | if (a < 1.0) { |
32a121cb SH |
59 | fprintf(stderr, "tc_red_eval_ewma() burst %u is too small ? Try burst %u\n", |
60 | burst, 1 + qmin/avpkt); | |
aba5acdf | 61 | return -1; |
0cf67ead | 62 | } |
32a121cb | 63 | for (wlog = 1; wlog < 32; wlog++, W /= 2) { |
aba5acdf SH |
64 | if (a <= (1 - pow(1-W, burst))/W) |
65 | return wlog; | |
66 | } | |
67 | return -1; | |
68 | } | |
69 | ||
70 | /* | |
71 | Stab[t>>Scell_log] = -log(1-W) * t/xmit_time | |
72 | */ | |
73 | ||
32a121cb | 74 | int tc_red_eval_idle_damping(int Wlog, unsigned int avpkt, unsigned int bps, __u8 *sbuf) |
aba5acdf | 75 | { |
476daa72 | 76 | double xmit_time = tc_calc_xmittime(bps, avpkt); |
aba5acdf SH |
77 | double lW = -log(1.0 - 1.0/(1<<Wlog))/xmit_time; |
78 | double maxtime = 31/lW; | |
79 | int clog; | |
80 | int i; | |
aba5acdf | 81 | |
32a121cb | 82 | for (clog = 0; clog < 32; clog++) { |
aba5acdf SH |
83 | if (maxtime/(1<<clog) < 512) |
84 | break; | |
85 | } | |
86 | if (clog >= 32) | |
87 | return -1; | |
88 | ||
89 | sbuf[0] = 0; | |
32a121cb | 90 | for (i = 1; i < 255; i++) { |
aba5acdf SH |
91 | sbuf[i] = (i<<clog)*lW; |
92 | if (sbuf[i] > 31) | |
93 | sbuf[i] = 31; | |
94 | } | |
95 | sbuf[255] = 31; | |
96 | return clog; | |
97 | } |