selftests: hsr: Add tests for faulty links

Add a test case that can support different types of faulty links for all
protocol versions (HSRv0, HSRv1, PRPv1). It starts with a baseline with
fully functional links. The first faulty case is one link being cut during
the ping. This test uses a different function for ping that sends more
packets in shorter intervals to stress the duplicate detection algorithms a
bit more and allow for future tests with other link faults (packet loss,
reordering, etc.).

As the link fault tests now cover the cut link for HSR and PRP, it can be
removed from the hsr_ping test. Note that the removed cut link test did not
really test the fault because do_ping_long takes about 1sec while the link
is only cut after a 3sec sleep.

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: https://patch.msgid.link/dad52276e2c349ecb96168bef7e3001bf7becc81.1770299429.git.fmaurer@redhat.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
Felix Maurer 2026-02-05 14:57:30 +01:00 committed by Paolo Abeni
parent 776b64ba12
commit ca4a09a950
3 changed files with 272 additions and 11 deletions

View file

@ -5,6 +5,7 @@ top_srcdir = ../../../../..
TEST_PROGS := \
hsr_ping.sh \
hsr_redbox.sh \
link_faults.sh \
prp_ping.sh \
# end of TEST_PROGS

View file

@ -94,17 +94,6 @@ do_link_problem_tests()
{
echo "INFO: Running link problem tests."
echo "INFO: Cutting one link."
do_ping_long "$ns1" 100.64.0.3 &
sleep 3
ip -net "$ns3" link set ns3eth1 down
wait
ip -net "$ns3" link set ns3eth1 up
stop_if_error "Failed with one link down."
echo "INFO: Delay the link and drop a few packages."
tc -net "$ns3" qdisc add dev ns3eth1 root netem delay 50ms
tc -net "$ns2" qdisc add dev ns2eth1 root netem delay 5ms loss 25%

View file

@ -0,0 +1,271 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# shellcheck disable=SC2329
source ../lib.sh
ALL_TESTS="
test_clean_hsrv0
test_cut_link_hsrv0
test_clean_hsrv1
test_cut_link_hsrv1
test_clean_prp
test_cut_link_prp
"
# The tests are running ping for 5sec with a relatively short interval with a
# cut link, which should be recoverable by HSR/PRP.
setup_hsr_topo()
{
# Three HSR nodes in a ring, every node has a LAN A interface connected
# to the LAN B interface of the next node.
#
# node1 node2
#
# vethA -------- vethB
# hsr1 hsr2
# vethB vethA
# \ /
# vethA vethB
# hsr3
#
# node3
local ver="$1"
setup_ns node1 node2 node3
# veth links
# shellcheck disable=SC2154 # variables assigned by setup_ns
ip link add vethA netns "$node1" type veth peer name vethB netns "$node2"
# shellcheck disable=SC2154 # variables assigned by setup_ns
ip link add vethA netns "$node2" type veth peer name vethB netns "$node3"
ip link add vethA netns "$node3" type veth peer name vethB netns "$node1"
# MAC addresses (not needed for HSR operation, but helps with debugging)
ip -net "$node1" link set address 00:11:22:00:01:01 dev vethA
ip -net "$node1" link set address 00:11:22:00:01:02 dev vethB
ip -net "$node2" link set address 00:11:22:00:02:01 dev vethA
ip -net "$node2" link set address 00:11:22:00:02:02 dev vethB
ip -net "$node3" link set address 00:11:22:00:03:01 dev vethA
ip -net "$node3" link set address 00:11:22:00:03:02 dev vethB
# HSR interfaces
ip -net "$node1" link add name hsr1 type hsr proto 0 version "$ver" \
slave1 vethA slave2 vethB supervision 45
ip -net "$node2" link add name hsr2 type hsr proto 0 version "$ver" \
slave1 vethA slave2 vethB supervision 45
ip -net "$node3" link add name hsr3 type hsr proto 0 version "$ver" \
slave1 vethA slave2 vethB supervision 45
# IP addresses
ip -net "$node1" addr add 100.64.0.1/24 dev hsr1
ip -net "$node2" addr add 100.64.0.2/24 dev hsr2
ip -net "$node3" addr add 100.64.0.3/24 dev hsr3
# Set all links up
ip -net "$node1" link set vethA up
ip -net "$node1" link set vethB up
ip -net "$node1" link set hsr1 up
ip -net "$node2" link set vethA up
ip -net "$node2" link set vethB up
ip -net "$node2" link set hsr2 up
ip -net "$node3" link set vethA up
ip -net "$node3" link set vethB up
ip -net "$node3" link set hsr3 up
}
setup_prp_topo()
{
# Two PRP nodes, connected by two links (treated as LAN A and LAN B).
#
# vethA ----- vethA
# prp1 prp2
# vethB ----- vethB
#
# node1 node2
setup_ns node1 node2
# veth links
ip link add vethA netns "$node1" type veth peer name vethA netns "$node2"
ip link add vethB netns "$node1" type veth peer name vethB netns "$node2"
# MAC addresses will be copied from LAN A interface
ip -net "$node1" link set address 00:11:22:00:00:01 dev vethA
ip -net "$node2" link set address 00:11:22:00:00:02 dev vethA
# PRP interfaces
ip -net "$node1" link add name prp1 type hsr \
slave1 vethA slave2 vethB supervision 45 proto 1
ip -net "$node2" link add name prp2 type hsr \
slave1 vethA slave2 vethB supervision 45 proto 1
# IP addresses
ip -net "$node1" addr add 100.64.0.1/24 dev prp1
ip -net "$node2" addr add 100.64.0.2/24 dev prp2
# All links up
ip -net "$node1" link set vethA up
ip -net "$node1" link set vethB up
ip -net "$node1" link set prp1 up
ip -net "$node2" link set vethA up
ip -net "$node2" link set vethB up
ip -net "$node2" link set prp2 up
}
wait_for_hsr_node_table()
{
log_info "Wait for node table entries to be merged."
WAIT=5
while [ "${WAIT}" -gt 0 ]; do
nts=$(cat /sys/kernel/debug/hsr/hsr*/node_table)
# We need entries in the node tables, and they need to be merged
if (echo "$nts" | grep -qE "^([0-9a-f]{2}:){5}") && \
! (echo "$nts" | grep -q "00:00:00:00:00:00"); then
return
fi
sleep 1
((WAIT--))
done
check_err 1 "Failed to wait for merged node table entries"
}
setup_topo()
{
local proto="$1"
if [ "$proto" = "HSRv0" ]; then
setup_hsr_topo 0
wait_for_hsr_node_table
elif [ "$proto" = "HSRv1" ]; then
setup_hsr_topo 1
wait_for_hsr_node_table
elif [ "$proto" = "PRP" ]; then
setup_prp_topo
else
check_err 1 "Unknown protocol (${proto})"
fi
}
check_ping()
{
local node="$1"
local dst="$2"
local ping_args="-q -i 0.01 -c 400"
log_info "Running ping $node -> $dst"
# shellcheck disable=SC2086
output=$(ip netns exec "$node" ping $ping_args "$dst" | \
grep "packets transmitted")
log_info "$output"
dups=0
loss=0
if [[ "$output" =~ \+([0-9]+)" duplicates" ]]; then
dups="${BASH_REMATCH[1]}"
fi
if [[ "$output" =~ ([0-9\.]+\%)" packet loss" ]]; then
loss="${BASH_REMATCH[1]}"
fi
check_err "$dups" "Unexpected duplicate packets (${dups})"
if [ "$loss" != "0%" ]; then
check_err 1 "Unexpected packet loss (${loss})"
fi
}
test_clean()
{
local proto="$1"
RET=0
tname="${FUNCNAME[0]} - ${proto}"
setup_topo "$proto"
if ((RET != ksft_pass)); then
log_test "${tname} setup"
return
fi
check_ping "$node1" "100.64.0.2"
log_test "${tname}"
}
test_clean_hsrv0()
{
test_clean "HSRv0"
}
test_clean_hsrv1()
{
test_clean "HSRv1"
}
test_clean_prp()
{
test_clean "PRP"
}
test_cut_link()
{
local proto="$1"
RET=0
tname="${FUNCNAME[0]} - ${proto}"
setup_topo "$proto"
if ((RET != ksft_pass)); then
log_test "${tname} setup"
return
fi
# Cutting link from subshell, so check_ping can run in the normal shell
# with access to global variables from the test harness.
(
sleep 2
log_info "Cutting link"
ip -net "$node1" link set vethB down
) &
check_ping "$node1" "100.64.0.2"
wait
log_test "${tname}"
}
test_cut_link_hsrv0()
{
test_cut_link "HSRv0"
}
test_cut_link_hsrv1()
{
test_cut_link "HSRv1"
}
test_cut_link_prp()
{
test_cut_link "PRP"
}
cleanup()
{
cleanup_all_ns
}
trap cleanup EXIT
tests_run
exit $EXIT_STATUS