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