1aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger/*
2aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger * tc_core.c		TC core library.
3aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
4aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		This program is free software; you can redistribute it and/or
5aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		modify it under the terms of the GNU General Public License
6aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		as published by the Free Software Foundation; either version
7aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		2 of the License, or (at your option) any later version.
8aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
9aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
11aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger */
12aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
13aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <stdio.h>
14aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <stdlib.h>
15aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <unistd.h>
16aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <syslog.h>
17aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <fcntl.h>
18aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <math.h>
19aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <sys/socket.h>
20aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <netinet/in.h>
21aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <arpa/inet.h>
22aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <string.h>
23aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
24aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include "tc_core.h"
25aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
26aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerint tc_setup_estimator(unsigned A, unsigned time_const, struct tc_estimator *est)
27aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger{
28aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	for (est->interval=0; est->interval<=5; est->interval++) {
29f0bda7e5a52d880a85af698a041afa7dc379e76ePatrick McHardy		if (A <= (1<<est->interval)*(TIME_UNITS_PER_SEC/4))
30aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			break;
31aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	}
32aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (est->interval > 5)
33aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		return -1;
34aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	est->interval -= 2;
35aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	for (est->ewma_log=1; est->ewma_log<32; est->ewma_log++) {
36aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		double w = 1.0 - 1.0/(1<<est->ewma_log);
37aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		if (A/(-log(w)) > time_const)
38aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			break;
39aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	}
40aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	est->ewma_log--;
41aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (est->ewma_log==0 || est->ewma_log >= 31)
42aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		return -1;
43aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	return 0;
44aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger}
45