* [PATCH nf-next 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
@ 2017-01-10 4:42 fgao
2017-01-18 19:55 ` Pablo Neira Ayuso
0 siblings, 1 reply; 2+ messages in thread
From: fgao @ 2017-01-10 4:42 UTC (permalink / raw)
To: pablo, netfilter-devel; +Cc: gfree.wind, Gao Feng
From: Gao Feng <fgao@ikuai8.com>
When something failed in nf_tables_table_enable, it would unregister
the chains. But the rollback codes are same as nf_tables_table_disable
almostly, except there is one counter check.
Now create one wrapper function to eliminate the duplicated codes.
Signed-off-by: Gao Feng <fgao@ikuai8.com>
---
net/netfilter/nf_tables_api.c | 47 ++++++++++++++++++++++---------------------
1 file changed, 24 insertions(+), 23 deletions(-)
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index a019a87..28a0bd7 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -576,6 +576,27 @@ static int nf_tables_gettable(struct net *net, struct sock *nlsk,
return err;
}
+static void _nf_tables_table_disable(struct net *net,
+ const struct nft_af_info *afi,
+ struct nft_table *table,
+ u32 cnt)
+{
+ struct nft_chain *chain;
+
+ list_for_each_entry(chain, &table->chains, list) {
+ if (!nft_is_active_next(net, chain))
+ continue;
+ if (!(chain->flags & NFT_BASE_CHAIN))
+ continue;
+
+ if (cnt-- == 0)
+ break;
+
+ nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
+ afi->nops);
+ }
+}
+
static int nf_tables_table_enable(struct net *net,
const struct nft_af_info *afi,
struct nft_table *table)
@@ -598,18 +619,8 @@ static int nf_tables_table_enable(struct net *net,
}
return 0;
err:
- list_for_each_entry(chain, &table->chains, list) {
- if (!nft_is_active_next(net, chain))
- continue;
- if (!(chain->flags & NFT_BASE_CHAIN))
- continue;
-
- if (i-- <= 0)
- break;
-
- nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
- afi->nops);
- }
+ if (i)
+ _nf_tables_table_disable(net, afi, table, i);
return err;
}
@@ -617,17 +628,7 @@ static void nf_tables_table_disable(struct net *net,
const struct nft_af_info *afi,
struct nft_table *table)
{
- struct nft_chain *chain;
-
- list_for_each_entry(chain, &table->chains, list) {
- if (!nft_is_active_next(net, chain))
- continue;
- if (!(chain->flags & NFT_BASE_CHAIN))
- continue;
-
- nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
- afi->nops);
- }
+ _nf_tables_table_disable(net, afi, table, U32_MAX);
}
static int nf_tables_updtable(struct nft_ctx *ctx)
--
1.9.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH nf-next 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
2017-01-10 4:42 [PATCH nf-next 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable fgao
@ 2017-01-18 19:55 ` Pablo Neira Ayuso
0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2017-01-18 19:55 UTC (permalink / raw)
To: fgao; +Cc: netfilter-devel, gfree.wind
On Tue, Jan 10, 2017 at 12:42:39PM +0800, fgao@ikuai8.com wrote:
> From: Gao Feng <fgao@ikuai8.com>
>
> When something failed in nf_tables_table_enable, it would unregister
> the chains. But the rollback codes are same as nf_tables_table_disable
> almostly, except there is one counter check.
> Now create one wrapper function to eliminate the duplicated codes.
>
> Signed-off-by: Gao Feng <fgao@ikuai8.com>
> ---
> net/netfilter/nf_tables_api.c | 47 ++++++++++++++++++++++---------------------
> 1 file changed, 24 insertions(+), 23 deletions(-)
>
> diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
> index a019a87..28a0bd7 100644
> --- a/net/netfilter/nf_tables_api.c
> +++ b/net/netfilter/nf_tables_api.c
> @@ -576,6 +576,27 @@ static int nf_tables_gettable(struct net *net, struct sock *nlsk,
> return err;
> }
>
> +static void _nf_tables_table_disable(struct net *net,
> + const struct nft_af_info *afi,
> + struct nft_table *table,
> + u32 cnt)
> +{
> + struct nft_chain *chain;
> +
> + list_for_each_entry(chain, &table->chains, list) {
> + if (!nft_is_active_next(net, chain))
> + continue;
> + if (!(chain->flags & NFT_BASE_CHAIN))
> + continue;
> +
> + if (cnt-- == 0)
> + break;
> +
> + nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
> + afi->nops);
> + }
> +}
> +
> static int nf_tables_table_enable(struct net *net,
> const struct nft_af_info *afi,
> struct nft_table *table)
> @@ -598,18 +619,8 @@ static int nf_tables_table_enable(struct net *net,
> }
> return 0;
> err:
> - list_for_each_entry(chain, &table->chains, list) {
> - if (!nft_is_active_next(net, chain))
> - continue;
> - if (!(chain->flags & NFT_BASE_CHAIN))
> - continue;
> -
> - if (i-- <= 0)
> - break;
> -
> - nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
> - afi->nops);
> - }
> + if (i)
> + _nf_tables_table_disable(net, afi, table, i);
> return err;
> }
>
> @@ -617,17 +628,7 @@ static void nf_tables_table_disable(struct net *net,
> const struct nft_af_info *afi,
> struct nft_table *table)
> {
> - struct nft_chain *chain;
> -
> - list_for_each_entry(chain, &table->chains, list) {
> - if (!nft_is_active_next(net, chain))
> - continue;
> - if (!(chain->flags & NFT_BASE_CHAIN))
> - continue;
> -
> - nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
> - afi->nops);
> - }
> + _nf_tables_table_disable(net, afi, table, U32_MAX);
^^^^^^^
While I think it's good to consolidate code, I don't like this.
Probably better something like this?
+static void nft_table_disable(struct net *net,
+ const struct nft_af_info *afi,
+ struct nft_table *table, int cnt)
+{
+ struct nft_chain *chain;
+
+ list_for_each_entry(chain, &table->chains, list) {
+ if (!nft_is_active_next(net, chain))
+ continue;
+ if (!(chain->flags & NFT_BASE_CHAIN))
+ continue;
+
+ nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
+ afi->nops);
+ if (cnt && --cnt == 0)
+ break;
+
+ }
+}
So you call:
nft_table_disable(net, afi, table, 0);
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-01-18 19:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-10 4:42 [PATCH nf-next 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable fgao
2017-01-18 19:55 ` Pablo Neira Ayuso
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).