netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: netfilter-devel@vger.kernel.org
Cc: Florian Westphal <fw@strlen.de>
Subject: [iptables PATCH v2 3/3] iptables-save: Avoid /etc/protocols lookups
Date: Wed, 10 Jan 2024 23:41:36 +0100	[thread overview]
Message-ID: <20240110224136.11211-4-phil@nwl.cc> (raw)
In-Reply-To: <20240110224136.11211-1-phil@nwl.cc>

Instrument proto_to_name() to abort if given protocol number is not
among the well-known ones in xtables_chain_protos. Along with
xtables_parse_protocol() preferring said array for lookups as well, this
ensures reliable dump'n'restore regardless of /etc/protocols contents.

Another benefit is rule dump performance. A simple test-case dumping
100k rules matching on dccp protocol shows an 8s delta (2s vs. 10s for
legacy, 0.5s vs. 8s for nft) with this patch applied. For reference:

| for variant in nft legacy; do
| 	(
| 		echo "*filter"
| 		for ((i = 0; i < 100000; i++)); do
| 		        echo "-A FORWARD -p dccp -j ACCEPT"
| 		done
| 		echo "COMMIT"
| 	) | iptables-${variant}-restore
| 	time iptables-${variant}-save | wc -l
| 	iptables-${variant} -F
| done

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 iptables/tests/shell/testcases/ipt-save/0001load-dumps_0 | 1 +
 iptables/xshared.c                                       | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/iptables/tests/shell/testcases/ipt-save/0001load-dumps_0 b/iptables/tests/shell/testcases/ipt-save/0001load-dumps_0
index 4e0be51c071c8..48f5f7b423379 100755
--- a/iptables/tests/shell/testcases/ipt-save/0001load-dumps_0
+++ b/iptables/tests/shell/testcases/ipt-save/0001load-dumps_0
@@ -39,6 +39,7 @@ do_simple()
 
 	$XT_MULTI ${iptables}-restore < "$dumpfile"
 	$XT_MULTI ${iptables}-save | grep -v "^#" > "$tmpfile"
+	sed -i -e 's/-p 47 /-p gre /' "$tmpfile"
 	do_diff $dumpfile "$tmpfile"
 	if [ $? -ne 0 ]; then
 		# cp "$tmpfile" "$dumpfile.got"
diff --git a/iptables/xshared.c b/iptables/xshared.c
index 68f70277388a6..51283d7afad1e 100644
--- a/iptables/xshared.c
+++ b/iptables/xshared.c
@@ -1097,7 +1097,7 @@ void save_rule_details(const char *iniface, const char *outiface,
 	}
 
 	if (proto > 0) {
-		const char *pname = proto_to_name(proto, 0);
+		const char *pname = proto_to_name(proto, true);
 
 		if (invflags & XT_INV_PROTO)
 			printf(" !");
-- 
2.43.0


  parent reply	other threads:[~2024-01-10 22:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10 22:41 [iptables PATCH v2 0/3] iptables-save: Avoid /etc/protocols lookups Phil Sutter
2024-01-10 22:41 ` [iptables PATCH v2 1/3] Revert "xshared: Print protocol numbers if --numeric was given" Phil Sutter
2024-01-10 22:41 ` [iptables PATCH v2 2/3] libxtables: Add dccp and ipcomp to xtables_chain_protos Phil Sutter
2024-01-10 22:41 ` Phil Sutter [this message]
2024-02-06 23:25 ` [iptables PATCH v2 0/3] iptables-save: Avoid /etc/protocols lookups Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240110224136.11211-4-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).