1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/seqlock.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables.h>
22 struct nft_counter_percpu_priv
{
23 struct nft_counter __percpu
*counter
;
26 static DEFINE_PER_CPU(seqcount_t
, nft_counter_seq
);
28 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv
*priv
,
29 struct nft_regs
*regs
,
30 const struct nft_pktinfo
*pkt
)
32 struct nft_counter
*this_cpu
;
36 this_cpu
= this_cpu_ptr(priv
->counter
);
37 myseq
= this_cpu_ptr(&nft_counter_seq
);
39 write_seqcount_begin(myseq
);
41 this_cpu
->bytes
+= pkt
->skb
->len
;
44 write_seqcount_end(myseq
);
48 static inline void nft_counter_obj_eval(struct nft_object
*obj
,
49 struct nft_regs
*regs
,
50 const struct nft_pktinfo
*pkt
)
52 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
54 nft_counter_do_eval(priv
, regs
, pkt
);
57 static int nft_counter_do_init(const struct nlattr
* const tb
[],
58 struct nft_counter_percpu_priv
*priv
)
60 struct nft_counter __percpu
*cpu_stats
;
61 struct nft_counter
*this_cpu
;
63 cpu_stats
= alloc_percpu(struct nft_counter
);
64 if (cpu_stats
== NULL
)
68 this_cpu
= this_cpu_ptr(cpu_stats
);
69 if (tb
[NFTA_COUNTER_PACKETS
]) {
71 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
73 if (tb
[NFTA_COUNTER_BYTES
]) {
75 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
78 priv
->counter
= cpu_stats
;
82 static int nft_counter_obj_init(const struct nft_ctx
*ctx
,
83 const struct nlattr
* const tb
[],
84 struct nft_object
*obj
)
86 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
88 return nft_counter_do_init(tb
, priv
);
91 static void nft_counter_do_destroy(struct nft_counter_percpu_priv
*priv
)
93 free_percpu(priv
->counter
);
96 static void nft_counter_obj_destroy(const struct nft_ctx
*ctx
,
97 struct nft_object
*obj
)
99 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
101 nft_counter_do_destroy(priv
);
104 static void nft_counter_reset(struct nft_counter_percpu_priv
*priv
,
105 struct nft_counter
*total
)
107 struct nft_counter
*this_cpu
;
110 this_cpu
= this_cpu_ptr(priv
->counter
);
111 this_cpu
->packets
-= total
->packets
;
112 this_cpu
->bytes
-= total
->bytes
;
116 static void nft_counter_fetch(struct nft_counter_percpu_priv
*priv
,
117 struct nft_counter
*total
)
119 struct nft_counter
*this_cpu
;
120 const seqcount_t
*myseq
;
125 memset(total
, 0, sizeof(*total
));
126 for_each_possible_cpu(cpu
) {
127 myseq
= per_cpu_ptr(&nft_counter_seq
, cpu
);
128 this_cpu
= per_cpu_ptr(priv
->counter
, cpu
);
130 seq
= read_seqcount_begin(myseq
);
131 bytes
= this_cpu
->bytes
;
132 packets
= this_cpu
->packets
;
133 } while (read_seqcount_retry(myseq
, seq
));
135 total
->bytes
+= bytes
;
136 total
->packets
+= packets
;
140 static int nft_counter_do_dump(struct sk_buff
*skb
,
141 struct nft_counter_percpu_priv
*priv
,
144 struct nft_counter total
;
146 nft_counter_fetch(priv
, &total
);
148 if (nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
150 nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.packets
),
152 goto nla_put_failure
;
155 nft_counter_reset(priv
, &total
);
163 static int nft_counter_obj_dump(struct sk_buff
*skb
,
164 struct nft_object
*obj
, bool reset
)
166 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
168 return nft_counter_do_dump(skb
, priv
, reset
);
171 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
172 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
173 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
176 static struct nft_object_type nft_counter_obj_type
;
177 static const struct nft_object_ops nft_counter_obj_ops
= {
178 .type
= &nft_counter_obj_type
,
179 .size
= sizeof(struct nft_counter_percpu_priv
),
180 .eval
= nft_counter_obj_eval
,
181 .init
= nft_counter_obj_init
,
182 .destroy
= nft_counter_obj_destroy
,
183 .dump
= nft_counter_obj_dump
,
186 static struct nft_object_type nft_counter_obj_type __read_mostly
= {
187 .type
= NFT_OBJECT_COUNTER
,
188 .ops
= &nft_counter_obj_ops
,
189 .maxattr
= NFTA_COUNTER_MAX
,
190 .policy
= nft_counter_policy
,
191 .owner
= THIS_MODULE
,
194 static void nft_counter_eval(const struct nft_expr
*expr
,
195 struct nft_regs
*regs
,
196 const struct nft_pktinfo
*pkt
)
198 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
200 nft_counter_do_eval(priv
, regs
, pkt
);
203 static int nft_counter_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
205 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
207 return nft_counter_do_dump(skb
, priv
, false);
210 static int nft_counter_init(const struct nft_ctx
*ctx
,
211 const struct nft_expr
*expr
,
212 const struct nlattr
* const tb
[])
214 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
216 return nft_counter_do_init(tb
, priv
);
219 static void nft_counter_destroy(const struct nft_ctx
*ctx
,
220 const struct nft_expr
*expr
)
222 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
224 nft_counter_do_destroy(priv
);
227 static int nft_counter_clone(struct nft_expr
*dst
, const struct nft_expr
*src
)
229 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(src
);
230 struct nft_counter_percpu_priv
*priv_clone
= nft_expr_priv(dst
);
231 struct nft_counter __percpu
*cpu_stats
;
232 struct nft_counter
*this_cpu
;
233 struct nft_counter total
;
235 nft_counter_fetch(priv
, &total
);
237 cpu_stats
= alloc_percpu_gfp(struct nft_counter
, GFP_ATOMIC
);
238 if (cpu_stats
== NULL
)
242 this_cpu
= this_cpu_ptr(cpu_stats
);
243 this_cpu
->packets
= total
.packets
;
244 this_cpu
->bytes
= total
.bytes
;
247 priv_clone
->counter
= cpu_stats
;
251 static struct nft_expr_type nft_counter_type
;
252 static const struct nft_expr_ops nft_counter_ops
= {
253 .type
= &nft_counter_type
,
254 .size
= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv
)),
255 .eval
= nft_counter_eval
,
256 .init
= nft_counter_init
,
257 .destroy
= nft_counter_destroy
,
258 .destroy_clone
= nft_counter_destroy
,
259 .dump
= nft_counter_dump
,
260 .clone
= nft_counter_clone
,
263 static struct nft_expr_type nft_counter_type __read_mostly
= {
265 .ops
= &nft_counter_ops
,
266 .policy
= nft_counter_policy
,
267 .maxattr
= NFTA_COUNTER_MAX
,
268 .flags
= NFT_EXPR_STATEFUL
,
269 .owner
= THIS_MODULE
,
272 static int __init
nft_counter_module_init(void)
276 for_each_possible_cpu(cpu
)
277 seqcount_init(per_cpu_ptr(&nft_counter_seq
, cpu
));
279 err
= nft_register_obj(&nft_counter_obj_type
);
283 err
= nft_register_expr(&nft_counter_type
);
289 nft_unregister_obj(&nft_counter_obj_type
);
293 static void __exit
nft_counter_module_exit(void)
295 nft_unregister_expr(&nft_counter_type
);
296 nft_unregister_obj(&nft_counter_obj_type
);
299 module_init(nft_counter_module_init
);
300 module_exit(nft_counter_module_exit
);
302 MODULE_LICENSE("GPL");
303 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
304 MODULE_ALIAS_NFT_EXPR("counter");
305 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER
);