]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-sim.in
treewide: Convert leading tabs to spaces.
[mirror_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 PATH=$sim_builddir/ovn/controller:$sim_builddir/ovn/northd:$sim_builddir/ovn/utilities:$PATH
74 export PATH
75
76 rm -rf sandbox
77 mkdir sandbox
78 cd sandbox
79 sim_base=`pwd`; export sim_base
80
81 trap_signals() {
82 for signal in 0 1 2 3 13 14 15; do
83 trap "
84 set +e
85 cd '$sim_base' && (kill \`cat */*.pid\`) >/dev/null 2>&1
86 trap - $signal
87 kill -$signal $$" $signal
88 done
89 }
90 export -f trap_signals
91 trap_signals
92
93 sim_setvars() {
94 sandbox=$1
95 OVS_RUNDIR=$sim_base/$1; export OVS_RUNDIR
96 OVS_LOGDIR=$sim_base/$1; export OVS_LOGDIR
97 OVS_DBDIR=$sim_base/$1; export OVS_DBDIR
98 OVS_SYSCONFDIR=$sim_base/$1; export OVS_SYSCONFDIR
99 PS1="|$1: $sim_PS1"
100 }
101 export -f sim_setvars
102
103 ovs-vsctl () { command ovs-vsctl -vsyslog:off "$@"; }; export -f ovs-vsctl
104 ovs-nbctl () { command ovs-nbctl -vsyslog:off "$@"; }; export -f ovs-nbctl
105 ovs-sbctl () { command ovs-sbctl -vsyslog:off "$@"; }; export -f ovs-sbctl
106 vtep-ctl () { command vtep-ctl -vsyslog:off "$@"; }; export -f vtep-ctl
107
108 as() {
109 case $# in
110 0)
111 echo >&2 "$FUNCNAME: missing arguments (use --help for help)"
112 return 1
113 ;;
114 1)
115 if test "$1" != --help; then
116 sim_setvars $1
117 else
118 cat <<EOF
119 $FUNCNAME: set the default sandbox for Open vSwitch commands
120 usage: $FUNCNAME SANDBOX [COMMAND ARG...]
121 where SANDBOX is the name of the desired sandbox.
122
123 With COMMAND arguments, this command sets the default target for that
124 single command, which it runs directly. Otherwise, it sets the default
125 target for all following commands.
126 EOF
127 fi
128 ;;
129 *)
130 (sim_setvars $1; shift; "$@")
131 ;;
132 esac
133 }
134 export -f as
135
136 sim_add() {
137 if test "$1" == --help; then
138 cat <<EOF
139 $FUNCNAME: create a new sandboxed Open vSwitch instance
140 usage: $FUNCNAME SANDBOX
141
142 where SANDBOX is the name of the new sandbox, which will be created in
143 a directory named $sim_base/SANDBOX.
144 Afterward, use "as SANDBOX" to execute OVS commands in the sandbox's
145 context.
146 EOF
147 return 0
148 fi
149 if test $# != 1; then
150 echo >&2 "$FUNCNAME: missing argument (use --help for help)"
151 return 1
152 fi
153
154 set X $1; shift
155 if test $# != 1; then
156 echo >&2 "$FUNCNAME: sandbox name must be a single word"
157 return 1
158 fi
159
160 if test -e "$sim_base/$1"; then
161 echo >&2 "$1 already exists"
162 return 1
163 fi
164
165 # Create sandbox.
166 mkdir "$sim_base"/$1 || return 1
167
168 daemon_opts="--detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file"
169
170 # Create database and start ovsdb-server.
171 touch $sim_base/$1/.conf.db.~lock~
172 as $1 ovsdb-tool create $sim_base/$1/conf.db "$sim_srcdir/vswitchd/vswitch.ovsschema"
173 as $1 ovsdb-server --remote=punix:"$sim_base"/$1/db.sock $daemon_opts
174
175 # Initialize database.
176 as $1 ovs-vsctl --no-wait -- init
177
178 # Start ovs-vswitchd.
179 as $1 ovs-vswitchd --enable-dummy=system -vvconn -vnetdev_dummy $daemon_opts
180 }
181 export -f sim_add
182
183 net_add() {
184 if test "$1" == --help; then
185 cat <<EOF
186 $FUNCNAME: create a new interconnection network
187 usage: $FUNCNAME NETWORK
188
189 where NETWORK is the name of the new network. Interconnection networks
190 are used with net_attach and ovn_attach.
191 EOF
192 return 0
193 fi
194 if test $# != 1; then
195 echo >&2 "$FUNCNAME: missing argument (use --help for help)"
196 return 1
197 fi
198
199 as main ovs-vsctl add-br "$1"
200 }
201 export -f net_add
202
203 net_attach() {
204 if test "$1" == --help; then
205 cat <<EOF
206 $FUNCNAME: attach the default sandbox to an interconnection network
207 usage: $FUNCNAME NETWORK BRIDGE
208
209 Adds a port to BRIDGE within the default sandbox that connects BRIDGE
210 to the interconnection network NETWORK. (Use "as" to set the default
211 sandbox.)
212 EOF
213 return 0
214 fi
215 if test $# != 2; then
216 echo >&2 "$FUNCNAME: wrong number of arguments (use --help for help)"
217 return 1
218 fi
219 if test $sandbox = main; then
220 echo >&2 "$FUNCNAME: can only attach interconnection networks to sandboxes other than main"
221 return 1
222 fi
223
224 local net=$1 bridge=$2
225
226 port=${sandbox}_$bridge
227 as main ovs-vsctl \
228 -- add-port $net "$port" \
229 -- 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
230
231 ovs-vsctl \
232 -- set Interface $bridge options:tx_pcap="$sim_base/$sandbox/$bridge-tx.pcap" options:rxq_pcap="$sim_base/$sandbox/$bridge-rx.pcap" \
233 -- add-port $bridge ${bridge}_$net \
234 -- 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
235 }
236 export -f net_attach
237
238 ovn_start_db() {
239 local db=$1 model=$2 servers=$3 schema=$4
240 local DB=$(echo $db | tr a-z A-Z)
241 local schema_name=$(ovsdb-tool schema-name $schema)
242
243 case $model in
244 standalone | backup) ;;
245 clustered)
246 case $servers in
247 [1-9] | [1-9][0-9]) ;;
248 *) echo "${db}db servers must be between 1 and 99" >&2
249 exit 1
250 ;;
251 esac
252 ;;
253 *)
254 echo "unknown ${db}db model \"$model\"" >&2
255 exit 1
256 ;;
257 esac
258
259 ovn_start_ovsdb_server() {
260 local i=$1; shift
261 as ${db}$i ovsdb-server --detach --no-chdir --pidfile=$db.pid \
262 -vsyslog:off -vconsole:off --log-file="$sim_base"/$db$i/$db.log \
263 --remote=db:$schema_name,${DB}_Global,connections \
264 --private-key=db:$schema_name,SSL,private_key \
265 --certificate=db:$schema_name,SSL,certificate \
266 --ca-cert=db:$schema_name,SSL,ca_cert \
267 --ssl-protocols=db:$schema_name,SSL,ssl_protocols \
268 --ssl-ciphers=db:$schema_name,SSL,ssl_ciphers \
269 --unixctl=${db} --remote=punix:$db.ovsdb \
270 "$sim_base"/$db$i/$db.db "$@"
271 }
272
273 ovn_prep_db() {
274 local i=$1
275 mkdir "$sim_base"/${db}$i
276 touch "$sim_base"/${db}$i/.$db.db.~lock~
277 }
278
279 local n_remotes=1
280 case $model in
281 standalone)
282 ovn_prep_db 1
283 ovsdb-tool create "$sim_base"/${db}1/$db.db "$schema"
284 ovn_start_ovsdb_server 1
285 ;;
286 backup)
287 for i in 1 2; do
288 ovn_prep_db $i
289 ovsdb-tool create "$sim_base"/$db$i/$db.db "$schema"
290 done
291 ovn_start_ovsdb_server 1
292 ovn_start_ovsdb_server 2 --sync-from=unix:"$sim_base"/${db}1/$db.ovsdb
293 cat <<EOF
294 The backup server of OVN $DB can be accessed by:
295 * ovn-${db}ctl --db=unix:$sim_base/${db}2/$db.ovsdb
296 * ovs-appctl -t $sim_base/${db}2/${db}
297 The backup database file is $sim_base/${db}2/$db.db
298 EOF
299 ;;
300 clustered)
301 n_remotes=$servers
302 for i in $(seq $servers); do
303 ovn_prep_db $i
304 if test $i = 1; then
305 ovsdb-tool create-cluster "$sim_base"/$db$i/$db.db "$schema" unix:"$sim_base"/$db$i/db.raft
306 else
307 ovsdb-tool join-cluster "$sim_base"/$db$i/$db.db $schema_name unix:"$sim_base"/$db$i/db.raft unix:"$sim_base"/${db}1/db.raft
308 fi
309 ovn_start_ovsdb_server $i
310 done
311 for i in $(seq $servers); do
312 ovsdb-client wait unix:"$sim_base"/${db}$i/$db.ovsdb $schema_name connected
313 done
314 ;;
315 esac
316
317 remote=unix:"$sim_base"/${db}1/$db.ovsdb
318 for i in `seq 2 $n_remotes`; do
319 remote=$remote,unix:"$sim_base"/${db}$i/$db.ovsdb
320 done
321 eval OVN_${DB}_DB=\$remote
322 eval export OVN_${DB}_DB
323 }
324 export -f ovn_start_db
325
326 ovn_start() {
327 local nbdb_model=standalone
328 local nbdb_servers=3
329 local sbdb_model=standalone
330 local sbdb_servers=3
331 local prev=
332 for option; do
333 # This option-parsing mechanism borrowed from a Autoconf-generated
334 # configure script under the following license:
335
336 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
337 # 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
338 # This configure script is free software; the Free Software Foundation
339 # gives unlimited permission to copy, distribute and modify it.
340
341 # If the previous option needs an argument, assign it.
342 if test -n "$prev"; then
343 eval $prev=\$option
344 prev=
345 continue
346 fi
347 case $option in
348 *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
349 *) optarg=yes ;;
350 esac
351
352 case $dashdash$option in
353 --)
354 dashdash=yes ;;
355 -h|--help)
356 cat <<EOF
357 $FUNCNAME: start OVN central databases and daemons
358 usage: $FUNCNAME [OPTION...]
359
360 This creates and initializes the central OVN databases (northbound and
361 southbound), starts their ovsdb-server daemons, and starts the ovn-northd
362 daemon.
363
364 Options:
365 --nbdb-model=standalone|backup|clustered northbound database model
366 --nbdb-servers=N number of servers in nbdb cluster (default: 3)
367 --sbdb-model=standalone|backup|clustered southbound database model
368 --sbdb-servers=N number of servers in sbdb cluster (default: 3)
369 -h, --help Print this usage message.
370 EOF
371 return
372 ;;
373
374 --nbdb-s*=*)
375 nbdb_servers=$optarg
376 nbdb_model=clustered
377 ;;
378 --nbdb-s*)
379 prev=nbdb_servers
380 nbdb_model=clustered
381 ;;
382 --nbdb-m*=*)
383 nbdb_model=$optarg
384 ;;
385 --nbdb-m*)
386 prev=nbdb_model
387 ;;
388 --sbdb-s*=*)
389 sbdb_servers=$optarg
390 sbdb_model=clustered
391 ;;
392 --sbdb-s*)
393 prev=sbdb_servers
394 sbdb_model=clustered
395 ;;
396 --sbdb-m*=*)
397 sbdb_model=$optarg
398 ;;
399 --sbdb-m*)
400 prev=sbdb_model
401 ;;
402 -*)
403 echo "unrecognized option $option (use --help for help)" >&2
404 return 1
405 ;;
406 *)
407 echo "$option: non-option arguments not supported (use --help for help)" >&2
408 return 1
409 ;;
410 esac
411 shift
412 done
413
414 if test -d ovn-sb || test -d ovn-nb; then
415 echo >&2 "OVN already started"
416 return 1
417 fi
418
419 ovn_start_db nb "$nbdb_model" "$nbdb_servers" "$sim_srcdir"/ovn/ovn-nb.ovsschema
420 ovn_start_db sb "$sbdb_model" "$sbdb_servers" "$sim_srcdir"/ovn/ovn-sb.ovsschema
421
422 ovn-nbctl init
423 ovn-sbctl init
424
425 mkdir "$sim_base"/northd
426 as northd ovn-northd --ovnnb-db="$OVN_NB_DB" --ovnsb-db="$OVN_SB_DB" \
427 $daemon_opts
428 }
429 export -f ovn_start
430
431 ovn_attach() {
432 if test "$1" == --help; then
433 cat <<EOF
434 $FUNCNAME: attach default sandbox to an interconnection network for OVN
435 usage: $FUNCNAME NETWORK BRIDGE IP [MASKLEN]
436
437 This starts by doing everything that net_attach does. Then it configures the
438 specified IP and MASKLEN (e.g. 192.168.0.1 and 24) on BRIDGE and starts
439 and configures ovn-controller.
440
441 MASKLEN defaults to 24 if it is not specified.
442 EOF
443 return 0
444 fi
445 if test $# != 3 && test $# != 4; then
446 echo >&2 "$FUNCNAME: wrong number of arguments (use --help for help)"
447 return 1
448 fi
449
450 local net=$1 bridge=$2 ip=$3 masklen=${4-24}
451 net_attach $net $bridge || return $?
452
453 ovs-appctl netdev-dummy/ip4addr $bridge $ip/$masklen >/dev/null
454 ovs-appctl ovs/route/add $ip/$masklen $bridge > /dev/null
455 ovs-vsctl \
456 -- set Open_vSwitch . external-ids:system-id=$sandbox \
457 -- set Open_vSwitch . external-ids:ovn-remote=$OVN_SB_DB \
458 -- set Open_vSwitch . external-ids:ovn-encap-type=geneve \
459 -- set Open_vSwitch . external-ids:ovn-encap-ip=$ip\
460 -- add-br br-int \
461 -- set bridge br-int fail-mode=secure other-config:disable-in-band=true
462 ovn-controller --detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file
463 }
464 export -f ovn_attach
465
466 # Easy access to OVS manpages.
467 mkdir $sim_base/man
468 mandir=`cd $sim_base/man && pwd`
469 (cd "$sim_builddir" && ${MAKE-make} install-man install-man-rst mandir=$mandir >/dev/null)
470 MANPATH=$mandir:; export MANPATH
471
472 export scripts
473 export interactive
474 rc='
475 if [ -f /etc/bashrc ]; then
476 . /etc/bashrc
477 fi
478 if [ -f ~/.bashrc ]; then
479 . ~/.bashrc
480 fi
481
482 trap_signals
483 sim_PS1=$PS1
484 sim_add main
485 as main
486
487 for script in $scripts; do
488 . $script || exit $?
489 done
490
491 $interactive || exit 0
492
493 cat <<EOF
494 ______________________________________________________________________
495 |
496 | You are running in a nested shell environment meant for Open vSwitch
497 | and OVN testing in simulation. The OVS manpages are available via
498 | "man". Please see ovs-sim(1) for more information.
499 |
500 | Exit the shell to kill the running daemons and leave the simulation
501 | environment.
502 EOF
503 '
504
505 set +e
506 status=0; bash --rcfile <(echo "$rc") || status=$?
507
508 if $interactive; then
509 cat <<EOF
510 |______________________________________________________________________
511
512 EOF
513 fi
514
515 exit $status