nf_tables set enhancements
Subsystems (2)
Pablo Neira Ayuso
maintainercover nf_tables set enhancements
Hi,
The following patches contains enhancements for the set infrastructure:
1) Introduce memory scalability notation for sets, this is useful in case that userspace provides no explicit number of elements for this set and to break ties if the set selection routine finds two backends providing similar runtime performance which is now possible with hash and the new bitmap set type.
2) Add new bitmap set type that represents an element using two bits. In order to reduce memory consumption, this set type is selected if the key size is smaller than 16 bit. For 8 bit keys, the bitmap consumes 66 bytes. For 16 bit keys, the bitmap takes 16388 bytes.
Comments welcome, thanks.
Pablo Neira Ayuso (7):
netfilter: nf_tables: pass netns to set->ops->remove()
netfilter: nf_tables: use struct nft_set_iter in set element flush
netfilter: nf_tables: rename deactivate_one() to flush()
netfilter: nf_tables: add flush field to struct nft_set_iter
netfilter: nf_tables: rename struct nft_set_estimate class field
netfilter: nf_tables: add space notation to sets
netfilter: nf_tables: add bitmap set type
include/net/netfilter/nf_tables.h | 18 ++- net/netfilter/Kconfig | 6 + net/netfilter/Makefile | 1 + net/netfilter/nf_tables_api.c | 49 +++--- net/netfilter/nft_set_bitmap.c | 314 ++++++++++++++++++++++++++++++++++++++ net/netfilter/nft_set_hash.c | 14 +- net/netfilter/nft_set_rbtree.c | 14 +- 7 files changed, 378 insertions(+), 38 deletions(-) create mode 100644 net/netfilter/nft_set_bitmap.c
--
2.1.4
Pablo Neira Ayuso
maintainerPATCH 1/7 netfilter: nf_tables: pass netns to set->ops->remove()
This is required by the new bitmap set type that comes in a follow up patch.
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 4 ++-- net/netfilter/nft_set_hash.c | 3 ++- net/netfilter/nft_set_rbtree.c | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -298,7 +298,8 @@ struct nft_set_ops {bool (*deactivate_one)(const struct net *net,const struct nft_set *set,void *priv); -void (*remove)(const struct nft_set *set, +void (*remove)(const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem); void (*walk)(const struct nft_ctx *ctx, const struct nft_set *set,
net/netfilter/nf_tables_api.c +2 -2
@@ -4803,7 +4803,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)nf_tables_setelem_notify (&trans->ctx, te->set, &te->elem,NFT_MSG_DELSETELEM ,0 ); - te->set->ops->remove (te->set, &te->elem); + te->set->ops->remove (net, te->set, &te->elem);atomic_dec (&te->set->nelems); te->set->ndeact--;break ;
@@ -4924,7 +4924,7 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb)case NFT_MSG_NEWSETELEM : te = (struct nft_trans_elem *)trans->data; - te->set->ops->remove (te->set, &te->elem); + te->set->ops->remove (net, te->set, &te->elem);atomic_dec (&te->set->nelems);break ;case NFT_MSG_DELSETELEM :
net/netfilter/nft_set_hash.c +2 -1
@@ -203,7 +203,8 @@ static void *nft_hash_deactivate(const struct net *net,return he; } -static void nft_hash_remove (const struct nft_set *set, +static void nft_hash_remove (const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem) {struct nft_hash *priv =nft_set_priv (set);
net/netfilter/nft_set_rbtree.c +2 -1
@@ -151,7 +151,8 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,return err; } -static void nft_rbtree_remove (const struct nft_set *set, +static void nft_rbtree_remove (const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem) {struct nft_rbtree *priv =nft_set_priv (set);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/7 netfilter: nf_tables: use struct nft_set_iter in set element flush
In reply to Pablo Neira AyusoInstead of struct nft_set_dump_args, remove unnecessary wrapper structure.
--- net/netfilter/nf_tables_api.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +5 -7
@@ -3934,15 +3934,13 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,return -EBUSY ;if (nla[NFTA_SET_ELEM_LIST_ELEMENTS ] ==NULL ) { -struct nft_set_dump_args args = { - .iter = { - .genmask = genmask, - .fn = nft_flush_set, - }, +struct nft_set_iter iter = { + .genmask = genmask, + .fn = nft_flush_set, }; - set->ops->walk (&ctx, set, &args.iter); + set->ops->walk (&ctx, set, &iter); -return args.iter.err; +return iter.err; }nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) {
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/7 netfilter: nf_tables: rename deactivate_one() to flush()
In reply to Pablo Neira AyusoAlthough semantics are similar to deactivate() with no implicit element lookup, this is only called from the set flush path, so better rename this to flush.
--- include/net/netfilter/nf_tables.h | 8 ++++---- net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_set_hash.c | 8 ++++---- net/netfilter/nft_set_rbtree.c | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +4 -4
@@ -260,7 +260,7 @@ struct nft_expr;* @insert: insert new element into set * @activate: activate new element in the next generation * @deactivate: lookup for element and deactivate it in the next generation -* @deactivate_one: deactivate element in the next generation +* @flush: deactivate element in the next generation * @remove: remove element from set * @walk: iterate over all set elemeennts * @privsize: function to return size of set private data
@@ -295,9 +295,9 @@ struct nft_set_ops {void * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -bool (*deactivate_one)(const struct net *net, -const struct nft_set *set, -void *priv); +bool (*flush)(const struct net *net, +const struct nft_set *set, +void *priv);void (*remove)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);
net/netfilter/nf_tables_api.c +1 -1
@@ -3897,7 +3897,7 @@ static int nft_flush_set(const struct nft_ctx *ctx,if (!trans)return -ENOMEM ; -if (!set->ops->deactivate_one (ctx->net, set, elem->priv)) { +if (!set->ops->flush (ctx->net, set, elem->priv)) { err = -ENOENT ; goto err1; }
net/netfilter/nft_set_hash.c +4 -4
@@ -167,8 +167,8 @@ static void nft_hash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_clear_busy (&he->ext); } -static bool nft_hash_deactivate_one (const struct net *net, -const struct nft_set *set,void *priv) +static bool nft_hash_flush (const struct net *net, +const struct nft_set *set,void *priv) {struct nft_hash_elem *he = priv;
@@ -195,7 +195,7 @@ static void *nft_hash_deactivate(const struct net *net,rcu_read_lock (); he =rhashtable_lookup_fast (&priv->ht, &arg, nft_hash_params);if (he !=NULL && - !nft_hash_deactivate_one (net, set, he)) + !nft_hash_flush (net, set, he)) he =NULL ;rcu_read_unlock ();
@@ -398,7 +398,7 @@ static struct nft_set_ops nft_hash_ops __read_mostly = { .insert = nft_hash_insert, .activate = nft_hash_activate, .deactivate = nft_hash_deactivate, - .deactivate_one = nft_hash_deactivate_one, + .flush = nft_hash_flush, .remove = nft_hash_remove, .lookup = nft_hash_lookup, .update = nft_hash_update,
net/netfilter/nft_set_rbtree.c +4 -4
@@ -172,8 +172,8 @@ static void nft_rbtree_activate(const struct net *net,nft_set_elem_change_active (net, set, &rbe->ext); } -static bool nft_rbtree_deactivate_one (const struct net *net, -const struct nft_set *set,void *priv) +static bool nft_rbtree_flush (const struct net *net, +const struct nft_set *set,void *priv) {struct nft_rbtree_elem *rbe = priv;
@@ -214,7 +214,7 @@ static void *nft_rbtree_deactivate(const struct net *net, parent = parent->rb_right;continue ; } -nft_rbtree_deactivate_one (net, set, rbe); +nft_rbtree_flush (net, set, rbe);return rbe; } }
@@ -305,7 +305,7 @@ static struct nft_set_ops nft_rbtree_ops __read_mostly = { .insert = nft_rbtree_insert, .remove = nft_rbtree_remove, .deactivate = nft_rbtree_deactivate, - .deactivate_one = nft_rbtree_deactivate_one, + .flush = nft_rbtree_flush, .activate = nft_rbtree_activate, .lookup = nft_rbtree_lookup, .walk = nft_rbtree_walk,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/7 netfilter: nf_tables: add flush field to struct nft_set_iter
In reply to Pablo Neira AyusoThis provides context to walk callback iterator, thus, we know if the walk happens from the set flush path. This is required by the new bitmap set type coming in a follow up patch which has no real struct nft_set_ext, so it has to allocate it based on the two bit compact element representation.
--- include/net/netfilter/nf_tables.h | 1 + net/netfilter/nf_tables_api.c | 1 + 2 files changed, 2 insertions(+)
include/net/netfilter/nf_tables.h +1 -0
@@ -203,6 +203,7 @@ struct nft_set_elem {struct nft_set ;struct nft_set_iter {u8 genmask; +bool flush;unsigned int count;unsigned int skip;int err;
net/netfilter/nf_tables_api.c +1 -0
@@ -3937,6 +3937,7 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,struct nft_set_iter iter = { .genmask = genmask, .fn = nft_flush_set, + .flush = true, }; set->ops->walk (&ctx, set, &iter);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/7 netfilter: nf_tables: rename struct nft_set_estimate class field
In reply to Pablo Neira AyusoUse lookup as field name instead, to prepare the introduction of the memory class in a follow up patch.
--- include/net/netfilter/nf_tables.h | 4 ++-- net/netfilter/nf_tables_api.c | 12 ++++++------ net/netfilter/nft_set_hash.c | 2 +- net/netfilter/nft_set_rbtree.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-)
include/net/netfilter/nf_tables.h +2 -2
@@ -244,11 +244,11 @@ enum nft_set_class {* characteristics * * @size: required memory -* @class: lookup performance class +* @lookup: lookup performance class */ struct nft_set_estimate {unsigned int size; -enum nft_set_class class; +enum nft_set_class lookup; };struct nft_set_ext ;
net/netfilter/nf_tables_api.c +6 -6
@@ -2400,9 +2400,9 @@ nft_select_set_ops(const struct nlattr * const nla[], features &=NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_TIMEOUT ; } - bops =NULL ; - best.size = ~0 ; - best.class = ~0 ; + bops =NULL ; + best.size = ~0 ; + best.lookup = ~0 ;list_for_each_entry (ops, &nf_tables_set_ops, list) {if ((ops->features & features) != features)
@@ -2412,15 +2412,15 @@ nft_select_set_ops(const struct nlattr * const nla[],switch (policy) {case NFT_SET_POL_PERFORMANCE : -if (est.class < best.class) +if (est.lookup < best.lookup)break ; -if (est.class == best.class && est.size < best.size) +if (est.lookup == best.lookup && est.size < best.size)break ;continue ;case NFT_SET_POL_MEMORY :if (est.size < best.size)break ; -if (est.size == best.size && est.class < best.class) +if (est.size == best.size && est.lookup < best.lookup)break ;continue ;default :
net/netfilter/nft_set_hash.c +1 -1
@@ -384,7 +384,7 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, est->size = esize +2 *sizeof (struct nft_hash_elem *); } - est->class =NFT_SET_CLASS_O_1 ; + est->lookup =NFT_SET_CLASS_O_1 ;return true; }
net/netfilter/nft_set_rbtree.c +1 -1
@@ -291,7 +291,7 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,else est->size = nsize; - est->class =NFT_SET_CLASS_O_LOG_N ; + est->lookup =NFT_SET_CLASS_O_LOG_N ;return true; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/7 netfilter: nf_tables: add space notation to sets
In reply to Pablo Neira AyusoThe space notation allows us to classify the set backend implementation based on the amount of required memory. This provides an order of the set representation scalability in terms of memory. The size field is still left in place so use this if the userspace provides no explicit number of elements, so we cannot calculate the real memory that this set needs. This also helps us break ties in the set backend selection routine, eg. two backend implementations provide the same performance.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 22 +++++++++++++++++----- net/netfilter/nft_set_hash.c | 1 + net/netfilter/nft_set_rbtree.c | 1 + 4 files changed, 21 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -245,10 +245,12 @@ enum nft_set_class {* * @size: required memory * @lookup: lookup performance class +* @space: memory class */ struct nft_set_estimate {unsigned int size;enum nft_set_class lookup; +enum nft_set_class space; };struct nft_set_ext ;
net/netfilter/nf_tables_api.c +17 -5
@@ -2403,6 +2403,7 @@ nft_select_set_ops(const struct nlattr * const nla[], bops =NULL ; best.size = ~0 ; best.lookup = ~0 ; + best.space = ~0 ;list_for_each_entry (ops, &nf_tables_set_ops, list) {if ((ops->features & features) != features)
@@ -2414,14 +2415,25 @@ nft_select_set_ops(const struct nlattr * const nla[],case NFT_SET_POL_PERFORMANCE :if (est.lookup < best.lookup)break ; -if (est.lookup == best.lookup && est.size < best.size) -break ; +if (est.lookup == best.lookup) { +if (!desc->size) { +if (est.space < best.space) +break ; + }else if (est.size < best.size) { +break ; + } + }continue ;case NFT_SET_POL_MEMORY : -if (est.size < best.size) -break ; -if (est.size == best.size && est.lookup < best.lookup) +if (!desc->size) { +if (est.space < best.space) +break ; +if (est.space == best.space && + est.lookup < best.lookup) +break ; + }else if (est.size < best.size) {break ; + }continue ;default :break ;
net/netfilter/nft_set_hash.c +1 -0
@@ -385,6 +385,7 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, } est->lookup =NFT_SET_CLASS_O_1 ; + est->space =NFT_SET_CLASS_O_N ;return true; }
net/netfilter/nft_set_rbtree.c +1 -0
@@ -292,6 +292,7 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features, est->size = nsize; est->lookup =NFT_SET_CLASS_O_LOG_N ; + est->space =NFT_SET_CLASS_O_N ;return true; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/7 netfilter: nf_tables: add bitmap set type
In reply to Pablo Neira AyusoThis patch adds a new bitmap set type. This bitmap uses two bits to represent one element. These two bits determine the element state in the current and the future generation that fits into the nf_tables commit protocol. When dumping elements back to userspace, the two bits are expanded into a struct nft_set_ext object.
If no NFTA_SET_DESC_SIZE is specified, the existing automatic set backend selection prefers bitmap over hash in case of keys whose size is <= 16 bit. If the set size is know, the bitmap set type is selected if with 16 bit kets and more than 390 elements in the set, otherwise the hash table set implementation is used.
For 8 bit keys, the bitmap consumes 66 bytes. For 16 bit keys, the bitmap takes 16388 bytes.
--- net/netfilter/Kconfig | 6 + net/netfilter/Makefile | 1 + net/netfilter/nft_set_bitmap.c | 314 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 321 insertions(+) create mode 100644 net/netfilter/nft_set_bitmap.c
net/netfilter/Kconfig +6 -0
@@ -509,6 +509,12 @@ config NFT_SET_HASH This option adds the "hash" set type that is used to build one-way mappings between matchings and actions. +config NFT_SET_BITMAP + tristate "Netfilter nf_tables bitmap set module" + help + This option adds the "bitmap" set type that is used to build sets + whose keys are smaller or equal to 16 bits. + config NFT_COUNTER tristate "Netfilter nf_tables counter module" help
net/netfilter/Makefile +1 -0
@@ -95,6 +95,7 @@ obj-$(CONFIG_NFT_REJECT) += nft_reject.o obj-$(CONFIG_NFT_REJECT_INET) += nft_reject_inet.o obj-$(CONFIG_NFT_SET_RBTREE) += nft_set_rbtree.o obj-$(CONFIG_NFT_SET_HASH) += nft_set_hash.o +obj-$(CONFIG_NFT_SET_BITMAP) += nft_set_bitmap.o obj-$(CONFIG_NFT_COUNTER) += nft_counter.o obj-$(CONFIG_NFT_LOG) += nft_log.o obj-$(CONFIG_NFT_MASQ) += nft_masq.o
net/netfilter/nft_set_bitmap.c new file +314 -0
314 lines not on this page · raw message
Signature · 1 line
2.1.4