Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 1 | #include <stdbool.h> |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stddef.h> |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include <xtables.h> |
| 9 | #include <linux/netfilter/xt_rateest.h> |
| 10 | |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 11 | static void rateest_help(void) |
| 12 | { |
| 13 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 14 | "rateest match options:\n" |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 15 | " --rateest1 name Rate estimator name\n" |
| 16 | " --rateest2 name Rate estimator name\n" |
| 17 | " --rateest-delta Compare difference(s) to given rate(s)\n" |
| 18 | " --rateest-bps1 [bps] Compare bps\n" |
| 19 | " --rateest-pps1 [pps] Compare pps\n" |
| 20 | " --rateest-bps2 [bps] Compare bps\n" |
| 21 | " --rateest-pps2 [pps] Compare pps\n" |
| 22 | " [!] --rateest-lt Match if rate is less than given rate/estimator\n" |
| 23 | " [!] --rateest-gt Match if rate is greater than given rate/estimator\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 24 | " [!] --rateest-eq Match if rate is equal to given rate/estimator\n"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | enum rateest_options { |
| 28 | OPT_RATEEST1, |
| 29 | OPT_RATEEST2, |
| 30 | OPT_RATEEST_BPS1, |
| 31 | OPT_RATEEST_PPS1, |
| 32 | OPT_RATEEST_BPS2, |
| 33 | OPT_RATEEST_PPS2, |
| 34 | OPT_RATEEST_DELTA, |
| 35 | OPT_RATEEST_LT, |
| 36 | OPT_RATEEST_GT, |
| 37 | OPT_RATEEST_EQ, |
| 38 | }; |
| 39 | |
| 40 | static const struct option rateest_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 41 | {.name = "rateest1", .has_arg = true, .val = OPT_RATEEST1}, |
| 42 | {.name = "rateest", .has_arg = true, .val = OPT_RATEEST1}, /* alias for absolute mode */ |
| 43 | {.name = "rateest2", .has_arg = true, .val = OPT_RATEEST2}, |
| 44 | {.name = "rateest-bps1", .has_arg = false, .val = OPT_RATEEST_BPS1}, |
| 45 | {.name = "rateest-pps1", .has_arg = false, .val = OPT_RATEEST_PPS1}, |
| 46 | {.name = "rateest-bps2", .has_arg = false, .val = OPT_RATEEST_BPS2}, |
| 47 | {.name = "rateest-pps2", .has_arg = false, .val = OPT_RATEEST_PPS2}, |
| 48 | {.name = "rateest-bps", .has_arg = false, .val = OPT_RATEEST_BPS2}, /* alias for absolute mode */ |
| 49 | {.name = "rateest-pps", .has_arg = false, .val = OPT_RATEEST_PPS2}, /* alias for absolute mode */ |
| 50 | {.name = "rateest-delta", .has_arg = false, .val = OPT_RATEEST_DELTA}, |
| 51 | {.name = "rateest-lt", .has_arg = false, .val = OPT_RATEEST_LT}, |
| 52 | {.name = "rateest-gt", .has_arg = false, .val = OPT_RATEEST_GT}, |
| 53 | {.name = "rateest-eq", .has_arg = false, .val = OPT_RATEEST_EQ}, |
| 54 | XT_GETOPT_TABLEEND, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* Copied from iproute. See http://physics.nist.gov/cuu/Units/binary.html */ |
| 58 | static const struct rate_suffix { |
| 59 | const char *name; |
| 60 | double scale; |
| 61 | } suffixes[] = { |
| 62 | { "bit", 1. }, |
| 63 | { "Kibit", 1024. }, |
| 64 | { "kbit", 1000. }, |
Jan Engelhardt | 463628b | 2011-05-12 17:36:25 +0200 | [diff] [blame] | 65 | { "Mibit", 1024.*1024. }, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 66 | { "mbit", 1000000. }, |
Jan Engelhardt | 463628b | 2011-05-12 17:36:25 +0200 | [diff] [blame] | 67 | { "Gibit", 1024.*1024.*1024. }, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 68 | { "gbit", 1000000000. }, |
Jan Engelhardt | 463628b | 2011-05-12 17:36:25 +0200 | [diff] [blame] | 69 | { "Tibit", 1024.*1024.*1024.*1024. }, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 70 | { "tbit", 1000000000000. }, |
| 71 | { "Bps", 8. }, |
| 72 | { "KiBps", 8.*1024. }, |
| 73 | { "KBps", 8000. }, |
| 74 | { "MiBps", 8.*1024*1024. }, |
| 75 | { "MBps", 8000000. }, |
| 76 | { "GiBps", 8.*1024.*1024.*1024. }, |
| 77 | { "GBps", 8000000000. }, |
| 78 | { "TiBps", 8.*1024.*1024.*1024.*1024. }, |
| 79 | { "TBps", 8000000000000. }, |
Jan Engelhardt | 104fb31 | 2011-05-07 04:01:25 +0200 | [diff] [blame] | 80 | {NULL}, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static int |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 84 | rateest_get_rate(uint32_t *rate, const char *str) |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 85 | { |
| 86 | char *p; |
| 87 | double bps = strtod(str, &p); |
| 88 | const struct rate_suffix *s; |
| 89 | |
| 90 | if (p == str) |
| 91 | return -1; |
| 92 | |
| 93 | if (*p == '\0') { |
| 94 | *rate = bps / 8.; /* assume bytes/sec */ |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | for (s = suffixes; s->name; ++s) { |
| 99 | if (strcasecmp(s->name, p) == 0) { |
| 100 | *rate = (bps * s->scale) / 8.; |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | rateest_parse(int c, char **argv, int invert, unsigned int *flags, |
| 110 | const void *entry, struct xt_entry_match **match) |
| 111 | { |
| 112 | struct xt_rateest_match_info *info = (void *)(*match)->data; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 113 | unsigned int val; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 114 | |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 115 | switch (c) { |
| 116 | case OPT_RATEEST1: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 117 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 118 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 119 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 120 | "rateest: rateest can't be inverted"); |
| 121 | |
| 122 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 123 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 124 | "rateest: can't specify --rateest1 twice"); |
| 125 | *flags |= 1 << c; |
| 126 | |
| 127 | strncpy(info->name1, optarg, sizeof(info->name1) - 1); |
| 128 | break; |
| 129 | |
| 130 | case OPT_RATEEST2: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 131 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 132 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 133 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 134 | "rateest: rateest can't be inverted"); |
| 135 | |
| 136 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 137 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 138 | "rateest: can't specify --rateest2 twice"); |
| 139 | *flags |= 1 << c; |
| 140 | |
| 141 | strncpy(info->name2, optarg, sizeof(info->name2) - 1); |
| 142 | info->flags |= XT_RATEEST_MATCH_REL; |
| 143 | break; |
| 144 | |
| 145 | case OPT_RATEEST_BPS1: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 146 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 147 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 148 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 149 | "rateest: rateest-bps can't be inverted"); |
| 150 | |
| 151 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 152 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 153 | "rateest: can't specify --rateest-bps1 twice"); |
| 154 | *flags |= 1 << c; |
| 155 | |
| 156 | info->flags |= XT_RATEEST_MATCH_BPS; |
| 157 | |
| 158 | /* The rate is optional and only required in absolute mode */ |
| 159 | if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!') |
| 160 | break; |
| 161 | |
| 162 | if (rateest_get_rate(&info->bps1, argv[optind]) < 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 163 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 164 | "rateest: could not parse rate `%s'", |
| 165 | argv[optind]); |
| 166 | optind++; |
| 167 | break; |
| 168 | |
| 169 | case OPT_RATEEST_PPS1: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 170 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 171 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 172 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 173 | "rateest: rateest-pps can't be inverted"); |
| 174 | |
| 175 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 176 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 177 | "rateest: can't specify --rateest-pps1 twice"); |
| 178 | *flags |= 1 << c; |
| 179 | |
| 180 | info->flags |= XT_RATEEST_MATCH_PPS; |
| 181 | |
| 182 | /* The rate is optional and only required in absolute mode */ |
| 183 | if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!') |
| 184 | break; |
| 185 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 186 | if (!xtables_strtoui(argv[optind], NULL, &val, 0, UINT32_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 187 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 188 | "rateest: could not parse pps `%s'", |
| 189 | argv[optind]); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 190 | info->pps1 = val; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 191 | optind++; |
| 192 | break; |
| 193 | |
| 194 | case OPT_RATEEST_BPS2: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 195 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 196 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 197 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 198 | "rateest: rateest-bps can't be inverted"); |
| 199 | |
| 200 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 201 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 202 | "rateest: can't specify --rateest-bps2 twice"); |
| 203 | *flags |= 1 << c; |
| 204 | |
| 205 | info->flags |= XT_RATEEST_MATCH_BPS; |
| 206 | |
| 207 | /* The rate is optional and only required in absolute mode */ |
| 208 | if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!') |
| 209 | break; |
| 210 | |
| 211 | if (rateest_get_rate(&info->bps2, argv[optind]) < 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 212 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 213 | "rateest: could not parse rate `%s'", |
| 214 | argv[optind]); |
| 215 | optind++; |
| 216 | break; |
| 217 | |
| 218 | case OPT_RATEEST_PPS2: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 219 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 220 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 221 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 222 | "rateest: rateest-pps can't be inverted"); |
| 223 | |
| 224 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 225 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 226 | "rateest: can't specify --rateest-pps2 twice"); |
| 227 | *flags |= 1 << c; |
| 228 | |
| 229 | info->flags |= XT_RATEEST_MATCH_PPS; |
| 230 | |
| 231 | /* The rate is optional and only required in absolute mode */ |
| 232 | if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!') |
| 233 | break; |
| 234 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 235 | if (!xtables_strtoui(argv[optind], NULL, &val, 0, UINT32_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 236 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 237 | "rateest: could not parse pps `%s'", |
| 238 | argv[optind]); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 239 | info->pps2 = val; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 240 | optind++; |
| 241 | break; |
| 242 | |
| 243 | case OPT_RATEEST_DELTA: |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 244 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 245 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 246 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 247 | "rateest: rateest-delta can't be inverted"); |
| 248 | |
| 249 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 250 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 251 | "rateest: can't specify --rateest-delta twice"); |
| 252 | *flags |= 1 << c; |
| 253 | |
| 254 | info->flags |= XT_RATEEST_MATCH_DELTA; |
| 255 | break; |
| 256 | |
| 257 | case OPT_RATEEST_EQ: |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 258 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 259 | |
| 260 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 261 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 262 | "rateest: can't specify lt/gt/eq twice"); |
| 263 | *flags |= 1 << c; |
| 264 | |
| 265 | info->mode = XT_RATEEST_MATCH_EQ; |
| 266 | if (invert) |
| 267 | info->flags |= XT_RATEEST_MATCH_INVERT; |
| 268 | break; |
| 269 | |
| 270 | case OPT_RATEEST_LT: |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 271 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 272 | |
| 273 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 274 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 275 | "rateest: can't specify lt/gt/eq twice"); |
| 276 | *flags |= 1 << c; |
| 277 | |
| 278 | info->mode = XT_RATEEST_MATCH_LT; |
| 279 | if (invert) |
| 280 | info->flags |= XT_RATEEST_MATCH_INVERT; |
| 281 | break; |
| 282 | |
| 283 | case OPT_RATEEST_GT: |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 284 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 285 | |
| 286 | if (*flags & (1 << c)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 287 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 288 | "rateest: can't specify lt/gt/eq twice"); |
| 289 | *flags |= 1 << c; |
| 290 | |
| 291 | info->mode = XT_RATEEST_MATCH_GT; |
| 292 | if (invert) |
| 293 | info->flags |= XT_RATEEST_MATCH_INVERT; |
| 294 | break; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return 1; |
| 298 | } |
| 299 | |
Jan Engelhardt | 4a96d2e | 2011-06-21 09:54:31 +0200 | [diff] [blame] | 300 | static void rateest_final_check(struct xt_fcheck_call *cb) |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 301 | { |
Jan Engelhardt | 4a96d2e | 2011-06-21 09:54:31 +0200 | [diff] [blame] | 302 | struct xt_rateest_match_info *info = cb->data; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 303 | |
Jan Engelhardt | c3d0a7b | 2008-12-30 12:03:39 +0100 | [diff] [blame] | 304 | if (info == NULL) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 305 | xtables_error(PARAMETER_PROBLEM, "rateest match: " |
Jan Engelhardt | c3d0a7b | 2008-12-30 12:03:39 +0100 | [diff] [blame] | 306 | "you need to specify some flags"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 307 | if (!(info->flags & XT_RATEEST_MATCH_REL)) |
| 308 | info->flags |= XT_RATEEST_MATCH_ABS; |
| 309 | } |
| 310 | |
| 311 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 312 | rateest_print_rate(uint32_t rate, int numeric) |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 313 | { |
| 314 | double tmp = (double)rate*8; |
| 315 | |
| 316 | if (numeric) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 317 | printf(" %u", rate); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 318 | else if (tmp >= 1000.0*1000000.0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 319 | printf(" %.0fMbit", tmp/1000000.0); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 320 | else if (tmp >= 1000.0 * 1000.0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 321 | printf(" %.0fKbit", tmp/1000.0); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 322 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 323 | printf(" %.0fbit", tmp); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 327 | rateest_print_mode(const struct xt_rateest_match_info *info, |
| 328 | const char *prefix) |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 329 | { |
| 330 | if (info->flags & XT_RATEEST_MATCH_INVERT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 331 | printf(" !"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 332 | |
| 333 | switch (info->mode) { |
| 334 | case XT_RATEEST_MATCH_EQ: |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 335 | printf(" %seq", prefix); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 336 | break; |
| 337 | case XT_RATEEST_MATCH_LT: |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 338 | printf(" %slt", prefix); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 339 | break; |
| 340 | case XT_RATEEST_MATCH_GT: |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 341 | printf(" %sgt", prefix); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | exit(1); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | rateest_print(const void *ip, const struct xt_entry_match *match, int numeric) |
| 350 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 351 | const struct xt_rateest_match_info *info = (const void *)match->data; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 352 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 353 | printf(" rateest match "); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 354 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 355 | printf("%s", info->name1); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 356 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 357 | printf(" delta"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 358 | |
| 359 | if (info->flags & XT_RATEEST_MATCH_BPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 360 | printf(" bps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 361 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
| 362 | rateest_print_rate(info->bps1, numeric); |
| 363 | if (info->flags & XT_RATEEST_MATCH_ABS) { |
| 364 | rateest_print_mode(info, ""); |
| 365 | rateest_print_rate(info->bps2, numeric); |
| 366 | } |
| 367 | } |
| 368 | if (info->flags & XT_RATEEST_MATCH_PPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 369 | printf(" pps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 370 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 371 | printf(" %u", info->pps1); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 372 | if (info->flags & XT_RATEEST_MATCH_ABS) { |
| 373 | rateest_print_mode(info, ""); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 374 | printf(" %u", info->pps2); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | if (info->flags & XT_RATEEST_MATCH_REL) { |
| 379 | rateest_print_mode(info, ""); |
| 380 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 381 | printf(" %s", info->name2); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 382 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 383 | printf(" delta"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 384 | |
| 385 | if (info->flags & XT_RATEEST_MATCH_BPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 386 | printf(" bps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 387 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
| 388 | rateest_print_rate(info->bps2, numeric); |
| 389 | } |
| 390 | if (info->flags & XT_RATEEST_MATCH_PPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 391 | printf(" pps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 392 | if (info->flags & XT_RATEEST_MATCH_DELTA) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 393 | printf(" %u", info->pps2); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | rateest_save(const void *ip, const struct xt_entry_match *match) |
| 400 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 401 | const struct xt_rateest_match_info *info = (const void *)match->data; |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 402 | |
| 403 | if (info->flags & XT_RATEEST_MATCH_REL) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 404 | printf(" --rateest1 %s", info->name1); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 405 | if (info->flags & XT_RATEEST_MATCH_BPS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 406 | printf(" --rateest-bps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 407 | if (info->flags & XT_RATEEST_MATCH_PPS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 408 | printf(" --rateest-pps"); |
| 409 | rateest_print_mode(info, " --rateest-"); |
| 410 | printf(" --rateest2 %s", info->name2); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 411 | } else { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 412 | printf(" --rateest %s", info->name1); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 413 | if (info->flags & XT_RATEEST_MATCH_BPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 414 | printf(" --rateest-bps1"); |
Luciano Coelho | b4fa722 | 2010-07-15 18:09:54 +0200 | [diff] [blame] | 415 | rateest_print_rate(info->bps1, 0); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 416 | printf(" --rateest-bps2"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 417 | rateest_print_rate(info->bps2, 0); |
Luciano Coelho | b4fa722 | 2010-07-15 18:09:54 +0200 | [diff] [blame] | 418 | rateest_print_mode(info, "--rateest-"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 419 | } |
| 420 | if (info->flags & XT_RATEEST_MATCH_PPS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 421 | printf(" --rateest-pps"); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 422 | rateest_print_mode(info, "--rateest-"); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 423 | printf(" %u", info->pps2); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 428 | static struct xtables_match rateest_mt_reg = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 429 | .family = NFPROTO_UNSPEC, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 430 | .name = "rateest", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 431 | .version = XTABLES_VERSION, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 432 | .size = XT_ALIGN(sizeof(struct xt_rateest_match_info)), |
| 433 | .userspacesize = XT_ALIGN(offsetof(struct xt_rateest_match_info, est1)), |
| 434 | .help = rateest_help, |
| 435 | .parse = rateest_parse, |
Jan Engelhardt | 4a96d2e | 2011-06-21 09:54:31 +0200 | [diff] [blame] | 436 | .x6_fcheck = rateest_final_check, |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 437 | .print = rateest_print, |
| 438 | .save = rateest_save, |
| 439 | .extra_opts = rateest_opts, |
| 440 | }; |
| 441 | |
| 442 | void _init(void) |
| 443 | { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 444 | xtables_register_match(&rateest_mt_reg); |
Patrick McHardy | 6afc5b7 | 2008-01-15 17:27:04 +0000 | [diff] [blame] | 445 | } |