expr: hash: support of symmetric hash
PATCH 1/1 expr: hash: support of symmetric hash
This patch provides symmetric hash support according to source ip address and port, and destination ip address and port.
The new attribute NFTA_HASH_TYPE has been included to support different types of hashing functions. Currently supported NFT_HASH_JENKINS through jhash and NFT_HASH_SYM through symhash.
The main difference between both types are:
- jhash requires an expression with sreg, symhash doesn't.
- symhash supports modulus and offset, but not seed.
Examples:
nft add rule ip nat prerouting ct mark set jhash ip saddr mod 2 nft add rule ip nat prerouting ct mark set symhash mod 2
--- include/libnftnl/expr.h | 1 + include/linux/netfilter/nf_tables.h | 13 +++++++++++ src/expr/hash.c | 46 ++++++++++++++++++++++++++++++++----- tests/nft-expr_hash-test.c | 4 ++++ 4 files changed, 58 insertions(+), 6 deletions(-)
include/libnftnl/expr.h +1 -0
@@ -227,6 +227,7 @@ enum {NFTNL_EXPR_HASH_MODULUS ,NFTNL_EXPR_HASH_SEED ,NFTNL_EXPR_HASH_OFFSET , + NFTNL_EXPR_HASH_TYPE, }; enum {
include/linux/netfilter/nf_tables.h +13 -0
@@ -793,6 +793,17 @@ enum nft_rt_keys { };/** +* enum nft_hash_types - nf_tables hash expression types +* +* @NFT_HASH_JENKINS: Jenkins Hash +* @NFT_HASH_SYM: Symmetric Hash +*/ +enum nft_hash_types { +NFT_HASH_JENKINS , +NFT_HASH_SYM , +}; + +/** * enum nft_hash_attributes - nf_tables hash expression netlink attributes * * @NFTA_HASH_SREG: source register (NLA_U32)
@@ -801,6 +812,7 @@ enum nft_rt_keys {* @NFTA_HASH_MODULUS: modulus value (NLA_U32) * @NFTA_HASH_SEED: seed value (NLA_U32) * @NFTA_HASH_OFFSET: add this offset value to hash result (NLA_U32) +* @NFTA_HASH_TYPE: hash operation (NLA_U32: nft_hash_types) */ enum nft_hash_attributes {NFTA_HASH_UNSPEC ,
@@ -810,6 +822,7 @@ enum nft_hash_attributes {NFTA_HASH_MODULUS ,NFTA_HASH_SEED ,NFTA_HASH_OFFSET , +NFTA_HASH_TYPE , __NFTA_HASH_MAX, };#define NFTA_HASH_MAX (__NFTA_HASH_MAX - 1)
src/expr/hash.c +40 -6
118 lines not on this page · raw message
tests/nft-expr_hash-test.c +4 -0
@@ -45,6 +45,9 @@ static void cmp_nftnl_expr(struct nftnl_expr *rule_a,if (nftnl_expr_get_u32 (rule_a,NFTNL_EXPR_HASH_OFFSET ) !=nftnl_expr_get_u32 (rule_b,NFTNL_EXPR_HASH_OFFSET ))print_err ("Expr NFTNL_EXPR_HASH_OFFSET mismatches" ); +if (nftnl_expr_get_u32 (rule_a,NFTNL_EXPR_HASH_TYPE ) != +nftnl_expr_get_u32 (rule_b,NFTNL_EXPR_HASH_TYPE )) +print_err ("Expr NFTNL_EXPR_HASH_TYPE mismatches" ); }int main (int argc,char *argv[])
@@ -69,6 +72,7 @@ int main(int argc, char *argv[])nftnl_expr_set_u32 (ex,NFTNL_EXPR_HASH_MODULUS ,0x78123456 );nftnl_expr_set_u32 (ex,NFTNL_EXPR_HASH_SEED ,0x78123456 );nftnl_expr_set_u32 (ex,NFTNL_EXPR_HASH_OFFSET ,0x3612845 ); +nftnl_expr_set_u32 (ex,NFTNL_EXPR_HASH_TYPE ,NFT_HASH_JENKINS );nftnl_rule_add_expr (a, ex);
Signature · 1 line
2.11.0
Quoted 7 lines · Laura Garcia Liebana
This patch provides symmetric hash support according to source ip address and port, and destination ip address and port.
The new attribute
NFTA_HASH_TYPEhas been included to support different types of hashing functions. Currently supportedNFT_HASH_JENKINSthrough jhash andNFT_HASH_SYMthrough symhash.
Quoted 8 lines
The main difference between both types are:
- jhash requires an expression with sreg, symhash doesn't.
- symhash supports modulus and offset, but not seed.Examples:
nft add rule ip nat prerouting ct mark set jhash ip saddr mod 2 nft add rule ip nat prerouting ct mark set symhash mod 2
Applied, thanks.