nf_tables set updates
Subsystems (2)
Pablo Neira Ayuso
maintainercover nf_tables set updates
Hi,
This is a first batch of nf_tables set updates:
1) Use nft_set_ext already accesible as parameter via .flush(), no need for pipapo_deactivate() call.
2) Turn .flush into void, this never fails.
3) Add and use struct nft_elem_priv placeholder, suggested by Florian.
4) Shrink memory usage for set elements in transactions, as well as stack usage.
5) Use struct nft_elem_priv in .insert, in preparation for set timeout updates, this will come in a later patch.
This batch has survived hours of 30s-stress runs and tests/shell, I am still stress testing the set element updates, that will come in a follow up batch.
Thanks
Pablo Neira Ayuso (5):
netfilter: nft_set_pipapo: no need to call pipapo_deactivate() from flush
netfilter: nf_tables: set backend .flush always succeeds
netfilter: nf_tables: expose opaque set element as struct nft_elem_priv
netfilter: nf_tables: shrink memory consumption of set elements
netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST
include/net/netfilter/nf_tables.h | 60 +++++---- net/netfilter/nf_tables_api.c | 217 ++++++++++++++---------------- net/netfilter/nft_dynset.c | 23 ++-- net/netfilter/nft_set_bitmap.c | 53 ++++---- net/netfilter/nft_set_hash.c | 109 +++++++-------- net/netfilter/nft_set_pipapo.c | 73 +++++----- net/netfilter/nft_set_pipapo.h | 4 +- net/netfilter/nft_set_rbtree.c | 71 +++++----- 8 files changed, 305 insertions(+), 305 deletions(-)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: nft_set_pipapo: no need to call pipapo_deactivate() from flush
Use the element object that is already offered instead.
--- net/netfilter/nft_set_pipapo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/netfilter/nft_set_pipapo.c +3 -2
@@ -1814,8 +1814,9 @@ static bool nft_pipapo_flush(const struct net *net, const struct nft_set *set, {struct nft_pipapo_elem *e = elem; -return pipapo_deactivate (net, set, (const u8 *)nft_set_ext_key (&e->ext), - &e->ext); +nft_set_elem_change_active (net, set, &e->ext); + +return true; }/**
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: nf_tables: set backend .flush always succeeds
In reply to Pablo Neira Ayuso.flush is always successful since this results from iterating over the set elements to toggle mark the element as inactive in the next generation.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 9 +-------- net/netfilter/nft_set_bitmap.c | 4 +--- net/netfilter/nft_set_hash.c | 7 ++----- net/netfilter/nft_set_pipapo.c | 4 +--- net/netfilter/nft_set_rbtree.c | 4 +--- 6 files changed, 7 insertions(+), 23 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -449,7 +449,7 @@ struct nft_set_ops {void * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -bool (*flush)(const struct net *net, +void (*flush)(const struct net *net,const struct nft_set *set,void *priv); void (*remove)(const struct net *net,
net/netfilter/nf_tables_api.c +1 -8
@@ -7072,17 +7072,13 @@ static int nft_setelem_flush(const struct nft_ctx *ctx, struct nft_set_elem *elem) {struct nft_trans *trans; -int err; trans =nft_trans_alloc_gfp (ctx,NFT_MSG_DELSETELEM ,sizeof (struct nft_trans_elem ),GFP_ATOMIC );if (!trans)return -ENOMEM ; -if (!set->ops->flush (ctx->net, set, elem->priv)) { - err = -ENOENT ; - goto err1; - } + set->ops->flush (ctx->net, set, elem->priv); set->ndeact++;nft_setelem_data_deactivate (ctx->net, set, elem);
@@ -7091,9 +7087,6 @@ static int nft_setelem_flush(const struct nft_ctx *ctx,nft_trans_commit_list_add_tail (ctx->net, trans);return 0 ; -err1: -kfree (trans); -return err; } static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
net/netfilter/nft_set_bitmap.c +1 -3
@@ -174,7 +174,7 @@ static void nft_bitmap_activate(const struct net *net,nft_set_elem_change_active (net, set, &be->ext); } -static bool nft_bitmap_flush (const struct net *net, +static void nft_bitmap_flush (const struct net *net,const struct nft_set *set,void *_be) {struct nft_bitmap *priv =nft_set_priv (set);
@@ -186,8 +186,6 @@ static bool nft_bitmap_flush(const struct net *net,/* Enter 10 state, similar to deactivation. */ priv->bitmap[idx] &= ~(genmask << off);nft_set_elem_change_active (net, set, &be->ext); - -return true; } static void *nft_bitmap_deactivate(const struct net *net,
net/netfilter/nft_set_hash.c +2 -5
@@ -192,14 +192,12 @@ static void nft_rhash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_change_active (net, set, &he->ext); } -static bool nft_rhash_flush (const struct net *net, +static void nft_rhash_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_rhash_elem *he = priv;nft_set_elem_change_active (net, set, &he->ext); - -return true; } static void *nft_rhash_deactivate(const struct net *net,
@@ -590,13 +588,12 @@ static void nft_hash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_change_active (net, set, &he->ext); } -static bool nft_hash_flush (const struct net *net, +static void nft_hash_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_hash_elem *he = priv;nft_set_elem_change_active (net, set, &he->ext); -return true; } static void *nft_hash_deactivate(const struct net *net,
net/netfilter/nft_set_pipapo.c +1 -3
@@ -1809,14 +1809,12 @@ static void *nft_pipapo_deactivate(const struct net *net,* * Return: true if element was found and deactivated. */ -static bool nft_pipapo_flush (const struct net *net,const struct nft_set *set, +static void nft_pipapo_flush (const struct net *net,const struct nft_set *set,void *elem) {struct nft_pipapo_elem *e = elem;nft_set_elem_change_active (net, set, &e->ext); - -return true; }/**
net/netfilter/nft_set_rbtree.c +1 -3
@@ -530,14 +530,12 @@ static void nft_rbtree_activate(const struct net *net,nft_set_elem_change_active (net, set, &rbe->ext); } -static bool nft_rbtree_flush (const struct net *net, +static void nft_rbtree_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_rbtree_elem *rbe = priv;nft_set_elem_change_active (net, set, &rbe->ext); - -return true; } static void *nft_rbtree_deactivate(const struct net *net,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: nf_tables: expose opaque set element as struct nft_elem_priv
In reply to Pablo Neira AyusoAdd placeholder structure and place it at the beginning of each struct nft_*_elem for each existing set backend, instead of exposing elements as void type to the frontend which defeats compiler type checks. Use this pointer to this new type to replace void *.
This patch updates the following set backend API to use this new struct nft_elem_priv placeholder structure:
- update
- deactivate
- flush
- get
as well as the following helper functions:
- nft_set_elem_ext()
- nft_set_elem_init()
- nft_set_elem_destroy()
- nf_tables_set_elem_destroy()
This patch adds nft_elem_priv_cast() to cast struct nft_elem_priv to native element representation from the corresponding set backend. BUILD_BUG_ON() makes sure this .priv placeholder is always at the top of the opaque set element representation.
---
@Florian, I preferred to BUILD_BUG_ON and have a single nft_elem_priv_cast() macro for this.
include/net/netfilter/nf_tables.h | 38 ++++++++++----- net/netfilter/nf_tables_api.c | 27 ++++++----- net/netfilter/nft_dynset.c | 23 ++++----- net/netfilter/nft_set_bitmap.c | 35 ++++++++------ net/netfilter/nft_set_hash.c | 80 ++++++++++++++++++------------- net/netfilter/nft_set_pipapo.c | 41 ++++++++++------ net/netfilter/nft_set_pipapo.h | 4 +- net/netfilter/nft_set_rbtree.c | 46 ++++++++++-------- 8 files changed, 173 insertions(+), 121 deletions(-)
include/net/netfilter/nf_tables.h +25 -13
@@ -274,6 +274,9 @@ struct nft_userdata {unsigned char data[]; }; +/* placeholder structure for opaque set element backend representation. */ +struct nft_elem_priv { }; +/** * struct nft_set_elem - generic representation of set elements *
@@ -294,9 +297,14 @@ struct nft_set_elem {u32 buf[NFT_DATA_VALUE_MAXLEN /sizeof (u32)];struct nft_data val; } data; -void *priv; +struct nft_elem_priv *priv; }; +static inline void *nft_elem_priv_cast (const struct nft_elem_priv *priv) +{ +return (void *)priv; +} +struct nft_set ;struct nft_set_iter {u8 genmask;
@@ -430,7 +438,8 @@ struct nft_set_ops { const struct nft_set_ext **ext); bool (*update)(struct nft_set *set, const u32 *key, - void *(*new)(struct nft_set *, + struct nft_elem_priv * + (*new)(struct nft_set *, const struct nft_expr *, struct nft_regs *), const struct nft_expr *expr,
@@ -446,19 +455,19 @@ struct nft_set_ops {void (*activate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -void * (*deactivate)(const struct net *net, +struct nft_elem_priv * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);void (*flush)(const struct net *net,const struct nft_set *set, -void *priv); +struct nft_elem_priv *priv);void (*remove)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);void (*walk)(const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_iter *iter); -void * (*get)(const struct net *net, +struct nft_elem_priv * (*get)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem,unsigned int flags);
@@ -796,9 +805,9 @@ static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) }static inline struct nft_set_ext *nft_set_elem_ext (const struct nft_set *set, -void *elem) +const struct nft_elem_priv *elem_priv) { -return elem + set->ops->elemsize; +return (void *)elem_priv + set->ops->elemsize; }static inline struct nft_object **nft_set_ext_obj (const struct nft_set_ext *ext)
@@ -810,16 +819,19 @@ struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,const struct nft_set *set, const struct nlattr *attr); -void *nft_set_elem_init (const struct nft_set *set, -const struct nft_set_ext_tmpl *tmpl, -const u32 *key,const u32 *key_end,const u32 *data, -u64 timeout,u64 expiration,gfp_t gfp); +struct nft_elem_priv *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const u32 *key,const u32 *key_end, +const u32 *data, +u64 timeout,u64 expiration,gfp_t gfp);int nft_set_elem_expr_clone (const struct nft_ctx *ctx,struct nft_set *set,struct nft_expr *expr_array[]); -void nft_set_elem_destroy (const struct nft_set *set,void *elem, +void nft_set_elem_destroy (const struct nft_set *set, +const struct nft_elem_priv *elem_priv,bool destroy_expr);void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, -const struct nft_set *set,void *elem); +const struct nft_set *set, +const struct nft_elem_priv *elem_priv);struct nft_expr_ops ;/**
net/netfilter/nf_tables_api.c +15 -12
@@ -601,7 +601,7 @@ static int nft_mapelem_deactivate(const struct nft_ctx *ctx,struct nft_set_elem_catchall {struct list_head list;struct rcu_head rcu; -void *elem; +struct nft_elem_priv *elem; }; static void nft_map_catchall_deactivate(const struct nft_ctx *ctx,
@@ -6144,10 +6144,11 @@ static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,return 0 ; } -void *nft_set_elem_init (const struct nft_set *set, -const struct nft_set_ext_tmpl *tmpl, -const u32 *key,const u32 *key_end, -const u32 *data,u64 timeout,u64 expiration,gfp_t gfp) +struct nft_elem_priv *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const u32 *key,const u32 *key_end, +const u32 *data, +u64 timeout,u64 expiration,gfp_t gfp) {struct nft_set_ext *ext;void *elem;
@@ -6212,10 +6213,11 @@ static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, }/* Drop references and destroy. Called from gc, dynset and abort path. */ -void nft_set_elem_destroy (const struct nft_set *set,void *elem, +void nft_set_elem_destroy (const struct nft_set *set, +const struct nft_elem_priv *elem_priv,bool destroy_expr) { -struct nft_set_ext *ext =nft_set_elem_ext (set, elem); +struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);struct nft_ctx ctx = { .net =read_pnet (&set->net), .family = set->table->family,
@@ -6226,10 +6228,10 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem,nft_data_release (nft_set_ext_data (ext), set->dtype);if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS ))nft_set_elem_expr_destroy (&ctx,nft_set_ext_expr (ext)); -if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF ))nft_use_dec (&(*nft_set_ext_obj (ext))->use); -kfree (elem); + +kfree (elem_priv); }EXPORT_SYMBOL_GPL (nft_set_elem_destroy);
@@ -6237,14 +6239,15 @@ EXPORT_SYMBOL_GPL(nft_set_elem_destroy);* path via nft_setelem_data_deactivate(). */ void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, -const struct nft_set *set,void *elem) +const struct nft_set *set, +const struct nft_elem_priv *elem_priv) { -struct nft_set_ext *ext =nft_set_elem_ext (set, elem); +struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS ))nft_set_elem_expr_destroy (ctx,nft_set_ext_expr (ext)); -kfree (elem); +kfree (elem_priv); } int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
net/netfilter/nft_dynset.c +12 -11
@@ -44,33 +44,34 @@ static int nft_dynset_expr_setup(const struct nft_dynset *priv,return 0 ; } -static void *nft_dynset_new (struct nft_set *set,const struct nft_expr *expr, -struct nft_regs *regs) +static struct nft_elem_priv *nft_dynset_new (struct nft_set *set, +const struct nft_expr *expr, +struct nft_regs *regs) {const struct nft_dynset *priv =nft_expr_priv (expr);struct nft_set_ext *ext; +void *elem_priv;u64 timeout; -void *elem;if (!atomic_add_unless (&set->nelems,1 , set->size))return NULL ; timeout = priv->timeout ? : set->timeout; - elem =nft_set_elem_init (set, &priv->tmpl, - ®s->data[priv->sreg_key],NULL , - ®s->data[priv->sreg_data], - timeout,0 ,GFP_ATOMIC ); -if (IS_ERR (elem)) + elem_priv =nft_set_elem_init (set, &priv->tmpl, + ®s->data[priv->sreg_key],NULL , + ®s->data[priv->sreg_data], + timeout,0 ,GFP_ATOMIC ); +if (IS_ERR (elem_priv)) goto err1; - ext =nft_set_elem_ext (set, elem); + ext =nft_set_elem_ext (set, elem_priv);if (priv->num_exprs &&nft_dynset_expr_setup (priv, ext) <0 ) goto err2; -return elem; +return elem_priv; err2: -nft_set_elem_destroy (set, elem, false); +nft_set_elem_destroy (set, elem_priv, false); err1:if (set->size)atomic_dec (&set->nelems);
net/netfilter/nft_set_bitmap.c +20 -15
@@ -13,6 +13,7 @@#include <net/netfilter/nf_tables_core.h> struct nft_bitmap_elem { +struct nft_elem_priv priv;struct list_head head;struct nft_set_ext ext; };
@@ -104,8 +105,9 @@ nft_bitmap_elem_find(const struct nft_set *set, struct nft_bitmap_elem *this,return NULL ; } -static void *nft_bitmap_get (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem,unsigned int flags) +static struct nft_elem_priv * +nft_bitmap_get (const struct net *net,const struct nft_set *set, +const struct nft_set_elem *elem,unsigned int flags) {const struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_cur (net);
@@ -116,7 +118,7 @@ static void *nft_bitmap_get(const struct net *net, const struct nft_set *set, !nft_set_elem_active(&be->ext, genmask))continue ; -return be; +return &be->priv; }return ERR_PTR (-ENOENT ); }
@@ -125,8 +127,8 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,const struct nft_set_elem *elem, struct nft_set_ext **ext) { + struct nft_bitmap_elem *new = nft_elem_priv_cast(elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *new = elem->priv, *be;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -148,8 +150,8 @@ static void nft_bitmap_remove(const struct net *net,const struct nft_set *set, const struct nft_set_elem *elem) { + struct nft_bitmap_elem *be = nft_elem_priv_cast(elem->priv);struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -163,8 +165,8 @@ static void nft_bitmap_activate(const struct net *net,const struct nft_set *set, const struct nft_set_elem *elem) { + struct nft_bitmap_elem *be = nft_elem_priv_cast(elem->priv);struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -175,11 +177,12 @@ static void nft_bitmap_activate(const struct net *net, }static void nft_bitmap_flush (const struct net *net, -const struct nft_set *set,void *_be) +const struct nft_set *set, +struct nft_elem_priv *elem_priv) { +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net); -struct nft_bitmap_elem *be = _be;u32 idx, off;nft_bitmap_location (set,nft_set_ext_key (&be->ext), &idx, &off);
@@ -188,12 +191,12 @@ static void nft_bitmap_flush(const struct net *net,nft_set_elem_change_active (net, set, &be->ext); } -static void *nft_bitmap_deactivate (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +static struct nft_elem_priv * +nft_bitmap_deactivate (const struct net *net,const struct nft_set *set, +const struct nft_set_elem *elem) { +struct nft_bitmap_elem *this =nft_elem_priv_cast (elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *this = elem->priv, *be;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -207,7 +210,7 @@ static void *nft_bitmap_deactivate(const struct net *net, priv->bitmap[idx] &= ~(genmask << off);nft_set_elem_change_active (net, set, &be->ext); -return be; +return &be->priv; } static void nft_bitmap_walk(const struct nft_ctx *ctx,
@@ -224,7 +227,7 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&be->ext, iter->genmask)) goto cont; - elem.priv = be; + elem.priv = &be->priv; iter->err = iter->fn (ctx, set, iter, &elem);
@@ -263,6 +266,8 @@ static int nft_bitmap_init(const struct nft_set *set, {struct nft_bitmap *priv =nft_set_priv (set); +BUILD_BUG_ON (offsetof(struct nft_bitmap_elem , priv) !=0 ); +INIT_LIST_HEAD (&priv->list); priv->bitmap_size =nft_bitmap_size (set->klen);
@@ -276,7 +281,7 @@ static void nft_bitmap_destroy(const struct nft_ctx *ctx,struct nft_bitmap_elem *be, *n;list_for_each_entry_safe (be, n, &priv->list, head) -nf_tables_set_elem_destroy (ctx, set, be); +nf_tables_set_elem_destroy (ctx, set, &be->priv); } static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features,
net/netfilter/nft_set_hash.c +46 -34
250 lines not on this page · raw message
net/netfilter/nft_set_pipapo.c +26 -15
115 lines not on this page · raw message
net/netfilter/nft_set_pipapo.h +3 -1
@@ -170,10 +170,12 @@ struct nft_pipapo_elem;/** * struct nft_pipapo_elem - API-facing representation of single set element +* @priv: element placeholder * @ext: nftables API extensions */ struct nft_pipapo_elem { -struct nft_set_ext ext; +struct nft_elem_priv priv; +struct nft_set_ext ext; }; int pipapo_refill(unsigned long *map, int len, int rules, unsigned long *dst,
net/netfilter/nft_set_rbtree.c +26 -20
133 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nf_tables: shrink memory consumption of set elements
In reply to Pablo Neira AyusoInstead of copying struct nft_set_elem into struct nft_trans_elem, store the pointer to the opaque set element object in the transaction. Adapt set backend API (and set backend implementations) to take the pointer to opaque set element representation whenever required.
This patch deconstifies .remove() and .activate() set backend API since these modify the set element opaque object. And it also constify nft_set_elem_ext() this provides access to the nft_set_ext struct without updating the object.
According to pahole on x86_64, this patch shrinks struct nft_trans_elem size from 216 to 24 bytes.
This patch also reduces stack memory consumption by removing the template struct nft_set_elem object, using the opaque set element object instead such as from the set iterator API, catchall elements and the get element command.
--- include/net/netfilter/nf_tables.h | 18 ++-- net/netfilter/nf_tables_api.c | 166 ++++++++++++++---------------- net/netfilter/nft_set_bitmap.c | 16 ++- net/netfilter/nft_set_hash.c | 26 ++--- net/netfilter/nft_set_pipapo.c | 25 ++--- net/netfilter/nft_set_rbtree.c | 19 ++-- 6 files changed, 115 insertions(+), 155 deletions(-)
include/net/netfilter/nf_tables.h +9 -9
@@ -314,7 +314,7 @@ struct nft_set_iter {int (*fn)(const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv); };/**
@@ -454,7 +454,7 @@ struct nft_set_ops { struct nft_set_ext **ext);void (*activate)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);struct nft_elem_priv * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);
@@ -463,7 +463,7 @@ struct nft_set_ops { struct nft_elem_priv *priv);void (*remove)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);void (*walk)(const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_iter *iter);
@@ -1073,7 +1073,7 @@ struct nft_chain {int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain);int nft_setelem_validate (const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);int nft_set_catchall_validate (const struct nft_ctx *ctx,struct nft_set *set);int nf_tables_bind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);void nf_tables_unbind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);
@@ -1650,14 +1650,14 @@ struct nft_trans_table {struct nft_trans_elem {struct nft_set *set; -struct nft_set_elem elem; +struct nft_elem_priv *elem_priv;bool bound; };#define nft_trans_elem_set (trans) \ (((struct nft_trans_elem *)trans->data)->set) -#define nft_trans_elem (trans) \ - (((struct nft_trans_elem *)trans->data)->elem) +#define nft_trans_elem_priv (trans) \ + (((struct nft_trans_elem *)trans->data)->elem_priv)#define nft_trans_elem_set_bound (trans) \ (((struct nft_trans_elem *)trans->data)->bound)
@@ -1698,7 +1698,7 @@ struct nft_trans_gc {struct nft_set *set;u32 seq;u16 count; -void *priv[NFT_TRANS_GC_BATCHCOUNT ]; +struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT ];struct rcu_head rcu; };
@@ -1721,7 +1721,7 @@ struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);void nft_setelem_data_deactivate (const struct net *net,const struct nft_set *set, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);int __init nft_chain_filter_init (void );void nft_chain_filter_fini (void );
net/netfilter/nf_tables_api.c +75 -91
490 lines not on this page · raw message
net/netfilter/nft_set_bitmap.c +6 -10
@@ -146,11 +146,10 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,return 0 ; } -static void nft_bitmap_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +static void nft_bitmap_remove (const struct net *net,const struct nft_set *set, +struct nft_elem_priv *elem_priv) { -struct nft_bitmap_elem *be =nft_elem_priv_cast (elem->priv); +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -163,9 +162,9 @@ static void nft_bitmap_remove(const struct net *net,static void nft_bitmap_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_bitmap_elem *be =nft_elem_priv_cast (elem->priv); +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -219,7 +218,6 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx, {const struct nft_bitmap *priv =nft_set_priv (set);struct nft_bitmap_elem *be; -struct nft_set_elem elem;list_for_each_entry_rcu (be, &priv->list, head) {if (iter->count < iter->skip)
@@ -227,9 +225,7 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&be->ext, iter->genmask)) goto cont; - elem.priv = &be->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &be->priv);if (iter->err <0 )return ;
net/netfilter/nft_set_hash.c +10 -16
@@ -190,9 +190,9 @@ static int nft_rhash_insert(const struct net *net, const struct nft_set *set, }static void nft_rhash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_rhash_elem *he =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &he->ext); }
@@ -230,9 +230,9 @@ nft_rhash_deactivate(const struct net *net, const struct nft_set *set,static void nft_rhash_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_rhash_elem *he =nft_elem_priv_cast (elem_priv);struct nft_rhash *priv =nft_set_priv (set);rhashtable_remove_fast (&priv->ht, &he->node, nft_rhash_params);
@@ -264,7 +264,6 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,struct nft_rhash *priv =nft_set_priv (set);struct nft_rhash_elem *he;struct rhashtable_iter hti; -struct nft_set_elem elem;rhashtable_walk_enter (&priv->ht, &hti);rhashtable_walk_start (&hti);
@@ -284,9 +283,7 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = &he->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &he->priv);if (iter->err <0 )break ;
@@ -592,9 +589,9 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set, }static void nft_hash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_hash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_hash_elem *he =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &he->ext); }
@@ -631,9 +628,9 @@ nft_hash_deactivate(const struct net *net, 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_elem_priv *elem_priv) { -struct nft_hash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_hash_elem *he =nft_elem_priv_cast (elem_priv);hlist_del_rcu (&he->node); }
@@ -643,7 +640,6 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; -struct nft_set_elem elem;int i;for (i =0 ; i < priv->buckets; i++) {
@@ -653,9 +649,7 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = &he->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &he->priv);if (iter->err <0 )return ; cont:
net/netfilter/nft_set_pipapo.c +9 -16
@@ -1548,11 +1548,7 @@ static void nft_pipapo_gc_deactivate(struct net *net, struct nft_set *set, struct nft_pipapo_elem *e) { -struct nft_set_elem elem = { - .priv = &e->priv, - }; - -nft_setelem_data_deactivate (net, set, &elem); +nft_setelem_data_deactivate (net, set, &e->priv); }/**
@@ -1739,7 +1735,7 @@ static void nft_pipapo_abort(const struct nft_set *set)* nft_pipapo_activate() - Mark element reference as active given key, commit * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * On insertion, elements are added to a copy of the matching data currently * in use for lookups, and not directly inserted into current lookup data. Both
@@ -1748,9 +1744,9 @@ static void nft_pipapo_abort(const struct nft_set *set)*/ static void nft_pipapo_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_pipapo_elem *e =nft_elem_priv_cast (elem->priv); +struct nft_pipapo_elem *e =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &e->ext); }
@@ -1803,7 +1799,7 @@ nft_pipapo_deactivate(const struct net *net, const struct nft_set *set,* nft_pipapo_flush() - Call pipapo_deactivate() to make element inactive * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * This is functionally the same as nft_pipapo_deactivate(), with a slightly * different interface, and it's also called once for each element in a set
@@ -1945,7 +1941,7 @@ static bool pipapo_match_field(struct nft_pipapo_field *f,* nft_pipapo_remove() - Remove element given key, commit * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * Similarly to nft_pipapo_activate(), this is used as commit operation by the * API, but it's called once per element in the pending transaction, so we can't
@@ -1953,7 +1949,7 @@ static bool pipapo_match_field(struct nft_pipapo_field *f,* the matched element here, if any, and commit the updated matching data. */ static void nft_pipapo_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) {struct nft_pipapo *priv =nft_set_priv (set);struct nft_pipapo_match *m = priv->clone;
@@ -1961,7 +1957,7 @@ static void nft_pipapo_remove(const struct net *net, const struct nft_set *set,struct nft_pipapo_elem *e;const u8 *data; - e =nft_elem_priv_cast (elem->priv); + e =nft_elem_priv_cast (elem_priv); data = (const u8 *)nft_set_ext_key (&e->ext);while ((rules_f0 =pipapo_rules_same_key (m->f, first_rule))) {
@@ -2038,7 +2034,6 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,for (r =0 ; r < f->rules; r++) {struct nft_pipapo_elem *e; -struct nft_set_elem elem;if (r < f->rules -1 && f->mt[r +1 ].e == f->mt[r].e)continue ;
@@ -2048,9 +2043,7 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set, e = f->mt[r].e; - elem.priv = &e->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &e->priv);if (iter->err <0 ) goto out;
net/netfilter/nft_set_rbtree.c +6 -13
@@ -228,11 +228,7 @@ static void nft_rbtree_gc_remove(struct net *net, struct nft_set *set,struct nft_rbtree *priv, struct nft_rbtree_elem *rbe) { - struct nft_set_elem elem = { - .priv = &rbe->priv, - }; - -nft_setelem_data_deactivate (net, set, &elem); + nft_setelem_data_deactivate(net, set, &rbe->priv);rb_erase (&rbe->node, &priv->root); }
@@ -512,9 +508,9 @@ static int nft_rbtree_insert(const struct net *net, 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_elem_priv *elem_priv) { -struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv); +struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem_priv);struct nft_rbtree *priv =nft_set_priv (set);write_lock_bh (&priv->lock);
@@ -526,9 +522,9 @@ static void nft_rbtree_remove(const struct net *net,static void nft_rbtree_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv); +struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &rbe->ext); }
@@ -589,7 +585,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx, {struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe; -struct nft_set_elem elem;struct rb_node *node;read_lock_bh (&priv->lock);
@@ -601,9 +596,7 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&rbe->ext, iter->genmask)) goto cont; - elem.priv = &rbe->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &rbe->priv);if (iter->err <0 ) {read_unlock_bh (&priv->lock);return ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST
In reply to Pablo Neira AyusoReturn struct nft_elem_priv instead of struct nft_set_ext for consistency with ("netfilter: nf_tables: expose opaque set element as struct nft_elem_priv") and to prepare the introduction of element timeout updates from control path.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 17 ++++++++++------- net/netfilter/nft_set_bitmap.c | 4 ++-- net/netfilter/nft_set_hash.c | 8 ++++---- net/netfilter/nft_set_pipapo.c | 10 +++++----- net/netfilter/nft_set_rbtree.c | 10 +++++----- 6 files changed, 27 insertions(+), 24 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -451,7 +451,7 @@ struct nft_set_ops {int (*insert)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext); +struct nft_elem_priv **priv);void (*activate)(const struct net *net,const struct nft_set *set,struct nft_elem_priv *elem_priv);
net/netfilter/nf_tables_api.c +10 -7
@@ -6332,7 +6332,7 @@ EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);static int nft_setelem_catchall_insert (const struct net *net,struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **pext) +struct nft_elem_priv **priv) {struct nft_set_elem_catchall *catchall;u8 genmask =nft_genmask_next (net);
@@ -6341,7 +6341,7 @@ static int nft_setelem_catchall_insert(const struct net *net,list_for_each_entry (catchall, &set->catchall_list, list) { ext =nft_set_elem_ext (set, catchall->elem);if (nft_set_elem_active (ext, genmask)) { - *pext = ext; + *priv = catchall->elem;return -EEXIST ; } }
@@ -6359,14 +6359,15 @@ static int nft_setelem_catchall_insert(const struct net *net,static int nft_setelem_insert (const struct net *net,struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext,unsigned int flags) +struct nft_elem_priv **elem_priv, +unsigned int flags) {int ret;if (flags &NFT_SET_ELEM_CATCHALL ) - ret =nft_setelem_catchall_insert (net, set, elem, ext); + ret =nft_setelem_catchall_insert (net, set, elem, elem_priv);else - ret = set->ops->insert (net, set, elem, ext); + ret = set->ops->insert (net, set, elem, elem_priv);return ret; }
@@ -6502,13 +6503,14 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,struct nft_set_ext *ext, *ext2;struct nft_set_elem elem;struct nft_set_binding *binding; +struct nft_elem_priv *elem_priv;struct nft_object *obj =NULL ;struct nft_userdata *udata;struct nft_data_desc desc;enum nft_registers dreg;struct nft_trans *trans; -u64 timeout;u64 expiration; +u64 timeout;int err, i;u8 ulen;
@@ -6801,9 +6803,10 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, ext->genmask =nft_genmask_cur (ctx->net); - err =nft_setelem_insert (ctx->net, set, &elem, &ext2, flags); + err =nft_setelem_insert (ctx->net, set, &elem, &elem_priv, flags); if (err) { if (err == -EEXIST) { + ext2 = nft_set_elem_ext(set, elem_priv); if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
net/netfilter/nft_set_bitmap.c +2 -2
@@ -125,7 +125,7 @@ nft_bitmap_get(const struct net *net, const struct nft_set *set,static int nft_bitmap_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_bitmap_elem *new =nft_elem_priv_cast (elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set);
@@ -134,7 +134,7 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set, be =nft_bitmap_elem_find (set, new, genmask);if (be) { - *ext = &be->ext; + *elem_priv = &be->priv;return -EEXIST ; }
net/netfilter/nft_set_hash.c +4 -4
@@ -167,7 +167,7 @@ static bool nft_rhash_update(struct nft_set *set, const u32 *key,static int nft_rhash_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv);struct nft_rhash *priv =nft_set_priv (set);
@@ -183,7 +183,7 @@ static int nft_rhash_insert(const struct net *net, const struct nft_set *set,if (IS_ERR (prev))return PTR_ERR (prev);if (prev) { - *ext = &prev->ext; + *elem_priv = &prev->priv;return -EEXIST ; }return 0 ;
@@ -568,7 +568,7 @@ static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,static int nft_hash_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_hash_elem *this =nft_elem_priv_cast (elem->priv), *he;struct nft_hash *priv =nft_set_priv (set);
@@ -580,7 +580,7 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set,if (!memcmp (nft_set_ext_key (&this->ext),nft_set_ext_key (&he->ext), set->klen) &&nft_set_elem_active (&he->ext, genmask)) { - *ext = &he->ext; + *elem_priv = &he->priv;return -EEXIST ; } }
net/netfilter/nft_set_pipapo.c +5 -5
@@ -1158,13 +1158,13 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,* @net: Network namespace * @set: nftables API set representation * @elem: nftables API element representation containing key data -* @ext2: Filled with pointer to &struct nft_set_ext in inserted element +* @elem_priv: Filled with pointer to &struct nft_set_ext in inserted element * * Return: 0 on success, error pointer on failure. */ static int nft_pipapo_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext2) +struct nft_elem_priv **elem_priv) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS ];
@@ -1195,7 +1195,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,if (!memcmp (start, dup_key->data,sizeof (*dup_key->data)) && !memcmp (end, dup_end->data,sizeof (*dup_end->data))) { - *ext2 = &dup->ext; + *elem_priv = &dup->priv;return -EEXIST ; }
@@ -1210,7 +1210,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,if (PTR_ERR (dup) != -ENOENT ) {if (IS_ERR (dup))return PTR_ERR (dup); - *ext2 = &dup->ext; + *elem_priv = &dup->priv;return -ENOTEMPTY ; }
@@ -1271,7 +1271,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set, } e =nft_elem_priv_cast (elem->priv); - *ext2 = &e->ext; + *elem_priv = &e->priv;pipapo_map (m, rulemap, e);
net/netfilter/nft_set_rbtree.c +5 -5
@@ -306,7 +306,7 @@ static bool nft_rbtree_update_first(const struct nft_set *set,static int __nft_rbtree_insert (const struct net *net,const struct nft_set *set,struct nft_rbtree_elem *new, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rbtree_elem *rbe, *rbe_le =NULL , *rbe_ge =NULL ;struct rb_node *node, *next, *parent, **p, *first =NULL ;
@@ -423,7 +423,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,*/ if (rbe_ge && !nft_rbtree_cmp (set, new, rbe_ge) &&nft_rbtree_interval_start (rbe_ge) ==nft_rbtree_interval_start (new)) { - *ext = &rbe_ge->ext; + *elem_priv = &rbe_ge->priv;return -EEXIST ; }
@@ -432,7 +432,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,*/ if (rbe_le && !nft_rbtree_cmp (set, new, rbe_le) &&nft_rbtree_interval_end (rbe_le) ==nft_rbtree_interval_end (new)) { - *ext = &rbe_le->ext; + *elem_priv = &rbe_le->priv;return -EEXIST ; }
@@ -484,7 +484,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,static int nft_rbtree_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv);struct nft_rbtree *priv =nft_set_priv (set);
@@ -498,7 +498,7 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,write_lock_bh (&priv->lock);write_seqcount_begin (&priv->count); - err =__nft_rbtree_insert (net, set, rbe, ext); + err =__nft_rbtree_insert (net, set, rbe, elem_priv);write_seqcount_end (&priv->count);write_unlock_bh (&priv->lock); }while (err == -EAGAIN );
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerQuoted 14 lines · Pablo Neira Ayuso
Hi,
This is a first batch of
nf_tablesset updates:1) Use
nft_set_extalready accesible as parameter via .flush(), no need forpipapo_deactivate()call.2) Turn .flush into void, this never fails.
3) Add and use struct
nft_elem_privplaceholder, suggested by Florian.4) Shrink memory usage for set elements in transactions, as well as stack usage.
Quoted 6 lines
5) Use struct
nft_elem_privin .insert, in preparation for set timeout updates, this will come in a later patch.This batch has survived hours of 30s-stress runs and tests/shell, I am still stress testing the set element updates, that will come in a follow up batch.
Series applied to nf-next