1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# In addition to the common variables, user might use:
5# LC_SLOT - If not set, all probed line cards are going to be tested,
6#	    with an exception of the "activation_16x100G_test".
7#	    It set, only the selected line card is going to be used
8#	    for tests, including "activation_16x100G_test".
9
10lib_dir=$(dirname $0)/../../../net/forwarding
11
12ALL_TESTS="
13	unprovision_test
14	provision_test
15	activation_16x100G_test
16"
17
18NUM_NETIFS=0
19
20source $lib_dir/lib.sh
21source $lib_dir/devlink_lib.sh
22
23until_lc_state_is()
24{
25	local state=$1; shift
26	local current=$("$@")
27
28	echo "$current"
29	[ "$current" == "$state" ]
30}
31
32until_lc_state_is_not()
33{
34	! until_lc_state_is "$@"
35}
36
37lc_state_get()
38{
39	local lc=$1
40
41	devlink lc show $DEVLINK_DEV lc $lc -j | jq -e -r ".[][][].state"
42}
43
44lc_wait_until_state_changes()
45{
46	local lc=$1
47	local state=$2
48	local timeout=$3 # ms
49
50	busywait "$timeout" until_lc_state_is_not "$state" lc_state_get "$lc"
51}
52
53lc_wait_until_state_becomes()
54{
55	local lc=$1
56	local state=$2
57	local timeout=$3 # ms
58
59	busywait "$timeout" until_lc_state_is "$state" lc_state_get "$lc"
60}
61
62until_lc_port_count_is()
63{
64	local port_count=$1; shift
65	local current=$("$@")
66
67	echo "$current"
68	[ $current == $port_count ]
69}
70
71lc_port_count_get()
72{
73	local lc=$1
74
75	devlink port -j | jq -e -r ".[][] | select(.lc==$lc) | .port" | wc -l
76}
77
78lc_wait_until_port_count_is()
79{
80	local lc=$1
81	local port_count=$2
82	local timeout=$3 # ms
83
84	busywait "$timeout" until_lc_port_count_is "$port_count" lc_port_count_get "$lc"
85}
86
87PROV_UNPROV_TIMEOUT=8000 # ms
88POST_PROV_ACT_TIMEOUT=2000 # ms
89PROV_PORTS_INSTANTIATION_TIMEOUT=15000 # ms
90
91unprovision_one()
92{
93	local lc=$1
94	local state
95
96	state=$(lc_state_get $lc)
97	check_err $? "Failed to get state of linecard $lc"
98	if [[ "$state" == "unprovisioned" ]]; then
99		return
100	fi
101
102	log_info "Unprovisioning linecard $lc"
103
104	devlink lc set $DEVLINK_DEV lc $lc notype
105	check_err $? "Failed to trigger linecard $lc unprovisioning"
106
107	state=$(lc_wait_until_state_changes $lc "unprovisioning" \
108		$PROV_UNPROV_TIMEOUT)
109	check_err $? "Failed to unprovision linecard $lc (timeout)"
110
111	[ "$state" == "unprovisioned" ]
112	check_err $? "Failed to unprovision linecard $lc (state=$state)"
113}
114
115provision_one()
116{
117	local lc=$1
118	local type=$2
119	local state
120
121	log_info "Provisioning linecard $lc"
122
123	devlink lc set $DEVLINK_DEV lc $lc type $type
124	check_err $? "Failed trigger linecard $lc provisioning"
125
126	state=$(lc_wait_until_state_changes $lc "provisioning" \
127		$PROV_UNPROV_TIMEOUT)
128	check_err $? "Failed to provision linecard $lc (timeout)"
129
130	[ "$state" == "provisioned" ] || [ "$state" == "active" ]
131	check_err $? "Failed to provision linecard $lc (state=$state)"
132
133	provisioned_type=$(devlink lc show $DEVLINK_DEV lc $lc -j | jq -e -r ".[][][].type")
134	[ "$provisioned_type" == "$type" ]
135	check_err $? "Wrong provision type returned for linecard $lc (got \"$provisioned_type\", expected \"$type\")"
136
137	# Wait for possible activation to make sure the state
138	# won't change after return from this function.
139	state=$(lc_wait_until_state_becomes $lc "active" \
140		$POST_PROV_ACT_TIMEOUT)
141}
142
143unprovision_test()
144{
145	RET=0
146	local lc
147
148	lc=$LC_SLOT
149	unprovision_one $lc
150	log_test "Unprovision"
151}
152
153LC_16X100G_TYPE="16x100G"
154LC_16X100G_PORT_COUNT=16
155
156supported_types_check()
157{
158	local lc=$1
159	local supported_types_count
160	local type_index
161	local lc_16x100_found=false
162
163	supported_types_count=$(devlink lc show $DEVLINK_DEV lc $lc -j | \
164				jq -e -r ".[][][].supported_types | length")
165	[ $supported_types_count != 0 ]
166	check_err $? "No supported types found for linecard $lc"
167	for (( type_index=0; type_index<$supported_types_count; type_index++ ))
168	do
169		type=$(devlink lc show $DEVLINK_DEV lc $lc -j | \
170		       jq -e -r ".[][][].supported_types[$type_index]")
171		if [[ "$type" == "$LC_16X100G_TYPE" ]]; then
172			lc_16x100_found=true
173			break
174		fi
175	done
176	[ $lc_16x100_found = true ]
177	check_err $? "16X100G not found between supported types of linecard $lc"
178}
179
180ports_check()
181{
182	local lc=$1
183	local expected_port_count=$2
184	local port_count
185
186	port_count=$(lc_wait_until_port_count_is $lc $expected_port_count \
187		$PROV_PORTS_INSTANTIATION_TIMEOUT)
188	[ $port_count != 0 ]
189	check_err $? "No port associated with linecard $lc"
190	[ $port_count == $expected_port_count ]
191	check_err $? "Unexpected port count linecard $lc (got $port_count, expected $expected_port_count)"
192}
193
194provision_test()
195{
196	RET=0
197	local lc
198	local type
199	local state
200
201	lc=$LC_SLOT
202	supported_types_check $lc
203	state=$(lc_state_get $lc)
204	check_err $? "Failed to get state of linecard $lc"
205	if [[ "$state" != "unprovisioned" ]]; then
206		unprovision_one $lc
207	fi
208	provision_one $lc $LC_16X100G_TYPE
209	ports_check $lc $LC_16X100G_PORT_COUNT
210	log_test "Provision"
211}
212
213ACTIVATION_TIMEOUT=20000 # ms
214
215interface_check()
216{
217	ip link set $h1 up
218	ip link set $h2 up
219	ifaces_upped=true
220	setup_wait
221}
222
223activation_16x100G_test()
224{
225	RET=0
226	local lc
227	local type
228	local state
229
230	lc=$LC_SLOT
231	type=$LC_16X100G_TYPE
232
233	unprovision_one $lc
234	provision_one $lc $type
235	state=$(lc_wait_until_state_becomes $lc "active" \
236		$ACTIVATION_TIMEOUT)
237	check_err $? "Failed to get linecard $lc activated (timeout)"
238
239	interface_check
240
241	log_test "Activation 16x100G"
242}
243
244setup_prepare()
245{
246	local lc_num=$(devlink lc show -j | jq -e -r ".[][\"$DEVLINK_DEV\"] |length")
247	if [[ $? -ne 0 ]] || [[ $lc_num -eq 0 ]]; then
248		echo "SKIP: No linecard support found"
249		exit $ksft_skip
250	fi
251
252	if [ -z "$LC_SLOT" ]; then
253		echo "SKIP: \"LC_SLOT\" variable not provided"
254		exit $ksft_skip
255	fi
256
257	# Interfaces are not present during the script start,
258	# that's why we define NUM_NETIFS here so dummy
259	# implicit veth pairs are not created.
260	NUM_NETIFS=2
261	h1=${NETIFS[p1]}
262	h2=${NETIFS[p2]}
263	ifaces_upped=false
264}
265
266cleanup()
267{
268	if [ "$ifaces_upped" = true ] ; then
269		ip link set $h1 down
270		ip link set $h2 down
271	fi
272}
273
274trap cleanup EXIT
275
276setup_prepare
277
278tests_run
279
280exit $EXIT_STATUS
281