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