~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nft_counter.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp