0001
0002
0003
0004
0005
0006 ret=0
0007
0008 ksft_skip=4
0009
0010
0011 TESTS="
0012 sw_drops
0013 hw_drops
0014 "
0015
0016 IP="ip -netns ns1"
0017 TC="tc -netns ns1"
0018 DEVLINK="devlink -N ns1"
0019 NS_EXEC="ip netns exec ns1"
0020 NETDEVSIM_PATH=/sys/bus/netdevsim/
0021 DEV_ADDR=1337
0022 DEV=netdevsim${DEV_ADDR}
0023 DEVLINK_DEV=netdevsim/${DEV}
0024
0025 log_test()
0026 {
0027 local rc=$1
0028 local expected=$2
0029 local msg="$3"
0030
0031 if [ ${rc} -eq ${expected} ]; then
0032 printf " TEST: %-60s [ OK ]\n" "${msg}"
0033 nsuccess=$((nsuccess+1))
0034 else
0035 ret=1
0036 nfail=$((nfail+1))
0037 printf " TEST: %-60s [FAIL]\n" "${msg}"
0038 fi
0039 }
0040
0041 setup()
0042 {
0043 modprobe netdevsim &> /dev/null
0044
0045 set -e
0046 ip netns add ns1
0047 $IP link add dummy10 up type dummy
0048
0049 $NS_EXEC echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
0050 udevadm settle
0051 local netdev=$($NS_EXEC ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)
0052 $IP link set dev $netdev up
0053
0054 set +e
0055 }
0056
0057 cleanup()
0058 {
0059 $NS_EXEC echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
0060 ip netns del ns1
0061 }
0062
0063 sw_drops_test()
0064 {
0065 echo
0066 echo "Software drops test"
0067
0068 setup
0069
0070 local dir=$(mktemp -d)
0071
0072 $TC qdisc add dev dummy10 clsact
0073 $TC filter add dev dummy10 egress pref 1 handle 101 proto ip \
0074 flower dst_ip 192.0.2.10 action drop
0075
0076 $NS_EXEC mausezahn dummy10 -a 00:11:22:33:44:55 -b 00:aa:bb:cc:dd:ee \
0077 -A 192.0.2.1 -B 192.0.2.10 -t udp sp=12345,dp=54321 -c 0 -q \
0078 -d 100msec &
0079 timeout 5 dwdump -o sw -w ${dir}/packets.pcap
0080 (( $(tshark -r ${dir}/packets.pcap \
0081 -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) != 0))
0082 log_test $? 0 "Capturing active software drops"
0083
0084 rm ${dir}/packets.pcap
0085
0086 { kill %% && wait %%; } 2>/dev/null
0087 timeout 5 dwdump -o sw -w ${dir}/packets.pcap
0088 (( $(tshark -r ${dir}/packets.pcap \
0089 -Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) == 0))
0090 log_test $? 0 "Capturing inactive software drops"
0091
0092 rm -r $dir
0093
0094 cleanup
0095 }
0096
0097 hw_drops_test()
0098 {
0099 echo
0100 echo "Hardware drops test"
0101
0102 setup
0103
0104 local dir=$(mktemp -d)
0105
0106 $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action trap
0107 timeout 5 dwdump -o hw -w ${dir}/packets.pcap
0108 (( $(tshark -r ${dir}/packets.pcap \
0109 -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
0110 | wc -l) != 0))
0111 log_test $? 0 "Capturing active hardware drops"
0112
0113 rm ${dir}/packets.pcap
0114
0115 $DEVLINK trap set $DEVLINK_DEV trap blackhole_route action drop
0116 timeout 5 dwdump -o hw -w ${dir}/packets.pcap
0117 (( $(tshark -r ${dir}/packets.pcap \
0118 -Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
0119 | wc -l) == 0))
0120 log_test $? 0 "Capturing inactive hardware drops"
0121
0122 rm -r $dir
0123
0124 cleanup
0125 }
0126
0127
0128
0129
0130 usage()
0131 {
0132 cat <<EOF
0133 usage: ${0
0134
0135 -t <test> Test(s) to run (default: all)
0136 (options: $TESTS)
0137 EOF
0138 }
0139
0140
0141
0142
0143 while getopts ":t:h" opt; do
0144 case $opt in
0145 t) TESTS=$OPTARG;;
0146 h) usage; exit 0;;
0147 *) usage; exit 1;;
0148 esac
0149 done
0150
0151 if [ "$(id -u)" -ne 0 ];then
0152 echo "SKIP: Need root privileges"
0153 exit $ksft_skip;
0154 fi
0155
0156 if [ ! -x "$(command -v ip)" ]; then
0157 echo "SKIP: Could not run test without ip tool"
0158 exit $ksft_skip
0159 fi
0160
0161 if [ ! -x "$(command -v devlink)" ]; then
0162 echo "SKIP: Could not run test without devlink tool"
0163 exit $ksft_skip
0164 fi
0165
0166 if [ ! -x "$(command -v tshark)" ]; then
0167 echo "SKIP: Could not run test without tshark tool"
0168 exit $ksft_skip
0169 fi
0170
0171 if [ ! -x "$(command -v dwdump)" ]; then
0172 echo "SKIP: Could not run test without dwdump tool"
0173 exit $ksft_skip
0174 fi
0175
0176 if [ ! -x "$(command -v udevadm)" ]; then
0177 echo "SKIP: Could not run test without udevadm tool"
0178 exit $ksft_skip
0179 fi
0180
0181 if [ ! -x "$(command -v timeout)" ]; then
0182 echo "SKIP: Could not run test without timeout tool"
0183 exit $ksft_skip
0184 fi
0185
0186 if [ ! -x "$(command -v mausezahn)" ]; then
0187 echo "SKIP: Could not run test without mausezahn tool"
0188 exit $ksft_skip
0189 fi
0190
0191 tshark -G fields 2> /dev/null | grep -q net_dm
0192 if [ $? -ne 0 ]; then
0193 echo "SKIP: tshark too old, missing net_dm dissector"
0194 exit $ksft_skip
0195 fi
0196
0197
0198 cleanup &> /dev/null
0199
0200 for t in $TESTS
0201 do
0202 case $t in
0203 sw_drops|sw) sw_drops_test;;
0204 hw_drops|hw) hw_drops_test;;
0205
0206 help) echo "Test names: $TESTS"; exit 0;;
0207 esac
0208 done
0209
0210 if [ "$TESTS" != "none" ]; then
0211 printf "\nTests passed: %3d\n" ${nsuccess}
0212 printf "Tests failed: %3d\n" ${nfail}
0213 fi
0214
0215 exit $ret