xref: /linux/tools/testing/selftests/rdma/rxe_socket_with_netns.sh (revision a67c554dbc0fdd7e3c5909cb9f0fff41c51b2e9d)
1#!/bin/bash
2
3# Configuration
4PORT=4791
5MODS=("tun" "rdma_rxe")
6
7source "$(dirname "$0")/../kselftest/ktap_helpers.sh"
8
9exec > /dev/null
10
11# --- Helper: Cleanup Routine ---
12cleanup() {
13    echo "Cleaning up resources..."
14    rdma link del rxe1 2>/dev/null
15    rdma link del rxe0 2>/dev/null
16    ip link del tun0 2>/dev/null
17    ip link del tun1 2>/dev/null
18    for m in "${MODS[@]}"; do modprobe -r "$m" 2>/dev/null; done
19}
20
21# Ensure cleanup runs on script exit or interrupt
22trap cleanup EXIT
23
24# --- Phase 1: Environment Check ---
25if [[ $EUID -ne 0 ]]; then
26   echo "Error: This script must be run as root."
27   exit 1
28fi
29
30for m in "${MODS[@]}"; do
31    if ! modinfo "$m" >/dev/null 2>&1; then
32        echo "SKIP: Kernel module '$m' not found." >&2
33        exit $KSFT_SKIP
34    fi
35    modprobe "$m" || { echo "Error: Failed to load $m"; exit 1; }
36done
37
38# --- Phase 2: Create Interfaces & RXE Links ---
39echo "Creating tun0 (1.1.1.1) and rxe0..."
40ip tuntap add mode tun tun0
41ip addr add 1.1.1.1/24 dev tun0
42ip link set tun0 up
43rdma link add rxe0 type rxe netdev tun0
44
45# Verify port 4791 is listening
46if ! ss -Huln sport = :$PORT | grep -q ":$PORT"; then
47    echo "Error: UDP port $PORT not found after rxe0 creation"
48    exit 1
49fi
50
51echo "Creating tun1 (2.2.2.2) and rxe1..."
52ip tuntap add mode tun tun1
53ip addr add 2.2.2.2/24 dev tun1
54ip link set tun1 up
55rdma link add rxe1 type rxe netdev tun1
56
57# Verify port 4791 is still listening
58if ! ss -Huln sport = :$PORT | grep -q ":$PORT"; then
59    echo "Error: UDP port $PORT missing after rxe1 creation"
60    exit 1
61fi
62
63# --- Phase 3: Targeted Deletion ---
64echo "Deleting rxe1..."
65rdma link del rxe1
66
67# Port should still be active because rxe0 is still alive
68if ! ss -Huln sport = :$PORT | grep -q ":$PORT"; then
69    echo "Error: UDP port $PORT closed prematurely"
70    exit 1
71fi
72
73echo "Deleting rxe0..."
74rdma link del rxe0
75
76# Port should now be gone
77if ss -Huln sport = :$PORT | grep -q ":$PORT"; then
78    echo "Error: UDP port $PORT still exists after all links deleted"
79    exit 1
80fi
81
82echo "Test passed successfully."
83