1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking GRE GSO.
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="gre_gso"
12
13VERBOSE=0
14PAUSE_ON_FAIL=no
15PAUSE=no
16IP="ip -netns ns1"
17NS_EXEC="ip netns exec ns1"
18TMPFILE=`mktemp`
19PID=
20
21log_test()
22{
23	local rc=$1
24	local expected=$2
25	local msg="$3"
26
27	if [ ${rc} -eq ${expected} ]; then
28		printf "    TEST: %-60s  [ OK ]\n" "${msg}"
29		nsuccess=$((nsuccess+1))
30	else
31		ret=1
32		nfail=$((nfail+1))
33		printf "    TEST: %-60s  [FAIL]\n" "${msg}"
34		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
35		echo
36			echo "hit enter to continue, 'q' to quit"
37			read a
38			[ "$a" = "q" ] && exit 1
39		fi
40	fi
41
42	if [ "${PAUSE}" = "yes" ]; then
43		echo
44		echo "hit enter to continue, 'q' to quit"
45		read a
46		[ "$a" = "q" ] && exit 1
47	fi
48}
49
50setup()
51{
52	set -e
53	ip netns add ns1
54	ip netns set ns1 auto
55	$IP link set dev lo up
56
57	ip link add veth0 type veth peer name veth1
58	ip link set veth0 up
59	ip link set veth1 netns ns1
60	$IP link set veth1 name veth0
61	$IP link set veth0 up
62
63	dd if=/dev/urandom of=$TMPFILE bs=1024 count=2048 &>/dev/null
64	set +e
65}
66
67cleanup()
68{
69	rm -rf $TMPFILE
70	[ -n "$PID" ] && kill $PID
71	ip link del dev gre1 &> /dev/null
72	ip link del dev veth0 &> /dev/null
73	ip netns del ns1
74}
75
76get_linklocal()
77{
78	local dev=$1
79	local ns=$2
80	local addr
81
82	[ -n "$ns" ] && ns="-netns $ns"
83
84	addr=$(ip -6 -br $ns addr show dev ${dev} | \
85	awk '{
86		for (i = 3; i <= NF; ++i) {
87			if ($i ~ /^fe80/)
88				print $i
89		}
90	}'
91	)
92	addr=${addr/\/*}
93
94	[ -z "$addr" ] && return 1
95
96	echo $addr
97
98	return 0
99}
100
101gre_create_tun()
102{
103	local a1=$1
104	local a2=$2
105	local mode
106
107	[[ $a1 =~ ^[0-9.]*$ ]] && mode=gre || mode=ip6gre
108
109	ip tunnel add gre1 mode $mode local $a1 remote $a2 dev veth0
110	ip link set gre1 up
111	$IP tunnel add gre1 mode $mode local $a2 remote $a1 dev veth0
112	$IP link set gre1 up
113}
114
115gre_gst_test_checks()
116{
117	local name=$1
118	local addr=$2
119
120	$NS_EXEC nc -kl $port >/dev/null &
121	PID=$!
122	while ! $NS_EXEC ss -ltn | grep -q $port; do ((i++)); sleep 0.01; done
123
124	cat $TMPFILE | timeout 1 nc $addr $port
125	log_test $? 0 "$name - copy file w/ TSO"
126
127	ethtool -K veth0 tso off
128
129	cat $TMPFILE | timeout 1 nc $addr $port
130	log_test $? 0 "$name - copy file w/ GSO"
131
132	ethtool -K veth0 tso on
133
134	kill $PID
135	PID=
136}
137
138gre6_gso_test()
139{
140	local port=7777
141
142	setup
143
144	a1=$(get_linklocal veth0)
145	a2=$(get_linklocal veth0 ns1)
146
147	gre_create_tun $a1 $a2
148
149	ip  addr add 172.16.2.1/24 dev gre1
150	$IP addr add 172.16.2.2/24 dev gre1
151
152	ip  -6 addr add 2001:db8:1::1/64 dev gre1 nodad
153	$IP -6 addr add 2001:db8:1::2/64 dev gre1 nodad
154
155	sleep 2
156
157	gre_gst_test_checks GREv6/v4 172.16.2.2
158	gre_gst_test_checks GREv6/v6 2001:db8:1::2
159
160	cleanup
161}
162
163gre_gso_test()
164{
165	gre6_gso_test
166}
167
168################################################################################
169# usage
170
171usage()
172{
173	cat <<EOF
174usage: ${0##*/} OPTS
175
176        -t <test>   Test(s) to run (default: all)
177                    (options: $TESTS)
178        -p          Pause on fail
179        -P          Pause after each test before cleanup
180        -v          verbose mode (show commands and output)
181EOF
182}
183
184################################################################################
185# main
186
187while getopts :t:pPhv o
188do
189	case $o in
190		t) TESTS=$OPTARG;;
191		p) PAUSE_ON_FAIL=yes;;
192		P) PAUSE=yes;;
193		v) VERBOSE=$(($VERBOSE + 1));;
194		h) usage; exit 0;;
195		*) usage; exit 1;;
196	esac
197done
198
199PEER_CMD="ip netns exec ${PEER_NS}"
200
201# make sure we don't pause twice
202[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
203
204if [ "$(id -u)" -ne 0 ];then
205	echo "SKIP: Need root privileges"
206	exit $ksft_skip;
207fi
208
209if [ ! -x "$(command -v ip)" ]; then
210	echo "SKIP: Could not run test without ip tool"
211	exit $ksft_skip
212fi
213
214if [ ! -x "$(command -v nc)" ]; then
215	echo "SKIP: Could not run test without nc tool"
216	exit $ksft_skip
217fi
218
219# start clean
220cleanup &> /dev/null
221
222for t in $TESTS
223do
224	case $t in
225	gre_gso)		gre_gso_test;;
226
227	help) echo "Test names: $TESTS"; exit 0;;
228	esac
229done
230
231if [ "$TESTS" != "none" ]; then
232	printf "\nTests passed: %3d\n" ${nsuccess}
233	printf "Tests failed: %3d\n"   ${nfail}
234fi
235
236exit $ret
237