blob: c40eea96f693d2d8ba51d7b770d53199f98a445c [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * tc_core.c TC core library.
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
Stephen Hemminger32a121c2016-03-21 11:48:36 -070026int tc_setup_estimator(unsigned int A, unsigned int time_const, struct tc_estimator *est)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000027{
Stephen Hemminger32a121c2016-03-21 11:48:36 -070028 for (est->interval = 0; est->interval <= 5; est->interval++) {
Patrick McHardyf0bda7e2007-03-04 20:14:59 +010029 if (A <= (1<<est->interval)*(TIME_UNITS_PER_SEC/4))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030 break;
31 }
32 if (est->interval > 5)
33 return -1;
34 est->interval -= 2;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070035 for (est->ewma_log = 1; est->ewma_log < 32; est->ewma_log++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036 double w = 1.0 - 1.0/(1<<est->ewma_log);
Stephen Hemminger32a121c2016-03-21 11:48:36 -070037
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038 if (A/(-log(w)) > time_const)
39 break;
40 }
41 est->ewma_log--;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070042 if (est->ewma_log == 0 || est->ewma_log >= 31)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043 return -1;
44 return 0;
45}