nft_counter.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/seqlock.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables.h>
  18. struct nft_counter {
  19. s64 bytes;
  20. s64 packets;
  21. };
  22. struct nft_counter_percpu_priv {
  23. struct nft_counter __percpu *counter;
  24. };
  25. static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
  26. static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
  27. struct nft_regs *regs,
  28. const struct nft_pktinfo *pkt)
  29. {
  30. struct nft_counter *this_cpu;
  31. seqcount_t *myseq;
  32. local_bh_disable();
  33. this_cpu = this_cpu_ptr(priv->counter);
  34. myseq = this_cpu_ptr(&nft_counter_seq);
  35. write_seqcount_begin(myseq);
  36. this_cpu->bytes += pkt->skb->len;
  37. this_cpu->packets++;
  38. write_seqcount_end(myseq);
  39. local_bh_enable();
  40. }
  41. static inline void nft_counter_obj_eval(struct nft_object *obj,
  42. struct nft_regs *regs,
  43. const struct nft_pktinfo *pkt)
  44. {
  45. struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
  46. nft_counter_do_eval(priv, regs, pkt);
  47. }
  48. static int nft_counter_do_init(const struct nlattr * const tb[],
  49. struct nft_counter_percpu_priv *priv)
  50. {
  51. struct nft_counter __percpu *cpu_stats;
  52. struct nft_counter *this_cpu;
  53. cpu_stats = alloc_percpu(struct nft_counter);
  54. if (cpu_stats == NULL)
  55. return -ENOMEM;
  56. preempt_disable();
  57. this_cpu = this_cpu_ptr(cpu_stats);
  58. if (tb[NFTA_COUNTER_PACKETS]) {
  59. this_cpu->packets =
  60. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
  61. }
  62. if (tb[NFTA_COUNTER_BYTES]) {
  63. this_cpu->bytes =
  64. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
  65. }
  66. preempt_enable();
  67. priv->counter = cpu_stats;
  68. return 0;
  69. }
  70. static int nft_counter_obj_init(const struct nft_ctx *ctx,
  71. const struct nlattr * const tb[],
  72. struct nft_object *obj)
  73. {
  74. struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
  75. return nft_counter_do_init(tb, priv);
  76. }
  77. static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
  78. {
  79. free_percpu(priv->counter);
  80. }
  81. static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
  82. struct nft_object *obj)
  83. {
  84. struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
  85. nft_counter_do_destroy(priv);
  86. }
  87. static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv,
  88. struct nft_counter *total)
  89. {
  90. struct nft_counter *this_cpu;
  91. local_bh_disable();
  92. this_cpu = this_cpu_ptr(priv->counter);
  93. this_cpu->packets -= total->packets;
  94. this_cpu->bytes -= total->bytes;
  95. local_bh_enable();
  96. }
  97. static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
  98. struct nft_counter *total)
  99. {
  100. struct nft_counter *this_cpu;
  101. const seqcount_t *myseq;
  102. u64 bytes, packets;
  103. unsigned int seq;
  104. int cpu;
  105. memset(total, 0, sizeof(*total));
  106. for_each_possible_cpu(cpu) {
  107. myseq = per_cpu_ptr(&nft_counter_seq, cpu);
  108. this_cpu = per_cpu_ptr(priv->counter, cpu);
  109. do {
  110. seq = read_seqcount_begin(myseq);
  111. bytes = this_cpu->bytes;
  112. packets = this_cpu->packets;
  113. } while (read_seqcount_retry(myseq, seq));
  114. total->bytes += bytes;
  115. total->packets += packets;
  116. }
  117. }
  118. static int nft_counter_do_dump(struct sk_buff *skb,
  119. struct nft_counter_percpu_priv *priv,
  120. bool reset)
  121. {
  122. struct nft_counter total;
  123. nft_counter_fetch(priv, &total);
  124. if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
  125. NFTA_COUNTER_PAD) ||
  126. nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
  127. NFTA_COUNTER_PAD))
  128. goto nla_put_failure;
  129. if (reset)
  130. nft_counter_reset(priv, &total);
  131. return 0;
  132. nla_put_failure:
  133. return -1;
  134. }
  135. static int nft_counter_obj_dump(struct sk_buff *skb,
  136. struct nft_object *obj, bool reset)
  137. {
  138. struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
  139. return nft_counter_do_dump(skb, priv, reset);
  140. }
  141. static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
  142. [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
  143. [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
  144. };
  145. static struct nft_object_type nft_counter_obj_type;
  146. static const struct nft_object_ops nft_counter_obj_ops = {
  147. .type = &nft_counter_obj_type,
  148. .size = sizeof(struct nft_counter_percpu_priv),
  149. .eval = nft_counter_obj_eval,
  150. .init = nft_counter_obj_init,
  151. .destroy = nft_counter_obj_destroy,
  152. .dump = nft_counter_obj_dump,
  153. };
  154. static struct nft_object_type nft_counter_obj_type __read_mostly = {
  155. .type = NFT_OBJECT_COUNTER,
  156. .ops = &nft_counter_obj_ops,
  157. .maxattr = NFTA_COUNTER_MAX,
  158. .policy = nft_counter_policy,
  159. .owner = THIS_MODULE,
  160. };
  161. static void nft_counter_eval(const struct nft_expr *expr,
  162. struct nft_regs *regs,
  163. const struct nft_pktinfo *pkt)
  164. {
  165. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  166. nft_counter_do_eval(priv, regs, pkt);
  167. }
  168. static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
  169. {
  170. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  171. return nft_counter_do_dump(skb, priv, false);
  172. }
  173. static int nft_counter_init(const struct nft_ctx *ctx,
  174. const struct nft_expr *expr,
  175. const struct nlattr * const tb[])
  176. {
  177. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  178. return nft_counter_do_init(tb, priv);
  179. }
  180. static void nft_counter_destroy(const struct nft_ctx *ctx,
  181. const struct nft_expr *expr)
  182. {
  183. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  184. nft_counter_do_destroy(priv);
  185. }
  186. static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
  187. {
  188. struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
  189. struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
  190. struct nft_counter __percpu *cpu_stats;
  191. struct nft_counter *this_cpu;
  192. struct nft_counter total;
  193. nft_counter_fetch(priv, &total);
  194. cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
  195. if (cpu_stats == NULL)
  196. return -ENOMEM;
  197. preempt_disable();
  198. this_cpu = this_cpu_ptr(cpu_stats);
  199. this_cpu->packets = total.packets;
  200. this_cpu->bytes = total.bytes;
  201. preempt_enable();
  202. priv_clone->counter = cpu_stats;
  203. return 0;
  204. }
  205. static struct nft_expr_type nft_counter_type;
  206. static const struct nft_expr_ops nft_counter_ops = {
  207. .type = &nft_counter_type,
  208. .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
  209. .eval = nft_counter_eval,
  210. .init = nft_counter_init,
  211. .destroy = nft_counter_destroy,
  212. .destroy_clone = nft_counter_destroy,
  213. .dump = nft_counter_dump,
  214. .clone = nft_counter_clone,
  215. };
  216. static struct nft_expr_type nft_counter_type __read_mostly = {
  217. .name = "counter",
  218. .ops = &nft_counter_ops,
  219. .policy = nft_counter_policy,
  220. .maxattr = NFTA_COUNTER_MAX,
  221. .flags = NFT_EXPR_STATEFUL,
  222. .owner = THIS_MODULE,
  223. };
  224. static int __init nft_counter_module_init(void)
  225. {
  226. int cpu, err;
  227. for_each_possible_cpu(cpu)
  228. seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
  229. err = nft_register_obj(&nft_counter_obj_type);
  230. if (err < 0)
  231. return err;
  232. err = nft_register_expr(&nft_counter_type);
  233. if (err < 0)
  234. goto err1;
  235. return 0;
  236. err1:
  237. nft_unregister_obj(&nft_counter_obj_type);
  238. return err;
  239. }
  240. static void __exit nft_counter_module_exit(void)
  241. {
  242. nft_unregister_expr(&nft_counter_type);
  243. nft_unregister_obj(&nft_counter_obj_type);
  244. }
  245. module_init(nft_counter_module_init);
  246. module_exit(nft_counter_module_exit);
  247. MODULE_LICENSE("GPL");
  248. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  249. MODULE_ALIAS_NFT_EXPR("counter");
  250. MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);