libxt_RATEEST.c 4.5 KB

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