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 {
26 struct nft_counter counter;
27 struct u64_stats_sync syncp;
30 struct nft_counter_percpu_priv {
31 struct nft_counter_percpu __percpu *counter;
34 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
35 struct nft_regs *regs,
36 const struct nft_pktinfo *pkt)
38 struct nft_counter_percpu *this_cpu;
41 this_cpu = this_cpu_ptr(priv->counter);
42 u64_stats_update_begin(&this_cpu->syncp);
43 this_cpu->counter.bytes += pkt->skb->len;
44 this_cpu->counter.packets++;
45 u64_stats_update_end(&this_cpu->syncp);
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 __percpu *cpu_stats;
62 struct nft_counter_percpu *this_cpu;
64 cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
65 if (cpu_stats == NULL)
69 this_cpu = this_cpu_ptr(cpu_stats);
70 if (tb[NFTA_COUNTER_PACKETS]) {
71 this_cpu->counter.packets =
72 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
74 if (tb[NFTA_COUNTER_BYTES]) {
75 this_cpu->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 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(struct nft_object *obj)
98 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
100 nft_counter_do_destroy(priv);
103 static void nft_counter_fetch(struct nft_counter_percpu __percpu *counter,
104 struct nft_counter *total)
106 struct nft_counter_percpu *cpu_stats;
111 memset(total, 0, sizeof(*total));
112 for_each_possible_cpu(cpu) {
113 cpu_stats = per_cpu_ptr(counter, cpu);
115 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
116 bytes = cpu_stats->counter.bytes;
117 packets = cpu_stats->counter.packets;
118 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
120 total->packets += packets;
121 total->bytes += bytes;
125 static u64 __nft_counter_reset(u64 *counter)
131 ret = cmpxchg64(counter, old, 0);
132 } while (ret != old);
137 static void nft_counter_reset(struct nft_counter_percpu __percpu *counter,
138 struct nft_counter *total)
140 struct nft_counter_percpu *cpu_stats;
145 memset(total, 0, sizeof(*total));
146 for_each_possible_cpu(cpu) {
149 cpu_stats = per_cpu_ptr(counter, cpu);
151 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
152 packets += __nft_counter_reset(&cpu_stats->counter.packets);
153 bytes += __nft_counter_reset(&cpu_stats->counter.bytes);
154 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
156 total->packets += packets;
157 total->bytes += bytes;
161 static int nft_counter_do_dump(struct sk_buff *skb,
162 const struct nft_counter_percpu_priv *priv,
165 struct nft_counter total;
168 nft_counter_reset(priv->counter, &total);
170 nft_counter_fetch(priv->counter, &total);
172 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
174 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
176 goto nla_put_failure;
183 static int nft_counter_obj_dump(struct sk_buff *skb,
184 struct nft_object *obj, bool reset)
186 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
188 return nft_counter_do_dump(skb, priv, reset);
191 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
192 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
193 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
196 static struct nft_object_type nft_counter_obj __read_mostly = {
197 .type = NFT_OBJECT_COUNTER,
198 .size = sizeof(struct nft_counter_percpu_priv),
199 .maxattr = NFTA_COUNTER_MAX,
200 .policy = nft_counter_policy,
201 .eval = nft_counter_obj_eval,
202 .init = nft_counter_obj_init,
203 .destroy = nft_counter_obj_destroy,
204 .dump = nft_counter_obj_dump,
205 .owner = THIS_MODULE,
208 static void nft_counter_eval(const struct nft_expr *expr,
209 struct nft_regs *regs,
210 const struct nft_pktinfo *pkt)
212 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
214 nft_counter_do_eval(priv, regs, pkt);
217 static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
219 const struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
221 return nft_counter_do_dump(skb, priv, false);
224 static int nft_counter_init(const struct nft_ctx *ctx,
225 const struct nft_expr *expr,
226 const struct nlattr * const tb[])
228 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
230 return nft_counter_do_init(tb, priv);
233 static void nft_counter_destroy(const struct nft_ctx *ctx,
234 const struct nft_expr *expr)
236 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
238 nft_counter_do_destroy(priv);
241 static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
243 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
244 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
245 struct nft_counter_percpu __percpu *cpu_stats;
246 struct nft_counter_percpu *this_cpu;
247 struct nft_counter total;
249 nft_counter_fetch(priv->counter, &total);
251 cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
253 if (cpu_stats == NULL)
257 this_cpu = this_cpu_ptr(cpu_stats);
258 this_cpu->counter.packets = total.packets;
259 this_cpu->counter.bytes = total.bytes;
262 priv_clone->counter = cpu_stats;
266 static struct nft_expr_type nft_counter_type;
267 static const struct nft_expr_ops nft_counter_ops = {
268 .type = &nft_counter_type,
269 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
270 .eval = nft_counter_eval,
271 .init = nft_counter_init,
272 .destroy = nft_counter_destroy,
273 .dump = nft_counter_dump,
274 .clone = nft_counter_clone,
277 static struct nft_expr_type nft_counter_type __read_mostly = {
279 .ops = &nft_counter_ops,
280 .policy = nft_counter_policy,
281 .maxattr = NFTA_COUNTER_MAX,
282 .flags = NFT_EXPR_STATEFUL,
283 .owner = THIS_MODULE,
286 static int __init nft_counter_module_init(void)
290 err = nft_register_obj(&nft_counter_obj);
294 err = nft_register_expr(&nft_counter_type);
300 nft_unregister_obj(&nft_counter_obj);
304 static void __exit nft_counter_module_exit(void)
306 nft_unregister_expr(&nft_counter_type);
307 nft_unregister_obj(&nft_counter_obj);
310 module_init(nft_counter_module_init);
311 module_exit(nft_counter_module_exit);
313 MODULE_LICENSE("GPL");
314 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
315 MODULE_ALIAS_NFT_EXPR("counter");
316 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);