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>
16 #include <net/netfilter/nf_tables_offload.h>
23 struct nft_counter_percpu_priv {
24 struct nft_counter __percpu *counter;
27 static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
29 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)
33 struct nft_counter *this_cpu;
37 this_cpu = this_cpu_ptr(priv->counter);
38 myseq = this_cpu_ptr(&nft_counter_seq);
40 write_seqcount_begin(myseq);
42 this_cpu->bytes += pkt->skb->len;
45 write_seqcount_end(myseq);
49 static inline void nft_counter_obj_eval(struct nft_object *obj,
50 struct nft_regs *regs,
51 const struct nft_pktinfo *pkt)
53 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
55 nft_counter_do_eval(priv, regs, pkt);
58 static int nft_counter_do_init(const struct nlattr * const tb[],
59 struct nft_counter_percpu_priv *priv)
61 struct nft_counter __percpu *cpu_stats;
62 struct nft_counter *this_cpu;
64 cpu_stats = alloc_percpu(struct nft_counter);
65 if (cpu_stats == NULL)
69 this_cpu = this_cpu_ptr(cpu_stats);
70 if (tb[NFTA_COUNTER_PACKETS]) {
72 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
74 if (tb[NFTA_COUNTER_BYTES]) {
76 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
79 priv->counter = cpu_stats;
83 static int nft_counter_obj_init(const struct nft_ctx *ctx,
84 const struct nlattr * const tb[],
85 struct nft_object *obj)
87 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
89 return nft_counter_do_init(tb, priv);
92 static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
94 free_percpu(priv->counter);
97 static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
98 struct nft_object *obj)
100 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
102 nft_counter_do_destroy(priv);
105 static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
106 struct nft_counter *total)
108 struct nft_counter *this_cpu;
111 this_cpu = this_cpu_ptr(priv->counter);
112 this_cpu->packets -= total->packets;
113 this_cpu->bytes -= total->bytes;
117 static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
118 struct nft_counter *total)
120 struct nft_counter *this_cpu;
121 const seqcount_t *myseq;
126 memset(total, 0, sizeof(*total));
127 for_each_possible_cpu(cpu) {
128 myseq = per_cpu_ptr(&nft_counter_seq, cpu);
129 this_cpu = per_cpu_ptr(priv->counter, cpu);
131 seq = read_seqcount_begin(myseq);
132 bytes = this_cpu->bytes;
133 packets = this_cpu->packets;
134 } while (read_seqcount_retry(myseq, seq));
136 total->bytes += bytes;
137 total->packets += packets;
141 static int nft_counter_do_dump(struct sk_buff *skb,
142 struct nft_counter_percpu_priv *priv,
145 struct nft_counter total;
147 nft_counter_fetch(priv, &total);
149 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
151 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
153 goto nla_put_failure;
156 nft_counter_reset(priv, &total);
164 static int nft_counter_obj_dump(struct sk_buff *skb,
165 struct nft_object *obj, bool reset)
167 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
169 return nft_counter_do_dump(skb, priv, reset);
172 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
173 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
174 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
177 static struct nft_object_type nft_counter_obj_type;
178 static const struct nft_object_ops nft_counter_obj_ops = {
179 .type = &nft_counter_obj_type,
180 .size = sizeof(struct nft_counter_percpu_priv),
181 .eval = nft_counter_obj_eval,
182 .init = nft_counter_obj_init,
183 .destroy = nft_counter_obj_destroy,
184 .dump = nft_counter_obj_dump,
187 static struct nft_object_type nft_counter_obj_type __read_mostly = {
188 .type = NFT_OBJECT_COUNTER,
189 .ops = &nft_counter_obj_ops,
190 .maxattr = NFTA_COUNTER_MAX,
191 .policy = nft_counter_policy,
192 .owner = THIS_MODULE,
195 static void nft_counter_eval(const struct nft_expr *expr,
196 struct nft_regs *regs,
197 const struct nft_pktinfo *pkt)
199 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
201 nft_counter_do_eval(priv, regs, pkt);
204 static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
206 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
208 return nft_counter_do_dump(skb, priv, false);
211 static int nft_counter_init(const struct nft_ctx *ctx,
212 const struct nft_expr *expr,
213 const struct nlattr * const tb[])
215 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
217 return nft_counter_do_init(tb, priv);
220 static void nft_counter_destroy(const struct nft_ctx *ctx,
221 const struct nft_expr *expr)
223 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
225 nft_counter_do_destroy(priv);
228 static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
230 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
231 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
232 struct nft_counter __percpu *cpu_stats;
233 struct nft_counter *this_cpu;
234 struct nft_counter total;
236 nft_counter_fetch(priv, &total);
238 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
239 if (cpu_stats == NULL)
243 this_cpu = this_cpu_ptr(cpu_stats);
244 this_cpu->packets = total.packets;
245 this_cpu->bytes = total.bytes;
248 priv_clone->counter = cpu_stats;
252 static int nft_counter_offload(struct nft_offload_ctx *ctx,
253 struct nft_flow_rule *flow,
254 const struct nft_expr *expr)
256 /* No specific offload action is needed, but report success. */
260 static void nft_counter_offload_stats(struct nft_expr *expr,
261 const struct flow_stats *stats)
263 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
264 struct nft_counter *this_cpu;
268 this_cpu = this_cpu_ptr(priv->counter);
269 myseq = this_cpu_ptr(&nft_counter_seq);
271 write_seqcount_begin(myseq);
272 this_cpu->packets += stats->pkts;
273 this_cpu->bytes += stats->bytes;
274 write_seqcount_end(myseq);
278 static struct nft_expr_type nft_counter_type;
279 static const struct nft_expr_ops nft_counter_ops = {
280 .type = &nft_counter_type,
281 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
282 .eval = nft_counter_eval,
283 .init = nft_counter_init,
284 .destroy = nft_counter_destroy,
285 .destroy_clone = nft_counter_destroy,
286 .dump = nft_counter_dump,
287 .clone = nft_counter_clone,
288 .offload = nft_counter_offload,
289 .offload_stats = nft_counter_offload_stats,
292 static struct nft_expr_type nft_counter_type __read_mostly = {
294 .ops = &nft_counter_ops,
295 .policy = nft_counter_policy,
296 .maxattr = NFTA_COUNTER_MAX,
297 .flags = NFT_EXPR_STATEFUL,
298 .owner = THIS_MODULE,
301 static int __init nft_counter_module_init(void)
305 for_each_possible_cpu(cpu)
306 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
308 err = nft_register_obj(&nft_counter_obj_type);
312 err = nft_register_expr(&nft_counter_type);
318 nft_unregister_obj(&nft_counter_obj_type);
322 static void __exit nft_counter_module_exit(void)
324 nft_unregister_expr(&nft_counter_type);
325 nft_unregister_obj(&nft_counter_obj_type);
328 module_init(nft_counter_module_init);
329 module_exit(nft_counter_module_exit);
331 MODULE_LICENSE("GPL");
332 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
333 MODULE_ALIAS_NFT_EXPR("counter");
334 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);
335 MODULE_DESCRIPTION("nftables counter rule support");