Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 4 | #include <math.h> |
| 5 | |
| 6 | #include <xtables.h> |
| 7 | #include <linux/netfilter/x_tables.h> |
| 8 | #include <linux/netfilter/xt_RATEEST.h> |
| 9 | |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 10 | struct rateest_tg_udata { |
| 11 | unsigned int interval; |
| 12 | unsigned int ewma_log; |
| 13 | }; |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 14 | |
| 15 | static void |
| 16 | RATEEST_help(void) |
| 17 | { |
| 18 | printf( |
Jan Engelhardt | bce1c21 | 2008-03-27 05:48:14 +0100 | [diff] [blame] | 19 | "RATEEST target options:\n" |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 20 | " --rateest-name name Rate estimator name\n" |
| 21 | " --rateest-interval sec Rate measurement interval in seconds\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 22 | " --rateest-ewmalog value Rate measurement averaging time constant\n"); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 25 | enum { |
| 26 | O_NAME = 0, |
| 27 | O_INTERVAL, |
| 28 | O_EWMALOG, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 31 | #define s struct xt_rateest_target_info |
| 32 | static const struct xt_option_entry RATEEST_opts[] = { |
| 33 | {.name = "rateest-name", .id = O_NAME, .type = XTTYPE_STRING, |
| 34 | .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name)}, |
| 35 | {.name = "rateest-interval", .id = O_INTERVAL, .type = XTTYPE_STRING, |
| 36 | .flags = XTOPT_MAND}, |
| 37 | {.name = "rateest-ewmalog", .id = O_EWMALOG, .type = XTTYPE_STRING, |
| 38 | .flags = XTOPT_MAND}, |
| 39 | XTOPT_TABLEEND, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 40 | }; |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 41 | #undef s |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 42 | |
| 43 | /* Copied from iproute */ |
| 44 | #define TIME_UNITS_PER_SEC 1000000 |
| 45 | |
| 46 | static int |
| 47 | RATEEST_get_time(unsigned int *time, const char *str) |
| 48 | { |
| 49 | double t; |
| 50 | char *p; |
| 51 | |
| 52 | t = strtod(str, &p); |
| 53 | if (p == str) |
| 54 | return -1; |
| 55 | |
| 56 | if (*p) { |
| 57 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || |
| 58 | strcasecmp(p, "secs")==0) |
| 59 | t *= TIME_UNITS_PER_SEC; |
| 60 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
| 61 | strcasecmp(p, "msecs") == 0) |
| 62 | t *= TIME_UNITS_PER_SEC/1000; |
| 63 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || |
| 64 | strcasecmp(p, "usecs") == 0) |
| 65 | t *= TIME_UNITS_PER_SEC/1000000; |
| 66 | else |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | *time = t; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | RATEEST_print_time(unsigned int time) |
| 76 | { |
| 77 | double tmp = time; |
| 78 | |
| 79 | if (tmp >= TIME_UNITS_PER_SEC) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 80 | printf(" %.1fs", tmp / TIME_UNITS_PER_SEC); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 81 | else if (tmp >= TIME_UNITS_PER_SEC/1000) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 82 | printf(" %.1fms", tmp / (TIME_UNITS_PER_SEC / 1000)); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 83 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 84 | printf(" %uus", time); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 87 | static void RATEEST_parse(struct xt_option_call *cb) |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 88 | { |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 89 | struct rateest_tg_udata *udata = cb->udata; |
| 90 | |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 91 | xtables_option_parse(cb); |
| 92 | switch (cb->entry->id) { |
| 93 | case O_INTERVAL: |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 94 | if (RATEEST_get_time(&udata->interval, cb->arg) < 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 95 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 96 | "RATEEST: bad interval value \"%s\"", |
| 97 | cb->arg); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 98 | break; |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 99 | case O_EWMALOG: |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 100 | if (RATEEST_get_time(&udata->ewma_log, cb->arg) < 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 101 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 102 | "RATEEST: bad ewmalog value \"%s\"", |
| 103 | cb->arg); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 104 | break; |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 105 | } |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 108 | static void RATEEST_final_check(struct xt_fcheck_call *cb) |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 109 | { |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 110 | struct xt_rateest_target_info *info = cb->data; |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 111 | struct rateest_tg_udata *udata = cb->udata; |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 112 | |
| 113 | for (info->interval = 0; info->interval <= 5; info->interval++) { |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 114 | if (udata->interval <= (1 << info->interval) * (TIME_UNITS_PER_SEC / 4)) |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (info->interval > 5) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 119 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 120 | "RATEEST: interval value is too large"); |
| 121 | info->interval -= 2; |
| 122 | |
| 123 | for (info->ewma_log = 1; info->ewma_log < 32; info->ewma_log++) { |
| 124 | double w = 1.0 - 1.0 / (1 << info->ewma_log); |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 125 | if (udata->interval / (-log(w)) > udata->ewma_log) |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | info->ewma_log--; |
| 129 | |
| 130 | if (info->ewma_log == 0 || info->ewma_log >= 31) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 131 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 132 | "RATEEST: ewmalog value is out of range"); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | __RATEEST_print(const struct xt_entry_target *target, const char *prefix) |
| 137 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 138 | const struct xt_rateest_target_info *info = (const void *)target->data; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 139 | unsigned int local_interval; |
| 140 | unsigned int local_ewma_log; |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 141 | |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 142 | local_interval = (TIME_UNITS_PER_SEC << (info->interval + 2)) / 4; |
| 143 | local_ewma_log = local_interval * (1 << (info->ewma_log)); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 144 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 145 | printf(" %sname %s", prefix, info->name); |
| 146 | printf(" %sinterval", prefix); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 147 | RATEEST_print_time(local_interval); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 148 | printf(" %sewmalog", prefix); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 149 | RATEEST_print_time(local_ewma_log); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void |
| 153 | RATEEST_print(const void *ip, const struct xt_entry_target *target, |
| 154 | int numeric) |
| 155 | { |
| 156 | __RATEEST_print(target, ""); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | RATEEST_save(const void *ip, const struct xt_entry_target *target) |
| 161 | { |
| 162 | __RATEEST_print(target, "--rateest-"); |
| 163 | } |
| 164 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 165 | static struct xtables_target rateest_tg_reg = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 166 | .family = NFPROTO_UNSPEC, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 167 | .name = "RATEEST", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 168 | .version = XTABLES_VERSION, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 169 | .size = XT_ALIGN(sizeof(struct xt_rateest_target_info)), |
| 170 | .userspacesize = XT_ALIGN(sizeof(struct xt_rateest_target_info)), |
| 171 | .help = RATEEST_help, |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 172 | .x6_parse = RATEEST_parse, |
| 173 | .x6_fcheck = RATEEST_final_check, |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 174 | .print = RATEEST_print, |
| 175 | .save = RATEEST_save, |
Jan Engelhardt | 68818f7 | 2011-06-21 14:20:15 +0200 | [diff] [blame] | 176 | .x6_options = RATEEST_opts, |
Jan Engelhardt | 12bc22a | 2011-06-21 14:22:20 +0200 | [diff] [blame] | 177 | .udata_size = sizeof(struct rateest_tg_udata), |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | void _init(void) |
| 181 | { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 182 | xtables_register_target(&rateest_tg_reg); |
Patrick McHardy | 2528258 | 2008-01-15 17:25:39 +0000 | [diff] [blame] | 183 | } |