forked from opencomputeproject/SAI-Challenger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·189 lines (170 loc) · 4.89 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# exit when any command fails
set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo ERROR: "\"${last_command}\" command filed with exit code $?."' ERR
IMAGE_TYPE="standalone"
ASIC_TYPE=""
ASIC_PATH=""
TARGET=""
OPTS=""
COMMAND="start"
SAI_INTERFACE="redis"
print-help() {
echo
echo "$(basename ""$0"") [OPTIONS]"
echo "Options:"
echo " -h Print script usage"
echo " -i [standalone|client|server]"
echo " Image type to be started"
echo " -a ASIC"
echo " ASIC to be tested"
echo " -t TARGET"
echo " Target device with this NPU"
echo " -c [start|stop]"
echo " Start or stop docker. Default (start)"
echo " -p Run Docker in --privileged mode"
echo " -n Run Docker with host networking namespace"
echo " -r Remove Docker after run"
echo " -s [redis|thrift]"
echo " SAI interface"
echo
exit 0
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
"-h"|"--help")
print-help
exit 0
;;
"-i"|"--image")
IMAGE_TYPE="$2"
shift
;;
"-a"|"--asic")
ASIC_TYPE="$2"
shift
;;
"-t"|"--target")
TARGET="$2"
shift
;;
"-p")
OPTS="$OPTS --privileged"
;;
"-r")
OPTS="$OPTS --rm"
;;
"-n")
OPTS="$OPTS --network host"
;;
"-c")
COMMAND="$2"
shift
;;
"-s"|"--sai_interface")
SAI_INTERFACE="$2"
shift
;;
esac
shift
done
if [[ "${IMAGE_TYPE}" != "standalone" && \
"${IMAGE_TYPE}" != "client" && \
"${IMAGE_TYPE}" != "server" ]]; then
echo "Unknown image type \"${IMAGE_TYPE}\""
exit 1
fi
if [[ "${IMAGE_TYPE}" != "client" ]]; then
if [ -z "${ASIC_TYPE}" ]; then
ASIC_TYPE="trident2"
fi
ASIC_PATH=$(find -L -type d -name "${ASIC_TYPE}")
if [ -z "${ASIC_PATH}" ]; then
echo "Unknown ASIC type \"${ASIC_TYPE}\""
exit 1
fi
if [ ! -z "${TARGET}" ]; then
if [ ! -d "${ASIC_PATH}/${TARGET}" ]; then
echo "Unknown target \"${TARGET}\""
exit 1
fi
else
# Get first folder as a default target
TARGETS=( $(find -L "${ASIC_PATH}" -mindepth 1 -maxdepth 1 -type d) )
TARGET="${TARGETS[0]}"
if [ -z "${TARGET}" ]; then
echo "Not able to find a default target..."
exit 1
fi
TARGET=$(basename $TARGET)
fi
fi
print-start-options() {
echo
echo "==========================================="
echo " SAI Challenger ${COMMAND} options"
echo "==========================================="
echo
echo " Docker image type : ${IMAGE_TYPE}"
echo " ASIC name : ${ASIC_TYPE}"
echo " ASIC target : ${TARGET}"
echo " Platform path : ${ASIC_PATH}"
echo " SAI interface : ${SAI_INTERFACE}"
echo
echo "==========================================="
echo
}
trap print-start-options EXIT
stop_docker_container() {
DOCKER_NAME=$1
PID=$(docker inspect --format '{{ .State.Pid }}' $DOCKER_NAME)
NETNS="$DOCKER_NAME_$PID"
docker stop $DOCKER_NAME
# Remove NetNS symbolic link if any
[ -L /var/run/netns/$NETNS ] && sudo rm /var/run/netns/$NETNS
}
if [ "${SAI_INTERFACE}" = "thrift" ]; then
PREFIX="sc-thrift"
else
PREFIX="sc"
fi
if [ "${COMMAND}" = "start" ]; then
# Start Docker container
if [ "${IMAGE_TYPE}" = "standalone" ]; then
IMG_NAME=$(echo "${PREFIX}-${ASIC_TYPE}-${TARGET}" | tr '[:upper:]' '[:lower:]')
docker run --name ${IMG_NAME}-run \
-v $(pwd):/sai-challenger \
--cap-add=NET_ADMIN \
${OPTS} \
--device /dev/net/tun:/dev/net/tun \
-d ${IMG_NAME}
elif [ "${IMAGE_TYPE}" = "server" ]; then
docker run --name sc-server-${ASIC_TYPE}-${TARGET}-run \
--cap-add=NET_ADMIN \
${OPTS} \
--device /dev/net/tun:/dev/net/tun \
-d sc-server-${ASIC_TYPE}-${TARGET}
else
docker run --name ${PREFIX}-client-run \
-v $(pwd):/sai-challenger \
--cap-add=NET_ADMIN \
--device /dev/net/tun:/dev/net/tun \
${OPTS} \
-d ${PREFIX}-client
fi
elif [ "${COMMAND}" = "stop" ]; then
# Stop Docker container
if [ "${IMAGE_TYPE}" = "standalone" ]; then
stop_docker_container ${PREFIX}-${ASIC_TYPE}-${TARGET}-run
elif [ "${IMAGE_TYPE}" = "server" ]; then
stop_docker_container sc-server-${ASIC_TYPE}-${TARGET}-run
else
stop_docker_container ${PREFIX}-client-run
fi
else
echo "Unknown command \"${COMMAND}\". Supported: start|stop."
fi