nft_counter.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. NFTA_COUNTER_PAD) ||
  69. nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
  70. NFTA_COUNTER_PAD))
  71. goto nla_put_failure;
  72. return 0;
  73. nla_put_failure:
  74. return -1;
  75. }
  76. static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
  77. [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
  78. [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
  79. };
  80. static int nft_counter_init(const struct nft_ctx *ctx,
  81. const struct nft_expr *expr,
  82. const struct nlattr * const tb[])
  83. {
  84. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  85. struct nft_counter_percpu __percpu *cpu_stats;
  86. struct nft_counter_percpu *this_cpu;
  87. cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
  88. if (cpu_stats == NULL)
  89. return -ENOMEM;
  90. preempt_disable();
  91. this_cpu = this_cpu_ptr(cpu_stats);
  92. if (tb[NFTA_COUNTER_PACKETS]) {
  93. this_cpu->counter.packets =
  94. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
  95. }
  96. if (tb[NFTA_COUNTER_BYTES]) {
  97. this_cpu->counter.bytes =
  98. be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
  99. }
  100. preempt_enable();
  101. priv->counter = cpu_stats;
  102. return 0;
  103. }
  104. static void nft_counter_destroy(const struct nft_ctx *ctx,
  105. const struct nft_expr *expr)
  106. {
  107. struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
  108. free_percpu(priv->counter);
  109. }
  110. static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
  111. {
  112. struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
  113. struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
  114. struct nft_counter_percpu __percpu *cpu_stats;
  115. struct nft_counter_percpu *this_cpu;
  116. struct nft_counter total;
  117. nft_counter_fetch(priv->counter, &total);
  118. cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
  119. GFP_ATOMIC);
  120. if (cpu_stats == NULL)
  121. return -ENOMEM;
  122. preempt_disable();
  123. this_cpu = this_cpu_ptr(cpu_stats);
  124. this_cpu->counter.packets = total.packets;
  125. this_cpu->counter.bytes = total.bytes;
  126. preempt_enable();
  127. priv_clone->counter = cpu_stats;
  128. return 0;
  129. }
  130. static struct nft_expr_type nft_counter_type;
  131. static const struct nft_expr_ops nft_counter_ops = {
  132. .type = &nft_counter_type,
  133. .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
  134. .eval = nft_counter_eval,
  135. .init = nft_counter_init,
  136. .destroy = nft_counter_destroy,
  137. .dump = nft_counter_dump,
  138. .clone = nft_counter_clone,
  139. };
  140. static struct nft_expr_type nft_counter_type __read_mostly = {
  141. .name = "counter",
  142. .ops = &nft_counter_ops,
  143. .policy = nft_counter_policy,
  144. .maxattr = NFTA_COUNTER_MAX,
  145. .flags = NFT_EXPR_STATEFUL,
  146. .owner = THIS_MODULE,
  147. };
  148. static int __init nft_counter_module_init(void)
  149. {
  150. return nft_register_expr(&nft_counter_type);
  151. }
  152. static void __exit nft_counter_module_exit(void)
  153. {
  154. nft_unregister_expr(&nft_counter_type);
  155. }
  156. module_init(nft_counter_module_init);
  157. module_exit(nft_counter_module_exit);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  160. MODULE_ALIAS_NFT_EXPR("counter");