]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | struct nft_counter { | |
d84701ec PN |
21 | s64 bytes; |
22 | s64 packets; | |
0c45e769 PNA |
23 | }; |
24 | ||
25 | struct nft_counter_percpu_priv { | |
d84701ec | 26 | struct nft_counter __percpu *counter; |
0c45e769 PNA |
27 | }; |
28 | ||
d84701ec PN |
29 | static DEFINE_PER_CPU(seqcount_t, nft_counter_seq); |
30 | ||
b1ce0ced PNA |
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) | |
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 |
51 | static 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 | ||
60 | static 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 |
85 | static 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 | ||
94 | static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv) | |
95 | { | |
96 | free_percpu(priv->counter); | |
97 | } | |
98 | ||
99 | static void nft_counter_obj_destroy(struct nft_object *obj) | |
100 | { | |
101 | struct nft_counter_percpu_priv *priv = nft_obj_data(obj); | |
102 | ||
103 | nft_counter_do_destroy(priv); | |
104 | } | |
105 | ||
d84701ec | 106 | static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv, |
086f3321 | 107 | struct nft_counter *total) |
96518518 | 108 | { |
d84701ec | 109 | struct nft_counter *this_cpu; |
0c45e769 | 110 | |
d84701ec PN |
111 | local_bh_disable(); |
112 | this_cpu = this_cpu_ptr(priv->counter); | |
113 | this_cpu->packets -= total->packets; | |
114 | this_cpu->bytes -= total->bytes; | |
115 | local_bh_enable(); | |
43da04a5 PNA |
116 | } |
117 | ||
d84701ec | 118 | static void nft_counter_fetch(struct nft_counter_percpu_priv *priv, |
43da04a5 PNA |
119 | struct nft_counter *total) |
120 | { | |
d84701ec PN |
121 | struct nft_counter *this_cpu; |
122 | const seqcount_t *myseq; | |
43da04a5 PNA |
123 | u64 bytes, packets; |
124 | unsigned int seq; | |
125 | int cpu; | |
126 | ||
127 | memset(total, 0, sizeof(*total)); | |
128 | for_each_possible_cpu(cpu) { | |
d84701ec PN |
129 | myseq = per_cpu_ptr(&nft_counter_seq, cpu); |
130 | this_cpu = per_cpu_ptr(priv->counter, cpu); | |
43da04a5 | 131 | do { |
d84701ec PN |
132 | seq = read_seqcount_begin(myseq); |
133 | bytes = this_cpu->bytes; | |
134 | packets = this_cpu->packets; | |
135 | } while (read_seqcount_retry(myseq, seq)); | |
43da04a5 | 136 | |
d84701ec PN |
137 | total->bytes += bytes; |
138 | total->packets += packets; | |
43da04a5 PNA |
139 | } |
140 | } | |
141 | ||
b1ce0ced | 142 | static int nft_counter_do_dump(struct sk_buff *skb, |
d84701ec | 143 | struct nft_counter_percpu_priv *priv, |
43da04a5 | 144 | bool reset) |
086f3321 | 145 | { |
086f3321 PNA |
146 | struct nft_counter total; |
147 | ||
d84701ec | 148 | nft_counter_fetch(priv, &total); |
0c45e769 | 149 | |
b46f6ded ND |
150 | if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), |
151 | NFTA_COUNTER_PAD) || | |
152 | nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets), | |
153 | NFTA_COUNTER_PAD)) | |
96518518 | 154 | goto nla_put_failure; |
d84701ec PN |
155 | |
156 | if (reset) | |
157 | nft_counter_reset(priv, &total); | |
158 | ||
96518518 PM |
159 | return 0; |
160 | ||
161 | nla_put_failure: | |
162 | return -1; | |
163 | } | |
164 | ||
b1ce0ced | 165 | static int nft_counter_obj_dump(struct sk_buff *skb, |
43da04a5 | 166 | struct nft_object *obj, bool reset) |
b1ce0ced | 167 | { |
43da04a5 | 168 | struct nft_counter_percpu_priv *priv = nft_obj_data(obj); |
b1ce0ced | 169 | |
43da04a5 | 170 | return nft_counter_do_dump(skb, priv, reset); |
b1ce0ced PNA |
171 | } |
172 | ||
96518518 PM |
173 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
174 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
175 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
176 | }; | |
177 | ||
b1ce0ced PNA |
178 | static struct nft_object_type nft_counter_obj __read_mostly = { |
179 | .type = NFT_OBJECT_COUNTER, | |
180 | .size = sizeof(struct nft_counter_percpu_priv), | |
181 | .maxattr = NFTA_COUNTER_MAX, | |
182 | .policy = nft_counter_policy, | |
183 | .eval = nft_counter_obj_eval, | |
184 | .init = nft_counter_obj_init, | |
185 | .destroy = nft_counter_obj_destroy, | |
186 | .dump = nft_counter_obj_dump, | |
187 | .owner = THIS_MODULE, | |
188 | }; | |
189 | ||
190 | static void nft_counter_eval(const struct nft_expr *expr, | |
191 | struct nft_regs *regs, | |
192 | const struct nft_pktinfo *pkt) | |
193 | { | |
194 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); | |
195 | ||
196 | nft_counter_do_eval(priv, regs, pkt); | |
197 | } | |
198 | ||
199 | static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
200 | { | |
d84701ec | 201 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); |
b1ce0ced | 202 | |
43da04a5 | 203 | return nft_counter_do_dump(skb, priv, false); |
b1ce0ced PNA |
204 | } |
205 | ||
96518518 PM |
206 | static int nft_counter_init(const struct nft_ctx *ctx, |
207 | const struct nft_expr *expr, | |
208 | const struct nlattr * const tb[]) | |
209 | { | |
0c45e769 | 210 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); |
0c45e769 | 211 | |
b1ce0ced | 212 | return nft_counter_do_init(tb, priv); |
0c45e769 | 213 | } |
96518518 | 214 | |
0c45e769 PNA |
215 | static void nft_counter_destroy(const struct nft_ctx *ctx, |
216 | const struct nft_expr *expr) | |
217 | { | |
218 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); | |
96518518 | 219 | |
b1ce0ced | 220 | nft_counter_do_destroy(priv); |
96518518 PM |
221 | } |
222 | ||
086f3321 PNA |
223 | static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src) |
224 | { | |
225 | struct nft_counter_percpu_priv *priv = nft_expr_priv(src); | |
226 | struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst); | |
d84701ec PN |
227 | struct nft_counter __percpu *cpu_stats; |
228 | struct nft_counter *this_cpu; | |
086f3321 PNA |
229 | struct nft_counter total; |
230 | ||
d84701ec | 231 | nft_counter_fetch(priv, &total); |
086f3321 | 232 | |
d84701ec | 233 | cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC); |
086f3321 | 234 | if (cpu_stats == NULL) |
5cc6ce9f | 235 | return -ENOMEM; |
086f3321 PNA |
236 | |
237 | preempt_disable(); | |
238 | this_cpu = this_cpu_ptr(cpu_stats); | |
d84701ec PN |
239 | this_cpu->packets = total.packets; |
240 | this_cpu->bytes = total.bytes; | |
086f3321 PNA |
241 | preempt_enable(); |
242 | ||
243 | priv_clone->counter = cpu_stats; | |
244 | return 0; | |
245 | } | |
246 | ||
ef1f7df9 PM |
247 | static struct nft_expr_type nft_counter_type; |
248 | static const struct nft_expr_ops nft_counter_ops = { | |
249 | .type = &nft_counter_type, | |
0c45e769 | 250 | .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)), |
96518518 PM |
251 | .eval = nft_counter_eval, |
252 | .init = nft_counter_init, | |
0c45e769 | 253 | .destroy = nft_counter_destroy, |
96518518 | 254 | .dump = nft_counter_dump, |
086f3321 | 255 | .clone = nft_counter_clone, |
96518518 PM |
256 | }; |
257 | ||
ef1f7df9 PM |
258 | static struct nft_expr_type nft_counter_type __read_mostly = { |
259 | .name = "counter", | |
260 | .ops = &nft_counter_ops, | |
261 | .policy = nft_counter_policy, | |
262 | .maxattr = NFTA_COUNTER_MAX, | |
151d799a | 263 | .flags = NFT_EXPR_STATEFUL, |
ef1f7df9 PM |
264 | .owner = THIS_MODULE, |
265 | }; | |
266 | ||
96518518 PM |
267 | static int __init nft_counter_module_init(void) |
268 | { | |
d84701ec PN |
269 | int cpu, err; |
270 | ||
271 | for_each_possible_cpu(cpu) | |
272 | seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu)); | |
b1ce0ced PNA |
273 | |
274 | err = nft_register_obj(&nft_counter_obj); | |
275 | if (err < 0) | |
276 | return err; | |
277 | ||
278 | err = nft_register_expr(&nft_counter_type); | |
279 | if (err < 0) | |
280 | goto err1; | |
281 | ||
282 | return 0; | |
283 | err1: | |
284 | nft_unregister_obj(&nft_counter_obj); | |
285 | return err; | |
96518518 PM |
286 | } |
287 | ||
288 | static void __exit nft_counter_module_exit(void) | |
289 | { | |
ef1f7df9 | 290 | nft_unregister_expr(&nft_counter_type); |
b1ce0ced | 291 | nft_unregister_obj(&nft_counter_obj); |
96518518 PM |
292 | } |
293 | ||
294 | module_init(nft_counter_module_init); | |
295 | module_exit(nft_counter_module_exit); | |
296 | ||
297 | MODULE_LICENSE("GPL"); | |
298 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
299 | MODULE_ALIAS_NFT_EXPR("counter"); | |
b1ce0ced | 300 | MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER); |