brintos

brintos / linux-shallow public Read only

0
0
Text · 4.4 KiB · 7c4818c Raw
217 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# This test is for checking drop monitor functionality.5source lib.sh6ret=07 8# all tests in this script. Can be overridden with -t option9TESTS="10	sw_drops11	hw_drops12"13 14NETDEVSIM_PATH=/sys/bus/netdevsim/15DEV_ADDR=133716DEV=netdevsim${DEV_ADDR}17DEVLINK_DEV=netdevsim/${DEV}18 19log_test()20{21	local rc=$122	local expected=$223	local msg="$3"24 25	if [ ${rc} -eq ${expected} ]; then26		printf "    TEST: %-60s  [ OK ]\n" "${msg}"27		nsuccess=$((nsuccess+1))28	else29		ret=130		nfail=$((nfail+1))31		printf "    TEST: %-60s  [FAIL]\n" "${msg}"32	fi33}34 35setup()36{37	modprobe netdevsim &> /dev/null38 39	set -e40	setup_ns NS141	$IP link add dummy10 up type dummy42 43	$NS_EXEC echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device44	udevadm settle45	local netdev=$($NS_EXEC ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)46	$IP link set dev $netdev up47 48	set +e49}50 51cleanup()52{53	$NS_EXEC echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device54	cleanup_ns ${NS1}55}56 57sw_drops_test()58{59	echo60	echo "Software drops test"61 62	setup63 64	local dir=$(mktemp -d)65 66	$TC qdisc add dev dummy10 clsact67	$TC filter add dev dummy10 egress pref 1 handle 101 proto ip \68		flower dst_ip 192.0.2.10 action drop69 70	$NS_EXEC mausezahn dummy10 -a 00:11:22:33:44:55 -b 00:aa:bb:cc:dd:ee \71		-A 192.0.2.1 -B 192.0.2.10 -t udp sp=12345,dp=54321 -c 0 -q \72		-d 100msec &73	timeout 5 dwdump -o sw -w ${dir}/packets.pcap74	(( $(tshark -r ${dir}/packets.pcap \75		-Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) != 0))76	log_test $? 0 "Capturing active software drops"77 78	rm ${dir}/packets.pcap79 80	{ kill %% && wait %%; } 2>/dev/null81	timeout 5 dwdump -o sw -w ${dir}/packets.pcap82	(( $(tshark -r ${dir}/packets.pcap \83		-Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) == 0))84	log_test $? 0 "Capturing inactive software drops"85 86	rm -r $dir87 88	cleanup89}90 91hw_drops_test()92{93	echo94	echo "Hardware drops test"95 96	setup97 98	local dir=$(mktemp -d)99 100	$DEVLINK trap set $DEVLINK_DEV trap blackhole_route action trap101	timeout 5 dwdump -o hw -w ${dir}/packets.pcap102	(( $(tshark -r ${dir}/packets.pcap \103		-Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \104		| wc -l) != 0))105	log_test $? 0 "Capturing active hardware drops"106 107	rm ${dir}/packets.pcap108 109	$DEVLINK trap set $DEVLINK_DEV trap blackhole_route action drop110	timeout 5 dwdump -o hw -w ${dir}/packets.pcap111	(( $(tshark -r ${dir}/packets.pcap \112		-Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \113		| wc -l) == 0))114	log_test $? 0 "Capturing inactive hardware drops"115 116	rm -r $dir117 118	cleanup119}120 121################################################################################122# usage123 124usage()125{126	cat <<EOF127usage: ${0##*/} OPTS128 129        -t <test>   Test(s) to run (default: all)130                    (options: $TESTS)131EOF132}133 134################################################################################135# main136 137while getopts ":t:h" opt; do138	case $opt in139		t) TESTS=$OPTARG;;140		h) usage; exit 0;;141		*) usage; exit 1;;142	esac143done144 145if [ "$(id -u)" -ne 0 ];then146	echo "SKIP: Need root privileges"147	exit $ksft_skip;148fi149 150if [ ! -x "$(command -v ip)" ]; then151	echo "SKIP: Could not run test without ip tool"152	exit $ksft_skip153fi154 155if [ ! -x "$(command -v devlink)" ]; then156	echo "SKIP: Could not run test without devlink tool"157	exit $ksft_skip158fi159 160if [ ! -x "$(command -v tshark)" ]; then161	echo "SKIP: Could not run test without tshark tool"162	exit $ksft_skip163fi164 165if [ ! -x "$(command -v dwdump)" ]; then166	echo "SKIP: Could not run test without dwdump tool"167	exit $ksft_skip168fi169 170if [ ! -x "$(command -v udevadm)" ]; then171	echo "SKIP: Could not run test without udevadm tool"172	exit $ksft_skip173fi174 175if [ ! -x "$(command -v timeout)" ]; then176	echo "SKIP: Could not run test without timeout tool"177	exit $ksft_skip178fi179 180if [ ! -x "$(command -v mausezahn)" ]; then181	echo "SKIP: Could not run test without mausezahn tool"182	exit $ksft_skip183fi184 185tshark -G fields 2> /dev/null | grep -q net_dm186if [ $? -ne 0 ]; then187	echo "SKIP: tshark too old, missing net_dm dissector"188	exit $ksft_skip189fi190 191# create netns first so we can get the namespace name192setup_ns NS1193cleanup &> /dev/null194trap cleanup EXIT195 196IP="ip -netns ${NS1}"197TC="tc -netns ${NS1}"198DEVLINK="devlink -N ${NS1}"199NS_EXEC="ip netns exec ${NS1}"200 201for t in $TESTS202do203	case $t in204	sw_drops|sw)			sw_drops_test;;205	hw_drops|hw)			hw_drops_test;;206 207	help) echo "Test names: $TESTS"; exit 0;;208	esac209done210 211if [ "$TESTS" != "none" ]; then212	printf "\nTests passed: %3d\n" ${nsuccess}213	printf "Tests failed: %3d\n"   ${nfail}214fi215 216exit $ret217