support for several expression in set elements
Subsystems (2)
Pablo Neira Ayuso
maintainercover support for several expression in set elements
Hi,
This patchset extends nftables to support for several expressions per set element.
So far, users can only specify either a counter or a ratelimit per set element, this patchset allows for combining both.
This patchset adds an artificial cap for up to 2 expressions for set elements which can be easily augmented later on by simply updating the NFT_SET_EXPR_MAX definition.
Comments welcome, thanks.
Pablo Neira Ayuso (5):
netfilter: nftables: generalize set expressions support
netfilter: nftables: move nft_expr before nft_set
netfilter: nftables: generalize set extension to support for several
expressions
netfilter: nftables: add nft_expr_parse() helper function
netfilter: nftables: netlink support for several set element
expressions
include/net/netfilter/nf_tables.h | 105 +++--- include/uapi/linux/netfilter/nf_tables.h | 3 + net/netfilter/nf_tables_api.c | 395 +++++++++++++++++------ net/netfilter/nft_dynset.c | 141 ++++++-- net/netfilter/nft_set_hash.c | 27 +- 5 files changed, 511 insertions(+), 160 deletions(-)
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: nftables: generalize set expressions support
Currently, the set infrastucture allows for one single expressions per element. This patch extends the existing infrastructure to allow for up to two expressions. This is not updating the netlink API yet, this is coming as an initial preparation patch.
--- include/net/netfilter/nf_tables.h | 5 +- net/netfilter/nf_tables_api.c | 87 ++++++++++++++++++++++--------- net/netfilter/nft_dynset.c | 3 +- 3 files changed, 67 insertions(+), 28 deletions(-)
include/net/netfilter/nf_tables.h +4 -1
@@ -396,6 +396,8 @@ struct nft_set_type { };#define to_set_type (o) container_of(o, struct nft_set_type, ops) +#define NFT_SET_EXPR_MAX 2 +/** * struct nft_set - nf_tables set instance *
@@ -448,13 +450,14 @@ struct nft_set {u16 policy;u16 udlen;unsigned char *udata; -struct nft_expr *expr;/* runtime data below here */ const struct nft_set_ops *ops____cacheline_aligned ; u16 flags:14, genmask:2;u8 klen;u8 dlen; +u8 num_exprs; +struct nft_expr *exprs[NFT_SET_EXPR_MAX ]; unsigned char data[] __attribute__((aligned(__alignof__(u64)))); };
net/netfilter/nf_tables_api.c +61 -26
176 lines not on this page · raw message
net/netfilter/nft_dynset.c +2 -1
@@ -188,7 +188,8 @@ static int nft_dynset_init(const struct nft_ctx *ctx,if (IS_ERR (priv->expr))return PTR_ERR (priv->expr); -if (set->expr && set->expr->ops != priv->expr->ops) { +if (set->num_exprs ==1 && + set->exprs[0 ] && set->exprs[0 ]->ops != priv->expr->ops) { err = -EOPNOTSUPP ; goto err_expr_free; }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: nftables: move nft_expr before nft_set
In reply to Pablo Neira AyusoMove the nft_expr structure definition before nft_set. Expressions are used by rules and sets, remove unnecessary forward declarations. This comes as preparation to support for multiple expressions per set element.
--- include/net/netfilter/nf_tables.h | 54 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 28 deletions(-)
include/net/netfilter/nf_tables.h +26 -28
@@ -305,8 +305,33 @@ struct nft_set_estimate {enum nft_set_class space; }; +#define NFT_EXPR_MAXATTR 16 +#define NFT_EXPR_SIZE (size) (sizeof(struct nft_expr) + \ + ALIGN(size, __alignof__(struct nft_expr))) + +/** +* struct nft_expr - nf_tables expression +* +* @ops: expression ops +* @data: expression private data +*/ +struct nft_expr { +const struct nft_expr_ops *ops; +unsigned char data[] + __attribute__((aligned (__alignof__(u64 )))); +}; + +static inline void *nft_expr_priv (const struct nft_expr *expr) +{ +return (void *)expr->data; +} + +int nft_expr_clone (struct nft_expr *dst,struct nft_expr *src); +void nft_expr_destroy (const struct nft_ctx *ctx,struct nft_expr *expr); +int nft_expr_dump (struct sk_buff *skb,unsigned int attr, +const struct nft_expr *expr); +struct nft_set_ext ; -struct nft_expr ;/** * struct nft_set_ops - nf_tables set operations
@@ -797,7 +822,6 @@ struct nft_offload_ctx;* @validate: validate expression, called during loop detection * @data: extra data to attach to this expression operation */ -struct nft_expr ;struct nft_expr_ops { void (*eval)(const struct nft_expr *expr, struct nft_regs *regs,
@@ -833,32 +857,6 @@ struct nft_expr_ops {void *data; }; -#define NFT_EXPR_MAXATTR 16 -#define NFT_EXPR_SIZE (size) (sizeof(struct nft_expr) + \ - ALIGN(size, __alignof__(struct nft_expr))) - -/** -* struct nft_expr - nf_tables expression -* -* @ops: expression ops -* @data: expression private data -*/ -struct nft_expr { -const struct nft_expr_ops *ops; -unsigned char data[] - __attribute__((aligned (__alignof__(u64 )))); -}; - -static inline void *nft_expr_priv (const struct nft_expr *expr) -{ -return (void *)expr->data; -} - -int nft_expr_clone (struct nft_expr *dst,struct nft_expr *src); -void nft_expr_destroy (const struct nft_ctx *ctx,struct nft_expr *expr); -int nft_expr_dump (struct sk_buff *skb,unsigned int attr, -const struct nft_expr *expr); -/** * struct nft_rule - nf_tables rule *
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: nftables: generalize set extension to support for several expressions
In reply to Pablo Neira AyusoThis patch replaces NFT_SET_EXPR by NFT_SET_EXT_EXPRESSIONS. This new extension allows to attach several expressions to one set element (not only one single expression as NFT_SET_EXPR provides). This patch prepares for support for several expressions per set element in the netlink userspace API.
--- include/net/netfilter/nf_tables.h | 31 +++++++-- net/netfilter/nf_tables_api.c | 85 ++++++++++++++++++------ net/netfilter/nft_dynset.c | 103 +++++++++++++++++++++++------- net/netfilter/nft_set_hash.c | 27 ++++++-- 4 files changed, 189 insertions(+), 57 deletions(-)
include/net/netfilter/nf_tables.h +24 -7
@@ -421,6 +421,20 @@ struct nft_set_type { };#define to_set_type (o) container_of(o, struct nft_set_type, ops) +struct nft_set_elem_expr { +u8 size; +unsigned char data[] + __attribute__((aligned (__alignof__(struct nft_expr )))); +}; + +#define nft_setelem_expr_at (__elem_expr, __offset) \ + ((struct nft_expr *)&__elem_expr->data[__offset]) + +#define nft_setelem_expr_foreach (__expr, __elem_expr, __size) \ + for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ + __size < (__elem_expr)->size; \ + __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) +#define NFT_SET_EXPR_MAX 2/**
@@ -547,7 +561,7 @@ void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);* @NFT_SET_EXT_TIMEOUT: element timeout * @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element -* @NFT_SET_EXT_EXPR: expression assiociated with the element +* @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element * @NFT_SET_EXT_OBJREF: stateful object reference associated with element * @NFT_SET_EXT_NUM: number of extension types */
@@ -559,7 +573,7 @@ enum nft_set_extensions {NFT_SET_EXT_TIMEOUT ,NFT_SET_EXT_EXPIRATION ,NFT_SET_EXT_USERDATA , -NFT_SET_EXT_EXPR , +NFT_SET_EXT_EXPRESSIONS ,NFT_SET_EXT_OBJREF , NFT_SET_EXT_NUM };
@@ -677,9 +691,9 @@ static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_extreturn nft_set_ext (ext,NFT_SET_EXT_USERDATA ); } -static inline struct nft_expr *nft_set_ext_expr (const struct nft_set_ext *ext) +static inline struct nft_set_elem_expr *nft_set_ext_expr (const struct nft_set_ext *ext) { -return nft_set_ext (ext,NFT_SET_EXT_EXPR ); +return nft_set_ext (ext,NFT_SET_EXT_EXPRESSIONS ); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
@@ -909,11 +923,14 @@ static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,struct nft_regs *regs, const struct nft_pktinfo *pkt) { + struct nft_set_elem_expr *elem_expr;struct nft_expr *expr; +u32 size; -if (__nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) { - expr =nft_set_ext_expr (ext); - expr->ops->eval (expr, regs, pkt); +if (__nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS )) { + elem_expr =nft_set_ext_expr (ext); +nft_setelem_expr_foreach (expr, elem_expr, size) + expr->ops->eval(expr, regs, pkt); } }
net/netfilter/nf_tables_api.c +65 -20
154 lines not on this page · raw message
net/netfilter/nft_dynset.c +80 -23
172 lines not on this page · raw message
net/netfilter/nft_set_hash.c +20 -7
@@ -293,6 +293,22 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,rhashtable_walk_exit (&hti); } +static bool nft_rhash_expr_needs_gc_run (const struct nft_set *set, +struct nft_set_ext *ext) +{ +struct nft_set_elem_expr *elem_expr =nft_set_ext_expr (ext); +struct nft_expr *expr; +u32 size; + + nft_setelem_expr_foreach(expr, elem_expr, size) { +if (expr->ops->gc && + expr->ops->gc (read_pnet (&set->net), expr)) +return true; + } + +return false; +} +static void nft_rhash_gc (struct work_struct *work) {struct nft_set *set;
@@ -314,16 +330,13 @@ static void nft_rhash_gc(struct work_struct *work)continue ; } -if (nft_set_ext_exists (&he->ext,NFT_SET_EXT_EXPR )) { -struct nft_expr *expr =nft_set_ext_expr (&he->ext); +if (nft_set_ext_exists (&he->ext,NFT_SET_EXT_EXPRESSIONS ) && +nft_rhash_expr_needs_gc_run (set, &he->ext)) + goto needs_gc_run; -if (expr->ops->gc && - expr->ops->gc (read_pnet (&set->net), expr)) - goto gc; - }if (!nft_set_elem_expired (&he->ext))continue ; -gc: +needs_gc_run:if (nft_set_elem_mark_busy (&he->ext))continue ;
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nftables: add nft_expr_parse() helper function
In reply to Pablo Neira AyusoThis new helper function allows you to parse a list expression netlink attributes. This also to reuse the same funcion to support for several expressions in a set element.
--- include/net/netfilter/nf_tables.h | 15 ++++ net/netfilter/nf_tables_api.c | 140 ++++++++++++++++++------------ 2 files changed, 100 insertions(+), 55 deletions(-)
include/net/netfilter/nf_tables.h +15 -0
@@ -331,6 +331,21 @@ void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);int nft_expr_dump (struct sk_buff *skb,unsigned int attr,const struct nft_expr *expr); +struct nft_expr_info { +const struct nft_expr_ops *ops; +const struct nlattr *attr; +struct nlattr *tb[NFT_EXPR_MAXATTR +1 ]; +}; + +struct nft_expr_info_array { +u32 size; +u32 num_exprs; +struct nft_expr_info *info; +}; + +int nft_expr_parse (const struct nft_ctx *ctx,const struct nlattr *attr, +struct nft_expr_info_array *info_array,u32 max_exprs); +struct nft_set_ext ;/**
net/netfilter/nf_tables_api.c +85 -55
226 lines not on this page · raw message
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nftables: netlink support for several set element expressions
In reply to Pablo Neira AyusoThis patch adds three new netlink attribute to encapsulate a list of expressions per set elements:
- NFTA_SET_EXPRESSIONS: this attribute provides the set definition in terms of expressions. New set elements get attached the list of expressions that is specified by this new netlink attribute.
- NFTA_SET_ELEM_EXPRESSIONS: this attribute allows users to restore (or initialize) the stateful information of set elements when adding an element to the set.
- NFTA_DYNSET_EXPRESSIONS: this attribute specifies the list of expressions that the set element gets when it is inserted from the packet path.
--- include/uapi/linux/netfilter/nf_tables.h | 3 + net/netfilter/nf_tables_api.c | 101 ++++++++++++++++++++++- net/netfilter/nft_dynset.c | 43 +++++++++- 3 files changed, 142 insertions(+), 5 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +3 -0
@@ -381,6 +381,7 @@ enum nft_set_attributes {NFTA_SET_OBJ_TYPE ,NFTA_SET_HANDLE ,NFTA_SET_EXPR , +NFTA_SET_EXPRESSIONS , __NFTA_SET_MAX };#define NFTA_SET_MAX (__NFTA_SET_MAX - 1)
@@ -419,6 +420,7 @@ enum nft_set_elem_attributes {NFTA_SET_ELEM_PAD ,NFTA_SET_ELEM_OBJREF ,NFTA_SET_ELEM_KEY_END , +NFTA_SET_ELEM_EXPRESSIONS , __NFTA_SET_ELEM_MAX };#define NFTA_SET_ELEM_MAX (__NFTA_SET_ELEM_MAX - 1)
@@ -725,6 +727,7 @@ enum nft_dynset_attributes {NFTA_DYNSET_SREG_DATA ,NFTA_DYNSET_TIMEOUT ,NFTA_DYNSET_EXPR , +NFTA_DYNSET_EXPRESSIONS ,NFTA_DYNSET_PAD ,NFTA_DYNSET_FLAGS , __NFTA_DYNSET_MAX,
net/netfilter/nf_tables_api.c +99 -2
162 lines not on this page · raw message
net/netfilter/nft_dynset.c +40 -3
70 lines not on this page · raw message
Signature · 1 line
2.20.1
Florian Westphal
maintainerRe: [PATCH nf-next 5/5] netfilter: nftables: netlink support for several set element expressions
net/netfilter/nft_dynset.c static int nft_dynset_init(const struct nft_ctx *ctx, · line 238
Quoted 3 lines · Pablo Neira Ayuso
net/netfilter/nft_dynset.c +3
-0+if ((tb[NFTA_DYNSET_EXPR ] || tb[NFTA_DYNSET_EXPR ]) && + !(set->flags &NFT_SET_EVAL )) +return -EINVAL ;
Hmm, same expression on both sides of ||, one of those should have been NFTA_DYNSET_EXPRESSIONS?