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