xref: /linux/tools/testing/selftests/net/forwarding/devlink_lib.sh (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest framework requirement - SKIP code is 4.
5ksft_skip=4
6
7##############################################################################
8# Defines
9
10if [[ ! -v DEVLINK_DEV ]]; then
11	DEVLINK_DEV=$(devlink port show "${NETIFS[p1]:-$NETIF_NO_CABLE}" -j \
12			     | jq -r '.port | keys[]' | cut -d/ -f-2)
13	if [ -z "$DEVLINK_DEV" ]; then
14		echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
15		exit $ksft_skip
16	fi
17	if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
18		echo "SKIP: devlink device's bus is not PCI"
19		exit $ksft_skip
20	fi
21
22	DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
23			 -n | cut -d" " -f3)
24elif [[ ! -z "$DEVLINK_DEV" ]]; then
25	devlink dev show $DEVLINK_DEV &> /dev/null
26	if [ $? -ne 0 ]; then
27		echo "SKIP: devlink device \"$DEVLINK_DEV\" not found"
28		exit $ksft_skip
29	fi
30fi
31
32##############################################################################
33# Sanity checks
34
35devlink help 2>&1 | grep resource &> /dev/null
36if [ $? -ne 0 ]; then
37	echo "SKIP: iproute2 too old, missing devlink resource support"
38	exit $ksft_skip
39fi
40
41devlink help 2>&1 | grep trap &> /dev/null
42if [ $? -ne 0 ]; then
43	echo "SKIP: iproute2 too old, missing devlink trap support"
44	exit $ksft_skip
45fi
46
47devlink dev help 2>&1 | grep info &> /dev/null
48if [ $? -ne 0 ]; then
49	echo "SKIP: iproute2 too old, missing devlink dev info support"
50	exit $ksft_skip
51fi
52
53##############################################################################
54# Devlink helpers
55
56devlink_resource_names_to_path()
57{
58	local resource
59	local path=""
60
61	for resource in "${@}"; do
62		if [ "$path" == "" ]; then
63			path="$resource"
64		else
65			path="${path}/$resource"
66		fi
67	done
68
69	echo "$path"
70}
71
72devlink_resource_get()
73{
74	local name=$1
75	local resource_name=.[][\"$DEVLINK_DEV\"]
76
77	resource_name="$resource_name | .[] | select (.name == \"$name\")"
78
79	shift
80	for resource in "${@}"; do
81		resource_name="${resource_name} | .[\"resources\"][] | \
82			       select (.name == \"$resource\")"
83	done
84
85	devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name"
86}
87
88devlink_resource_size_get()
89{
90	local size=$(devlink_resource_get "$@" | jq '.["size_new"]')
91
92	if [ "$size" == "null" ]; then
93		devlink_resource_get "$@" | jq '.["size"]'
94	else
95		echo "$size"
96	fi
97}
98
99devlink_resource_size_set()
100{
101	local new_size=$1
102	local path
103
104	shift
105	path=$(devlink_resource_names_to_path "$@")
106	devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size"
107	check_err $? "Failed setting path $path to size $size"
108}
109
110devlink_resource_occ_get()
111{
112	devlink_resource_get "$@" | jq '.["occ"]'
113}
114
115devlink_reload()
116{
117	local still_pending
118
119	devlink dev reload "$DEVLINK_DEV" &> /dev/null
120	check_err $? "Failed reload"
121
122	still_pending=$(devlink resource show "$DEVLINK_DEV" | \
123			grep -c "size_new")
124	check_err $still_pending "Failed reload - There are still unset sizes"
125
126	udevadm settle
127}
128
129declare -A DEVLINK_ORIG
130
131# Changing pool type from static to dynamic causes reinterpretation of threshold
132# values. They therefore need to be saved before pool type is changed, then the
133# pool type can be changed, and then the new values need to be set up. Therefore
134# instead of saving the current state implicitly in the _set call, provide
135# functions for all three primitives: save, set, and restore.
136
137devlink_port_pool_threshold()
138{
139	local port=$1; shift
140	local pool=$1; shift
141
142	devlink sb port pool show $port pool $pool -j \
143		| jq '.port_pool."'"$port"'"[].threshold'
144}
145
146devlink_port_pool_th_save()
147{
148	local port=$1; shift
149	local pool=$1; shift
150	local key="port_pool($port,$pool).threshold"
151
152	DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool)
153}
154
155devlink_port_pool_th_set()
156{
157	local port=$1; shift
158	local pool=$1; shift
159	local th=$1; shift
160
161	devlink sb port pool set $port pool $pool th $th
162}
163
164devlink_port_pool_th_restore()
165{
166	local port=$1; shift
167	local pool=$1; shift
168	local key="port_pool($port,$pool).threshold"
169	local -a orig=(${DEVLINK_ORIG[$key]})
170
171	if [[ -z $orig ]]; then
172		echo "WARNING: Mismatched devlink_port_pool_th_restore"
173	else
174		devlink sb port pool set $port pool $pool th $orig
175	fi
176}
177
178devlink_pool_size_thtype()
179{
180	local pool=$1; shift
181
182	devlink sb pool show "$DEVLINK_DEV" pool $pool -j \
183	    | jq -r '.pool[][] | (.size, .thtype)'
184}
185
186devlink_pool_size_thtype_save()
187{
188	local pool=$1; shift
189	local key="pool($pool).size_thtype"
190
191	DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool)
192}
193
194devlink_pool_size_thtype_set()
195{
196	local pool=$1; shift
197	local thtype=$1; shift
198	local size=$1; shift
199
200	devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype
201}
202
203devlink_pool_size_thtype_restore()
204{
205	local pool=$1; shift
206	local key="pool($pool).size_thtype"
207	local -a orig=(${DEVLINK_ORIG[$key]})
208
209	if [[ -z ${orig[0]} ]]; then
210		echo "WARNING: Mismatched devlink_pool_size_thtype_restore"
211	else
212		devlink sb pool set "$DEVLINK_DEV" pool $pool \
213			size ${orig[0]} thtype ${orig[1]}
214	fi
215}
216
217devlink_tc_bind_pool_th()
218{
219	local port=$1; shift
220	local tc=$1; shift
221	local dir=$1; shift
222
223	devlink sb tc bind show $port tc $tc type $dir -j \
224	    | jq -r '.tc_bind[][] | (.pool, .threshold)'
225}
226
227devlink_tc_bind_pool_th_save()
228{
229	local port=$1; shift
230	local tc=$1; shift
231	local dir=$1; shift
232	local key="tc_bind($port,$dir,$tc).pool_th"
233
234	DEVLINK_ORIG[$key]=$(devlink_tc_bind_pool_th $port $tc $dir)
235}
236
237devlink_tc_bind_pool_th_set()
238{
239	local port=$1; shift
240	local tc=$1; shift
241	local dir=$1; shift
242	local pool=$1; shift
243	local th=$1; shift
244
245	devlink sb tc bind set $port tc $tc type $dir pool $pool th $th
246}
247
248devlink_tc_bind_pool_th_restore()
249{
250	local port=$1; shift
251	local tc=$1; shift
252	local dir=$1; shift
253	local key="tc_bind($port,$dir,$tc).pool_th"
254	local -a orig=(${DEVLINK_ORIG[$key]})
255
256	if [[ -z ${orig[0]} ]]; then
257		echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore"
258	else
259		devlink sb tc bind set $port tc $tc type $dir \
260			pool ${orig[0]} th ${orig[1]}
261	fi
262}
263
264devlink_traps_num_get()
265{
266	devlink -j trap | jq '.[]["'$DEVLINK_DEV'"] | length'
267}
268
269devlink_traps_get()
270{
271	devlink -j trap | jq -r '.[]["'$DEVLINK_DEV'"][].name'
272}
273
274devlink_trap_type_get()
275{
276	local trap_name=$1; shift
277
278	devlink -j trap show $DEVLINK_DEV trap $trap_name \
279		| jq -r '.[][][].type'
280}
281
282devlink_trap_action_set()
283{
284	local trap_name=$1; shift
285	local action=$1; shift
286
287	# Pipe output to /dev/null to avoid expected warnings.
288	devlink trap set $DEVLINK_DEV trap $trap_name \
289		action $action &> /dev/null
290}
291
292devlink_trap_action_get()
293{
294	local trap_name=$1; shift
295
296	devlink -j trap show $DEVLINK_DEV trap $trap_name \
297		| jq -r '.[][][].action'
298}
299
300devlink_trap_group_get()
301{
302	devlink -j trap show $DEVLINK_DEV trap $trap_name \
303		| jq -r '.[][][].group'
304}
305
306devlink_trap_metadata_test()
307{
308	local trap_name=$1; shift
309	local metadata=$1; shift
310
311	devlink -jv trap show $DEVLINK_DEV trap $trap_name \
312		| jq -e '.[][][].metadata | contains(["'$metadata'"])' \
313		&> /dev/null
314}
315
316devlink_trap_rx_packets_get()
317{
318	local trap_name=$1; shift
319
320	devlink -js trap show $DEVLINK_DEV trap $trap_name \
321		| jq '.[][][]["stats"]["rx"]["packets"]'
322}
323
324devlink_trap_rx_bytes_get()
325{
326	local trap_name=$1; shift
327
328	devlink -js trap show $DEVLINK_DEV trap $trap_name \
329		| jq '.[][][]["stats"]["rx"]["bytes"]'
330}
331
332devlink_trap_drop_packets_get()
333{
334	local trap_name=$1; shift
335
336	devlink -js trap show $DEVLINK_DEV trap $trap_name \
337		| jq '.[][][]["stats"]["rx"]["dropped"]'
338}
339
340devlink_trap_stats_idle_test()
341{
342	local trap_name=$1; shift
343	local t0_packets t0_bytes
344	local t1_packets t1_bytes
345
346	t0_packets=$(devlink_trap_rx_packets_get $trap_name)
347	t0_bytes=$(devlink_trap_rx_bytes_get $trap_name)
348
349	sleep 1
350
351	t1_packets=$(devlink_trap_rx_packets_get $trap_name)
352	t1_bytes=$(devlink_trap_rx_bytes_get $trap_name)
353
354	if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
355		return 0
356	else
357		return 1
358	fi
359}
360
361devlink_trap_drop_stats_idle_test()
362{
363	local trap_name=$1; shift
364	local t0_packets t0_bytes
365
366	t0_packets=$(devlink_trap_drop_packets_get $trap_name)
367
368	sleep 1
369
370	t1_packets=$(devlink_trap_drop_packets_get $trap_name)
371
372	if [[ $t0_packets -eq $t1_packets ]]; then
373		return 0
374	else
375		return 1
376	fi
377}
378
379devlink_traps_enable_all()
380{
381	local trap_name
382
383	for trap_name in $(devlink_traps_get); do
384		devlink_trap_action_set $trap_name "trap"
385	done
386}
387
388devlink_traps_disable_all()
389{
390	for trap_name in $(devlink_traps_get); do
391		devlink_trap_action_set $trap_name "drop"
392	done
393}
394
395devlink_trap_groups_get()
396{
397	devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name'
398}
399
400devlink_trap_group_action_set()
401{
402	local group_name=$1; shift
403	local action=$1; shift
404
405	# Pipe output to /dev/null to avoid expected warnings.
406	devlink trap group set $DEVLINK_DEV group $group_name action $action \
407		&> /dev/null
408}
409
410devlink_trap_group_rx_packets_get()
411{
412	local group_name=$1; shift
413
414	devlink -js trap group show $DEVLINK_DEV group $group_name \
415		| jq '.[][][]["stats"]["rx"]["packets"]'
416}
417
418devlink_trap_group_rx_bytes_get()
419{
420	local group_name=$1; shift
421
422	devlink -js trap group show $DEVLINK_DEV group $group_name \
423		| jq '.[][][]["stats"]["rx"]["bytes"]'
424}
425
426devlink_trap_group_stats_idle_test()
427{
428	local group_name=$1; shift
429	local t0_packets t0_bytes
430	local t1_packets t1_bytes
431
432	t0_packets=$(devlink_trap_group_rx_packets_get $group_name)
433	t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
434
435	sleep 1
436
437	t1_packets=$(devlink_trap_group_rx_packets_get $group_name)
438	t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
439
440	if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
441		return 0
442	else
443		return 1
444	fi
445}
446
447devlink_trap_exception_test()
448{
449	local trap_name=$1; shift
450	local group_name
451
452	group_name=$(devlink_trap_group_get $trap_name)
453
454	devlink_trap_stats_idle_test $trap_name
455	check_fail $? "Trap stats idle when packets should have been trapped"
456
457	devlink_trap_group_stats_idle_test $group_name
458	check_fail $? "Trap group idle when packets should have been trapped"
459}
460
461devlink_trap_drop_test()
462{
463	local trap_name=$1; shift
464	local dev=$1; shift
465	local handle=$1; shift
466	local group_name
467
468	group_name=$(devlink_trap_group_get $trap_name)
469
470	# This is the common part of all the tests. It checks that stats are
471	# initially idle, then non-idle after changing the trap action and
472	# finally idle again. It also makes sure the packets are dropped and
473	# never forwarded.
474	devlink_trap_stats_idle_test $trap_name
475	check_err $? "Trap stats not idle with initial drop action"
476	devlink_trap_group_stats_idle_test $group_name
477	check_err $? "Trap group stats not idle with initial drop action"
478
479	devlink_trap_action_set $trap_name "trap"
480	devlink_trap_stats_idle_test $trap_name
481	check_fail $? "Trap stats idle after setting action to trap"
482	devlink_trap_group_stats_idle_test $group_name
483	check_fail $? "Trap group stats idle after setting action to trap"
484
485	devlink_trap_action_set $trap_name "drop"
486
487	devlink_trap_stats_idle_test $trap_name
488	check_err $? "Trap stats not idle after setting action to drop"
489	devlink_trap_group_stats_idle_test $group_name
490	check_err $? "Trap group stats not idle after setting action to drop"
491
492	tc_check_packets "dev $dev egress" $handle 0
493	check_err $? "Packets were not dropped"
494}
495
496devlink_trap_drop_cleanup()
497{
498	local mz_pid=$1; shift
499	local dev=$1; shift
500	local proto=$1; shift
501	local pref=$1; shift
502	local handle=$1; shift
503
504	kill $mz_pid && wait $mz_pid &> /dev/null
505	tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower
506}
507
508devlink_trap_stats_check()
509{
510	local trap_name=$1; shift
511	local send_one="$@"
512	local t0_packets
513	local t1_packets
514
515	t0_packets=$(devlink_trap_rx_packets_get $trap_name)
516
517	$send_one && sleep 1
518
519	t1_packets=$(devlink_trap_rx_packets_get $trap_name)
520
521	[[ $t1_packets -ne $t0_packets ]]
522}
523
524devlink_trap_stats_test()
525{
526	local test_name=$1; shift
527
528	RET=0
529
530	devlink_trap_stats_check "$@"
531	check_err $? "Trap stats did not increase"
532
533	log_test "$test_name"
534}
535
536devlink_trap_policers_num_get()
537{
538	devlink -j -p trap policer show | jq '.[]["'$DEVLINK_DEV'"] | length'
539}
540
541devlink_trap_policer_rate_get()
542{
543	local policer_id=$1; shift
544
545	devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
546		| jq '.[][][]["rate"]'
547}
548
549devlink_trap_policer_burst_get()
550{
551	local policer_id=$1; shift
552
553	devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
554		| jq '.[][][]["burst"]'
555}
556
557devlink_trap_policer_rx_dropped_get()
558{
559	local policer_id=$1; shift
560
561	devlink -j -p -s trap policer show $DEVLINK_DEV policer $policer_id \
562		| jq '.[][][]["stats"]["rx"]["dropped"]'
563}
564
565devlink_trap_group_policer_get()
566{
567	local group_name=$1; shift
568
569	devlink -j -p trap group show $DEVLINK_DEV group $group_name \
570		| jq '.[][][]["policer"]'
571}
572
573devlink_port_by_netdev()
574{
575	local if_name=$1
576
577	devlink -j port show $if_name | jq -e '.[] | keys' | jq -r '.[]'
578}
579
580devlink_cpu_port_get()
581{
582	local cpu_dl_port_num=$(devlink port list | grep "$DEVLINK_DEV" |
583				grep cpu | cut -d/ -f3 | cut -d: -f1 |
584				sed -n '1p')
585
586	echo "$DEVLINK_DEV/$cpu_dl_port_num"
587}
588
589devlink_cell_size_get()
590{
591	devlink sb pool show "$DEVLINK_DEV" pool 0 -j \
592	    | jq '.pool[][].cell_size'
593}
594
595devlink_pool_size_get()
596{
597	devlink sb show "$DEVLINK_DEV" -j | jq '.[][][]["size"]'
598}
599