[PATCH net-next v4 2/2] selftests: net: add tcp_bound_dev_listener test
From: Kalash Nainwal via B4 Relay
Date: Sat Dec 05 2026 - 00:06:33 EST
From: Kalash Nainwal <kalash@xxxxxxxxxx>
Check that a TCP listener bound to a device takes the connections that
arrive on it when an unbound listener shares the port with
SO_REUSEPORT, in both listener creation orders, for IPv4 and IPv6:
- in a VRF (tcp_l3mdev_accept=1), a listener bound to the VRF or to the
VRF slave device wins, and an unbound listener alone still accepts;
- in the default VRF, a listener bound to the ingress device wins, and
one bound to the VRF never does;
- in a VRF, a listener bound to the VRF with SO_BINDTODEVICE only after
listen() also wins.
Add a --bind-after-listen option to nettest for the last case.
Signed-off-by: Kalash Nainwal <kalash@xxxxxxxxxx>
---
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/nettest.c | 17 +-
.../selftests/net/tcp_bound_dev_listener.sh | 308 +++++++++++++++++++++
3 files changed, 324 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 4409ecaf0b5f..8116fb9024f3 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -109,6 +109,7 @@ TEST_PROGS := \
srv6_hl2encap_red_l2vpn_test.sh \
srv6_iptunnel_cache.sh \
stress_reuseport_listen.sh \
+ tcp_bound_dev_listener.sh \
tcp_ecmp_failover.sh \
tcp_fastopen_backup_key.sh \
test_bridge_backup_port.sh \
diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
index 1f5227f3d64d..a3145d067999 100644
--- a/tools/testing/selftests/net/nettest.c
+++ b/tools/testing/selftests/net/nettest.c
@@ -78,7 +78,8 @@ struct sock_args {
has_expected_raddr:1,
bind_test_only:1,
client_dontroute:1,
- server_dontroute:1;
+ server_dontroute:1,
+ bind_after_listen:1;
unsigned short port;
@@ -1507,7 +1508,8 @@ static int lsock_init(struct sock_args *args)
if (args->server_dontroute && set_dontroute(sd) != 0)
goto err;
- if (args->dev && bind_to_device(sd, args->dev) != 0)
+ if (args->dev && !args->bind_after_listen &&
+ bind_to_device(sd, args->dev) != 0)
goto err;
else if (args->use_setsockopt &&
set_unicast_if(sd, args->ifindex, args->version))
@@ -1527,6 +1529,10 @@ static int lsock_init(struct sock_args *args)
goto err;
}
+ if (args->dev && args->bind_after_listen &&
+ bind_to_device(sd, args->dev) != 0)
+ goto err;
+
flags = fcntl(sd, F_GETFL);
if ((flags < 0) || (fcntl(sd, F_SETFL, flags|O_NONBLOCK) < 0)) {
log_err_errno("Failed to set non-blocking option");
@@ -1936,12 +1942,14 @@ static int ipc_parent(int cpid, int fd, struct sock_args *args)
#define OPT_NO_BIND_KEY_IFINDEX 1002
#define OPT_CLIENT_DONTROUTE 1003
#define OPT_SERVER_DONTROUTE 1004
+#define OPT_BIND_AFTER_LISTEN 1005
static struct option long_opts[] = {
{"force-bind-key-ifindex", 0, 0, OPT_FORCE_BIND_KEY_IFINDEX},
{"no-bind-key-ifindex", 0, 0, OPT_NO_BIND_KEY_IFINDEX},
{"client-dontroute", 0, 0, OPT_CLIENT_DONTROUTE},
{"server-dontroute", 0, 0, OPT_SERVER_DONTROUTE},
+ {"bind-after-listen", 0, 0, OPT_BIND_AFTER_LISTEN},
{0, 0, 0, 0}
};
@@ -1993,6 +2001,8 @@ static void print_usage(char *prog)
" --server-dontroute: don't use gateways for server socket: send\n"
" packets only if destination is on link (see\n"
" SO_DONTROUTE in socket(7))\n"
+ " --bind-after-listen: bind the server socket to the -I device\n"
+ " after listen() instead of before bind()\n"
"\n"
" -g grp multicast group (e.g., 239.1.1.1)\n"
" -i interactive mode (default is echo and terminate)\n"
@@ -2121,6 +2131,9 @@ int main(int argc, char *argv[])
case OPT_SERVER_DONTROUTE:
args.server_dontroute = 1;
break;
+ case OPT_BIND_AFTER_LISTEN:
+ args.bind_after_listen = 1;
+ break;
case 'X':
args.client_pw = optarg;
break;
diff --git a/tools/testing/selftests/net/tcp_bound_dev_listener.sh b/tools/testing/selftests/net/tcp_bound_dev_listener.sh
new file mode 100755
index 000000000000..3d155109e3a9
--- /dev/null
+++ b/tools/testing/selftests/net/tcp_bound_dev_listener.sh
@@ -0,0 +1,308 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test that a TCP listener bound to a device takes the connections that
+# arrive on that device, also when an unbound listener uses SO_REUSEPORT
+# on the same port, whichever of the two was created first, and also when
+# the listener is bound to the device only after listen().
+#
+# Connections that arrive in a VRF (with tcp_l3mdev_accept=1) must go to
+# a listener bound to the VRF or to the VRF slave device. Connections in
+# the default VRF must go to a listener bound to the ingress device, and
+# never to one bound to the VRF.
+#
+# ns_c (client in VRF) ns_s (server) ns_d (client, default VRF)
+# veth0 ------------- veth_vrf [vrf red]
+# veth_def ----------------- veth0
+
+# ns_s, ns_c and ns_d are assigned by setup_ns, and the test functions
+# are called through ALL_TESTS.
+# shellcheck disable=SC2154,SC2329
+
+source lib.sh
+
+PORT=12345
+VRF=red
+VRF_TABLE=1000
+
+VRF_SRV4=172.16.1.1
+VRF_CLI4=172.16.1.2
+DEF_SRV4=172.16.2.1
+DEF_CLI4=172.16.2.2
+VRF_SRV6=2001:db8:1::1
+VRF_CLI6=2001:db8:1::2
+DEF_SRV6=2001:db8:2::1
+DEF_CLI6=2001:db8:2::2
+
+# Servers that must not get a connection are stopped by the test, so this
+# only needs to be longer than any wait before the client connects.
+SRV_TIMEOUT=20
+
+ALL_TESTS="
+ vrf_unbound_only
+ vrf_bound_listener
+ vrf_slave_bound_listener
+ default_dev_bound_listener
+ default_vrf_bound_listener
+ vrf_bound_after_listen
+"
+
+cleanup()
+{
+ cleanup_all_ns
+}
+
+setup_fail()
+{
+ echo "FAIL: setup"
+ exit "$ksft_fail"
+}
+
+setup()
+{
+ setup_ns ns_s ns_c ns_d || exit "$ksft_skip"
+
+ if ! ip -n "$ns_s" link add "$VRF" type vrf table "$VRF_TABLE"; then
+ echo "SKIP: VRF not supported"
+ exit "$ksft_skip"
+ fi
+
+ ip -n "$ns_s" -batch - <<-EOF || setup_fail
+ link set $VRF up
+ link add veth_vrf type veth peer name veth0 netns $ns_c
+ link set veth_vrf master $VRF
+ link set veth_vrf up
+ addr add $VRF_SRV4/24 dev veth_vrf
+ addr add $VRF_SRV6/64 dev veth_vrf nodad
+ link add veth_def type veth peer name veth0 netns $ns_d
+ link set veth_def up
+ addr add $DEF_SRV4/24 dev veth_def
+ addr add $DEF_SRV6/64 dev veth_def nodad
+ EOF
+
+ ip -n "$ns_c" -batch - <<-EOF || setup_fail
+ link set veth0 up
+ addr add $VRF_CLI4/24 dev veth0
+ addr add $VRF_CLI6/64 dev veth0 nodad
+ EOF
+
+ ip -n "$ns_d" -batch - <<-EOF || setup_fail
+ link set veth0 up
+ addr add $DEF_CLI4/24 dev veth0
+ addr add $DEF_CLI6/64 dev veth0 nodad
+ EOF
+
+ ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_accept=1 ||
+ setup_fail
+}
+
+# Start a server in the background. nettest sets SO_REUSEPORT on its
+# listeners, so an unbound and a device-bound listener can share the port.
+# $1: IP version, $2: device to bind to, or "unbound", $3...: more
+# nettest options
+start_server()
+{
+ local args=()
+
+ [ "$1" = 6 ] && args+=(-6)
+ [ "$2" != unbound ] && args+=(-I "$2")
+ args+=("${@:3}")
+ ip netns exec "$ns_s" nettest "${args[@]}" -s -p "$PORT" \
+ -t "$SRV_TIMEOUT" -q &
+}
+
+# Stop a server, ignoring its exit code.
+stop_server()
+{
+ kill "$1" 2>/dev/null
+ wait "$1" 2>/dev/null
+}
+
+# Stop a server that must not have received a connection. It must still
+# be running: had it accepted the connection or timed out, it would have
+# exited.
+# $1: pid, $2: name for the error message
+stop_idle_server()
+{
+ kill -0 "$1" 2>/dev/null
+ check_err $? "$2 exited, expected it to still be waiting"
+ stop_server "$1"
+}
+
+# $1: client netns, $2: IP version, $3: server address
+connect()
+{
+ local args=()
+
+ [ "$2" = 6 ] && args+=(-6)
+ ip netns exec "$1" nettest "${args[@]}" -r "$3" -p "$PORT" -t 2 -q
+}
+
+listeners_are()
+{
+ local n
+
+ n=$(ip netns exec "$ns_s" ss -Hltn "sport = :$PORT" | wc -l)
+ [ "$n" -eq "$1" ]
+}
+
+# $1: number of listeners to wait for on $PORT
+wait_listeners()
+{
+ slowwait 2 listeners_are "$1"
+}
+
+listener_bound_to()
+{
+ ip netns exec "$ns_s" ss -Hltn "sport = :$PORT" | grep -q "%$1:"
+}
+
+# $1: "vrf" or "def", $2: IP version
+server_addr()
+{
+ case "$1$2" in
+ vrf4) echo "$VRF_SRV4" ;;
+ vrf6) echo "$VRF_SRV6" ;;
+ def4) echo "$DEF_SRV4" ;;
+ def6) echo "$DEF_SRV6" ;;
+ esac
+}
+
+vrf_unbound_only()
+{
+ local ver pid
+
+ for ver in 4 6; do
+ RET=0
+ start_server "$ver" unbound
+ pid=$!
+ if wait_listeners 1; then
+ connect "$ns_c" "$ver" "$(server_addr vrf "$ver")"
+ check_err $? "VRF connection failed"
+ wait "$pid"
+ check_err $? "unbound listener did not get the conn"
+ else
+ check_err 1 "listener did not start"
+ stop_server "$pid"
+ fi
+ log_test "IPv$ver: VRF conn, unbound only, unbound wins"
+ done
+}
+
+# An unbound listener and a listener bound to a device on the port, in
+# both creation orders. One connection is made, and exactly the expected
+# listener must get it.
+# $1: client netns, $2: "vrf" or "def" (server address to connect to),
+# $3: device to bind the second listener to, $4: expected winner, $3 or
+# "unbound"
+two_listeners()
+{
+ local cli=$1 net=$2 dev=$3 winner=$4
+ local ver order first second pid_first pid_second
+ local pid_win pid_lose loser label="VRF conn"
+
+ [ "$net" = def ] && label="default conn"
+ for ver in 4 6; do
+ for order in "unbound $dev" "$dev unbound"; do
+ read -r first second <<< "$order"
+ RET=0
+
+ pid_second=
+ start_server "$ver" "$first"
+ pid_first=$!
+ if wait_listeners 1; then
+ start_server "$ver" "$second"
+ pid_second=$!
+ fi
+ if [ -z "$pid_second" ] || ! wait_listeners 2; then
+ check_err 1 "listeners did not start"
+ stop_server "$pid_first"
+ [ -n "$pid_second" ] &&
+ stop_server "$pid_second"
+ log_test "IPv$ver: $label, $first first"
+ continue
+ fi
+
+ if [ "$first" = "$winner" ]; then
+ pid_win=$pid_first
+ pid_lose=$pid_second
+ loser=$second
+ else
+ pid_win=$pid_second
+ pid_lose=$pid_first
+ loser=$first
+ fi
+
+ connect "$cli" "$ver" "$(server_addr "$net" "$ver")"
+ check_err $? "connection failed"
+ wait "$pid_win"
+ check_err $? "$winner listener did not get the conn"
+ stop_idle_server "$pid_lose" "$loser listener"
+
+ log_test "IPv$ver: $label, $first first, $winner wins"
+ done
+ done
+}
+
+vrf_bound_listener()
+{
+ two_listeners "$ns_c" vrf "$VRF" "$VRF"
+}
+
+vrf_slave_bound_listener()
+{
+ two_listeners "$ns_c" vrf veth_vrf veth_vrf
+}
+
+default_dev_bound_listener()
+{
+ two_listeners "$ns_d" def veth_def veth_def
+}
+
+default_vrf_bound_listener()
+{
+ two_listeners "$ns_d" def "$VRF" unbound
+}
+
+# A listener that is bound to the VRF only after listen() must be preferred
+# too. It is started first, and bound before the unbound listener starts,
+# so that the two are not in the same reuseport group.
+vrf_bound_after_listen()
+{
+ local ver pid_bound pid_unbound
+
+ for ver in 4 6; do
+ RET=0
+
+ pid_unbound=
+ start_server "$ver" "$VRF" --bind-after-listen
+ pid_bound=$!
+ if slowwait 2 listener_bound_to "$VRF"; then
+ start_server "$ver" unbound
+ pid_unbound=$!
+ fi
+ if [ -z "$pid_unbound" ] || ! wait_listeners 2; then
+ check_err 1 "listeners did not start"
+ stop_server "$pid_bound"
+ [ -n "$pid_unbound" ] && stop_server "$pid_unbound"
+ log_test "IPv$ver: VRF conn, $VRF bound after listen"
+ continue
+ fi
+
+ connect "$ns_c" "$ver" "$(server_addr vrf "$ver")"
+ check_err $? "connection failed"
+ wait "$pid_bound"
+ check_err $? "$VRF listener did not get the conn"
+ stop_idle_server "$pid_unbound" "unbound listener"
+
+ log_test "IPv$ver: VRF conn, $VRF bound after listen, $VRF wins"
+ done
+}
+
+trap cleanup EXIT
+
+check_gen_prog nettest
+setup
+tests_run
+
+exit "$EXIT_STATUS"
--
2.43.0