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