1#!/bin/bash 2 3# Configuration 4DEV_NAME="tun0" 5RXE_NAME="rxe0" 6RDMA_PORT=4791 7 8exec > /dev/null 9 10# --- Cleanup Routine --- 11# Ensures environment is clean even if the script hits an error 12cleanup() { 13 echo "Performing cleanup..." 14 rdma link del $RXE_NAME 2>/dev/null 15 ip link del $DEV_NAME 2>/dev/null 16 modprobe -r rdma_rxe 2>/dev/null 17} 18trap cleanup EXIT 19 20# 1. Dependency Check 21if ! modinfo rdma_rxe >/dev/null 2>&1; then 22 echo "Error: rdma_rxe module not found." 23 exit 1 24fi 25 26modprobe rdma_rxe 27 28# 2. Setup TUN Device 29echo "Creating $DEV_NAME..." 30ip tuntap add mode tun "$DEV_NAME" 31ip addr add 1.1.1.1/24 dev "$DEV_NAME" 32ip link set "$DEV_NAME" up 33 34# 3. Attach RXE Link 35echo "Attaching RXE link $RXE_NAME to $DEV_NAME..." 36rdma link add "$RXE_NAME" type rxe netdev "$DEV_NAME" 37 38# 4. Verification: Port Check 39# Use -H (no header) and -q (quiet) for cleaner scripting logic 40if ! ss -Huln sport == :$RDMA_PORT | grep -q ":$RDMA_PORT"; then 41 echo "Error: UDP port $RDMA_PORT is not listening." 42 exit 1 43fi 44echo "Verified: RXE is listening on UDP $RDMA_PORT." 45 46# 5. Trigger NETDEV_UNREGISTER 47# We delete the underlying device without deleting the RDMA link first. 48echo "Triggering NETDEV_UNREGISTER by deleting $DEV_NAME..." 49ip link del "$DEV_NAME" 50 51# 6. Final Verification 52# The RXE link and the UDP port should be automatically cleaned up by the kernel. 53if rdma link show "$RXE_NAME" 2>/dev/null; then 54 echo "Error: $RXE_NAME still exists after netdev removal." 55 exit 1 56fi 57 58if ss -Huln sport == :$RDMA_PORT | grep -q ":$RDMA_PORT"; then 59 echo "Error: UDP port $RDMA_PORT still listening after netdev removal." 60 exit 1 61fi 62 63echo "Success: NETDEV_UNREGISTER handled correctly." 64