Skip to contentSkip to conversation

netfilter: nfnetlink: always ACK batch end if requested

PATCH Superseded Nikolaos Gkarlis 5 participants 28 messages Oct 1, 2025 – Feb 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nfnetlink: always ACK batch end if requested

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

To preserve this deterministic behavior, send an ACK for batch end messages even when an error happens in the middle of the batch, similar to how ACKs are handled for command messages.

---
Hi,

I recently came across the issue introduced by bf2ac490d28c and while trying to find a way to handle it by adding an ACK on batch begin, end messages, I spotted what looks like an inconsistency?

I have tested this change with my userspace application and it seems to resolve the "problem". However, I am not sure if there is a suitable place to add a regression test, since AFAIK nft userspace does not currently use this feature. I would be happy to contribute a test if you could point me to the right place.

I may be missing some context, so feedback on whether this is the right approach would be very welcome.

 net/netfilter/nfnetlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/netfilter/nfnetlink.c +5 -0
index 811d02b4c4f7..0342087ead06 100644
@@ -600,6 +600,11 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 			status |= NFNL_BATCH_FAILURE;
 			goto replay_abort;
 		}
+
+		if (nlh->nlmsg_flags & NLM_F_ACK) {
+			memset(&extack, 0, sizeof(extack));
+			nfnl_err_add(&err_list, nlh, 0, &extack);
+		}
 	}
 
 	nfnl_err_deliver(&err_list, oskb);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 33 lines · Nikolaos Gkarlis

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

To preserve this deterministic behavior, send an ACK for batch end messages even when an error happens in the middle of the batch, similar to how ACKs are handled for command messages.

---
Hi,

I recently came across the issue introduced by bf2ac490d28c and while trying to find a way to handle it by adding an ACK on batch begin, end messages, I spotted what looks like an inconsistency?

I have tested this change with my userspace application and it seems to resolve the "problem". However, I am not sure if there is a suitable place to add a regression test, since AFAIK nft userspace does not currently use this feature. I would be happy to contribute a test if you could point me to the right place.

I may be missing some context, so feedback on whether this is the right approach would be very welcome.

  net/netfilter/nfnetlink.c | 5 +++++
  1 file changed, 5 insertions(+)
net/netfilter/nfnetlink.c +0 -0
index 811d02b4c4f7..0342087ead06 100644

net/netfilter/nfnetlink.c static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, · line 608

Quoted 9 lines
net/netfilter/nfnetlink.c +5 -0
@@ -600,6 +600,11 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
  			status |= NFNL_BATCH_FAILURE;
  			goto replay_abort;
  		}
+
+		if (nlh->nlmsg_flags & NLM_F_ACK) {
+			memset(&extack, 0, sizeof(extack));
+			nfnl_err_add(&err_list, nlh, 0, &extack);
+		}
  	}

Right, if BATCH_END message is reached and has the NLM_F_ACK, nfnetlink should send the corresponding ACK. Currently if BATCH_END message is missing, this would send an extra wrong ACK if the previous message was using NLM_F_ACK.

e.g for a batch formatted like (BATCH_BEGIN|NFT_MSG_NEWRULE + NLM_F_ACK) - nfnetlink would send two ACKs while it should be only one. Granted it won't configure anything but it would be still misleading.

What about this?

net/netfilter/nfnetlink.c +1 -1
index b8d0fad1ed10..ecf85346d883 100644
@@ -601,7 +601,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
                        goto replay_abort;
                }
 
-               if (nlh->nlmsg_flags & NLM_F_ACK) {
+               if (nlh->nlmsg_flags & NLM_F_ACK && status & NFNL_BATCH_DONE) {
                        memset(&extack, 0, sizeof(extack));
                        nfnl_err_add(&err_list, nlh, 0, &extack);
                }
Quoted 2 lines
net/netfilter/nfnetlink.c +0 -0
  	nfnl_err_deliver(&err_list, oskb);
#2 lore raw
Quoted 6 lines · Fernando Fernandez Mancera

e.g for a batch formatted like (BATCH_BEGIN|NFT_MSG_NEWRULE + NLM_F_ACK)
- nfnetlink would send two ACKs while it should be only one. Granted it won't configure anything but it would be still misleading.

What about this?

Thanks ! I was a bit unsure on whether the status should also be checked. This seems to work with my test.

#3 lore raw
Quoted 11 lines · Nikolaos Gkarlis
Quoted 6 lines · Fernando Fernandez Mancera

e.g for a batch formatted like (BATCH_BEGIN|NFT_MSG_NEWRULE + NLM_F_ACK)
- nfnetlink would send two ACKs while it should be only one. Granted it won't configure anything but it would be still misleading.

What about this?

Thanks ! I was a bit unsure on whether the status should also be checked. This seems to work with my test.

Just a nit, the commit should also have a Fixes tag IMHO.

Fixes bf2ac490d28c ("netfilter: nfnetlink: Handle ACK flags for batch messages")

Thanks!

#4 lore raw

v2 cover always ACK batch end if requested

Hi again,

Apologies for the delay. I have now added an nfnetlink selftest as suggested by Florian, and committed Fernando's suggestion to also check for the status. I have confirmed that the test fails on kernels > 6.10 that do not include Fernando's 09efbac953f6 fix. The test could be made more extensive, but I think this is a good starting point. Let me know what you think.

Nikolaos Gkarlis (2):
netfilter: nfnetlink: always ACK batch end if requested
selftests: netfilter: add nfnetlink ACK handling tests

 net/netfilter/nfnetlink.c                     |   5 +
 .../testing/selftests/net/netfilter/Makefile  |   3 +
 .../selftests/net/netfilter/nfnetlink.c       | 424 ++++++++++++++++++
 .../selftests/net/netfilter/nfnetlink.sh      |  11 +
 4 files changed, 443 insertions(+)
 create mode 100644 tools/testing/selftests/net/netfilter/nfnetlink.c
 create mode 100755 tools/testing/selftests/net/netfilter/nfnetlink.sh
Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 1/2 netfilter: nfnetlink: always ACK batch end if requested

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

To preserve this deterministic behavior, send an ACK for batch end messages even when an error happens in the middle of the batch, similar to how ACKs are handled for command messages.

Fixes bf2ac490d28c ("netfilter: nfnetlink: Handle ACK flags for batch

messages")

---
 net/netfilter/nfnetlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/netfilter/nfnetlink.c +5 -0
index 811d02b4c4f7..33acc1b94a0e 100644
@@ -600,6 +600,11 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 			status |= NFNL_BATCH_FAILURE;
 			goto replay_abort;
 		}
+
+		if (nlh->nlmsg_flags & NLM_F_ACK && status & NFNL_BATCH_DONE) {
+			memset(&extack, 0, sizeof(extack));
+			nfnl_err_add(&err_list, nlh, 0, &extack);
+		}
 	}
 
 	nfnl_err_deliver(&err_list, oskb);
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 2/2 selftests: netfilter: add nfnetlink ACK handling tests

In reply to Nikolaos Gkarlis

Add nfnetlink selftests to validate the ACKs sent after a batch message. These tests verify that:

- ACKs are always received in order.
- Module loading does not affect the responses.
- The number of ACKs matches the number of requests, unless a fatal error occurs.

---
 .../testing/selftests/net/netfilter/Makefile  |   3 +
 .../selftests/net/netfilter/nfnetlink.c       | 424 ++++++++++++++++++
 .../selftests/net/netfilter/nfnetlink.sh      |  11 +
 3 files changed, 438 insertions(+)
 create mode 100644 tools/testing/selftests/net/netfilter/nfnetlink.c
 create mode 100755 tools/testing/selftests/net/netfilter/nfnetlink.sh
tools/testing/selftests/net/netfilter/Makefile +3 -0
index a98ed892f55f..cb38c8a9b2cc 100644
@@ -37,6 +37,7 @@ TEST_PROGS += nft_zones_many.sh
 TEST_PROGS += rpath.sh
 TEST_PROGS += vxlan_mtu_frag.sh
 TEST_PROGS += xt_string.sh
+TEST_PROGS += nfnetlink.sh
 
 TEST_PROGS_EXTENDED = nft_concat_range_perf.sh
 
@@ -46,6 +47,7 @@ TEST_GEN_FILES += conntrack_dump_flush
 TEST_GEN_FILES += conntrack_reverse_clash
 TEST_GEN_FILES += sctp_collision
 TEST_GEN_FILES += udpclash
+TEST_GEN_FILES += nfnetlink
 
 include ../../lib.mk
 
@@ -54,6 +56,7 @@ $(OUTPUT)/nf_queue: LDLIBS += $(MNL_LDLIBS)
 
 $(OUTPUT)/conntrack_dump_flush: CFLAGS += $(MNL_CFLAGS)
 $(OUTPUT)/conntrack_dump_flush: LDLIBS += $(MNL_LDLIBS)
+$(OUTPUT)/nfnetlink: LDLIBS += $(MNL_LDLIBS)
 $(OUTPUT)/udpclash: LDLIBS += -lpthread
 
 TEST_FILES := lib.sh
tools/testing/selftests/net/netfilter/nfnetlink.c new file +424 -0

424 lines not on this page · raw message

tools/testing/selftests/net/netfilter/nfnetlink.sh new file +11 -0
new file mode 100755
index 000000000000..4456796732a8
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+exec unshare -n bash -c '
+  nft flush ruleset
+
+  rmmod nft_ct
+
+  ./nfnetlink
+
+  nft flush ruleset
+'
\ No newline at end of file
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH v2 2/2] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 7 lines · Nikolaos Gkarlis

Add nfnetlink selftests to validate the ACKs sent after a batch message. These tests verify that:

- ACKs are always received in order.
- Module loading does not affect the responses.
- The number of ACKs matches the number of requests, unless a fatal error occurs.

Thanks for the tests!

This looks good to me, justs one minor nit.

Can you drop the shell wrapper and just call unshare(CLONE_NEWNET) from the fixture setup function?

Quoted 10 lines

+++ b/tools/testing/selftests/net/netfilter/Makefile

tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -37,6 +37,7 @@ TEST_PROGS += nft_zones_many.sh
 TEST_PROGS += rpath.sh
 TEST_PROGS += vxlan_mtu_frag.sh
 TEST_PROGS += xt_string.sh
+TEST_PROGS += nfnetlink.sh
 
 TEST_PROGS_EXTENDED = nft_concat_range_perf.sh
 

tools/testing/selftests/net/netfilter/Makefile TEST_GEN_FILES += conntrack_dump_flush · line 50

Quoted 4 lines
tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -46,6 +47,7 @@ TEST_GEN_FILES += conntrack_dump_flush
 TEST_GEN_FILES += conntrack_reverse_clash
 TEST_GEN_FILES += sctp_collision
 TEST_GEN_FILES += udpclash
+TEST_GEN_FILES += nfnetlink

replacing this with
TEST_GEN_PROGS = nfnetlink

... should make kselftests run this prog just like it does for TEST_PROGS shell scripts.

#8 lore raw

Re: [PATCH v2 2/2] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 2 lines · Florian Westphal

Can you drop the shell wrapper and just call unshare(CLONE_NEWNET) from the fixture setup function?

Yes, I can do that. However, I am wondering about cleanup both before and after the tests, as well as ensuring that the nft_ct module is not loaded prior to execution. Should these steps be included in the program, or do we assume that the tests will always run on a clean system with no modules already loaded?

Thanks for the review!

maintainer
#9 lore raw

Re: [PATCH v2 2/2] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 7 lines · Nikolaos Gkarlis
Quoted 2 lines · Florian Westphal

Can you drop the shell wrapper and just call unshare(CLONE_NEWNET) from the fixture setup function?

Yes, I can do that. However, I am wondering about cleanup both before and after the tests, as well as ensuring that the nft_ct module is not loaded prior to execution.

Is this to exercise replay path?
Perhaps add a comment to the subtest that depends on this.

I don't see the need for this otherwise.

What happens when nft_ct is builtin? The test should not fail in that case.

Quoted 3 lines

Should these steps be
included in the program, or do we assume that the tests will always run on a clean system with no modules already loaded?

No, if you need nft_ct to not be loaded then the rmmod has to be used, but there is no guarantee it will work, e.g. because nft_ct is builtin or because its in use.

#10 lore raw

Re: [PATCH v2 2/2] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 2 lines · Florian Westphal

Is this to exercise replay path?
Perhaps add a comment to the subtest that depends on this.

Yes, that is the goal. I can add a comment to the test explaining that.

Quoted 2 lines

What happens when nft_ct is builtin? The test should not fail in that case.

No, the test does not fail when nft_ct is builtin or missing. It only verifies the number of ACKs returned and their order, which is what bf2ac490d28c broke. I have tested both builtin and non-existent cases.

Quoted 3 lines

No, if you need nft_ct to not be loaded then the rmmod has to be used, but there is no guarantee it will work, e.g. because nft_ct is builtin or because it's in use.

What do you think about something like this?

+++ b/tools/testing/selftests/net/netfilter/nfnetlink.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+# If nft_ct is a module and is loaded, remove it to test module auto-loading +lsmod | grep -q "^nft_ct\b" && rmmod nft_ct
+./nfnetlink

I can send a v3 if that looks okay to you.

maintainer
#11 lore raw

Re: [PATCH v2 2/2] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 5 lines · Nikolaos Gkarlis
Quoted 2 lines · Florian Westphal

Is this to exercise replay path?
Perhaps add a comment to the subtest that depends on this.

Yes, that is the goal. I can add a comment to the test explaining that.

Thanks!

Quoted 2 lines

verifies the number of ACKs returned and their order, which is what bf2ac490d28c broke. I have tested both builtin and non-existent cases.

Great.

tools/testing/selftests/net/netfilter/nfnetlink.sh line 3

Quoted 7 lines

What do you think about something like this?

+++ b/tools/testing/selftests/net/netfilter/nfnetlink.sh

tools/testing/selftests/net/netfilter/nfnetlink.sh new file +3 -0
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+# If nft_ct is a module and is loaded, remove it to test module auto-loading

Looks good, can you also mention that if removal doesn't work this is ok?

Review line 4

Quoted 1 line
(quoted diff) +1 -0
+lsmod | grep -q "^nft_ct\b" && rmmod nft_ct

You could just "rmmod nft_ct 2>/dev/null" with
above comment in place.

tools/testing/selftests/net/netfilter/nfnetlink.sh line 8

Quoted 1 line
tools/testing/selftests/net/netfilter/nfnetlink.sh new file +1 -0
+./nfnetlink

Since you need the shell wraper for the rmmod you could also make this

unshare -n ./nfnetlink and not do it in the .c setup function. Your call.

Quoted 1 line

I can send a v3 if that looks okay to you.

Yes please. You can just send a v3 of the test case, no need to resend the fix.

#12 lore raw

PATCH v3 1/1 selftests: netfilter: add nfnetlink ACK handling tests

Add nfnetlink selftests to validate the ACKs sent after a batch message. These tests verify that:

- ACKs are always received in order.
- Module loading does not affect the responses.
- The number of ACKs matches the number of requests, unless a fatal error occurs.

---
 .../testing/selftests/net/netfilter/Makefile  |   3 +
 .../selftests/net/netfilter/nfnetlink.c       | 437 ++++++++++++++++++
 .../selftests/net/netfilter/nfnetlink.sh      |   6 +
 3 files changed, 446 insertions(+)
 create mode 100644 tools/testing/selftests/net/netfilter/nfnetlink.c
 create mode 100755 tools/testing/selftests/net/netfilter/nfnetlink.sh
tools/testing/selftests/net/netfilter/Makefile +3 -0
index a98ed892f55f..cb38c8a9b2cc 100644
@@ -37,6 +37,7 @@ TEST_PROGS += nft_zones_many.sh
 TEST_PROGS += rpath.sh
 TEST_PROGS += vxlan_mtu_frag.sh
 TEST_PROGS += xt_string.sh
+TEST_PROGS += nfnetlink.sh
 
 TEST_PROGS_EXTENDED = nft_concat_range_perf.sh
 
@@ -46,6 +47,7 @@ TEST_GEN_FILES += conntrack_dump_flush
 TEST_GEN_FILES += conntrack_reverse_clash
 TEST_GEN_FILES += sctp_collision
 TEST_GEN_FILES += udpclash
+TEST_GEN_FILES += nfnetlink
 
 include ../../lib.mk
 
@@ -54,6 +56,7 @@ $(OUTPUT)/nf_queue: LDLIBS += $(MNL_LDLIBS)
 
 $(OUTPUT)/conntrack_dump_flush: CFLAGS += $(MNL_CFLAGS)
 $(OUTPUT)/conntrack_dump_flush: LDLIBS += $(MNL_LDLIBS)
+$(OUTPUT)/nfnetlink: LDLIBS += $(MNL_LDLIBS)
 $(OUTPUT)/udpclash: LDLIBS += -lpthread
 
 TEST_FILES := lib.sh
tools/testing/selftests/net/netfilter/nfnetlink.c new file +437 -0

437 lines not on this page · raw message

tools/testing/selftests/net/netfilter/nfnetlink.sh new file +6 -0
new file mode 100755
index 000000000000..3d264a4bde6f
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+# If nft_ct is a module and is loaded, remove it to test module auto-loading.
+# If removal fails, continue anyway since it won't affect the test result.
+rmmod nft_ct 2>/dev/null
+./nfnetlink
\ No newline at end of file
Signature · 1 line
2.34.1
maintainer
#13 lore raw

Re: [PATCH v3] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 7 lines · Nikolaos Gkarlis

Add nfnetlink selftests to validate the ACKs sent after a batch message. These tests verify that:

- ACKs are always received in order.
- Module loading does not affect the responses.
- The number of ACKs matches the number of requests, unless a fatal error occurs.

I will keep this on hold while the discussion wrt. ACK flag on batches is ongoing.

Also, checkpatch.pl reports:

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1 #59: FILE: tools/testing/selftests/net/netfilter/nfnetlink.c:1:

... and a few other nits that should be resolved too.

#14 lore raw

Re: [PATCH v3] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 2 lines · Florian Westphal

I will keep this on hold while the discussion wrt. ACK flag on batches is ongoing.

Thanks, I appreciate you taking the time for this.

Quoted 6 lines

Also, checkpatch.pl reports:

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1 #59: FILE: tools/testing/selftests/net/netfilter/nfnetlink.c:1:

... and a few other nits that should be resolved too.

Apologies, I will submit a new version ASAP.

maintainer
#15 lore raw

Re: [PATCH v3] selftests: netfilter: add nfnetlink ACK handling tests

Quoted 3 lines · Nikolaos Gkarlis
Quoted 1 line

... and a few other nits that should be resolved too.

Apologies, I will submit a new version ASAP.

No need, please wait until the discussion has completed so we know if the selftest can be taken as-is or if it has to be adjusted (no-batch-end-ack-on-error).

maintainer
#16 lore raw

Re: [PATCH v3] selftests: netfilter: add nfnetlink ACK handling tests

In reply to Nikolaos Gkarlis
Quoted 7 lines · Nikolaos Gkarlis

Add nfnetlink selftests to validate the ACKs sent after a batch message. These tests verify that:

- ACKs are always received in order.
- Module loading does not affect the responses.
- The number of ACKs matches the number of requests, unless a fatal error occurs.

I'm marking this as 'changes requested' in patchwork.

As far as I can see this test also needs to be updated to not expect an ACK on the 'end batch'.

The consensus in the other thread appears to be that userspace cannot rely on a given number of acks being present, as the receive buffer might not be large enough to hold all of them.

#17 lore raw

v2 cover always ACK batch end if requested

In reply to Fernando Fernandez Mancera

Hi again,

Apologies for the delay. I have now added an nfnetlink selftest as suggested by Florian, and committed Fernando's suggestion to also check for the status. I have confirmed that the test fails on kernels > 6.10 that do not include Fernando's 09efbac953f6 fix. The test could be made more extensive, but I think this is a good starting point. Let me know what you think.

This is a resend to fix threading. Sorry about that.

Nikolaos Gkarlis (2):
netfilter: nfnetlink: always ACK batch end if requested
selftests: netfilter: add nfnetlink ACK handling tests

 net/netfilter/nfnetlink.c                     |   5 +
 .../testing/selftests/net/netfilter/Makefile  |   3 +
 .../selftests/net/netfilter/nfnetlink.c       | 424 ++++++++++++++++++
 .../selftests/net/netfilter/nfnetlink.sh      |  11 +
 4 files changed, 443 insertions(+)
 create mode 100644 tools/testing/selftests/net/netfilter/nfnetlink.c
 create mode 100755 tools/testing/selftests/net/netfilter/nfnetlink.sh
Signature · 1 line
2.34.1
maintainer
#18 lore raw
In reply to Nikolaos Gkarlis
Quoted 5 lines · Nikolaos Gkarlis

I have tested this change with my userspace application and it seems to resolve the "problem". However, I am not sure if there is a suitable place to add a regression test, since AFAIK nft userspace does not currently use this feature. I would be happy to contribute a test if you could point me to the right place.

You could add a nfnetlink selftest to:
tools/testing/selftests/net/netfilter

#19 lore raw
Quoted 2 lines · Florian Westphal

You could add a nfnetlink selftest to:
tools/testing/selftests/net/netfilter

Thanks! I will add the test and include it in the patch. I should have some time to look at it this evening. Since there seem to be many corner cases, I suggest we wait for the test before merging, unless there is a particular rush to get this out.

#20 lore raw
In reply to Nikolaos Gkarlis

Hi Nikolaos,

Quoted 3 lines · Nikolaos Gkarlis

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

Regarding bf2ac490d28c, I don't understand why one needs an ack for _BEGIN message. Maybe, an ack for END message might make sense when BATCH_DONE is reached so you get a confirmation that the batch has been fully processed, however...

Quoted 3 lines

To preserve this deterministic behavior, send an ACK for batch end messages even when an error happens in the middle of the batch, similar to how ACKs are handled for command messages.

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

Let's just forget about this bf2ac490d28c for a moment, a quick summary:

#1 If you don't set NLM_F_ACK in your netlink messages in the batch
(this is what netfilter's userspace does): then errors result in acknowledgement. But ENOBUFS is still possible: this means your batch has resulted in too many acknowledment messages (errors) filling up the userspace netlink socket buffer.

#2 If you set NLM_F_ACK in your netlink messages in the batch:
You get one acknowledgement for each message in the batch, with a sufficiently large batch, this may overrun the userspace socket buffer (ENOBUFS), then maybe the kernel was successful to fully process the transaction but some of those acks get lost.

That is, 1bf2ac490d28c allows you to set on NLM_F_ACK for BATCH_END, but that acknowledgement can get lost in case of netlink socket overrun.

ENOBUFS scenarios break assumptions on the number of messages that you are going to receive from the kernel.

Netlink is a unreliable transport protocol, there are mechanisms to make it "more reliable" but message loss (particularly in the kernel -> userspace direction) is still possible.

In this particular case, where batching several netlink messages in one single send(), userspace will not process the acknowledments messages in the userspace socket buffer until the batch is complete.

maintainer
#21 lore raw

[ Cc Donald Hunter ]

Quoted 9 lines · Pablo Neira Ayuso
Quoted 3 lines · Nikolaos Gkarlis

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

Regarding bf2ac490d28c, I don't understand why one needs an ack for _BEGIN message. Maybe, an ack for END message might make sense when BATCH_DONE is reached so you get a confirmation that the batch has been fully processed, however...

... which (BATCH_DONE reached) would be made moot by this proposed patch, as we would ACK it even if its not reached anymore.

Quoted 6 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

Let's just forget about this bf2ac490d28c for a moment, a quick summary:

Quoted 10 lines

#1 If you don't set NLM_F_ACK in your netlink messages in the batch
(this is what netfilter's userspace does): then errors result in acknowledgement. But ENOBUFS is still possible: this means your batch has resulted in too many acknowledment messages (errors) filling up the userspace netlink socket buffer.
#2 If you set NLM_F_ACK in your netlink messages in the batch:
You get one acknowledgement for each message in the batch, with a sufficiently large batch, this may overrun the userspace socket buffer (ENOBUFS), then maybe the kernel was successful to fully process the transaction but some of those acks get lost.

Right, 1:1 relationship between messages and ACKs is only there for theoretical infinite receive buffer which makes this feature rather limited for batched case.

Quoted 3 lines

In this particular case, where batching several netlink messages in one single send(), userspace will not process the acknowledments messages in the userspace socket buffer until the batch is complete.

OK, from what I gather you'd like for "netfilter: nfnetlink: always ACK batch end if requested" to not be applied.

I would still like to apply the nfnetlink selftest however (even
if it has to be trimmed/modified), because it does catch the issue fixed by Fernando
[ 09efbac953f6 ("netfilter: nfnetlink: reset nlh pointer during batch replay") ]:

ok 1 nfnetlink_batch.simple_batch
# RUN nfnetlink_batch.module_load ...
# nfnetlink.c:239:module_load:[seq=1759907514] ACK
# nfnetlink.c:239:module_load:[seq=1759907512] ACK
# nfnetlink.c:244:module_load:Out of order ack: seq 1759907512 after 1759907514 # nfnetlink.c:239:module_load:[seq=1759907513] ACK
# nfnetlink.c:239:module_load:[seq=1759907514] ACK
# nfnetlink.c:239:module_load:[seq=1759907515] ACK
# nfnetlink.c:254:module_load:Expected 0 (0) == out_of_order (1)
# module_load: Test terminated by assertion
# FAIL nfnetlink_batch.module_load

If the decision is that there should NOT be an ACK for the BATCH_END if there was an error, then the test only needs minor adjustment:

-       // Expect 5 acks: batch_begin, table, invalid_table(error), chain, batch_end
-       validate_res(self->nl, seq - 5, 5, _metadata);
+       // Expect 4 acks: batch_begin, table, invalid_table(error), chain
+       validate_res(self->nl, seq - 4, 4, _metadata);

So, what is the 'more useful' behaviour? Choices are all equally bad:

1. If we want to always include it, it might not be there due to -ENOBUFS, which will always happen if the batch was large (triggers too many acks).
2. If we only include it on success, it might not be there for the same reason, so absence doesn't imply failure.

HOWEVER, if the batch was small enough then 2) gives slightly more useable feedback in the sense that the entire batch was processed.

So I am leaning towards not applying the nfnetlink patch but applying the (adjusted) test case.

Other takes?

#22 lore raw
Quoted 8 lines · Florian Westphal

[ Cc Donald Hunter ]

Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Nikolaos Gkarlis

Before ACKs were introduced for batch begin and batch end messages, userspace expected to receive the same number of ACKs as it sent, unless a fatal error occurred.

Quoted 7 lines
Quoted 4 lines

Regarding bf2ac490d28c, I don't understand why one needs an ack for _BEGIN message. Maybe, an ack for END message might make sense when BATCH_DONE is reached so you get a confirmation that the batch has been fully processed, however...

... which (BATCH_DONE reached) would be made moot by this proposed patch, as we would ACK it even if its not reached anymore.

Yes, I am inclined not to add more features to bf2ac490d28c (and follow up fixes patches that came with it).

Quoted 17 lines
Quoted 6 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

Let's just forget about this bf2ac490d28c for a moment, a quick summary:

Quoted 10 lines

#1 If you don't set NLM_F_ACK in your netlink messages in the batch
(this is what netfilter's userspace does): then errors result in acknowledgement. But ENOBUFS is still possible: this means your batch has resulted in too many acknowledment messages (errors) filling up the userspace netlink socket buffer.
#2 If you set NLM_F_ACK in your netlink messages in the batch:
You get one acknowledgement for each message in the batch, with a sufficiently large batch, this may overrun the userspace socket buffer (ENOBUFS), then maybe the kernel was successful to fully process the transaction but some of those acks get lost.

Quoted 3 lines

Right, 1:1 relationship between messages and ACKs is only there for theoretical infinite receive buffer which makes this feature rather limited for batched case.

Exactly.

Quoted 7 lines
Quoted 3 lines

In this particular case, where batching several netlink messages in one single send(), userspace will not process the acknowledments messages in the userspace socket buffer until the batch is complete.

OK, from what I gather you'd like for "netfilter: nfnetlink: always ACK batch end if requested" to not be applied.

I think this at least needs more discussion, I think we are now understanding the implications of bf2ac490d28c.

Quoted 25 lines

I would still like to apply the nfnetlink selftest however (even
if it has to be trimmed/modified), because it does catch the issue fixed by Fernando
[ 09efbac953f6 ("netfilter: nfnetlink: reset nlh pointer during batch replay") ]:

ok 1 nfnetlink_batch.simple_batch
# RUN nfnetlink_batch.module_load ...
# nfnetlink.c:239:module_load:[seq=1759907514] ACK
# nfnetlink.c:239:module_load:[seq=1759907512] ACK
# nfnetlink.c:244:module_load:Out of order ack: seq 1759907512 after 1759907514 # nfnetlink.c:239:module_load:[seq=1759907513] ACK
# nfnetlink.c:239:module_load:[seq=1759907514] ACK
# nfnetlink.c:239:module_load:[seq=1759907515] ACK
# nfnetlink.c:254:module_load:Expected 0 (0) == out_of_order (1)
# module_load: Test terminated by assertion
# FAIL nfnetlink_batch.module_load

If the decision is that there should NOT be an ACK for the BATCH_END if there was an error, then the test only needs minor adjustment:

(quoted diff) +2 -2
-       // Expect 5 acks: batch_begin, table, invalid_table(error), chain, batch_end
-       validate_res(self->nl, seq - 5, 5, _metadata);
+       // Expect 4 acks: batch_begin, table, invalid_table(error), chain
+       validate_res(self->nl, seq - 4, 4, _metadata);
Quoted 5 lines

So, what is the 'more useful' behaviour? Choices are all equally bad:

1. If we want to always include it, it might not be there due to -ENOBUFS, which will always happen if the batch was large (triggers too many acks).

Yes.

Quoted 2 lines

2. If we only include it on success, it might not be there for the same reason, so absence doesn't imply failure.

Yes.

Quoted 2 lines

HOWEVER, if the batch was small enough then 2) gives slightly more useable feedback in the sense that the entire batch was processed.

Yes.

I think Nikolaos pointed out that _BEGIN+NLM_F_ACK could actually provide an indication, with the assumption that the netlink userspace queue is going to be empty because it will be the first acknowledgement...

Quoted 4 lines

So I am leaning towards not applying the nfnetlink patch but applying the (adjusted) test case.

Other takes?

Yes, I would start with this approach you propose, then keep discussing if it makes sense to keep extending bf2ac490d28c or leave it as is.

#23 lore raw
Quoted 3 lines · Pablo Neira Ayuso
Quoted 1 line · Florian Westphal

[ Cc Donald Hunter ]

Thanks for the cc. I'm having difficulty catching up with the thread of conversation, so forgive me if I miss something.

Quoted 2 lines

Yes, I am inclined not to add more features to bf2ac490d28c (and follow up fixes patches that came with it).

Is there a problem with bf2ac490d28c or just with things that have come after it?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

Hopefully not. In the success case, ask for an ack, get an ack. Without that guarantee to userspace, we'd need to extend the YNL spec to say which messages don't honour acks.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Let's just forget about this bf2ac490d28c for a moment, a quick summary:

#1 If you don't set NLM_F_ACK in your netlink messages in the batch
(this is what netfilter's userspace does): then errors result in acknowledgement. But ENOBUFS is still possible: this means your batch has resulted in too many acknowledment messages (errors) filling up the userspace netlink socket buffer.
#2 If you set NLM_F_ACK in your netlink messages in the batch:
You get one acknowledgement for each message in the batch, with a sufficiently large batch, this may overrun the userspace socket buffer (ENOBUFS), then maybe the kernel was successful to fully process the transaction but some of those acks get lost.

Are people reporting ENOBUFS because of ACKs in practice or is this theoretical?

Quoted 10 lines
Quoted 3 lines

Right, 1:1 relationship between messages and ACKs is only there for theoretical infinite receive buffer which makes this feature rather limited for batched case.

Exactly.

Quoted 4 lines
Quoted 3 lines

In this particular case, where batching several netlink messages in one single send(), userspace will not process the acknowledments messages in the userspace socket buffer until the batch is complete.

Quoted 6 lines
Quoted 3 lines

OK, from what I gather you'd like for "netfilter: nfnetlink: always ACK batch end if requested" to not be applied.

I think this at least needs more discussion, I think we are now understanding the implications of bf2ac490d28c.

Is the problem due to interplay between ACKs and ERRORs ?

I don't think anyone has successfully specified the expected behaviour when errors get reported, namely how many acks versus how many errors could be expected. I know that the YNL python library reports the first error received and terminates, regardless of how many ACKs were received.

My guess is that it should be possible to report as many errors as were received within the receive message length before exiting but I have not tried to implement that.

Quoted 16 lines
Quoted 5 lines

So, what is the 'more useful' behaviour? Choices are all equally bad:

1. If we want to always include it, it might not be there due to -ENOBUFS, which will always happen if the batch was large (triggers too many acks).

Yes.

Quoted 2 lines

2. If we only include it on success, it might not be there for the same reason, so absence doesn't imply failure.

Yes.

Quoted 2 lines

HOWEVER, if the batch was small enough then 2) gives slightly more useable feedback in the sense that the entire batch was processed.

Quoted 11 lines

Yes.

I think Nikolaos pointed out that _BEGIN+NLM_F_ACK could actually provide an indication, with the assumption that the netlink userspace queue is going to be empty because it will be the first acknowledgement...

Quoted 4 lines

So I am leaning towards not applying the nfnetlink patch but applying the (adjusted) test case.

Other takes?

I think we should try to specify the behaviour and see if it makes sense before layering more functionality onto what is there.

I can describe how the YNL python code sees the world, when it asks for ACKs:

1. An ack for BEGIN, cmd, cmd, ... , END in the success scenario.
2. An ack for BEGIN, cmd, cmd, ... up to the first ERROR in a failure scenario.

Quoted 3 lines

Yes, I would start with this approach you propose, then keep discussing if it makes sense to keep extending bf2ac490d28c or leave it as is.

maintainer
#24 lore raw
Quoted 8 lines · Donald Hunter

Thanks for the cc. I'm having difficulty catching up with the thread of conversation, so forgive me if I miss something.

Quoted 2 lines

Yes, I am inclined not to add more features to bf2ac490d28c (and follow up fixes patches that came with it).

Is there a problem with bf2ac490d28c or just with things that have come after it?

it broke golang library for nftables:
https://github.com/google/nftables/issues/329

Fix is:
https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git/commit/?id=09efbac953f6f076a07735f9ba885148d4796235

Current discussion centers more on what bf2ac490d28c wants, what it does, what netlink can provide in practice and what userspace would expect/need.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

Hopefully not. In the success case, ask for an ack, get an ack. Without that guarantee to userspace, we'd need to extend the YNL spec to say which messages don't honour acks.

Its not about the types, its more about available buffer space for acks.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

#2 If you set NLM_F_ACK in your netlink messages in the batch:
You get one acknowledgement for each message in the batch, with a sufficiently large batch, this may overrun the userspace socket buffer (ENOBUFS), then maybe the kernel was successful to fully process the transaction but some of those acks get lost.

Are people reporting ENOBUFS because of ACKs in practice or is this theoretical?

It happens in practice, e.g.:
https://lore.kernel.org/netfilter/[email protected]/

(every set element triggers another error message). Note, this is without NLM_F_ACK, just due to error messages triggered during batch processing.

Quoted 2 lines

I think we should try to specify the behaviour and see if it makes sense before layering more functionality onto what is there.

I think we can all agree on that :-)

Quoted 4 lines

I can describe how the YNL python code sees the world, when it asks for ACKs:

1. An ack for BEGIN, cmd, cmd, ... , END in the success scenario.
2. An ack for BEGIN, cmd, cmd, ... up to the first ERROR in a failure scenario.

Thanks for clarifying.

This should work, IFF the batch is small enough so receive buffer can hold all acks.

nftables doesn't bail out on first error (transaction will fail and no permanent changes are made, but it will move to next contained message.

#25 lore raw
In reply to Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso

Regarding bf2ac490d28c, I don't understand why one needs an ack for _BEGIN message. Maybe, an ack for END message might make sense when BATCH_DONE is reached so you get a confirmation that the batch has been fully processed, however...

_BEGIN might be excessive, but as you said, I do think _END could be useful in the way you describe.

My assumption is that the author of 1bf2ac490d28c aims to standardize the behavior while also allowing some flexibility in what flags are sent. If someone tried to use those flags in a creative way that deviates from what nft userspace expects, they might run into difficulties handling the responses correctly.

Quoted 3 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

That could very well be the case. As you said, you’re not always guaranteed to receive the same number of ACKs.

I’m aware of the ENOBUFS error. Personally, I see it as a “fatal” or “delivery” error, which should tell userspace that no more messages are coming. Similar to EPERM which I have a test case for. It might not be the best approach, since one could argue such errors might also occur for individual batch commands. Still, now that I think about it, not receiving a _BEGIN message could indicate that the error is indeed fatal.

Receiving an error about an invalid command isn’t necessarily a delivery failure (unlike ENOBUFS), and I’d still expect to get the entire message back, including the ACK. Otherwise, how would userspace know that it has read all messages and drained the buffer?

You could argue that userspace should bail on the first error it receives, but if I’m not mistaken, the kernel will still send an error for any subsequent invalid command, meaning the buffer isn’t being drained again.

Quoted 3 lines

Netlink is a unreliable transport protocol, there are mechanisms to make it "more reliable" but message loss (particularly in the kernel -> userspace direction) is still possible.

Is it unreliable mainly because of those corner cases, or are there other factors to consider as well?

#26 lore raw

Hi,

Quoted 6 lines · Nikolaos Gkarlis
Quoted 4 lines · Pablo Neira Ayuso

Regarding bf2ac490d28c, I don't understand why one needs an ack for _BEGIN message. Maybe, an ack for END message might make sense when BATCH_DONE is reached so you get a confirmation that the batch has been fully processed, however...

Quoted 8 lines

_BEGIN might be excessive, but as you said, I do think _END could be useful in the way you describe.

My assumption is that the author of 1bf2ac490d28c aims to standardize the behavior while also allowing some flexibility in what flags are sent. If someone tried to use those flags in a creative way that deviates from what nft userspace expects, they might run into difficulties handling the responses correctly.

I think the author of 1bf2ac490d28c is using it for a testing tool that sends very small batches (only few commands at a time). In that case, considering the default socket buffer size, the acknowledment is going to fit into the userspace netlink socket buffer.

Quoted 10 lines
Quoted 3 lines

I suspect the author of bf2ac490d28c is making wrong assumptions on the number of acknowledgements that are going to be received by userspace.

That could very well be the case. As you said, you’re not always guaranteed to receive the same number of ACKs.

I’m aware of the ENOBUFS error. Personally, I see it as a “fatal” or “delivery” error, which should tell userspace that no more messages are coming.

In netlink, ENOBUFS is not "fatal", it means messages got lost, but there are still messages in the netlink socket buffer to be processed, ie. the netlink messages before the overrun are still in place, but the messages that could not fit in into the socket buffer has been dropped.

nfnetlink handles a batch in two stages:

1) Process every netlink message in the batch, if either netlink message triggers an error or NLM_F_ACK is set on, then enqueue an error to the list.

2) If batch was successfully processed, iterate over the list of errors and create the netlink acknowledgement messages that is stores in the userspace netlink socket buffer.

Since this is a batch of netlink messages, acknowledgement either triggered by explicit NLM_F_ACK or by errors may overrun the netlink socket buffer.

Quoted 1 line

Similar to EPERM which I have a test case for.

EPERM is indeed fatal.

Quoted 4 lines

It might not be the best approach, since one could argue such errors might also occur for individual batch commands. Still, now that I think about it, not receiving a _BEGIN message could indicate that the error is indeed fatal.

I think I see your point.

Acknowledgement for _BEGIN will be likely in the netlink socket buffer, because it is the first message to be acknowledged, but _END is the last one to be processed, so it could get lost if many acknowledgements before have been queued to the userspace netlink socket buffer (leading to overrun).

It seems with 1bf2ac490d28c, an acknowledgement with _BEGIN can be an indication of successfully handling a batch in the way you describe.

Quoted 4 lines

Receiving an error about an invalid command isn’t necessarily a delivery failure (unlike ENOBUFS), and I’d still expect to get the entire message back, including the ACK. Otherwise, how would userspace know that it has read all messages and drained the buffer?

For this nfnetlink batching, use select() to poll for pending messages to process, and make no assumptions on how many messages you receive.

Quoted 4 lines

You could argue that userspace should bail on the first error it receives, but if I’m not mistaken, the kernel will still send an error for any subsequent invalid command, meaning the buffer isn’t being drained again.

If you open, send then batch, process response, then close. Lazy approach that consist of bailing out on the first error is OK. The close call on the socket implicitly cleans up the ignored pending error messages in the socket buffer.

But if you keep the socket open for several batches, with the approach you describe, then unprocessed netlink error messages will pile up on the socket buffer. If you do not do any sort of sequence tracking, then you application process old pending errors as new, libmnl handles this with EILSEQ.

All these netlink subtle details are not easy to follow :).

Quoted 6 lines
Quoted 3 lines

Netlink is a unreliable transport protocol, there are mechanisms to make it "more reliable" but message loss (particularly in the kernel -> userspace direction) is still possible.

Is it unreliable mainly because of those corner cases, or are there other factors to consider as well?

As for netlink batching, which is supported in other classic netlink subsystems, this acknowledgement overrun issue exists, I am referring to the scenario where you add several netlink messages to the buffer and send() them to the kernel.

As for nfnetlink, it is a bit special in that it has begin and end messages because this is needed for the transaction semantics (to implement a dryrun to test if incremental ruleset update is OK).

1bf2ac490d28c added the handling for NLM_F_ACK which I left it unspecified at the time.

Netlink can be also used for event delivery to userspace, ENOBUFS can also happen in that case, but that is a different scenario.

TBH, I am trying to remember the details, I don't talk about Netlink very often.

#27 lore raw

Hi again,

Thanks for the detailed explanation. For the record, I am not strongly for or against one or the other approach but I do think that my approach is something to consider if batch ACKs are to be supported in the long run.

Quoted 5 lines

In netlink, ENOBUFS is not "fatal", it means messages got lost, but there are still messages in the netlink socket buffer to be processed, ie. the netlink messages before the overrun are still in place, but the messages that could not fit in into the socket buffer has been dropped.

Yes perhaps in this instance "delivery" error would be a better description but I understand it's not an error in applying the batch.

Quoted 3 lines

For this nfnetlink batching, use select() to poll for pending messages to process, and make no assumptions on how many messages you receive.

That's an option indeed although the library that I use
https://github.com/google/nftables is unfortunately limited to blocking recvmsg calls and this is why it has to keep track of the sent acks and validate them after. I wonder if there are more cases like that ?

In fact, that's the reason why I stumbled upon the duplicate ACK bug and attempted using ACKs on batch messages to work around the issue.

Additionally, this library will dynamically adjust its buffer based on the expected response size.

I appreciate you probably don't want to make architectural decisions based on unofficial library limitations but I thought it might be a good thing to mention that.

To make the discussion clearer, I think we are referring to three different scenarios. I've outlined them below for clarity.

Scenario 1: Success

User sends:
Command1
Command2
Command3

Kernel replies:
Command1 - 0
Command2 - 0
Command3 - 0

Result: The batch is processed successfully.

Scenario 2: Failure

User sends:
Command1
Command2 (invalid)
Command3 (invalid)

Kernel replies:
Command1 - 0
Command2 - EINVAL
Command3 - EINVAL

Note: The reply for Command3 is still received even though Command2 failed.

Scenario 3: Fatal / Permission Error

User sends:
Command1
Command2
Command3

Kernel replies:
Command1 - EPERM

Result: Indicates a fatal error. Processing stops immediately.

Now let's add BATCH_BEGIN and BATCH_END into the mix.

Scenario 1: Success with Batch

User sends:
BATCH_BEGIN
Command1
Command2
Command3
BATCH_END

Kernel replies:
BATCH_BEGIN
Command1
Command2
Command3
BATCH_END

Result: Consistent behavior. Full batch completed successfully.

Scenario 2: Failure with Batch

User sends:
BATCH_BEGIN
Command1
Command2 (invalid)
Command3 (invalid)
BATCH_END

Kernel replies:
BATCH_BEGIN
Command1 - 0
Command2 - EINVAL
Command3 - EINVAL

Observation: This seems inconsistent with earlier behavior. I would expect a BATCH_END reply here as well to indicate that the batch processing is complete, even if errors occurred.

Scenario 3: Fatal Error with Batch

User sends:
BATCH_BEGIN
Command1
Command2
Command3
BATCH_END

Kernel replies:
Command1 - EPERM

Result: Matches the previous "fatal" case which I believe is what you're pointing out does not make sense and would require further changes which you don't think is a good idea to make.

I’m not trying to push this change, since I don’t rely on batch ACKs. Just want to make sure we’re on the same page.
Let me know how you’d like to proceed and whether I should update the test patch.

Thanks!