Skip to content

Commit 023223d

Browse files
committed
netfilter: nf_tables: make counter support built-in
Make counter support built-in to allow for direct call in case of CONFIG_RETPOLINE. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 690d541 commit 023223d

5 files changed

Lines changed: 27 additions & 51 deletions

File tree

‎include/net/netfilter/nf_tables_core.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
extern struct nft_expr_type nft_imm_type;
99
extern struct nft_expr_type nft_cmp_type;
10+
extern struct nft_expr_type nft_counter_type;
1011
extern struct nft_expr_type nft_lookup_type;
1112
extern struct nft_expr_type nft_bitwise_type;
1213
extern struct nft_expr_type nft_byteorder_type;
@@ -21,6 +22,7 @@ extern struct nft_expr_type nft_last_type;
2122
#ifdef CONFIG_NETWORK_SECMARK
2223
extern struct nft_object_type nft_secmark_obj_type;
2324
#endif
25+
extern struct nft_object_type nft_counter_obj_type;
2426

2527
int nf_tables_core_module_init(void);
2628
void nf_tables_core_module_exit(void);
@@ -120,6 +122,8 @@ bool nft_pipapo_lookup(const struct net *net, const struct nft_set *set,
120122
bool nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,
121123
const u32 *key, const struct nft_set_ext **ext);
122124

125+
void nft_counter_init_seqcount(void);
126+
123127
struct nft_expr;
124128
struct nft_regs;
125129
struct nft_pktinfo;
@@ -143,4 +147,6 @@ void nft_dynset_eval(const struct nft_expr *expr,
143147
struct nft_regs *regs, const struct nft_pktinfo *pkt);
144148
void nft_rt_get_eval(const struct nft_expr *expr,
145149
struct nft_regs *regs, const struct nft_pktinfo *pkt);
150+
void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
151+
const struct nft_pktinfo *pkt);
146152
#endif /* _NET_NF_TABLES_CORE_H */

‎net/netfilter/Kconfig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,6 @@ config NFT_FLOW_OFFLOAD
515515
This option adds the "flow_offload" expression that you can use to
516516
choose what flows are placed into the hardware.
517517

518-
config NFT_COUNTER
519-
tristate "Netfilter nf_tables counter module"
520-
help
521-
This option adds the "counter" expression that you can use to
522-
include packet and byte counters in a rule.
523-
524518
config NFT_CONNLIMIT
525519
tristate "Netfilter nf_tables connlimit module"
526520
depends on NF_CONNTRACK

‎net/netfilter/Makefile‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \
7575
nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \
7676
nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \
7777
nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o nft_last.o \
78-
nft_chain_route.o nf_tables_offload.o \
78+
nft_counter.o nft_chain_route.o nf_tables_offload.o \
7979
nft_set_hash.o nft_set_bitmap.o nft_set_rbtree.o \
8080
nft_set_pipapo.o
8181

@@ -100,7 +100,6 @@ obj-$(CONFIG_NFT_REJECT) += nft_reject.o
100100
obj-$(CONFIG_NFT_REJECT_INET) += nft_reject_inet.o
101101
obj-$(CONFIG_NFT_REJECT_NETDEV) += nft_reject_netdev.o
102102
obj-$(CONFIG_NFT_TUNNEL) += nft_tunnel.o
103-
obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
104103
obj-$(CONFIG_NFT_LOG) += nft_log.o
105104
obj-$(CONFIG_NFT_MASQ) += nft_masq.o
106105
obj-$(CONFIG_NFT_REDIR) += nft_redir.o

‎net/netfilter/nf_tables_core.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static void expr_call_ops_eval(const struct nft_expr *expr,
169169

170170
X(e, nft_payload_eval);
171171
X(e, nft_cmp_eval);
172+
X(e, nft_counter_eval);
172173
X(e, nft_meta_get_eval);
173174
X(e, nft_lookup_eval);
174175
X(e, nft_range_eval);
@@ -292,18 +293,22 @@ static struct nft_expr_type *nft_basic_types[] = {
292293
&nft_rt_type,
293294
&nft_exthdr_type,
294295
&nft_last_type,
296+
&nft_counter_type,
295297
};
296298

297299
static struct nft_object_type *nft_basic_objects[] = {
298300
#ifdef CONFIG_NETWORK_SECMARK
299301
&nft_secmark_obj_type,
300302
#endif
303+
&nft_counter_obj_type,
301304
};
302305

303306
int __init nf_tables_core_module_init(void)
304307
{
305308
int err, i, j = 0;
306309

310+
nft_counter_init_seqcount();
311+
307312
for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
308313
err = nft_register_obj(nft_basic_objects[i]);
309314
if (err)

‎net/netfilter/nft_counter.c‎

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/netfilter.h>
1414
#include <linux/netfilter/nf_tables.h>
1515
#include <net/netfilter/nf_tables.h>
16+
#include <net/netfilter/nf_tables_core.h>
1617
#include <net/netfilter/nf_tables_offload.h>
1718

1819
struct nft_counter {
@@ -174,7 +175,7 @@ static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
174175
[NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
175176
};
176177

177-
static struct nft_object_type nft_counter_obj_type;
178+
struct nft_object_type nft_counter_obj_type;
178179
static const struct nft_object_ops nft_counter_obj_ops = {
179180
.type = &nft_counter_obj_type,
180181
.size = sizeof(struct nft_counter_percpu_priv),
@@ -184,17 +185,16 @@ static const struct nft_object_ops nft_counter_obj_ops = {
184185
.dump = nft_counter_obj_dump,
185186
};
186187

187-
static struct nft_object_type nft_counter_obj_type __read_mostly = {
188+
struct nft_object_type nft_counter_obj_type __read_mostly = {
188189
.type = NFT_OBJECT_COUNTER,
189190
.ops = &nft_counter_obj_ops,
190191
.maxattr = NFTA_COUNTER_MAX,
191192
.policy = nft_counter_policy,
192193
.owner = THIS_MODULE,
193194
};
194195

195-
static void nft_counter_eval(const struct nft_expr *expr,
196-
struct nft_regs *regs,
197-
const struct nft_pktinfo *pkt)
196+
void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
197+
const struct nft_pktinfo *pkt)
198198
{
199199
struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
200200

@@ -275,7 +275,15 @@ static void nft_counter_offload_stats(struct nft_expr *expr,
275275
preempt_enable();
276276
}
277277

278-
static struct nft_expr_type nft_counter_type;
278+
void nft_counter_init_seqcount(void)
279+
{
280+
int cpu;
281+
282+
for_each_possible_cpu(cpu)
283+
seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
284+
}
285+
286+
struct nft_expr_type nft_counter_type;
279287
static const struct nft_expr_ops nft_counter_ops = {
280288
.type = &nft_counter_type,
281289
.size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
@@ -289,47 +297,11 @@ static const struct nft_expr_ops nft_counter_ops = {
289297
.offload_stats = nft_counter_offload_stats,
290298
};
291299

292-
static struct nft_expr_type nft_counter_type __read_mostly = {
300+
struct nft_expr_type nft_counter_type __read_mostly = {
293301
.name = "counter",
294302
.ops = &nft_counter_ops,
295303
.policy = nft_counter_policy,
296304
.maxattr = NFTA_COUNTER_MAX,
297305
.flags = NFT_EXPR_STATEFUL,
298306
.owner = THIS_MODULE,
299307
};
300-
301-
static 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;
317-
err1:
318-
nft_unregister_obj(&nft_counter_obj_type);
319-
return err;
320-
}
321-
322-
static 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-
328-
module_init(nft_counter_module_init);
329-
module_exit(nft_counter_module_exit);
330-
331-
MODULE_LICENSE("GPL");
332-
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
333-
MODULE_ALIAS_NFT_EXPR("counter");
334-
MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);
335-
MODULE_DESCRIPTION("nftables counter rule support");

0 commit comments

Comments
 (0)