1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4BPF_FILE="../bpf/xdp_dummy.bpf.o"
5readonly BASE="ns-$(mktemp -u XXXXXX)"
6readonly SRC=2
7readonly DST=1
8readonly DST_NAT=100
9readonly NS_SRC=$BASE$SRC
10readonly NS_DST=$BASE$DST
11
12# "baremetal" network used for raw UDP traffic
13readonly BM_NET_V4=192.168.1.
14readonly BM_NET_V6=2001:db8::
15
16# "overlay" network used for UDP over UDP tunnel traffic
17readonly OL_NET_V4=172.16.1.
18readonly OL_NET_V6=2001:db8:1::
19readonly NPROCS=`nproc`
20
21cleanup() {
22	local ns
23	local -r jobs="$(jobs -p)"
24	[ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null
25
26	for ns in $NS_SRC $NS_DST; do
27		ip netns del $ns 2>/dev/null
28	done
29}
30
31trap cleanup EXIT
32
33create_ns() {
34	local net
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
46		ip -n $BASE$ns link set dev veth$ns up
47		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V4$ns/24
48		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V6$ns/64 nodad
49	done
50	ip -n $NS_DST link set veth$DST xdp object ${BPF_FILE} section xdp 2>/dev/null
51}
52
53create_vxlan_endpoint() {
54	local -r netns=$1
55	local -r bm_dev=$2
56	local -r bm_rem_addr=$3
57	local -r vxlan_dev=$4
58	local -r vxlan_id=$5
59	local -r vxlan_port=4789
60
61	ip -n $netns link set dev $bm_dev up
62	ip -n $netns link add dev $vxlan_dev type vxlan id $vxlan_id \
63				dstport $vxlan_port remote $bm_rem_addr
64	ip -n $netns link set dev $vxlan_dev up
65}
66
67create_vxlan_pair() {
68	local ns
69
70	create_ns
71
72	for ns in $SRC $DST; do
73		# note that 3 - $SRC == $DST and 3 - $DST == $SRC
74		create_vxlan_endpoint $BASE$ns veth$ns $BM_NET_V4$((3 - $ns)) vxlan$ns 4
75		ip -n $BASE$ns addr add dev vxlan$ns $OL_NET_V4$ns/24
76	done
77	for ns in $SRC $DST; do
78		create_vxlan_endpoint $BASE$ns veth$ns $BM_NET_V6$((3 - $ns)) vxlan6$ns 6
79		ip -n $BASE$ns addr add dev vxlan6$ns $OL_NET_V6$ns/24 nodad
80	done
81}
82
83is_ipv6() {
84	if [[ $1 =~ .*:.* ]]; then
85		return 0
86	fi
87	return 1
88}
89
90run_test() {
91	local -r msg=$1
92	local -r dst=$2
93	local -r pkts=$3
94	local -r vxpkts=$4
95	local bind=$5
96	local rx_args=""
97	local rx_family="-4"
98	local family=-4
99	local filter=IpInReceives
100	local ipt=iptables
101
102	printf "%-40s" "$msg"
103
104	if is_ipv6 $dst; then
105		# rx program does not support '-6' and implies ipv6 usage by default
106		rx_family=""
107		family=-6
108		filter=Ip6InReceives
109		ipt=ip6tables
110	fi
111
112	rx_args="$rx_family"
113	[ -n "$bind" ] && rx_args="$rx_args -b $bind"
114
115	# send a single GSO packet, segmented in 10 UDP frames.
116	# Always expect 10 UDP frames on RX side as rx socket does
117	# not enable GRO
118	ip netns exec $NS_DST $ipt -A INPUT -p udp --dport 4789
119	ip netns exec $NS_DST $ipt -A INPUT -p udp --dport 8000
120	ip netns exec $NS_DST ./udpgso_bench_rx -C 1000 -R 10 -n 10 -l 1300 $rx_args &
121	local spid=$!
122	sleep 0.1
123	ip netns exec $NS_SRC ./udpgso_bench_tx $family -M 1 -s 13000 -S 1300 -D $dst
124	local retc=$?
125	wait $spid
126	local rets=$?
127	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
128		echo " fail client exit code $retc, server $rets"
129		ret=1
130		return
131	fi
132
133	local rcv=`ip netns exec $NS_DST $ipt"-save" -c | grep 'dport 8000' | \
134							  sed -e 's/\[//' -e 's/:.*//'`
135	if [ $rcv != $pkts ]; then
136		echo " fail - received $rcv packets, expected $pkts"
137		ret=1
138		return
139	fi
140
141	local vxrcv=`ip netns exec $NS_DST $ipt"-save" -c | grep 'dport 4789' | \
142							    sed -e 's/\[//' -e 's/:.*//'`
143
144	# upper net can generate a little noise, allow some tolerance
145	if [ $vxrcv -lt $vxpkts -o $vxrcv -gt $((vxpkts + 3)) ]; then
146		echo " fail - received $vxrcv vxlan packets, expected $vxpkts"
147		ret=1
148		return
149	fi
150	echo " ok"
151}
152
153run_bench() {
154	local -r msg=$1
155	local -r dst=$2
156	local family=-4
157
158	printf "%-40s" "$msg"
159	if [ $NPROCS -lt 2 ]; then
160		echo " skip - needed 2 CPUs found $NPROCS"
161		return
162	fi
163
164	is_ipv6 $dst && family=-6
165
166	# bind the sender and the receiver to different CPUs to try
167	# get reproducible results
168	ip netns exec $NS_DST bash -c "echo 2 > /sys/class/net/veth$DST/queues/rx-0/rps_cpus"
169	ip netns exec $NS_DST taskset 0x2 ./udpgso_bench_rx -C 1000 -R 10  &
170	local spid=$!
171	sleep 0.1
172	ip netns exec $NS_SRC taskset 0x1 ./udpgso_bench_tx $family -l 3 -S 1300 -D $dst
173	local retc=$?
174	wait $spid
175	local rets=$?
176	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
177		echo " fail client exit code $retc, server $rets"
178		ret=1
179		return
180	fi
181}
182
183for family in 4 6; do
184	BM_NET=$BM_NET_V4
185	OL_NET=$OL_NET_V4
186	IPT=iptables
187	SUFFIX=24
188	VXDEV=vxlan
189	PING=ping
190
191	if [ $family = 6 ]; then
192		BM_NET=$BM_NET_V6
193		OL_NET=$OL_NET_V6
194		SUFFIX="64 nodad"
195		VXDEV=vxlan6
196		IPT=ip6tables
197		# Use ping6 on systems where ping doesn't handle IPv6
198		ping -w 1 -c 1 ::1 > /dev/null 2>&1 || PING="ping6"
199	fi
200
201	echo "IPv$family"
202
203	create_ns
204	run_test "No GRO" $BM_NET$DST 10 0
205	cleanup
206
207	create_ns
208	ip netns exec $NS_DST ethtool -K veth$DST rx-gro-list on
209	run_test "GRO frag list" $BM_NET$DST 1 0
210	cleanup
211
212	# UDP GRO fwd skips aggregation when find an udp socket with the GRO option
213	# if there is an UDP tunnel in the running system, such lookup happen
214	# take place.
215	# use NAT to circumvent GRO FWD check
216	create_ns
217	ip -n $NS_DST addr add dev veth$DST $BM_NET$DST_NAT/$SUFFIX
218	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
219	ip netns exec $NS_DST $IPT -t nat -I PREROUTING -d $BM_NET$DST_NAT \
220					-j DNAT --to-destination $BM_NET$DST
221	run_test "GRO fwd" $BM_NET$DST_NAT 1 0 $BM_NET$DST
222	cleanup
223
224	create_ns
225	run_bench "UDP fwd perf" $BM_NET$DST
226	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
227	run_bench "UDP GRO fwd perf" $BM_NET$DST
228	cleanup
229
230	create_vxlan_pair
231	ip netns exec $NS_DST ethtool -K veth$DST rx-gro-list on
232	run_test "GRO frag list over UDP tunnel" $OL_NET$DST 1 1
233	cleanup
234
235	# use NAT to circumvent GRO FWD check
236	create_vxlan_pair
237	ip -n $NS_DST addr add dev $VXDEV$DST $OL_NET$DST_NAT/$SUFFIX
238	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
239	ip netns exec $NS_DST $IPT -t nat -I PREROUTING -d $OL_NET$DST_NAT \
240					-j DNAT --to-destination $OL_NET$DST
241
242	# load arp cache before running the test to reduce the amount of
243	# stray traffic on top of the UDP tunnel
244	ip netns exec $NS_SRC $PING -q -c 1 $OL_NET$DST_NAT >/dev/null
245	run_test "GRO fwd over UDP tunnel" $OL_NET$DST_NAT 1 1 $OL_NET$DST
246	cleanup
247
248	create_vxlan_pair
249	run_bench "UDP tunnel fwd perf" $OL_NET$DST
250	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
251	run_bench "UDP tunnel GRO fwd perf" $OL_NET$DST
252	cleanup
253done
254
255exit $ret
256