* [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
@ 2017-01-19 11:18 fgao
2017-01-20 7:42 ` Feng Gao
0 siblings, 1 reply; 5+ messages in thread
From: fgao @ 2017-01-19 11:18 UTC (permalink / raw)
To: pablo, netfilter-devel; +Cc: gfree.wind, Feng
From: 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: Feng <fgao@ikuai8.com>
---
v2: Use cnt "0" as unlimited rules counter, per Pablo;
v1: Initial version;
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..c87f8d0 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 && --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, 0);
}
static int nf_tables_updtable(struct nft_ctx *ctx)
--
1.9.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
2017-01-19 11:18 [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable fgao
@ 2017-01-20 7:42 ` Feng Gao
2017-01-20 10:57 ` Liping Zhang
0 siblings, 1 reply; 5+ messages in thread
From: Feng Gao @ 2017-01-20 7:42 UTC (permalink / raw)
To: Pablo Neira Ayuso, Netfilter Developer Mailing List; +Cc: Feng Gao, Feng
On Thu, Jan 19, 2017 at 7:18 PM, <fgao@ikuai8.com> wrote:
> From: 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: Feng <fgao@ikuai8.com>
> ---
> v2: Use cnt "0" as unlimited rules counter, per Pablo;
> v1: Initial version;
>
> 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..c87f8d0 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 && --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, 0);
> }
>
> static int nf_tables_updtable(struct nft_ctx *ctx)
> --
> 1.9.1
>
>
Hi Pablo,
Please ignore this patch.
In v2 patch, I copied your recommend codes "if (cnt && --cnt == 0)".
Today I think it is not right to use "--cnt", it should use "cnt--".
Because when the cnt is 1, the "--cnt" would break directly.
So I send the v3 patch.
Regards
Feng
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
2017-01-20 7:42 ` Feng Gao
@ 2017-01-20 10:57 ` Liping Zhang
2017-01-20 12:30 ` Feng Gao
0 siblings, 1 reply; 5+ messages in thread
From: Liping Zhang @ 2017-01-20 10:57 UTC (permalink / raw)
To: Feng Gao; +Cc: Pablo Neira Ayuso, Netfilter Developer Mailing List, Feng
2017-01-20 15:42 GMT+08:00 Feng Gao <gfree.wind@gmail.com>:
[...]
> In v2 patch, I copied your recommend codes "if (cnt && --cnt == 0)".
> Today I think it is not right to use "--cnt", it should use "cnt--".
> Because when the cnt is 1, the "--cnt" would break directly.
> So I send the v3 patch.
But this "if (cnt && cnt-- == 0) break;" check condition seems still incorrect,
when the cnt is 1, next round cnt will become 0, so we will fail to break the
loop ...
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
2017-01-20 10:57 ` Liping Zhang
@ 2017-01-20 12:30 ` Feng Gao
2017-01-20 13:43 ` Feng Gao
0 siblings, 1 reply; 5+ messages in thread
From: Feng Gao @ 2017-01-20 12:30 UTC (permalink / raw)
To: Liping Zhang; +Cc: Pablo Neira Ayuso, Netfilter Developer Mailing List
Hi Liping,
On Fri, Jan 20, 2017 at 6:57 PM, Liping Zhang <zlpnobody@gmail.com> wrote:
> 2017-01-20 15:42 GMT+08:00 Feng Gao <gfree.wind@gmail.com>:
> [...]
>> In v2 patch, I copied your recommend codes "if (cnt && --cnt == 0)".
>> Today I think it is not right to use "--cnt", it should use "cnt--".
>> Because when the cnt is 1, the "--cnt" would break directly.
>> So I send the v3 patch.
>
> But this "if (cnt && cnt-- == 0) break;" check condition seems still incorrect,
> when the cnt is 1, next round cnt will become 0, so we will fail to break the
> loop ...
Thanks your catch.
So we should keep the "(cnt && --cnt == 0)", with
_nf_tables_table_disable(net, afi, table, i+1) in the error handler of
nf_tables_table_enable.
Do you think if "i+1" is a little werid?
Regards
Feng
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable
2017-01-20 12:30 ` Feng Gao
@ 2017-01-20 13:43 ` Feng Gao
0 siblings, 0 replies; 5+ messages in thread
From: Feng Gao @ 2017-01-20 13:43 UTC (permalink / raw)
To: Liping Zhang; +Cc: Pablo Neira Ayuso, Netfilter Developer Mailing List
On Fri, Jan 20, 2017 at 8:30 PM, Feng Gao <gfree.wind@gmail.com> wrote:
> Hi Liping,
>
> On Fri, Jan 20, 2017 at 6:57 PM, Liping Zhang <zlpnobody@gmail.com> wrote:
>> 2017-01-20 15:42 GMT+08:00 Feng Gao <gfree.wind@gmail.com>:
>> [...]
>>> In v2 patch, I copied your recommend codes "if (cnt && --cnt == 0)".
>>> Today I think it is not right to use "--cnt", it should use "cnt--".
>>> Because when the cnt is 1, the "--cnt" would break directly.
>>> So I send the v3 patch.
>>
>> But this "if (cnt && cnt-- == 0) break;" check condition seems still incorrect,
>> when the cnt is 1, next round cnt will become 0, so we will fail to break the
>> loop ...
>
> Thanks your catch.
> So we should keep the "(cnt && --cnt == 0)", with
> _nf_tables_table_disable(net, afi, table, i+1) in the error handler of
> nf_tables_table_enable.
>
> Do you think if "i+1" is a little werid?
>
> Regards
> Feng
Because the cnt has two usages, it is used as one flag and one counter.
So it is hard to meet these two requirements.
Then I bring one new counter "i", when cnt is positive and i equals
cnt, it could break.
The codes like following:
if (cnt && i++ == cnt) and the i is initialized as 0.
The v4 patch is sent already.
Regards
Feng
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2017-01-20 13:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-19 11:18 [PATCH nf-next v2 1/1] netfilter: nf_tables: Eliminate duplicated codes in nf_tables_table_enable fgao
2017-01-20 7:42 ` Feng Gao
2017-01-20 10:57 ` Liping Zhang
2017-01-20 12:30 ` Feng Gao
2017-01-20 13:43 ` Feng Gao
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.