netfilter: nft_cmp: optimize comparison for 16-bytes
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 1/1 netfilter: nft_cmp: optimize comparison for 16-bytes
Allow up to 16-byte comparisons with a new cmp fast version. Use two 64-bit words and calculate the mask representing the bits to be compared. Make sure the comparison is 64-bit aligned and avoid out-of-bound memory access.
---
v2: this version adds a new cmp fast variant for two reasons:
- unaligned u32 cmp using v1 patch cmp fast result in using slow cmp. ie. NFT_REG32_01, NFT_REG32_03, ... use slow cmp.
- register NFT_REG32_13, NFT_REG32_14 and NFT_REG32_15 always use slow cmp for u32 cmp.
Although this version adds more code compare to v1, the numbers I obtained here show that the results are similar results compared to v1:
This version shows only slightly better numbers for cmp u32 fast vs cmp fast u128 being slightly slower.
include/net/netfilter/nf_tables_core.h | 8 ++ net/netfilter/nf_tables_core.c | 16 ++++ net/netfilter/nft_cmp.c | 103 ++++++++++++++++++++++++- 3 files changed, 125 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tables_core.h +8 -0
@@ -42,6 +42,13 @@ struct nft_cmp_fast_expr {bool inv; }; +struct nft_cmp16_fast_expr { +struct nft_data data; +struct nft_data mask; +u8 sreg; +bool inv; +}; +struct nft_immediate_expr {struct nft_data data;u8 dreg;
@@ -59,6 +66,7 @@ static inline u32 nft_cmp_fast_mask(unsigned int len) }extern const struct nft_expr_ops nft_cmp_fast_ops; +extern const struct nft_expr_ops nft_cmp16_fast_ops;struct nft_payload {enum nft_payload_bases base:8 ;
net/netfilter/nf_tables_core.c +16 -0
@@ -67,6 +67,20 @@ static void nft_cmp_fast_eval(const struct nft_expr *expr, regs->verdict.code =NFT_BREAK ; } +static void nft_cmp16_fast_eval (const struct nft_expr *expr, +struct nft_regs *regs) +{ +const struct nft_cmp16_fast_expr *priv =nft_expr_priv (expr); +const u64 *reg_data = (const u64 *)®s->data[priv->sreg]; +const u64 *mask = (const u64 *)&priv->mask; +const u64 *data = (const u64 *)&priv->data; + +if (((reg_data[0 ] & mask[0 ]) == data[0 ] && + ((reg_data[1 ] & mask[1 ]) == data[1 ])) ^ priv->inv) +return ; + regs->verdict.code =NFT_BREAK ; +} + static noinline void __nft_trace_verdict(struct nft_traceinfo *info,const struct nft_chain *chain,const struct nft_regs *regs)
@@ -225,6 +239,8 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)nft_rule_dp_for_each_expr (expr, last, rule) {if (expr->ops == &nft_cmp_fast_ops)nft_cmp_fast_eval (expr, ®s); +else if (expr->ops == &nft_cmp16_fast_ops) +nft_cmp16_fast_eval (expr, ®s);else if (expr->ops == &nft_bitwise_fast_ops)nft_bitwise_fast_eval (expr, ®s); else if (expr->ops != &nft_payload_fast_ops ||
net/netfilter/nft_cmp.c +101 -2
122 lines not on this page · raw message
Signature · 1 line
2.30.2