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