Skip to contentSkip to conversation

test updates

PATCH New Florian Westphal 1 participant 3 messages Nov 17 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover test updates

1) Extend nfulnl_test's print_pkt() to also output NFULA_HWTYPE, NFULA_HWLEN, NFULA_HWHEADER, and the bridge port. Update the existing test case to expect the new output.

2) Add an nftables-style shell test framework for nfulnl hook/family/hwheader logic, with per-test network-namespace isolation. Add six testcases asserting hwheader presence/absence and indev correctness across prerouting, output, forward, netdev-ingress, ARP, and bridge-forward hooks. Keep tests out of make check since they require root.

Passes with both nf:main and the nf ulog / syslog patches on the ML.

Florian Westphal (2):
utils: nfulnl_test: print NFULA_HWTYPE/HWLEN/HWHEADER
tests: add shell test framework covering nfulnl hook/family hwheader logic

 tests/shell/lib.sh                            | 140 ++++++++++++++++++
 tests/shell/run-tests.sh                      |  66 +++++++++
 .../testcases/nfulnl/0001-prerouting-loopback |  36 +++++
 .../testcases/nfulnl/0002-output-no-hwheader  |  43 ++++++
 .../testcases/nfulnl/0003-forward-hwheader    |  74 +++++++++
 .../nfulnl/0004-netdev-ingress-hwheader       |  29 ++++
 .../shell/testcases/nfulnl/0005-arp-hwheader  |  69 +++++++++
 .../nfulnl/0006-bridge-forward-indev          |  88 +++++++++++
 utils/nfulnl_test.c                           |  26 ++++
 utils/nfulnl_test.sh                          |   4 +-
 10 files changed, 573 insertions(+), 2 deletions(-)
 create mode 100644 tests/shell/lib.sh
 create mode 100755 tests/shell/run-tests.sh
 create mode 100755 tests/shell/testcases/nfulnl/0001-prerouting-loopback
 create mode 100755 tests/shell/testcases/nfulnl/0002-output-no-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0003-forward-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0004-netdev-ingress-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0005-arp-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0006-bridge-forward-indev
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/2 utils: nfulnl_test: print NFULA_HWTYPE/HWLEN/HWHEADER

print_pkt() only read NFULA_PACKET_HDR and NFULA_HWADDR.

Extend this to include NFULA_HWTYPE/NFULA_HWLEN/NFULA_HWHEADER and fix up the existing test case to expect the new output.

While at it, also print the bridge port, if any, so that future test cases can check for their presence.

---
 utils/nfulnl_test.c  | 26 ++++++++++++++++++++++++++
 utils/nfulnl_test.sh |  4 ++--
 2 files changed, 28 insertions(+), 2 deletions(-)
utils/nfulnl_test.c +26 -0
index 840c0bea3a86..b6427957e01b 100644
@@ -11,9 +11,11 @@ static int print_pkt(struct nflog_data *nfad)
 	uint32_t outdev, indev, hw_addrlen;
 	struct nfulnl_msg_packet_hdr *ph;
 	struct nfulnl_msg_packet_hw *hw;
+	uint16_t hwhdrlen;
 	int payload_len;
 	char *payload;
 	char *prefix;
+	char *hwhdr;
 	int i;
 
 	ph = nflog_get_msg_packet_hdr(nfad);
@@ -34,16 +36,40 @@ static int print_pkt(struct nflog_data *nfad)
 		printf("%02x ", hw->hw_addr[hw_addrlen - 1]);
 	}
 
+	/* NFULA_HWTYPE/NFULA_HWLEN/NFULA_HWHEADER: emitted by
+	 * nfulnl_put_hwheader() in the kernel, independently of
+	 * NFULA_HWADDR above. Only printed when the kernel decided the
+	 * hook is on indev's input path (see nfulnl_pre_egress()); a
+	 * missing hw_header= token is how a test asserts that exclusion.
+	 */
+	hwhdr = nflog_get_msg_packet_hwhdr(nfad);
+	if (hwhdr) {
+		hwhdrlen = nflog_get_msg_packet_hwhdrlen(nfad);
+
+		printf("hw_type=0x%04x hw_hdrlen=%u hw_header=",
+			nflog_get_hwtype(nfad), hwhdrlen);
+		for (i = 0; i < hwhdrlen; i++)
+			printf("%02x", (unsigned char)hwhdr[i]);
+		printf(" ");
+	}
+
 	printf("mark=%u ", nflog_get_nfmark(nfad));
 
 	indev = nflog_get_indev(nfad);
 	if (indev > 0)
 		printf("indev=%u ", indev);
 
+	indev = nflog_get_physindev(nfad);
+	if (indev > 0)
+		printf("physindev=%u ", indev);
+
 	outdev = nflog_get_outdev(nfad);
 	if (outdev > 0)
 		printf("outdev=%u ", outdev);
 
+	outdev = nflog_get_physoutdev(nfad);
+	if (outdev > 0)
+		printf("physoutdev=%u ", outdev);
 
 	prefix = nflog_get_prefix(nfad);
 	if (prefix)
utils/nfulnl_test.sh +2 -2
index b23ccab04bae..d97f82a366ab 100755
@@ -49,8 +49,8 @@ ping6 -q -c "$PING_COUNT" ::1 > /dev/null
 wait
 
 set +e
-cnt4=$(grep -c '^hw_protocol=0x0800 hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 mark=0 indev=1 prefix="test icmp " payload_len=84' "$out")
-cnt6=$(grep -c '^hw_protocol=0x86dd hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 mark=0 indev=1 prefix="test icmp6 " payload_len=104' "$out")
+cnt4=$(grep -c '^hw_protocol=0x0800 hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 hw_type=0x0304 hw_hdrlen=14 hw_header=0000000000000000000000000800 mark=0 indev=1 prefix="test icmp " payload_len=84' "$out")
+cnt6=$(grep -c '^hw_protocol=0x86dd hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 hw_type=0x0304 hw_hdrlen=14 hw_header=00000000000000000000000086dd mark=0 indev=1 prefix="test icmp6 " payload_len=104' "$out")
 
 [ "$cnt4" -ne 4 ] && die "Expected 2 counts each for ipv4 and ipv6, got $cnt4, $cnt6"
 [ "$cnt6" -ne 4 ] && die "Expected 2 counts each for ipv4 and ipv6, got $cnt4, $cnt6"
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/2 tests: add shell test framework covering nfulnl hook/family hwheader logic

In reply to Florian Westphal

utils/nfulnl_test.sh was a single monolithic script covering one hook (prerouting) on loopback only.

Add a small nftables-style shell test instead.

tests/shell/run-tests.sh discovers and runs executable scripts under tests/shell/testcases/, each a self-contained test exiting 0 (pass), 77 (skip) or non-zero (fail).

tests/shell/lib.sh holds the common nfulnl_test-capture/cleanup helpers. Every test runs inside its own unshare -n network namespace.

Testcases added:
- 0001-prerouting-loopback: baseline, port of the old script, now also asserting the HWTYPE/HWLEN/HWHEADER triplet
- 0002-output-no-hwheader: hwheader must be absent past LOCAL_OUT
- 0003-forward-hwheader: hwheader present at NF_INET_FORWARD
- 0004-netdev-ingress-hwheader: NFPROTO_NETDEV ingress hook
- 0005-arp-hwheader: NFPROTO_ARP, NF_ARP_IN, NF_ARP_OUT
- 0006-bridge-forward-indev: the actual regression test - bridges two netns together and asserts indev/hw_addr in the log follow the real ingress port per direction at NF_BR_FORWARD, not whatever skb->dev was last repointed to

Not wired into "make check": testcases need root (netns, nft).

---
 tests/shell/lib.sh                            | 140 ++++++++++++++++++
 tests/shell/run-tests.sh                      |  66 +++++++++
 .../testcases/nfulnl/0001-prerouting-loopback |  36 +++++
 .../testcases/nfulnl/0002-output-no-hwheader  |  43 ++++++
 .../testcases/nfulnl/0003-forward-hwheader    |  74 +++++++++
 .../nfulnl/0004-netdev-ingress-hwheader       |  29 ++++
 .../shell/testcases/nfulnl/0005-arp-hwheader  |  69 +++++++++
 .../nfulnl/0006-bridge-forward-indev          |  88 +++++++++++
 8 files changed, 545 insertions(+)
 create mode 100644 tests/shell/lib.sh
 create mode 100755 tests/shell/run-tests.sh
 create mode 100755 tests/shell/testcases/nfulnl/0001-prerouting-loopback
 create mode 100755 tests/shell/testcases/nfulnl/0002-output-no-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0003-forward-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0004-netdev-ingress-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0005-arp-hwheader
 create mode 100755 tests/shell/testcases/nfulnl/0006-bridge-forward-indev
tests/shell/lib.sh new file +140 -0
new file mode 100644
index 000000000000..28995c24f421
@@ -0,0 +1,140 @@
+# Common helpers sourced by testcases/nfulnl/*.
+#
+# A testcase is expected to:
+#   source "$(dirname "$0")/../../lib.sh"
+#   trap nfulnl_cleanup EXIT	# extend, don't replace, if the testcase has
+#				# its own cleanup (netns teardown etc.)
+#   nfulnl_capture [netns]
+#   ... generate traffic, use nft log group 0 ...
+#   nfulnl_wait
+# and then grep/assert on $NFULNL_OUT.
+#
+# nfulnl_test(1) always binds group 0 (and 100), so every nft ruleset in
+# these testcases must use "log group 0".
+
+SKIP_RC=77
+NFULNL_TEST_NAME="${0##*/}"
+
+# Make sure a failure inside a helper function (nfulnl_capture, nfulnl_wait,
+# ...) still runs the ERR trap below, and print a command trace when
+# NFULNL_TEST_VERBOSE is set, so a standalone run gives more to go on than a
+# bare exit code.
+set -E
+[ -n "${NFULNL_TEST_VERBOSE:-}" ] && set -x
+
+# Locate the nfulnl_test helper binary built under utils/.
+: "${NFULNL_TEST_BIN:=}"
+if [ -z "$NFULNL_TEST_BIN" ]; then
+	for candidate in \
+		"$(dirname "$0")/../../../../utils/nfulnl_test" \
+		"$(pwd)/utils/nfulnl_test"; do
+		if [ -x "$candidate" ]; then
+			NFULNL_TEST_BIN="$candidate"
+			break
+		fi
+	done
+fi
+
+# nfulnl_dump_out: print whatever nfulnl_test captured so far, for
+# post-mortem debugging. Safe to call even if capture never started.
+nfulnl_dump_out() {
+	if [ -n "$NFULNL_OUT" ] && [ -s "$NFULNL_OUT" ]; then
+		echo "---- $NFULNL_TEST_NAME: nfulnl_test output ($NFULNL_OUT) ----" >&2
+		cat "$NFULNL_OUT" >&2
+		echo "---- end nfulnl_test output ----" >&2
+	fi
+}
+
+die() {
+	echo "FAIL: $NFULNL_TEST_NAME: $@" >&2
+	nfulnl_dump_out
+	exit 1
+}
+
+skip() {
+	echo "SKIP: $NFULNL_TEST_NAME: $@" >&2
+	exit $SKIP_RC
+}
+
+# Reports the failing command/line whenever `set -e` aborts a testcase, eg.
+# a setup command (ip, nft, ping, ...) failing - those currently exit with
+# no message at all since their own stdout is usually redirected away.
+# Deliberately a no-op once a testcase does `set +e` for its assertion
+# section: bash still runs the ERR trap there for things like
+# `cnt=$(grep -c ...)` even though -e is off, and those failures are
+# expected/handled by the `die` calls that follow, not errors in themselves.
+nfulnl_on_err() {
+	local rc=$? line="$1" cmd="$2"
+	case $- in
+	*e*) ;;
+	*) return 0 ;;
+	esac
+	# nfulnl_wait already reports the process it ran and its output before
+	# propagating failure via a plain "return $rc"; skip the generic message
+	# for that case, it would only point at the "return" statement itself,
+	# not anything a test author wrote.
+	case "$cmd" in
+	"return "*) return 0 ;;
+	esac
+	echo "FAIL: $NFULNL_TEST_NAME: line $line: '$cmd' exited with status $rc" >&2
+	nfulnl_dump_out
+}
+trap 'nfulnl_on_err "$LINENO" "$BASH_COMMAND"' ERR
+
+[ -x "$NFULNL_TEST_BIN" ] || skip "nfulnl_test binary not found/built (looked for \$NFULNL_TEST_BIN, utils/nfulnl_test)"
+command -v nft >/dev/null || skip "nft not found"
+
+NFULNL_PID=
+NFULNL_OUT=
+
+# nfulnl_capture [netns]
+#
+# Starts nfulnl_test in the background (optionally inside netns <netns>),
+# redirecting its output to a tempfile ($NFULNL_OUT). The caller is
+# responsible for calling `nfulnl_wait` once traffic generation is done.
+nfulnl_capture() {
+	local ns="$1"
+	NFULNL_OUT=$(mktemp)
+
+	if [ -n "$ns" ]; then
+		ip netns exec "$ns" timeout 5 "$NFULNL_TEST_BIN" > "$NFULNL_OUT" &
+	else
+		timeout 5 "$NFULNL_TEST_BIN" > "$NFULNL_OUT" &
+	fi
+	NFULNL_PID=$!
+
+	# give nflog_bind_group()/nft a moment to come up before traffic flows
+	sleep 1
+}
+
+# Waits for the backgrounded nfulnl_test (started by nfulnl_capture) to
+# finish. Under normal operation that's `timeout 5` killing it once the
+# capture window elapses, which `wait` reports as rc 124 - that is success,
+# not a failure, so it must not be returned as-is: under `set -e` it is the
+# right-most (ie. checked) element of a `&&` list, so returning it verbatim
+# would silently abort the testcase right here, before any of its assertions
+# ever run.
+nfulnl_wait() {
+	local rc=0
+	[ -n "$NFULNL_PID" ] || return 0
+	wait "$NFULNL_PID" || rc=$?
+	if [ "$rc" -ne 0 ] && [ "$rc" -ne 124 ]; then
+		echo "FAIL: $NFULNL_TEST_NAME: nfulnl_test exited with status $rc (expected 124 from the timeout, or 0)" >&2
+		nfulnl_dump_out
+		return "$rc"
+	fi
+	return 0
+}
+
+# Runs as an EXIT trap, so under `set -e` any command here that fails (eg.
+# `kill` on a PID that has already exited on its own - the common case,
+# since nfulnl_wait() only returns after the capture has ended) would abort
+# the rest of the trap and silently replace the testcase's real exit status
+# with the failed command's, turning a PASS into an unexplained FAIL. `set
+# +e` so a failure here can't do that; stdout/stderr are left alone so a
+# genuine problem (eg. permission denied) is still visible.
+nfulnl_cleanup() {
+	set +e
+	[ -n "$NFULNL_PID" ] && kill "$NFULNL_PID"
+	[ -n "$NFULNL_OUT" ] && rm -f "$NFULNL_OUT"
+}
tests/shell/run-tests.sh new file +66 -0

66 lines not on this page · raw message

tests/shell/testcases/nfulnl/0001-prerouting-loopback new file +36 -0
new file mode 100755
index 000000000000..abdd70656407
@@ -0,0 +1,36 @@
+#!/bin/bash
+#
+# Baseline: log ICMP/ICMPv6 in the prerouting chain on loopback.
+# NF_INET_PRE_ROUTING is the simplest nfulnl_pre_egress() case (hooknum <=
+# NF_INET_FORWARD for pf IPv4/IPv6) and indev == skb->dev always holds here,
+# so NFULA_HWADDR and the NFULA_HWTYPE/HWLEN/HWHEADER triplet must both be
+# present.
+
+source "$(dirname "$0")/../../lib.sh"
+trap nfulnl_cleanup EXIT
+set -e
+
+nfulnl_capture
+ip link set lo up
+
+nft -f - <<EOF
+table inet filter {
+	chain icmp-log {
+		type filter hook prerouting priority 0; policy accept;
+		icmp type echo-request counter log group 0 prefix "test icmp "
+		icmpv6 type echo-request counter log group 0 prefix "test icmp6 "
+	}
+}
+EOF
+
+ping -q -c 2 127.0.0.1
+ping6 -q -c 2 ::1
+
+nfulnl_wait
+
+set +e
+cnt4=$(grep -c '^hw_protocol=0x0800 hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 hw_type=0x0304 hw_hdrlen=14 hw_header=[0-9a-f]\{28\} mark=0 indev=1 prefix="test icmp " payload_len=84' "$NFULNL_OUT")
+cnt6=$(grep -c '^hw_protocol=0x86dd hook=0 hw_addrlen=6 hw_addr=00:00:00:00:00:00 hw_type=0x0304 hw_hdrlen=14 hw_header=[0-9a-f]\{28\} mark=0 indev=1 prefix="test icmp6 " payload_len=104' "$NFULNL_OUT")
+
+[ "$cnt4" -eq 2 ] || die "expected 4 ipv4 log lines with hwheader, got $cnt4"
+[ "$cnt6" -eq 2 ] || die "expected 4 ipv6 log lines with hwheader, got $cnt6"
tests/shell/testcases/nfulnl/0002-output-no-hwheader new file +43 -0

43 lines not on this page · raw message

tests/shell/testcases/nfulnl/0003-forward-hwheader new file +74 -0

74 lines not on this page · raw message

tests/shell/testcases/nfulnl/0004-netdev-ingress-hwheader new file +29 -0
new file mode 100755
index 000000000000..ac127e004a9a
@@ -0,0 +1,29 @@
+#!/bin/bash
+#
+# Regression test for nfulnl_pre_egress()'s NFPROTO_NETDEV branch: logging
+# from a `netdev`-family ingress hook (NF_NETDEV_INGRESS == 0, no relation
+# to NF_INET_FORWARD's numbering) must still get the hwheader triplet.
+
+source "$(dirname "$0")/../../lib.sh"
+trap nfulnl_cleanup EXIT
+set -e
+
+ip link set lo up
+
+nft -f - <<EOF
+table netdev filter {
+	chain ingress-log {
+		type filter hook ingress device lo priority 0; policy accept;
+		meta l4proto icmp counter log group 0 prefix "test netdev-ingress "
+	}
+}
+EOF
+
+nfulnl_capture
+
+ping -q -c 2 127.0.0.1
+nfulnl_wait
+
+set +e
+cnt=$(grep 'prefix="test netdev-ingress "' "$NFULNL_OUT" | grep -c 'hw_header=')
+[ "$cnt" -ge 1 ] || die "expected hw_header on netdev ingress hook, got $cnt matching lines"
tests/shell/testcases/nfulnl/0005-arp-hwheader new file +69 -0

69 lines not on this page · raw message

tests/shell/testcases/nfulnl/0006-bridge-forward-indev new file +88 -0

88 lines not on this page · raw message

Signature · 1 line
2.56.1