blob: e9b2f0ee3baa5db935e235895262f932aea587be [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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>
16#include <syslog.h>
17#include <fcntl.h>
18#include <math.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23
24#include "tc_core.h"
25#include "tc_red.h"
26
27/*
28 Plog = log(prob/(qmax - qmin))
29 */
Stephen Hemminger32a121c2016-03-21 11:48:36 -070030int tc_red_eval_P(unsigned int qmin, unsigned int qmax, double prob)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000031{
32 int i = qmax - qmin;
33
34 if (i <= 0)
35 return -1;
36
37 prob /= i;
38
Stephen Hemminger32a121c2016-03-21 11:48:36 -070039 for (i = 0; i < 32; i++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040 if (prob > 1.0)
41 break;
42 prob *= 2;
43 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070044 if (i >= 32)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045 return -1;
46 return i;
47}
48
49/*
50 burst + 1 - qmin/avpkt < (1-(1-W)^burst)/W
51 */
52
Stephen Hemminger32a121c2016-03-21 11:48:36 -070053int tc_red_eval_ewma(unsigned int qmin, unsigned int burst, unsigned int avpkt)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000054{
55 int wlog = 1;
56 double W = 0.5;
57 double a = (double)burst + 1 - (double)qmin/avpkt;
58
Eric Dumazet0cf67ea2011-12-01 12:04:31 +010059 if (a < 1.0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070060 fprintf(stderr, "tc_red_eval_ewma() burst %u is too small ? Try burst %u\n",
61 burst, 1 + qmin/avpkt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 return -1;
Eric Dumazet0cf67ea2011-12-01 12:04:31 +010063 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070064 for (wlog = 1; wlog < 32; wlog++, W /= 2) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000065 if (a <= (1 - pow(1-W, burst))/W)
66 return wlog;
67 }
68 return -1;
69}
70
71/*
72 Stab[t>>Scell_log] = -log(1-W) * t/xmit_time
73 */
74
Stephen Hemminger32a121c2016-03-21 11:48:36 -070075int tc_red_eval_idle_damping(int Wlog, unsigned int avpkt, unsigned int bps, __u8 *sbuf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000076{
Patrick McHardy476daa72007-03-04 20:14:56 +010077 double xmit_time = tc_calc_xmittime(bps, avpkt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000078 double lW = -log(1.0 - 1.0/(1<<Wlog))/xmit_time;
79 double maxtime = 31/lW;
80 int clog;
81 int i;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082
Stephen Hemminger32a121c2016-03-21 11:48:36 -070083 for (clog = 0; clog < 32; clog++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000084 if (maxtime/(1<<clog) < 512)
85 break;
86 }
87 if (clog >= 32)
88 return -1;
89
90 sbuf[0] = 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070091 for (i = 1; i < 255; i++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000092 sbuf[i] = (i<<clog)*lW;
93 if (sbuf[i] > 31)
94 sbuf[i] = 31;
95 }
96 sbuf[255] = 31;
97 return clog;
98}