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 rate_test"
9NUM_NETIFS=0
10source $lib_dir/lib.sh
11
12BUS_ADDR=10
13PORT_COUNT=4
14VF_COUNT=4
15DEV_NAME=netdevsim$BUS_ADDR
16SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV_NAME/net/
17DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV_NAME/
18DL_HANDLE=netdevsim/$DEV_NAME
19
20fw_flash_test()
21{
22	RET=0
23
24	devlink dev flash $DL_HANDLE file dummy
25	check_err $? "Failed to flash with status updates on"
26
27	devlink dev flash $DL_HANDLE file dummy component fw.mgmt
28	check_err $? "Failed to flash with component attribute"
29
30	devlink dev flash $DL_HANDLE file dummy overwrite settings
31	check_fail $? "Flash with overwrite settings should be rejected"
32
33	echo "1"> $DEBUGFS_DIR/fw_update_overwrite_mask
34	check_err $? "Failed to change allowed overwrite mask"
35
36	devlink dev flash $DL_HANDLE file dummy overwrite settings
37	check_err $? "Failed to flash with settings overwrite enabled"
38
39	devlink dev flash $DL_HANDLE file dummy overwrite identifiers
40	check_fail $? "Flash with overwrite settings should be identifiers"
41
42	echo "3"> $DEBUGFS_DIR/fw_update_overwrite_mask
43	check_err $? "Failed to change allowed overwrite mask"
44
45	devlink dev flash $DL_HANDLE file dummy overwrite identifiers overwrite settings
46	check_err $? "Failed to flash with settings and identifiers overwrite enabled"
47
48	echo "n"> $DEBUGFS_DIR/fw_update_status
49	check_err $? "Failed to disable status updates"
50
51	devlink dev flash $DL_HANDLE file dummy
52	check_err $? "Failed to flash with status updates off"
53
54	log_test "fw flash test"
55}
56
57param_get()
58{
59	local name=$1
60
61	cmd_jq "devlink dev param show $DL_HANDLE name $name -j" \
62	       '.[][][].values[] | select(.cmode == "driverinit").value'
63}
64
65param_set()
66{
67	local name=$1
68	local value=$2
69
70	devlink dev param set $DL_HANDLE name $name cmode driverinit value $value
71}
72
73check_value()
74{
75	local name=$1
76	local phase_name=$2
77	local expected_param_value=$3
78	local expected_debugfs_value=$4
79	local value
80
81	value=$(param_get $name)
82	check_err $? "Failed to get $name param value"
83	[ "$value" == "$expected_param_value" ]
84	check_err $? "Unexpected $phase_name $name param value"
85	value=$(<$DEBUGFS_DIR/$name)
86	check_err $? "Failed to get $name debugfs value"
87	[ "$value" == "$expected_debugfs_value" ]
88	check_err $? "Unexpected $phase_name $name debugfs value"
89}
90
91params_test()
92{
93	RET=0
94
95	local max_macs
96	local test1
97
98	check_value max_macs initial 32 32
99	check_value test1 initial true Y
100
101	param_set max_macs 16
102	check_err $? "Failed to set max_macs param value"
103	param_set test1 false
104	check_err $? "Failed to set test1 param value"
105
106	check_value max_macs post-set 16 32
107	check_value test1 post-set false Y
108
109	devlink dev reload $DL_HANDLE
110
111	check_value max_macs post-reload 16 16
112	check_value test1 post-reload false N
113
114	log_test "params test"
115}
116
117check_region_size()
118{
119	local name=$1
120	local size
121
122	size=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].size')
123	check_err $? "Failed to get $name region size"
124	[ $size -eq 32768 ]
125	check_err $? "Invalid $name region size"
126}
127
128check_region_snapshot_count()
129{
130	local name=$1
131	local phase_name=$2
132	local expected_count=$3
133	local count
134
135	count=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].snapshot | length')
136	[ $count -eq $expected_count ]
137	check_err $? "Unexpected $phase_name snapshot count"
138}
139
140regions_test()
141{
142	RET=0
143
144	local count
145
146	check_region_size dummy
147	check_region_snapshot_count dummy initial 0
148
149	echo ""> $DEBUGFS_DIR/take_snapshot
150	check_err $? "Failed to take first dummy region snapshot"
151	check_region_snapshot_count dummy post-first-snapshot 1
152
153	echo ""> $DEBUGFS_DIR/take_snapshot
154	check_err $? "Failed to take second dummy region snapshot"
155	check_region_snapshot_count dummy post-second-snapshot 2
156
157	echo ""> $DEBUGFS_DIR/take_snapshot
158	check_err $? "Failed to take third dummy region snapshot"
159	check_region_snapshot_count dummy post-third-snapshot 3
160
161	devlink region del $DL_HANDLE/dummy snapshot 1
162	check_err $? "Failed to delete first dummy region snapshot"
163
164	check_region_snapshot_count dummy post-first-delete 2
165
166	devlink region new $DL_HANDLE/dummy snapshot 25
167	check_err $? "Failed to create a new snapshot with id 25"
168
169	check_region_snapshot_count dummy post-first-request 3
170
171	devlink region dump $DL_HANDLE/dummy snapshot 25 >> /dev/null
172	check_err $? "Failed to dump snapshot with id 25"
173
174	devlink region read $DL_HANDLE/dummy snapshot 25 addr 0 len 1 >> /dev/null
175	check_err $? "Failed to read snapshot with id 25 (1 byte)"
176
177	devlink region read $DL_HANDLE/dummy snapshot 25 addr 128 len 128 >> /dev/null
178	check_err $? "Failed to read snapshot with id 25 (128 bytes)"
179
180	devlink region read $DL_HANDLE/dummy snapshot 25 addr 128 len $((1<<32)) >> /dev/null
181	check_err $? "Failed to read snapshot with id 25 (oversized)"
182
183	devlink region read $DL_HANDLE/dummy snapshot 25 addr $((1<<32)) len 128 >> /dev/null 2>&1
184	check_fail $? "Bad read of snapshot with id 25 did not fail"
185
186	devlink region del $DL_HANDLE/dummy snapshot 25
187	check_err $? "Failed to delete snapshot with id 25"
188
189	check_region_snapshot_count dummy post-second-delete 2
190
191	sid=$(devlink -j region new $DL_HANDLE/dummy | jq '.[][][][]')
192	check_err $? "Failed to create a new snapshot with id allocated by the kernel"
193
194	check_region_snapshot_count dummy post-first-request 3
195
196	devlink region dump $DL_HANDLE/dummy snapshot $sid >> /dev/null
197	check_err $? "Failed to dump a snapshot with id allocated by the kernel"
198
199	devlink region del $DL_HANDLE/dummy snapshot $sid
200	check_err $? "Failed to delete snapshot with id allocated by the kernel"
201
202	check_region_snapshot_count dummy post-first-request 2
203
204	log_test "regions test"
205}
206
207reload_test()
208{
209	RET=0
210
211	devlink dev reload $DL_HANDLE
212	check_err $? "Failed to reload"
213
214	echo "y"> $DEBUGFS_DIR/fail_reload
215	check_err $? "Failed to setup devlink reload to fail"
216
217	devlink dev reload $DL_HANDLE
218	check_fail $? "Unexpected success of devlink reload"
219
220	echo "n"> $DEBUGFS_DIR/fail_reload
221	check_err $? "Failed to setup devlink reload not to fail"
222
223	devlink dev reload $DL_HANDLE
224	check_err $? "Failed to reload after set not to fail"
225
226	echo "y"> $DEBUGFS_DIR/dont_allow_reload
227	check_err $? "Failed to forbid devlink reload"
228
229	devlink dev reload $DL_HANDLE
230	check_fail $? "Unexpected success of devlink reload"
231
232	echo "n"> $DEBUGFS_DIR/dont_allow_reload
233	check_err $? "Failed to re-enable devlink reload"
234
235	devlink dev reload $DL_HANDLE
236	check_err $? "Failed to reload after re-enable"
237
238	log_test "reload test"
239}
240
241netns_reload_test()
242{
243	RET=0
244
245	ip netns add testns1
246	check_err $? "Failed add netns \"testns1\""
247	ip netns add testns2
248	check_err $? "Failed add netns \"testns2\""
249
250	devlink dev reload $DL_HANDLE netns testns1
251	check_err $? "Failed to reload into netns \"testns1\""
252
253	devlink -N testns1 dev reload $DL_HANDLE netns testns2
254	check_err $? "Failed to reload from netns \"testns1\" into netns \"testns2\""
255
256	ip netns del testns2
257	ip netns del testns1
258
259	log_test "netns reload test"
260}
261
262DUMMYDEV="dummytest"
263
264res_val_get()
265{
266	local netns=$1
267	local parentname=$2
268	local name=$3
269	local type=$4
270
271	cmd_jq "devlink -N $netns resource show $DL_HANDLE -j" \
272	       ".[][][] | select(.name == \"$parentname\").resources[] \
273	        | select(.name == \"$name\").$type"
274}
275
276resource_test()
277{
278	RET=0
279
280	ip netns add testns1
281	check_err $? "Failed add netns \"testns1\""
282	ip netns add testns2
283	check_err $? "Failed add netns \"testns2\""
284
285	devlink dev reload $DL_HANDLE netns testns1
286	check_err $? "Failed to reload into netns \"testns1\""
287
288	# Create dummy dev to add the address and routes on.
289
290	ip -n testns1 link add name $DUMMYDEV type dummy
291	check_err $? "Failed create dummy device"
292	ip -n testns1 link set $DUMMYDEV up
293	check_err $? "Failed bring up dummy device"
294	ip -n testns1 a a 192.0.1.1/24 dev $DUMMYDEV
295	check_err $? "Failed add an IP address to dummy device"
296
297	local occ=$(res_val_get testns1 IPv4 fib occ)
298	local limit=$((occ+1))
299
300	# Set fib size limit to handle one another route only.
301
302	devlink -N testns1 resource set $DL_HANDLE path IPv4/fib size $limit
303	check_err $? "Failed to set IPv4/fib resource size"
304	local size_new=$(res_val_get testns1 IPv4 fib size_new)
305	[ "$size_new" -eq "$limit" ]
306	check_err $? "Unexpected \"size_new\" value (got $size_new, expected $limit)"
307
308	devlink -N testns1 dev reload $DL_HANDLE
309	check_err $? "Failed to reload"
310	local size=$(res_val_get testns1 IPv4 fib size)
311	[ "$size" -eq "$limit" ]
312	check_err $? "Unexpected \"size\" value (got $size, expected $limit)"
313
314	# Insert 2 routes, the first is going to be inserted,
315	# the second is expected to fail to be inserted.
316
317	ip -n testns1 r a 192.0.2.0/24 via 192.0.1.2
318	check_err $? "Failed to add route"
319
320	ip -n testns1 r a 192.0.3.0/24 via 192.0.1.2
321	check_fail $? "Unexpected successful route add over limit"
322
323	# Now create another dummy in second network namespace and
324	# insert two routes. That is over the limit of the netdevsim
325	# instance in the first namespace. Move the netdevsim instance
326	# into the second namespace and expect it to fail.
327
328	ip -n testns2 link add name $DUMMYDEV type dummy
329	check_err $? "Failed create dummy device"
330	ip -n testns2 link set $DUMMYDEV up
331	check_err $? "Failed bring up dummy device"
332	ip -n testns2 a a 192.0.1.1/24 dev $DUMMYDEV
333	check_err $? "Failed add an IP address to dummy device"
334	ip -n testns2 r a 192.0.2.0/24 via 192.0.1.2
335	check_err $? "Failed to add route"
336	ip -n testns2 r a 192.0.3.0/24 via 192.0.1.2
337	check_err $? "Failed to add route"
338
339	devlink -N testns1 dev reload $DL_HANDLE netns testns2
340	check_fail $? "Unexpected successful reload from netns \"testns1\" into netns \"testns2\""
341
342	devlink -N testns2 resource set $DL_HANDLE path IPv4/fib size ' -1'
343	check_err $? "Failed to reset IPv4/fib resource size"
344
345	devlink -N testns2 dev reload $DL_HANDLE netns 1
346	check_err $? "Failed to reload devlink back"
347
348	ip netns del testns2
349	ip netns del testns1
350
351	log_test "resource test"
352}
353
354info_get()
355{
356	local name=$1
357
358	cmd_jq "devlink dev info $DL_HANDLE -j" ".[][][\"$name\"]" "-e"
359}
360
361dev_info_test()
362{
363	RET=0
364
365	driver=$(info_get "driver")
366	check_err $? "Failed to get driver name"
367	[ "$driver" == "netdevsim" ]
368	check_err $? "Unexpected driver name $driver"
369
370	log_test "dev_info test"
371}
372
373empty_reporter_test()
374{
375	RET=0
376
377	devlink health show $DL_HANDLE reporter empty >/dev/null
378	check_err $? "Failed show empty reporter"
379
380	devlink health dump show $DL_HANDLE reporter empty >/dev/null
381	check_err $? "Failed show dump of empty reporter"
382
383	devlink health diagnose $DL_HANDLE reporter empty >/dev/null
384	check_err $? "Failed diagnose empty reporter"
385
386	devlink health recover $DL_HANDLE reporter empty
387	check_err $? "Failed recover empty reporter"
388
389	log_test "empty reporter test"
390}
391
392check_reporter_info()
393{
394	local name=$1
395	local expected_state=$2
396	local expected_error=$3
397	local expected_recover=$4
398	local expected_grace_period=$5
399	local expected_auto_recover=$6
400
401	local show=$(devlink health show $DL_HANDLE reporter $name -j | jq -e -r ".[][][]")
402	check_err $? "Failed show $name reporter"
403
404	local state=$(echo $show | jq -r ".state")
405	[ "$state" == "$expected_state" ]
406	check_err $? "Unexpected \"state\" value (got $state, expected $expected_state)"
407
408	local error=$(echo $show | jq -r ".error")
409	[ "$error" == "$expected_error" ]
410	check_err $? "Unexpected \"error\" value (got $error, expected $expected_error)"
411
412	local recover=`echo $show | jq -r ".recover"`
413	[ "$recover" == "$expected_recover" ]
414	check_err $? "Unexpected \"recover\" value (got $recover, expected $expected_recover)"
415
416	local grace_period=$(echo $show | jq -r ".grace_period")
417	check_err $? "Failed get $name reporter grace_period"
418	[ "$grace_period" == "$expected_grace_period" ]
419	check_err $? "Unexpected \"grace_period\" value (got $grace_period, expected $expected_grace_period)"
420
421	local auto_recover=$(echo $show | jq -r ".auto_recover")
422	[ "$auto_recover" == "$expected_auto_recover" ]
423	check_err $? "Unexpected \"auto_recover\" value (got $auto_recover, expected $expected_auto_recover)"
424}
425
426dummy_reporter_test()
427{
428	RET=0
429
430	check_reporter_info dummy healthy 0 0 0 true
431
432	devlink health set $DL_HANDLE reporter dummy auto_recover false
433	check_err $? "Failed to dummy reporter auto_recover option"
434
435	check_reporter_info dummy healthy 0 0 0 false
436
437	local BREAK_MSG="foo bar"
438	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
439	check_err $? "Failed to break dummy reporter"
440
441	check_reporter_info dummy error 1 0 0 false
442
443	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
444	check_err $? "Failed show dump of dummy reporter"
445
446	local dump_break_msg=$(echo $dump | jq -r ".break_message")
447	[ "$dump_break_msg" == "$BREAK_MSG" ]
448	check_err $? "Unexpected dump break message value (got $dump_break_msg, expected $BREAK_MSG)"
449
450	devlink health dump clear $DL_HANDLE reporter dummy
451	check_err $? "Failed clear dump of dummy reporter"
452
453	devlink health recover $DL_HANDLE reporter dummy
454	check_err $? "Failed recover dummy reporter"
455
456	check_reporter_info dummy healthy 1 1 0 false
457
458	devlink health set $DL_HANDLE reporter dummy auto_recover true
459	check_err $? "Failed to dummy reporter auto_recover option"
460
461	check_reporter_info dummy healthy 1 1 0 true
462
463	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
464	check_err $? "Failed to break dummy reporter"
465
466	check_reporter_info dummy healthy 2 2 0 true
467
468	local diagnose=$(devlink health diagnose $DL_HANDLE reporter dummy -j -p)
469	check_err $? "Failed show diagnose of dummy reporter"
470
471	local rcvrd_break_msg=$(echo $diagnose | jq -r ".recovered_break_message")
472	[ "$rcvrd_break_msg" == "$BREAK_MSG" ]
473	check_err $? "Unexpected recovered break message value (got $rcvrd_break_msg, expected $BREAK_MSG)"
474
475	devlink health set $DL_HANDLE reporter dummy grace_period 10
476	check_err $? "Failed to dummy reporter grace_period option"
477
478	check_reporter_info dummy healthy 2 2 10 true
479
480	echo "Y"> $DEBUGFS_DIR/health/fail_recover
481	check_err $? "Failed set dummy reporter recovery to fail"
482
483	echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health
484	check_fail $? "Unexpected success of dummy reporter break"
485
486	check_reporter_info dummy error 3 2 10 true
487
488	devlink health recover $DL_HANDLE reporter dummy
489	check_fail $? "Unexpected success of dummy reporter recover"
490
491	echo "N"> $DEBUGFS_DIR/health/fail_recover
492	check_err $? "Failed set dummy reporter recovery to be successful"
493
494	devlink health recover $DL_HANDLE reporter dummy
495	check_err $? "Failed recover dummy reporter"
496
497	check_reporter_info dummy healthy 3 3 10 true
498
499	echo 8192> $DEBUGFS_DIR/health/binary_len
500	check_fail $? "Failed set dummy reporter binary len to 8192"
501
502	local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j)
503	check_err $? "Failed show dump of dummy reporter"
504
505	devlink health dump clear $DL_HANDLE reporter dummy
506	check_err $? "Failed clear dump of dummy reporter"
507
508	log_test "dummy reporter test"
509}
510
511rate_leafs_get()
512{
513	local handle=$1
514
515	cmd_jq "devlink port function rate show -j" \
516	       '.[] | to_entries | .[] | select(.value.type == "leaf") | .key | select(contains("'$handle'"))'
517}
518
519rate_nodes_get()
520{
521	local handle=$1
522
523	cmd_jq "devlink port function rate show -j" \
524		'.[] | to_entries | .[] | select(.value.type == "node") | .key | select(contains("'$handle'"))'
525}
526
527rate_attr_set()
528{
529	local handle=$1
530	local name=$2
531	local value=$3
532	local units=$4
533
534	devlink port function rate set $handle $name $value$units
535}
536
537rate_attr_get()
538{
539	local handle=$1
540	local name=$2
541
542	cmd_jq "devlink port function rate show $handle -j" '.[][].'$name
543}
544
545rate_attr_tx_rate_check()
546{
547	local handle=$1
548	local name=$2
549	local rate=$3
550	local debug_file=$4
551
552	rate_attr_set $handle $name $rate mbit
553	check_err $? "Failed to set $name value"
554
555	local debug_value=$(cat $debug_file)
556	check_err $? "Failed to read $name value from debugfs"
557	[ "$debug_value" == "$rate" ]
558	check_err $? "Unexpected $name debug value $debug_value != $rate"
559
560	local api_value=$(( $(rate_attr_get $handle $name) * 8 / 1000000 ))
561	check_err $? "Failed to get $name attr value"
562	[ "$api_value" == "$rate" ]
563	check_err $? "Unexpected $name attr value $api_value != $rate"
564}
565
566rate_attr_parent_check()
567{
568	local handle=$1
569	local parent=$2
570	local debug_file=$3
571
572	rate_attr_set $handle parent $parent
573	check_err $? "Failed to set parent"
574
575	debug_value=$(cat $debug_file)
576	check_err $? "Failed to get parent debugfs value"
577	[ "$debug_value" == "$parent" ]
578	check_err $? "Unexpected parent debug value $debug_value != $parent"
579
580	api_value=$(rate_attr_get $r_obj parent)
581	check_err $? "Failed to get parent attr value"
582	[ "$api_value" == "$parent" ]
583	check_err $? "Unexpected parent attr value $api_value != $parent"
584}
585
586rate_node_add()
587{
588	local handle=$1
589
590	devlink port function rate add $handle
591}
592
593rate_node_del()
594{
595	local handle=$1
596
597	devlink port function rate del $handle
598}
599
600rate_test()
601{
602	RET=0
603
604	echo $VF_COUNT > /sys/bus/netdevsim/devices/$DEV_NAME/sriov_numvfs
605	devlink dev eswitch set $DL_HANDLE mode switchdev
606	local leafs=`rate_leafs_get $DL_HANDLE`
607	local num_leafs=`echo $leafs | wc -w`
608	[ "$num_leafs" == "$VF_COUNT" ]
609	check_err $? "Expected $VF_COUNT rate leafs but got $num_leafs"
610
611	rate=10
612	for r_obj in $leafs
613	do
614		rate_attr_tx_rate_check $r_obj tx_share $rate \
615			$DEBUGFS_DIR/ports/${r_obj##*/}/tx_share
616		rate=$(($rate+10))
617	done
618
619	rate=100
620	for r_obj in $leafs
621	do
622		rate_attr_tx_rate_check $r_obj tx_max $rate \
623			$DEBUGFS_DIR/ports/${r_obj##*/}/tx_max
624		rate=$(($rate+100))
625	done
626
627	local node1_name='group1'
628	local node1="$DL_HANDLE/$node1_name"
629	rate_node_add "$node1"
630	check_err $? "Failed to add node $node1"
631
632	local num_nodes=`rate_nodes_get $DL_HANDLE | wc -w`
633	[ $num_nodes == 1 ]
634	check_err $? "Expected 1 rate node in output but got $num_nodes"
635
636	local node_tx_share=10
637	rate_attr_tx_rate_check $node1 tx_share $node_tx_share \
638		$DEBUGFS_DIR/rate_nodes/${node1##*/}/tx_share
639
640	local node_tx_max=100
641	rate_attr_tx_rate_check $node1 tx_max $node_tx_max \
642		$DEBUGFS_DIR/rate_nodes/${node1##*/}/tx_max
643
644	rate_node_del "$node1"
645	check_err $? "Failed to delete node $node1"
646	local num_nodes=`rate_nodes_get $DL_HANDLE | wc -w`
647	[ $num_nodes == 0 ]
648	check_err $? "Expected 0 rate node but got $num_nodes"
649
650	local node1_name='group1'
651	local node1="$DL_HANDLE/$node1_name"
652	rate_node_add "$node1"
653	check_err $? "Failed to add node $node1"
654
655	rate_attr_parent_check $r_obj $node1_name \
656		$DEBUGFS_DIR/ports/${r_obj##*/}/rate_parent
657
658	local node2_name='group2'
659	local node2="$DL_HANDLE/$node2_name"
660	rate_node_add "$node2"
661	check_err $? "Failed to add node $node2"
662
663	rate_attr_parent_check $node2 $node1_name \
664		$DEBUGFS_DIR/rate_nodes/$node2_name/rate_parent
665	rate_node_del "$node2"
666	check_err $? "Failed to delete node $node2"
667	rate_attr_set "$r_obj" noparent
668	check_err $? "Failed to unset $r_obj parent node"
669	rate_node_del "$node1"
670	check_err $? "Failed to delete node $node1"
671
672	log_test "rate test"
673}
674
675setup_prepare()
676{
677	modprobe netdevsim
678	echo "$BUS_ADDR $PORT_COUNT" > /sys/bus/netdevsim/new_device
679	while [ ! -d $SYSFS_NET_DIR ] ; do :; done
680}
681
682cleanup()
683{
684	pre_cleanup
685	echo "$BUS_ADDR" > /sys/bus/netdevsim/del_device
686	modprobe -r netdevsim
687}
688
689trap cleanup EXIT
690
691setup_prepare
692
693tests_run
694
695exit $EXIT_STATUS
696