Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  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 {
 21	s64		bytes;
 22	s64		packets;
 23};
 24
 25struct nft_counter_percpu_priv {
 26	struct nft_counter __percpu *counter;
 27};
 28
 29static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
 30
 31static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
 32				       struct nft_regs *regs,
 33				       const struct nft_pktinfo *pkt)
 34{
 35	struct nft_counter *this_cpu;
 36	seqcount_t *myseq;
 37
 38	local_bh_disable();
 39	this_cpu = this_cpu_ptr(priv->counter);
 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);
 48	local_bh_enable();
 49}
 50
 51static 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
 60static int nft_counter_do_init(const struct nlattr * const tb[],
 61			       struct nft_counter_percpu_priv *priv)
 62{
 63	struct nft_counter __percpu *cpu_stats;
 64	struct nft_counter *this_cpu;
 65
 66	cpu_stats = alloc_percpu(struct nft_counter);
 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]) {
 73	        this_cpu->packets =
 74			be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
 75	}
 76	if (tb[NFTA_COUNTER_BYTES]) {
 77		this_cpu->bytes =
 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
 85static 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
 93static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
 94{
 95	free_percpu(priv->counter);
 96}
 97
 98static 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
105static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv,
106			      struct nft_counter *total)
107{
108	struct nft_counter *this_cpu;
109
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();
115}
116
117static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
118			      struct nft_counter *total)
119{
120	struct nft_counter *this_cpu;
121	const seqcount_t *myseq;
122	u64 bytes, packets;
123	unsigned int seq;
124	int cpu;
125
126	memset(total, 0, sizeof(*total));
127	for_each_possible_cpu(cpu) {
128		myseq = per_cpu_ptr(&nft_counter_seq, cpu);
129		this_cpu = per_cpu_ptr(priv->counter, cpu);
130		do {
131			seq	= read_seqcount_begin(myseq);
132			bytes	= this_cpu->bytes;
133			packets	= this_cpu->packets;
134		} while (read_seqcount_retry(myseq, seq));
135
136		total->bytes	+= bytes;
137		total->packets	+= packets;
138	}
139}
140
141static int nft_counter_do_dump(struct sk_buff *skb,
142			       struct nft_counter_percpu_priv *priv,
143			       bool reset)
144{
145	struct nft_counter total;
146
147	nft_counter_fetch(priv, &total);
148
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))
153		goto nla_put_failure;
154
155	if (reset)
156		nft_counter_reset(priv, &total);
157
158	return 0;
159
160nla_put_failure:
161	return -1;
162}
163
164static int nft_counter_obj_dump(struct sk_buff *skb,
165				struct nft_object *obj, bool reset)
166{
167	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
168
169	return nft_counter_do_dump(skb, priv, reset);
170}
171
172static 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
177static 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
189static 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
198static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
 
199{
200	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
201
202	return nft_counter_do_dump(skb, priv, false);
203}
204
205static int nft_counter_init(const struct nft_ctx *ctx,
206			    const struct nft_expr *expr,
207			    const struct nlattr * const tb[])
208{
209	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
210
211	return nft_counter_do_init(tb, priv);
212}
213
214static 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);
218
219	nft_counter_do_destroy(priv);
220}
221
222static 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);
226	struct nft_counter __percpu *cpu_stats;
227	struct nft_counter *this_cpu;
228	struct nft_counter total;
229
230	nft_counter_fetch(priv, &total);
231
232	cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
233	if (cpu_stats == NULL)
234		return -ENOMEM;
235
236	preempt_disable();
237	this_cpu = this_cpu_ptr(cpu_stats);
238	this_cpu->packets = total.packets;
239	this_cpu->bytes = total.bytes;
240	preempt_enable();
241
242	priv_clone->counter = cpu_stats;
243	return 0;
244}
245
246static struct nft_expr_type nft_counter_type;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247static const struct nft_expr_ops nft_counter_ops = {
248	.type		= &nft_counter_type,
249	.size		= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
250	.eval		= nft_counter_eval,
251	.init		= nft_counter_init,
252	.destroy	= nft_counter_destroy,
 
253	.dump		= nft_counter_dump,
254	.clone		= nft_counter_clone,
 
 
 
255};
256
257static 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,
262	.flags		= NFT_EXPR_STATEFUL,
263	.owner		= THIS_MODULE,
264};
265
266static int __init nft_counter_module_init(void)
267{
268	int cpu, err;
269
270	for_each_possible_cpu(cpu)
271		seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
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;
282err1:
283	nft_unregister_obj(&nft_counter_obj);
284	return err;
285}
286
287static void __exit nft_counter_module_exit(void)
288{
289	nft_unregister_expr(&nft_counter_type);
290	nft_unregister_obj(&nft_counter_obj);
291}
292
293module_init(nft_counter_module_init);
294module_exit(nft_counter_module_exit);
295
296MODULE_LICENSE("GPL");
297MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
298MODULE_ALIAS_NFT_EXPR("counter");
299MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  4 *
 
 
 
 
  5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
  6 */
  7
  8#include <linux/kernel.h>
  9#include <linux/init.h>
 10#include <linux/module.h>
 11#include <linux/seqlock.h>
 12#include <linux/netlink.h>
 13#include <linux/netfilter.h>
 14#include <linux/netfilter/nf_tables.h>
 15#include <net/netfilter/nf_tables.h>
 16#include <net/netfilter/nf_tables_core.h>
 17#include <net/netfilter/nf_tables_offload.h>
 18
 19struct nft_counter {
 20	s64		bytes;
 21	s64		packets;
 22};
 23
 24struct nft_counter_percpu_priv {
 25	struct nft_counter __percpu *counter;
 26};
 27
 28static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
 29
 30static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
 31				       struct nft_regs *regs,
 32				       const struct nft_pktinfo *pkt)
 33{
 34	struct nft_counter *this_cpu;
 35	seqcount_t *myseq;
 36
 37	local_bh_disable();
 38	this_cpu = this_cpu_ptr(priv->counter);
 39	myseq = this_cpu_ptr(&nft_counter_seq);
 40
 41	write_seqcount_begin(myseq);
 42
 43	this_cpu->bytes += pkt->skb->len;
 44	this_cpu->packets++;
 45
 46	write_seqcount_end(myseq);
 47	local_bh_enable();
 48}
 49
 50static inline void nft_counter_obj_eval(struct nft_object *obj,
 51					struct nft_regs *regs,
 52					const struct nft_pktinfo *pkt)
 53{
 54	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
 55
 56	nft_counter_do_eval(priv, regs, pkt);
 57}
 58
 59static int nft_counter_do_init(const struct nlattr * const tb[],
 60			       struct nft_counter_percpu_priv *priv)
 61{
 62	struct nft_counter __percpu *cpu_stats;
 63	struct nft_counter *this_cpu;
 64
 65	cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_KERNEL_ACCOUNT);
 66	if (cpu_stats == NULL)
 67		return -ENOMEM;
 68
 69	preempt_disable();
 70	this_cpu = this_cpu_ptr(cpu_stats);
 71	if (tb[NFTA_COUNTER_PACKETS]) {
 72	        this_cpu->packets =
 73			be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
 74	}
 75	if (tb[NFTA_COUNTER_BYTES]) {
 76		this_cpu->bytes =
 77			be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
 78	}
 79	preempt_enable();
 80	priv->counter = cpu_stats;
 81	return 0;
 82}
 83
 84static int nft_counter_obj_init(const struct nft_ctx *ctx,
 85				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
 93static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
 94{
 95	free_percpu(priv->counter);
 96}
 97
 98static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
 99				    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
106static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
107			      struct nft_counter *total)
108{
109	struct nft_counter *this_cpu;
110
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();
116}
117
118static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
119			      struct nft_counter *total)
120{
121	struct nft_counter *this_cpu;
122	const seqcount_t *myseq;
123	u64 bytes, packets;
124	unsigned int seq;
125	int cpu;
126
127	memset(total, 0, sizeof(*total));
128	for_each_possible_cpu(cpu) {
129		myseq = per_cpu_ptr(&nft_counter_seq, cpu);
130		this_cpu = per_cpu_ptr(priv->counter, cpu);
131		do {
132			seq	= read_seqcount_begin(myseq);
133			bytes	= this_cpu->bytes;
134			packets	= this_cpu->packets;
135		} while (read_seqcount_retry(myseq, seq));
136
137		total->bytes	+= bytes;
138		total->packets	+= packets;
139	}
140}
141
142static int nft_counter_do_dump(struct sk_buff *skb,
143			       struct nft_counter_percpu_priv *priv,
144			       bool reset)
145{
146	struct nft_counter total;
147
148	nft_counter_fetch(priv, &total);
149
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))
154		goto nla_put_failure;
155
156	if (reset)
157		nft_counter_reset(priv, &total);
158
159	return 0;
160
161nla_put_failure:
162	return -1;
163}
164
165static int nft_counter_obj_dump(struct sk_buff *skb,
166				struct nft_object *obj, bool reset)
167{
168	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
169
170	return nft_counter_do_dump(skb, priv, reset);
171}
172
173static 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
178struct nft_object_type nft_counter_obj_type;
179static const struct nft_object_ops nft_counter_obj_ops = {
180	.type		= &nft_counter_obj_type,
181	.size		= sizeof(struct nft_counter_percpu_priv),
 
 
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};
187
188struct nft_object_type nft_counter_obj_type __read_mostly = {
189	.type		= NFT_OBJECT_COUNTER,
190	.ops		= &nft_counter_obj_ops,
191	.maxattr	= NFTA_COUNTER_MAX,
192	.policy		= nft_counter_policy,
193	.owner		= THIS_MODULE,
194};
195
196void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
197		      const struct nft_pktinfo *pkt)
 
198{
199	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
200
201	nft_counter_do_eval(priv, regs, pkt);
202}
203
204static int nft_counter_dump(struct sk_buff *skb,
205			    const struct nft_expr *expr, bool reset)
206{
207	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
208
209	return nft_counter_do_dump(skb, priv, reset);
210}
211
212static int nft_counter_init(const struct nft_ctx *ctx,
213			    const struct nft_expr *expr,
214			    const struct nlattr * const tb[])
215{
216	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
217
218	return nft_counter_do_init(tb, priv);
219}
220
221static void nft_counter_destroy(const struct nft_ctx *ctx,
222				const struct nft_expr *expr)
223{
224	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
225
226	nft_counter_do_destroy(priv);
227}
228
229static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
230{
231	struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
232	struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
233	struct nft_counter __percpu *cpu_stats;
234	struct nft_counter *this_cpu;
235	struct nft_counter total;
236
237	nft_counter_fetch(priv, &total);
238
239	cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
240	if (cpu_stats == NULL)
241		return -ENOMEM;
242
243	preempt_disable();
244	this_cpu = this_cpu_ptr(cpu_stats);
245	this_cpu->packets = total.packets;
246	this_cpu->bytes = total.bytes;
247	preempt_enable();
248
249	priv_clone->counter = cpu_stats;
250	return 0;
251}
252
253static int nft_counter_offload(struct nft_offload_ctx *ctx,
254			       struct nft_flow_rule *flow,
255			       const struct nft_expr *expr)
256{
257	/* No specific offload action is needed, but report success. */
258	return 0;
259}
260
261static void nft_counter_offload_stats(struct nft_expr *expr,
262				      const struct flow_stats *stats)
263{
264	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
265	struct nft_counter *this_cpu;
266	seqcount_t *myseq;
267
268	preempt_disable();
269	this_cpu = this_cpu_ptr(priv->counter);
270	myseq = this_cpu_ptr(&nft_counter_seq);
271
272	write_seqcount_begin(myseq);
273	this_cpu->packets += stats->pkts;
274	this_cpu->bytes += stats->bytes;
275	write_seqcount_end(myseq);
276	preempt_enable();
277}
278
279void nft_counter_init_seqcount(void)
280{
281	int cpu;
282
283	for_each_possible_cpu(cpu)
284		seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
285}
286
287struct nft_expr_type nft_counter_type;
288static const struct nft_expr_ops nft_counter_ops = {
289	.type		= &nft_counter_type,
290	.size		= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
291	.eval		= nft_counter_eval,
292	.init		= nft_counter_init,
293	.destroy	= nft_counter_destroy,
294	.destroy_clone	= nft_counter_destroy,
295	.dump		= nft_counter_dump,
296	.clone		= nft_counter_clone,
297	.reduce		= NFT_REDUCE_READONLY,
298	.offload	= nft_counter_offload,
299	.offload_stats	= nft_counter_offload_stats,
300};
301
302struct nft_expr_type nft_counter_type __read_mostly = {
303	.name		= "counter",
304	.ops		= &nft_counter_ops,
305	.policy		= nft_counter_policy,
306	.maxattr	= NFTA_COUNTER_MAX,
307	.flags		= NFT_EXPR_STATEFUL,
308	.owner		= THIS_MODULE,
309};