brintos

brintos / linux-shallow public Read only

0
0
Text · 4.2 KiB · be8707b Raw
236 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4##############################################################################5# Defines6 7: "${WAIT_TIMEOUT:=20}"8 9BUSYWAIT_TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms10 11# Kselftest framework constants.12ksft_pass=013ksft_fail=114ksft_xfail=215ksft_skip=416 17# namespace list created by setup_ns18NS_LIST=()19 20##############################################################################21# Helpers22 23__ksft_status_merge()24{25	local a=$1; shift26	local b=$1; shift27	local -A weights28	local weight=029 30	local i31	for i in "$@"; do32		weights[$i]=$((weight++))33	done34 35	if [[ ${weights[$a]} > ${weights[$b]} ]]; then36		echo "$a"37		return 038	else39		echo "$b"40		return 141	fi42}43 44ksft_status_merge()45{46	local a=$1; shift47	local b=$1; shift48 49	__ksft_status_merge "$a" "$b" \50		$ksft_pass $ksft_xfail $ksft_skip $ksft_fail51}52 53ksft_exit_status_merge()54{55	local a=$1; shift56	local b=$1; shift57 58	__ksft_status_merge "$a" "$b" \59		$ksft_xfail $ksft_pass $ksft_skip $ksft_fail60}61 62loopy_wait()63{64	local sleep_cmd=$1; shift65	local timeout_ms=$1; shift66 67	local start_time="$(date -u +%s%3N)"68	while true69	do70		local out71		if out=$("$@"); then72			echo -n "$out"73			return 074		fi75 76		local current_time="$(date -u +%s%3N)"77		if ((current_time - start_time > timeout_ms)); then78			echo -n "$out"79			return 180		fi81 82		$sleep_cmd83	done84}85 86busywait()87{88	local timeout_ms=$1; shift89 90	loopy_wait : "$timeout_ms" "$@"91}92 93# timeout in seconds94slowwait()95{96	local timeout_sec=$1; shift97 98	loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"99}100 101until_counter_is()102{103	local expr=$1; shift104	local current=$("$@")105 106	echo $((current))107	((current $expr))108}109 110busywait_for_counter()111{112	local timeout=$1; shift113	local delta=$1; shift114 115	local base=$("$@")116	busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"117}118 119slowwait_for_counter()120{121	local timeout=$1; shift122	local delta=$1; shift123 124	local base=$("$@")125	slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"126}127 128# Check for existence of tools which are built as part of selftests129# but may also already exist in $PATH130check_gen_prog()131{132	local prog_name=$1; shift133 134	if ! which $prog_name >/dev/null 2>/dev/null; then135		PATH=$PWD:$PATH136		if ! which $prog_name >/dev/null; then137			echo "'$prog_name' command not found; skipping tests"138			exit $ksft_skip139		fi140	fi141}142 143remove_ns_list()144{145	local item=$1146	local ns147	local ns_list=("${NS_LIST[@]}")148	NS_LIST=()149 150	for ns in "${ns_list[@]}"; do151		if [ "${ns}" != "${item}" ]; then152			NS_LIST+=("${ns}")153		fi154	done155}156 157cleanup_ns()158{159	local ns=""160	local ret=0161 162	for ns in "$@"; do163		[ -z "${ns}" ] && continue164		ip netns pids "${ns}" 2> /dev/null | xargs -r kill || true165		ip netns delete "${ns}" &> /dev/null || true166		if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then167			echo "Warn: Failed to remove namespace $ns"168			ret=1169		else170			remove_ns_list "${ns}"171		fi172	done173 174	return $ret175}176 177cleanup_all_ns()178{179	cleanup_ns "${NS_LIST[@]}"180}181 182# setup netns with given names as prefix. e.g183# setup_ns local remote184setup_ns()185{186	local ns_name=""187	local ns_list=()188	for ns_name in "$@"; do189		# avoid conflicts with local var: internal error190		if [ "${ns_name}" = "ns_name" ]; then191			echo "Failed to setup namespace '${ns_name}': invalid name"192			cleanup_ns "${ns_list[@]}"193			exit $ksft_fail194		fi195 196		# Some test may setup/remove same netns multi times197		if [ -z "${!ns_name}" ]; then198			eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"199		else200			cleanup_ns "${!ns_name}"201		fi202 203		if ! ip netns add "${!ns_name}"; then204			echo "Failed to create namespace $ns_name"205			cleanup_ns "${ns_list[@]}"206			return $ksft_skip207		fi208		ip -n "${!ns_name}" link set lo up209		ns_list+=("${!ns_name}")210	done211	NS_LIST+=("${ns_list[@]}")212}213 214tc_rule_stats_get()215{216	local dev=$1; shift217	local pref=$1; shift218	local dir=${1:-ingress}; shift219	local selector=${1:-.packets}; shift220 221	tc -j -s filter show dev $dev $dir pref $pref \222	    | jq ".[1].options.actions[].stats$selector"223}224 225tc_rule_handle_stats_get()226{227	local id=$1; shift228	local handle=$1; shift229	local selector=${1:-.packets}; shift230	local netns=${1:-""}; shift231 232	tc $netns -j -s filter show $id \233	    | jq ".[] | select(.options.handle == $handle) | \234		  .options.actions[0].stats$selector"235}236