nft_counter.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. seqlock_t lock;
  20. u64 bytes;
  21. u64 packets;
  22. };
  23. static void nft_counter_eval(const struct nft_expr *expr,
  24. struct nft_data data[NFT_REG_MAX + 1],
  25. const struct nft_pktinfo *pkt)
  26. {
  27. struct nft_counter *priv = nft_expr_priv(expr);
  28. write_seqlock_bh(&priv->lock);
  29. priv->bytes += pkt->skb->len;
  30. priv->packets++;
  31. write_sequnlock_bh(&priv->lock);
  32. }
  33. static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
  34. {
  35. struct nft_counter *priv = nft_expr_priv(expr);
  36. unsigned int seq;
  37. u64 bytes;
  38. u64 packets;
  39. do {
  40. seq = read_seqbegin(&priv->lock);
  41. bytes = priv->bytes;
  42. packets = priv->packets;
  43. } while (read_seqretry(&priv->lock, seq));
  44. if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(bytes)))
  45. goto nla_put_failure;
  46. if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(packets)))
  47. goto nla_put_failure;
  48. return 0;
  49. nla_put_failure:
  50. return -1;
  51. }
  52. static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
  53. [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
  54. [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
  55. };
  56. static int nft_counter_init(const struct nft_ctx *ctx,
  57. const struct nft_expr *expr,
  58. const struct nlattr * const tb[])
  59. {
  60. struct nft_counter *priv = nft_expr_priv(expr);
  61. if (tb[NFTA_COUNTER_PACKETS])
  62. priv->packets = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
  63. if (tb[NFTA_COUNTER_BYTES])
  64. priv->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
  65. seqlock_init(&priv->lock);
  66. return 0;
  67. }
  68. static struct nft_expr_type nft_counter_type;
  69. static const struct nft_expr_ops nft_counter_ops = {
  70. .type = &nft_counter_type,
  71. .size = NFT_EXPR_SIZE(sizeof(struct nft_counter)),
  72. .eval = nft_counter_eval,
  73. .init = nft_counter_init,
  74. .dump = nft_counter_dump,
  75. };
  76. static struct nft_expr_type nft_counter_type __read_mostly = {
  77. .name = "counter",
  78. .ops = &nft_counter_ops,
  79. .policy = nft_counter_policy,
  80. .maxattr = NFTA_COUNTER_MAX,
  81. .owner = THIS_MODULE,
  82. };
  83. static int __init nft_counter_module_init(void)
  84. {
  85. return nft_register_expr(&nft_counter_type);
  86. }
  87. static void __exit nft_counter_module_exit(void)
  88. {
  89. nft_unregister_expr(&nft_counter_type);
  90. }
  91. module_init(nft_counter_module_init);
  92. module_exit(nft_counter_module_exit);
  93. MODULE_LICENSE("GPL");
  94. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  95. MODULE_ALIAS_NFT_EXPR("counter");