]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-sim.in
ovs-vsctl: Add datapath_type column to show command.
[mirror_ovs.git] / utilities / ovs-sim.in
CommitLineData
6901639b
BP
1#! /usr/bin/env bash
2#
fa183acc 3# Copyright (c) 2013, 2015, 2016 Nicira, Inc.
6901639b
BP
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
17set -e
18
19sim_builddir='@abs_builddir@'; export sim_builddir
20sim_srcdir='@abs_top_srcdir@'; export sim_srcdir
21interactive=false
22scripts=
23
24for option; do
25 case $option in
26 -h|--help)
27 cat <<EOF
28$0, for starting sandboxed dummy Open vSwitch environments
29usage: $0 [OPTION...] [SCRIPT...]
30
31Options:
32 -i, --interactive Prompt for interactive input (default if no SCRIPTs)
33 -h, --help Print this usage message.
34EOF
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 *)
49d9bd5b
SF
47 case $option in
48 /*) ;;
49 *) option=`pwd`/$option ;;
50 esac
6901639b
BP
51 scripts="$scripts $option"
52 ;;
53 esac
54 shift
55done
56
57if test -z "$scripts"; then
58 interactive=:
59fi
60
61# Check that we've got proper builddir and srcdir.
62if 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
65fi
3c8a3b31
SF
66if test ! -e "$sim_srcdir"/README.rst; then
67 echo "$sim_srcdir/README.rst does not exist" >&2
6901639b
BP
68 exit 1
69fi
70
71# Put built tools early in $PATH.
72PATH=$sim_builddir/ovsdb:$sim_builddir/vswitchd:$sim_builddir/utilities:$PATH
72eaa2ba 73PATH=$sim_builddir/ovn/controller:$sim_builddir/ovn/northd:$sim_builddir/ovn/utilities:$PATH
6901639b
BP
74export PATH
75
76rm -rf sandbox
77mkdir sandbox
78cd sandbox
79sim_base=`pwd`; export sim_base
80
81trap_signals() {
82 for signal in 0 1 2 3 13 14 15; do
49d9bd5b
SF
83 trap "
84 set +e
85 cd '$sim_base' && (kill \`cat */*.pid\`) >/dev/null 2>&1
86 trap - $signal
87 kill -$signal $$" $signal
6901639b
BP
88 done
89}
90export -f trap_signals
91trap_signals
92
93sim_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}
101export -f sim_setvars
102
2128f9b4
BP
103ovs-vsctl () { command ovs-vsctl -vsyslog:off "$@"; }; export -f ovs-vsctl
104ovs-nbctl () { command ovs-nbctl -vsyslog:off "$@"; }; export -f ovs-nbctl
105ovs-sbctl () { command ovs-sbctl -vsyslog:off "$@"; }; export -f ovs-sbctl
106vtep-ctl () { command vtep-ctl -vsyslog:off "$@"; }; export -f vtep-ctl
107
6901639b
BP
108as() {
109 case $# in
49d9bd5b 110 0)
6901639b
BP
111 echo >&2 "$FUNCNAME: missing arguments (use --help for help)"
112 return 1
49d9bd5b
SF
113 ;;
114 1)
115 if test "$1" != --help; then
116 sim_setvars $1
117 else
118 cat <<EOF
6901639b
BP
119$FUNCNAME: set the default sandbox for Open vSwitch commands
120usage: $FUNCNAME SANDBOX [COMMAND ARG...]
121where SANDBOX is the name of the desired sandbox.
122
123With COMMAND arguments, this command sets the default target for that
124single command, which it runs directly. Otherwise, it sets the default
125target for all following commands.
126EOF
49d9bd5b
SF
127 fi
128 ;;
129 *)
e36f7b9a 130 (sim_setvars $1; shift; "$@")
49d9bd5b 131 ;;
6901639b
BP
132 esac
133}
134export -f as
135
136sim_add() {
137 if test "$1" == --help; then
138 cat <<EOF
139$FUNCNAME: create a new sandboxed Open vSwitch instance
140usage: $FUNCNAME SANDBOX
141
142where SANDBOX is the name of the new sandbox, which will be created in
143a directory named $sim_base/SANDBOX.
144Afterward, use "as SANDBOX" to execute OVS commands in the sandbox's
145context.
146EOF
49d9bd5b 147 return 0
6901639b
BP
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
49d9bd5b
SF
161 echo >&2 "$1 already exists"
162 return 1
6901639b
BP
163 fi
164
165 # Create sandbox.
166 mkdir "$sim_base"/$1 || return 1
167
2128f9b4 168 daemon_opts="--detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file"
6901639b
BP
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"
2128f9b4 173 as $1 ovsdb-server --remote=punix:"$sim_base"/$1/db.sock $daemon_opts
6901639b
BP
174
175 # Initialize database.
176 as $1 ovs-vsctl --no-wait -- init
177
178 # Start ovs-vswitchd.
2128f9b4 179 as $1 ovs-vswitchd --enable-dummy=system -vvconn -vnetdev_dummy $daemon_opts
6901639b
BP
180}
181export -f sim_add
182
183net_add() {
184 if test "$1" == --help; then
185 cat <<EOF
186$FUNCNAME: create a new interconnection network
187usage: $FUNCNAME NETWORK
188
189where NETWORK is the name of the new network. Interconnection networks
190are used with net_attach and ovn_attach.
191EOF
49d9bd5b 192 return 0
6901639b
BP
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}
201export -f net_add
202
203net_attach() {
204 if test "$1" == --help; then
205 cat <<EOF
206$FUNCNAME: attach the default sandbox to an interconnection network
207usage: $FUNCNAME NETWORK BRIDGE
208
209Adds a port to BRIDGE within the default sandbox that connects BRIDGE
210to the interconnection network NETWORK. (Use "as" to set the default
211sandbox.)
212EOF
49d9bd5b 213 return 0
6901639b
BP
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
49d9bd5b
SF
220 echo >&2 "$FUNCNAME: can only attach interconnection networks to sandboxes other than main"
221 return 1
6901639b
BP
222 fi
223
224 local net=$1 bridge=$2
225
226 port=${sandbox}_$bridge
227 as main ovs-vsctl \
49d9bd5b
SF
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
6901639b
BP
230
231 ovs-vsctl \
49d9bd5b
SF
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
6901639b
BP
235}
236export -f net_attach
237
9a6b8961
BP
238ovn_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
5a0e4aec
BP
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 ;;
9a6b8961
BP
257 esac
258
259 ovn_start_ovsdb_server() {
5a0e4aec
BP
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 "$@"
9a6b8961
BP
271 }
272
273 ovn_prep_db() {
5a0e4aec
BP
274 local i=$1
275 mkdir "$sim_base"/${db}$i
276 touch "$sim_base"/${db}$i/.$db.db.~lock~
9a6b8961
BP
277 }
278
279 local n_remotes=1
280 case $model in
5a0e4aec
BP
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
9a6b8961
BP
294The 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}
297The backup database file is $sim_base/${db}2/$db.db
298EOF
5a0e4aec
BP
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 ;;
9a6b8961
BP
315 esac
316
317 remote=unix:"$sim_base"/${db}1/$db.ovsdb
318 for i in `seq 2 $n_remotes`; do
5a0e4aec 319 remote=$remote,unix:"$sim_base"/${db}$i/$db.ovsdb
9a6b8961
BP
320 done
321 eval OVN_${DB}_DB=\$remote
322 eval export OVN_${DB}_DB
323}
324export -f ovn_start_db
325
6901639b 326ovn_start() {
9a6b8961
BP
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
5a0e4aec
BP
333 # This option-parsing mechanism borrowed from a Autoconf-generated
334 # configure script under the following license:
9a6b8961 335
5a0e4aec
BP
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.
9a6b8961 340
5a0e4aec
BP
341 # If the previous option needs an argument, assign it.
342 if test -n "$prev"; then
9a6b8961
BP
343 eval $prev=\$option
344 prev=
345 continue
5a0e4aec
BP
346 fi
347 case $option in
9a6b8961
BP
348 *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
349 *) optarg=yes ;;
5a0e4aec 350 esac
9a6b8961 351
5a0e4aec 352 case $dashdash$option in
9a6b8961 353 --)
5a0e4aec 354 dashdash=yes ;;
9a6b8961 355 -h|--help)
5a0e4aec 356 cat <<EOF
6901639b 357$FUNCNAME: start OVN central databases and daemons
9a6b8961 358usage: $FUNCNAME [OPTION...]
6901639b
BP
359
360This creates and initializes the central OVN databases (northbound and
361southbound), starts their ovsdb-server daemons, and starts the ovn-northd
362daemon.
9a6b8961
BP
363
364Options:
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.
6901639b 370EOF
5a0e4aec
BP
371 return
372 ;;
9a6b8961
BP
373
374 --nbdb-s*=*)
5a0e4aec
BP
375 nbdb_servers=$optarg
376 nbdb_model=clustered
377 ;;
9a6b8961 378 --nbdb-s*)
5a0e4aec
BP
379 prev=nbdb_servers
380 nbdb_model=clustered
381 ;;
9a6b8961 382 --nbdb-m*=*)
5a0e4aec
BP
383 nbdb_model=$optarg
384 ;;
9a6b8961 385 --nbdb-m*)
5a0e4aec
BP
386 prev=nbdb_model
387 ;;
9a6b8961 388 --sbdb-s*=*)
5a0e4aec
BP
389 sbdb_servers=$optarg
390 sbdb_model=clustered
391 ;;
9a6b8961 392 --sbdb-s*)
5a0e4aec
BP
393 prev=sbdb_servers
394 sbdb_model=clustered
395 ;;
9a6b8961 396 --sbdb-m*=*)
5a0e4aec
BP
397 sbdb_model=$optarg
398 ;;
9a6b8961 399 --sbdb-m*)
5a0e4aec
BP
400 prev=sbdb_model
401 ;;
9a6b8961 402 -*)
5a0e4aec
BP
403 echo "unrecognized option $option (use --help for help)" >&2
404 return 1
405 ;;
9a6b8961 406 *)
5a0e4aec
BP
407 echo "$option: non-option arguments not supported (use --help for help)" >&2
408 return 1
409 ;;
410 esac
411 shift
9a6b8961 412 done
6901639b
BP
413
414 if test -d ovn-sb || test -d ovn-nb; then
49d9bd5b 415 echo >&2 "OVN already started"
9a6b8961 416 return 1
6901639b
BP
417 fi
418
9a6b8961
BP
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
6901639b 421
fa183acc
BP
422 ovn-nbctl init
423 ovn-sbctl init
424
6901639b 425 mkdir "$sim_base"/northd
2128f9b4
BP
426 as northd ovn-northd --ovnnb-db="$OVN_NB_DB" --ovnsb-db="$OVN_SB_DB" \
427 $daemon_opts
6901639b
BP
428}
429export -f ovn_start
430
431ovn_attach() {
432 if test "$1" == --help; then
433 cat <<EOF
434$FUNCNAME: attach default sandbox to an interconnection network for OVN
435usage: $FUNCNAME NETWORK BRIDGE IP [MASKLEN]
436
437This starts by doing everything that net_attach does. Then it configures the
438specified IP and MASKLEN (e.g. 192.168.0.1 and 24) on BRIDGE and starts
439and configures ovn-controller.
440
441MASKLEN defaults to 24 if it is not specified.
442EOF
49d9bd5b 443 return 0
6901639b
BP
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 \
49d9bd5b 456 -- set Open_vSwitch . external-ids:system-id=$sandbox \
9a6b8961 457 -- set Open_vSwitch . external-ids:ovn-remote=$OVN_SB_DB \
49d9bd5b
SF
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
2128f9b4 462 ovn-controller --detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file
6901639b
BP
463}
464export -f ovn_attach
465
466# Easy access to OVS manpages.
467mkdir $sim_base/man
468mandir=`cd $sim_base/man && pwd`
16ee8726 469(cd "$sim_builddir" && ${MAKE-make} install-man install-man-rst mandir=$mandir EXTRA_RST_MANPAGES=ovs-sim.1.rst >/dev/null)
6901639b
BP
470MANPATH=$mandir:; export MANPATH
471
472export scripts
473export interactive
474rc='
475 if [ -f /etc/bashrc ]; then
49d9bd5b 476 . /etc/bashrc
6901639b
BP
477 fi
478 if [ -f ~/.bashrc ]; then
49d9bd5b 479 . ~/.bashrc
6901639b
BP
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.
502EOF
503'
504
2128f9b4 505set +e
6901639b
BP
506status=0; bash --rcfile <(echo "$rc") || status=$?
507
508if $interactive; then
509 cat <<EOF
510|______________________________________________________________________
511
512EOF
513fi
514
515exit $status