-
Notifications
You must be signed in to change notification settings - Fork 4
/
run-trex
executable file
·152 lines (135 loc) · 5.07 KB
/
run-trex
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
#!/bin/bash -eux
# Builds and runs the trex container.
[[ ${EUID} -ne 0 ]] && echo "This script must be run as root." && exit 1
# Work out of the directory containing this script.
cd "$(readlink --canonicalize "$(dirname "$0")")"
# Command-line defaults.
BASH=false
CONFIG_SCRIPT=
KILL_EXISTING=false
NO_CONSOLE=false
TREX_CONFIG="$(pwd)/traffic-configurations/mlx5.yaml"
TREX_OPTIONS=
TREX_PATTERNS="$(pwd)/traffic-patterns"
# Parse command-line options.
while [[ ${#} -gt 0 ]]; do
KEY="${1}"
case ${KEY} in
--help)
echo "Start up the trex container to generate traffic."
echo
echo "Usage: $0 [OPTION]..."
echo
echo "Options:"
echo " --bash: Instead of starting the trex server, start a bash shell"
echo " in the trex container instead."
echo " --config-script [path]: Path to a configuration script that configures network"
echo " interfaces, if needed. This script runs in the _host_"
echo " context, but a \"trex\" network namespace will be"
echo " available for manipulating interfaces in the trex container."
echo " Default: none"
echo " --kill-existing: Kill any existing trex container."
echo " --no-console: Don't launch trex-console, just launch the trex container"
echo " and exit this script."
echo " --trex-config [path]: Path to the Trex configuration YAML to use. This file"
echo " contains the configuration for what network interfaces"
echo " to use."
echo " Default: ./traffic-configurations/mlx5.yaml"
echo " --trex-patterns [dir]: The directory containing the traffic patterns to use."
echo " This directory will be mounted to /patterns."
echo " Default: ./traffic-patterns"
echo " -- [options]: Options to pass through directly to t-rex-64. The flag"
echo " --no-ofed-check should be included, or the trex server"
echo " won't start."
echo " Default: -i --no-ofed-check"
exit 0
;;
--bash)
BASH=true
shift 1
;;
--config-script)
CONFIG_SCRIPT="${2}"
shift 2
;;
--config-script=*)
CONFIG_SCRIPT="${KEY#*=}"
shift 2
;;
--kill-existing)
KILL_EXISTING=true
shift 1
;;
--no-console)
NO_CONSOLE=true
shift 1
;;
--trex-config)
TREX_CONFIG="${2}"
shift 2
;;
--trex-config=*)
TREX_CONFIG="${KEY#*=}"
shift
;;
--trex-patterns)
TREX_PATTERNS="${2}"
shift 2
;;
--trex-patterns=*)
TREX_PATTERNS="${KEY#*=}"
shift
;;
--)
shift
TREX_OPTIONS="$@"
break
;;
*)
echo "Unrecognized option: ${KEY}"
exit 1
;;
esac
done
[[ -n "${CONFIG_SCRIPT}" && ! -x "${CONFIG_SCRIPT}" ]] && echo "Config script must be executable." && exit 1
[[ "${BASH}" == "true" && -n "${TREX_OPTIONS}" ]] && echo "Don't provide trex options with --bash." && exit 1
COMMON_OPTIONS=(
--name trex
--hostname trex
--privileged
--cap-add=ALL
--security-opt seccomp=unconfined
--net host
--volume /lib/modules:/lib/modules:ro
--volume "$(realpath ${TREX_CONFIG}):/etc/trex_cfg.yaml:ro"
--volume "$(realpath ${TREX_PATTERNS}):/patterns"
)
docker build . --tag trex
if [[ "${KILL_EXISTING}" == "true" ]]; then
docker rm -f trex || true
fi
# Launch the trex container, but run bash instead of supervisord.
if [[ "${BASH}" == "true" ]]; then
exec docker run ${COMMON_OPTIONS[@]} --interactive --tty --entrypoint /bin/bash trex
fi
# Launch the trex container. Trex will not automatically start.
docker run ${COMMON_OPTIONS[@]} --detach trex
# Expose the trex container's network namespace to the "ip" command by making a symlink in /var/run/netns.
mkdir -p /var/run/netns
rm -f /var/run/netns/trex
ln -sfT /proc/$(docker inspect --format '{{.State.Pid}}' trex)/ns/net /var/run/netns/trex
# Run any interface configuration script if one was provided.
[[ -n "${CONFIG_SCRIPT}" ]] && ${CONFIG_SCRIPT}
# If the trex launch options are overridden, write them to a file inside of the container. When we tell supervisord to
# launch trex, supervisord will read this file and pass the options to trex. This is pretty convoluted, but it works.
[[ -n "${TREX_OPTIONS}" ]] && docker exec trex bash -c "echo ${TREX_OPTIONS} > /tmp/trex-args"
# Now that the interfaces are configured, start the Trex service via supervisord.
docker exec trex supervisorctl start trex
# Open a trex console if requested, otherwise just exit.
[[ "${NO_CONSOLE}" == "true" ]] && exit 0
# Wait until trex server is up.
until docker exec -it trex /bin/bash -c "ss -ntl | grep -q '0.0.0.0:4501'"; do
echo "Waiting for trex to start up..."
sleep 5
done
exec docker exec -it trex ./trex-console