2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
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
)
30 struct nft_counter
*priv
= nft_expr_priv(expr
);
32 write_seqlock_bh(&priv
->lock
);
33 priv
->bytes
+= pkt
->skb
->len
;
35 write_sequnlock_bh(&priv
->lock
);
38 static int nft_counter_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
40 struct nft_counter
*priv
= nft_expr_priv(expr
);
46 seq
= read_seqbegin(&priv
->lock
);
48 packets
= priv
->packets
;
49 } while (read_seqretry(&priv
->lock
, seq
));
51 if (nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(bytes
)))
53 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(packets
)))
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
},
66 static int nft_counter_init(const struct nft_ctx
*ctx
,
67 const struct nft_expr
*expr
,
68 const struct nlattr
* const tb
[])
70 struct nft_counter
*priv
= nft_expr_priv(expr
);
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
]));
77 seqlock_init(&priv
->lock
);
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
,
90 static struct nft_expr_type nft_counter_type __read_mostly
= {
92 .ops
= &nft_counter_ops
,
93 .policy
= nft_counter_policy
,
94 .maxattr
= NFTA_COUNTER_MAX
,
98 static int __init
nft_counter_module_init(void)
100 return nft_register_expr(&nft_counter_type
);
103 static void __exit
nft_counter_module_exit(void)
105 nft_unregister_expr(&nft_counter_type
);
108 module_init(nft_counter_module_init
);
109 module_exit(nft_counter_module_exit
);
111 MODULE_LICENSE("GPL");
112 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
113 MODULE_ALIAS_NFT_EXPR("counter");