xref: /linux/tools/testing/selftests/rdma/rxe_rping_between_netns.sh (revision ab868c10971c5d2cd27b3709d11225941eabe78e)
1#!/bin/bash
2
3# Configuration
4NS="test1"
5VETH_A="veth-a"
6VETH_B="veth-b"
7IP_A="1.1.1.1"
8IP_B="1.1.1.2"
9PORT=4791
10
11source "$(dirname "$0")/../kselftest/ktap_helpers.sh"
12
13exec > /dev/null
14
15# --- Cleanup Routine ---
16cleanup() {
17    echo "Cleaning up resources..."
18    rdma link del rxe1 2>/dev/null
19    ip netns exec "$NS" rdma link del rxe0 2>/dev/null
20    ip link delete "$VETH_B" 2>/dev/null
21    ip netns del "$NS" 2>/dev/null
22    modprobe -r rdma_rxe 2>/dev/null
23}
24trap cleanup EXIT
25
26# --- Prerequisite Checks ---
27if [[ $EUID -ne 0 ]]; then
28   echo "This script must be run as root"
29   exit 1
30fi
31
32if ! modinfo rdma_rxe >/dev/null 2>&1; then
33    echo "SKIP: Kernel module 'rdma_rxe' not found." >&2
34    exit $KSFT_SKIP
35fi
36
37modprobe rdma_rxe || { echo "Failed to load rdma_rxe"; exit 1; }
38
39# --- Setup Network Topology ---
40echo "Setting up network namespace and veth pair..."
41ip netns add "$NS"
42ip link add "$VETH_A" type veth peer name "$VETH_B"
43ip link set "$VETH_A" netns "$NS"
44
45# Configure Namespace side (test1)
46ip netns exec "$NS" ip addr add "$IP_A/24" dev "$VETH_A"
47ip netns exec "$NS" ip link set "$VETH_A" up
48ip netns exec "$NS" ip link set lo up
49
50# Configure Host side
51ip addr add "$IP_B/24" dev "$VETH_B"
52ip link set "$VETH_B" up
53
54# --- RXE Link Creation ---
55echo "Creating RDMA links..."
56ip netns exec "$NS" rdma link add rxe0 type rxe netdev "$VETH_A"
57rdma link add rxe1 type rxe netdev "$VETH_B"
58
59# Verify UDP 4791 is listening
60check_port() {
61    local target=$1 # "host" or "ns"
62    if [ "$target" == "ns" ]; then
63        ip netns exec "$NS" ss -Huln sport == :$PORT | grep -q ":$PORT"
64    else
65        ss -Huln sport == :$PORT | grep -q ":$PORT"
66    fi
67}
68
69check_port "ns" || { echo "Error: RXE port not listening in namespace"; exit 1; }
70check_port "host" || { echo "Error: RXE port not listening on host"; exit 1; }
71
72# --- Connectivity Test ---
73echo "Testing connectivity with rping..."
74ping -c 2 -W 1 "$IP_A" > /dev/null || { echo "Ping failed"; exit 1; }
75
76# Start rping server in background
77ip netns exec "$NS" rping -s -a "$IP_A" -v > /dev/null 2>&1 &
78RPING_PID=$!
79sleep 1 # Allow server to bind
80
81# Run rping client
82rping -c -a "$IP_A" -d -v -C 3
83RESULT=$?
84
85kill $RPING_PID 2>/dev/null
86
87if [ $RESULT -eq 0 ]; then
88    echo "SUCCESS: RDMA traffic verified."
89else
90    echo "FAILURE: rping failed."
91    exit 1
92fi
93