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
87lc_nested_devlink_dev_get()
88{
89	local lc=$1
90
91	devlink lc show $DEVLINK_DEV lc $lc -j | jq -e -r ".[][][].nested_devlink"
92}
93
94PROV_UNPROV_TIMEOUT=8000 # ms
95POST_PROV_ACT_TIMEOUT=2000 # ms
96PROV_PORTS_INSTANTIATION_TIMEOUT=15000 # ms
97
98unprovision_one()
99{
100	local lc=$1
101	local state
102
103	state=$(lc_state_get $lc)
104	check_err $? "Failed to get state of linecard $lc"
105	if [[ "$state" == "unprovisioned" ]]; then
106		return
107	fi
108
109	log_info "Unprovisioning linecard $lc"
110
111	devlink lc set $DEVLINK_DEV lc $lc notype
112	check_err $? "Failed to trigger linecard $lc unprovisioning"
113
114	state=$(lc_wait_until_state_changes $lc "unprovisioning" \
115		$PROV_UNPROV_TIMEOUT)
116	check_err $? "Failed to unprovision linecard $lc (timeout)"
117
118	[ "$state" == "unprovisioned" ]
119	check_err $? "Failed to unprovision linecard $lc (state=$state)"
120}
121
122provision_one()
123{
124	local lc=$1
125	local type=$2
126	local state
127
128	log_info "Provisioning linecard $lc"
129
130	devlink lc set $DEVLINK_DEV lc $lc type $type
131	check_err $? "Failed trigger linecard $lc provisioning"
132
133	state=$(lc_wait_until_state_changes $lc "provisioning" \
134		$PROV_UNPROV_TIMEOUT)
135	check_err $? "Failed to provision linecard $lc (timeout)"
136
137	[ "$state" == "provisioned" ] || [ "$state" == "active" ]
138	check_err $? "Failed to provision linecard $lc (state=$state)"
139
140	provisioned_type=$(devlink lc show $DEVLINK_DEV lc $lc -j | jq -e -r ".[][][].type")
141	[ "$provisioned_type" == "$type" ]
142	check_err $? "Wrong provision type returned for linecard $lc (got \"$provisioned_type\", expected \"$type\")"
143
144	# Wait for possible activation to make sure the state
145	# won't change after return from this function.
146	state=$(lc_wait_until_state_becomes $lc "active" \
147		$POST_PROV_ACT_TIMEOUT)
148}
149
150unprovision_test()
151{
152	RET=0
153	local lc
154
155	lc=$LC_SLOT
156	unprovision_one $lc
157	log_test "Unprovision"
158}
159
160LC_16X100G_TYPE="16x100G"
161LC_16X100G_PORT_COUNT=16
162
163supported_types_check()
164{
165	local lc=$1
166	local supported_types_count
167	local type_index
168	local lc_16x100_found=false
169
170	supported_types_count=$(devlink lc show $DEVLINK_DEV lc $lc -j | \
171				jq -e -r ".[][][].supported_types | length")
172	[ $supported_types_count != 0 ]
173	check_err $? "No supported types found for linecard $lc"
174	for (( type_index=0; type_index<$supported_types_count; type_index++ ))
175	do
176		type=$(devlink lc show $DEVLINK_DEV lc $lc -j | \
177		       jq -e -r ".[][][].supported_types[$type_index]")
178		if [[ "$type" == "$LC_16X100G_TYPE" ]]; then
179			lc_16x100_found=true
180			break
181		fi
182	done
183	[ $lc_16x100_found = true ]
184	check_err $? "16X100G not found between supported types of linecard $lc"
185}
186
187ports_check()
188{
189	local lc=$1
190	local expected_port_count=$2
191	local port_count
192
193	port_count=$(lc_wait_until_port_count_is $lc $expected_port_count \
194		$PROV_PORTS_INSTANTIATION_TIMEOUT)
195	[ $port_count != 0 ]
196	check_err $? "No port associated with linecard $lc"
197	[ $port_count == $expected_port_count ]
198	check_err $? "Unexpected port count linecard $lc (got $port_count, expected $expected_port_count)"
199}
200
201lc_dev_info_provisioned_check()
202{
203	local lc=$1
204	local nested_devlink_dev=$2
205	local fixed_hw_revision
206	local running_ini_version
207
208	fixed_hw_revision=$(devlink dev info $nested_devlink_dev -j | \
209			    jq -e -r '.[][].versions.fixed."hw.revision"')
210	check_err $? "Failed to get linecard $lc fixed.hw.revision"
211	log_info "Linecard $lc fixed.hw.revision: \"$fixed_hw_revision\""
212	running_ini_version=$(devlink dev info $nested_devlink_dev -j | \
213			      jq -e -r '.[][].versions.running."ini.version"')
214	check_err $? "Failed to get linecard $lc running.ini.version"
215	log_info "Linecard $lc running.ini.version: \"$running_ini_version\""
216}
217
218provision_test()
219{
220	RET=0
221	local lc
222	local type
223	local state
224	local nested_devlink_dev
225
226	lc=$LC_SLOT
227	supported_types_check $lc
228	state=$(lc_state_get $lc)
229	check_err $? "Failed to get state of linecard $lc"
230	if [[ "$state" != "unprovisioned" ]]; then
231		unprovision_one $lc
232	fi
233	provision_one $lc $LC_16X100G_TYPE
234	ports_check $lc $LC_16X100G_PORT_COUNT
235
236	nested_devlink_dev=$(lc_nested_devlink_dev_get $lc)
237	check_err $? "Failed to get nested devlink handle of linecard $lc"
238	lc_dev_info_provisioned_check $lc $nested_devlink_dev
239
240	log_test "Provision"
241}
242
243ACTIVATION_TIMEOUT=20000 # ms
244
245interface_check()
246{
247	ip link set $h1 up
248	ip link set $h2 up
249	ifaces_upped=true
250	setup_wait
251}
252
253lc_dev_info_active_check()
254{
255	local lc=$1
256	local nested_devlink_dev=$2
257	local fixed_device_fw_psid
258	local running_device_fw
259
260	fixed_device_fw_psid=$(devlink dev info $nested_devlink_dev -j | \
261			       jq -e -r ".[][].versions.fixed" | \
262			       jq -e -r '."fw.psid"')
263	check_err $? "Failed to get linecard $lc fixed fw PSID"
264	log_info "Linecard $lc fixed.fw.psid: \"$fixed_device_fw_psid\""
265
266	running_device_fw=$(devlink dev info $nested_devlink_dev -j | \
267			    jq -e -r ".[][].versions.running.fw")
268	check_err $? "Failed to get linecard $lc running.fw.version"
269	log_info "Linecard $lc running.fw: \"$running_device_fw\""
270}
271
272activation_16x100G_test()
273{
274	RET=0
275	local lc
276	local type
277	local state
278	local nested_devlink_dev
279
280	lc=$LC_SLOT
281	type=$LC_16X100G_TYPE
282
283	unprovision_one $lc
284	provision_one $lc $type
285	state=$(lc_wait_until_state_becomes $lc "active" \
286		$ACTIVATION_TIMEOUT)
287	check_err $? "Failed to get linecard $lc activated (timeout)"
288
289	interface_check
290
291	nested_devlink_dev=$(lc_nested_devlink_dev_get $lc)
292	check_err $? "Failed to get nested devlink handle of linecard $lc"
293	lc_dev_info_active_check $lc $nested_devlink_dev
294
295	log_test "Activation 16x100G"
296}
297
298setup_prepare()
299{
300	local lc_num=$(devlink lc show -j | jq -e -r ".[][\"$DEVLINK_DEV\"] |length")
301	if [[ $? -ne 0 ]] || [[ $lc_num -eq 0 ]]; then
302		echo "SKIP: No linecard support found"
303		exit $ksft_skip
304	fi
305
306	if [ -z "$LC_SLOT" ]; then
307		echo "SKIP: \"LC_SLOT\" variable not provided"
308		exit $ksft_skip
309	fi
310
311	# Interfaces are not present during the script start,
312	# that's why we define NUM_NETIFS here so dummy
313	# implicit veth pairs are not created.
314	NUM_NETIFS=2
315	h1=${NETIFS[p1]}
316	h2=${NETIFS[p2]}
317	ifaces_upped=false
318}
319
320cleanup()
321{
322	if [ "$ifaces_upped" = true ] ; then
323		ip link set $h1 down
324		ip link set $h2 down
325	fi
326}
327
328trap cleanup EXIT
329
330setup_prepare
331
332tests_run
333
334exit $EXIT_STATUS
335