xref: /linux/tools/testing/selftests/net/lib/ksft_setup_loopback.sh (revision 84318277d6334c6981ab326d4acc87c6a6ddc9b8)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Setup script for running ksft tests over a real interface in loopback mode.
5# This scripts replaces the historical setup_loopback.sh. It puts
6# a (presumably) real hardware interface into loopback mode, creates macvlan
7# interfaces on top and places them in a network namespace for isolation.
8#
9# NETIF env variable must be exported to indicate the real target device.
10# Note that the test will override NETIF with one of the macvlans, the
11# actual ksft test will only see the macvlans.
12#
13# Example use:
14#   export NETIF=eth0
15#   ./net/lib/ksft_setup_loopback.sh ./drivers/net/gro.py
16
17if [ -z "$NETIF" ]; then
18    echo "Error: NETIF variable not set"
19    exit 1
20fi
21if ! [ -d "/sys/class/net/$NETIF" ]; then
22    echo "Error: Can't find $NETIF, invalid netdevice"
23    exit 1
24fi
25
26# Save original settings for cleanup
27readonly FLUSH_PATH="/sys/class/net/${NETIF}/gro_flush_timeout"
28readonly IRQ_PATH="/sys/class/net/${NETIF}/napi_defer_hard_irqs"
29FLUSH_TIMEOUT="$(< "${FLUSH_PATH}")"
30readonly FLUSH_TIMEOUT
31HARD_IRQS="$(< "${IRQ_PATH}")"
32readonly HARD_IRQS
33
34SERVER_NS=$(mktemp -u server-XXXXXXXX)
35readonly SERVER_NS
36CLIENT_NS=$(mktemp -u client-XXXXXXXX)
37readonly CLIENT_NS
38readonly SERVER_MAC="aa:00:00:00:00:02"
39readonly CLIENT_MAC="aa:00:00:00:00:01"
40
41# ksft expects addresses to communicate with remote
42export  LOCAL_V6=2001:db8:1::1
43export REMOTE_V6=2001:db8:1::2
44
45cleanup() {
46    local exit_code=$?
47
48    echo "Cleaning up..."
49
50    # Remove macvlan interfaces and namespaces
51    ip -netns "${SERVER_NS}" link del dev server 2>/dev/null || true
52    ip netns del "${SERVER_NS}" 2>/dev/null || true
53    ip -netns "${CLIENT_NS}" link del dev client 2>/dev/null || true
54    ip netns del "${CLIENT_NS}" 2>/dev/null || true
55
56    # Disable loopback
57    ethtool -K "${NETIF}" loopback off 2>/dev/null || true
58    sleep 1
59
60    echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}"
61    echo "${HARD_IRQS}" >"${IRQ_PATH}"
62
63    exit $exit_code
64}
65
66trap cleanup EXIT INT TERM
67
68# Enable loopback mode
69echo "Enabling loopback on ${NETIF}..."
70ethtool -K "${NETIF}" loopback on || {
71    echo "Failed to enable loopback mode"
72    exit 1
73}
74# The interface may need time to get carrier back, but selftests
75# will wait for carrier, so no need to wait / sleep here.
76
77# Use timer on  host to trigger the network stack
78# Also disable device interrupt to not depend on NIC interrupt
79# Reduce test flakiness caused by unexpected interrupts
80echo 100000 >"${FLUSH_PATH}"
81echo 50 >"${IRQ_PATH}"
82
83# Create server namespace with macvlan
84ip netns add "${SERVER_NS}"
85ip link add link "${NETIF}" dev server address "${SERVER_MAC}" type macvlan
86ip link set dev server netns "${SERVER_NS}"
87ip -netns "${SERVER_NS}" link set dev server up
88ip -netns "${SERVER_NS}" addr add $LOCAL_V6/64 dev server
89ip -netns "${SERVER_NS}" link set dev lo up
90
91# Create client namespace with macvlan
92ip netns add "${CLIENT_NS}"
93ip link add link "${NETIF}" dev client address "${CLIENT_MAC}" type macvlan
94ip link set dev client netns "${CLIENT_NS}"
95ip -netns "${CLIENT_NS}" link set dev client up
96ip -netns "${CLIENT_NS}" addr add $REMOTE_V6/64 dev client
97ip -netns "${CLIENT_NS}" link set dev lo up
98
99echo "Setup complete!"
100echo "  Device: ${NETIF}"
101echo "  Server NS: ${SERVER_NS}"
102echo "  Client NS: ${CLIENT_NS}"
103echo ""
104
105# Setup environment variables for tests
106export NETIF=server
107export REMOTE_TYPE=netns
108export REMOTE_ARGS="${CLIENT_NS}"
109
110# Run the command
111ip netns exec "${SERVER_NS}" "$@"
112