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