|
Packit |
7b22a4 |
/*
|
|
Packit |
7b22a4 |
* Copyright (c) 2008-2013 Patrick McHardy <kaber@trash.net>
|
|
Packit |
7b22a4 |
*/
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
#include <stdio.h>
|
|
Packit |
7b22a4 |
#include <string.h>
|
|
Packit |
7b22a4 |
#include <stdlib.h>
|
|
Packit |
7b22a4 |
#include <math.h>
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
#include <xtables.h>
|
|
Packit |
7b22a4 |
#include <linux/netfilter/x_tables.h>
|
|
Packit |
7b22a4 |
#include <linux/netfilter/xt_RATEEST.h>
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
struct rateest_tg_udata {
|
|
Packit |
7b22a4 |
unsigned int interval;
|
|
Packit |
7b22a4 |
unsigned int ewma_log;
|
|
Packit |
7b22a4 |
};
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void
|
|
Packit |
7b22a4 |
RATEEST_help(void)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
printf(
|
|
Packit |
7b22a4 |
"RATEEST target options:\n"
|
|
Packit |
7b22a4 |
" --rateest-name name Rate estimator name\n"
|
|
Packit |
7b22a4 |
" --rateest-interval sec Rate measurement interval in seconds\n"
|
|
Packit |
7b22a4 |
" --rateest-ewmalog value Rate measurement averaging time constant\n");
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
enum {
|
|
Packit |
7b22a4 |
O_NAME = 0,
|
|
Packit |
7b22a4 |
O_INTERVAL,
|
|
Packit |
7b22a4 |
O_EWMALOG,
|
|
Packit |
7b22a4 |
};
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
#define s struct xt_rateest_target_info
|
|
Packit |
7b22a4 |
static const struct xt_option_entry RATEEST_opts[] = {
|
|
Packit |
7b22a4 |
{.name = "rateest-name", .id = O_NAME, .type = XTTYPE_STRING,
|
|
Packit |
7b22a4 |
.flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name)},
|
|
Packit |
7b22a4 |
{.name = "rateest-interval", .id = O_INTERVAL, .type = XTTYPE_STRING,
|
|
Packit |
7b22a4 |
.flags = XTOPT_MAND},
|
|
Packit |
7b22a4 |
{.name = "rateest-ewmalog", .id = O_EWMALOG, .type = XTTYPE_STRING,
|
|
Packit |
7b22a4 |
.flags = XTOPT_MAND},
|
|
Packit |
7b22a4 |
XTOPT_TABLEEND,
|
|
Packit |
7b22a4 |
};
|
|
Packit |
7b22a4 |
#undef s
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
/* Copied from iproute */
|
|
Packit |
7b22a4 |
#define TIME_UNITS_PER_SEC 1000000
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static int
|
|
Packit |
7b22a4 |
RATEEST_get_time(unsigned int *time, const char *str)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
double t;
|
|
Packit |
7b22a4 |
char *p;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
t = strtod(str, &p);
|
|
Packit |
7b22a4 |
if (p == str)
|
|
Packit |
7b22a4 |
return -1;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
if (*p) {
|
|
Packit |
7b22a4 |
if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
|
|
Packit |
7b22a4 |
strcasecmp(p, "secs")==0)
|
|
Packit |
7b22a4 |
t *= TIME_UNITS_PER_SEC;
|
|
Packit |
7b22a4 |
else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
|
|
Packit |
7b22a4 |
strcasecmp(p, "msecs") == 0)
|
|
Packit |
7b22a4 |
t *= TIME_UNITS_PER_SEC/1000;
|
|
Packit |
7b22a4 |
else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
|
|
Packit |
7b22a4 |
strcasecmp(p, "usecs") == 0)
|
|
Packit |
7b22a4 |
t *= TIME_UNITS_PER_SEC/1000000;
|
|
Packit |
7b22a4 |
else
|
|
Packit |
7b22a4 |
return -1;
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
*time = t;
|
|
Packit |
7b22a4 |
return 0;
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void
|
|
Packit |
7b22a4 |
RATEEST_print_time(unsigned int time)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
double tmp = time;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
if (tmp >= TIME_UNITS_PER_SEC)
|
|
Packit |
7b22a4 |
printf(" %.1fs", tmp / TIME_UNITS_PER_SEC);
|
|
Packit |
7b22a4 |
else if (tmp >= TIME_UNITS_PER_SEC/1000)
|
|
Packit |
7b22a4 |
printf(" %.1fms", tmp / (TIME_UNITS_PER_SEC / 1000));
|
|
Packit |
7b22a4 |
else
|
|
Packit |
7b22a4 |
printf(" %uus", time);
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void RATEEST_parse(struct xt_option_call *cb)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
struct rateest_tg_udata *udata = cb->udata;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
xtables_option_parse(cb);
|
|
Packit |
7b22a4 |
switch (cb->entry->id) {
|
|
Packit |
7b22a4 |
case O_INTERVAL:
|
|
Packit |
7b22a4 |
if (RATEEST_get_time(&udata->interval, cb->arg) < 0)
|
|
Packit |
7b22a4 |
xtables_error(PARAMETER_PROBLEM,
|
|
Packit |
7b22a4 |
"RATEEST: bad interval value \"%s\"",
|
|
Packit |
7b22a4 |
cb->arg);
|
|
Packit |
7b22a4 |
break;
|
|
Packit |
7b22a4 |
case O_EWMALOG:
|
|
Packit |
7b22a4 |
if (RATEEST_get_time(&udata->ewma_log, cb->arg) < 0)
|
|
Packit |
7b22a4 |
xtables_error(PARAMETER_PROBLEM,
|
|
Packit |
7b22a4 |
"RATEEST: bad ewmalog value \"%s\"",
|
|
Packit |
7b22a4 |
cb->arg);
|
|
Packit |
7b22a4 |
break;
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void RATEEST_final_check(struct xt_fcheck_call *cb)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
struct xt_rateest_target_info *info = cb->data;
|
|
Packit |
7b22a4 |
struct rateest_tg_udata *udata = cb->udata;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
for (info->interval = 0; info->interval <= 5; info->interval++) {
|
|
Packit |
7b22a4 |
if (udata->interval <= (1 << info->interval) * (TIME_UNITS_PER_SEC / 4))
|
|
Packit |
7b22a4 |
break;
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
if (info->interval > 5)
|
|
Packit |
7b22a4 |
xtables_error(PARAMETER_PROBLEM,
|
|
Packit |
7b22a4 |
"RATEEST: interval value is too large");
|
|
Packit |
7b22a4 |
info->interval -= 2;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
for (info->ewma_log = 1; info->ewma_log < 32; info->ewma_log++) {
|
|
Packit |
7b22a4 |
double w = 1.0 - 1.0 / (1 << info->ewma_log);
|
|
Packit |
7b22a4 |
if (udata->interval / (-log(w)) > udata->ewma_log)
|
|
Packit |
7b22a4 |
break;
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
info->ewma_log--;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
if (info->ewma_log == 0 || info->ewma_log >= 31)
|
|
Packit |
7b22a4 |
xtables_error(PARAMETER_PROBLEM,
|
|
Packit |
7b22a4 |
"RATEEST: ewmalog value is out of range");
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void
|
|
Packit |
7b22a4 |
__RATEEST_print(const struct xt_entry_target *target, const char *prefix)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
const struct xt_rateest_target_info *info = (const void *)target->data;
|
|
Packit |
7b22a4 |
unsigned int local_interval;
|
|
Packit |
7b22a4 |
unsigned int local_ewma_log;
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
local_interval = (TIME_UNITS_PER_SEC << (info->interval + 2)) / 4;
|
|
Packit |
7b22a4 |
local_ewma_log = local_interval * (1 << (info->ewma_log));
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
printf(" %sname %s", prefix, info->name);
|
|
Packit |
7b22a4 |
printf(" %sinterval", prefix);
|
|
Packit |
7b22a4 |
RATEEST_print_time(local_interval);
|
|
Packit |
7b22a4 |
printf(" %sewmalog", prefix);
|
|
Packit |
7b22a4 |
RATEEST_print_time(local_ewma_log);
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void
|
|
Packit |
7b22a4 |
RATEEST_print(const void *ip, const struct xt_entry_target *target,
|
|
Packit |
7b22a4 |
int numeric)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
__RATEEST_print(target, "");
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static void
|
|
Packit |
7b22a4 |
RATEEST_save(const void *ip, const struct xt_entry_target *target)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
__RATEEST_print(target, "--rateest-");
|
|
Packit |
7b22a4 |
}
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
static struct xtables_target rateest_tg_reg = {
|
|
Packit |
7b22a4 |
.family = NFPROTO_UNSPEC,
|
|
Packit |
7b22a4 |
.name = "RATEEST",
|
|
Packit |
7b22a4 |
.version = XTABLES_VERSION,
|
|
Packit |
7b22a4 |
.size = XT_ALIGN(sizeof(struct xt_rateest_target_info)),
|
|
Packit |
7b22a4 |
.userspacesize = offsetof(struct xt_rateest_target_info, est),
|
|
Packit |
7b22a4 |
.help = RATEEST_help,
|
|
Packit |
7b22a4 |
.x6_parse = RATEEST_parse,
|
|
Packit |
7b22a4 |
.x6_fcheck = RATEEST_final_check,
|
|
Packit |
7b22a4 |
.print = RATEEST_print,
|
|
Packit |
7b22a4 |
.save = RATEEST_save,
|
|
Packit |
7b22a4 |
.x6_options = RATEEST_opts,
|
|
Packit |
7b22a4 |
.udata_size = sizeof(struct rateest_tg_udata),
|
|
Packit |
7b22a4 |
};
|
|
Packit |
7b22a4 |
|
|
Packit |
7b22a4 |
void _init(void)
|
|
Packit |
7b22a4 |
{
|
|
Packit |
7b22a4 |
xtables_register_target(&rateest_tg_reg);
|
|
Packit |
7b22a4 |
}
|