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>
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(struct nft_object
*obj
)
101 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
103 nft_counter_do_destroy(priv
);
106 static void nft_counter_reset(struct nft_counter_percpu_priv __percpu
*priv
,
107 struct nft_counter
*total
)
109 struct nft_counter
*this_cpu
;
112 this_cpu
= this_cpu_ptr(priv
->counter
);
113 this_cpu
->packets
-= total
->packets
;
114 this_cpu
->bytes
-= total
->bytes
;
118 static void nft_counter_fetch(struct nft_counter_percpu_priv
*priv
,
119 struct nft_counter
*total
)
121 struct nft_counter
*this_cpu
;
122 const seqcount_t
*myseq
;
127 memset(total
, 0, sizeof(*total
));
128 for_each_possible_cpu(cpu
) {
129 myseq
= per_cpu_ptr(&nft_counter_seq
, cpu
);
130 this_cpu
= per_cpu_ptr(priv
->counter
, cpu
);
132 seq
= read_seqcount_begin(myseq
);
133 bytes
= this_cpu
->bytes
;
134 packets
= this_cpu
->packets
;
135 } while (read_seqcount_retry(myseq
, seq
));
137 total
->bytes
+= bytes
;
138 total
->packets
+= packets
;
142 static int nft_counter_do_dump(struct sk_buff
*skb
,
143 struct nft_counter_percpu_priv
*priv
,
146 struct nft_counter total
;
148 nft_counter_fetch(priv
, &total
);
150 if (nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
152 nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.packets
),
154 goto nla_put_failure
;
157 nft_counter_reset(priv
, &total
);
165 static int nft_counter_obj_dump(struct sk_buff
*skb
,
166 struct nft_object
*obj
, bool reset
)
168 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
170 return nft_counter_do_dump(skb
, priv
, reset
);
173 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
174 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
175 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
178 static struct nft_object_type nft_counter_obj_type
;
179 static const struct nft_object_ops nft_counter_obj_ops
= {
180 .type
= &nft_counter_obj_type
,
181 .size
= sizeof(struct nft_counter_percpu_priv
),
182 .eval
= nft_counter_obj_eval
,
183 .init
= nft_counter_obj_init
,
184 .destroy
= nft_counter_obj_destroy
,
185 .dump
= nft_counter_obj_dump
,
188 static struct nft_object_type nft_counter_obj_type __read_mostly
= {
189 .type
= NFT_OBJECT_COUNTER
,
190 .ops
= &nft_counter_obj_ops
,
191 .maxattr
= NFTA_COUNTER_MAX
,
192 .policy
= nft_counter_policy
,
193 .owner
= THIS_MODULE
,
196 static void nft_counter_eval(const struct nft_expr
*expr
,
197 struct nft_regs
*regs
,
198 const struct nft_pktinfo
*pkt
)
200 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
202 nft_counter_do_eval(priv
, regs
, pkt
);
205 static int nft_counter_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
207 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
209 return nft_counter_do_dump(skb
, priv
, false);
212 static int nft_counter_init(const struct nft_ctx
*ctx
,
213 const struct nft_expr
*expr
,
214 const struct nlattr
* const tb
[])
216 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
218 return nft_counter_do_init(tb
, priv
);
221 static void nft_counter_destroy(const struct nft_ctx
*ctx
,
222 const struct nft_expr
*expr
)
224 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
226 nft_counter_do_destroy(priv
);
229 static int nft_counter_clone(struct nft_expr
*dst
, const struct nft_expr
*src
)
231 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(src
);
232 struct nft_counter_percpu_priv
*priv_clone
= nft_expr_priv(dst
);
233 struct nft_counter __percpu
*cpu_stats
;
234 struct nft_counter
*this_cpu
;
235 struct nft_counter total
;
237 nft_counter_fetch(priv
, &total
);
239 cpu_stats
= alloc_percpu_gfp(struct nft_counter
, GFP_ATOMIC
);
240 if (cpu_stats
== NULL
)
244 this_cpu
= this_cpu_ptr(cpu_stats
);
245 this_cpu
->packets
= total
.packets
;
246 this_cpu
->bytes
= total
.bytes
;
249 priv_clone
->counter
= cpu_stats
;
253 static struct nft_expr_type nft_counter_type
;
254 static const struct nft_expr_ops nft_counter_ops
= {
255 .type
= &nft_counter_type
,
256 .size
= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv
)),
257 .eval
= nft_counter_eval
,
258 .init
= nft_counter_init
,
259 .destroy
= nft_counter_destroy
,
260 .dump
= nft_counter_dump
,
261 .clone
= nft_counter_clone
,
264 static struct nft_expr_type nft_counter_type __read_mostly
= {
266 .ops
= &nft_counter_ops
,
267 .policy
= nft_counter_policy
,
268 .maxattr
= NFTA_COUNTER_MAX
,
269 .flags
= NFT_EXPR_STATEFUL
,
270 .owner
= THIS_MODULE
,
273 static int __init
nft_counter_module_init(void)
277 for_each_possible_cpu(cpu
)
278 seqcount_init(per_cpu_ptr(&nft_counter_seq
, cpu
));
280 err
= nft_register_obj(&nft_counter_obj_type
);
284 err
= nft_register_expr(&nft_counter_type
);
290 nft_unregister_obj(&nft_counter_obj_type
);
294 static void __exit
nft_counter_module_exit(void)
296 nft_unregister_expr(&nft_counter_type
);
297 nft_unregister_obj(&nft_counter_obj_type
);
300 module_init(nft_counter_module_init
);
301 module_exit(nft_counter_module_exit
);
303 MODULE_LICENSE("GPL");
304 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
305 MODULE_ALIAS_NFT_EXPR("counter");
306 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER
);