#!/bin/bash# SPDX-License-Identifier: GPL-2.0 ############################################################################### Defines # Kselftest framework requirement - SKIP code is 4.ksft_skip=4 # Can be overridden by the configuration file.PING=${PING:=ping}PING6=${PING6:=ping6}MZ=${MZ:=mausezahn}ARPING=${ARPING:=arping}TEAMD=${TEAMD:=teamd}WAIT_TIME=${WAIT_TIME:=5}PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}PAUSE_ON_CLEANUP=${PAUSE_ON_CLEANUP:=no}NETIF_TYPE=${NETIF_TYPE:=veth}NETIF_CREATE=${NETIF_CREATE:=yes}MCD=${MCD:=smcrouted}MC_CLI=${MC_CLI:=smcroutectl}PING_COUNT=${PING_COUNT:=10}PING_TIMEOUT=${PING_TIMEOUT:=5}WAIT_TIMEOUT=${WAIT_TIMEOUT:=20}INTERFACE_TIMEOUT=${INTERFACE_TIMEOUT:=600}LOW_AGEING_TIME=${LOW_AGEING_TIME:=1000}REQUIRE_JQ=${REQUIRE_JQ:=yes}REQUIRE_MZ=${REQUIRE_MZ:=yes}REQUIRE_MTOOLS=${REQUIRE_MTOOLS:=no}STABLE_MAC_ADDRS=${STABLE_MAC_ADDRS:=no}TCPDUMP_EXTRA_FLAGS=${TCPDUMP_EXTRA_FLAGS:=} relative_path="${BASH_SOURCE%/*}"if [[ "$relative_path" == "${BASH_SOURCE}" ]]; then relative_path="."fi if [[ -f $relative_path/forwarding.config ]]; then source "$relative_path/forwarding.config"fi ############################################################################### Sanity checks check_tc_version(){ tc -j &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing JSON support" exit $ksft_skip fi} # Old versions of tc don't understand "mpls_uc"check_tc_mpls_support(){ local dev=$1; shift tc filter add dev $dev ingress protocol mpls_uc pref 1 handle 1 \ matchall action pipe &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing MPLS support" return $ksft_skip fi tc filter del dev $dev ingress protocol mpls_uc pref 1 handle 1 \ matchall} # Old versions of tc produce invalid json output for mpls lse statisticscheck_tc_mpls_lse_stats(){ local dev=$1; shift local ret; tc filter add dev $dev ingress protocol mpls_uc pref 1 handle 1 \ flower mpls lse depth 2 \ action continue &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc-flower is missing extended MPLS support" return $ksft_skip fi tc -j filter show dev $dev ingress protocol mpls_uc | jq . &> /dev/null ret=$? tc filter del dev $dev ingress protocol mpls_uc pref 1 handle 1 \ flower if [[ $ret -ne 0 ]]; then echo "SKIP: iproute2 too old; tc-flower produces invalid json output for extended MPLS filters" return $ksft_skip fi} check_tc_shblock_support(){ tc filter help 2>&1 | grep block &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing shared block support" exit $ksft_skip fi} check_tc_chain_support(){ tc help 2>&1|grep chain &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing chain support" exit $ksft_skip fi} check_tc_action_hw_stats_support(){ tc actions help 2>&1 | grep -q hw_stats if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing action hw_stats support" exit $ksft_skip fi} check_ethtool_lanes_support(){ ethtool --help 2>&1| grep lanes &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: ethtool too old; it is missing lanes support" exit $ksft_skip fi} check_locked_port_support(){ if ! bridge -d link show | grep -q " locked"; then echo "SKIP: iproute2 too old; Locked port feature not supported." return $ksft_skip fi} check_port_mab_support(){ if ! bridge -d link show | grep -q "mab"; then echo "SKIP: iproute2 too old; MacAuth feature not supported." return $ksft_skip fi} if [[ "$(id -u)" -ne 0 ]]; then echo "SKIP: need root privileges" exit $ksft_skipfi if [[ "$CHECK_TC" = "yes" ]]; then check_tc_versionfi require_command(){ local cmd=$1; shift if [[ ! -x "$(command -v "$cmd")" ]]; then echo "SKIP: $cmd not installed" exit $ksft_skip fi} if [[ "$REQUIRE_JQ" = "yes" ]]; then require_command jqfiif [[ "$REQUIRE_MZ" = "yes" ]]; then require_command $MZfiif [[ "$REQUIRE_MTOOLS" = "yes" ]]; then # https://github.com/vladimiroltean/mtools/ # patched for IPv6 support require_command msend require_command mreceivefi if [[ ! -v NUM_NETIFS ]]; then echo "SKIP: importer does not define \"NUM_NETIFS\"" exit $ksft_skipfi ############################################################################### Command line options handling count=0 while [[ $# -gt 0 ]]; do if [[ "$count" -eq "0" ]]; then unset NETIFS declare -A NETIFS fi count=$((count + 1)) NETIFS[p$count]="$1" shiftdone ############################################################################### Network interfaces configuration create_netif_veth(){ local i for ((i = 1; i <= NUM_NETIFS; ++i)); do local j=$((i+1)) ip link show dev ${NETIFS[p$i]} &> /dev/null if [[ $? -ne 0 ]]; then ip link add ${NETIFS[p$i]} type veth \ peer name ${NETIFS[p$j]} if [[ $? -ne 0 ]]; then echo "Failed to create netif" exit 1 fi fi i=$j done} create_netif(){ case "$NETIF_TYPE" in veth) create_netif_veth ;; *) echo "Can not create interfaces of type \'$NETIF_TYPE\'" exit 1 ;; esac} declare -A MAC_ADDR_ORIGmac_addr_prepare(){ local new_addr= local dev= for ((i = 1; i <= NUM_NETIFS; ++i)); do dev=${NETIFS[p$i]} new_addr=$(printf "00:01:02:03:04:%02x" $i) MAC_ADDR_ORIG["$dev"]=$(ip -j link show dev $dev | jq -e '.[].address') # Strip quotes MAC_ADDR_ORIG["$dev"]=${MAC_ADDR_ORIG["$dev"]//\"/} ip link set dev $dev address $new_addr done} mac_addr_restore(){ local dev= for ((i = 1; i <= NUM_NETIFS; ++i)); do dev=${NETIFS[p$i]} ip link set dev $dev address ${MAC_ADDR_ORIG["$dev"]} done} if [[ "$NETIF_CREATE" = "yes" ]]; then create_netiffi if [[ "$STABLE_MAC_ADDRS" = "yes" ]]; then mac_addr_preparefi for ((i = 1; i <= NUM_NETIFS; ++i)); do ip link show dev ${NETIFS[p$i]} &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: could not find all required interfaces" exit $ksft_skip fidone ############################################################################### Helpers # 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 check_err(){ local err=$1 local msg=$2 if [[ $RET -eq 0 && $err -ne 0 ]]; then RET=$err retmsg=$msg fi} check_fail(){ local err=$1 local msg=$2 if [[ $RET -eq 0 && $err -eq 0 ]]; then RET=1 retmsg=$msg fi} check_err_fail(){ local should_fail=$1; shift local err=$1; shift local what=$1; shift if ((should_fail)); then check_fail $err "$what succeeded, but should have failed" else check_err $err "$what failed" fi} log_test(){ local test_name=$1 local opt_str=$2 if [[ $# -eq 2 ]]; then opt_str="($opt_str)" fi if [[ $RET -ne 0 ]]; then EXIT_STATUS=1 printf "TEST: %-60s [FAIL]\n" "$test_name $opt_str" if [[ ! -z "$retmsg" ]]; then printf "\t%s\n" "$retmsg" fi if [ "${PAUSE_ON_FAIL}" = "yes" ]; then echo "Hit enter to continue, 'q' to quit" read a [ "$a" = "q" ] && exit 1 fi return 1 fi printf "TEST: %-60s [ OK ]\n" "$test_name $opt_str" return 0} log_test_skip(){ local test_name=$1 local opt_str=$2 printf "TEST: %-60s [SKIP]\n" "$test_name $opt_str" return 0} log_info(){ local msg=$1 echo "INFO: $msg"} busywait(){ local timeout=$1; shift local start_time="$(date -u +%s%3N)" while true do local out out=$("$@") local ret=$? if ((!ret)); then echo -n "$out" return 0 fi local current_time="$(date -u +%s%3N)" if ((current_time - start_time > timeout)); then echo -n "$out" return 1 fi done} not(){ "$@" [[ $? != 0 ]]} get_max(){ local arr=("$@") max=${arr[0]} for cur in ${arr[@]}; do if [[ $cur -gt $max ]]; then max=$cur fi done echo $max} grep_bridge_fdb(){ local addr=$1; shift local word local flag if [ "$1" == "self" ] || [ "$1" == "master" ]; then word=$1; shift if [ "$1" == "-v" ]; then flag=$1; shift fi fi $@ | grep $addr | grep $flag "$word"} wait_for_port_up(){ "$@" | grep -q "Link detected: yes"} wait_for_offload(){ "$@" | grep -q offload} wait_for_trap(){ "$@" | grep -q trap} 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))" "$@"} setup_wait_dev(){ local dev=$1; shift local wait_time=${1:-$WAIT_TIME}; shift setup_wait_dev_with_timeout "$dev" $INTERFACE_TIMEOUT $wait_time if (($?)); then check_err 1 log_test setup_wait_dev ": Interface $dev does not come up." exit 1 fi} setup_wait_dev_with_timeout(){ local dev=$1; shift local max_iterations=${1:-$WAIT_TIMEOUT}; shift local wait_time=${1:-$WAIT_TIME}; shift local i for ((i = 1; i <= $max_iterations; ++i)); do ip link show dev $dev up \ | grep 'state UP' &> /dev/null if [[ $? -ne 0 ]]; then sleep 1 else sleep $wait_time return 0 fi done return 1} setup_wait(){ local num_netifs=${1:-$NUM_NETIFS} local i for ((i = 1; i <= num_netifs; ++i)); do setup_wait_dev ${NETIFS[p$i]} 0 done # Make sure links are ready. sleep $WAIT_TIMELines 1–500 of 1694. Load more
#!/bin/bash # SPDX-License-Identifier: GPL-2.0 ############################################################################## # Defines # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 # Can be overridden by the configuration file. PING=${PING:=ping} PING6=${PING6:=ping6} MZ=${MZ:=mausezahn} ARPING=${ARPING:=arping} TEAMD=${TEAMD:=teamd} WAIT_TIME=${WAIT_TIME:=5} PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no} PAUSE_ON_CLEANUP=${PAUSE_ON_CLEANUP:=no} NETIF_TYPE=${NETIF_TYPE:=veth} NETIF_CREATE=${NETIF_CREATE:=yes} MCD=${MCD:=smcrouted} MC_CLI=${MC_CLI:=smcroutectl} PING_COUNT=${PING_COUNT:=10} PING_TIMEOUT=${PING_TIMEOUT:=5} WAIT_TIMEOUT=${WAIT_TIMEOUT:=20} INTERFACE_TIMEOUT=${INTERFACE_TIMEOUT:=600} LOW_AGEING_TIME=${LOW_AGEING_TIME:=1000} REQUIRE_JQ=${REQUIRE_JQ:=yes} REQUIRE_MZ=${REQUIRE_MZ:=yes} REQUIRE_MTOOLS=${REQUIRE_MTOOLS:=no} STABLE_MAC_ADDRS=${STABLE_MAC_ADDRS:=no} TCPDUMP_EXTRA_FLAGS=${TCPDUMP_EXTRA_FLAGS:=} relative_path="${BASH_SOURCE%/*}" if [[ "$relative_path" == "${BASH_SOURCE}" ]]; then relative_path="." fi if [[ -f $relative_path/forwarding.config ]]; then source "$relative_path/forwarding.config" fi ############################################################################## # Sanity checks check_tc_version() { tc -j &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing JSON support" exit $ksft_skip fi } # Old versions of tc don't understand "mpls_uc" check_tc_mpls_support() { local dev=$1; shift tc filter add dev $dev ingress protocol mpls_uc pref 1 handle 1 \ matchall action pipe &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing MPLS support" return $ksft_skip fi tc filter del dev $dev ingress protocol mpls_uc pref 1 handle 1 \ matchall } # Old versions of tc produce invalid json output for mpls lse statistics check_tc_mpls_lse_stats() { local dev=$1; shift local ret; tc filter add dev $dev ingress protocol mpls_uc pref 1 handle 1 \ flower mpls lse depth 2 \ action continue &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc-flower is missing extended MPLS support" return $ksft_skip fi tc -j filter show dev $dev ingress protocol mpls_uc | jq . &> /dev/null ret=$? tc filter del dev $dev ingress protocol mpls_uc pref 1 handle 1 \ flower if [[ $ret -ne 0 ]]; then echo "SKIP: iproute2 too old; tc-flower produces invalid json output for extended MPLS filters" return $ksft_skip fi } check_tc_shblock_support() { tc filter help 2>&1 | grep block &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing shared block support" exit $ksft_skip fi } check_tc_chain_support() { tc help 2>&1|grep chain &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing chain support" exit $ksft_skip fi } check_tc_action_hw_stats_support() { tc actions help 2>&1 | grep -q hw_stats if [[ $? -ne 0 ]]; then echo "SKIP: iproute2 too old; tc is missing action hw_stats support" exit $ksft_skip fi } check_ethtool_lanes_support() { ethtool --help 2>&1| grep lanes &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: ethtool too old; it is missing lanes support" exit $ksft_skip fi } check_locked_port_support() { if ! bridge -d link show | grep -q " locked"; then echo "SKIP: iproute2 too old; Locked port feature not supported." return $ksft_skip fi } check_port_mab_support() { if ! bridge -d link show | grep -q "mab"; then echo "SKIP: iproute2 too old; MacAuth feature not supported." return $ksft_skip fi } if [[ "$(id -u)" -ne 0 ]]; then echo "SKIP: need root privileges" exit $ksft_skip fi if [[ "$CHECK_TC" = "yes" ]]; then check_tc_version fi require_command() { local cmd=$1; shift if [[ ! -x "$(command -v "$cmd")" ]]; then echo "SKIP: $cmd not installed" exit $ksft_skip fi } if [[ "$REQUIRE_JQ" = "yes" ]]; then require_command jq fi if [[ "$REQUIRE_MZ" = "yes" ]]; then require_command $MZ fi if [[ "$REQUIRE_MTOOLS" = "yes" ]]; then # https://github.com/vladimiroltean/mtools/ # patched for IPv6 support require_command msend require_command mreceive fi if [[ ! -v NUM_NETIFS ]]; then echo "SKIP: importer does not define \"NUM_NETIFS\"" exit $ksft_skip fi ############################################################################## # Command line options handling count=0 while [[ $# -gt 0 ]]; do if [[ "$count" -eq "0" ]]; then unset NETIFS declare -A NETIFS fi count=$((count + 1)) NETIFS[p$count]="$1" shift done ############################################################################## # Network interfaces configuration create_netif_veth() { local i for ((i = 1; i <= NUM_NETIFS; ++i)); do local j=$((i+1)) ip link show dev ${NETIFS[p$i]} &> /dev/null if [[ $? -ne 0 ]]; then ip link add ${NETIFS[p$i]} type veth \ peer name ${NETIFS[p$j]} if [[ $? -ne 0 ]]; then echo "Failed to create netif" exit 1 fi fi i=$j done } create_netif() { case "$NETIF_TYPE" in veth) create_netif_veth ;; *) echo "Can not create interfaces of type \'$NETIF_TYPE\'" exit 1 ;; esac } declare -A MAC_ADDR_ORIG mac_addr_prepare() { local new_addr= local dev= for ((i = 1; i <= NUM_NETIFS; ++i)); do dev=${NETIFS[p$i]} new_addr=$(printf "00:01:02:03:04:%02x" $i) MAC_ADDR_ORIG["$dev"]=$(ip -j link show dev $dev | jq -e '.[].address') # Strip quotes MAC_ADDR_ORIG["$dev"]=${MAC_ADDR_ORIG["$dev"]//\"/} ip link set dev $dev address $new_addr done } mac_addr_restore() { local dev= for ((i = 1; i <= NUM_NETIFS; ++i)); do dev=${NETIFS[p$i]} ip link set dev $dev address ${MAC_ADDR_ORIG["$dev"]} done } if [[ "$NETIF_CREATE" = "yes" ]]; then create_netif fi if [[ "$STABLE_MAC_ADDRS" = "yes" ]]; then mac_addr_prepare fi for ((i = 1; i <= NUM_NETIFS; ++i)); do ip link show dev ${NETIFS[p$i]} &> /dev/null if [[ $? -ne 0 ]]; then echo "SKIP: could not find all required interfaces" exit $ksft_skip fi done ############################################################################## # Helpers # 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 check_err() { local err=$1 local msg=$2 if [[ $RET -eq 0 && $err -ne 0 ]]; then RET=$err retmsg=$msg fi } check_fail() { local err=$1 local msg=$2 if [[ $RET -eq 0 && $err -eq 0 ]]; then RET=1 retmsg=$msg fi } check_err_fail() { local should_fail=$1; shift local err=$1; shift local what=$1; shift if ((should_fail)); then check_fail $err "$what succeeded, but should have failed" else check_err $err "$what failed" fi } log_test() { local test_name=$1 local opt_str=$2 if [[ $# -eq 2 ]]; then opt_str="($opt_str)" fi if [[ $RET -ne 0 ]]; then EXIT_STATUS=1 printf "TEST: %-60s [FAIL]\n" "$test_name $opt_str" if [[ ! -z "$retmsg" ]]; then printf "\t%s\n" "$retmsg" fi if [ "${PAUSE_ON_FAIL}" = "yes" ]; then echo "Hit enter to continue, 'q' to quit" read a [ "$a" = "q" ] && exit 1 fi return 1 fi printf "TEST: %-60s [ OK ]\n" "$test_name $opt_str" return 0 } log_test_skip() { local test_name=$1 local opt_str=$2 printf "TEST: %-60s [SKIP]\n" "$test_name $opt_str" return 0 } log_info() { local msg=$1 echo "INFO: $msg" } busywait() { local timeout=$1; shift local start_time="$(date -u +%s%3N)" while true do local out out=$("$@") local ret=$? if ((!ret)); then echo -n "$out" return 0 fi local current_time="$(date -u +%s%3N)" if ((current_time - start_time > timeout)); then echo -n "$out" return 1 fi done } not() { "$@" [[ $? != 0 ]] } get_max() { local arr=("$@") max=${arr[0]} for cur in ${arr[@]}; do if [[ $cur -gt $max ]]; then max=$cur fi done echo $max } grep_bridge_fdb() { local addr=$1; shift local word local flag if [ "$1" == "self" ] || [ "$1" == "master" ]; then word=$1; shift if [ "$1" == "-v" ]; then flag=$1; shift fi fi $@ | grep $addr | grep $flag "$word" } wait_for_port_up() { "$@" | grep -q "Link detected: yes" } wait_for_offload() { "$@" | grep -q offload } wait_for_trap() { "$@" | grep -q trap } 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))" "$@" } setup_wait_dev() { local dev=$1; shift local wait_time=${1:-$WAIT_TIME}; shift setup_wait_dev_with_timeout "$dev" $INTERFACE_TIMEOUT $wait_time if (($?)); then check_err 1 log_test setup_wait_dev ": Interface $dev does not come up." exit 1 fi } setup_wait_dev_with_timeout() { local dev=$1; shift local max_iterations=${1:-$WAIT_TIMEOUT}; shift local wait_time=${1:-$WAIT_TIME}; shift local i for ((i = 1; i <= $max_iterations; ++i)); do ip link show dev $dev up \ | grep 'state UP' &> /dev/null if [[ $? -ne 0 ]]; then sleep 1 else sleep $wait_time return 0 fi done return 1 } setup_wait() { local num_netifs=${1:-$NUM_NETIFS} local i for ((i = 1; i <= num_netifs; ++i)); do setup_wait_dev ${NETIFS[p$i]} 0 done # Make sure links are ready. sleep $WAIT_TIME