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 void nft_counter_eval(const struct nft_expr
*expr
,
35 struct nft_regs
*regs
,
36 const struct nft_pktinfo
*pkt
)
38 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
39 struct nft_counter_percpu
*this_cpu
;
42 this_cpu
= this_cpu_ptr(priv
->counter
);
43 u64_stats_update_begin(&this_cpu
->syncp
);
44 this_cpu
->counter
.bytes
+= pkt
->skb
->len
;
45 this_cpu
->counter
.packets
++;
46 u64_stats_update_end(&this_cpu
->syncp
);
50 static void nft_counter_fetch(const struct nft_counter_percpu __percpu
*counter
,
51 struct nft_counter
*total
)
53 const struct nft_counter_percpu
*cpu_stats
;
58 memset(total
, 0, sizeof(*total
));
59 for_each_possible_cpu(cpu
) {
60 cpu_stats
= per_cpu_ptr(counter
, cpu
);
62 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
63 bytes
= cpu_stats
->counter
.bytes
;
64 packets
= cpu_stats
->counter
.packets
;
65 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
67 total
->packets
+= packets
;
68 total
->bytes
+= bytes
;
72 static int nft_counter_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
74 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
75 struct nft_counter total
;
77 nft_counter_fetch(priv
->counter
, &total
);
79 if (nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
)) ||
80 nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.packets
)))
88 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
89 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
90 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
93 static int nft_counter_init(const struct nft_ctx
*ctx
,
94 const struct nft_expr
*expr
,
95 const struct nlattr
* const tb
[])
97 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
98 struct nft_counter_percpu __percpu
*cpu_stats
;
99 struct nft_counter_percpu
*this_cpu
;
101 cpu_stats
= netdev_alloc_pcpu_stats(struct nft_counter_percpu
);
102 if (cpu_stats
== NULL
)
106 this_cpu
= this_cpu_ptr(cpu_stats
);
107 if (tb
[NFTA_COUNTER_PACKETS
]) {
108 this_cpu
->counter
.packets
=
109 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
111 if (tb
[NFTA_COUNTER_BYTES
]) {
112 this_cpu
->counter
.bytes
=
113 be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
116 priv
->counter
= cpu_stats
;
120 static void nft_counter_destroy(const struct nft_ctx
*ctx
,
121 const struct nft_expr
*expr
)
123 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(expr
);
125 free_percpu(priv
->counter
);
128 static int nft_counter_clone(struct nft_expr
*dst
, const struct nft_expr
*src
)
130 struct nft_counter_percpu_priv
*priv
= nft_expr_priv(src
);
131 struct nft_counter_percpu_priv
*priv_clone
= nft_expr_priv(dst
);
132 struct nft_counter_percpu __percpu
*cpu_stats
;
133 struct nft_counter_percpu
*this_cpu
;
134 struct nft_counter total
;
136 nft_counter_fetch(priv
->counter
, &total
);
138 cpu_stats
= __netdev_alloc_pcpu_stats(struct nft_counter_percpu
,
140 if (cpu_stats
== NULL
)
144 this_cpu
= this_cpu_ptr(cpu_stats
);
145 this_cpu
->counter
.packets
= total
.packets
;
146 this_cpu
->counter
.bytes
= total
.bytes
;
149 priv_clone
->counter
= cpu_stats
;
153 static struct nft_expr_type nft_counter_type
;
154 static const struct nft_expr_ops nft_counter_ops
= {
155 .type
= &nft_counter_type
,
156 .size
= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv
)),
157 .eval
= nft_counter_eval
,
158 .init
= nft_counter_init
,
159 .destroy
= nft_counter_destroy
,
160 .dump
= nft_counter_dump
,
161 .clone
= nft_counter_clone
,
164 static struct nft_expr_type nft_counter_type __read_mostly
= {
166 .ops
= &nft_counter_ops
,
167 .policy
= nft_counter_policy
,
168 .maxattr
= NFTA_COUNTER_MAX
,
169 .flags
= NFT_EXPR_STATEFUL
,
170 .owner
= THIS_MODULE
,
173 static int __init
nft_counter_module_init(void)
175 return nft_register_expr(&nft_counter_type
);
178 static void __exit
nft_counter_module_exit(void)
180 nft_unregister_expr(&nft_counter_type
);
183 module_init(nft_counter_module_init
);
184 module_exit(nft_counter_module_exit
);
186 MODULE_LICENSE("GPL");
187 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
188 MODULE_ALIAS_NFT_EXPR("counter");