1#!/bin/bash
2#
3# Send data between two processes across namespaces
4# Run twice: once without and once with zerocopy
5
6set -e
7
8readonly DEV="veth0"
9readonly DEV_MTU=65535
10readonly BIN_TX="./io_uring_zerocopy_tx"
11readonly BIN_RX="./msg_zerocopy"
12
13readonly RAND="$(mktemp -u XXXXXX)"
14readonly NSPREFIX="ns-${RAND}"
15readonly NS1="${NSPREFIX}1"
16readonly NS2="${NSPREFIX}2"
17
18readonly SADDR4='192.168.1.1'
19readonly DADDR4='192.168.1.2'
20readonly SADDR6='fd::1'
21readonly DADDR6='fd::2'
22
23readonly path_sysctl_mem="net.core.optmem_max"
24
25# No arguments: automated test
26if [[ "$#" -eq "0" ]]; then
27	IPs=( "4" "6" )
28	protocols=( "tcp" "udp" )
29
30	for IP in "${IPs[@]}"; do
31		for proto in "${protocols[@]}"; do
32			for mode in $(seq 1 3); do
33				$0 "$IP" "$proto" -m "$mode" -t 1 -n 32
34				$0 "$IP" "$proto" -m "$mode" -t 1 -n 32 -f
35				$0 "$IP" "$proto" -m "$mode" -t 1 -n 32 -c -f
36			done
37		done
38	done
39
40	echo "OK. All tests passed"
41	exit 0
42fi
43
44# Argument parsing
45if [[ "$#" -lt "2" ]]; then
46	echo "Usage: $0 [4|6] [tcp|udp|raw|raw_hdrincl|packet|packet_dgram] <args>"
47	exit 1
48fi
49
50readonly IP="$1"
51shift
52readonly TXMODE="$1"
53shift
54readonly EXTRA_ARGS="$@"
55
56# Argument parsing: configure addresses
57if [[ "${IP}" == "4" ]]; then
58	readonly SADDR="${SADDR4}"
59	readonly DADDR="${DADDR4}"
60elif [[ "${IP}" == "6" ]]; then
61	readonly SADDR="${SADDR6}"
62	readonly DADDR="${DADDR6}"
63else
64	echo "Invalid IP version ${IP}"
65	exit 1
66fi
67
68# Argument parsing: select receive mode
69#
70# This differs from send mode for
71# - packet:	use raw recv, because packet receives skb clones
72# - raw_hdrinc: use raw recv, because hdrincl is a tx-only option
73case "${TXMODE}" in
74'packet' | 'packet_dgram' | 'raw_hdrincl')
75	RXMODE='raw'
76	;;
77*)
78	RXMODE="${TXMODE}"
79	;;
80esac
81
82# Start of state changes: install cleanup handler
83save_sysctl_mem="$(sysctl -n ${path_sysctl_mem})"
84
85cleanup() {
86	ip netns del "${NS2}"
87	ip netns del "${NS1}"
88	sysctl -w -q "${path_sysctl_mem}=${save_sysctl_mem}"
89}
90
91trap cleanup EXIT
92
93# Configure system settings
94sysctl -w -q "${path_sysctl_mem}=1000000"
95
96# Create virtual ethernet pair between network namespaces
97ip netns add "${NS1}"
98ip netns add "${NS2}"
99
100ip link add "${DEV}" mtu "${DEV_MTU}" netns "${NS1}" type veth \
101  peer name "${DEV}" mtu "${DEV_MTU}" netns "${NS2}"
102
103# Bring the devices up
104ip -netns "${NS1}" link set "${DEV}" up
105ip -netns "${NS2}" link set "${DEV}" up
106
107# Set fixed MAC addresses on the devices
108ip -netns "${NS1}" link set dev "${DEV}" address 02:02:02:02:02:02
109ip -netns "${NS2}" link set dev "${DEV}" address 06:06:06:06:06:06
110
111# Add fixed IP addresses to the devices
112ip -netns "${NS1}" addr add 192.168.1.1/24 dev "${DEV}"
113ip -netns "${NS2}" addr add 192.168.1.2/24 dev "${DEV}"
114ip -netns "${NS1}" addr add       fd::1/64 dev "${DEV}" nodad
115ip -netns "${NS2}" addr add       fd::2/64 dev "${DEV}" nodad
116
117# Optionally disable sg or csum offload to test edge cases
118# ip netns exec "${NS1}" ethtool -K "${DEV}" sg off
119
120do_test() {
121	local readonly ARGS="$1"
122
123	echo "ipv${IP} ${TXMODE} ${ARGS}"
124	ip netns exec "${NS2}" "${BIN_RX}" "-${IP}" -t 2 -C 2 -S "${SADDR}" -D "${DADDR}" -r "${RXMODE}" &
125	sleep 0.2
126	ip netns exec "${NS1}" "${BIN_TX}" "-${IP}" -t 1 -D "${DADDR}" ${ARGS} "${TXMODE}"
127	wait
128}
129
130do_test "${EXTRA_ARGS}"
131echo ok
132