tools/testing/selftests/net/lib/ksft_setup_loopback.sh

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/lib/ksft_setup_loopback.sh

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/lib/ksft_setup_loopback.sh
Extension
.sh
Size
3476 bytes
Lines
112
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/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 || {

Annotation

Implementation Notes