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