Skip to contentSkip to conversation

examples: add nft-ruleset-replace

PATCH In review Arturo Borrero Gonzalez 3 participants 10 messages Aug 26, 2014 – Sep 1, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 examples: add nft-ruleset-replace

This code examples uses the new NFT_MSG_DELTABLE functionality to replace an entire ruleset in a single transaction/batch.

---
 examples/Makefile.am           |    4 +
 examples/nft-ruleset-replace.c |  203 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 207 insertions(+)
 create mode 100644 examples/nft-ruleset-replace.c
examples/Makefile.am +4 -0
index 69f5c7f..1fbeff9 100644
@@ -23,6 +23,7 @@ check_PROGRAMS = nft-table-add		\
 		 nft-set-elem-get	\
 		 nft-set-elem-del	\
 		 nft-ruleset-get	\
+		 nft-ruleset-replace	\
 		 nft-compat-get
 
 nft_table_add_SOURCES = nft-table-add.c
@@ -94,5 +95,8 @@ nft_set_elem_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 nft_ruleset_get_SOURCES = nft-ruleset-get.c
 nft_ruleset_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
+nft_ruleset_replace_SOURCES = nft-ruleset-replace.c
+nft_ruleset_replace_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
 nft_compat_get_SOURCES = nft-compat-get.c
 nft_compat_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
examples/nft-ruleset-replace.c new file +203 -0

203 lines not on this page · raw message

#1 lore raw

nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Renaming the subject to make it to start a new discussion on something related. Cc'ing Patrick too, perhaps he can pull some better idea out of his hat.

Quoted 2 lines · Arturo Borrero Gonzalez

This code examples uses the new NFT_MSG_DELTABLE functionality to replace an entire ruleset in a single transaction/batch.

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

nft -f -x ruleset-file

The '-x' indicates that you want to flush any previous existing configuration before loading this 'ruleset-file'.

-xx could also be used to remove any configuration regarding the existing families in the ruleset-file, ie. if the ruleset-file only contains a configuration for 'ip', all remaining families are left untouched.

#2 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 8 lines · Pablo Neira Ayuso

Renaming the subject to make it to start a new discussion on something related. Cc'ing Patrick too, perhaps he can pull some better idea out of his hat.

Quoted 2 lines · Arturo Borrero Gonzalez

This code examples uses the new NFT_MSG_DELTABLE functionality to replace an entire ruleset in a single transaction/batch.

Quoted 6 lines

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

Agreed. The naive aproach seems to be something like this:

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

#3 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 18 lines · Patrick McHardy
Quoted 8 lines · Pablo Neira Ayuso

Renaming the subject to make it to start a new discussion on something related. Cc'ing Patrick too, perhaps he can pull some better idea out of his hat.

Quoted 2 lines · Arturo Borrero Gonzalez

This code examples uses the new NFT_MSG_DELTABLE functionality to replace an entire ruleset in a single transaction/batch.

Quoted 6 lines

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

Agreed. The naive aproach seems to be something like this:

Quoted 7 lines

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

The approach in my patchset is different:

- generate a delete command that will flush all the previous ruleset
- generate add commands for each new rule/chain/set/tables
- send the batch to the kernel

In this approach, we don't care about what is in the kernel previous to the delete command.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 31 lines · Arturo Borrero Gonzalez
Quoted 2 lines · Patrick McHardy
Quoted 1 line · Pablo Neira Ayuso

Renaming the subject to make it to start a new discussion on

something

Quoted 1 line
Quoted 1 line

related. Cc'ing Patrick too, perhaps he can pull some better idea

out

Quoted 3 lines
Quoted 3 lines

of his hat.

On Tue, Aug 26, 2014 at 11:57:16AM +0200, Arturo Borrero Gonzalez

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line

This code examples uses the new NFT_MSG_DELTABLE functionality to

replace

Quoted 11 lines
Quoted 8 lines
Quoted 1 line

an entire ruleset in a single transaction/batch.

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

Agreed. The naive aproach seems to be something like this:

Quoted 7 lines

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

Quoted 8 lines

The approach in my patchset is different:

- generate a delete command that will flush all the previous ruleset
- generate add commands for each new rule/chain/set/tables
- send the batch to the kernel

In this approach, we don't care about what is in the kernel previous to the delete command.

Sure, but as Pablo pointed out, it adds more code that needs to be maintained that isn't strictly neccessary.

#5 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 34 lines · Patrick McHardy
Quoted 33 lines · Arturo Borrero Gonzalez
Quoted 2 lines · Patrick McHardy
Quoted 1 line · Pablo Neira Ayuso

Renaming the subject to make it to start a new discussion on

something

Quoted 1 line
Quoted 1 line

related. Cc'ing Patrick too, perhaps he can pull some better idea

out

Quoted 3 lines
Quoted 3 lines

of his hat.

On Tue, Aug 26, 2014 at 11:57:16AM +0200, Arturo Borrero Gonzalez

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line

This code examples uses the new NFT_MSG_DELTABLE functionality to

replace

Quoted 11 lines
Quoted 8 lines
Quoted 1 line

an entire ruleset in a single transaction/batch.

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

Agreed. The naive aproach seems to be something like this:

Quoted 7 lines

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

The approach in my patchset is different:

Quoted 8 lines
Quoted 6 lines

- generate a delete command that will flush all the previous ruleset
- generate add commands for each new rule/chain/set/tables
- send the batch to the kernel

In this approach, we don't care about what is in the kernel previous to the delete command.

Sure, but as Pablo pointed out, it adds more code that needs to be maintained that isn't strictly neccessary.

Oh, I probably didn't explain well myself. I'd like to see Arturo's shortcut using _DELTABLE unless you have any concern with them :-).

We still need that generation ID indeed to catch interferences between two object dumps, I'll send you a patch proposal for this soon.

#6 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Patrick McHardy
Quoted 14 lines
Quoted 7 lines

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

The approach in my patchset is different:

- generate a delete command that will flush all the previous ruleset
- generate add commands for each new rule/chain/set/tables
- send the batch to the kernel

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

In this approach, we don't care about what is in the kernel previous to the delete command.

Sure, but as Pablo pointed out, it adds more code that needs to be maintained that isn't strictly neccessary.

Oh, I probably didn't explain well myself. I'd like to see Arturo's shortcut using _DELTABLE unless you have any concern with them :-).

No concerns, was just trying to support your arguments :) Either way seems reasonable to me.

Quoted 2 lines

We still need that generation ID indeed to catch interferences between two object dumps, I'll send you a patch proposal for this soon.

Yep, we also need this for set transactions. I suppose we need generation IDs for each individual object type as well as AF-specific ones which change for any object change (table level doesn't work because sets are per AF).

#7 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

In reply to Arturo Borrero Gonzalez
Quoted 27 lines · Arturo Borrero Gonzalez
Quoted 18 lines · Patrick McHardy
Quoted 8 lines · Pablo Neira Ayuso

Renaming the subject to make it to start a new discussion on something related. Cc'ing Patrick too, perhaps he can pull some better idea out of his hat.

Quoted 2 lines · Arturo Borrero Gonzalez

This code examples uses the new NFT_MSG_DELTABLE functionality to replace an entire ruleset in a single transaction/batch.

Quoted 6 lines

Thanks for the example but we already have quite a lot of them, and this is yet another almost copy and paste that would need to be maintained.

Please, implement this in nft. I think we can probably have an -x option, eg.

Agreed. The naive aproach seems to be something like this:

Quoted 7 lines

- add a generation ID to the ruleset
- dump the entire ruleset
- generate delete commands for each existing rule/chain/set...
- generate add commands for each new rule/chain/set...
- send the entire thing to the kernel, including the generation ID
- if the generation ID doesn't match, meaning the ruleset has changed since the last dump, return an error to userspace, retry

Quoted 5 lines

The approach in my patchset is different:

- generate a delete command that will flush all the previous ruleset
- generate add commands for each new rule/chain/set/tables
- send the batch to the kernel

We're still going to require the generation ID anyway to catch interferences between rule updates and rule dumping. My plan is to make a patch to include this in the nfgenmsg->res_id when dumping the objects to userspace. The NLM_F_DUMP_INTR is partially solving the problem for us, but we really need that generation ID to catch changes between two object dumps.

Anyway I think both approaches are compatible. The one Arturo is working should be faster if the kernel supports the flusing from the nfnl mutex. I think nft should make it the way Patrick proposes if the kernel doesn't support this command (ie. if it hits -EBUSY when trying to flush a table).

#8 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

In reply to Pablo Neira Ayuso
Quoted 5 lines · Pablo Neira Ayuso

Please, implement this in nft. I think we can probably have an -x option, eg.

nft -f -x ruleset-file

Quoted 8 lines

The '-x' indicates that you want to flush any previous existing configuration before loading this 'ruleset-file'.

-xx could also be used to remove any configuration regarding the existing families in the ruleset-file, ie. if the ruleset-file only contains a configuration for 'ip', all remaining families are left untouched.

Hi Pablo, Patrick.

I've looked into how to implement this '-x' option.

I wonder if it worth having better a "formal" command, like
% nft flush ruleset
% nft flush ruleset ip
% nft flush ruleset ip6
% nft flush ruleset arp
[...]

This way, a user loading a new ruleset with -f can just put a first line like this:

=========
nft flush ruleset
nft add table ip filter
nft add chain ip filter input
nft add rule ip filter input counter
nft add table ip6 filter
nft add chain ip6 filter input
[...]
=========

Or flush per family, as Pablo suggested:

=========
nft flush ruleset inet
nft add table inet filter
[...]
=========

Some benefits of this approach is that we have a concrete order to flush the ruleset, in the case the user wants no ruleset. The lack of this shortcut seem an actual concern of some users.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

Re: nft option to flush out the existing ruleset [was Re: [libnftnl PATCH] examples: add nft-ruleset-replace]

Quoted 47 lines · Arturo Borrero Gonzalez
Quoted 5 lines · Pablo Neira Ayuso

Please, implement this in nft. I think we can probably have an -x option, eg.

nft -f -x ruleset-file

Quoted 8 lines

The '-x' indicates that you want to flush any previous existing configuration before loading this 'ruleset-file'.

-xx could also be used to remove any configuration regarding the existing families in the ruleset-file, ie. if the ruleset-file only contains a configuration for 'ip', all remaining families are left untouched.

Hi Pablo, Patrick.

I've looked into how to implement this '-x' option.

I wonder if it worth having better a "formal" command, like
% nft flush ruleset
% nft flush ruleset ip
% nft flush ruleset ip6
% nft flush ruleset arp
[...]

This way, a user loading a new ruleset with -f can just put a first line like this:

=========
nft flush ruleset
nft add table ip filter
nft add chain ip filter input
nft add rule ip filter input counter
nft add table ip6 filter
nft add chain ip6 filter input
[...]
=========

Or flush per family, as Pablo suggested:

=========
nft flush ruleset inet
nft add table inet filter
[...]
=========

Quoted 3 lines

Some benefits of this approach is that we have a concrete order to flush the ruleset, in the case the user wants no ruleset. The lack of this shortcut seem an actual concern of some users.

I agree, this sounds better than a command line option.