11#! /usr/bin/env bash
2+ # This script sets up a simple SoftRoCE device to facilitate testing.
23
34set -euxETo pipefail
45
5- declare -r RXE_INTERFACE_NAME=" rust_ibverbs"
6+ # Configuration
7+ declare -r RXE_INTERFACE_NAME=" ${1} "
68
79# Print an error message
8- log_err () {
9- >&2 echo " ${* } "
10+ log () {
11+ >&2 printf " %s\n " " ${* } "
1012}
1113
1214# Determine if netns state of rdma devices is shared or exclusive
@@ -22,13 +24,49 @@ confirm_rdma_netns_shared() {
2224 access=" $( get_rdma_netns_state) "
2325 declare -r access
2426 if [[ " ${access} " != " shared" ]]; then
25- log_err " rdma netns state is not shared: current state ${access} "
27+ log " rdma netns state is not shared: current state ${access} "
2628 return 1
2729 fi
2830}
2931
30- confirm_rdma_netns_shared
31- ip link add root type dummy
32- ip link add link root name " ${RXE_INTERFACE_NAME} " type macvlan mode bridge
33- rdma link add " ${RXE_INTERFACE_NAME} " type rxe netdev " ${RXE_INTERFACE_NAME} "
34- ip link set group default up
32+ # Set up a SoftRoCE loopback device. To do this we create a dummy device and then hang a macvlan in bridge mode off of
33+ # that dummy device. This strategy allows all ordinary network traffic transmitted to the macvlan to be sent back to
34+ # that macvlan. We then add a rxe device to the macvlan so that RDMA traffic can be exchanged as well.
35+ set_up_soft_roce_loopback_device () {
36+ declare -r DEVICE=" ${1} "
37+ modprobe rdma_rxe
38+ ip link add root type dummy
39+ ip link add link root name " ${DEVICE} " type macvlan mode bridge
40+ rdma link add " ${DEVICE} " type rxe netdev " ${DEVICE} "
41+ log " Added SoftRoCE link ${DEVICE} "
42+ ip link set dev root up
43+ ip link set dev " ${DEVICE} " up
44+ }
45+
46+ # Get state of RDMA device supplied in first argument.
47+ get_rdma_device_state () {
48+ rdma --json link | jq --raw-output --arg device " ${1} " ' .[] | select(.ifname == $device).state'
49+ }
50+
51+ wait_for_rdma_device_to_be_ready () {
52+ declare -r DEVICE=" ${1} "
53+ declare -ri MAX_RETRY_COUNT=100
54+ declare -i RETRY_COUNT=0
55+ until [[ " $( get_rdma_device_state " ${DEVICE} " ) " == " ACTIVE" ]]; do
56+ log " waiting for ${DEVICE} RDMA device to be active"
57+ RETRY_COUNT+=1
58+ if [[ " ${RETRY_COUNT} " -gt " ${MAX_RETRY_COUNT} " ]]; then
59+ log " Timed out waiting for RDMA device ${DEVICE} to be ready"
60+ return 1
61+ fi
62+ sleep 0.1
63+ done
64+ }
65+
66+ main () {
67+ confirm_rdma_netns_shared
68+ set_up_soft_roce_loopback_device " ${RXE_INTERFACE_NAME} "
69+ wait_for_rdma_device_to_be_ready " ${RXE_INTERFACE_NAME} "
70+ }
71+
72+ main
0 commit comments