1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4BPF_FILE="xdp_dummy.o"
5readonly STATS="$(mktemp -p /tmp ns-XXXXXX)"
6readonly BASE=`basename $STATS`
7readonly SRC=2
8readonly DST=1
9readonly DST_NAT=100
10readonly NS_SRC=$BASE$SRC
11readonly NS_DST=$BASE$DST
12
13# "baremetal" network used for raw UDP traffic
14readonly BM_NET_V4=192.168.1.
15readonly BM_NET_V6=2001:db8::
16
17readonly CPUS=`nproc`
18ret=0
19
20cleanup() {
21	local ns
22	local jobs
23	readonly jobs="$(jobs -p)"
24	[ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null
25	rm -f $STATS
26
27	for ns in $NS_SRC $NS_DST; do
28		ip netns del $ns 2>/dev/null
29	done
30}
31
32trap cleanup EXIT
33
34create_ns() {
35	local ns
36
37	for ns in $NS_SRC $NS_DST; do
38		ip netns add $ns
39		ip -n $ns link set dev lo up
40	done
41
42	ip link add name veth$SRC type veth peer name veth$DST
43
44	for ns in $SRC $DST; do
45		ip link set dev veth$ns netns $BASE$ns up
46		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V4$ns/24
47		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V6$ns/64 nodad
48	done
49	echo "#kernel" > $BASE
50	chmod go-rw $BASE
51}
52
53__chk_flag() {
54	local msg="$1"
55	local target=$2
56	local expected=$3
57	local flagname=$4
58
59	local flag=`ip netns exec $BASE$target ethtool -k veth$target |\
60		    grep $flagname | awk '{print $2}'`
61
62	printf "%-60s" "$msg"
63	if [ "$flag" = "$expected" ]; then
64		echo " ok "
65	else
66		echo " fail - expected $expected found $flag"
67		ret=1
68	fi
69}
70
71chk_gro_flag() {
72	__chk_flag "$1" $2 $3 generic-receive-offload
73}
74
75chk_tso_flag() {
76	__chk_flag "$1" $2 $3 tcp-segmentation-offload
77}
78
79chk_channels() {
80	local msg="$1"
81	local target=$2
82	local rx=$3
83	local tx=$4
84
85	local dev=veth$target
86
87	local cur_rx=`ip netns exec $BASE$target ethtool -l $dev |\
88		grep RX: | tail -n 1 | awk '{print $2}' `
89		local cur_tx=`ip netns exec $BASE$target ethtool -l $dev |\
90		grep TX: | tail -n 1 | awk '{print $2}'`
91	local cur_combined=`ip netns exec $BASE$target ethtool -l $dev |\
92		grep Combined: | tail -n 1 | awk '{print $2}'`
93
94	printf "%-60s" "$msg"
95	if [ "$cur_rx" = "$rx" -a "$cur_tx" = "$tx" -a "$cur_combined" = "n/a" ]; then
96		echo " ok "
97	else
98		echo " fail rx:$rx:$cur_rx tx:$tx:$cur_tx combined:n/a:$cur_combined"
99	fi
100}
101
102chk_gro() {
103	local msg="$1"
104	local expected=$2
105
106	ip netns exec $BASE$SRC ping -qc 1 $BM_NET_V4$DST >/dev/null
107	NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat -n
108
109	printf "%-60s" "$msg"
110	ip netns exec $BASE$DST ./udpgso_bench_rx -C 1000 -R 10 &
111	local spid=$!
112	sleep 0.1
113
114	ip netns exec $NS_SRC ./udpgso_bench_tx -4 -s 13000 -S 1300 -M 1 -D $BM_NET_V4$DST
115	local retc=$?
116	wait $spid
117	local rets=$?
118	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
119		echo " fail client exit code $retc, server $rets"
120		ret=1
121		return
122	fi
123
124	local pkts=`NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat IpInReceives | \
125		    awk '{print $2}' | tail -n 1`
126	if [ "$pkts" = "$expected" ]; then
127		echo " ok "
128	else
129		echo " fail - got $pkts packets, expected $expected "
130		ret=1
131	fi
132}
133
134__change_channels()
135{
136	local cur_cpu
137	local end=$1
138	local cur
139	local i
140
141	while true; do
142		printf -v cur '%(%s)T'
143		[ $cur -le $end ] || break
144
145		for i in `seq 1 $CPUS`; do
146			ip netns exec $NS_SRC ethtool -L veth$SRC rx $i tx $i
147			ip netns exec $NS_DST ethtool -L veth$DST rx $i tx $i
148		done
149
150		for i in `seq 1 $((CPUS - 1))`; do
151			cur_cpu=$((CPUS - $i))
152			ip netns exec $NS_SRC ethtool -L veth$SRC rx $cur_cpu tx $cur_cpu
153			ip netns exec $NS_DST ethtool -L veth$DST rx $cur_cpu tx $cur_cpu
154		done
155	done
156}
157
158__send_data() {
159	local end=$1
160
161	while true; do
162		printf -v cur '%(%s)T'
163		[ $cur -le $end ] || break
164
165		ip netns exec $NS_SRC ./udpgso_bench_tx -4 -s 1000 -M 300 -D $BM_NET_V4$DST
166	done
167}
168
169do_stress() {
170	local end
171	printf -v end '%(%s)T'
172	end=$((end + $STRESS))
173
174	ip netns exec $NS_SRC ethtool -L veth$SRC rx 3 tx 3
175	ip netns exec $NS_DST ethtool -L veth$DST rx 3 tx 3
176
177	ip netns exec $NS_DST ./udpgso_bench_rx &
178	local rx_pid=$!
179
180	echo "Running stress test for $STRESS seconds..."
181	__change_channels $end &
182	local ch_pid=$!
183	__send_data $end &
184	local data_pid_1=$!
185	__send_data $end &
186	local data_pid_2=$!
187	__send_data $end &
188	local data_pid_3=$!
189	__send_data $end &
190	local data_pid_4=$!
191
192	wait $ch_pid $data_pid_1 $data_pid_2 $data_pid_3 $data_pid_4
193	kill -9 $rx_pid
194	echo "done"
195
196	# restore previous setting
197	ip netns exec $NS_SRC ethtool -L veth$SRC rx 2 tx 2
198	ip netns exec $NS_DST ethtool -L veth$DST rx 2 tx 1
199}
200
201usage() {
202	echo "Usage: $0 [-h] [-s <seconds>]"
203	echo -e "\t-h: show this help"
204	echo -e "\t-s: run optional stress tests for the given amount of seconds"
205}
206
207STRESS=0
208while getopts "hs:" option; do
209	case "$option" in
210	"h")
211		usage $0
212		exit 0
213		;;
214	"s")
215		STRESS=$OPTARG
216		;;
217	esac
218done
219
220if [ ! -f ${BPF_FILE} ]; then
221	echo "Missing ${BPF_FILE}. Run 'make' first"
222	exit 1
223fi
224
225[ $CPUS -lt 2 ] && echo "Only one CPU available, some tests will be skipped"
226[ $STRESS -gt 0 -a $CPUS -lt 3 ] && echo " stress test will be skipped, too"
227
228create_ns
229chk_gro_flag "default - gro flag" $SRC off
230chk_gro_flag "        - peer gro flag" $DST off
231chk_tso_flag "        - tso flag" $SRC on
232chk_tso_flag "        - peer tso flag" $DST on
233chk_gro "        - aggregation" 1
234ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
235chk_gro "        - aggregation with TSO off" 10
236cleanup
237
238create_ns
239ip netns exec $NS_DST ethtool -K veth$DST gro on
240chk_gro_flag "with gro on - gro flag" $DST on
241chk_gro_flag "        - peer gro flag" $SRC off
242chk_tso_flag "        - tso flag" $SRC on
243chk_tso_flag "        - peer tso flag" $DST on
244ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
245ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
246chk_gro "        - aggregation with TSO off" 1
247cleanup
248
249create_ns
250chk_channels "default channels" $DST 1 1
251
252ip -n $NS_DST link set dev veth$DST down
253ip netns exec $NS_DST ethtool -K veth$DST gro on
254chk_gro_flag "with gro enabled on link down - gro flag" $DST on
255chk_gro_flag "        - peer gro flag" $SRC off
256chk_tso_flag "        - tso flag" $SRC on
257chk_tso_flag "        - peer tso flag" $DST on
258ip -n $NS_DST link set dev veth$DST up
259ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
260ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
261chk_gro "        - aggregation with TSO off" 1
262cleanup
263
264create_ns
265
266CUR_TX=1
267CUR_RX=1
268if [ $CPUS -gt 1 ]; then
269	ip netns exec $NS_DST ethtool -L veth$DST tx 2
270	chk_channels "setting tx channels" $DST 1 2
271	CUR_TX=2
272fi
273
274if [ $CPUS -gt 2 ]; then
275	ip netns exec $NS_DST ethtool -L veth$DST rx 3 tx 3
276	chk_channels "setting both rx and tx channels" $DST 3 3
277	CUR_RX=3
278	CUR_TX=3
279fi
280
281ip netns exec $NS_DST ethtool -L veth$DST combined 2 2>/dev/null
282chk_channels "bad setting: combined channels" $DST $CUR_RX $CUR_TX
283
284ip netns exec $NS_DST ethtool -L veth$DST tx $((CPUS + 1)) 2>/dev/null
285chk_channels "setting invalid channels nr" $DST $CUR_RX $CUR_TX
286
287if [ $CPUS -gt 1 ]; then
288	# this also tests queues nr reduction
289	ip netns exec $NS_DST ethtool -L veth$DST rx 1 tx 2 2>/dev/null
290	ip netns exec $NS_SRC ethtool -L veth$SRC rx 1 tx 2 2>/dev/null
291	printf "%-60s" "bad setting: XDP with RX nr less than TX"
292	ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
293		section xdp 2>/dev/null &&\
294		echo "fail - set operation successful ?!?" || echo " ok "
295
296	# the following tests will run with multiple channels active
297	ip netns exec $NS_SRC ethtool -L veth$SRC rx 2
298	ip netns exec $NS_DST ethtool -L veth$DST rx 2
299	ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
300		section xdp 2>/dev/null
301	printf "%-60s" "bad setting: reducing RX nr below peer TX with XDP set"
302	ip netns exec $NS_DST ethtool -L veth$DST rx 1 2>/dev/null &&\
303		echo "fail - set operation successful ?!?" || echo " ok "
304	CUR_RX=2
305	CUR_TX=2
306fi
307
308if [ $CPUS -gt 2 ]; then
309	printf "%-60s" "bad setting: increasing peer TX nr above RX with XDP set"
310	ip netns exec $NS_SRC ethtool -L veth$SRC tx 3 2>/dev/null &&\
311		echo "fail - set operation successful ?!?" || echo " ok "
312	chk_channels "setting invalid channels nr" $DST 2 2
313fi
314
315ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp 2>/dev/null
316chk_gro_flag "with xdp attached - gro flag" $DST on
317chk_gro_flag "        - peer gro flag" $SRC off
318chk_tso_flag "        - tso flag" $SRC off
319chk_tso_flag "        - peer tso flag" $DST on
320ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
321chk_gro "        - aggregation" 1
322
323
324ip -n $NS_DST link set dev veth$DST down
325ip -n $NS_SRC link set dev veth$SRC down
326chk_gro_flag "        - after dev off, flag" $DST on
327chk_gro_flag "        - peer flag" $SRC off
328
329ip netns exec $NS_DST ethtool -K veth$DST gro on
330ip -n $NS_DST link set dev veth$DST xdp off
331chk_gro_flag "        - after gro on xdp off, gro flag" $DST on
332chk_gro_flag "        - peer gro flag" $SRC off
333chk_tso_flag "        - tso flag" $SRC on
334chk_tso_flag "        - peer tso flag" $DST on
335
336if [ $CPUS -gt 1 ]; then
337	ip netns exec $NS_DST ethtool -L veth$DST tx 1
338	chk_channels "decreasing tx channels with device down" $DST 2 1
339fi
340
341ip -n $NS_DST link set dev veth$DST up
342ip -n $NS_SRC link set dev veth$SRC up
343chk_gro "        - aggregation" 1
344
345if [ $CPUS -gt 1 ]; then
346	[ $STRESS -gt 0 -a $CPUS -gt 2 ] && do_stress
347
348	ip -n $NS_DST link set dev veth$DST down
349	ip -n $NS_SRC link set dev veth$SRC down
350	ip netns exec $NS_DST ethtool -L veth$DST tx 2
351	chk_channels "increasing tx channels with device down" $DST 2 2
352	ip -n $NS_DST link set dev veth$DST up
353	ip -n $NS_SRC link set dev veth$SRC up
354fi
355
356ip netns exec $NS_DST ethtool -K veth$DST gro off
357ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
358chk_gro "aggregation again with default and TSO off" 10
359
360exit $ret
361