Path: blob/master/tools/testing/selftests/drivers/net/team/teamd_activebackup.sh
171056 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023# These tests verify that teamd is able to enable and disable ports via the4# active backup runner.5#6# Topology:7#8# +-------------------------+ NS19# | test_team1 |10# | + |11# | eth0 | eth1 |12# | +---+---+ |13# | | | |14# +-------------------------+15# | |16# +-------------------------+ NS217# | | | |18# | +-------+ |19# | eth0 | eth1 |20# | + |21# | test_team2 |22# +-------------------------+2324export ALL_TESTS="teamd_test_active_backup"2526test_dir="$(dirname "$0")"27# shellcheck disable=SC109128source "${test_dir}/../../../net/lib.sh"29# shellcheck disable=SC109130source "${test_dir}/team_lib.sh"3132NS1=""33NS2=""34export NODAD="nodad"35PREFIX_LENGTH="64"36NS1_IP="fd00::1"37NS2_IP="fd00::2"38NS1_IP4="192.168.0.1"39NS2_IP4="192.168.0.2"40NS1_TEAMD_CONF=""41NS2_TEAMD_CONF=""42NS1_TEAMD_PID=""43NS2_TEAMD_PID=""4445while getopts "4" opt; do46case $opt in474)48echo "IPv4 mode selected."49export NODAD=50PREFIX_LENGTH="24"51NS1_IP="${NS1_IP4}"52NS2_IP="${NS2_IP4}"53;;54\?)55echo "Invalid option: -${OPTARG}" >&256exit 157;;58esac59done6061teamd_config_create()62{63local runner=$164local dev=$265local conf6667conf=$(mktemp)6869cat > "${conf}" <<-EOF70{71"device": "${dev}",72"runner": {"name": "${runner}"},73"ports": {74"eth0": {},75"eth1": {}76}77}78EOF79echo "${conf}"80}8182# Create the network namespaces, veth pair, and team devices in the specified83# runner.84# Globals:85# RET - Used by test infra, set by `check_err` functions.86# Arguments:87# runner - The Teamd runner to use for the Team devices.88environment_create()89{90local runner=$19192echo "Setting up two-link aggregation for runner ${runner}"93echo "Teamd version is: $(teamd --version)"94trap environment_destroy EXIT9596setup_ns ns1 ns297NS1="${NS_LIST[0]}"98NS2="${NS_LIST[1]}"99100for link in $(seq 0 1); do101ip -n "${NS1}" link add "eth${link}" type veth peer name \102"eth${link}" netns "${NS2}"103check_err $? "Failed to create veth pair"104done105106NS1_TEAMD_CONF=$(teamd_config_create "${runner}" "test_team1")107NS2_TEAMD_CONF=$(teamd_config_create "${runner}" "test_team2")108echo "Conf files are ${NS1_TEAMD_CONF} and ${NS2_TEAMD_CONF}"109110ip netns exec "${NS1}" teamd -d -f "${NS1_TEAMD_CONF}"111check_err $? "Failed to create team device in ${NS1}"112NS1_TEAMD_PID=$(pgrep -f "teamd -d -f ${NS1_TEAMD_CONF}")113114ip netns exec "${NS2}" teamd -d -f "${NS2_TEAMD_CONF}"115check_err $? "Failed to create team device in ${NS2}"116NS2_TEAMD_PID=$(pgrep -f "teamd -d -f ${NS2_TEAMD_CONF}")117118echo "Created team devices"119echo "Teamd PIDs are ${NS1_TEAMD_PID} and ${NS2_TEAMD_PID}"120121ip -n "${NS1}" link set test_team1 up122check_err $? "Failed to set test_team1 up in ${NS1}"123ip -n "${NS2}" link set test_team2 up124check_err $? "Failed to set test_team2 up in ${NS2}"125126ip -n "${NS1}" addr add "${NS1_IP}/${PREFIX_LENGTH}" "${NODAD}" dev \127test_team1128check_err $? "Failed to add address to team device in ${NS1}"129ip -n "${NS2}" addr add "${NS2_IP}/${PREFIX_LENGTH}" "${NODAD}" dev \130test_team2131check_err $? "Failed to add address to team device in ${NS2}"132133slowwait 2 timeout 0.5 ip netns exec "${NS1}" ping -W 1 -c 1 "${NS2_IP}"134}135136# Tear down the environment: kill teamd and delete network namespaces.137environment_destroy()138{139echo "Tearing down two-link aggregation"140141rm "${NS1_TEAMD_CONF}"142rm "${NS2_TEAMD_CONF}"143144# First, try graceful teamd teardown.145ip netns exec "${NS1}" teamd -k -t test_team1146ip netns exec "${NS2}" teamd -k -t test_team2147148# If teamd can't be killed gracefully, then sigkill.149if kill -0 "${NS1_TEAMD_PID}" 2>/dev/null; then150echo "Sending sigkill to teamd for test_team1"151kill -9 "${NS1_TEAMD_PID}"152rm -f /var/run/teamd/test_team1.{pid,sock}153fi154if kill -0 "${NS2_TEAMD_PID}" 2>/dev/null; then155echo "Sending sigkill to teamd for test_team2"156kill -9 "${NS2_TEAMD_PID}"157rm -f /var/run/teamd/test_team2.{pid,sock}158fi159cleanup_all_ns160}161162# Change the active port for an active-backup mode team.163# Arguments:164# namespace - The network namespace that the team is in.165# team - The name of the team.166# active_port - The port to make active.167set_active_port()168{169local namespace=$1170local team=$2171local active_port=$3172173ip netns exec "${namespace}" teamdctl "${team}" state item set \174runner.active_port "${active_port}"175slowwait 2 bash -c "ip netns exec ${namespace} teamdctl ${team} state \176item get runner.active_port | grep -q ${active_port}"177}178179# Wait for an interface to stop receiving traffic. If it keeps receiving traffic180# for the duration of the timeout, then return an error.181# Arguments:182# - namespace - The network namespace that the interface is in.183# - interface - The name of the interface.184wait_to_stop_receiving()185{186local namespace=$1187local interface=$2188189echo "Waiting for ${interface} in ${namespace} to stop receiving"190slowwait 10 check_no_traffic "${interface}" "${NS2_IP}" \191"${namespace}"192}193194# Test that active backup runner can change active ports.195# Globals:196# RET - Used by test infra, set by `check_err` functions.197teamd_test_active_backup()198{199export RET=0200201start_listening_and_sending202203### Scenario 1: Don't manually set active port, just make sure team204# works.205save_tcpdump_outputs "${NS2}" test_team2206did_interface_receive test_team2 "${NS2_IP}"207check_err $? "Traffic did not reach team interface in NS2."208clear_tcpdump_outputs test_team2209210### Scenario 2: Choose active port.211set_active_port "${NS1}" test_team1 eth1212set_active_port "${NS2}" test_team2 eth1213214wait_to_stop_receiving "${NS2}" eth0215save_tcpdump_outputs "${NS2}" eth0 eth1216did_interface_receive eth0 "${NS2_IP}"217check_fail $? "eth0 IS transmitting when inactive"218did_interface_receive eth1 "${NS2_IP}"219check_err $? "eth1 not transmitting when active"220clear_tcpdump_outputs eth0 eth1221222### Scenario 3: Change active port.223set_active_port "${NS1}" test_team1 eth0224set_active_port "${NS2}" test_team2 eth0225226wait_to_stop_receiving "${NS2}" eth1227save_tcpdump_outputs "${NS2}" eth0 eth1228did_interface_receive eth0 "${NS2_IP}"229check_err $? "eth0 not transmitting when active"230did_interface_receive eth1 "${NS2_IP}"231check_fail $? "eth1 IS transmitting when inactive"232clear_tcpdump_outputs eth0 eth1233234log_test "teamd active backup runner test"235236stop_sending_and_listening237}238239require_command teamd240require_command teamdctl241require_command iperf3242require_command tcpdump243environment_create activebackup244tests_run245exit "${EXIT_STATUS}"246247248