xref: /linux/tools/testing/selftests/drivers/net/team/options.sh (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
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
14ALL_TESTS="
15        team_test_options
16"
17
18source "${test_dir}/../../../net/lib.sh"
19
20TEAM_PORT="team0"
21MEMBER_PORT="dummy0"
22
23setup()
24{
25        ip link add name "${MEMBER_PORT}" type dummy
26        ip link add name "${TEAM_PORT}" type team
27}
28
29get_and_check_value()
30{
31        local option_name="$1"
32        local expected_value="$2"
33        local port_flag="$3"
34
35        local value_from_get
36
37        if ! value_from_get=$(teamnl "${TEAM_PORT}" getoption "${option_name}" \
38                        "${port_flag}"); then
39                echo "Could not get option '${option_name}'" >&2
40                return 1
41        fi
42
43        if [[ "${value_from_get}" != "${expected_value}" ]]; then
44                echo "Incorrect value for option '${option_name}'" >&2
45                echo "get (${value_from_get}) != set (${expected_value})" >&2
46                return 1
47        fi
48}
49
50set_and_check_get()
51{
52        local option_name="$1"
53        local option_value="$2"
54        local port_flag="$3"
55
56        local value_from_get
57
58        if ! teamnl "${TEAM_PORT}" setoption "${option_name}" \
59                        "${option_value}" "${port_flag}"; then
60                echo "'setoption ${option_name} ${option_value}' failed" >&2
61                return 1
62        fi
63
64        get_and_check_value "${option_name}" "${option_value}" "${port_flag}"
65        return $?
66}
67
68# Get a "port flag" to pass to the `teamnl` command.
69# E.g. $1="dummy0" -> "port=dummy0",
70#      $1=""       -> ""
71get_port_flag()
72{
73        local port_name="$1"
74
75        if [[ -n "${port_name}" ]]; then
76                echo "--port=${port_name}"
77        fi
78}
79
80attach_port_if_specified()
81{
82        local port_name="$1"
83
84        if [[ -n "${port_name}" ]]; then
85                ip link set dev "${port_name}" master "${TEAM_PORT}"
86                return $?
87        fi
88}
89
90detach_port_if_specified()
91{
92        local port_name="$1"
93
94        if [[ -n "${port_name}" ]]; then
95                ip link set dev "${port_name}" nomaster
96                return $?
97        fi
98}
99
100# Test that an option's get value matches its set value.
101# Globals:
102#   RET - Used by testing infra like `check_err`.
103#   EXIT_STATUS - Used by `log_test` for whole script exit value.
104# Arguments:
105#   option_name - The name of the option.
106#   value_1 - The first value to try setting.
107#   value_2 - The second value to try setting.
108#   port_name - The (optional) name of the attached port.
109team_test_option()
110{
111        local option_name="$1"
112        local value_1="$2"
113        local value_2="$3"
114        local possible_values="$2 $3 $2"
115        local port_name="$4"
116        local port_flag
117
118        RET=0
119
120        echo "Setting '${option_name}' to '${value_1}' and '${value_2}'"
121
122        attach_port_if_specified "${port_name}"
123        check_err $? "Couldn't attach ${port_name} to master"
124        port_flag=$(get_port_flag "${port_name}")
125
126        # Set and get both possible values.
127        for value in ${possible_values}; do
128                set_and_check_get "${option_name}" "${value}" "${port_flag}"
129                check_err $? "Failed to set '${option_name}' to '${value}'"
130        done
131
132        detach_port_if_specified "${port_name}"
133        check_err $? "Couldn't detach ${port_name} from its master"
134
135        log_test "Set + Get '${option_name}' test"
136}
137
138# Test that getting a non-existant option fails.
139# Globals:
140#   RET - Used by testing infra like `check_err`.
141#   EXIT_STATUS - Used by `log_test` for whole script exit value.
142# Arguments:
143#   option_name - The name of the option.
144#   port_name - The (optional) name of the attached port.
145team_test_get_option_fails()
146{
147        local option_name="$1"
148        local port_name="$2"
149        local port_flag
150
151        RET=0
152
153        attach_port_if_specified "${port_name}"
154        check_err $? "Couldn't attach ${port_name} to master"
155        port_flag=$(get_port_flag "${port_name}")
156
157        # Just confirm that getting the value fails.
158        teamnl "${TEAM_PORT}" getoption "${option_name}" "${port_flag}"
159        check_fail $? "Shouldn't be able to get option '${option_name}'"
160
161        detach_port_if_specified "${port_name}"
162
163        log_test "Get '${option_name}' fails"
164}
165
166team_test_options()
167{
168        # Wrong option name behavior.
169        team_test_get_option_fails fake_option1
170        team_test_get_option_fails fake_option2 "${MEMBER_PORT}"
171
172        # Correct set and get behavior.
173        team_test_option mode activebackup loadbalance
174        team_test_option notify_peers_count 0 5
175        team_test_option notify_peers_interval 0 5
176        team_test_option mcast_rejoin_count 0 5
177        team_test_option mcast_rejoin_interval 0 5
178        team_test_option enabled true false "${MEMBER_PORT}"
179        team_test_option user_linkup true false "${MEMBER_PORT}"
180        team_test_option user_linkup_enabled true false "${MEMBER_PORT}"
181        team_test_option priority 10 20 "${MEMBER_PORT}"
182        team_test_option queue_id 0 1 "${MEMBER_PORT}"
183}
184
185require_command teamnl
186setup
187tests_run
188exit "${EXIT_STATUS}"
189