mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Florian Fainelli <florian.fainelli@broadcom.com>
To: netdev@vger.kernel.org
Cc: Florian Fainelli <florian.fainelli@broadcom.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>,
	David Ahern <dsahern@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Jiayuan Chen <jiayuan.chen@linux.dev>,
	Angus Peng <angus.peng@broadcom.com>,
	Maoyi Xie <maoyi.xie@ntu.edu.sg>,
	linux-kernel@vger.kernel.org (open list),
	linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST
	FRAMEWORK),
	bcm-kernel-feedback-list@broadcom.com
Subject: [PATCH net-next 2/2] selftests: net: add test for ipv6 strict_router_selection
Date: Mon, 30 Nov 2026 14:26:31 -0800	[thread overview]
Message-ID: <20261130222631.1786545-3-florian.fainelli@broadcom.com> (raw)
In-Reply-To: <20261130222631.1786545-1-florian.fainelli@broadcom.com>

Add a selftest for net.ipv6.route.strict_router_selection, which
enables strict RFC 4861 6.3.6 default router selection.

The test builds a single namespace with two same-metric default
routes, via fe80::1 on r1 and fe80::2 on r2, with r1 first in FIB leaf
order. Neighbour entries are set by hand, so no router needs to
answer anything. The selected router is read back with "ip route
get", both for locally generated traffic and, using "iif", for
forwarded traffic, which takes the ip6_pol_route_input() path where
the incoming ifindex is handed to route scoring as the oif.

The following is checked:
 - the sysctl defaults to 0 and only accepts 0 and 1
 - disabled: with forwarding on, neighbour state is ignored, FIB leaf
   order decides and there is no round-robin
 - enabled: a router with a valid neighbour entry beats one with a
   failed or missing entry, in either leaf order, with forwarding on
   and off, on the output and input paths
 - enabled: unreachable routers are selected in round-robin, and a
   reachable router stops the rotation
 - enabled: a router whose address resolution is in progress stays
   selected instead of being rotated away
 - enabled: RFC 4191 preference only breaks ties between equally
   reachable routers
 - enabled: RFC 4191 6.2.3 router probing is suppressed, with a
   positive control showing a neighbour solicitation is sent with the
   sysctl off

The preference and probing tests need CONFIG_IPV6_ROUTER_PREF and are
skipped at runtime when it is not enabled.

Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
 tools/testing/selftests/net/Makefile          |   1 +
 .../net/ipv6_strict_router_select.sh          | 507 ++++++++++++++++++
 2 files changed, 508 insertions(+)
 create mode 100755 tools/testing/selftests/net/ipv6_strict_router_select.sh

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index fb0a64662053..caa92d9ff0ae 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -59,6 +59,7 @@ TEST_PROGS := \
 	ipv6_flowlabel.sh \
 	ipv6_force_forwarding.sh \
 	ipv6_route_update_soft_lockup.sh \
+	ipv6_strict_router_select.sh \
 	ipvtap_test.sh \
 	l2_tos_ttl_inherit.sh \
 	l2tp.sh \
diff --git a/tools/testing/selftests/net/ipv6_strict_router_select.sh b/tools/testing/selftests/net/ipv6_strict_router_select.sh
new file mode 100755
index 000000000000..54500b25c967
--- /dev/null
+++ b/tools/testing/selftests/net/ipv6_strict_router_select.sh
@@ -0,0 +1,507 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test net.ipv6.route.strict_router_selection (strict RFC 4861 6.3.6 default
+# router selection).
+#
+# The device under test (DUT) has two default routers, fe80::1 behind r1 and
+# fe80::2 behind r2, installed as two same-metric default routes in the order
+# r1, r2 (i.e. r1 is first in FIB leaf order). Neighbour entries for the
+# routers are set up by hand, so no router has to answer anything, and the
+# selected router is read back with "ip route get", both for locally
+# generated traffic (output path) and for forwarded traffic (input path,
+# emulated with "iif").
+#
+#  +---------------------------+
+#  |            DUT            |
+#  | dum0                      |    PEER (no IPv6, only absorbs frames)
+#  | 2001:db8:100::1/64        |
+#  |                           |
+#  | r1 (gw fe80::1) ----------+--- r1p
+#  | r2 (gw fe80::2) ----------+--- r2p
+#  +---------------------------+
+#
+# Checked behaviour:
+#  - the sysctl defaults to 0 and only accepts 0 and 1
+#  - disabled: with forwarding on, neighbour state is ignored and FIB leaf
+#    order decides (upstream behaviour, unchanged)
+#  - enabled: a router with a valid neighbour entry beats a failed or missing
+#    one, whatever the leaf order and with forwarding on or off, on the
+#    output and on the input path
+#  - enabled: unreachable routers are selected in round-robin
+#  - enabled: a router whose resolution is in progress stays selected
+#  - enabled: RFC 4191 preference only breaks ties between equally reachable
+#    routers (needs CONFIG_IPV6_ROUTER_PREF)
+#  - enabled: RFC 4191 6.2.3 router probing is suppressed (needs
+#    CONFIG_IPV6_ROUTER_PREF)
+
+source lib.sh
+
+ALL_TESTS="
+	test_sysctl_default_range
+	test_disabled_forwarding_ignores_neigh
+	test_enabled_prefers_reachable
+	test_enabled_round_robin
+	test_enabled_incomplete_sticky
+	test_enabled_preference_tiebreak
+	test_enabled_no_probing
+"
+
+SYSCTL=net.ipv6.route.strict_router_selection
+
+GW1=fe80::1
+GW2=fe80::2
+# Used through indirect expansion in neigh_set()
+# shellcheck disable=SC2034
+LLADDR1=00:00:5e:00:53:01
+# shellcheck disable=SC2034
+LLADDR2=00:00:5e:00:53:02
+DST=2001:db8:ffff::1
+SRC_IN=2001:db8:100::2
+
+DUT=""
+PEER=""
+
+dut()
+{
+	ip netns exec "$DUT" "$@"
+}
+
+cleanup()
+{
+	# Kill any background ping before dropping the namespaces
+	[ -n "$ping_pid" ] && kill "$ping_pid" 2>/dev/null
+	cleanup_ns "$DUT" "$PEER"
+}
+
+setup()
+{
+	setup_ns DUT PEER || exit $ksft_skip
+
+	# No DAD, no RA/RS, no autoconf: the only NS sent on r1/r2 must be the
+	# ones we trigger ourselves. Shorten the router probe interval (in
+	# seconds, the smallest the sysctl allows) so that probing can be
+	# observed without waiting a minute. Set the defaults before the devices are
+	# created or moved so that they apply to them.
+	dut sysctl -qw net.ipv6.conf.default.dad_transmits=0 \
+		       net.ipv6.conf.default.accept_ra=0 \
+		       net.ipv6.conf.default.router_solicitations=0 \
+		       net.ipv6.conf.default.autoconf=0 \
+		       net.ipv6.conf.default.router_probe_interval=1 || exit $ksft_skip
+	ip netns exec "$PEER" sysctl -qw net.ipv6.conf.default.disable_ipv6=1 ||
+		exit $ksft_skip
+
+	ip -n "$DUT" link add dum0 type dummy || exit $ksft_skip
+	ip -n "$DUT" link add r1 type veth peer name r1p netns "$PEER" ||
+		exit $ksft_skip
+	ip -n "$DUT" link add r2 type veth peer name r2p netns "$PEER" ||
+		exit $ksft_skip
+
+	ip -n "$DUT" link set dum0 up
+	ip -n "$DUT" link set r1 up
+	ip -n "$DUT" link set r2 up
+	ip -n "$PEER" link set r1p up
+	ip -n "$PEER" link set r2p up
+
+	ip -n "$DUT" addr add 2001:db8:100::1/64 dev dum0 nodad
+
+	# Make sure link-local addresses are in place before routes use them
+	slowwait 5 dut_has_lladdr r1 || exit $ksft_skip
+	slowwait 5 dut_has_lladdr r2 || exit $ksft_skip
+}
+
+dut_has_lladdr()
+{
+	dut ip -6 addr show dev "$1" scope link | grep -q "inet6 fe80:.*scope link$" &&
+	! dut ip -6 addr show dev "$1" scope link | grep -q tentative
+}
+
+# Pristine state for every test: both routes (and their fib6_nh and the
+# round-robin pointer) recreated, no neighbour entries, strict mode off and
+# forwarding on. Extra arguments are appended to the r1 and r2 routes
+# respectively, e.g. "pref low".
+reset_state()
+{
+	local r1_opts=$1
+	local r2_opts=$2
+
+	dut sysctl -qw "$SYSCTL=0" net.ipv6.conf.all.forwarding=1
+	while dut ip -6 route del default 2>/dev/null; do :; done
+	dut ip neigh del "$GW1" dev r1 2>/dev/null
+	dut ip neigh del "$GW2" dev r2 2>/dev/null
+
+	# "append" is needed to get two same-metric default routes
+	# shellcheck disable=SC2086
+	dut ip -6 route append default via "$GW1" dev r1 $r1_opts || return 1
+	# shellcheck disable=SC2086
+	dut ip -6 route append default via "$GW2" dev r2 $r2_opts || return 1
+}
+
+# neigh_set <1|2> <permanent|failed|none>
+neigh_set()
+{
+	local gw=GW$1 dev=r$1 ll=LLADDR$1
+
+	if [ "$2" = none ]; then
+		dut ip -6 neigh del "${!gw}" dev "$dev" 2>/dev/null
+		return 0
+	fi
+	dut ip -6 neigh replace "${!gw}" dev "$dev" lladdr "${!ll}" nud "$2"
+}
+
+# selected_gw <out|in>: print the gateway chosen by a single route lookup
+selected_gw()
+{
+	local path=$1
+
+	if [ "$path" = in ]; then
+		dut ip -6 route get "$DST" from "$SRC_IN" iif dum0
+	else
+		dut ip -6 route get "$DST"
+	fi | grep -o ' via [^ ]*' | head -n1 | cut -d' ' -f3
+}
+
+# selected_gws <out|in> <count>: gateways chosen by <count> lookups
+selected_gws()
+{
+	local path=$1 count=$2 i
+
+	for ((i = 0; i < count; i++)); do
+		selected_gw "$path"
+	done | tr '\n' ' '
+}
+
+# check_selected <out|in> <expected gw> <description>
+check_selected()
+{
+	local path=$1 exp=$2 desc=$3
+	local got
+
+	got=$(selected_gw "$path")
+	if [ "$got" != "$exp" ]; then
+		RET=$ksft_fail
+		retmsg="$desc: $path path selected '$got', expected '$exp'"
+		return 1
+	fi
+}
+
+# Lookup paths exercised by the tests. Output is a locally generated flow, in
+# is a forwarded flow: the input path hands the incoming ifindex to the route
+# scoring in place of the output interface, so the device score never matches.
+PATHS="out in"
+
+strict_supported()
+{
+	dut test -f /proc/sys/net/ipv6/route/strict_router_selection
+}
+
+# Preference support: build a case where only RFC 4191 preference can tell the
+# two routes apart (both valid, strict mode off, forwarding off so that
+# reachability-based selection is used) and see whether it is honoured.
+pref_supported()
+{
+	local ret=1
+
+	reset_state "pref low" "pref high" || return 1
+	neigh_set 1 permanent
+	neigh_set 2 permanent
+	dut sysctl -qw net.ipv6.conf.all.forwarding=0
+	[ "$(selected_gw out)" = "$GW2" ] && ret=0
+	reset_state
+	return $ret
+}
+
+test_sysctl_default_range()
+{
+	local val
+
+	RET=0
+
+	val=$(dut sysctl -n "$SYSCTL")
+	[ "$val" = 0 ] || {
+		RET=$ksft_fail
+		retmsg="default is '$val', expected 0"
+	}
+
+	dut sysctl -qw "$SYSCTL=1" 2>/dev/null
+	val=$(dut sysctl -n "$SYSCTL")
+	[ "$val" = 1 ] || {
+		RET=$ksft_fail
+		retmsg="could not set $SYSCTL to 1"
+	}
+
+	if dut sysctl -qw "$SYSCTL=2" 2>/dev/null; then
+		RET=$ksft_fail
+		retmsg="accepted out of range value 2"
+	fi
+
+	dut sysctl -qw "$SYSCTL=0"
+	log_test "strict_router_selection: sysctl default and range"
+}
+
+# Strict mode off: with forwarding on, reachability is not looked at at all and
+# the first route in leaf order wins even if its router is unreachable.
+test_disabled_forwarding_ignores_neigh()
+{
+	local path
+
+	RET=0
+
+	reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+	neigh_set 1 failed
+	neigh_set 2 permanent
+	for path in $PATHS; do
+		check_selected "$path" "$GW1" "strict off, r1 failed, r2 valid"
+	done
+
+	# And no round-robin either with both routers unreachable
+	reset_state
+	local seq
+	for path in $PATHS; do
+		seq=$(selected_gws "$path" 4)
+		if [ "$seq" != "$GW1 $GW1 $GW1 $GW1 " ]; then
+			RET=$ksft_fail
+			retmsg="strict off, forwarding on: $path path rotated: $seq"
+		fi
+	done
+
+	log_test "strict_router_selection: disabled, forwarding on, neighbour state ignored"
+}
+
+test_enabled_prefers_reachable()
+{
+	local path fwd st1 st2 exp
+
+	RET=0
+
+	# <r1 state> <r2 state> <expected gw>
+	local cases=(
+		"failed    permanent $GW2"
+		"none      permanent $GW2"
+		"permanent failed    $GW1"
+		"permanent none      $GW1"
+		"permanent permanent $GW1"
+	)
+	local c
+
+	for fwd in 1 0; do
+		for c in "${cases[@]}"; do
+			read -r st1 st2 exp <<< "$c"
+
+			reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+			dut sysctl -qw "$SYSCTL=1"
+			# Forwarding is already on after reset_state
+			[ "$fwd" = 1 ] ||
+				dut sysctl -qw net.ipv6.conf.all.forwarding=0
+			neigh_set 1 "$st1"
+			neigh_set 2 "$st2"
+			for path in $PATHS; do
+				check_selected "$path" "$exp" \
+					"forwarding $fwd, r1 $st1, r2 $st2"
+			done
+		done
+	done
+
+	log_test "strict_router_selection: enabled, reachable router preferred"
+}
+
+test_enabled_round_robin()
+{
+	local path seq
+
+	RET=0
+
+	reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1"
+
+	# RFC 4861 6.3.6 rule 2: no router known reachable, rotate between them
+	for path in $PATHS; do
+		seq=$(selected_gws "$path" 4)
+		if [[ "$seq" != *"$GW1"* || "$seq" != *"$GW2"* ]]; then
+			RET=$ksft_fail
+			retmsg="$path path did not round-robin: $seq"
+		fi
+	done
+
+	# Same with FAILED entries
+	neigh_set 1 failed
+	neigh_set 2 failed
+	for path in $PATHS; do
+		seq=$(selected_gws "$path" 4)
+		if [[ "$seq" != *"$GW1"* || "$seq" != *"$GW2"* ]]; then
+			RET=$ksft_fail
+			retmsg="$path path did not round-robin with failed entries: $seq"
+		fi
+	done
+
+	# A known reachable router stops the rotation
+	neigh_set 2 permanent
+	for path in $PATHS; do
+		seq=$(selected_gws "$path" 4)
+		if [ "$seq" != "$GW2 $GW2 $GW2 $GW2 " ]; then
+			RET=$ksft_fail
+			retmsg="$path path kept rotating with a reachable router: $seq"
+		fi
+	done
+
+	log_test "strict_router_selection: enabled, round-robin of unreachable routers"
+}
+
+test_enabled_incomplete_sticky()
+{
+	local path seq
+
+	RET=0
+
+	reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1"
+
+	# Stretch address resolution so that the entry stays INCOMPLETE for the
+	# whole test
+	dut sysctl -qw net.ipv6.neigh.r2.retrans_time_ms=20000
+
+	# Start resolving r2's address: NS goes out, nobody answers
+	dut ping -6 -c 1 -W 1 -I r2 "$GW2" &>/dev/null &
+	ping_pid=$!
+
+	if ! busywait "$BUSYWAIT_TIMEOUT" dut_neigh_incomplete r2 "$GW2"; then
+		kill "$ping_pid" 2>/dev/null
+		ping_pid=""
+		dut sysctl -qw net.ipv6.neigh.r2.retrans_time_ms=1000
+		log_test_skip "strict_router_selection: enabled, router being resolved stays selected" \
+			"could not get an INCOMPLETE neighbour entry"
+		return
+	fi
+
+	# r1 (first in leaf order) has no neighbour entry, r2 is being resolved:
+	# r2 must be selected every time, not rotated away.
+	for path in $PATHS; do
+		seq=$(selected_gws "$path" 4)
+		if [ "$seq" != "$GW2 $GW2 $GW2 $GW2 " ]; then
+			RET=$ksft_fail
+			retmsg="$path path rotated away from INCOMPLETE router: $seq"
+		fi
+	done
+
+	kill "$ping_pid" 2>/dev/null
+	ping_pid=""
+	dut sysctl -qw net.ipv6.neigh.r2.retrans_time_ms=1000
+
+	log_test "strict_router_selection: enabled, router being resolved stays selected"
+}
+
+dut_neigh_incomplete()
+{
+	dut ip -6 neigh show dev "$1" "$2" | grep -q INCOMPLETE
+}
+
+test_enabled_preference_tiebreak()
+{
+	local path
+
+	RET=0
+
+	if ! pref_supported; then
+		log_test_skip "strict_router_selection: enabled, preference breaks ties only" \
+			"CONFIG_IPV6_ROUTER_PREF not enabled"
+		return
+	fi
+
+	dut sysctl -qw "$SYSCTL=1"
+
+	# Equally reachable: preference decides
+	reset_state "pref low" "pref high" || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1"
+	neigh_set 1 permanent
+	neigh_set 2 permanent
+	for path in $PATHS; do
+		check_selected "$path" "$GW2" "both valid, r2 pref high"
+	done
+
+	# Less preferred but reachable beats preferred but unreachable
+	reset_state "pref low" "pref high" || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1"
+	neigh_set 1 permanent
+	neigh_set 2 failed
+	for path in $PATHS; do
+		check_selected "$path" "$GW1" "r1 pref low valid, r2 pref high failed"
+	done
+
+	reset_state "pref high" "pref low" || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1"
+	neigh_set 1 none
+	neigh_set 2 permanent
+	for path in $PATHS; do
+		check_selected "$path" "$GW2" "r1 pref high missing, r2 pref low valid"
+	done
+
+	log_test "strict_router_selection: enabled, preference breaks ties only"
+}
+
+nsol_out()
+{
+	dut awk '/Icmp6OutNeighborSolicits/ { print $2 }' \
+		"/proc/net/dev_snmp6/$1"
+}
+
+test_enabled_no_probing()
+{
+	local before after
+
+	RET=0
+
+	# Positive control: without strict mode (and with reachability-based
+	# selection, i.e. forwarding off) a lookup probes the routers it has no
+	# neighbour entry for with an NS (RFC 4191 6.2.3)
+	reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw net.ipv6.conf.all.forwarding=0
+	sleep 1.2	# let the (shortened) probe interval elapse
+	before=$(nsol_out r1)
+	selected_gw out > /dev/null
+	if ! slowwait 3 ns_nsol_increased r1 "$before"; then
+		log_test_skip "strict_router_selection: enabled, router probing suppressed" \
+			"router probing not available (CONFIG_IPV6_ROUTER_PREF)"
+		return
+	fi
+
+	# Same with strict mode: fresh routes (probe rate limit state is per
+	# route), no NS may leave
+	reset_state || { RET=$ksft_fail; retmsg="route setup failed"; }
+	dut sysctl -qw "$SYSCTL=1" net.ipv6.conf.all.forwarding=0
+	sleep 1.2
+	before=$(nsol_out r1)
+	selected_gw out > /dev/null
+	selected_gw in > /dev/null
+	sleep 1
+	after=$(nsol_out r1)
+	if [ "$after" != "$before" ]; then
+		RET=$ksft_fail
+		retmsg="r1 sent $((after - before)) NS in strict mode"
+	fi
+
+	log_test "strict_router_selection: enabled, router probing suppressed"
+}
+
+ns_nsol_increased()
+{
+	[ "$(nsol_out "$1")" -gt "$2" ]
+}
+
+trap cleanup EXIT
+ping_pid=""
+
+require_command ip
+require_command ping
+require_command awk
+
+setup
+if ! strict_supported; then
+	echo "SKIP: $SYSCTL not available"
+	exit $ksft_skip
+fi
+
+for t in $ALL_TESTS; do
+	$t
+done
+
+exit "$EXIT_STATUS"
-- 
2.34.1


      parent reply	other threads:[~2026-11-30 22:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:26 [PATCH net-next 0/2] net: ipv6: add strict RFC 4861 6.3.6 default router selection Florian Fainelli
2026-11-30 22:26 ` [PATCH net-next 1/2] " Florian Fainelli
2026-11-30 22:26 ` Florian Fainelli [this message]

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=20261130222631.1786545-3-florian.fainelli@broadcom.com \
    --to=florian.fainelli@broadcom.com \
    --cc=angus.peng@broadcom.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=jiayuan.chen@linux.dev \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=maoyi.xie@ntu.edu.sg \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=yoshfuji@linux-ipv6.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

all inboxes | Powered by JetHome®