1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Run a series of udpgro benchmarks
5
6readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
7
8BPF_FILE="xdp_dummy.o"
9
10cleanup() {
11	local -r jobs="$(jobs -p)"
12	local -r ns="$(ip netns list|grep $PEER_NS)"
13
14	[ -n "${jobs}" ] && kill -INT ${jobs} 2>/dev/null
15	[ -n "$ns" ] && ip netns del $ns 2>/dev/null
16}
17trap cleanup EXIT
18
19run_one() {
20	# use 'rx' as separator between sender args and receiver args
21	local -r all="$@"
22	local -r tx_args=${all%rx*}
23	local rx_args=${all#*rx}
24
25	[[ "${tx_args}" == *"-4"* ]] && rx_args="${rx_args} -4"
26
27	ip netns add "${PEER_NS}"
28	ip -netns "${PEER_NS}" link set lo up
29	ip link add type veth
30	ip link set dev veth0 up
31	ip addr add dev veth0 192.168.1.2/24
32	ip addr add dev veth0 2001:db8::2/64 nodad
33
34	ip link set dev veth1 netns "${PEER_NS}"
35	ip -netns "${PEER_NS}" addr add dev veth1 192.168.1.1/24
36	ip -netns "${PEER_NS}" addr add dev veth1 2001:db8::1/64 nodad
37	ip -netns "${PEER_NS}" link set dev veth1 up
38
39	ip -n "${PEER_NS}" link set veth1 xdp object ${BPF_FILE} section xdp
40	ip netns exec "${PEER_NS}" ./udpgso_bench_rx ${rx_args} -r &
41	ip netns exec "${PEER_NS}" ./udpgso_bench_rx -t ${rx_args} -r &
42
43	# Hack: let bg programs complete the startup
44	sleep 0.2
45	./udpgso_bench_tx ${tx_args}
46}
47
48run_in_netns() {
49	local -r args=$@
50
51	./in_netns.sh $0 __subprocess ${args}
52}
53
54run_udp() {
55	local -r args=$@
56
57	echo "udp gso - over veth touching data"
58	run_in_netns ${args} -S 0 rx
59
60	echo "udp gso and gro - over veth touching data"
61	run_in_netns ${args} -S 0 rx -G
62}
63
64run_tcp() {
65	local -r args=$@
66
67	echo "tcp - over veth touching data"
68	run_in_netns ${args} -t rx
69}
70
71run_all() {
72	local -r core_args="-l 4"
73	local -r ipv4_args="${core_args} -4 -D 192.168.1.1"
74	local -r ipv6_args="${core_args} -6 -D 2001:db8::1"
75
76	echo "ipv4"
77	run_tcp "${ipv4_args}"
78	run_udp "${ipv4_args}"
79
80	echo "ipv6"
81	run_tcp "${ipv4_args}"
82	run_udp "${ipv6_args}"
83}
84
85if [ ! -f ${BPF_FILE} ]; then
86	echo "Missing ${BPF_FILE}. Run 'make' first"
87	exit -1
88fi
89
90if [[ $# -eq 0 ]]; then
91	run_all
92elif [[ $1 == "__subprocess" ]]; then
93	shift
94	run_one $@
95else
96	run_in_netns $@
97fi
98