blob: c9743f78f21999ae01ed735a63c4e856e79cdc37 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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 {
Patrick McHardy96518512013-10-14 11:00:02 +020021 u64 bytes;
22 u64 packets;
23};
24
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020025struct 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
Patrick McHardy96518512013-10-14 11:00:02 +020034static void nft_counter_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010035 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +020036 const struct nft_pktinfo *pkt)
37{
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020038 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
39 struct nft_counter_percpu *this_cpu;
Patrick McHardy96518512013-10-14 11:00:02 +020040
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020041 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();
Patrick McHardy96518512013-10-14 11:00:02 +020048}
49
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010050static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
51 struct nft_counter *total)
Patrick McHardy96518512013-10-14 11:00:02 +020052{
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010053 const struct nft_counter_percpu *cpu_stats;
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020054 u64 bytes, packets;
Patrick McHardy96518512013-10-14 11:00:02 +020055 unsigned int seq;
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020056 int cpu;
Patrick McHardy96518512013-10-14 11:00:02 +020057
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010058 memset(total, 0, sizeof(*total));
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020059 for_each_possible_cpu(cpu) {
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010060 cpu_stats = per_cpu_ptr(counter, cpu);
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020061 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));
Patrick McHardy96518512013-10-14 11:00:02 +020066
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010067 total->packets += packets;
68 total->bytes += bytes;
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020069 }
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010070}
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);
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020078
79 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)) ||
80 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets)))
Patrick McHardy96518512013-10-14 11:00:02 +020081 goto nla_put_failure;
82 return 0;
83
84nla_put_failure:
85 return -1;
86}
87
88static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
89 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
90 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
91};
92
93static int nft_counter_init(const struct nft_ctx *ctx,
94 const struct nft_expr *expr,
95 const struct nlattr * const tb[])
96{
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +020097 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
98 struct nft_counter_percpu __percpu *cpu_stats;
99 struct nft_counter_percpu *this_cpu;
Patrick McHardy96518512013-10-14 11:00:02 +0200100
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +0200101 cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu);
102 if (cpu_stats == NULL)
Anton Protopopov5cc6ce92016-02-06 23:31:19 -0500103 return -ENOMEM;
Patrick McHardy96518512013-10-14 11:00:02 +0200104
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +0200105 preempt_disable();
106 this_cpu = this_cpu_ptr(cpu_stats);
107 if (tb[NFTA_COUNTER_PACKETS]) {
108 this_cpu->counter.packets =
109 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
110 }
111 if (tb[NFTA_COUNTER_BYTES]) {
112 this_cpu->counter.bytes =
113 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
114 }
115 preempt_enable();
116 priv->counter = cpu_stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200117 return 0;
118}
119
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +0200120static void nft_counter_destroy(const struct nft_ctx *ctx,
121 const struct nft_expr *expr)
122{
123 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
124
125 free_percpu(priv->counter);
126}
127
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100128static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
129{
130 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
131 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
132 struct nft_counter_percpu __percpu *cpu_stats;
133 struct nft_counter_percpu *this_cpu;
134 struct nft_counter total;
135
136 nft_counter_fetch(priv->counter, &total);
137
138 cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
139 GFP_ATOMIC);
140 if (cpu_stats == NULL)
Anton Protopopov5cc6ce92016-02-06 23:31:19 -0500141 return -ENOMEM;
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100142
143 preempt_disable();
144 this_cpu = this_cpu_ptr(cpu_stats);
145 this_cpu->counter.packets = total.packets;
146 this_cpu->counter.bytes = total.bytes;
147 preempt_enable();
148
149 priv_clone->counter = cpu_stats;
150 return 0;
151}
152
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200153static struct nft_expr_type nft_counter_type;
154static const struct nft_expr_ops nft_counter_ops = {
155 .type = &nft_counter_type,
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +0200156 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
Patrick McHardy96518512013-10-14 11:00:02 +0200157 .eval = nft_counter_eval,
158 .init = nft_counter_init,
Pablo Neira Ayuso0c45e762015-06-08 14:42:40 +0200159 .destroy = nft_counter_destroy,
Patrick McHardy96518512013-10-14 11:00:02 +0200160 .dump = nft_counter_dump,
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100161 .clone = nft_counter_clone,
Patrick McHardy96518512013-10-14 11:00:02 +0200162};
163
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200164static struct nft_expr_type nft_counter_type __read_mostly = {
165 .name = "counter",
166 .ops = &nft_counter_ops,
167 .policy = nft_counter_policy,
168 .maxattr = NFTA_COUNTER_MAX,
Patrick McHardy151d7992015-04-11 10:46:40 +0100169 .flags = NFT_EXPR_STATEFUL,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200170 .owner = THIS_MODULE,
171};
172
Patrick McHardy96518512013-10-14 11:00:02 +0200173static int __init nft_counter_module_init(void)
174{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200175 return nft_register_expr(&nft_counter_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200176}
177
178static void __exit nft_counter_module_exit(void)
179{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200180 nft_unregister_expr(&nft_counter_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200181}
182
183module_init(nft_counter_module_init);
184module_exit(nft_counter_module_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
188MODULE_ALIAS_NFT_EXPR("counter");