]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-sim.in
bump version to 2.15.0+ds1-2+deb11u3.1
[ovs.git] / utilities / ovs-sim.in
1 #! /usr/bin/env bash
2 #
3 # Copyright (c) 2013, 2015, 2016 Nicira, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -e
18
19 sim_builddir='@abs_builddir@'; export sim_builddir
20 sim_srcdir='@abs_top_srcdir@'; export sim_srcdir
21 interactive=false
22 scripts=
23
24 for option; do
25 case $option in
26 -h|--help)
27 cat <<EOF
28 $0, for starting sandboxed dummy Open vSwitch environments
29 usage: $0 [OPTION...] [SCRIPT...]
30
31 Options:
32 -i, --interactive Prompt for interactive input (default if no SCRIPTs)
33 -h, --help Print this usage message.
34 EOF
35 exit 0
36 ;;
37
38 -i|--i*)
39 interactive=:
40 ;;
41
42 -*)
43 echo "unrecognized option $option (use --help for help)" >&2
44 exit 1
45 ;;
46 *)
47 case $option in
48 /*) ;;
49 *) option=`pwd`/$option ;;
50 esac
51 scripts="$scripts $option"
52 ;;
53 esac
54 shift
55 done
56
57 if test -z "$scripts"; then
58 interactive=:
59 fi
60
61 # Check that we've got proper builddir and srcdir.
62 if test ! -e "$sim_builddir"/vswitchd/ovs-vswitchd; then
63 echo "$sim_builddir/vswitchd/ovs-vswitchd does not exist (need to run \"make\"?)" >&2
64 exit 1
65 fi
66 if test ! -e "$sim_srcdir"/README.rst; then
67 echo "$sim_srcdir/README.rst does not exist" >&2
68 exit 1
69 fi
70
71 # Put built tools early in $PATH.
72 PATH=$sim_builddir/ovsdb:$sim_builddir/vswitchd:$sim_builddir/utilities:$PATH
73 export PATH
74
75 rm -rf sandbox
76 mkdir sandbox
77 cd sandbox
78 sim_base=`pwd`; export sim_base
79
80 trap_signals() {
81 for signal in 0 1 2 3 13 14 15; do
82 trap "
83 set +e
84 cd '$sim_base' && (kill \`cat */*.pid\`) >/dev/null 2>&1
85 trap - $signal
86 kill -$signal $$" $signal
87 done
88 }
89 export -f trap_signals
90 trap_signals
91
92 sim_setvars() {
93 sandbox=$1
94 OVS_RUNDIR=$sim_base/$1; export OVS_RUNDIR
95 OVS_LOGDIR=$sim_base/$1; export OVS_LOGDIR
96 OVS_DBDIR=$sim_base/$1; export OVS_DBDIR
97 OVS_SYSCONFDIR=$sim_base/$1; export OVS_SYSCONFDIR
98 PS1="|$1: $sim_PS1"
99 }
100 export -f sim_setvars
101
102 ovs-vsctl () { command ovs-vsctl -vsyslog:off "$@"; }; export -f ovs-vsctl
103 vtep-ctl () { command vtep-ctl -vsyslog:off "$@"; }; export -f vtep-ctl
104
105 as() {
106 case $# in
107 0)
108 echo >&2 "$FUNCNAME: missing arguments (use --help for help)"
109 return 1
110 ;;
111 1)
112 if test "$1" != --help; then
113 sim_setvars $1
114 else
115 cat <<EOF
116 $FUNCNAME: set the default sandbox for Open vSwitch commands
117 usage: $FUNCNAME SANDBOX [COMMAND ARG...]
118 where SANDBOX is the name of the desired sandbox.
119
120 With COMMAND arguments, this command sets the default target for that
121 single command, which it runs directly. Otherwise, it sets the default
122 target for all following commands.
123 EOF
124 fi
125 ;;
126 *)
127 (sim_setvars $1; shift; "$@")
128 ;;
129 esac
130 }
131 export -f as
132
133 sim_add() {
134 if test "$1" == --help; then
135 cat <<EOF
136 $FUNCNAME: create a new sandboxed Open vSwitch instance
137 usage: $FUNCNAME SANDBOX
138
139 where SANDBOX is the name of the new sandbox, which will be created in
140 a directory named $sim_base/SANDBOX.
141 Afterward, use "as SANDBOX" to execute OVS commands in the sandbox's
142 context.
143 EOF
144 return 0
145 fi
146 if test $# != 1; then
147 echo >&2 "$FUNCNAME: missing argument (use --help for help)"
148 return 1
149 fi
150
151 set X $1; shift
152 if test $# != 1; then
153 echo >&2 "$FUNCNAME: sandbox name must be a single word"
154 return 1
155 fi
156
157 if test -e "$sim_base/$1"; then
158 echo >&2 "$1 already exists"
159 return 1
160 fi
161
162 # Create sandbox.
163 mkdir "$sim_base"/$1 || return 1
164
165 daemon_opts="--detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file"
166
167 # Create database and start ovsdb-server.
168 touch $sim_base/$1/.conf.db.~lock~
169 as $1 ovsdb-tool create $sim_base/$1/conf.db "$sim_srcdir/vswitchd/vswitch.ovsschema"
170 as $1 ovsdb-server --remote=punix:"$sim_base"/$1/db.sock $daemon_opts
171
172 # Initialize database.
173 as $1 ovs-vsctl --no-wait -- init
174
175 # Start ovs-vswitchd.
176 as $1 ovs-vswitchd --enable-dummy=system -vvconn -vnetdev_dummy $daemon_opts
177 }
178 export -f sim_add
179
180 net_add() {
181 if test "$1" == --help; then
182 cat <<EOF
183 $FUNCNAME: create a new interconnection network
184 usage: $FUNCNAME NETWORK
185
186 where NETWORK is the name of the new network. Interconnection networks
187 are used with net_attach.
188 EOF
189 return 0
190 fi
191 if test $# != 1; then
192 echo >&2 "$FUNCNAME: missing argument (use --help for help)"
193 return 1
194 fi
195
196 as main ovs-vsctl add-br "$1"
197 }
198 export -f net_add
199
200 net_attach() {
201 if test "$1" == --help; then
202 cat <<EOF
203 $FUNCNAME: attach the default sandbox to an interconnection network
204 usage: $FUNCNAME NETWORK BRIDGE
205
206 Adds a port to BRIDGE within the default sandbox that connects BRIDGE
207 to the interconnection network NETWORK. (Use "as" to set the default
208 sandbox.)
209 EOF
210 return 0
211 fi
212 if test $# != 2; then
213 echo >&2 "$FUNCNAME: wrong number of arguments (use --help for help)"
214 return 1
215 fi
216 if test $sandbox = main; then
217 echo >&2 "$FUNCNAME: can only attach interconnection networks to sandboxes other than main"
218 return 1
219 fi
220
221 local net=$1 bridge=$2
222
223 port=${sandbox}_$bridge
224 as main ovs-vsctl \
225 -- add-port $net "$port" \
226 -- set Interface "$port" options:pstream="punix:$sim_base/main/$port.sock" options:rxq_pcap="$sim_base/main/$port-rx.pcap" options:tx_pcap="$sim_base/main/$port-tx.pcap" options:header=extended
227
228 ovs-vsctl \
229 -- set Interface $bridge options:tx_pcap="$sim_base/$sandbox/$bridge-tx.pcap" options:rxq_pcap="$sim_base/$sandbox/$bridge-rx.pcap" \
230 -- add-port $bridge ${bridge}_$net \
231 -- set Interface ${bridge}_$net options:stream="unix:$sim_base/main/$port.sock" options:rxq_pcap="$sim_base/$sandbox/${bridge}_$net-rx.pcap" options:tx_pcap="$sim_base/$sandbox/${bridge}_$net-tx.pcap" options:header=extended
232 }
233 export -f net_attach
234
235 # Easy access to OVS manpages.
236 mkdir $sim_base/man
237 mandir=`cd $sim_base/man && pwd`
238 (cd "$sim_builddir" && ${MAKE-make} install-man install-man-rst mandir=$mandir EXTRA_RST_MANPAGES=ovs-sim.1.rst >/dev/null)
239 MANPATH=$mandir:; export MANPATH
240
241 export scripts
242 export interactive
243 rc='
244 if [ -f /etc/bashrc ]; then
245 . /etc/bashrc
246 fi
247 if [ -f ~/.bashrc ]; then
248 . ~/.bashrc
249 fi
250
251 trap_signals
252 sim_PS1=$PS1
253 sim_add main
254 as main
255
256 for script in $scripts; do
257 . $script || exit $?
258 done
259
260 $interactive || exit 0
261
262 cat <<EOF
263 ______________________________________________________________________
264 |
265 | You are running in a nested shell environment meant for Open vSwitch
266 | testing in simulation. The OVS manpages are available via "man".
267 | Please see ovs-sim(1) for more information.
268 |
269 | Exit the shell to kill the running daemons and leave the simulation
270 | environment.
271 EOF
272 '
273
274 set +e
275 status=0; bash --rcfile <(echo "$rc") || status=$?
276
277 if $interactive; then
278 cat <<EOF
279 |______________________________________________________________________
280
281 EOF
282 fi
283
284 exit $status