1 // SPDX-License-Identifier: GPL-2.0-only
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_core.h>
17 #include <net/netfilter/nf_tables_offload.h>
24 struct nft_counter_percpu_priv {
25 struct nft_counter __percpu *counter;
28 static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
30 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt)
34 struct nft_counter *this_cpu;
38 this_cpu = this_cpu_ptr(priv->counter);
39 myseq = this_cpu_ptr(&nft_counter_seq);
41 write_seqcount_begin(myseq);
43 this_cpu->bytes += pkt->skb->len;
46 write_seqcount_end(myseq);
50 static inline void nft_counter_obj_eval(struct nft_object *obj,
51 struct nft_regs *regs,
52 const struct nft_pktinfo *pkt)
54 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
56 nft_counter_do_eval(priv, regs, pkt);
59 static int nft_counter_do_init(const struct nlattr * const tb[],
60 struct nft_counter_percpu_priv *priv)
62 struct nft_counter __percpu *cpu_stats;
63 struct nft_counter *this_cpu;
65 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_KERNEL_ACCOUNT);
66 if (cpu_stats == NULL)
70 this_cpu = this_cpu_ptr(cpu_stats);
71 if (tb[NFTA_COUNTER_PACKETS]) {
73 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
75 if (tb[NFTA_COUNTER_BYTES]) {
77 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
80 priv->counter = cpu_stats;
84 static int nft_counter_obj_init(const struct nft_ctx *ctx,
85 const struct nlattr * const tb[],
86 struct nft_object *obj)
88 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
90 return nft_counter_do_init(tb, priv);
93 static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
95 free_percpu(priv->counter);
98 static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
99 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 *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 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 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 void nft_counter_eval(const struct nft_expr *expr, 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,
205 const struct nft_expr *expr, bool reset)
207 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
209 return nft_counter_do_dump(skb, priv, reset);
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 int nft_counter_offload(struct nft_offload_ctx *ctx,
254 struct nft_flow_rule *flow,
255 const struct nft_expr *expr)
257 /* No specific offload action is needed, but report success. */
261 static void nft_counter_offload_stats(struct nft_expr *expr,
262 const struct flow_stats *stats)
264 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
265 struct nft_counter *this_cpu;
269 this_cpu = this_cpu_ptr(priv->counter);
270 myseq = this_cpu_ptr(&nft_counter_seq);
272 write_seqcount_begin(myseq);
273 this_cpu->packets += stats->pkts;
274 this_cpu->bytes += stats->bytes;
275 write_seqcount_end(myseq);
279 void nft_counter_init_seqcount(void)
283 for_each_possible_cpu(cpu)
284 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
287 struct nft_expr_type nft_counter_type;
288 static const struct nft_expr_ops nft_counter_ops = {
289 .type = &nft_counter_type,
290 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
291 .eval = nft_counter_eval,
292 .init = nft_counter_init,
293 .destroy = nft_counter_destroy,
294 .destroy_clone = nft_counter_destroy,
295 .dump = nft_counter_dump,
296 .clone = nft_counter_clone,
297 .reduce = NFT_REDUCE_READONLY,
298 .offload = nft_counter_offload,
299 .offload_stats = nft_counter_offload_stats,
302 struct nft_expr_type nft_counter_type __read_mostly = {
304 .ops = &nft_counter_ops,
305 .policy = nft_counter_policy,
306 .maxattr = NFTA_COUNTER_MAX,
307 .flags = NFT_EXPR_STATEFUL,
308 .owner = THIS_MODULE,