netfilter: set extensions
Subsystems (4)
cover netfilter: set extensions
The following patches add so called "set extensions" to maintain optional per element data for nft sets and converts nf_tables to use them.
Following patches will use these set extensions to add some features from ipset which we are currently missing, namely timeouts and comments, as well as set transactions, variable sized data for concatenations and per-flow expressions. I'll send the next batch as soon as these have been merged.
Please apply, thanks!
Patrick McHardy (6):
rhashtable: provide len to obj_hashfn
netfilter: nft_hash: restore struct nft_hash
netfilter: nft_hash: indent rhashtable parameters
netfilter: nft_hash: convert to use rhashtable callbacks
netfilter: nf_tables: add set extensions
netfilter: nf_tables: convert hash and rbtree to set extensions
include/linux/rhashtable.h | 6 +- include/net/netfilter/nf_tables.h | 119 ++++++++++++++++++++++++++++++-- lib/rhashtable.c | 2 +- net/netfilter/nf_tables_api.c | 135 ++++++++++++++++++++++++++++-------- net/netfilter/nft_hash.c | 141 +++++++++++++++++++++----------------- net/netfilter/nft_rbtree.c | 64 ++++++----------- net/netlink/af_netlink.c | 2 +- 7 files changed, 326 insertions(+), 143 deletions(-)
Signature · 1 line
2.1.0
PATCH 1/6 rhashtable: provide len to obj_hashfn
nftables sets will be converted to use so called setextensions, moving the key to a non-fixed position. To hash it, the obj_hashfn must be used, however it so far doesn't receive the length parameter.
Pass the key length to obj_hashfn() and convert existing users.
--- include/linux/rhashtable.h | 6 ++++-- lib/rhashtable.c | 2 +- net/netlink/af_netlink.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-)
include/linux/rhashtable.h +4 -2
@@ -88,7 +88,7 @@ struct rhashtable_compare_arg { };typedef u32 (*rht_hashfn_t )(const void *data,u32 len,u32 seed); -typedef u32 (*rht_obj_hashfn_t )(const void *data,u32 seed); +typedef u32 (*rht_obj_hashfn_t )(const void *data,u32 len,u32 seed);typedef int (*rht_obj_cmpfn_t )(struct rhashtable_compare_arg *arg,const void *obj);
@@ -242,7 +242,9 @@ static inline unsigned int rht_head_hashfn(const char *ptr =rht_obj (ht, he);return likely (params.obj_hashfn) ? -rht_bucket_index (tbl, params.obj_hashfn (ptr, tbl->hash_rnd)) : +rht_bucket_index (tbl, params.obj_hashfn (ptr, params.key_len ?: + ht->p.key_len, + tbl->hash_rnd)) :rht_key_hashfn (ht, tbl, ptr + params.key_offset, params); }
lib/rhashtable.c +1 -1
@@ -691,7 +691,7 @@ static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)* struct rhash_head node; * }; * -* u32 my_hash_fn(const void *data, u32 seed) +* u32 my_hash_fn(const void *data, u32 len, u32 seed) * { * struct test_obj *obj = data; *
net/netlink/af_netlink.c +1 -1
@@ -3127,7 +3127,7 @@ static struct pernet_operations __net_initdata netlink_net_ops = { .exit = netlink_net_exit, }; -static inline u32 netlink_hash (const void *data,u32 seed) +static inline u32 netlink_hash (const void *data,u32 len,u32 seed) {const struct netlink_sock *nlk = data;struct netlink_compare_arg arg;
Signature · 1 line
2.1.0
PATCH 2/6 netfilter: nft_hash: restore struct nft_hash
In reply to Patrick McHardyFollowing patches will add new private members, restore struct nft_hash as preparation.
--- net/netfilter/nft_hash.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
net/netfilter/nft_hash.c +21 -16
@@ -23,6 +23,10 @@/* We target a hash table size of 4, element hint is 75% of final size */ #define NFT_HASH_ELEMENT_HINT 3 +struct nft_hash { +struct rhashtable ht; +}; +struct nft_hash_elem {struct rhash_head node;struct nft_data key;
@@ -35,10 +39,10 @@ static bool nft_hash_lookup(const struct nft_set *set,const struct nft_data *key, struct nft_data *data) { - struct rhashtable *priv = nft_set_priv(set); + struct nft_hash *priv = nft_set_priv(set);const struct nft_hash_elem *he; - he =rhashtable_lookup_fast (priv, key, nft_hash_params); + he =rhashtable_lookup_fast (&priv->ht, key, nft_hash_params);if (he && set->flags &NFT_SET_MAP )nft_data_copy (data, he->data);
@@ -48,7 +52,7 @@ static bool nft_hash_lookup(const struct nft_set *set,static int nft_hash_insert (const struct nft_set *set,const struct nft_set_elem *elem) { -struct rhashtable *priv =nft_set_priv (set); +struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he;unsigned int size;int err;
@@ -68,7 +72,7 @@ static int nft_hash_insert(const struct nft_set *set,if (set->flags &NFT_SET_MAP )nft_data_copy (he->data, &elem->data); - err =rhashtable_insert_fast (priv, &he->node, nft_hash_params); + err =rhashtable_insert_fast (&priv->ht, &he->node, nft_hash_params);if (err)kfree (he);
@@ -87,19 +91,19 @@ static void nft_hash_elem_destroy(const struct nft_set *set,static void nft_hash_remove (const struct nft_set *set,const struct nft_set_elem *elem) { -struct rhashtable *priv =nft_set_priv (set); +struct nft_hash *priv =nft_set_priv (set); -rhashtable_remove_fast (priv, elem->cookie, nft_hash_params); +rhashtable_remove_fast (&priv->ht, elem->cookie, nft_hash_params);synchronize_rcu ();kfree (elem->cookie); }static int nft_hash_get (const struct nft_set *set,struct nft_set_elem *elem) { -struct rhashtable *priv =nft_set_priv (set); +struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; - he =rhashtable_lookup_fast (priv, &elem->key, nft_hash_params); + he =rhashtable_lookup_fast (&priv->ht, &elem->key, nft_hash_params);if (!he)return -ENOENT ;
@@ -114,13 +118,13 @@ static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)static void nft_hash_walk (const struct nft_ctx *ctx,const struct nft_set *set,struct nft_set_iter *iter) { -struct rhashtable *priv =nft_set_priv (set); +struct nft_hash *priv =nft_set_priv (set);const struct nft_hash_elem *he;struct rhashtable_iter hti;struct nft_set_elem elem;int err; - err =rhashtable_walk_init (priv, &hti); + err =rhashtable_walk_init (&priv->ht, &hti); iter->err = err;if (err)return ;
@@ -165,7 +169,7 @@ out:static unsigned int nft_hash_privsize (const struct nlattr *const nla[]) { -return sizeof (struct rhashtable ); +return sizeof (struct nft_hash ); }static const struct rhashtable_params nft_hash_params = {
@@ -179,13 +183,13 @@ static int nft_hash_init(const struct nft_set *set,const struct nft_set_desc *desc, const struct nlattr * const tb[]) { - struct rhashtable *priv = nft_set_priv(set); + struct nft_hash *priv = nft_set_priv(set);struct rhashtable_params params = nft_hash_params; params.nelem_hint = desc->size ?:NFT_HASH_ELEMENT_HINT ; params.key_len = set->klen; -return rhashtable_init (priv, ¶ms); +return rhashtable_init (&priv->ht, ¶ms); }static void nft_free_element (void *ptr,void *arg)
@@ -195,8 +199,9 @@ static void nft_free_element(void *ptr, void *arg)static void nft_hash_destroy (const struct nft_set *set) { -rhashtable_free_and_destroy (nft_set_priv (set), nft_free_element, - (void *)set); +struct nft_hash *priv =nft_set_priv (set); + +rhashtable_free_and_destroy (&priv->ht, nft_free_element, (void *)set); } static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
@@ -209,7 +214,7 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);if (desc->size) { - est->size =sizeof (struct rhashtable ) + + est->size =sizeof (struct nft_hash ) +roundup_pow_of_two (desc->size *4 /3 ) *sizeof (struct nft_hash_elem *) + desc->size * esize;
Signature · 1 line
2.1.0
PATCH 3/6 netfilter: nft_hash: indent rhashtable parameters
In reply to Patrick McHardyImprove readability by indenting the parameter initialization.
--- net/netfilter/nft_hash.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nft_hash.c +5 -5
@@ -173,10 +173,10 @@ static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) }static const struct rhashtable_params nft_hash_params = { - .head_offset = offsetof(struct nft_hash_elem , node), - .key_offset = offsetof(struct nft_hash_elem , key), - .hashfn = jhash, - .automatic_shrinking = true, + .head_offset = offsetof(struct nft_hash_elem , node), + .key_offset = offsetof(struct nft_hash_elem , key), + .hashfn = jhash, + .automatic_shrinking = true, }; static int nft_hash_init(const struct nft_set *set,
@@ -187,7 +187,7 @@ static int nft_hash_init(const struct nft_set *set,struct rhashtable_params params = nft_hash_params; params.nelem_hint = desc->size ?:NFT_HASH_ELEMENT_HINT ; - params.key_len = set->klen; + params.key_len = set->klen;return rhashtable_init (&priv->ht, ¶ms); }
Signature · 1 line
2.1.0
PATCH 4/6 netfilter: nft_hash: convert to use rhashtable callbacks
In reply to Patrick McHardyA following patch will convert sets to use so called set extensions, where the key is not located in a fixed position anymore. This will require rhashtable hashing and comparison callbacks to be used.
As preparation, convert nft_hash to use these callbacks without any functional changes.
--- net/netfilter/nft_hash.c | 54 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-)
net/netfilter/nft_hash.c +49 -5
@@ -33,16 +33,50 @@ struct nft_hash_elem {struct nft_data data[]; }; +struct nft_hash_cmp_arg { +const struct nft_set *set; +const struct nft_data *key; +}; +static const struct rhashtable_params nft_hash_params; +static inline u32 nft_hash_key (const void *data,u32 len,u32 seed) +{ +const struct nft_hash_cmp_arg *arg = data; + +return jhash (arg->key, len, seed); +} + +static inline u32 nft_hash_obj (const void *data,u32 len,u32 seed) +{ +const struct nft_hash_elem *he = data; + +return jhash (&he->key, len, seed); +} + +static inline int nft_hash_cmp (struct rhashtable_compare_arg *arg, +const void *ptr) +{ +const struct nft_hash_cmp_arg *x = arg->key; +const struct nft_hash_elem *he = ptr; + +if (nft_data_cmp (&he->key, x->key, x->set->klen)) +return 1 ; +return 0 ; +} +static bool nft_hash_lookup (const struct nft_set *set,const struct nft_data *key,struct nft_data *data) {struct nft_hash *priv =nft_set_priv (set);const struct nft_hash_elem *he; +struct nft_hash_cmp_arg arg = { + .set = set, + .key = key, + }; - he =rhashtable_lookup_fast (&priv->ht, key, nft_hash_params); + he =rhashtable_lookup_fast (&priv->ht, &arg, nft_hash_params);if (he && set->flags &NFT_SET_MAP )nft_data_copy (data, he->data);
@@ -54,6 +88,10 @@ static int nft_hash_insert(const struct nft_set *set, {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; +struct nft_hash_cmp_arg arg = { + .set = set, + .key = &elem->key, + };unsigned int size;int err;
@@ -72,7 +110,8 @@ static int nft_hash_insert(const struct nft_set *set,if (set->flags &NFT_SET_MAP )nft_data_copy (he->data, &elem->data); - err =rhashtable_insert_fast (&priv->ht, &he->node, nft_hash_params); + err =rhashtable_lookup_insert_key (&priv->ht, &arg, &he->node, + nft_hash_params);if (err)kfree (he);
@@ -102,8 +141,12 @@ static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem) {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; +struct nft_hash_cmp_arg arg = { + .set = set, + .key = &elem->key, + }; - he =rhashtable_lookup_fast (&priv->ht, &elem->key, nft_hash_params); + he =rhashtable_lookup_fast (&priv->ht, &arg, nft_hash_params);if (!he)return -ENOENT ;
@@ -174,8 +217,9 @@ static unsigned int nft_hash_privsize(const struct nlattr * const nla[])static const struct rhashtable_params nft_hash_params = { .head_offset = offsetof(struct nft_hash_elem , node), - .key_offset = offsetof(struct nft_hash_elem , key), - .hashfn = jhash, + .hashfn = nft_hash_key, + .obj_hashfn = nft_hash_obj, + .obj_cmpfn = nft_hash_cmp, .automatic_shrinking = true, };
Signature · 1 line
2.1.0
PATCH 5/6 netfilter: nf_tables: add set extensions
In reply to Patrick McHardyAdd simple set extension infrastructure for maintaining variable sized and optional per element data.
--- include/net/netfilter/nf_tables.h | 105 ++++++++++++++++++++++++++++++++++++++ net/netfilter/nf_tables_api.c | 16 ++++++ 2 files changed, 121 insertions(+)
include/net/netfilter/nf_tables.h +105 -0
@@ -311,6 +311,111 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,void nf_tables_unbind_set (const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_binding *binding); +/** +* enum nft_set_extensions - set extension type IDs +* +* @NFT_SET_EXT_KEY: element key +* @NFT_SET_EXT_DATA: mapping data +* @NFT_SET_EXT_FLAGS: element flags +* @NFT_SET_EXT_NUM: number of extension types +*/ +enum nft_set_extensions { +NFT_SET_EXT_KEY , +NFT_SET_EXT_DATA , +NFT_SET_EXT_FLAGS , +NFT_SET_EXT_NUM +}; + +/** +* struct nft_set_ext_type - set extension type +* +* @len: fixed part length of the extension +* @align: alignment requirements of the extension +*/ +struct nft_set_ext_type { +u8 len; +u8 align; +}; + +extern const struct nft_set_ext_type nft_set_ext_types[]; + +/** +* struct nft_set_ext_tmpl - set extension template +* +* @len: length of extension area +* @offset: offsets of individual extension types +*/ +struct nft_set_ext_tmpl { +u16 len; +u8 offset[NFT_SET_EXT_NUM ]; +}; + +/** +* struct nft_set_ext - set extensions +* +* @offset: offsets of individual extension types +* @data: beginning of extension data +*/ +struct nft_set_ext { +u8 offset[NFT_SET_EXT_NUM ]; +char data[0 ]; +}; + +static inline void nft_set_ext_prepare (struct nft_set_ext_tmpl *tmpl) +{ +memset (tmpl,0 ,sizeof (*tmpl)); + tmpl->len =sizeof (struct nft_set_ext ); +} + +static inline void nft_set_ext_add_length (struct nft_set_ext_tmpl *tmpl,u8 id, +unsigned int len) +{ + tmpl->len =ALIGN (tmpl->len, nft_set_ext_types[id].align); +BUG_ON (tmpl->len >U8_MAX ); + tmpl->offset[id] = tmpl->len; + tmpl->len += nft_set_ext_types[id].len + len; +} + +static inline void nft_set_ext_add (struct nft_set_ext_tmpl *tmpl,u8 id) +{ +nft_set_ext_add_length (tmpl, id,0 ); +} + +static inline void nft_set_ext_init (struct nft_set_ext *ext, +const struct nft_set_ext_tmpl *tmpl) +{ +memcpy (ext->offset, tmpl->offset,sizeof (ext->offset)); +} + +static inline bool __nft_set_ext_exists (const struct nft_set_ext *ext,u8 id) +{ +return !!ext->offset[id]; +} + +static inline bool nft_set_ext_exists (const struct nft_set_ext *ext,u8 id) +{ +return ext &&__nft_set_ext_exists (ext, id); +} + +static inline void *nft_set_ext (const struct nft_set_ext *ext,u8 id) +{ +return (void *)ext + ext->offset[id]; +} + +static inline struct nft_data *nft_set_ext_key (const struct nft_set_ext *ext) +{ +return nft_set_ext (ext,NFT_SET_EXT_KEY ); +} + +static inline struct nft_data *nft_set_ext_data (const struct nft_set_ext *ext) +{ +return nft_set_ext (ext,NFT_SET_EXT_DATA ); +} + +static inline u8 *nft_set_ext_flags (const struct nft_set_ext *ext) +{ +return nft_set_ext (ext,NFT_SET_EXT_FLAGS ); +}/** * struct nft_expr_type - nf_tables expression type
net/netfilter/nf_tables_api.c +16 -0
@@ -2827,6 +2827,22 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,nf_tables_set_destroy (ctx, set); } +const struct nft_set_ext_type nft_set_ext_types[] = { + [NFT_SET_EXT_KEY ] = { + .len =sizeof (struct nft_data ), + .align = __alignof__(struct nft_data ), + }, + [NFT_SET_EXT_DATA ] = { + .len =sizeof (struct nft_data ), + .align = __alignof__(struct nft_data ), + }, + [NFT_SET_EXT_FLAGS ] = { + .len =sizeof (u8), + .align = __alignof__(u8 ), + }, +}; +EXPORT_SYMBOL_GPL (nft_set_ext_types); +/* * Set elements */
Signature · 1 line
2.1.0
PATCH 6/6 netfilter: nf_tables: convert hash and rbtree to set extensions
In reply to Patrick McHardyThe set implementations' private struct will only contain the elements needed to maintain the search structure, all other elements are moved to the set extensions.
Element allocation and initialization is performed centrally by nf_tables_api instead of by the different set implementations' ->insert() functions. A new "elemsize" member in the set ops specifies the amount of memory to reserve for internal usage. Destruction will also be moved out of the set implementations by a following patch.
Except for element allocation, the patch is a simple conversion to using data from the extension area.
--- include/net/netfilter/nf_tables.h | 14 +++-- net/netfilter/nf_tables_api.c | 119 ++++++++++++++++++++++++++++---------- net/netfilter/nft_hash.c | 56 +++++------------- net/netfilter/nft_rbtree.c | 64 +++++++------------- 4 files changed, 132 insertions(+), 121 deletions(-)
include/net/netfilter/nf_tables.h +10 -4
@@ -140,8 +140,7 @@ struct nft_userdata {* * @cookie: implementation specific element cookie * @key: element key -* @data: element data (maps only) -* @flags: element flags (end of interval) +* @priv: element private data and extensions * * The cookie can be used to store a handle to the element for subsequent * removal.
@@ -149,8 +148,7 @@ struct nft_userdata {struct nft_set_elem {void *cookie;struct nft_data key; -struct nft_data data; -u32 flags; +void *priv; };struct nft_set ;
@@ -214,6 +212,7 @@ struct nft_set_estimate {* @destroy: destroy private data of set instance * @list: nf_tables_set_ops list node * @owner: module reference +* @elemsize: element private size * @features: features supported by the implementation */ struct nft_set_ops {
@@ -241,6 +240,7 @@ struct nft_set_ops {struct list_head list;struct module *owner; +unsigned int elemsize;u32 features; };
@@ -417,6 +417,12 @@ static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)return nft_set_ext (ext,NFT_SET_EXT_FLAGS ); } +static inline struct nft_set_ext *nft_set_elem_ext (const struct nft_set *set, +void *elem) +{ +return elem + set->ops->elemsize; +} +/** * struct nft_expr_type - nf_tables expression type *
net/netfilter/nf_tables_api.c +89 -30
249 lines not on this page · raw message
net/netfilter/nft_hash.c +13 -43
127 lines not on this page · raw message
net/netfilter/nft_rbtree.c +20 -44
144 lines not on this page · raw message
Signature · 1 line
2.1.0
Quoted 10 lines · Patrick McHardy
The following patches add so called "set extensions" to maintain optional per element data for nft sets and converts
nf_tablesto use them.Following patches will use these set extensions to add some features from ipset which we are currently missing, namely timeouts and comments, as well as set transactions, variable sized data for concatenations and per-flow expressions. I'll send the next batch as soon as these have been merged.
Please apply, thanks!
Applied, thanks Patrick!