brintos

brintos / linux-shallow public Read only

0
0
Text · 4.0 KiB · 5100d90 Raw
236 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# This test is for checking GRE GSO.5source lib.sh6ret=07 8# all tests in this script. Can be overridden with -t option9TESTS="gre_gso"10 11VERBOSE=012PAUSE_ON_FAIL=no13PAUSE=no14TMPFILE=`mktemp`15PID=16 17log_test()18{19	local rc=$120	local expected=$221	local msg="$3"22 23	if [ ${rc} -eq ${expected} ]; then24		printf "    TEST: %-60s  [ OK ]\n" "${msg}"25		nsuccess=$((nsuccess+1))26	else27		ret=128		nfail=$((nfail+1))29		printf "    TEST: %-60s  [FAIL]\n" "${msg}"30		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then31		echo32			echo "hit enter to continue, 'q' to quit"33			read a34			[ "$a" = "q" ] && exit 135		fi36	fi37 38	if [ "${PAUSE}" = "yes" ]; then39		echo40		echo "hit enter to continue, 'q' to quit"41		read a42		[ "$a" = "q" ] && exit 143	fi44}45 46setup()47{48	set -e49	setup_ns ns150	IP="ip -netns $ns1"51	NS_EXEC="ip netns exec $ns1"52 53	ip link add veth0 type veth peer name veth154	ip link set veth0 up55	ip link set veth1 netns $ns156	$IP link set veth1 name veth057	$IP link set veth0 up58 59	dd if=/dev/urandom of=$TMPFILE bs=1024 count=2048 &>/dev/null60	set +e61}62 63cleanup()64{65	rm -rf $TMPFILE66	[ -n "$PID" ] && kill $PID67	ip link del dev gre1 &> /dev/null68	ip link del dev veth0 &> /dev/null69	cleanup_ns $ns170}71 72get_linklocal()73{74	local dev=$175	local ns=$276	local addr77 78	[ -n "$ns" ] && ns="-netns $ns"79 80	addr=$(ip -6 -br $ns addr show dev ${dev} | \81	awk '{82		for (i = 3; i <= NF; ++i) {83			if ($i ~ /^fe80/)84				print $i85		}86	}'87	)88	addr=${addr/\/*}89 90	[ -z "$addr" ] && return 191 92	echo $addr93 94	return 095}96 97gre_create_tun()98{99	local a1=$1100	local a2=$2101	local mode102 103	[[ $a1 =~ ^[0-9.]*$ ]] && mode=gre || mode=ip6gre104 105	ip tunnel add gre1 mode $mode local $a1 remote $a2 dev veth0106	ip link set gre1 up107	$IP tunnel add gre1 mode $mode local $a2 remote $a1 dev veth0108	$IP link set gre1 up109}110 111gre_gst_test_checks()112{113	local name=$1114	local addr=$2115	local proto=$3116 117	[ "$proto" == 6 ] && addr="[$addr]"118 119	$NS_EXEC socat - tcp${proto}-listen:$port,reuseaddr,fork >/dev/null &120	PID=$!121	while ! $NS_EXEC ss -ltn | grep -q $port; do ((i++)); sleep 0.01; done122 123	cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port124	log_test $? 0 "$name - copy file w/ TSO"125 126	ethtool -K veth0 tso off127 128	cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port129	log_test $? 0 "$name - copy file w/ GSO"130 131	ethtool -K veth0 tso on132 133	kill $PID134	PID=135}136 137gre6_gso_test()138{139	local port=7777140 141	setup142 143	a1=$(get_linklocal veth0)144	a2=$(get_linklocal veth0 $ns1)145 146	gre_create_tun $a1 $a2147 148	ip  addr add 172.16.2.1/24 dev gre1149	$IP addr add 172.16.2.2/24 dev gre1150 151	ip  -6 addr add 2001:db8:1::1/64 dev gre1 nodad152	$IP -6 addr add 2001:db8:1::2/64 dev gre1 nodad153 154	sleep 2155 156	gre_gst_test_checks GREv6/v4 172.16.2.2 4157	gre_gst_test_checks GREv6/v6 2001:db8:1::2 6158 159	cleanup160}161 162gre_gso_test()163{164	gre6_gso_test165}166 167################################################################################168# usage169 170usage()171{172	cat <<EOF173usage: ${0##*/} OPTS174 175        -t <test>   Test(s) to run (default: all)176                    (options: $TESTS)177        -p          Pause on fail178        -P          Pause after each test before cleanup179        -v          verbose mode (show commands and output)180EOF181}182 183################################################################################184# main185 186while getopts :t:pPhv o187do188	case $o in189		t) TESTS=$OPTARG;;190		p) PAUSE_ON_FAIL=yes;;191		P) PAUSE=yes;;192		v) VERBOSE=$(($VERBOSE + 1));;193		h) usage; exit 0;;194		*) usage; exit 1;;195	esac196done197 198PEER_CMD="ip netns exec ${PEER_NS}"199 200# make sure we don't pause twice201[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no202 203if [ "$(id -u)" -ne 0 ];then204	echo "SKIP: Need root privileges"205	exit $ksft_skip;206fi207 208if [ ! -x "$(command -v ip)" ]; then209	echo "SKIP: Could not run test without ip tool"210	exit $ksft_skip211fi212 213if [ ! -x "$(command -v socat)" ]; then214	echo "SKIP: Could not run test without socat tool"215	exit $ksft_skip216fi217 218# start clean219cleanup &> /dev/null220 221for t in $TESTS222do223	case $t in224	gre_gso)		gre_gso_test;;225 226	help) echo "Test names: $TESTS"; exit 0;;227	esac228done229 230if [ "$TESTS" != "none" ]; then231	printf "\nTests passed: %3d\n" ${nsuccess}232	printf "Tests failed: %3d\n"   ${nfail}233fi234 235exit $ret236