1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4lib_dir=$(dirname $0)/../../../net/forwarding
5
6ALL_TESTS="fw_flash_test params_test regions_test reload_test \
7	   netns_reload_test resource_test dev_info_test \
8	   empty_reporter_test dummy_reporter_test"
9NUM_NETIFS=0
10source $lib_dir/lib.sh
11
12BUS_ADDR=10
13PORT_COUNT=4
14DEV_NAME=netdevsim$BUS_ADDR
15SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV_NAME/net/
16DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV_NAME/
17DL_HANDLE=netdevsim/$DEV_NAME
18
19fw_flash_test()
20{
21	RET=0
22
23	devlink dev flash $DL_HANDLE file dummy
24	check_err $? "Failed to flash with status updates on"
25
26	echo "n"> $DEBUGFS_DIR/fw_update_status
27	check_err $? "Failed to disable status updates"
28
29	devlink dev flash $DL_HANDLE file dummy
30	check_err $? "Failed to flash with status updates off"
31
32	log_test "fw flash test"
33}
34
35param_get()
36{
37	local name=$1
38
39	cmd_jq "devlink dev param show $DL_HANDLE name $name -j" \
40	       '.[][][].values[] | select(.cmode == "driverinit").value'
41}
42
43param_set()
44{
45	local name=$1
46	local value=$2
47
48	devlink dev param set $DL_HANDLE name $name cmode driverinit value $value
49}
50
51check_value()
52{
53	local name=$1
54	local phase_name=$2
55	local expected_param_value=$3
56	local expected_debugfs_value=$4
57	local value
58
59	value=$(param_get $name)
60	check_err $? "Failed to get $name param value"
61	[ "$value" == "$expected_param_value" ]
62	check_err $? "Unexpected $phase_name $name param value"
63	value=$(<$DEBUGFS_DIR/$name)
64	check_err $? "Failed to get $name debugfs value"
65	[ "$value" == "$expected_debugfs_value" ]
66	check_err $? "Unexpected $phase_name $name debugfs value"
67}
68
69params_test()
70{
71	RET=0
72
73	local max_macs
74	local test1
75
76	check_value max_macs initial 32 32
77	check_value test1 initial true Y
78
79	param_set max_macs 16
80	check_err $? "Failed to set max_macs param value"
81	param_set test1 false
82	check_err $? "Failed to set test1 param value"
83
84	check_value max_macs post-set 16 32
85	check_value test1 post-set false Y
86
87	devlink dev reload $DL_HANDLE
88
89	check_value max_macs post-reload 16 16
90	check_value test1 post-reload false N
91
92	log_test "params test"
93}
94
95check_region_size()
96{
97	local name=$1
98	local size
99
100	size=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].size')
101	check_err $? "Failed to get $name region size"
102	[ $size -eq 32768 ]
103	check_err $? "Invalid $name region size"
104}
105
106check_region_snapshot_count()
107{
108	local name=$1
109	local phase_name=$2
110	local expected_count=$3
111	local count
112
113	count=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].snapshot | length')
114	[ $count -eq $expected_count ]
115	check_err $? "Unexpected $phase_name snapshot count"
116}
117
118regions_test()
119{
120	RET=0
121
122	local count
123
124	check_region_size dummy
125	check_region_snapshot_count dummy initial 0
126
127	echo ""> $DEBUGFS_DIR/take_snapshot
128	check_err $? "Failed to take first dummy region snapshot"
129	check_region_snapshot_count dummy post-first-snapshot 1
130
131	echo ""> $DEBUGFS_DIR/take_snapshot
132	check_err $? "Failed to take second dummy region snapshot"
133	check_region_snapshot_count dummy post-second-snapshot 2
134
135	echo ""> $DEBUGFS_DIR/take_snapshot
136	check_err $? "Failed to take third dummy region snapshot"
137	check_region_snapshot_count dummy post-third-snapshot 3
138
139	devlink region del $DL_HANDLE/dummy snapshot 1
140	check_err $? "Failed to delete first dummy region snapshot"
141
142	check_region_snapshot_count dummy post-first-delete 2
143
144	devlink region new $DL_HANDLE/dummy snapshot 25
145	check_err $? "Failed to create a new snapshot with id 25"
146
147	check_region_snapshot_count dummy post-first-request 3
148
149	devlink region del $DL_HANDLE/dummy snapshot 25
150	check_err $? "Failed to delete snapshot with id 25"
151
152	check_region_snapshot_count dummy post-second-delete 2
153
154	log_test "regions test"
155}
156
157reload_test()
158{
159	RET=0
160
161	devlink dev reload $DL_HANDLE
162	check_err $? "Failed to reload"
163
164	echo "y"> $DEBUGFS_DIR/fail_reload
165	check_err $? "Failed to setup devlink reload to fail"
166
167	devlink dev reload $DL_HANDLE
168	check_fail $? "Unexpected success of devlink reload"
169
170	echo "n"> $DEBUGFS_DIR/fail_reload
171	check_err $? "Failed to setup devlink reload not to fail"
172
173	devlink dev reload $DL_HANDLE
174	check_err $? "Failed to reload after set not to fail"
175
176	echo "y"> $DEBUGFS_DIR/dont_allow_reload
177	check_err $? "Failed to forbid devlink reload"
178
179	devlink dev reload $DL_HANDLE
180	check_fail $? "Unexpected success of devlink reload"
181
182	echo "n"> $DEBUGFS_DIR/dont_allow_reload
183	check_err $? "Failed to re-enable devlink reload"
184
185	devlink dev reload $DL_HANDLE
186	check_err $? "Failed to reload after re-enable"
187
188	log_test "reload test"
189}
190
191netns_reload_test()
192{
193	RET=0
194
195	ip netns add testns1
196	check_err $? "Failed add netns \"testns1\""
197	ip netns add testns2
198	check_err $? "Failed add netns \"testns2\""
199
200	devlink dev reload $DL_HANDLE netns testns1
201	check_err $? "Failed to reload into netns \"testns1\""
202
203	devlink -N testns1 dev reload $DL_HANDLE netns testns2
204	check_err $? "Failed to reload from netns \"testns1\" into netns \"testns2\""
205
206	ip netns del testns2
207	ip netns del testns1
208
209	log_test "netns reload test"
210}
211
212DUMMYDEV="dummytest"
213
214res_val_get()
215{
216	local netns=$1
217	local parentname=$2
218	local name=$3
219	local type=$4
220
221	cmd_jq "devlink -N $netns resource show $DL_HANDLE -j" \
222	       ".[][][] | select(.name == \"$parentname\").resources[] \
223	        | select(.name == \"$name\").$type"
224}
225
226resource_test()
227{
228	RET=0
229
230	ip netns add testns1
231	check_err $? "Failed add netns \"testns1\""
232	ip netns add testns2
233	check_err $? "Failed add netns \"testns2\""
234
235	devlink dev reload $DL_HANDLE netns testns1
236	check_err $? "Failed to reload into netns \"testns1\""
237
238	# Create dummy dev to add the address and routes on.
239
240	ip -n testns1 link add name $DUMMYDEV type dummy
241	check_err $? "Failed create dummy device"
242	ip -n testns1 link set $DUMMYDEV up
243	check_err $? "Failed bring up dummy device"
244	ip -n testns1 a a 192.0.1.1/24 dev $DUMMYDEV
245	check_err $? "Failed add an IP address to dummy device"
246
247	local occ=$(res_val_get testns1 IPv4 fib occ)
248	local limit=$((occ+1))
249
250	# Set fib size limit to handle one another route only.
251
252	devlink -N testns1 resource set $DL_HANDLE path IPv4/fib size $limit
253	check_err $? "Failed to set IPv4/fib resource size"
254	local size_new=$(res_val_get testns1 IPv4 fib size_new)
255	[ "$size_new" -eq "$limit" ]
256	check_err $? "Unexpected \"size_new\" value (got $size_new, expected $limit)"
257
258	devlink -N testns1 dev reload $DL_HANDLE
259	check_err $? "Failed to reload"
260	local size=$(res_val_get testns1 IPv4 fib size)
261	[ "$size" -eq "$limit" ]
262	check_err $? "Unexpected \"size\" value (got $size, expected $limit)"
263
264	# Insert 2 routes, the first is going to be inserted,
265	# the second is expected to fail to be inserted.
266
267	ip -n testns1 r a 192.0.2.0/24 via 192.0.1.2
268	check_err $? "Failed to add route"
269
270	ip -n testns1 r a 192.0.3.0/24 via 192.0.1.2
271	check_fail $? "Unexpected successful route add over limit"
272
273	# Now create another dummy in second network namespace and
274	# insert two routes. That is over the limit of the netdevsim
275	# instance in the first namespace. Move the netdevsim instance
276	# into the second namespace and expect it to fail.
277
278	ip -n testns2 link add name $DUMMYDEV type dummy
279	check_err $? "Failed create dummy device"
280	ip -n testns2 link set $DUMMYDEV up
281	check_err $? "Failed bring up dummy device"
282	ip -n testns2 a a 192.0.1.1/24 dev $DUMMYDEV
283	check_err $? "Failed add an IP address to dummy device"
284	ip -n testns2 r a 192.0.2.0/24 via 192.0.1.2
285	check_err $? "Failed to add route"
286	ip -n testns2 r a 192.0.3.0/24 via 192.0.1.2
287	check_err $? "Failed to add route"
288
289	devlink -N testns1 dev reload $DL_HANDLE netns testns2
290	check_fail $? "Unexpected successful reload from netns \"testns1\" into netns \"testns2\""
291
292	devlink -N testns2 resource set $DL_HANDLE path IPv4/fib size ' -1'
293	check_err $? "Failed to reset IPv4/fib resource size"
294
295	devlink -N testns2 dev reload $DL_HANDLE netns 1
296	check_err $? "Failed to reload devlink back"
297
298	ip netns del testns2
299	ip netns del testns1
300
301	log_test "resource test"
302}
303
304info_get()
305{
306	local name=$1
307
308	cmd_jq "devlink dev info $DL_HANDLE -j" ".[][][\"$name\"]" "-e"
309}
310
311dev_info_test()
312{
313	RET=0
314
315	driver=$(info_get "driver")
316	check_err $? "Failed to get driver name"
317	[ "$driver" == "netdevsim" ]
318	check_err $? "Unexpected driver name $driver"
319
320	log_test "dev_info test"
321}
322
323empty_reporter_test()
324{
325	RET=0
326
327	devlink health show $DL_HANDLE reporter empty >/dev/null
328	check_err $? "Failed show empty reporter"
329
330	devlink health dump show $DL_HANDLE reporter empty >/dev/null
331	check_err $? "Failed show dump of empty reporter"
332
333	devlink health diagnose $DL_HANDLE reporter empty >/dev/null
334	check_err $? "Failed diagnose empty reporter"
335
336	devlink health recover $DL_HANDLE reporter empty
337	check_err $? "Failed recover empty reporter"
338
339	log_test "empty reporter test"
340}
341
342check_reporter_info()
343{
344	local name=$1
345	local expected_state=$2
346	local expected_error=$3
347	local expected_recover=$4
348	local expected_grace_period=$5
349	local expected_auto_recover=$6
350
351	local show=$(devlink health show $DL_HANDLE reporter $name -j | jq -e -r ".[][][]")
352	check_err $? "Failed show $name reporter"
353
354	local state=$(echo $show | jq -r ".state")
355	[ "$state" == "$expected_state" ]
356	check_err $? "Unexpected \"state\" value (got $state, expected $expected_state)"
357
358	local error=$(echo $show | jq -r ".error")
359	[ "$error" == "$expected_error" ]
360	check_err $? "Unexpected \"error\" value (got $error, expected $expected_error)"
361
362	local recover=`echo $show | jq -r ".recover"`
363	[ "$recover" == "$expected_recover" ]
364	check_err $? "Unexpected \"recover\" value (got $recover, expected $expected_recover)"
365
366	local grace_period=$(echo $show | jq -r ".grace_period")
367	check_err $? "Failed get $name reporter grace_period"
368	[ "$grace_period" == "$expected_grace_period" ]
369	check_err $? "Unexpected \"grace_period\" value (got $grace_period, expected $expected_grace_period)"
370
371	local auto_recover=$(echo $show | jq -r ".auto_recover")
372	[ "$auto_recover" == "$expected_auto_recover" ]
373	check_err $? "Unexpected \"auto_recover\" value (got $auto_recover, expected $expected_auto_recover)"
374}
375
376dummy_reporter_test()
377{
378	RET=0
379
380	check_reporter_info dummy healthy 0 0 0 true
381
382	devlink health set $DL_HANDLE reporter dummy auto_recover false
383	check_err $? "Failed to dummy reporter auto_recover option"
384
385	check_reporter_info dummy healthy 0 0 0 false
386
387	local BREAK_MSG="foo bar"
388	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
389	check_err $? "Failed to break dummy reporter"
390
391	check_reporter_info dummy error 1 0 0 false
392
393	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
394	check_err $? "Failed show dump of dummy reporter"
395
396	local dump_break_msg=$(echo $dump | jq -r ".break_message")
397	[ "$dump_break_msg" == "$BREAK_MSG" ]
398	check_err $? "Unexpected dump break message value (got $dump_break_msg, expected $BREAK_MSG)"
399
400	devlink health dump clear $DL_HANDLE reporter dummy
401	check_err $? "Failed clear dump of dummy reporter"
402
403	devlink health recover $DL_HANDLE reporter dummy
404	check_err $? "Failed recover dummy reporter"
405
406	check_reporter_info dummy healthy 1 1 0 false
407
408	devlink health set $DL_HANDLE reporter dummy auto_recover true
409	check_err $? "Failed to dummy reporter auto_recover option"
410
411	check_reporter_info dummy healthy 1 1 0 true
412
413	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
414	check_err $? "Failed to break dummy reporter"
415
416	check_reporter_info dummy healthy 2 2 0 true
417
418	local diagnose=$(devlink health diagnose $DL_HANDLE reporter dummy -j -p)
419	check_err $? "Failed show diagnose of dummy reporter"
420
421	local rcvrd_break_msg=$(echo $diagnose | jq -r ".recovered_break_message")
422	[ "$rcvrd_break_msg" == "$BREAK_MSG" ]
423	check_err $? "Unexpected recovered break message value (got $rcvrd_break_msg, expected $BREAK_MSG)"
424
425	devlink health set $DL_HANDLE reporter dummy grace_period 10
426	check_err $? "Failed to dummy reporter grace_period option"
427
428	check_reporter_info dummy healthy 2 2 10 true
429
430	echo "Y"> $DEBUGFS_DIR/health/fail_recover
431	check_err $? "Failed set dummy reporter recovery to fail"
432
433	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
434	check_fail $? "Unexpected success of dummy reporter break"
435
436	check_reporter_info dummy error 3 2 10 true
437
438	devlink health recover $DL_HANDLE reporter dummy
439	check_fail $? "Unexpected success of dummy reporter recover"
440
441	echo "N"> $DEBUGFS_DIR/health/fail_recover
442	check_err $? "Failed set dummy reporter recovery to be successful"
443
444	devlink health recover $DL_HANDLE reporter dummy
445	check_err $? "Failed recover dummy reporter"
446
447	check_reporter_info dummy healthy 3 3 10 true
448
449	echo 8192> $DEBUGFS_DIR/health/binary_len
450	check_fail $? "Failed set dummy reporter binary len to 8192"
451
452	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
453	check_err $? "Failed show dump of dummy reporter"
454
455	devlink health dump clear $DL_HANDLE reporter dummy
456	check_err $? "Failed clear dump of dummy reporter"
457
458	log_test "dummy reporter test"
459}
460
461setup_prepare()
462{
463	modprobe netdevsim
464	echo "$BUS_ADDR $PORT_COUNT" > /sys/bus/netdevsim/new_device
465	while [ ! -d $SYSFS_NET_DIR ] ; do :; done
466}
467
468cleanup()
469{
470	pre_cleanup
471	echo "$BUS_ADDR" > /sys/bus/netdevsim/del_device
472	modprobe -r netdevsim
473}
474
475trap cleanup EXIT
476
477setup_prepare
478
479tests_run
480
481exit $EXIT_STATUS
482