1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4# These tests verify basic set and get functionality of the team 5# driver options over netlink. 6 7# Run in private netns. 8test_dir="$(dirname "$0")" 9if [[ $# -eq 0 ]]; then 10 "${test_dir}"/../../../net/in_netns.sh "$0" __subprocess 11 exit $? 12fi 13 14export ALL_TESTS=" 15 team_test_options 16 team_test_enabled_implicit_changes 17 team_test_rx_enabled_implicit_changes 18 team_test_tx_enabled_implicit_changes 19" 20 21# shellcheck disable=SC1091 22source "${test_dir}/../../../net/lib.sh" 23 24TEAM_PORT="team0" 25MEMBER_PORT="dummy0" 26 27setup() 28{ 29 ip link add name "${MEMBER_PORT}" type dummy 30 ip link add name "${TEAM_PORT}" type team 31} 32 33get_and_check_value() 34{ 35 local option_name="$1" 36 local expected_value="$2" 37 local port_flag="$3" 38 39 local value_from_get 40 41 if ! value_from_get=$(teamnl "${TEAM_PORT}" getoption "${option_name}" \ 42 "${port_flag}"); then 43 echo "Could not get option '${option_name}'" >&2 44 return 1 45 fi 46 47 if [[ "${value_from_get}" != "${expected_value}" ]]; then 48 echo "Incorrect value for option '${option_name}'" >&2 49 echo "get (${value_from_get}) != set (${expected_value})" >&2 50 return 1 51 fi 52} 53 54set_and_check_get() 55{ 56 local option_name="$1" 57 local option_value="$2" 58 local port_flag="$3" 59 60 local value_from_get 61 62 if ! teamnl "${TEAM_PORT}" setoption "${option_name}" \ 63 "${option_value}" "${port_flag}"; then 64 echo "'setoption ${option_name} ${option_value}' failed" >&2 65 return 1 66 fi 67 68 get_and_check_value "${option_name}" "${option_value}" "${port_flag}" 69 return $? 70} 71 72# Get a "port flag" to pass to the `teamnl` command. 73# E.g. $1="dummy0" -> "port=dummy0", 74# $1="" -> "" 75get_port_flag() 76{ 77 local port_name="$1" 78 79 if [[ -n "${port_name}" ]]; then 80 echo "--port=${port_name}" 81 fi 82} 83 84attach_port_if_specified() 85{ 86 local port_name="$1" 87 88 if [[ -n "${port_name}" ]]; then 89 ip link set dev "${port_name}" master "${TEAM_PORT}" 90 return $? 91 fi 92} 93 94detach_port_if_specified() 95{ 96 local port_name="$1" 97 98 if [[ -n "${port_name}" ]]; then 99 ip link set dev "${port_name}" nomaster 100 return $? 101 fi 102} 103 104# Test that an option's get value matches its set value. 105# Globals: 106# RET - Used by testing infra like `check_err`. 107# EXIT_STATUS - Used by `log_test` for whole script exit value. 108# Arguments: 109# option_name - The name of the option. 110# value_1 - The first value to try setting. 111# value_2 - The second value to try setting. 112# port_name - The (optional) name of the attached port. 113team_test_option() 114{ 115 local option_name="$1" 116 local value_1="$2" 117 local value_2="$3" 118 local possible_values="$2 $3 $2" 119 local port_name="$4" 120 local port_flag 121 122 RET=0 123 124 echo "Setting '${option_name}' to '${value_1}' and '${value_2}'" 125 126 attach_port_if_specified "${port_name}" 127 check_err $? "Couldn't attach ${port_name} to master" 128 port_flag=$(get_port_flag "${port_name}") 129 130 # Set and get both possible values. 131 for value in ${possible_values}; do 132 set_and_check_get "${option_name}" "${value}" "${port_flag}" 133 check_err $? "Failed to set '${option_name}' to '${value}'" 134 done 135 136 detach_port_if_specified "${port_name}" 137 check_err $? "Couldn't detach ${port_name} from its master" 138 139 log_test "Set + Get '${option_name}' test" 140} 141 142# Test that getting a non-existant option fails. 143# Globals: 144# RET - Used by testing infra like `check_err`. 145# EXIT_STATUS - Used by `log_test` for whole script exit value. 146# Arguments: 147# option_name - The name of the option. 148# port_name - The (optional) name of the attached port. 149team_test_get_option_fails() 150{ 151 local option_name="$1" 152 local port_name="$2" 153 local port_flag 154 155 RET=0 156 157 attach_port_if_specified "${port_name}" 158 check_err $? "Couldn't attach ${port_name} to master" 159 port_flag=$(get_port_flag "${port_name}") 160 161 # Just confirm that getting the value fails. 162 teamnl "${TEAM_PORT}" getoption "${option_name}" "${port_flag}" 163 check_fail $? "Shouldn't be able to get option '${option_name}'" 164 165 detach_port_if_specified "${port_name}" 166 167 log_test "Get '${option_name}' fails" 168} 169 170team_test_options() 171{ 172 # Wrong option name behavior. 173 team_test_get_option_fails fake_option1 174 team_test_get_option_fails fake_option2 "${MEMBER_PORT}" 175 176 # Correct set and get behavior. 177 team_test_option mode activebackup loadbalance 178 team_test_option notify_peers_count 0 5 179 team_test_option notify_peers_interval 0 5 180 team_test_option mcast_rejoin_count 0 5 181 team_test_option mcast_rejoin_interval 0 5 182 team_test_option enabled true false "${MEMBER_PORT}" 183 team_test_option rx_enabled true false "${MEMBER_PORT}" 184 team_test_option tx_enabled true false "${MEMBER_PORT}" 185 team_test_option user_linkup true false "${MEMBER_PORT}" 186 team_test_option user_linkup_enabled true false "${MEMBER_PORT}" 187 team_test_option priority 10 20 "${MEMBER_PORT}" 188 team_test_option queue_id 0 1 "${MEMBER_PORT}" 189} 190 191team_test_enabled_implicit_changes() 192{ 193 export RET=0 194 195 attach_port_if_specified "${MEMBER_PORT}" 196 check_err $? "Couldn't attach ${MEMBER_PORT} to master" 197 198 # Set enabled to true. 199 set_and_check_get enabled true "--port=${MEMBER_PORT}" 200 check_err $? "Failed to set 'enabled' to true" 201 202 # Show that both rx enabled and tx enabled are true. 203 get_and_check_value rx_enabled true "--port=${MEMBER_PORT}" 204 check_err $? "'Rx_enabled' wasn't implicitly set to true" 205 get_and_check_value tx_enabled true "--port=${MEMBER_PORT}" 206 check_err $? "'Tx_enabled' wasn't implicitly set to true" 207 208 # Set enabled to false. 209 set_and_check_get enabled false "--port=${MEMBER_PORT}" 210 check_err $? "Failed to set 'enabled' to false" 211 212 # Show that both rx enabled and tx enabled are false. 213 get_and_check_value rx_enabled false "--port=${MEMBER_PORT}" 214 check_err $? "'Rx_enabled' wasn't implicitly set to false" 215 get_and_check_value tx_enabled false "--port=${MEMBER_PORT}" 216 check_err $? "'Tx_enabled' wasn't implicitly set to false" 217 218 log_test "'Enabled' implicit changes" 219} 220 221team_test_rx_enabled_implicit_changes() 222{ 223 export RET=0 224 225 attach_port_if_specified "${MEMBER_PORT}" 226 check_err $? "Couldn't attach ${MEMBER_PORT} to master" 227 228 # Set enabled to true. 229 set_and_check_get enabled true "--port=${MEMBER_PORT}" 230 check_err $? "Failed to set 'enabled' to true" 231 232 # Set rx_enabled to false. 233 set_and_check_get rx_enabled false "--port=${MEMBER_PORT}" 234 check_err $? "Failed to set 'rx_enabled' to false" 235 236 # Show that enabled is false. 237 get_and_check_value enabled false "--port=${MEMBER_PORT}" 238 check_err $? "'enabled' wasn't implicitly set to false" 239 240 # Set rx_enabled to true. 241 set_and_check_get rx_enabled true "--port=${MEMBER_PORT}" 242 check_err $? "Failed to set 'rx_enabled' to true" 243 244 # Show that enabled is true. 245 get_and_check_value enabled true "--port=${MEMBER_PORT}" 246 check_err $? "'enabled' wasn't implicitly set to true" 247 248 log_test "'Rx_enabled' implicit changes" 249} 250 251team_test_tx_enabled_implicit_changes() 252{ 253 export RET=0 254 255 attach_port_if_specified "${MEMBER_PORT}" 256 check_err $? "Couldn't attach ${MEMBER_PORT} to master" 257 258 # Set enabled to true. 259 set_and_check_get enabled true "--port=${MEMBER_PORT}" 260 check_err $? "Failed to set 'enabled' to true" 261 262 # Set tx_enabled to false. 263 set_and_check_get tx_enabled false "--port=${MEMBER_PORT}" 264 check_err $? "Failed to set 'tx_enabled' to false" 265 266 # Show that enabled is false. 267 get_and_check_value enabled false "--port=${MEMBER_PORT}" 268 check_err $? "'enabled' wasn't implicitly set to false" 269 270 # Set tx_enabled to true. 271 set_and_check_get tx_enabled true "--port=${MEMBER_PORT}" 272 check_err $? "Failed to set 'tx_enabled' to true" 273 274 # Show that enabled is true. 275 get_and_check_value enabled true "--port=${MEMBER_PORT}" 276 check_err $? "'enabled' wasn't implicitly set to true" 277 278 log_test "'Tx_enabled' implicit changes" 279} 280 281 282require_command teamnl 283setup 284tests_run 285exit "${EXIT_STATUS}" 286