linux/tools/testing/selftests/net/drop_monitor_tests.sh
<<
>>
Prefs
   1#!/bin/bash
   2# SPDX-License-Identifier: GPL-2.0
   3
   4# This test is for checking drop monitor functionality.
   5
   6ret=0
   7# Kselftest framework requirement - SKIP code is 4.
   8ksft_skip=4
   9
  10# all tests in this script. Can be overridden with -t option
  11TESTS="
  12        sw_drops
  13        hw_drops
  14"
  15
  16IP="ip -netns ns1"
  17TC="tc -netns ns1"
  18DEVLINK="devlink -N ns1"
  19NS_EXEC="ip netns exec ns1"
  20NETDEVSIM_PATH=/sys/bus/netdevsim/
  21DEV_ADDR=1337
  22DEV=netdevsim${DEV_ADDR}
  23DEVLINK_DEV=netdevsim/${DEV}
  24
  25log_test()
  26{
  27        local rc=$1
  28        local expected=$2
  29        local msg="$3"
  30
  31        if [ ${rc} -eq ${expected} ]; then
  32                printf "    TEST: %-60s  [ OK ]\n" "${msg}"
  33                nsuccess=$((nsuccess+1))
  34        else
  35                ret=1
  36                nfail=$((nfail+1))
  37                printf "    TEST: %-60s  [FAIL]\n" "${msg}"
  38        fi
  39}
  40
  41setup()
  42{
  43        modprobe netdevsim &> /dev/null
  44
  45        set -e
  46        ip netns add ns1
  47        $IP link add dummy10 up type dummy
  48
  49        $NS_EXEC echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
  50        udevadm settle
  51        local netdev=$($NS_EXEC ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)
  52        $IP link set dev $netdev up
  53
  54        set +e
  55}
  56
  57cleanup()
  58{
  59        $NS_EXEC echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
  60        ip netns del ns1
  61}
  62
  63sw_drops_test()
  64{
  65        echo
  66        echo "Software drops test"
  67
  68        setup
  69
  70        local dir=$(mktemp -d)
  71
  72        $TC qdisc add dev dummy10 clsact
  73        $TC filter add dev dummy10 egress pref 1 handle 101 proto ip \
  74                flower dst_ip 192.0.2.10 action drop
  75
  76        $NS_EXEC mausezahn dummy10 -a 00:11:22:33:44:55 -b 00:aa:bb:cc:dd:ee \
  77                -A 192.0.2.1 -B 192.0.2.10 -t udp sp=12345,dp=54321 -c 0 -q \
  78                -d 100msec &
  79        timeout 5 dwdump -o sw -w ${dir}/packets.pcap
  80        (( $(tshark -r ${dir}/packets.pcap \
  81                -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) != 0))
  82        log_test $? 0 "Capturing active software drops"
  83
  84        rm ${dir}/packets.pcap
  85
  86        { kill %% && wait %%; } 2>/dev/null
  87        timeout 5 dwdump -o sw -w ${dir}/packets.pcap
  88        (( $(tshark -r ${dir}/packets.pcap \
  89                -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) == 0))
  90        log_test $? 0 "Capturing inactive software drops"
  91
  92        rm -r $dir
  93
  94        cleanup
  95}
  96
  97hw_drops_test()
  98{
  99        echo
 100        echo "Hardware drops test"
 101
 102        setup
 103
 104        local dir=$(mktemp -d)
 105
 106        $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action trap
 107        timeout 5 dwdump -o hw -w ${dir}/packets.pcap
 108        (( $(tshark -r ${dir}/packets.pcap \
 109                -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
 110                | wc -l) != 0))
 111        log_test $? 0 "Capturing active hardware drops"
 112
 113        rm ${dir}/packets.pcap
 114
 115        $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action drop
 116        timeout 5 dwdump -o hw -w ${dir}/packets.pcap
 117        (( $(tshark -r ${dir}/packets.pcap \
 118                -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
 119                | wc -l) == 0))
 120        log_test $? 0 "Capturing inactive hardware drops"
 121
 122        rm -r $dir
 123
 124        cleanup
 125}
 126
 127################################################################################
 128# usage
 129
 130usage()
 131{
 132        cat <<EOF
 133usage: ${0##*/} OPTS
 134
 135        -t <test>   Test(s) to run (default: all)
 136                    (options: $TESTS)
 137EOF
 138}
 139
 140################################################################################
 141# main
 142
 143while getopts ":t:h" opt; do
 144        case $opt in
 145                t) TESTS=$OPTARG;;
 146                h) usage; exit 0;;
 147                *) usage; exit 1;;
 148        esac
 149done
 150
 151if [ "$(id -u)" -ne 0 ];then
 152        echo "SKIP: Need root privileges"
 153        exit $ksft_skip;
 154fi
 155
 156if [ ! -x "$(command -v ip)" ]; then
 157        echo "SKIP: Could not run test without ip tool"
 158        exit $ksft_skip
 159fi
 160
 161if [ ! -x "$(command -v devlink)" ]; then
 162        echo "SKIP: Could not run test without devlink tool"
 163        exit $ksft_skip
 164fi
 165
 166if [ ! -x "$(command -v tshark)" ]; then
 167        echo "SKIP: Could not run test without tshark tool"
 168        exit $ksft_skip
 169fi
 170
 171if [ ! -x "$(command -v dwdump)" ]; then
 172        echo "SKIP: Could not run test without dwdump tool"
 173        exit $ksft_skip
 174fi
 175
 176if [ ! -x "$(command -v udevadm)" ]; then
 177        echo "SKIP: Could not run test without udevadm tool"
 178        exit $ksft_skip
 179fi
 180
 181if [ ! -x "$(command -v timeout)" ]; then
 182        echo "SKIP: Could not run test without timeout tool"
 183        exit $ksft_skip
 184fi
 185
 186if [ ! -x "$(command -v mausezahn)" ]; then
 187        echo "SKIP: Could not run test without mausezahn tool"
 188        exit $ksft_skip
 189fi
 190
 191tshark -G fields 2> /dev/null | grep -q net_dm
 192if [ $? -ne 0 ]; then
 193        echo "SKIP: tshark too old, missing net_dm dissector"
 194        exit $ksft_skip
 195fi
 196
 197# start clean
 198cleanup &> /dev/null
 199
 200for t in $TESTS
 201do
 202        case $t in
 203        sw_drops|sw)                    sw_drops_test;;
 204        hw_drops|hw)                    hw_drops_test;;
 205
 206        help) echo "Test names: $TESTS"; exit 0;;
 207        esac
 208done
 209
 210if [ "$TESTS" != "none" ]; then
 211        printf "\nTests passed: %3d\n" ${nsuccess}
 212        printf "Tests failed: %3d\n"   ${nfail}
 213fi
 214
 215exit $ret
 216