Linux Audio

Check our new training course

Loading...
v5.14.15
  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_offload.h>
 17
 18struct nft_counter {
 
 
 
 
 
 19	s64		bytes;
 20	s64		packets;
 21};
 22
 23struct nft_counter_percpu_priv {
 24	struct nft_counter __percpu *counter;
 25};
 26
 27static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
 28
 29static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
 30				       struct nft_regs *regs,
 31				       const struct nft_pktinfo *pkt)
 32{
 
 33	struct nft_counter *this_cpu;
 34	seqcount_t *myseq;
 35
 36	local_bh_disable();
 37	this_cpu = this_cpu_ptr(priv->counter);
 38	myseq = this_cpu_ptr(&nft_counter_seq);
 39
 40	write_seqcount_begin(myseq);
 
 
 
 41
 42	this_cpu->bytes += pkt->skb->len;
 43	this_cpu->packets++;
 44
 45	write_seqcount_end(myseq);
 46	local_bh_enable();
 47}
 48
 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 *cpu_stats;
 62	struct nft_counter *this_cpu;
 63
 64	cpu_stats = alloc_percpu(struct nft_counter);
 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->packets =
 72			be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
 73	}
 74	if (tb[NFTA_COUNTER_BYTES]) {
 75		this_cpu->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 nft_ctx *ctx,
 84				const struct nlattr * const tb[],
 85				struct nft_object *obj)
 86{
 87	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
 88
 89	return nft_counter_do_init(tb, priv);
 90}
 91
 92static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
 93{
 94	free_percpu(priv->counter);
 95}
 96
 97static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
 98				    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 *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_type;
178static const struct nft_object_ops nft_counter_obj_ops = {
179	.type		= &nft_counter_obj_type,
180	.size		= sizeof(struct nft_counter_percpu_priv),
181	.eval		= nft_counter_obj_eval,
182	.init		= nft_counter_obj_init,
183	.destroy	= nft_counter_obj_destroy,
184	.dump		= nft_counter_obj_dump,
185};
186
187static struct nft_object_type nft_counter_obj_type __read_mostly = {
188	.type		= NFT_OBJECT_COUNTER,
189	.ops		= &nft_counter_obj_ops,
190	.maxattr	= NFTA_COUNTER_MAX,
191	.policy		= nft_counter_policy,
192	.owner		= THIS_MODULE,
193};
194
195static void nft_counter_eval(const struct nft_expr *expr,
196			     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, const struct nft_expr *expr)
 
205{
206	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
207
208	return nft_counter_do_dump(skb, priv, false);
209}
210
211static int nft_counter_init(const struct nft_ctx *ctx,
212			    const struct nft_expr *expr,
213			    const struct nlattr * const tb[])
214{
215	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
216
217	return nft_counter_do_init(tb, priv);
218}
219
220static void nft_counter_destroy(const struct nft_ctx *ctx,
221				const struct nft_expr *expr)
222{
223	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
224
225	nft_counter_do_destroy(priv);
226}
227
228static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
229{
230	struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
231	struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
232	struct nft_counter __percpu *cpu_stats;
233	struct nft_counter *this_cpu;
234	struct nft_counter total;
235
236	nft_counter_fetch(priv, &total);
237
238	cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
239	if (cpu_stats == NULL)
240		return -ENOMEM;
241
242	preempt_disable();
243	this_cpu = this_cpu_ptr(cpu_stats);
244	this_cpu->packets = total.packets;
245	this_cpu->bytes = total.bytes;
246	preempt_enable();
247
248	priv_clone->counter = cpu_stats;
249	return 0;
250}
251
252static int nft_counter_offload(struct nft_offload_ctx *ctx,
253			       struct nft_flow_rule *flow,
254			       const struct nft_expr *expr)
255{
256	/* No specific offload action is needed, but report success. */
257	return 0;
258}
259
260static void nft_counter_offload_stats(struct nft_expr *expr,
261				      const struct flow_stats *stats)
262{
263	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
 
264	struct nft_counter *this_cpu;
265	seqcount_t *myseq;
266
267	preempt_disable();
268	this_cpu = this_cpu_ptr(priv->counter);
269	myseq = this_cpu_ptr(&nft_counter_seq);
270
271	write_seqcount_begin(myseq);
272	this_cpu->packets += stats->pkts;
273	this_cpu->bytes += stats->bytes;
274	write_seqcount_end(myseq);
275	preempt_enable();
276}
277
278static struct nft_expr_type nft_counter_type;
 
 
 
 
 
 
 
 
279static const struct nft_expr_ops nft_counter_ops = {
280	.type		= &nft_counter_type,
281	.size		= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
282	.eval		= nft_counter_eval,
283	.init		= nft_counter_init,
284	.destroy	= nft_counter_destroy,
285	.destroy_clone	= nft_counter_destroy,
286	.dump		= nft_counter_dump,
287	.clone		= nft_counter_clone,
 
288	.offload	= nft_counter_offload,
289	.offload_stats	= nft_counter_offload_stats,
290};
291
292static struct nft_expr_type nft_counter_type __read_mostly = {
293	.name		= "counter",
294	.ops		= &nft_counter_ops,
295	.policy		= nft_counter_policy,
296	.maxattr	= NFTA_COUNTER_MAX,
297	.flags		= NFT_EXPR_STATEFUL,
298	.owner		= THIS_MODULE,
299};
300
301static int __init nft_counter_module_init(void)
302{
303	int cpu, err;
304
305	for_each_possible_cpu(cpu)
306		seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
307
308	err = nft_register_obj(&nft_counter_obj_type);
309	if (err < 0)
310		return err;
311
312	err = nft_register_expr(&nft_counter_type);
313	if (err < 0)
314		goto err1;
315
316	return 0;
317err1:
318	nft_unregister_obj(&nft_counter_obj_type);
319	return err;
320}
321
322static void __exit nft_counter_module_exit(void)
323{
324	nft_unregister_expr(&nft_counter_type);
325	nft_unregister_obj(&nft_counter_obj_type);
326}
327
328module_init(nft_counter_module_init);
329module_exit(nft_counter_module_exit);
330
331MODULE_LICENSE("GPL");
332MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
333MODULE_ALIAS_NFT_EXPR("counter");
334MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);
335MODULE_DESCRIPTION("nftables counter rule support");
v6.13.7
  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/u64_stats_sync.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	u64_stats_t	bytes;
 21	u64_stats_t	packets;
 22};
 23
 24struct nft_counter_tot {
 25	s64		bytes;
 26	s64		packets;
 27};
 28
 29struct nft_counter_percpu_priv {
 30	struct nft_counter __percpu *counter;
 31};
 32
 33static DEFINE_PER_CPU(struct u64_stats_sync, nft_counter_sync);
 34
 35static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
 36				       struct nft_regs *regs,
 37				       const struct nft_pktinfo *pkt)
 38{
 39	struct u64_stats_sync *nft_sync;
 40	struct nft_counter *this_cpu;
 
 41
 42	local_bh_disable();
 43	this_cpu = this_cpu_ptr(priv->counter);
 44	nft_sync = this_cpu_ptr(&nft_counter_sync);
 45
 46	u64_stats_update_begin(nft_sync);
 47	u64_stats_add(&this_cpu->bytes, pkt->skb->len);
 48	u64_stats_inc(&this_cpu->packets);
 49	u64_stats_update_end(nft_sync);
 50
 
 
 
 
 51	local_bh_enable();
 52}
 53
 54static inline void nft_counter_obj_eval(struct nft_object *obj,
 55					struct nft_regs *regs,
 56					const struct nft_pktinfo *pkt)
 57{
 58	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
 59
 60	nft_counter_do_eval(priv, regs, pkt);
 61}
 62
 63static int nft_counter_do_init(const struct nlattr * const tb[],
 64			       struct nft_counter_percpu_priv *priv)
 65{
 66	struct nft_counter __percpu *cpu_stats;
 67	struct nft_counter *this_cpu;
 68
 69	cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_KERNEL_ACCOUNT);
 70	if (cpu_stats == NULL)
 71		return -ENOMEM;
 72
 73	this_cpu = raw_cpu_ptr(cpu_stats);
 
 74	if (tb[NFTA_COUNTER_PACKETS]) {
 75		u64_stats_set(&this_cpu->packets,
 76			      be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])));
 77	}
 78	if (tb[NFTA_COUNTER_BYTES]) {
 79		u64_stats_set(&this_cpu->bytes,
 80			      be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])));
 81	}
 82
 83	priv->counter = cpu_stats;
 84	return 0;
 85}
 86
 87static int nft_counter_obj_init(const struct nft_ctx *ctx,
 88				const struct nlattr * const tb[],
 89				struct nft_object *obj)
 90{
 91	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
 92
 93	return nft_counter_do_init(tb, priv);
 94}
 95
 96static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
 97{
 98	free_percpu(priv->counter);
 99}
100
101static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
102				    struct nft_object *obj)
103{
104	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
105
106	nft_counter_do_destroy(priv);
107}
108
109static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
110			      struct nft_counter_tot *total)
111{
112	struct u64_stats_sync *nft_sync;
113	struct nft_counter *this_cpu;
114
115	local_bh_disable();
116	this_cpu = this_cpu_ptr(priv->counter);
117	nft_sync = this_cpu_ptr(&nft_counter_sync);
118
119	u64_stats_update_begin(nft_sync);
120	u64_stats_add(&this_cpu->packets, -total->packets);
121	u64_stats_add(&this_cpu->bytes, -total->bytes);
122	u64_stats_update_end(nft_sync);
123
124	local_bh_enable();
125}
126
127static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
128			      struct nft_counter_tot *total)
129{
130	struct nft_counter *this_cpu;
 
131	u64 bytes, packets;
132	unsigned int seq;
133	int cpu;
134
135	memset(total, 0, sizeof(*total));
136	for_each_possible_cpu(cpu) {
137		struct u64_stats_sync *nft_sync = per_cpu_ptr(&nft_counter_sync, cpu);
138
139		this_cpu = per_cpu_ptr(priv->counter, cpu);
140		do {
141			seq	= u64_stats_fetch_begin(nft_sync);
142			bytes	= u64_stats_read(&this_cpu->bytes);
143			packets	= u64_stats_read(&this_cpu->packets);
144		} while (u64_stats_fetch_retry(nft_sync, seq));
145
146		total->bytes	+= bytes;
147		total->packets	+= packets;
148	}
149}
150
151static int nft_counter_do_dump(struct sk_buff *skb,
152			       struct nft_counter_percpu_priv *priv,
153			       bool reset)
154{
155	struct nft_counter_tot total;
156
157	nft_counter_fetch(priv, &total);
158
159	if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
160			 NFTA_COUNTER_PAD) ||
161	    nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
162			 NFTA_COUNTER_PAD))
163		goto nla_put_failure;
164
165	if (reset)
166		nft_counter_reset(priv, &total);
167
168	return 0;
169
170nla_put_failure:
171	return -1;
172}
173
174static int nft_counter_obj_dump(struct sk_buff *skb,
175				struct nft_object *obj, bool reset)
176{
177	struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
178
179	return nft_counter_do_dump(skb, priv, reset);
180}
181
182static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
183	[NFTA_COUNTER_PACKETS]	= { .type = NLA_U64 },
184	[NFTA_COUNTER_BYTES]	= { .type = NLA_U64 },
185};
186
187struct nft_object_type nft_counter_obj_type;
188static const struct nft_object_ops nft_counter_obj_ops = {
189	.type		= &nft_counter_obj_type,
190	.size		= sizeof(struct nft_counter_percpu_priv),
191	.eval		= nft_counter_obj_eval,
192	.init		= nft_counter_obj_init,
193	.destroy	= nft_counter_obj_destroy,
194	.dump		= nft_counter_obj_dump,
195};
196
197struct nft_object_type nft_counter_obj_type __read_mostly = {
198	.type		= NFT_OBJECT_COUNTER,
199	.ops		= &nft_counter_obj_ops,
200	.maxattr	= NFTA_COUNTER_MAX,
201	.policy		= nft_counter_policy,
202	.owner		= THIS_MODULE,
203};
204
205void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
206		      const struct nft_pktinfo *pkt)
 
207{
208	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
209
210	nft_counter_do_eval(priv, regs, pkt);
211}
212
213static int nft_counter_dump(struct sk_buff *skb,
214			    const struct nft_expr *expr, bool reset)
215{
216	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
217
218	return nft_counter_do_dump(skb, priv, reset);
219}
220
221static int nft_counter_init(const struct nft_ctx *ctx,
222			    const struct nft_expr *expr,
223			    const struct nlattr * const tb[])
224{
225	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
226
227	return nft_counter_do_init(tb, priv);
228}
229
230static void nft_counter_destroy(const struct nft_ctx *ctx,
231				const struct nft_expr *expr)
232{
233	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
234
235	nft_counter_do_destroy(priv);
236}
237
238static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src, gfp_t gfp)
239{
240	struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
241	struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
242	struct nft_counter __percpu *cpu_stats;
243	struct nft_counter *this_cpu;
244	struct nft_counter_tot total;
245
246	nft_counter_fetch(priv, &total);
247
248	cpu_stats = alloc_percpu_gfp(struct nft_counter, gfp);
249	if (cpu_stats == NULL)
250		return -ENOMEM;
251
252	this_cpu = raw_cpu_ptr(cpu_stats);
253	u64_stats_set(&this_cpu->packets, total.packets);
254	u64_stats_set(&this_cpu->bytes, total.bytes);
 
 
255
256	priv_clone->counter = cpu_stats;
257	return 0;
258}
259
260static int nft_counter_offload(struct nft_offload_ctx *ctx,
261			       struct nft_flow_rule *flow,
262			       const struct nft_expr *expr)
263{
264	/* No specific offload action is needed, but report success. */
265	return 0;
266}
267
268static void nft_counter_offload_stats(struct nft_expr *expr,
269				      const struct flow_stats *stats)
270{
271	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
272	struct u64_stats_sync *nft_sync;
273	struct nft_counter *this_cpu;
 
274
275	local_bh_disable();
276	this_cpu = this_cpu_ptr(priv->counter);
277	nft_sync = this_cpu_ptr(&nft_counter_sync);
278
279	u64_stats_update_begin(nft_sync);
280	u64_stats_add(&this_cpu->packets, stats->pkts);
281	u64_stats_add(&this_cpu->bytes, stats->bytes);
282	u64_stats_update_end(nft_sync);
283	local_bh_enable();
284}
285
286void nft_counter_init_seqcount(void)
287{
288	int cpu;
289
290	for_each_possible_cpu(cpu)
291		u64_stats_init(per_cpu_ptr(&nft_counter_sync, cpu));
292}
293
294struct nft_expr_type nft_counter_type;
295static const struct nft_expr_ops nft_counter_ops = {
296	.type		= &nft_counter_type,
297	.size		= NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
298	.eval		= nft_counter_eval,
299	.init		= nft_counter_init,
300	.destroy	= nft_counter_destroy,
301	.destroy_clone	= nft_counter_destroy,
302	.dump		= nft_counter_dump,
303	.clone		= nft_counter_clone,
304	.reduce		= NFT_REDUCE_READONLY,
305	.offload	= nft_counter_offload,
306	.offload_stats	= nft_counter_offload_stats,
307};
308
309struct nft_expr_type nft_counter_type __read_mostly = {
310	.name		= "counter",
311	.ops		= &nft_counter_ops,
312	.policy		= nft_counter_policy,
313	.maxattr	= NFTA_COUNTER_MAX,
314	.flags		= NFT_EXPR_STATEFUL,
315	.owner		= THIS_MODULE,
316};