new transaction infrastructure for nf_tables (v2)
Subsystems (2)
Pablo Neira Ayuso
maintainercover new transaction infrastructure for nf_tables (v2)
Hi,
This patchset contains updates to the transaction infrastructure and a new batch API to userspace for updating tables, chains and sets. Basically, it generalises the existing rule batching so we can also include sets, chains and tables in one single batch.
This helps to:
* speed up updates since we save many netlink messages between kernel and userspace and this also improves several batch loading error cases.
* leave things in consistent state if we have to abort a batch in the middle of the processing.
The patchset includes userspace changes that allow new versions of nft to operate with the with old and new nf_tables kernels. This means that old nft binaries (ie. 0.099) will not work after this change unless we add some glue code to the kernel. However, I don't find a good reason why someone would be willing to stick to nft-0.099 at this moment, it contains many bugs that we will be resolved in the upcoming version and we also changed some semantic aspects in the upcoming version. If there is someone really willing to stick to nft-0.099 for some reason, I can send patches to add the compatibility code to the kernel.
Changes with v1:
* Simplify the approach by adding a new message type to the transaction and by early addition/deletion of objects from the lists, then undo that in the abort step if needed. Based on suggestions from Patrick.
* Chain counter, rename and policy updates happen in an all or nothing fashion, so the chain is left in consistent state if we have to abort.
* The table configuration are also left in consistent state if we have to abort.
Pablo Neira Ayuso (8):
netfilter: nf_tables: generalise transaction infrastructure
netfilter: nf_tables: relocate commit and abort routines in the source file
netfilter: nf_tables: add message type to transactions
netfilter: nf_tables: move set handling to the transaction infrastructure
netfilter: nf_tables: move chain handling to the transaction infrastructure
netfilter: nf_tables: disabling table hooks always succeeds
netfilter: nf_tables: pass context to nf_tables_uptable
netfilter: nf_tables: move table handling to the transaction infrastructure
include/net/netfilter/nf_tables.h | 28 +- include/uapi/linux/netfilter/nf_tables.h | 6 + net/netfilter/nf_tables_api.c | 788 ++++++++++++++++++++++-------- net/netfilter/nft_lookup.c | 10 +- 4 files changed, 610 insertions(+), 222 deletions(-)
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/8 netfilter: nf_tables: generalise transaction infrastructure
This patch generalises the existing rule transaction infrastructure so it can be used to handle set, table and chain object transactions as well.
--- include/net/netfilter/nf_tables.h | 9 +-- net/netfilter/nf_tables_api.c | 113 +++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 54 deletions(-)
include/net/netfilter/nf_tables.h +5 -4
@@ -341,16 +341,17 @@ struct nft_rule { };/** -* struct nft_rule_trans - nf_tables rule update in transaction +* struct nft_trans - nf_tables object update in transaction * * @list: used internally * @ctx: rule context -* @rule: rule that needs to be updated */ -struct nft_rule_trans { +struct nft_trans {struct list_head list;struct nft_ctx ctx; -struct nft_rule *rule; +union { +struct nft_rule *rule; + }; };static inline struct nft_expr *nft_expr_first (const struct nft_rule *rule)
net/netfilter/nf_tables_api.c +63 -50
201 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/8 netfilter: nf_tables: relocate commit and abort routines in the source file
In reply to Pablo Neira AyusoMove the commit and abort routines to the bottom of the source code file. This change is required by the follow up patches that add the set, chain and table transaction support.
This patch is just a cleanup to access several functions without having to declare their prototypes.
--- net/netfilter/nf_tables_api.c | 156 ++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 78 deletions(-)
net/netfilter/nf_tables_api.c +78 -78
@@ -1824,84 +1824,6 @@ static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,return err; } -static int nf_tables_commit (struct sk_buff *skb) -{ -struct net *net =sock_net (skb->sk); -struct nft_trans *trans, *next; - -/* Bump generation counter, invalidate any dump in progress */ - net->nft.genctr++; - -/* A new generation has just started */ - net->nft.gencursor =gencursor_next (net); - -/* Make sure all packets have left the previous generation before -* purging old rules. -*/ -synchronize_rcu (); - -list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { -/* This rule was inactive in the past and just became active. -* Clear the next bit of the genmask since its meaning has -* changed, now it is the future. -*/ -if (nft_rule_is_active (net, trans->rule)) { -nft_rule_clear (net, trans->rule); -nf_tables_rule_notify (skb, trans->ctx.nlh, - trans->ctx.table, trans->ctx.chain, - trans->rule,NFT_MSG_NEWRULE ,0 , - trans->ctx.afi->family); -nft_trans_destroy (trans); -continue ; - } - -/* This rule is in the past, get rid of it */ -list_del_rcu (&trans->rule->list); -nf_tables_rule_notify (skb, trans->ctx.nlh, - trans->ctx.table, trans->ctx.chain, - trans->rule,NFT_MSG_DELRULE ,0 , - trans->ctx.afi->family); - } - -/* Make sure we don't see any packet traversing old rules */ -synchronize_rcu (); - -/* Now we can safely release unused old rules */ -list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { -nf_tables_rule_destroy (&trans->ctx, trans->rule); -nft_trans_destroy (trans); - } - -return 0 ; -} - -static int nf_tables_abort (struct sk_buff *skb) -{ -struct net *net =sock_net (skb->sk); -struct nft_trans *trans, *next; - -list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { -if (!nft_rule_is_active_next (net, trans->rule)) { -nft_rule_clear (net, trans->rule); -nft_trans_destroy (trans); -continue ; - } - -/* This rule is inactive, get rid of it */ -list_del_rcu (&trans->rule->list); - } - -/* Make sure we don't see any packet accessing aborted rules */ -synchronize_rcu (); - -list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { -nf_tables_rule_destroy (&trans->ctx, trans->rule); -nft_trans_destroy (trans); - } - -return 0 ; -} -/* * Sets */
@@ -2996,6 +2918,84 @@ static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { }, }; +static int nf_tables_commit (struct sk_buff *skb) +{ +struct net *net =sock_net (skb->sk); +struct nft_trans *trans, *next; + +/* Bump generation counter, invalidate any dump in progress */ + net->nft.genctr++; + +/* A new generation has just started */ + net->nft.gencursor =gencursor_next (net); + +/* Make sure all packets have left the previous generation before +* purging old rules. +*/ +synchronize_rcu (); + +list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { +/* This rule was inactive in the past and just became active. +* Clear the next bit of the genmask since its meaning has +* changed, now it is the future. +*/ +if (nft_rule_is_active (net, trans->rule)) { +nft_rule_clear (net, trans->rule); +nf_tables_rule_notify (skb, trans->ctx.nlh, + trans->ctx.table, trans->ctx.chain, + trans->rule,NFT_MSG_NEWRULE ,0 , + trans->ctx.afi->family); +nft_trans_destroy (trans); +continue ; + } + +/* This rule is in the past, get rid of it */ +list_del_rcu (&trans->rule->list); +nf_tables_rule_notify (skb, trans->ctx.nlh, + trans->ctx.table, trans->ctx.chain, + trans->rule,NFT_MSG_DELRULE ,0 , + trans->ctx.afi->family); + } + +/* Make sure we don't see any packet traversing old rules */ +synchronize_rcu (); + +/* Now we can safely release unused old rules */ +list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { +nf_tables_rule_destroy (&trans->ctx, trans->rule); +nft_trans_destroy (trans); + } + +return 0 ; +} + +static int nf_tables_abort (struct sk_buff *skb) +{ +struct net *net =sock_net (skb->sk); +struct nft_trans *trans, *next; + +list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { +if (!nft_rule_is_active_next (net, trans->rule)) { +nft_rule_clear (net, trans->rule); +nft_trans_destroy (trans); +continue ; + } + +/* This rule is inactive, get rid of it */ +list_del_rcu (&trans->rule->list); + } + +/* Make sure we don't see any packet accessing aborted rules */ +synchronize_rcu (); + +list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) { +nf_tables_rule_destroy (&trans->ctx, trans->rule); +nft_trans_destroy (trans); + } + +return 0 ; +} +static const struct nfnetlink_subsystem nf_tables_subsys = { .name ="nf_tables" , .subsys_id =NFNL_SUBSYS_NFTABLES ,
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/8 netfilter: nf_tables: add message type to transactions
In reply to Pablo Neira AyusoThe patch adds message type to the transaction to simplify the commit the and abort routines.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 71 +++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 31 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -344,10 +344,12 @@ struct nft_rule {* struct nft_trans - nf_tables object update in transaction * * @list: used internally +* @msg_type: message type * @ctx: rule context */ struct nft_trans {struct list_head list; +int msg_type;struct nft_ctx ctx;union {struct nft_rule *rule;
net/netfilter/nf_tables_api.c +40 -31
139 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/8 netfilter: nf_tables: move set handling to the transaction infrastructure
In reply to Pablo Neira AyusoThis patch reworks the nf_tables API so set updates are moved into the same batch that contains rule updates. This speeds up rule-set updates we skip a dialog of four messages between kernel and user-space (two on each direction).
1) create the set and send netlink message to the kernel
2) process the response from the kernel that contains the allocated name.
3) add the set elements and send netlink message to the kernel.
4) process the response from the kernel (to check for errors).
To:
1) add the set to the batch.
2) add the set elements to the batch.
3) add the rule that points to the set.
4) send batch to the kernel.
The idea is to allocate an internal set ID to the batch that can be used when adding set elements and rules that refer to the set in the batch.
Note that this patch doesn't add atomic set element updates, it just helps to leave the set configuration in consistent state in case that we fail to load the entire batch for some reason.
Backward compatibility has been only retained in userspace, this means that new nft versions can talk to the kernel both in the new and the old fashion.
--- include/net/netfilter/nf_tables.h | 6 ++ include/uapi/linux/netfilter/nf_tables.h | 6 ++ net/netfilter/nf_tables_api.c | 119 ++++++++++++++++++++++++++---- net/netfilter/nft_lookup.c | 10 ++- 4 files changed, 126 insertions(+), 15 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
@@ -222,6 +222,8 @@ static inline void *nft_set_priv(const struct nft_set *set)struct nft_set *nf_tables_set_lookup (const struct nft_table *table,const struct nlattr *nla); +struct nft_set *nf_tables_set_lookup_byid (const struct net *net, +const struct nlattr *nla);/** * struct nft_set_binding - nf_tables set binding
@@ -353,6 +355,10 @@ struct nft_trans {struct nft_ctx ctx;union {struct nft_rule *rule; +struct { +struct nft_set *set; +u32 set_id; + }; }; };
include/uapi/linux/netfilter/nf_tables.h +6 -0
@@ -221,6 +221,7 @@ enum nft_set_flags {* @NFTA_SET_KEY_LEN: key data length (NLA_U32) * @NFTA_SET_DATA_TYPE: mapping data type (NLA_U32) * @NFTA_SET_DATA_LEN: mapping data length (NLA_U32) +* @NFTA_SET_ID: uniquely identifies a set in a transaction (NLA_U32) */ enum nft_set_attributes {NFTA_SET_UNSPEC ,
@@ -231,6 +232,7 @@ enum nft_set_attributes {NFTA_SET_KEY_LEN ,NFTA_SET_DATA_TYPE ,NFTA_SET_DATA_LEN , +NFTA_SET_ID , __NFTA_SET_MAX };#define NFTA_SET_MAX (__NFTA_SET_MAX - 1)
@@ -266,12 +268,14 @@ enum nft_set_elem_attributes {* @NFTA_SET_ELEM_LIST_TABLE: table of the set to be changed (NLA_STRING) * @NFTA_SET_ELEM_LIST_SET: name of the set to be changed (NLA_STRING) * @NFTA_SET_ELEM_LIST_ELEMENTS: list of set elements (NLA_NESTED: nft_set_elem_attributes) +* @NFTA_SET_ELEM_LIST_SET_ID: uniquely identifies a set in a transaction (NLA_U32) */ enum nft_set_elem_list_attributes {NFTA_SET_ELEM_LIST_UNSPEC ,NFTA_SET_ELEM_LIST_TABLE ,NFTA_SET_ELEM_LIST_SET ,NFTA_SET_ELEM_LIST_ELEMENTS , +NFTA_SET_ELEM_LIST_SET_ID , __NFTA_SET_ELEM_LIST_MAX };#define NFTA_SET_ELEM_LIST_MAX (__NFTA_SET_ELEM_LIST_MAX - 1)
@@ -457,12 +461,14 @@ enum nft_cmp_attributes {* @NFTA_LOOKUP_SET: name of the set where to look for (NLA_STRING) * @NFTA_LOOKUP_SREG: source register of the data to look for (NLA_U32: nft_registers) * @NFTA_LOOKUP_DREG: destination register (NLA_U32: nft_registers) +* @NFTA_LOOKUP_SET_ID: uniquely identifies a set in a transaction (NLA_U32) */ enum nft_lookup_attributes {NFTA_LOOKUP_UNSPEC ,NFTA_LOOKUP_SET ,NFTA_LOOKUP_SREG ,NFTA_LOOKUP_DREG , +NFTA_LOOKUP_SET_ID , __NFTA_LOOKUP_MAX };#define NFTA_LOOKUP_MAX (__NFTA_LOOKUP_MAX - 1)
net/netfilter/nf_tables_api.c +106 -13
259 lines not on this page · raw message
net/netfilter/nft_lookup.c +8 -2
@@ -56,8 +56,14 @@ static int nft_lookup_init(const struct nft_ctx *ctx,return -EINVAL ; set =nf_tables_set_lookup (ctx->table, tb[NFTA_LOOKUP_SET ]); -if (IS_ERR (set)) -return PTR_ERR (set); +if (IS_ERR (set)) { +if (tb[NFTA_LOOKUP_SET_ID ]) { + set =nf_tables_set_lookup_byid (ctx->net, + tb[NFTA_LOOKUP_SET_ID ]); + } +if (IS_ERR (set)) +return PTR_ERR (set); + } priv->sreg =ntohl (nla_get_be32 (tb[NFTA_LOOKUP_SREG ])); err =nft_validate_input_register (priv->sreg);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 5/8 netfilter: nf_tables: move chain handling to the transaction infrastructure
In reply to Pablo Neira AyusoThis patch speeds up rule-set updates and it also introduces a way to revert chain updates if the batch is aborted. The idea is to store the changes in the transaction to apply that in the commit step.
--- include/net/netfilter/nf_tables.h | 7 ++ net/netfilter/nf_tables_api.c | 233 +++++++++++++++++++++++++++---------- 2 files changed, 179 insertions(+), 61 deletions(-)
include/net/netfilter/nf_tables.h +7 -0
@@ -359,6 +359,12 @@ struct nft_trans {struct nft_set *set;u32 set_id; }; +struct { +bool update; +char name[NFT_CHAIN_MAXNAMELEN ]; +struct nft_stats __percpu *stats; +u8 policy; + }; }; };
@@ -394,6 +400,7 @@ static inline void *nft_userdata(const struct nft_rule *rule)enum nft_chain_flags {NFT_BASE_CHAIN =0x1 , +NFT_CHAIN_INACTIVE =0x2 , };/**
net/netfilter/nf_tables_api.c +172 -61
370 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 6/8 netfilter: nf_tables: disabling table hooks always succeeds
In reply to Pablo Neira Ayusonf_tables_table_disable() always succeeds, make this function void.
--- net/netfilter/nf_tables_api.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_api.c +3 -6
@@ -380,7 +380,7 @@ err:return err; } -static int nf_tables_table_disable (const struct nft_af_info *afi, +static void nf_tables_table_disable (const struct nft_af_info *afi,struct nft_table *table) {struct nft_chain *chain;
@@ -390,8 +390,6 @@ static int nf_tables_table_disable(const struct nft_af_info *afi,nf_unregister_hooks (nft_base_chain (chain)->ops, afi->nops); } - -return 0 ; } static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
@@ -411,9 +409,8 @@ static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,if ((flags &NFT_TABLE_F_DORMANT ) && !(table->flags &NFT_TABLE_F_DORMANT )) { - ret =nf_tables_table_disable (afi, table); -if (ret >=0 ) - table->flags |=NFT_TABLE_F_DORMANT ; +nf_tables_table_disable (afi, table); + table->flags |=NFT_TABLE_F_DORMANT ; }else if (!(flags &NFT_TABLE_F_DORMANT ) && table->flags &NFT_TABLE_F_DORMANT ) { ret =nf_tables_table_enable (afi, table);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 7/8 netfilter: nf_tables: pass context to nf_tables_uptable
In reply to Pablo Neira AyusoSo nf_tables_uptable() only takes one single parameter.
--- net/netfilter/nf_tables_api.c | 51 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-)
net/netfilter/nf_tables_api.c +26 -25
@@ -392,36 +392,34 @@ static void nf_tables_table_disable(const struct nft_af_info *afi, } } -static int nf_tables_updtable (struct sock *nlsk,struct sk_buff *skb, -const struct nlmsghdr *nlh, -const struct nlattr *const nla[], -struct nft_af_info *afi,struct nft_table *table) +static int nf_tables_updtable (struct nft_ctx *ctx) { -const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nfgenmsg *nfmsg =nlmsg_data (ctx->nlh);int family = nfmsg->nfgen_family, ret =0 ; +u32 flags; -if (nla[NFTA_TABLE_FLAGS ]) { -u32 flags; +if (!ctx->nla[NFTA_TABLE_FLAGS ]) +return 0 ; - flags =ntohl (nla_get_be32 (nla[NFTA_TABLE_FLAGS ])); -if (flags & ~NFT_TABLE_F_DORMANT ) -return -EINVAL ; + flags =ntohl (nla_get_be32 (ctx->nla[NFTA_TABLE_FLAGS ])); +if (flags & ~NFT_TABLE_F_DORMANT ) +return -EINVAL ; -if ((flags &NFT_TABLE_F_DORMANT ) && - !(table->flags &NFT_TABLE_F_DORMANT )) { -nf_tables_table_disable (afi, table); - table->flags |=NFT_TABLE_F_DORMANT ; - }else if (!(flags &NFT_TABLE_F_DORMANT ) && - table->flags &NFT_TABLE_F_DORMANT ) { - ret =nf_tables_table_enable (afi, table); -if (ret >=0 ) - table->flags &= ~NFT_TABLE_F_DORMANT ; - } -if (ret <0 ) - goto err; - } +if ((flags &NFT_TABLE_F_DORMANT ) && + !(ctx->table->flags &NFT_TABLE_F_DORMANT )) { +nf_tables_table_disable (ctx->afi, ctx->table); + ctx->table->flags |=NFT_TABLE_F_DORMANT ; + }else if (!(flags &NFT_TABLE_F_DORMANT ) && + ctx->table->flags &NFT_TABLE_F_DORMANT ) { + ret =nf_tables_table_enable (ctx->afi, ctx->table); +if (ret >=0 ) + ctx->table->flags &= ~NFT_TABLE_F_DORMANT ; + } +if (ret <0 ) + goto err; -nf_tables_table_notify (skb, nlh, table,NFT_MSG_NEWTABLE , family); +nf_tables_table_notify (ctx->skb, ctx->nlh, ctx->table, +NFT_MSG_NEWTABLE , family); err:return ret; }
@@ -437,6 +435,7 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;u32 flags =0 ; +struct nft_ctx ctx; afi =nf_tables_afinfo_lookup (net, family, true);if (IS_ERR (afi))
@@ -455,7 +454,9 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,return -EEXIST ;if (nlh->nlmsg_flags &NLM_F_REPLACE )return -EOPNOTSUPP ; -return nf_tables_updtable (nlsk, skb, nlh, nla, afi, table); + +nft_ctx_init (&ctx, skb, nlh, afi, table,NULL , nla); +return nf_tables_updtable (&ctx); }if (nla[NFTA_TABLE_FLAGS ]) {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 8/8 netfilter: nf_tables: move table handling to the transaction infrastructure
In reply to Pablo Neira AyusoThis patch speeds up rule-set updates and it also provides a way to revert updates and leave things in consistent state in case that the batch needs to be aborted.
--- include/net/netfilter/nf_tables.h | 4 ++ net/netfilter/nf_tables_api.c | 143 ++++++++++++++++++++++++++++++++----- 2 files changed, 128 insertions(+), 19 deletions(-)
include/net/netfilter/nf_tables.h +4 -0
@@ -365,6 +365,10 @@ struct nft_trans {struct nft_stats __percpu *stats;u8 policy; }; +struct { +bool update_table; +bool enable; + }; }; };
net/netfilter/nf_tables_api.c +124 -19
323 lines not on this page · raw message
Signature · 1 line
1.7.10.4