brintos

brintos / linux-shallow public Read only

0
0
Text · 12.8 KiB · 62a05bc Raw
599 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# Kselftest framework requirement - SKIP code is 4.5ksft_skip=46 7##############################################################################8# Defines9 10if [[ ! -v DEVLINK_DEV ]]; then11	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" ]; then14		echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"15		exit $ksft_skip16	fi17	if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then18		echo "SKIP: devlink device's bus is not PCI"19		exit $ksft_skip20	fi21 22	DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \23			 -n | cut -d" " -f3)24elif [[ ! -z "$DEVLINK_DEV" ]]; then25	devlink dev show $DEVLINK_DEV &> /dev/null26	if [ $? -ne 0 ]; then27		echo "SKIP: devlink device \"$DEVLINK_DEV\" not found"28		exit $ksft_skip29	fi30fi31 32##############################################################################33# Sanity checks34 35devlink help 2>&1 | grep resource &> /dev/null36if [ $? -ne 0 ]; then37	echo "SKIP: iproute2 too old, missing devlink resource support"38	exit $ksft_skip39fi40 41devlink help 2>&1 | grep trap &> /dev/null42if [ $? -ne 0 ]; then43	echo "SKIP: iproute2 too old, missing devlink trap support"44	exit $ksft_skip45fi46 47devlink dev help 2>&1 | grep info &> /dev/null48if [ $? -ne 0 ]; then49	echo "SKIP: iproute2 too old, missing devlink dev info support"50	exit $ksft_skip51fi52 53##############################################################################54# Devlink helpers55 56devlink_resource_names_to_path()57{58	local resource59	local path=""60 61	for resource in "${@}"; do62		if [ "$path" == "" ]; then63			path="$resource"64		else65			path="${path}/$resource"66		fi67	done68 69	echo "$path"70}71 72devlink_resource_get()73{74	local name=$175	local resource_name=.[][\"$DEVLINK_DEV\"]76 77	resource_name="$resource_name | .[] | select (.name == \"$name\")"78 79	shift80	for resource in "${@}"; do81		resource_name="${resource_name} | .[\"resources\"][] | \82			       select (.name == \"$resource\")"83	done84 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" ]; then93		devlink_resource_get "$@" | jq '.["size"]'94	else95		echo "$size"96	fi97}98 99devlink_resource_size_set()100{101	local new_size=$1102	local path103 104	shift105	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_pending118 119	devlink dev reload "$DEVLINK_DEV" &> /dev/null120	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 settle127}128 129declare -A DEVLINK_ORIG130 131# Changing pool type from static to dynamic causes reinterpretation of threshold132# values. They therefore need to be saved before pool type is changed, then the133# pool type can be changed, and then the new values need to be set up. Therefore134# instead of saving the current state implicitly in the _set call, provide135# functions for all three primitives: save, set, and restore.136 137devlink_port_pool_threshold()138{139	local port=$1; shift140	local pool=$1; shift141 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; shift149	local pool=$1; shift150	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; shift158	local pool=$1; shift159	local th=$1; shift160 161	devlink sb port pool set $port pool $pool th $th162}163 164devlink_port_pool_th_restore()165{166	local port=$1; shift167	local pool=$1; shift168	local key="port_pool($port,$pool).threshold"169	local -a orig=(${DEVLINK_ORIG[$key]})170 171	if [[ -z $orig ]]; then172		echo "WARNING: Mismatched devlink_port_pool_th_restore"173	else174		devlink sb port pool set $port pool $pool th $orig175	fi176}177 178devlink_pool_size_thtype()179{180	local pool=$1; shift181 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; shift189	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; shift197	local thtype=$1; shift198	local size=$1; shift199 200	devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype201}202 203devlink_pool_size_thtype_restore()204{205	local pool=$1; shift206	local key="pool($pool).size_thtype"207	local -a orig=(${DEVLINK_ORIG[$key]})208 209	if [[ -z ${orig[0]} ]]; then210		echo "WARNING: Mismatched devlink_pool_size_thtype_restore"211	else212		devlink sb pool set "$DEVLINK_DEV" pool $pool \213			size ${orig[0]} thtype ${orig[1]}214	fi215}216 217devlink_tc_bind_pool_th()218{219	local port=$1; shift220	local tc=$1; shift221	local dir=$1; shift222 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; shift230	local tc=$1; shift231	local dir=$1; shift232	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; shift240	local tc=$1; shift241	local dir=$1; shift242	local pool=$1; shift243	local th=$1; shift244 245	devlink sb tc bind set $port tc $tc type $dir pool $pool th $th246}247 248devlink_tc_bind_pool_th_restore()249{250	local port=$1; shift251	local tc=$1; shift252	local dir=$1; shift253	local key="tc_bind($port,$dir,$tc).pool_th"254	local -a orig=(${DEVLINK_ORIG[$key]})255 256	if [[ -z ${orig[0]} ]]; then257		echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore"258	else259		devlink sb tc bind set $port tc $tc type $dir \260			pool ${orig[0]} th ${orig[1]}261	fi262}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; shift277 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; shift285	local action=$1; shift286 287	# Pipe output to /dev/null to avoid expected warnings.288	devlink trap set $DEVLINK_DEV trap $trap_name \289		action $action &> /dev/null290}291 292devlink_trap_action_get()293{294	local trap_name=$1; shift295 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; shift309	local metadata=$1; shift310 311	devlink -jv trap show $DEVLINK_DEV trap $trap_name \312		| jq -e '.[][][].metadata | contains(["'$metadata'"])' \313		&> /dev/null314}315 316devlink_trap_rx_packets_get()317{318	local trap_name=$1; shift319 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; shift327 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; shift335 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; shift343	local t0_packets t0_bytes344	local t1_packets t1_bytes345 346	t0_packets=$(devlink_trap_rx_packets_get $trap_name)347	t0_bytes=$(devlink_trap_rx_bytes_get $trap_name)348 349	sleep 1350 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 ]]; then355		return 0356	else357		return 1358	fi359}360 361devlink_trap_drop_stats_idle_test()362{363	local trap_name=$1; shift364	local t0_packets t0_bytes365 366	t0_packets=$(devlink_trap_drop_packets_get $trap_name)367 368	sleep 1369 370	t1_packets=$(devlink_trap_drop_packets_get $trap_name)371 372	if [[ $t0_packets -eq $t1_packets ]]; then373		return 0374	else375		return 1376	fi377}378 379devlink_traps_enable_all()380{381	local trap_name382 383	for trap_name in $(devlink_traps_get); do384		devlink_trap_action_set $trap_name "trap"385	done386}387 388devlink_traps_disable_all()389{390	for trap_name in $(devlink_traps_get); do391		devlink_trap_action_set $trap_name "drop"392	done393}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; shift403	local action=$1; shift404 405	# Pipe output to /dev/null to avoid expected warnings.406	devlink trap group set $DEVLINK_DEV group $group_name action $action \407		&> /dev/null408}409 410devlink_trap_group_rx_packets_get()411{412	local group_name=$1; shift413 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; shift421 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; shift429	local t0_packets t0_bytes430	local t1_packets t1_bytes431 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 1436 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 ]]; then441		return 0442	else443		return 1444	fi445}446 447devlink_trap_exception_test()448{449	local trap_name=$1; shift450	local group_name451 452	group_name=$(devlink_trap_group_get $trap_name)453 454	devlink_trap_stats_idle_test $trap_name455	check_fail $? "Trap stats idle when packets should have been trapped"456 457	devlink_trap_group_stats_idle_test $group_name458	check_fail $? "Trap group idle when packets should have been trapped"459}460 461devlink_trap_drop_test()462{463	local trap_name=$1; shift464	local dev=$1; shift465	local handle=$1; shift466	local group_name467 468	group_name=$(devlink_trap_group_get $trap_name)469 470	# This is the common part of all the tests. It checks that stats are471	# initially idle, then non-idle after changing the trap action and472	# finally idle again. It also makes sure the packets are dropped and473	# never forwarded.474	devlink_trap_stats_idle_test $trap_name475	check_err $? "Trap stats not idle with initial drop action"476	devlink_trap_group_stats_idle_test $group_name477	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_name481	check_fail $? "Trap stats idle after setting action to trap"482	devlink_trap_group_stats_idle_test $group_name483	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_name488	check_err $? "Trap stats not idle after setting action to drop"489	devlink_trap_group_stats_idle_test $group_name490	check_err $? "Trap group stats not idle after setting action to drop"491 492	tc_check_packets "dev $dev egress" $handle 0493	check_err $? "Packets were not dropped"494}495 496devlink_trap_drop_cleanup()497{498	local mz_pid=$1; shift499	local dev=$1; shift500	local proto=$1; shift501	local pref=$1; shift502	local handle=$1; shift503 504	kill $mz_pid && wait $mz_pid &> /dev/null505	tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower506}507 508devlink_trap_stats_check()509{510	local trap_name=$1; shift511	local send_one="$@"512	local t0_packets513	local t1_packets514 515	t0_packets=$(devlink_trap_rx_packets_get $trap_name)516 517	$send_one && sleep 1518 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; shift527 528	RET=0529 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; shift544 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; shift552 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; shift560 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; shift568 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=$1576 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