Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[deliverable/linux.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
96518518 34static void nft_counter_eval(const struct nft_expr *expr,
a55e22e9 35 struct nft_regs *regs,
96518518
PM
36 const struct nft_pktinfo *pkt)
37{
0c45e769
PNA
38 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
39 struct nft_counter_percpu *this_cpu;
40
41 local_bh_disable();
42 this_cpu = this_cpu_ptr(priv->counter);
43 u64_stats_update_begin(&this_cpu->syncp);
44 this_cpu->counter.bytes += pkt->skb->len;
45 this_cpu->counter.packets++;
46 u64_stats_update_end(&this_cpu->syncp);
47 local_bh_enable();
96518518
PM
48}
49
086f3321
PNA
50static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
51 struct nft_counter *total)
96518518 52{
086f3321 53 const struct nft_counter_percpu *cpu_stats;
0c45e769 54 u64 bytes, packets;
96518518 55 unsigned int seq;
0c45e769
PNA
56 int cpu;
57
086f3321 58 memset(total, 0, sizeof(*total));
0c45e769 59 for_each_possible_cpu(cpu) {
086f3321 60 cpu_stats = per_cpu_ptr(counter, cpu);
0c45e769
PNA
61 do {
62 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
63 bytes = cpu_stats->counter.bytes;
64 packets = cpu_stats->counter.packets;
65 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
66
086f3321
PNA
67 total->packets += packets;
68 total->bytes += bytes;
0c45e769 69 }
086f3321
PNA
70}
71
72static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
73{
74 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
75 struct nft_counter total;
76
77 nft_counter_fetch(priv->counter, &total);
0c45e769 78
b46f6ded
ND
79 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
80 NFTA_COUNTER_PAD) ||
81 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
82 NFTA_COUNTER_PAD))
96518518
PM
83 goto nla_put_failure;
84 return 0;
85
86nla_put_failure:
87 return -1;
88}
89
90static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
91 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
92 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
93};
94
95static int nft_counter_init(const struct nft_ctx *ctx,
96 const struct nft_expr *expr,
97 const struct nlattr * const tb[])
98{
0c45e769
PNA
99 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
100 struct nft_counter_percpu __percpu *cpu_stats;
101 struct nft_counter_percpu *this_cpu;
102
103 cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
104 if (cpu_stats == NULL)
5cc6ce9f 105 return -ENOMEM;
0c45e769
PNA
106
107 preempt_disable();
108 this_cpu = this_cpu_ptr(cpu_stats);
109 if (tb[NFTA_COUNTER_PACKETS]) {
110 this_cpu->counter.packets =
111 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
112 }
113 if (tb[NFTA_COUNTER_BYTES]) {
114 this_cpu->counter.bytes =
115 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
116 }
117 preempt_enable();
118 priv->counter = cpu_stats;
119 return 0;
120}
96518518 121
0c45e769
PNA
122static void nft_counter_destroy(const struct nft_ctx *ctx,
123 const struct nft_expr *expr)
124{
125 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
96518518 126
0c45e769 127 free_percpu(priv->counter);
96518518
PM
128}
129
086f3321
PNA
130static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
131{
132 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
133 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
134 struct nft_counter_percpu __percpu *cpu_stats;
135 struct nft_counter_percpu *this_cpu;
136 struct nft_counter total;
137
138 nft_counter_fetch(priv->counter, &total);
139
140 cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
141 GFP_ATOMIC);
142 if (cpu_stats == NULL)
5cc6ce9f 143 return -ENOMEM;
086f3321
PNA
144
145 preempt_disable();
146 this_cpu = this_cpu_ptr(cpu_stats);
147 this_cpu->counter.packets = total.packets;
148 this_cpu->counter.bytes = total.bytes;
149 preempt_enable();
150
151 priv_clone->counter = cpu_stats;
152 return 0;
153}
154
ef1f7df9
PM
155static struct nft_expr_type nft_counter_type;
156static const struct nft_expr_ops nft_counter_ops = {
157 .type = &nft_counter_type,
0c45e769 158 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
96518518
PM
159 .eval = nft_counter_eval,
160 .init = nft_counter_init,
0c45e769 161 .destroy = nft_counter_destroy,
96518518 162 .dump = nft_counter_dump,
086f3321 163 .clone = nft_counter_clone,
96518518
PM
164};
165
ef1f7df9
PM
166static struct nft_expr_type nft_counter_type __read_mostly = {
167 .name = "counter",
168 .ops = &nft_counter_ops,
169 .policy = nft_counter_policy,
170 .maxattr = NFTA_COUNTER_MAX,
151d799a 171 .flags = NFT_EXPR_STATEFUL,
ef1f7df9
PM
172 .owner = THIS_MODULE,
173};
174
96518518
PM
175static int __init nft_counter_module_init(void)
176{
ef1f7df9 177 return nft_register_expr(&nft_counter_type);
96518518
PM
178}
179
180static void __exit nft_counter_module_exit(void)
181{
ef1f7df9 182 nft_unregister_expr(&nft_counter_type);
96518518
PM
183}
184
185module_init(nft_counter_module_init);
186module_exit(nft_counter_module_exit);
187
188MODULE_LICENSE("GPL");
189MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
190MODULE_ALIAS_NFT_EXPR("counter");
This page took 0.154421 seconds and 5 git commands to generate.