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	log_test "regions test"
145}
146
147reload_test()
148{
149	RET=0
150
151	devlink dev reload $DL_HANDLE
152	check_err $? "Failed to reload"
153
154	echo "y"> $DEBUGFS_DIR/fail_reload
155	check_err $? "Failed to setup devlink reload to fail"
156
157	devlink dev reload $DL_HANDLE
158	check_fail $? "Unexpected success of devlink reload"
159
160	echo "n"> $DEBUGFS_DIR/fail_reload
161	check_err $? "Failed to setup devlink reload not to fail"
162
163	devlink dev reload $DL_HANDLE
164	check_err $? "Failed to reload after set not to fail"
165
166	echo "y"> $DEBUGFS_DIR/dont_allow_reload
167	check_err $? "Failed to forbid devlink reload"
168
169	devlink dev reload $DL_HANDLE
170	check_fail $? "Unexpected success of devlink reload"
171
172	echo "n"> $DEBUGFS_DIR/dont_allow_reload
173	check_err $? "Failed to re-enable devlink reload"
174
175	devlink dev reload $DL_HANDLE
176	check_err $? "Failed to reload after re-enable"
177
178	log_test "reload test"
179}
180
181netns_reload_test()
182{
183	RET=0
184
185	ip netns add testns1
186	check_err $? "Failed add netns \"testns1\""
187	ip netns add testns2
188	check_err $? "Failed add netns \"testns2\""
189
190	devlink dev reload $DL_HANDLE netns testns1
191	check_err $? "Failed to reload into netns \"testns1\""
192
193	devlink -N testns1 dev reload $DL_HANDLE netns testns2
194	check_err $? "Failed to reload from netns \"testns1\" into netns \"testns2\""
195
196	ip netns del testns2
197	ip netns del testns1
198
199	log_test "netns reload test"
200}
201
202DUMMYDEV="dummytest"
203
204res_val_get()
205{
206	local netns=$1
207	local parentname=$2
208	local name=$3
209	local type=$4
210
211	cmd_jq "devlink -N $netns resource show $DL_HANDLE -j" \
212	       ".[][][] | select(.name == \"$parentname\").resources[] \
213	        | select(.name == \"$name\").$type"
214}
215
216resource_test()
217{
218	RET=0
219
220	ip netns add testns1
221	check_err $? "Failed add netns \"testns1\""
222	ip netns add testns2
223	check_err $? "Failed add netns \"testns2\""
224
225	devlink dev reload $DL_HANDLE netns testns1
226	check_err $? "Failed to reload into netns \"testns1\""
227
228	# Create dummy dev to add the address and routes on.
229
230	ip -n testns1 link add name $DUMMYDEV type dummy
231	check_err $? "Failed create dummy device"
232	ip -n testns1 link set $DUMMYDEV up
233	check_err $? "Failed bring up dummy device"
234	ip -n testns1 a a 192.0.1.1/24 dev $DUMMYDEV
235	check_err $? "Failed add an IP address to dummy device"
236
237	local occ=$(res_val_get testns1 IPv4 fib occ)
238	local limit=$((occ+1))
239
240	# Set fib size limit to handle one another route only.
241
242	devlink -N testns1 resource set $DL_HANDLE path IPv4/fib size $limit
243	check_err $? "Failed to set IPv4/fib resource size"
244	local size_new=$(res_val_get testns1 IPv4 fib size_new)
245	[ "$size_new" -eq "$limit" ]
246	check_err $? "Unexpected \"size_new\" value (got $size_new, expected $limit)"
247
248	devlink -N testns1 dev reload $DL_HANDLE
249	check_err $? "Failed to reload"
250	local size=$(res_val_get testns1 IPv4 fib size)
251	[ "$size" -eq "$limit" ]
252	check_err $? "Unexpected \"size\" value (got $size, expected $limit)"
253
254	# Insert 2 routes, the first is going to be inserted,
255	# the second is expected to fail to be inserted.
256
257	ip -n testns1 r a 192.0.2.0/24 via 192.0.1.2
258	check_err $? "Failed to add route"
259
260	ip -n testns1 r a 192.0.3.0/24 via 192.0.1.2
261	check_fail $? "Unexpected successful route add over limit"
262
263	# Now create another dummy in second network namespace and
264	# insert two routes. That is over the limit of the netdevsim
265	# instance in the first namespace. Move the netdevsim instance
266	# into the second namespace and expect it to fail.
267
268	ip -n testns2 link add name $DUMMYDEV type dummy
269	check_err $? "Failed create dummy device"
270	ip -n testns2 link set $DUMMYDEV up
271	check_err $? "Failed bring up dummy device"
272	ip -n testns2 a a 192.0.1.1/24 dev $DUMMYDEV
273	check_err $? "Failed add an IP address to dummy device"
274	ip -n testns2 r a 192.0.2.0/24 via 192.0.1.2
275	check_err $? "Failed to add route"
276	ip -n testns2 r a 192.0.3.0/24 via 192.0.1.2
277	check_err $? "Failed to add route"
278
279	devlink -N testns1 dev reload $DL_HANDLE netns testns2
280	check_fail $? "Unexpected successful reload from netns \"testns1\" into netns \"testns2\""
281
282	devlink -N testns2 resource set $DL_HANDLE path IPv4/fib size ' -1'
283	check_err $? "Failed to reset IPv4/fib resource size"
284
285	devlink -N testns2 dev reload $DL_HANDLE netns 1
286	check_err $? "Failed to reload devlink back"
287
288	ip netns del testns2
289	ip netns del testns1
290
291	log_test "resource test"
292}
293
294info_get()
295{
296	local name=$1
297
298	cmd_jq "devlink dev info $DL_HANDLE -j" ".[][][\"$name\"]" "-e"
299}
300
301dev_info_test()
302{
303	RET=0
304
305	driver=$(info_get "driver")
306	check_err $? "Failed to get driver name"
307	[ "$driver" == "netdevsim" ]
308	check_err $? "Unexpected driver name $driver"
309
310	log_test "dev_info test"
311}
312
313empty_reporter_test()
314{
315	RET=0
316
317	devlink health show $DL_HANDLE reporter empty >/dev/null
318	check_err $? "Failed show empty reporter"
319
320	devlink health dump show $DL_HANDLE reporter empty >/dev/null
321	check_err $? "Failed show dump of empty reporter"
322
323	devlink health diagnose $DL_HANDLE reporter empty >/dev/null
324	check_err $? "Failed diagnose empty reporter"
325
326	devlink health recover $DL_HANDLE reporter empty
327	check_err $? "Failed recover empty reporter"
328
329	log_test "empty reporter test"
330}
331
332check_reporter_info()
333{
334	local name=$1
335	local expected_state=$2
336	local expected_error=$3
337	local expected_recover=$4
338	local expected_grace_period=$5
339	local expected_auto_recover=$6
340
341	local show=$(devlink health show $DL_HANDLE reporter $name -j | jq -e -r ".[][][]")
342	check_err $? "Failed show $name reporter"
343
344	local state=$(echo $show | jq -r ".state")
345	[ "$state" == "$expected_state" ]
346	check_err $? "Unexpected \"state\" value (got $state, expected $expected_state)"
347
348	local error=$(echo $show | jq -r ".error")
349	[ "$error" == "$expected_error" ]
350	check_err $? "Unexpected \"error\" value (got $error, expected $expected_error)"
351
352	local recover=`echo $show | jq -r ".recover"`
353	[ "$recover" == "$expected_recover" ]
354	check_err $? "Unexpected \"recover\" value (got $recover, expected $expected_recover)"
355
356	local grace_period=$(echo $show | jq -r ".grace_period")
357	check_err $? "Failed get $name reporter grace_period"
358	[ "$grace_period" == "$expected_grace_period" ]
359	check_err $? "Unexpected \"grace_period\" value (got $grace_period, expected $expected_grace_period)"
360
361	local auto_recover=$(echo $show | jq -r ".auto_recover")
362	[ "$auto_recover" == "$expected_auto_recover" ]
363	check_err $? "Unexpected \"auto_recover\" value (got $auto_recover, expected $expected_auto_recover)"
364}
365
366dummy_reporter_test()
367{
368	RET=0
369
370	check_reporter_info dummy healthy 0 0 0 false
371
372	local BREAK_MSG="foo bar"
373	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
374	check_err $? "Failed to break dummy reporter"
375
376	check_reporter_info dummy error 1 0 0 false
377
378	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
379	check_err $? "Failed show dump of dummy reporter"
380
381	local dump_break_msg=$(echo $dump | jq -r ".break_message")
382	[ "$dump_break_msg" == "$BREAK_MSG" ]
383	check_err $? "Unexpected dump break message value (got $dump_break_msg, expected $BREAK_MSG)"
384
385	devlink health dump clear $DL_HANDLE reporter dummy
386	check_err $? "Failed clear dump of dummy reporter"
387
388	devlink health recover $DL_HANDLE reporter dummy
389	check_err $? "Failed recover dummy reporter"
390
391	check_reporter_info dummy healthy 1 1 0 false
392
393	devlink health set $DL_HANDLE reporter dummy auto_recover true
394	check_err $? "Failed to dummy reporter auto_recover option"
395
396	check_reporter_info dummy healthy 1 1 0 true
397
398	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
399	check_err $? "Failed to break dummy reporter"
400
401	check_reporter_info dummy healthy 2 2 0 true
402
403	local diagnose=$(devlink health diagnose $DL_HANDLE reporter dummy -j -p)
404	check_err $? "Failed show diagnose of dummy reporter"
405
406	local rcvrd_break_msg=$(echo $diagnose | jq -r ".recovered_break_message")
407	[ "$rcvrd_break_msg" == "$BREAK_MSG" ]
408	check_err $? "Unexpected recovered break message value (got $rcvrd_break_msg, expected $BREAK_MSG)"
409
410	devlink health set $DL_HANDLE reporter dummy grace_period 10
411	check_err $? "Failed to dummy reporter grace_period option"
412
413	check_reporter_info dummy healthy 2 2 10 true
414
415	echo "Y"> $DEBUGFS_DIR/health/fail_recover
416	check_err $? "Failed set dummy reporter recovery to fail"
417
418	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
419	check_fail $? "Unexpected success of dummy reporter break"
420
421	check_reporter_info dummy error 3 2 10 true
422
423	devlink health recover $DL_HANDLE reporter dummy
424	check_fail $? "Unexpected success of dummy reporter recover"
425
426	echo "N"> $DEBUGFS_DIR/health/fail_recover
427	check_err $? "Failed set dummy reporter recovery to be successful"
428
429	devlink health recover $DL_HANDLE reporter dummy
430	check_err $? "Failed recover dummy reporter"
431
432	check_reporter_info dummy healthy 3 3 10 true
433
434	echo 8192> $DEBUGFS_DIR/health/binary_len
435	check_fail $? "Failed set dummy reporter binary len to 8192"
436
437	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
438	check_err $? "Failed show dump of dummy reporter"
439
440	devlink health dump clear $DL_HANDLE reporter dummy
441	check_err $? "Failed clear dump of dummy reporter"
442
443	log_test "dummy reporter test"
444}
445
446setup_prepare()
447{
448	modprobe netdevsim
449	echo "$BUS_ADDR $PORT_COUNT" > /sys/bus/netdevsim/new_device
450	while [ ! -d $SYSFS_NET_DIR ] ; do :; done
451}
452
453cleanup()
454{
455	pre_cleanup
456	echo "$BUS_ADDR" > /sys/bus/netdevsim/del_device
457	modprobe -r netdevsim
458}
459
460trap cleanup EXIT
461
462setup_prepare
463
464tests_run
465
466exit $EXIT_STATUS
467