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