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