# Exit status to return at the end. Set in case one of the tests fails.
EXIT_STATUS=0 # Per-test return value. Clear at the beginning of each test.
RET=0
until_counter_is()
{
local expr=$1; shift
local current=$("$@")
echo $((current))
((current $expr))
}
busywait_for_counter()
{
local timeout=$1; shift
local delta=$1; shift
local base=$("$@")
busywait "$timeout" until_counter_is ">= $((base + delta))""$@"
}
slowwait_for_counter()
{
local timeout=$1; shift
local delta=$1; shift
local base=$("$@")
slowwait "$timeout" until_counter_is ">= $((base + delta))""$@"
}
# Check for existence of tools which are built as part of selftests # but may also already exist in $PATH
check_gen_prog()
{
local prog_name=$1; shift
if ! which $prog_name >/dev/null 2>/dev/null; then
PATH=$PWD:$PATH if ! which $prog_name >/dev/null; then echo"'$prog_name' command not found; skipping tests"
exit $ksft_skip fi fi
}
remove_ns_list()
{
local item=$1
local ns
local ns_list=("${NS_LIST[@]}")
NS_LIST=()
for ns in "${ns_list[@]}"; do if [ "${ns}" != "${item}" ]; then
NS_LIST+=("${ns}") fi done
}
cleanup_ns()
{
local ns=""
local ret=0
for ns in "$@"; do
[ -z "${ns}" ] && continue
ip netns pids "${ns}" 2> /dev/null | xargs -r kill || true
ip netns delete"${ns}" &> /dev/null || true if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then echo"Warn: Failed to remove namespace $ns"
ret=1 else
remove_ns_list "${ns}" fi done
return $ret
}
cleanup_all_ns()
{
cleanup_ns "${NS_LIST[@]}"
}
# setup netns with given names as prefix. e.g # setup_ns local remote
setup_ns()
{
local ns_name=""
local ns_list=() for ns_name in "$@"; do # avoid conflicts with local var: internal error if [ "${ns_name}" = "ns_name" ]; then echo"Failed to setup namespace '${ns_name}': invalid name"
cleanup_ns "${ns_list[@]}"
exit $ksft_fail fi
# Some test may setup/remove same netns multi times if [ -z "${!ns_name}" ]; then eval"${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)" else
cleanup_ns "${!ns_name}" fi
if ! ip netns add "${!ns_name}"; then echo"Failed to create namespace $ns_name"
cleanup_ns "${ns_list[@]}"
return $ksft_skip fi
ip -n "${!ns_name}" link set lo up
ip netns exec "${!ns_name}" sysctl -wq net.ipv4.conf.all.rp_filter=0
ip netns exec "${!ns_name}" sysctl -wq net.ipv4.conf.default.rp_filter=0
ns_list+=("${!ns_name}") done
NS_LIST+=("${ns_list[@]}")
}
# Create netdevsim with given id and net namespace.
create_netdevsim() {
local id="$1"
local ns="$2"
modprobe netdevsim &> /dev/null
udevadm settle
echo"$id 1" | ip netns exec $ns tee /sys/bus/netdevsim/new_device >/dev/null
local dev=$(ip netns exec $ns ls /sys/bus/netdevsim/devices/netdevsim$id/net)
ip -netns $ns link set dev $dev name nsim$id
ip -netns $ns link set dev nsim$id up
echo nsim$id
}
create_netdevsim_port() {
local nsim_id="$1"
local ns="$2"
local port_id="$3"
local perm_addr="$4"
local orig_dev
local new_dev
local nsim_path
# attach a qdisc with two children match/no-match and a flower filter to match
tc_set_flower_counter() {
local -r ns=$1
local -r ipver=$2
local -r dev=$3
local -r flower_expr=$4
if ((RET == ksft_pass)); then
handle_test_result_pass "$test_name""$opt_str" elif ((RET == ksft_xfail)); then
handle_test_result_xfail "$test_name""$opt_str" elif ((RET == ksft_skip)); then
handle_test_result_skip "$test_name""$opt_str" else
handle_test_result_fail "$test_name""$opt_str" fi
wait_local_port_listen()
{
local listener_ns="${1}"
local port="${2}"
local protocol="${3}"
local pattern
local i
pattern=":$(printf "%04X" "${port}") "
# for tcp protocol additionally check the socket state
[ ${protocol} = "tcp" ] && pattern="${pattern}0A" for i in $(seq 10); do if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \
/proc/net/"${protocol}"* | grep -q "${pattern}"; then
break fi
sleep 0.1 done
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.