]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_counter.c
netfilter: nft_quota: dump consumed quota
[mirror_ubuntu-bionic-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 {
96518518
PM
21 u64 bytes;
22 u64 packets;
23};
24
0c45e769
PNA
25struct nft_counter_percpu {
26 struct nft_counter counter;
27 struct u64_stats_sync syncp;
28};
29
30struct nft_counter_percpu_priv {
31 struct nft_counter_percpu __percpu *counter;
32};
33
b1ce0ced
PNA
34static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
35 struct nft_regs *regs,
36 const struct nft_pktinfo *pkt)
96518518 37{
0c45e769
PNA
38 struct nft_counter_percpu *this_cpu;
39
40 local_bh_disable();
41 this_cpu = this_cpu_ptr(priv->counter);
42 u64_stats_update_begin(&this_cpu->syncp);
43 this_cpu->counter.bytes += pkt->skb->len;
44 this_cpu->counter.packets++;
45 u64_stats_update_end(&this_cpu->syncp);
46 local_bh_enable();
96518518
PM
47}
48
b1ce0ced
PNA
49static inline void nft_counter_obj_eval(struct nft_object *obj,
50 struct nft_regs *regs,
51 const struct nft_pktinfo *pkt)
52{
53 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
54
55 nft_counter_do_eval(priv, regs, pkt);
56}
57
58static int nft_counter_do_init(const struct nlattr * const tb[],
59 struct nft_counter_percpu_priv *priv)
60{
61 struct nft_counter_percpu __percpu *cpu_stats;
62 struct nft_counter_percpu *this_cpu;
63
64 cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
65 if (cpu_stats == NULL)
66 return -ENOMEM;
67
68 preempt_disable();
69 this_cpu = this_cpu_ptr(cpu_stats);
70 if (tb[NFTA_COUNTER_PACKETS]) {
71 this_cpu->counter.packets =
72 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
73 }
74 if (tb[NFTA_COUNTER_BYTES]) {
75 this_cpu->counter.bytes =
76 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
77 }
78 preempt_enable();
79 priv->counter = cpu_stats;
80 return 0;
81}
82
83static int nft_counter_obj_init(const struct nlattr * const tb[],
84 struct nft_object *obj)
85{
86 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
87
88 return nft_counter_do_init(tb, priv);
89}
90
91static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
92{
93 free_percpu(priv->counter);
94}
95
96static void nft_counter_obj_destroy(struct nft_object *obj)
97{
98 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
99
100 nft_counter_do_destroy(priv);
101}
102
086f3321
PNA
103static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
104 struct nft_counter *total)
96518518 105{
086f3321 106 const struct nft_counter_percpu *cpu_stats;
0c45e769 107 u64 bytes, packets;
96518518 108 unsigned int seq;
0c45e769
PNA
109 int cpu;
110
086f3321 111 memset(total, 0, sizeof(*total));
0c45e769 112 for_each_possible_cpu(cpu) {
086f3321 113 cpu_stats = per_cpu_ptr(counter, cpu);
0c45e769
PNA
114 do {
115 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
116 bytes = cpu_stats->counter.bytes;
117 packets = cpu_stats->counter.packets;
118 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
119
086f3321
PNA
120 total->packets += packets;
121 total->bytes += bytes;
0c45e769 122 }
086f3321
PNA
123}
124
b1ce0ced
PNA
125static int nft_counter_do_dump(struct sk_buff *skb,
126 const struct nft_counter_percpu_priv *priv)
086f3321 127{
086f3321
PNA
128 struct nft_counter total;
129
130 nft_counter_fetch(priv->counter, &total);
0c45e769 131
b46f6ded
ND
132 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
133 NFTA_COUNTER_PAD) ||
134 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
135 NFTA_COUNTER_PAD))
96518518
PM
136 goto nla_put_failure;
137 return 0;
138
139nla_put_failure:
140 return -1;
141}
142
b1ce0ced
PNA
143static int nft_counter_obj_dump(struct sk_buff *skb,
144 const struct nft_object *obj)
145{
146 const struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
147
148 return nft_counter_do_dump(skb, priv);
149}
150
96518518
PM
151static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
152 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
153 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
154};
155
b1ce0ced
PNA
156static struct nft_object_type nft_counter_obj __read_mostly = {
157 .type = NFT_OBJECT_COUNTER,
158 .size = sizeof(struct nft_counter_percpu_priv),
159 .maxattr = NFTA_COUNTER_MAX,
160 .policy = nft_counter_policy,
161 .eval = nft_counter_obj_eval,
162 .init = nft_counter_obj_init,
163 .destroy = nft_counter_obj_destroy,
164 .dump = nft_counter_obj_dump,
165 .owner = THIS_MODULE,
166};
167
168static void nft_counter_eval(const struct nft_expr *expr,
169 struct nft_regs *regs,
170 const struct nft_pktinfo *pkt)
171{
172 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
173
174 nft_counter_do_eval(priv, regs, pkt);
175}
176
177static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
178{
179 const struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
180
181 return nft_counter_do_dump(skb, priv);
182}
183
96518518
PM
184static int nft_counter_init(const struct nft_ctx *ctx,
185 const struct nft_expr *expr,
186 const struct nlattr * const tb[])
187{
0c45e769 188 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
0c45e769 189
b1ce0ced 190 return nft_counter_do_init(tb, priv);
0c45e769 191}
96518518 192
0c45e769
PNA
193static void nft_counter_destroy(const struct nft_ctx *ctx,
194 const struct nft_expr *expr)
195{
196 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
96518518 197
b1ce0ced 198 nft_counter_do_destroy(priv);
96518518
PM
199}
200
086f3321
PNA
201static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
202{
203 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
204 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
205 struct nft_counter_percpu __percpu *cpu_stats;
206 struct nft_counter_percpu *this_cpu;
207 struct nft_counter total;
208
209 nft_counter_fetch(priv->counter, &total);
210
211 cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
212 GFP_ATOMIC);
213 if (cpu_stats == NULL)
5cc6ce9f 214 return -ENOMEM;
086f3321
PNA
215
216 preempt_disable();
217 this_cpu = this_cpu_ptr(cpu_stats);
218 this_cpu->counter.packets = total.packets;
219 this_cpu->counter.bytes = total.bytes;
220 preempt_enable();
221
222 priv_clone->counter = cpu_stats;
223 return 0;
224}
225
ef1f7df9
PM
226static struct nft_expr_type nft_counter_type;
227static const struct nft_expr_ops nft_counter_ops = {
228 .type = &nft_counter_type,
0c45e769 229 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
96518518
PM
230 .eval = nft_counter_eval,
231 .init = nft_counter_init,
0c45e769 232 .destroy = nft_counter_destroy,
96518518 233 .dump = nft_counter_dump,
086f3321 234 .clone = nft_counter_clone,
96518518
PM
235};
236
ef1f7df9
PM
237static struct nft_expr_type nft_counter_type __read_mostly = {
238 .name = "counter",
239 .ops = &nft_counter_ops,
240 .policy = nft_counter_policy,
241 .maxattr = NFTA_COUNTER_MAX,
151d799a 242 .flags = NFT_EXPR_STATEFUL,
ef1f7df9
PM
243 .owner = THIS_MODULE,
244};
245
96518518
PM
246static int __init nft_counter_module_init(void)
247{
b1ce0ced
PNA
248 int err;
249
250 err = nft_register_obj(&nft_counter_obj);
251 if (err < 0)
252 return err;
253
254 err = nft_register_expr(&nft_counter_type);
255 if (err < 0)
256 goto err1;
257
258 return 0;
259err1:
260 nft_unregister_obj(&nft_counter_obj);
261 return err;
96518518
PM
262}
263
264static void __exit nft_counter_module_exit(void)
265{
ef1f7df9 266 nft_unregister_expr(&nft_counter_type);
b1ce0ced 267 nft_unregister_obj(&nft_counter_obj);
96518518
PM
268}
269
270module_init(nft_counter_module_init);
271module_exit(nft_counter_module_exit);
272
273MODULE_LICENSE("GPL");
274MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
275MODULE_ALIAS_NFT_EXPR("counter");
b1ce0ced 276MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);