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/u64_stats_sync.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_tot
{
29 struct nft_counter_percpu_priv
{
30 struct nft_counter __percpu
*counter
;
33 static DEFINE_PER_CPU(struct u64_stats_sync
, nft_counter_sync
);
35 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv
*priv
,
36 struct nft_regs
*regs
,
37 const struct nft_pktinfo
*pkt
)
39 struct u64_stats_sync
*nft_sync
;
40 struct nft_counter
*this_cpu
;
43 this_cpu
= this_cpu_ptr(priv
->counter
);
44 nft_sync
= this_cpu_ptr(&nft_counter_sync
);
46 u64_stats_update_begin(nft_sync
);
47 u64_stats_add(&this_cpu
->bytes
, pkt
->skb
->len
);
48 u64_stats_inc(&this_cpu
->packets
);
49 u64_stats_update_end(nft_sync
);
54 static inline void nft_counter_obj_eval(struct nft_object
*obj
,
55 struct nft_regs
*regs
,
56 const struct nft_pktinfo
*pkt
)
58 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
60 nft_counter_do_eval(priv
, regs
, pkt
);
63 static int nft_counter_do_init(const struct nlattr
* const tb
[],
64 struct nft_counter_percpu_priv
*priv
)
66 struct nft_counter __percpu
*cpu_stats
;
67 struct nft_counter
*this_cpu
;
69 cpu_stats
= alloc_percpu_gfp(struct nft_counter
, GFP_KERNEL_ACCOUNT
);
70 if (cpu_stats
== NULL
)
73 this_cpu
= raw_cpu_ptr(cpu_stats
);
74 if (tb
[NFTA_COUNTER_PACKETS
]) {
75 u64_stats_set(&this_cpu
->packets
,
76 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
])));
78 if (tb
[NFTA_COUNTER_BYTES
]) {
79 u64_stats_set(&this_cpu
->bytes
,
80 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
])));
83 priv
->counter
= cpu_stats
;
87 static int nft_counter_obj_init(const struct nft_ctx
*ctx
,
88 const struct nlattr
* const tb
[],
89 struct nft_object
*obj
)
91 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
93 return nft_counter_do_init(tb
, priv
);
96 static void nft_counter_do_destroy(struct nft_counter_percpu_priv
*priv
)
98 free_percpu(priv
->counter
);
101 static void nft_counter_obj_destroy(const struct nft_ctx
*ctx
,
102 struct nft_object
*obj
)
104 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
106 nft_counter_do_destroy(priv
);
109 static void nft_counter_reset(struct nft_counter_percpu_priv
*priv
,
110 struct nft_counter_tot
*total
)
112 struct u64_stats_sync
*nft_sync
;
113 struct nft_counter
*this_cpu
;
116 this_cpu
= this_cpu_ptr(priv
->counter
);
117 nft_sync
= this_cpu_ptr(&nft_counter_sync
);
119 u64_stats_update_begin(nft_sync
);
120 u64_stats_add(&this_cpu
->packets
, -total
->packets
);
121 u64_stats_add(&this_cpu
->bytes
, -total
->bytes
);
122 u64_stats_update_end(nft_sync
);
127 static void nft_counter_fetch(struct nft_counter_percpu_priv
*priv
,
128 struct nft_counter_tot
*total
)
130 struct nft_counter
*this_cpu
;
135 memset(total
, 0, sizeof(*total
));
136 for_each_possible_cpu(cpu
) {
137 struct u64_stats_sync
*nft_sync
= per_cpu_ptr(&nft_counter_sync
, cpu
);
139 this_cpu
= per_cpu_ptr(priv
->counter
, cpu
);
141 seq
= u64_stats_fetch_begin(nft_sync
);
142 bytes
= u64_stats_read(&this_cpu
->bytes
);
143 packets
= u64_stats_read(&this_cpu
->packets
);
144 } while (u64_stats_fetch_retry(nft_sync
, seq
));
146 total
->bytes
+= bytes
;
147 total
->packets
+= packets
;
151 static int nft_counter_do_dump(struct sk_buff
*skb
,
152 struct nft_counter_percpu_priv
*priv
,
155 struct nft_counter_tot total
;
157 nft_counter_fetch(priv
, &total
);
159 if (nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
161 nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.packets
),
163 goto nla_put_failure
;
166 nft_counter_reset(priv
, &total
);
174 static int nft_counter_obj_dump(struct sk_buff
*skb
,
175 struct nft_object
*obj
, bool reset
)
177 struct nft_counter_percpu_priv
*priv
= nft_obj_data(obj
);
179 return nft_counter_do_dump(skb
, priv
, reset
);
182 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
183 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
184 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
187 struct nft_object_type nft_counter_obj_type
;
188 static const struct nft_object_ops nft_counter_obj_ops
= {
189 .type
= &nft_counter_obj_type
,
190 .size
= sizeof(struct nft_counter_percpu_priv
),
191 .eval
= nft_counter_obj_eval
,
192 .init
= nft_counter_obj_init
,
193 .destroy
= nft_counter_obj_destroy
,
194 .dump
= nft_counter_obj_dump
,
197 struct nft_object_type nft_counter_obj_type __read_mostly
= {
198 .type
= NFT_OBJECT_COUNTER
,
199 .ops
= &nft_counter_obj_ops
,
200 .maxattr
= NFTA_COUNTER_MAX
,
201 .policy
= nft_counter_policy
,
202 .owner
= THIS_MODULE
,
205 void nft_counter_eval(const struct nft_expr
*expr
, struct nft_regs
*regs
,
206 const struct nft_pktinfo
*pkt
)
208 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
210 nft_counter_do_eval(priv
, regs
, pkt
);
213 static int nft_counter_dump(struct sk_buff
*skb
,
214 const struct nft_expr
*expr
, bool reset
)
216 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
218 return nft_counter_do_dump(skb
, priv
, reset
);
221 static int nft_counter_init(const struct nft_ctx
*ctx
,
222 const struct nft_expr
*expr
,
223 const struct nlattr
* const tb
[])
225 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
227 return nft_counter_do_init(tb
, priv
);
230 static void nft_counter_destroy(const struct nft_ctx
*ctx
,
231 const struct nft_expr
*expr
)
233 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
235 nft_counter_do_destroy(priv
);
238 static int nft_counter_clone(struct nft_expr
*dst
, const struct nft_expr
*src
, gfp_t gfp
)
240 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(src
);
241 struct nft_counter_percpu_priv
*priv_clone
= nft_expr_priv(dst
);
242 struct nft_counter __percpu
*cpu_stats
;
243 struct nft_counter
*this_cpu
;
244 struct nft_counter_tot total
;
246 nft_counter_fetch(priv
, &total
);
248 cpu_stats
= alloc_percpu_gfp(struct nft_counter
, gfp
);
249 if (cpu_stats
== NULL
)
252 this_cpu
= raw_cpu_ptr(cpu_stats
);
253 u64_stats_set(&this_cpu
->packets
, total
.packets
);
254 u64_stats_set(&this_cpu
->bytes
, total
.bytes
);
256 priv_clone
->counter
= cpu_stats
;
260 static int nft_counter_offload(struct nft_offload_ctx
*ctx
,
261 struct nft_flow_rule
*flow
,
262 const struct nft_expr
*expr
)
264 /* No specific offload action is needed, but report success. */
268 static void nft_counter_offload_stats(struct nft_expr
*expr
,
269 const struct flow_stats
*stats
)
271 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
272 struct u64_stats_sync
*nft_sync
;
273 struct nft_counter
*this_cpu
;
276 this_cpu
= this_cpu_ptr(priv
->counter
);
277 nft_sync
= this_cpu_ptr(&nft_counter_sync
);
279 u64_stats_update_begin(nft_sync
);
280 u64_stats_add(&this_cpu
->packets
, stats
->pkts
);
281 u64_stats_add(&this_cpu
->bytes
, stats
->bytes
);
282 u64_stats_update_end(nft_sync
);
286 void nft_counter_init_seqcount(void)
290 for_each_possible_cpu(cpu
)
291 u64_stats_init(per_cpu_ptr(&nft_counter_sync
, cpu
));
294 struct nft_expr_type nft_counter_type
;
295 static const struct nft_expr_ops nft_counter_ops
= {
296 .type
= &nft_counter_type
,
297 .size
= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv
)),
298 .eval
= nft_counter_eval
,
299 .init
= nft_counter_init
,
300 .destroy
= nft_counter_destroy
,
301 .destroy_clone
= nft_counter_destroy
,
302 .dump
= nft_counter_dump
,
303 .clone
= nft_counter_clone
,
304 .reduce
= NFT_REDUCE_READONLY
,
305 .offload
= nft_counter_offload
,
306 .offload_stats
= nft_counter_offload_stats
,
309 struct nft_expr_type nft_counter_type __read_mostly
= {
311 .ops
= &nft_counter_ops
,
312 .policy
= nft_counter_policy
,
313 .maxattr
= NFTA_COUNTER_MAX
,
314 .flags
= NFT_EXPR_STATEFUL
,
315 .owner
= THIS_MODULE
,