Commit 12e4ecfa authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso

netfilter: nf_tables: add register tracking infrastructure

This patch adds new infrastructure to skip redundant selector store
operations on the same register to achieve a performance boost from
the packet path.

This is particularly noticeable in pure linear rulesets but it also
helps in rulesets which are already heaving relying in maps to avoid
ruleset linear inspection.

The idea is to keep data of the most recurrent store operations on
register to reuse them with cmp and lookup expressions.

This infrastructure allows for dynamic ruleset updates since the ruleset
blob reduction happens from the kernel.

Userspace still needs to be updated to maximize register utilization to
cooperate to improve register data reuse / reduce number of store on
register operations.
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 642c8eff
...@@ -122,6 +122,16 @@ struct nft_regs { ...@@ -122,6 +122,16 @@ struct nft_regs {
}; };
}; };
struct nft_regs_track {
struct {
const struct nft_expr *selector;
const struct nft_expr *bitwise;
} regs[NFT_REG32_NUM];
const struct nft_expr *cur;
const struct nft_expr *last;
};
/* Store/load an u8, u16 or u64 integer to/from the u32 data register. /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
* *
* Note, when using concatenations, register allocation happens at 32-bit * Note, when using concatenations, register allocation happens at 32-bit
...@@ -886,6 +896,8 @@ struct nft_expr_ops { ...@@ -886,6 +896,8 @@ struct nft_expr_ops {
int (*validate)(const struct nft_ctx *ctx, int (*validate)(const struct nft_ctx *ctx,
const struct nft_expr *expr, const struct nft_expr *expr,
const struct nft_data **data); const struct nft_data **data);
bool (*reduce)(struct nft_regs_track *track,
const struct nft_expr *expr);
bool (*gc)(struct net *net, bool (*gc)(struct net *net,
const struct nft_expr *expr); const struct nft_expr *expr);
int (*offload)(struct nft_offload_ctx *ctx, int (*offload)(struct nft_offload_ctx *ctx,
......
...@@ -8259,6 +8259,7 @@ EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); ...@@ -8259,6 +8259,7 @@ EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
{ {
const struct nft_expr *expr, *last; const struct nft_expr *expr, *last;
struct nft_regs_track track = {};
unsigned int size, data_size; unsigned int size, data_size;
void *data, *data_boundary; void *data, *data_boundary;
struct nft_rule_dp *prule; struct nft_rule_dp *prule;
...@@ -8298,7 +8299,17 @@ static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *cha ...@@ -8298,7 +8299,17 @@ static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *cha
if (WARN_ON_ONCE(data > data_boundary)) if (WARN_ON_ONCE(data > data_boundary))
return -ENOMEM; return -ENOMEM;
size = 0;
track.last = last;
nft_rule_for_each_expr(expr, last, rule) { nft_rule_for_each_expr(expr, last, rule) {
track.cur = expr;
if (expr->ops->reduce &&
expr->ops->reduce(&track, expr)) {
expr = track.cur;
continue;
}
if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) if (WARN_ON_ONCE(data + expr->ops->size > data_boundary))
return -ENOMEM; return -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment