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>
25 struct nft_counter_percpu_priv {
26 struct nft_counter __percpu *counter;
29 static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
31 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
32 struct nft_regs *regs,
33 const struct nft_pktinfo *pkt)
35 struct nft_counter *this_cpu;
39 this_cpu = this_cpu_ptr(priv->counter);
40 myseq = this_cpu_ptr(&nft_counter_seq);
42 write_seqcount_begin(myseq);
44 this_cpu->bytes += pkt->skb->len;
47 write_seqcount_end(myseq);
51 static inline void nft_counter_obj_eval(struct nft_object *obj,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
55 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
57 nft_counter_do_eval(priv, regs, pkt);
60 static int nft_counter_do_init(const struct nlattr * const tb[],
61 struct nft_counter_percpu_priv *priv)
63 struct nft_counter __percpu *cpu_stats;
64 struct nft_counter *this_cpu;
66 cpu_stats = alloc_percpu(struct nft_counter);
67 if (cpu_stats == NULL)
71 this_cpu = this_cpu_ptr(cpu_stats);
72 if (tb[NFTA_COUNTER_PACKETS]) {
74 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
76 if (tb[NFTA_COUNTER_BYTES]) {
78 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
81 priv->counter = cpu_stats;
85 static int nft_counter_obj_init(const struct nft_ctx *ctx,
86 const struct nlattr * const tb[],
87 struct nft_object *obj)
89 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
91 return nft_counter_do_init(tb, priv);
94 static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
96 free_percpu(priv->counter);
99 static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
100 struct nft_object *obj)
102 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
104 nft_counter_do_destroy(priv);
107 static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv,
108 struct nft_counter *total)
110 struct nft_counter *this_cpu;
113 this_cpu = this_cpu_ptr(priv->counter);
114 this_cpu->packets -= total->packets;
115 this_cpu->bytes -= total->bytes;
119 static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
120 struct nft_counter *total)
122 struct nft_counter *this_cpu;
123 const seqcount_t *myseq;
128 memset(total, 0, sizeof(*total));
129 for_each_possible_cpu(cpu) {
130 myseq = per_cpu_ptr(&nft_counter_seq, cpu);
131 this_cpu = per_cpu_ptr(priv->counter, cpu);
133 seq = read_seqcount_begin(myseq);
134 bytes = this_cpu->bytes;
135 packets = this_cpu->packets;
136 } while (read_seqcount_retry(myseq, seq));
138 total->bytes += bytes;
139 total->packets += packets;
143 static int nft_counter_do_dump(struct sk_buff *skb,
144 struct nft_counter_percpu_priv *priv,
147 struct nft_counter total;
149 nft_counter_fetch(priv, &total);
151 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
153 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
155 goto nla_put_failure;
158 nft_counter_reset(priv, &total);
166 static int nft_counter_obj_dump(struct sk_buff *skb,
167 struct nft_object *obj, bool reset)
169 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
171 return nft_counter_do_dump(skb, priv, reset);
174 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
175 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
176 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
179 static struct nft_object_type nft_counter_obj_type;
180 static const struct nft_object_ops nft_counter_obj_ops = {
181 .type = &nft_counter_obj_type,
182 .size = sizeof(struct nft_counter_percpu_priv),
183 .eval = nft_counter_obj_eval,
184 .init = nft_counter_obj_init,
185 .destroy = nft_counter_obj_destroy,
186 .dump = nft_counter_obj_dump,
189 static struct nft_object_type nft_counter_obj_type __read_mostly = {
190 .type = NFT_OBJECT_COUNTER,
191 .ops = &nft_counter_obj_ops,
192 .maxattr = NFTA_COUNTER_MAX,
193 .policy = nft_counter_policy,
194 .owner = THIS_MODULE,
197 static void nft_counter_eval(const struct nft_expr *expr,
198 struct nft_regs *regs,
199 const struct nft_pktinfo *pkt)
201 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
203 nft_counter_do_eval(priv, regs, pkt);
206 static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
208 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
210 return nft_counter_do_dump(skb, priv, false);
213 static int nft_counter_init(const struct nft_ctx *ctx,
214 const struct nft_expr *expr,
215 const struct nlattr * const tb[])
217 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
219 return nft_counter_do_init(tb, priv);
222 static void nft_counter_destroy(const struct nft_ctx *ctx,
223 const struct nft_expr *expr)
225 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
227 nft_counter_do_destroy(priv);
230 static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
232 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
233 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
234 struct nft_counter __percpu *cpu_stats;
235 struct nft_counter *this_cpu;
236 struct nft_counter total;
238 nft_counter_fetch(priv, &total);
240 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
241 if (cpu_stats == NULL)
245 this_cpu = this_cpu_ptr(cpu_stats);
246 this_cpu->packets = total.packets;
247 this_cpu->bytes = total.bytes;
250 priv_clone->counter = cpu_stats;
254 static struct nft_expr_type nft_counter_type;
255 static const struct nft_expr_ops nft_counter_ops = {
256 .type = &nft_counter_type,
257 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
258 .eval = nft_counter_eval,
259 .init = nft_counter_init,
260 .destroy = nft_counter_destroy,
261 .destroy_clone = nft_counter_destroy,
262 .dump = nft_counter_dump,
263 .clone = nft_counter_clone,
266 static struct nft_expr_type nft_counter_type __read_mostly = {
268 .ops = &nft_counter_ops,
269 .policy = nft_counter_policy,
270 .maxattr = NFTA_COUNTER_MAX,
271 .flags = NFT_EXPR_STATEFUL,
272 .owner = THIS_MODULE,
275 static int __init nft_counter_module_init(void)
279 for_each_possible_cpu(cpu)
280 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
282 err = nft_register_obj(&nft_counter_obj_type);
286 err = nft_register_expr(&nft_counter_type);
292 nft_unregister_obj(&nft_counter_obj_type);
296 static void __exit nft_counter_module_exit(void)
298 nft_unregister_expr(&nft_counter_type);
299 nft_unregister_obj(&nft_counter_obj_type);
302 module_init(nft_counter_module_init);
303 module_exit(nft_counter_module_exit);
305 MODULE_LICENSE("GPL");
307 MODULE_ALIAS_NFT_EXPR("counter");
308 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);