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