xref: /linux/tools/testing/selftests/net/veth.sh (revision 6af91e3d2cfc8bb579b1aa2d22cd91f8c34acdf6)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4BPF_FILE="xdp_dummy.bpf.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
250ip -n $NS_DST link set dev veth$DST up
251ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp
252chk_gro_flag "gro vs xdp while down - gro flag off" $DST off
253ip -n $NS_DST link set dev veth$DST down
254chk_gro_flag "                      - after down" $DST off
255ip -n $NS_DST link set dev veth$DST xdp off
256chk_gro_flag "                      - after xdp off" $DST off
257ip -n $NS_DST link set dev veth$DST up
258chk_gro_flag "                      - after up" $DST off
259ip -n $NS_SRC link set dev veth$SRC xdp object ${BPF_FILE} section xdp
260chk_gro_flag "                      - after peer xdp" $DST off
261cleanup
262
263create_ns
264ip -n $NS_DST link set dev veth$DST up
265ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp
266ip netns exec $NS_DST ethtool -K veth$DST generic-receive-offload on
267chk_gro_flag "gro vs xdp while down - gro flag on" $DST on
268ip -n $NS_DST link set dev veth$DST down
269chk_gro_flag "                      - after down" $DST on
270ip -n $NS_DST link set dev veth$DST xdp off
271chk_gro_flag "                      - after xdp off" $DST on
272ip -n $NS_DST link set dev veth$DST up
273chk_gro_flag "                      - after up" $DST on
274ip -n $NS_SRC link set dev veth$SRC xdp object ${BPF_FILE} section xdp
275chk_gro_flag "                      - after peer xdp" $DST on
276cleanup
277
278create_ns
279chk_channels "default channels" $DST 1 1
280
281ip -n $NS_DST link set dev veth$DST down
282ip netns exec $NS_DST ethtool -K veth$DST gro on
283chk_gro_flag "with gro enabled on link down - gro flag" $DST on
284chk_gro_flag "        - peer gro flag" $SRC off
285chk_tso_flag "        - tso flag" $SRC on
286chk_tso_flag "        - peer tso flag" $DST on
287ip -n $NS_DST link set dev veth$DST up
288ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
289ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
290chk_gro "        - aggregation with TSO off" 1
291cleanup
292
293create_ns
294
295CUR_TX=1
296CUR_RX=1
297if [ $CPUS -gt 1 ]; then
298	ip netns exec $NS_DST ethtool -L veth$DST tx 2
299	chk_channels "setting tx channels" $DST 1 2
300	CUR_TX=2
301fi
302
303if [ $CPUS -gt 2 ]; then
304	ip netns exec $NS_DST ethtool -L veth$DST rx 3 tx 3
305	chk_channels "setting both rx and tx channels" $DST 3 3
306	CUR_RX=3
307	CUR_TX=3
308fi
309
310ip netns exec $NS_DST ethtool -L veth$DST combined 2 2>/dev/null
311chk_channels "bad setting: combined channels" $DST $CUR_RX $CUR_TX
312
313ip netns exec $NS_DST ethtool -L veth$DST tx $((CPUS + 1)) 2>/dev/null
314chk_channels "setting invalid channels nr" $DST $CUR_RX $CUR_TX
315
316if [ $CPUS -gt 1 ]; then
317	# this also tests queues nr reduction
318	ip netns exec $NS_DST ethtool -L veth$DST rx 1 tx 2 2>/dev/null
319	ip netns exec $NS_SRC ethtool -L veth$SRC rx 1 tx 2 2>/dev/null
320	printf "%-60s" "bad setting: XDP with RX nr less than TX"
321	ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
322		section xdp 2>/dev/null &&\
323		echo "fail - set operation successful ?!?" || echo " ok "
324
325	# the following tests will run with multiple channels active
326	ip netns exec $NS_SRC ethtool -L veth$SRC rx 2
327	ip netns exec $NS_DST ethtool -L veth$DST rx 2
328	ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
329		section xdp 2>/dev/null
330	printf "%-60s" "bad setting: reducing RX nr below peer TX with XDP set"
331	ip netns exec $NS_DST ethtool -L veth$DST rx 1 2>/dev/null &&\
332		echo "fail - set operation successful ?!?" || echo " ok "
333	CUR_RX=2
334	CUR_TX=2
335fi
336
337if [ $CPUS -gt 2 ]; then
338	printf "%-60s" "bad setting: increasing peer TX nr above RX with XDP set"
339	ip netns exec $NS_SRC ethtool -L veth$SRC tx 3 2>/dev/null &&\
340		echo "fail - set operation successful ?!?" || echo " ok "
341	chk_channels "setting invalid channels nr" $DST 2 2
342fi
343
344ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp 2>/dev/null
345chk_gro_flag "with xdp attached - gro flag" $DST off
346chk_gro_flag "        - peer gro flag" $SRC off
347chk_tso_flag "        - tso flag" $SRC off
348chk_tso_flag "        - peer tso flag" $DST on
349ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
350chk_gro "        - no aggregation" 10
351ip netns exec $NS_DST ethtool -K veth$DST generic-receive-offload on
352chk_gro_flag "        - gro flag with GRO on" $DST on
353chk_gro "        - aggregation" 1
354
355
356ip -n $NS_DST link set dev veth$DST down
357ip -n $NS_SRC link set dev veth$SRC down
358chk_gro_flag "        - after dev off, flag" $DST on
359chk_gro_flag "        - peer flag" $SRC off
360
361ip netns exec $NS_DST ethtool -K veth$DST gro on
362ip -n $NS_DST link set dev veth$DST xdp off
363chk_gro_flag "        - after gro on xdp off, gro flag" $DST on
364chk_gro_flag "        - peer gro flag" $SRC off
365chk_tso_flag "        - tso flag" $SRC on
366chk_tso_flag "        - peer tso flag" $DST on
367
368if [ $CPUS -gt 1 ]; then
369	ip netns exec $NS_DST ethtool -L veth$DST tx 1
370	chk_channels "decreasing tx channels with device down" $DST 2 1
371fi
372
373ip -n $NS_DST link set dev veth$DST up
374ip -n $NS_SRC link set dev veth$SRC up
375chk_gro "        - aggregation" 1
376
377if [ $CPUS -gt 1 ]; then
378	[ $STRESS -gt 0 -a $CPUS -gt 2 ] && do_stress
379
380	ip -n $NS_DST link set dev veth$DST down
381	ip -n $NS_SRC link set dev veth$SRC down
382	ip netns exec $NS_DST ethtool -L veth$DST tx 2
383	chk_channels "increasing tx channels with device down" $DST 2 2
384	ip -n $NS_DST link set dev veth$DST up
385	ip -n $NS_SRC link set dev veth$SRC up
386fi
387
388ip netns exec $NS_DST ethtool -K veth$DST gro off
389ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
390chk_gro "aggregation again with default and TSO off" 10
391
392exit $ret
393