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