Commit bd28e5bd authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

selftests: net: remove old setup_* scripts



gro.sh and toeplitz.sh used to source in one of two setup scripts
depending on whether the test was expected to be run against
veth or a real device. veth testing is replaced by netdevsim
and existing "remote endpoint" support in our Python tests.
Add a script which sets up loopback mode.

The usage is a little bit more complicated than running
the scripts used to be. Testing used to work like this:

  ./../gro.sh -i eth0 ...

now the "setup script" has to be run explicitly:

  NETIF=eth0 ./../ksft_setup_loopback.sh ./../gro.sh

But the functionality itself is retained.

Reviewed-by: default avatarPetr Machata <petrm@nvidia.com>
Reviewed-by: default avatarWillem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/20251120021024.2944527-13-kuba@kernel.org


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 358008f4
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -8,8 +8,7 @@ CFLAGS += -I../../
TEST_FILES := \
	../../../../net/ynl \
	../../../../../Documentation/netlink/specs \
	setup_loopback.sh \
	setup_veth.sh \
	ksft_setup_loopback.sh \
# end of TEST_FILES

TEST_GEN_FILES := \
+111 −0
Original line number Diff line number Diff line
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# Setup script for running ksft tests over a real interface in loopback mode.
# This scripts replaces the historical setup_loopback.sh. It puts
# a (presumably) real hardware interface into loopback mode, creates macvlan
# interfaces on top and places them in a network namespace for isolation.
#
# NETIF env variable must be exported to indicate the real target device.
# Note that the test will override NETIF with one of the macvlans, the
# actual ksft test will only see the macvlans.
#
# Example use:
#   export NETIF=eth0
#   ./net/lib/ksft_setup_loopback.sh ./drivers/net/gro.py

if [ -z "$NETIF" ]; then
    echo "Error: NETIF variable not set"
    exit 1
fi
if ! [ -d "/sys/class/net/$NETIF" ]; then
    echo "Error: Can't find $NETIF, invalid netdevice"
    exit 1
fi

# Save original settings for cleanup
readonly FLUSH_PATH="/sys/class/net/${NETIF}/gro_flush_timeout"
readonly IRQ_PATH="/sys/class/net/${NETIF}/napi_defer_hard_irqs"
FLUSH_TIMEOUT="$(< "${FLUSH_PATH}")"
readonly FLUSH_TIMEOUT
HARD_IRQS="$(< "${IRQ_PATH}")"
readonly HARD_IRQS

SERVER_NS=$(mktemp -u server-XXXXXXXX)
readonly SERVER_NS
CLIENT_NS=$(mktemp -u client-XXXXXXXX)
readonly CLIENT_NS
readonly SERVER_MAC="aa:00:00:00:00:02"
readonly CLIENT_MAC="aa:00:00:00:00:01"

# ksft expects addresses to communicate with remote
export  LOCAL_V6=2001:db8:1::1
export REMOTE_V6=2001:db8:1::2

cleanup() {
    local exit_code=$?

    echo "Cleaning up..."

    # Remove macvlan interfaces and namespaces
    ip -netns "${SERVER_NS}" link del dev server 2>/dev/null || true
    ip netns del "${SERVER_NS}" 2>/dev/null || true
    ip -netns "${CLIENT_NS}" link del dev client 2>/dev/null || true
    ip netns del "${CLIENT_NS}" 2>/dev/null || true

    # Disable loopback
    ethtool -K "${NETIF}" loopback off 2>/dev/null || true
    sleep 1

    echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}"
    echo "${HARD_IRQS}" >"${IRQ_PATH}"

    exit $exit_code
}

trap cleanup EXIT INT TERM

# Enable loopback mode
echo "Enabling loopback on ${NETIF}..."
ethtool -K "${NETIF}" loopback on || {
    echo "Failed to enable loopback mode"
    exit 1
}
# The interface may need time to get carrier back, but selftests
# will wait for carrier, so no need to wait / sleep here.

# Use timer on  host to trigger the network stack
# Also disable device interrupt to not depend on NIC interrupt
# Reduce test flakiness caused by unexpected interrupts
echo 100000 >"${FLUSH_PATH}"
echo 50 >"${IRQ_PATH}"

# Create server namespace with macvlan
ip netns add "${SERVER_NS}"
ip link add link "${NETIF}" dev server address "${SERVER_MAC}" type macvlan
ip link set dev server netns "${SERVER_NS}"
ip -netns "${SERVER_NS}" link set dev server up
ip -netns "${SERVER_NS}" addr add $LOCAL_V6/64 dev server
ip -netns "${SERVER_NS}" link set dev lo up

# Create client namespace with macvlan
ip netns add "${CLIENT_NS}"
ip link add link "${NETIF}" dev client address "${CLIENT_MAC}" type macvlan
ip link set dev client netns "${CLIENT_NS}"
ip -netns "${CLIENT_NS}" link set dev client up
ip -netns "${CLIENT_NS}" addr add $REMOTE_V6/64 dev client
ip -netns "${CLIENT_NS}" link set dev lo up

echo "Setup complete!"
echo "  Device: ${NETIF}"
echo "  Server NS: ${SERVER_NS}"
echo "  Client NS: ${CLIENT_NS}"
echo ""

# Setup environment variables for tests
export NETIF=server
export REMOTE_TYPE=netns
export REMOTE_ARGS="${CLIENT_NS}"

# Run the command
ip netns exec "${SERVER_NS}" "$@"
+0 −120
Original line number Diff line number Diff line
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

readonly FLUSH_PATH="/sys/class/net/${dev}/gro_flush_timeout"
readonly IRQ_PATH="/sys/class/net/${dev}/napi_defer_hard_irqs"
readonly FLUSH_TIMEOUT="$(< ${FLUSH_PATH})"
readonly HARD_IRQS="$(< ${IRQ_PATH})"
readonly server_ns=$(mktemp -u server-XXXXXXXX)
readonly client_ns=$(mktemp -u client-XXXXXXXX)

netdev_check_for_carrier() {
	local -r dev="$1"

	for i in {1..5}; do
		carrier="$(cat /sys/class/net/${dev}/carrier)"
		if [[ "${carrier}" -ne 1 ]] ; then
			echo "carrier not ready yet..." >&2
			sleep 1
		else
			echo "carrier ready" >&2
			break
		fi
	done
	echo "${carrier}"
}

# Assumes that there is no existing ipvlan device on the physical device
setup_loopback_environment() {
	local dev="$1"

	# Fail hard if cannot turn on loopback mode for current NIC
	ethtool -K "${dev}" loopback on || exit 1
	sleep 1

	# Check for the carrier
	carrier=$(netdev_check_for_carrier ${dev})
	if [[ "${carrier}" -ne 1 ]] ; then
		echo "setup_loopback_environment failed"
		exit 1
	fi
}

setup_macvlan_ns(){
	local -r link_dev="$1"
	local -r ns_name="$2"
	local -r ns_dev="$3"
	local -r ns_mac="$4"
	local -r addr="$5"

	ip link add link "${link_dev}" dev "${ns_dev}" \
		address "${ns_mac}" type macvlan
	exit_code=$?
	if [[ "${exit_code}" -ne 0 ]]; then
		echo "setup_macvlan_ns failed"
		exit $exit_code
	fi

	[[ -e /var/run/netns/"${ns_name}" ]] || ip netns add "${ns_name}"
	ip link set dev "${ns_dev}" netns "${ns_name}"
	ip -netns "${ns_name}" link set dev "${ns_dev}" up
	if [[ -n "${addr}" ]]; then
		ip -netns "${ns_name}" addr add dev "${ns_dev}" "${addr}"
	fi

	sleep 1
}

cleanup_macvlan_ns(){
	while (( $# >= 2 )); do
		ns_name="$1"
		ns_dev="$2"
		ip -netns "${ns_name}" link del dev "${ns_dev}"
		ip netns del "${ns_name}"
		shift 2
	done
}

cleanup_loopback(){
	local -r dev="$1"

	ethtool -K "${dev}" loopback off
	sleep 1

	# Check for the carrier
	carrier=$(netdev_check_for_carrier ${dev})
	if [[ "${carrier}" -ne 1 ]] ; then
		echo "setup_loopback_environment failed"
		exit 1
	fi
}

setup_interrupt() {
	# Use timer on  host to trigger the network stack
	# Also disable device interrupt to not depend on NIC interrupt
	# Reduce test flakiness caused by unexpected interrupts
	echo 100000 >"${FLUSH_PATH}"
	echo 50 >"${IRQ_PATH}"
}

setup_ns() {
	# Set up server_ns namespace and client_ns namespace
	setup_macvlan_ns "${dev}" ${server_ns} server "${SERVER_MAC}"
	setup_macvlan_ns "${dev}" ${client_ns} client "${CLIENT_MAC}"
}

cleanup_ns() {
	cleanup_macvlan_ns ${server_ns} server ${client_ns} client
}

setup() {
	setup_loopback_environment "${dev}"
	setup_interrupt
}

cleanup() {
	cleanup_loopback "${dev}"

	echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}"
	echo "${HARD_IRQS}" >"${IRQ_PATH}"
}
+0 −45
Original line number Diff line number Diff line
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

readonly server_ns=$(mktemp -u server-XXXXXXXX)
readonly client_ns=$(mktemp -u client-XXXXXXXX)

setup_veth_ns() {
	local -r link_dev="$1"
	local -r ns_name="$2"
	local -r ns_dev="$3"
	local -r ns_mac="$4"

	[[ -e /var/run/netns/"${ns_name}" ]] || ip netns add "${ns_name}"
	echo 200000 > "/sys/class/net/${ns_dev}/gro_flush_timeout"
	echo 1 > "/sys/class/net/${ns_dev}/napi_defer_hard_irqs"
	ip link set dev "${ns_dev}" netns "${ns_name}" mtu 65535
	ip -netns "${ns_name}" link set dev "${ns_dev}" up

	ip netns exec "${ns_name}" ethtool -K "${ns_dev}" gro on tso off
}

setup_ns() {
	# Set up server_ns namespace and client_ns namespace
	ip link add name server type veth peer name client

	setup_veth_ns "${dev}" ${server_ns} server "${SERVER_MAC}"
	setup_veth_ns "${dev}" ${client_ns} client "${CLIENT_MAC}"
}

cleanup_ns() {
	local ns_name

	for ns_name in ${client_ns} ${server_ns}; do
		[[ -e /var/run/netns/"${ns_name}" ]] && ip netns del "${ns_name}"
	done
}

setup() {
	# no global init setup step needed
	:
}

cleanup() {
	cleanup_ns
}