]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nft_counter.c
netfilter: nf_tables: pass context to object destroy indirection
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nft_counter.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
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.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
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>
19
20struct nft_counter {
d84701ec
PN
21 s64 bytes;
22 s64 packets;
0c45e769
PNA
23};
24
25struct nft_counter_percpu_priv {
d84701ec 26 struct nft_counter __percpu *counter;
0c45e769
PNA
27};
28
d84701ec
PN
29static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
30
b1ce0ced
PNA
31static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
32 struct nft_regs *regs,
33 const struct nft_pktinfo *pkt)
96518518 34{
d84701ec
PN
35 struct nft_counter *this_cpu;
36 seqcount_t *myseq;
0c45e769
PNA
37
38 local_bh_disable();
39 this_cpu = this_cpu_ptr(priv->counter);
d84701ec
PN
40 myseq = this_cpu_ptr(&nft_counter_seq);
41
42 write_seqcount_begin(myseq);
43
44 this_cpu->bytes += pkt->skb->len;
45 this_cpu->packets++;
46
47 write_seqcount_end(myseq);
0c45e769 48 local_bh_enable();
96518518
PM
49}
50
b1ce0ced
PNA
51static inline void nft_counter_obj_eval(struct nft_object *obj,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
54{
55 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
56
57 nft_counter_do_eval(priv, regs, pkt);
58}
59
60static int nft_counter_do_init(const struct nlattr * const tb[],
61 struct nft_counter_percpu_priv *priv)
62{
d84701ec
PN
63 struct nft_counter __percpu *cpu_stats;
64 struct nft_counter *this_cpu;
b1ce0ced 65
d84701ec 66 cpu_stats = alloc_percpu(struct nft_counter);
b1ce0ced
PNA
67 if (cpu_stats == NULL)
68 return -ENOMEM;
69
70 preempt_disable();
71 this_cpu = this_cpu_ptr(cpu_stats);
72 if (tb[NFTA_COUNTER_PACKETS]) {
d84701ec 73 this_cpu->packets =
b1ce0ced
PNA
74 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
75 }
76 if (tb[NFTA_COUNTER_BYTES]) {
d84701ec 77 this_cpu->bytes =
b1ce0ced
PNA
78 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
79 }
80 preempt_enable();
81 priv->counter = cpu_stats;
82 return 0;
83}
84
84fba055
FW
85static int nft_counter_obj_init(const struct nft_ctx *ctx,
86 const struct nlattr * const tb[],
b1ce0ced
PNA
87 struct nft_object *obj)
88{
89 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
90
91 return nft_counter_do_init(tb, priv);
92}
93
94static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
95{
96 free_percpu(priv->counter);
97}
98
00bfb320
PNA
99static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
100 struct nft_object *obj)
b1ce0ced
PNA
101{
102 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
103
104 nft_counter_do_destroy(priv);
105}
106
d84701ec 107static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv,
086f3321 108 struct nft_counter *total)
96518518 109{
d84701ec 110 struct nft_counter *this_cpu;
0c45e769 111
d84701ec
PN
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();
43da04a5
PNA
117}
118
d84701ec 119static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
43da04a5
PNA
120 struct nft_counter *total)
121{
d84701ec
PN
122 struct nft_counter *this_cpu;
123 const seqcount_t *myseq;
43da04a5
PNA
124 u64 bytes, packets;
125 unsigned int seq;
126 int cpu;
127
128 memset(total, 0, sizeof(*total));
129 for_each_possible_cpu(cpu) {
d84701ec
PN
130 myseq = per_cpu_ptr(&nft_counter_seq, cpu);
131 this_cpu = per_cpu_ptr(priv->counter, cpu);
43da04a5 132 do {
d84701ec
PN
133 seq = read_seqcount_begin(myseq);
134 bytes = this_cpu->bytes;
135 packets = this_cpu->packets;
136 } while (read_seqcount_retry(myseq, seq));
43da04a5 137
d84701ec
PN
138 total->bytes += bytes;
139 total->packets += packets;
43da04a5
PNA
140 }
141}
142
b1ce0ced 143static int nft_counter_do_dump(struct sk_buff *skb,
d84701ec 144 struct nft_counter_percpu_priv *priv,
43da04a5 145 bool reset)
086f3321 146{
086f3321
PNA
147 struct nft_counter total;
148
d84701ec 149 nft_counter_fetch(priv, &total);
0c45e769 150
b46f6ded
ND
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))
96518518 155 goto nla_put_failure;
d84701ec
PN
156
157 if (reset)
158 nft_counter_reset(priv, &total);
159
96518518
PM
160 return 0;
161
162nla_put_failure:
163 return -1;
164}
165
b1ce0ced 166static int nft_counter_obj_dump(struct sk_buff *skb,
43da04a5 167 struct nft_object *obj, bool reset)
b1ce0ced 168{
43da04a5 169 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
b1ce0ced 170
43da04a5 171 return nft_counter_do_dump(skb, priv, reset);
b1ce0ced
PNA
172}
173
96518518
PM
174static 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 },
177};
178
dfc46034
PBG
179static struct nft_object_type nft_counter_obj_type;
180static const struct nft_object_ops nft_counter_obj_ops = {
181 .type = &nft_counter_obj_type,
b1ce0ced 182 .size = sizeof(struct nft_counter_percpu_priv),
b1ce0ced
PNA
183 .eval = nft_counter_obj_eval,
184 .init = nft_counter_obj_init,
185 .destroy = nft_counter_obj_destroy,
186 .dump = nft_counter_obj_dump,
dfc46034
PBG
187};
188
189static 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,
b1ce0ced
PNA
194 .owner = THIS_MODULE,
195};
196
197static void nft_counter_eval(const struct nft_expr *expr,
198 struct nft_regs *regs,
199 const struct nft_pktinfo *pkt)
200{
201 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
202
203 nft_counter_do_eval(priv, regs, pkt);
204}
205
206static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
207{
d84701ec 208 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
b1ce0ced 209
43da04a5 210 return nft_counter_do_dump(skb, priv, false);
b1ce0ced
PNA
211}
212
96518518
PM
213static int nft_counter_init(const struct nft_ctx *ctx,
214 const struct nft_expr *expr,
215 const struct nlattr * const tb[])
216{
0c45e769 217 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
0c45e769 218
b1ce0ced 219 return nft_counter_do_init(tb, priv);
0c45e769 220}
96518518 221
0c45e769
PNA
222static void nft_counter_destroy(const struct nft_ctx *ctx,
223 const struct nft_expr *expr)
224{
225 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
96518518 226
b1ce0ced 227 nft_counter_do_destroy(priv);
96518518
PM
228}
229
086f3321
PNA
230static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
231{
232 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
233 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
d84701ec
PN
234 struct nft_counter __percpu *cpu_stats;
235 struct nft_counter *this_cpu;
086f3321
PNA
236 struct nft_counter total;
237
d84701ec 238 nft_counter_fetch(priv, &total);
086f3321 239
d84701ec 240 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
086f3321 241 if (cpu_stats == NULL)
5cc6ce9f 242 return -ENOMEM;
086f3321
PNA
243
244 preempt_disable();
245 this_cpu = this_cpu_ptr(cpu_stats);
d84701ec
PN
246 this_cpu->packets = total.packets;
247 this_cpu->bytes = total.bytes;
086f3321
PNA
248 preempt_enable();
249
250 priv_clone->counter = cpu_stats;
251 return 0;
252}
253
ef1f7df9
PM
254static struct nft_expr_type nft_counter_type;
255static const struct nft_expr_ops nft_counter_ops = {
256 .type = &nft_counter_type,
0c45e769 257 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
96518518
PM
258 .eval = nft_counter_eval,
259 .init = nft_counter_init,
0c45e769 260 .destroy = nft_counter_destroy,
96518518 261 .dump = nft_counter_dump,
086f3321 262 .clone = nft_counter_clone,
96518518
PM
263};
264
ef1f7df9
PM
265static struct nft_expr_type nft_counter_type __read_mostly = {
266 .name = "counter",
267 .ops = &nft_counter_ops,
268 .policy = nft_counter_policy,
269 .maxattr = NFTA_COUNTER_MAX,
151d799a 270 .flags = NFT_EXPR_STATEFUL,
ef1f7df9
PM
271 .owner = THIS_MODULE,
272};
273
96518518
PM
274static int __init nft_counter_module_init(void)
275{
d84701ec
PN
276 int cpu, err;
277
278 for_each_possible_cpu(cpu)
279 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
b1ce0ced 280
dfc46034 281 err = nft_register_obj(&nft_counter_obj_type);
b1ce0ced
PNA
282 if (err < 0)
283 return err;
284
285 err = nft_register_expr(&nft_counter_type);
286 if (err < 0)
287 goto err1;
288
289 return 0;
290err1:
dfc46034 291 nft_unregister_obj(&nft_counter_obj_type);
b1ce0ced 292 return err;
96518518
PM
293}
294
295static void __exit nft_counter_module_exit(void)
296{
ef1f7df9 297 nft_unregister_expr(&nft_counter_type);
dfc46034 298 nft_unregister_obj(&nft_counter_obj_type);
96518518
PM
299}
300
301module_init(nft_counter_module_init);
302module_exit(nft_counter_module_exit);
303
304MODULE_LICENSE("GPL");
305MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
306MODULE_ALIAS_NFT_EXPR("counter");
b1ce0ced 307MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);