1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Run a series of udpgro functional tests.
5
6readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
7
8BPF_FILE="xdp_dummy.o"
9
10# set global exit status, but never reset nonzero one.
11check_err()
12{
13	if [ $ret -eq 0 ]; then
14		ret=$1
15	fi
16}
17
18cleanup() {
19	local -r jobs="$(jobs -p)"
20	local -r ns="$(ip netns list|grep $PEER_NS)"
21
22	[ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null
23	[ -n "$ns" ] && ip netns del $ns 2>/dev/null
24}
25trap cleanup EXIT
26
27cfg_veth() {
28	ip netns add "${PEER_NS}"
29	ip -netns "${PEER_NS}" link set lo up
30	ip link add type veth
31	ip link set dev veth0 up
32	ip addr add dev veth0 192.168.1.2/24
33	ip addr add dev veth0 2001:db8::2/64 nodad
34
35	ip link set dev veth1 netns "${PEER_NS}"
36	ip -netns "${PEER_NS}" addr add dev veth1 192.168.1.1/24
37	ip -netns "${PEER_NS}" addr add dev veth1 2001:db8::1/64 nodad
38	ip -netns "${PEER_NS}" link set dev veth1 up
39	ip -n "${PEER_NS}" link set veth1 xdp object ${BPF_FILE} section xdp
40}
41
42run_one() {
43	# use 'rx' as separator between sender args and receiver args
44	local -r all="$@"
45	local -r tx_args=${all%rx*}
46	local -r rx_args=${all#*rx}
47
48	cfg_veth
49
50	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -C 1000 -R 10 ${rx_args} && \
51		echo "ok" || \
52		echo "failed" &
53
54	# Hack: let bg programs complete the startup
55	sleep 0.2
56	./udpgso_bench_tx ${tx_args}
57	ret=$?
58	wait $(jobs -p)
59	return $ret
60}
61
62run_test() {
63	local -r args=$@
64
65	printf " %-40s" "$1"
66	./in_netns.sh $0 __subprocess $2 rx -G -r $3
67}
68
69run_one_nat() {
70	# use 'rx' as separator between sender args and receiver args
71	local addr1 addr2 pid family="" ipt_cmd=ip6tables
72	local -r all="$@"
73	local -r tx_args=${all%rx*}
74	local -r rx_args=${all#*rx}
75
76	if [[ ${tx_args} = *-4* ]]; then
77		ipt_cmd=iptables
78		family=-4
79		addr1=192.168.1.1
80		addr2=192.168.1.3/24
81	else
82		addr1=2001:db8::1
83		addr2="2001:db8::3/64 nodad"
84	fi
85
86	cfg_veth
87	ip -netns "${PEER_NS}" addr add dev veth1 ${addr2}
88
89	# fool the GRO engine changing the destination address ...
90	ip netns exec "${PEER_NS}" $ipt_cmd -t nat -I PREROUTING -d ${addr1} -j DNAT --to-destination ${addr2%/*}
91
92	# ... so that GRO will match the UDP_GRO enabled socket, but packets
93	# will land on the 'plain' one
94	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -G ${family} -b ${addr1} -n 0 &
95	pid=$!
96	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -C 1000 -R 10 ${family} -b ${addr2%/*} ${rx_args} && \
97		echo "ok" || \
98		echo "failed"&
99
100	sleep 0.1
101	./udpgso_bench_tx ${tx_args}
102	ret=$?
103	kill -INT $pid
104	wait $(jobs -p)
105	return $ret
106}
107
108run_one_2sock() {
109	# use 'rx' as separator between sender args and receiver args
110	local -r all="$@"
111	local -r tx_args=${all%rx*}
112	local -r rx_args=${all#*rx}
113
114	cfg_veth
115
116	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -C 1000 -R 10 ${rx_args} -p 12345 &
117	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -C 2000 -R 10 ${rx_args} && \
118		echo "ok" || \
119		echo "failed" &
120
121	# Hack: let bg programs complete the startup
122	sleep 0.2
123	./udpgso_bench_tx ${tx_args} -p 12345
124	sleep 0.1
125	# first UDP GSO socket should be closed at this point
126	./udpgso_bench_tx ${tx_args}
127	ret=$?
128	wait $(jobs -p)
129	return $ret
130}
131
132run_nat_test() {
133	local -r args=$@
134
135	printf " %-40s" "$1"
136	./in_netns.sh $0 __subprocess_nat $2 rx -r $3
137}
138
139run_2sock_test() {
140	local -r args=$@
141
142	printf " %-40s" "$1"
143	./in_netns.sh $0 __subprocess_2sock $2 rx -G -r $3
144}
145
146run_all() {
147	local -r core_args="-l 4"
148	local -r ipv4_args="${core_args} -4 -D 192.168.1.1"
149	local -r ipv6_args="${core_args} -6 -D 2001:db8::1"
150	ret=0
151
152	echo "ipv4"
153	run_test "no GRO" "${ipv4_args} -M 10 -s 1400" "-4 -n 10 -l 1400"
154	check_err $?
155
156	# explicitly check we are not receiving UDP_SEGMENT cmsg (-S -1)
157	# when GRO does not take place
158	run_test "no GRO chk cmsg" "${ipv4_args} -M 10 -s 1400" "-4 -n 10 -l 1400 -S -1"
159	check_err $?
160
161	# the GSO packets are aggregated because:
162	# * veth schedule napi after each xmit
163	# * segmentation happens in BH context, veth napi poll is delayed after
164	#   the transmission of the last segment
165	run_test "GRO" "${ipv4_args} -M 1 -s 14720 -S 0 " "-4 -n 1 -l 14720"
166	check_err $?
167	run_test "GRO chk cmsg" "${ipv4_args} -M 1 -s 14720 -S 0 " "-4 -n 1 -l 14720 -S 1472"
168	check_err $?
169	run_test "GRO with custom segment size" "${ipv4_args} -M 1 -s 14720 -S 500 " "-4 -n 1 -l 14720"
170	check_err $?
171	run_test "GRO with custom segment size cmsg" "${ipv4_args} -M 1 -s 14720 -S 500 " "-4 -n 1 -l 14720 -S 500"
172	check_err $?
173
174	run_nat_test "bad GRO lookup" "${ipv4_args} -M 1 -s 14720 -S 0" "-n 10 -l 1472"
175	check_err $?
176	run_2sock_test "multiple GRO socks" "${ipv4_args} -M 1 -s 14720 -S 0 " "-4 -n 1 -l 14720 -S 1472"
177	check_err $?
178
179	echo "ipv6"
180	run_test "no GRO" "${ipv6_args} -M 10 -s 1400" "-n 10 -l 1400"
181	check_err $?
182	run_test "no GRO chk cmsg" "${ipv6_args} -M 10 -s 1400" "-n 10 -l 1400 -S -1"
183	check_err $?
184	run_test "GRO" "${ipv6_args} -M 1 -s 14520 -S 0" "-n 1 -l 14520"
185	check_err $?
186	run_test "GRO chk cmsg" "${ipv6_args} -M 1 -s 14520 -S 0" "-n 1 -l 14520 -S 1452"
187	check_err $?
188	run_test "GRO with custom segment size" "${ipv6_args} -M 1 -s 14520 -S 500" "-n 1 -l 14520"
189	check_err $?
190	run_test "GRO with custom segment size cmsg" "${ipv6_args} -M 1 -s 14520 -S 500" "-n 1 -l 14520 -S 500"
191	check_err $?
192
193	run_nat_test "bad GRO lookup" "${ipv6_args} -M 1 -s 14520 -S 0" "-n 10 -l 1452"
194	check_err $?
195	run_2sock_test "multiple GRO socks" "${ipv6_args} -M 1 -s 14520 -S 0 " "-n 1 -l 14520 -S 1452"
196	check_err $?
197	return $ret
198}
199
200if [ ! -f ${BPF_FILE} ]; then
201	echo "Missing ${BPF_FILE}. Run 'make' first"
202	exit -1
203fi
204
205if [[ $# -eq 0 ]]; then
206	run_all
207elif [[ $1 == "__subprocess" ]]; then
208	shift
209	run_one $@
210elif [[ $1 == "__subprocess_nat" ]]; then
211	shift
212	run_one_nat $@
213elif [[ $1 == "__subprocess_2sock" ]]; then
214	shift
215	run_one_2sock $@
216fi
217
218exit $?
219