nft_counter.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. u64 bytes;
  20. u64 packets;
  21. };
  22. struct nft_counter_percpu {
  23. struct nft_counter counter;
  24. struct u64_stats_sync syncp;
  25. };
  26. struct nft_counter_percpu_priv {
  27. struct nft_counter_percpu __percpu *counter;
  28. };
  29. static void nft_counter_eval(const struct nft_expr *expr,
  30. struct nft_regs *regs,
  31. const struct nft_pktinfo *pkt)
  32. {
  33. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  34. struct nft_counter_percpu *this_cpu;
  35. local_bh_disable();
  36. this_cpu = this_cpu_ptr(priv->counter);
  37. u64_stats_update_begin(&this_cpu->syncp);
  38. this_cpu->counter.bytes += pkt->skb->len;
  39. this_cpu->counter.packets++;
  40. u64_stats_update_end(&this_cpu->syncp);
  41. local_bh_enable();
  42. }
  43. static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
  44. struct nft_counter *total)
  45. {
  46. const struct nft_counter_percpu *cpu_stats;
  47. u64 bytes, packets;
  48. unsigned int seq;
  49. int cpu;
  50. memset(total, 0, sizeof(*total));
  51. for_each_possible_cpu(cpu) {
  52. cpu_stats = per_cpu_ptr(counter, cpu);
  53. do {
  54. seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  55. bytes = cpu_stats->counter.bytes;
  56. packets = cpu_stats->counter.packets;
  57. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
  58. total->packets += packets;
  59. total->bytes += bytes;
  60. }
  61. }
  62. static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
  63. {
  64. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  65. struct nft_counter total;
  66. nft_counter_fetch(priv->counter, &total);
  67. if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)) ||
  68. nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets)))
  69. goto nla_put_failure;
  70. return 0;
  71. nla_put_failure:
  72. return -1;
  73. }
  74. static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
  75. [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
  76. [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
  77. };
  78. static int nft_counter_init(const struct nft_ctx *ctx,
  79. const struct nft_expr *expr,
  80. const struct nlattr * const tb[])
  81. {
  82. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  83. struct nft_counter_percpu __percpu *cpu_stats;
  84. struct nft_counter_percpu *this_cpu;
  85. cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
  86. if (cpu_stats == NULL)
  87. return ENOMEM;
  88. preempt_disable();
  89. this_cpu = this_cpu_ptr(cpu_stats);
  90. if (tb[NFTA_COUNTER_PACKETS]) {
  91. this_cpu->counter.packets =
  92. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
  93. }
  94. if (tb[NFTA_COUNTER_BYTES]) {
  95. this_cpu->counter.bytes =
  96. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
  97. }
  98. preempt_enable();
  99. priv->counter = cpu_stats;
  100. return 0;
  101. }
  102. static void nft_counter_destroy(const struct nft_ctx *ctx,
  103. const struct nft_expr *expr)
  104. {
  105. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  106. free_percpu(priv->counter);
  107. }
  108. static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
  109. {
  110. struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
  111. struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
  112. struct nft_counter_percpu __percpu *cpu_stats;
  113. struct nft_counter_percpu *this_cpu;
  114. struct nft_counter total;
  115. nft_counter_fetch(priv->counter, &total);
  116. cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
  117. GFP_ATOMIC);
  118. if (cpu_stats == NULL)
  119. return ENOMEM;
  120. preempt_disable();
  121. this_cpu = this_cpu_ptr(cpu_stats);
  122. this_cpu->counter.packets = total.packets;
  123. this_cpu->counter.bytes = total.bytes;
  124. preempt_enable();
  125. priv_clone->counter = cpu_stats;
  126. return 0;
  127. }
  128. static struct nft_expr_type nft_counter_type;
  129. static const struct nft_expr_ops nft_counter_ops = {
  130. .type = &nft_counter_type,
  131. .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
  132. .eval = nft_counter_eval,
  133. .init = nft_counter_init,
  134. .destroy = nft_counter_destroy,
  135. .dump = nft_counter_dump,
  136. .clone = nft_counter_clone,
  137. };
  138. static struct nft_expr_type nft_counter_type __read_mostly = {
  139. .name = "counter",
  140. .ops = &nft_counter_ops,
  141. .policy = nft_counter_policy,
  142. .maxattr = NFTA_COUNTER_MAX,
  143. .flags = NFT_EXPR_STATEFUL,
  144. .owner = THIS_MODULE,
  145. };
  146. static int __init nft_counter_module_init(void)
  147. {
  148. return nft_register_expr(&nft_counter_type);
  149. }
  150. static void __exit nft_counter_module_exit(void)
  151. {
  152. nft_unregister_expr(&nft_counter_type);
  153. }
  154. module_init(nft_counter_module_init);
  155. module_exit(nft_counter_module_exit);
  156. MODULE_LICENSE("GPL");
  157. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  158. MODULE_ALIAS_NFT_EXPR("counter");