crypto: s5p-sss - Use AES_BLOCK_SIZE define instead of number
[linux/fpc-iii.git] / net / netfilter / nft_counter.c
bloba61d7edfc290d854e9216d015efa22c38aacc39b
1 /*
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/)
9 */
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>
20 struct nft_counter {
21 s64 bytes;
22 s64 packets;
25 struct nft_counter_percpu_priv {
26 struct nft_counter __percpu *counter;
29 static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
31 static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
32 struct nft_regs *regs,
33 const struct nft_pktinfo *pkt)
35 struct nft_counter *this_cpu;
36 seqcount_t *myseq;
38 local_bh_disable();
39 this_cpu = this_cpu_ptr(priv->counter);
40 myseq = this_cpu_ptr(&nft_counter_seq);
42 write_seqcount_begin(myseq);
44 this_cpu->bytes += pkt->skb->len;
45 this_cpu->packets++;
47 write_seqcount_end(myseq);
48 local_bh_enable();
51 static inline void nft_counter_obj_eval(struct nft_object *obj,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
55 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
57 nft_counter_do_eval(priv, regs, pkt);
60 static int nft_counter_do_init(const struct nlattr * const tb[],
61 struct nft_counter_percpu_priv *priv)
63 struct nft_counter __percpu *cpu_stats;
64 struct nft_counter *this_cpu;
66 cpu_stats = alloc_percpu(struct nft_counter);
67 if (cpu_stats == NULL)
68 return -ENOMEM;
70 preempt_disable();
71 this_cpu = this_cpu_ptr(cpu_stats);
72 if (tb[NFTA_COUNTER_PACKETS]) {
73 this_cpu->packets =
74 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
76 if (tb[NFTA_COUNTER_BYTES]) {
77 this_cpu->bytes =
78 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
80 preempt_enable();
81 priv->counter = cpu_stats;
82 return 0;
85 static int nft_counter_obj_init(const struct nft_ctx *ctx,
86 const struct nlattr * const tb[],
87 struct nft_object *obj)
89 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
91 return nft_counter_do_init(tb, priv);
94 static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
96 free_percpu(priv->counter);
99 static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
100 struct nft_object *obj)
102 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
104 nft_counter_do_destroy(priv);
107 static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv,
108 struct nft_counter *total)
110 struct nft_counter *this_cpu;
112 local_bh_disable();
113 this_cpu = this_cpu_ptr(priv->counter);
114 this_cpu->packets -= total->packets;
115 this_cpu->bytes -= total->bytes;
116 local_bh_enable();
119 static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
120 struct nft_counter *total)
122 struct nft_counter *this_cpu;
123 const seqcount_t *myseq;
124 u64 bytes, packets;
125 unsigned int seq;
126 int cpu;
128 memset(total, 0, sizeof(*total));
129 for_each_possible_cpu(cpu) {
130 myseq = per_cpu_ptr(&nft_counter_seq, cpu);
131 this_cpu = per_cpu_ptr(priv->counter, cpu);
132 do {
133 seq = read_seqcount_begin(myseq);
134 bytes = this_cpu->bytes;
135 packets = this_cpu->packets;
136 } while (read_seqcount_retry(myseq, seq));
138 total->bytes += bytes;
139 total->packets += packets;
143 static int nft_counter_do_dump(struct sk_buff *skb,
144 struct nft_counter_percpu_priv *priv,
145 bool reset)
147 struct nft_counter total;
149 nft_counter_fetch(priv, &total);
151 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
152 NFTA_COUNTER_PAD) ||
153 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
154 NFTA_COUNTER_PAD))
155 goto nla_put_failure;
157 if (reset)
158 nft_counter_reset(priv, &total);
160 return 0;
162 nla_put_failure:
163 return -1;
166 static int nft_counter_obj_dump(struct sk_buff *skb,
167 struct nft_object *obj, bool reset)
169 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
171 return nft_counter_do_dump(skb, priv, reset);
174 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
175 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
176 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
179 static struct nft_object_type nft_counter_obj_type;
180 static const struct nft_object_ops nft_counter_obj_ops = {
181 .type = &nft_counter_obj_type,
182 .size = sizeof(struct nft_counter_percpu_priv),
183 .eval = nft_counter_obj_eval,
184 .init = nft_counter_obj_init,
185 .destroy = nft_counter_obj_destroy,
186 .dump = nft_counter_obj_dump,
189 static struct nft_object_type nft_counter_obj_type __read_mostly = {
190 .type = NFT_OBJECT_COUNTER,
191 .ops = &nft_counter_obj_ops,
192 .maxattr = NFTA_COUNTER_MAX,
193 .policy = nft_counter_policy,
194 .owner = THIS_MODULE,
197 static void nft_counter_eval(const struct nft_expr *expr,
198 struct nft_regs *regs,
199 const struct nft_pktinfo *pkt)
201 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
203 nft_counter_do_eval(priv, regs, pkt);
206 static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
208 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
210 return nft_counter_do_dump(skb, priv, false);
213 static int nft_counter_init(const struct nft_ctx *ctx,
214 const struct nft_expr *expr,
215 const struct nlattr * const tb[])
217 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
219 return nft_counter_do_init(tb, priv);
222 static void nft_counter_destroy(const struct nft_ctx *ctx,
223 const struct nft_expr *expr)
225 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
227 nft_counter_do_destroy(priv);
230 static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
232 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
233 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
234 struct nft_counter __percpu *cpu_stats;
235 struct nft_counter *this_cpu;
236 struct nft_counter total;
238 nft_counter_fetch(priv, &total);
240 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
241 if (cpu_stats == NULL)
242 return -ENOMEM;
244 preempt_disable();
245 this_cpu = this_cpu_ptr(cpu_stats);
246 this_cpu->packets = total.packets;
247 this_cpu->bytes = total.bytes;
248 preempt_enable();
250 priv_clone->counter = cpu_stats;
251 return 0;
254 static struct nft_expr_type nft_counter_type;
255 static const struct nft_expr_ops nft_counter_ops = {
256 .type = &nft_counter_type,
257 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
258 .eval = nft_counter_eval,
259 .init = nft_counter_init,
260 .destroy = nft_counter_destroy,
261 .destroy_clone = nft_counter_destroy,
262 .dump = nft_counter_dump,
263 .clone = nft_counter_clone,
266 static struct nft_expr_type nft_counter_type __read_mostly = {
267 .name = "counter",
268 .ops = &nft_counter_ops,
269 .policy = nft_counter_policy,
270 .maxattr = NFTA_COUNTER_MAX,
271 .flags = NFT_EXPR_STATEFUL,
272 .owner = THIS_MODULE,
275 static int __init nft_counter_module_init(void)
277 int cpu, err;
279 for_each_possible_cpu(cpu)
280 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
282 err = nft_register_obj(&nft_counter_obj_type);
283 if (err < 0)
284 return err;
286 err = nft_register_expr(&nft_counter_type);
287 if (err < 0)
288 goto err1;
290 return 0;
291 err1:
292 nft_unregister_obj(&nft_counter_obj_type);
293 return err;
296 static void __exit nft_counter_module_exit(void)
298 nft_unregister_expr(&nft_counter_type);
299 nft_unregister_obj(&nft_counter_obj_type);
302 module_init(nft_counter_module_init);
303 module_exit(nft_counter_module_exit);
305 MODULE_LICENSE("GPL");
306 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
307 MODULE_ALIAS_NFT_EXPR("counter");
308 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);