summaryrefslogtreecommitdiff
path: root/utilities/ovs-sim.in
blob: 24c1da0a93e34951e3d26527afc384b55a4bae1e (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#! /usr/bin/env bash
#
# Copyright (c) 2013, 2015, 2016 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

sim_builddir='@abs_builddir@'; export sim_builddir
sim_srcdir='@abs_top_srcdir@'; export sim_srcdir
interactive=false
scripts=

for option; do
    case $option in
        -h|--help)
            cat <<EOF
$0, for starting sandboxed dummy Open vSwitch environments
usage: $0 [OPTION...] [SCRIPT...]

Options:
  -i, --interactive    Prompt for interactive input (default if no SCRIPTs)
  -h, --help           Print this usage message.
EOF
            exit 0
            ;;

        -i|--i*)
            interactive=:
            ;;

        -*)
            echo "unrecognized option $option (use --help for help)" >&2
            exit 1
            ;;
        *)
            case $option in
                /*) ;;
                 *) option=`pwd`/$option ;;
            esac
            scripts="$scripts $option"
            ;;
    esac
    shift
done

if test -z "$scripts"; then
    interactive=:
fi

# Check that we've got proper builddir and srcdir.
if test ! -e "$sim_builddir"/vswitchd/ovs-vswitchd; then
    echo "$sim_builddir/vswitchd/ovs-vswitchd does not exist (need to run \"make\"?)" >&2
    exit 1
fi
if test ! -e "$sim_srcdir"/README.rst; then
    echo "$sim_srcdir/README.rst does not exist" >&2
    exit 1
fi

# Put built tools early in $PATH.
PATH=$sim_builddir/ovsdb:$sim_builddir/vswitchd:$sim_builddir/utilities:$PATH
PATH=$sim_builddir/ovn/controller:$sim_builddir/ovn/northd:$sim_builddir/ovn/utilities:$PATH
export PATH

rm -rf sandbox
mkdir sandbox
cd sandbox
sim_base=`pwd`; export sim_base

trap_signals() {
    for signal in 0 1 2 3 13 14 15; do
        trap "
            set +e
            cd '$sim_base' && (kill \`cat */*.pid\`) >/dev/null 2>&1
            trap - $signal
            kill -$signal $$" $signal
    done
}
export -f trap_signals
trap_signals

sim_setvars() {
    sandbox=$1
    OVS_RUNDIR=$sim_base/$1; export OVS_RUNDIR
    OVS_LOGDIR=$sim_base/$1; export OVS_LOGDIR
    OVS_DBDIR=$sim_base/$1; export OVS_DBDIR
    OVS_SYSCONFDIR=$sim_base/$1; export OVS_SYSCONFDIR
    PS1="|$1: $sim_PS1"
}
export -f sim_setvars

ovs-vsctl () { command ovs-vsctl -vsyslog:off "$@"; }; export -f ovs-vsctl
ovs-nbctl () { command ovs-nbctl -vsyslog:off "$@"; }; export -f ovs-nbctl
ovs-sbctl () { command ovs-sbctl -vsyslog:off "$@"; }; export -f ovs-sbctl
vtep-ctl () { command vtep-ctl -vsyslog:off "$@"; }; export -f vtep-ctl

as() {
    case $# in
        0)
            echo >&2 "$FUNCNAME: missing arguments (use --help for help)"
            return 1
            ;;
        1)
            if test "$1" != --help; then
                sim_setvars $1
            else
                cat <<EOF
$FUNCNAME: set the default sandbox for Open vSwitch commands
usage: $FUNCNAME SANDBOX [COMMAND ARG...]
where SANDBOX is the name of the desired sandbox.

With COMMAND arguments, this command sets the default target for that
single command, which it runs directly.  Otherwise, it sets the default
target for all following commands.
EOF
            fi
            ;;
        *)
            (sim_setvars $1; shift; "$@")
            ;;
    esac
}
export -f as

sim_add() {
    if test "$1" == --help; then
        cat <<EOF
$FUNCNAME: create a new sandboxed Open vSwitch instance
usage: $FUNCNAME SANDBOX

where SANDBOX is the name of the new sandbox, which will be created in
a directory named $sim_base/SANDBOX.
Afterward, use "as SANDBOX" to execute OVS commands in the sandbox's
context.
EOF
        return 0
    fi
    if test $# != 1; then
        echo >&2 "$FUNCNAME: missing argument (use --help for help)"
        return 1
    fi

    set X $1; shift
    if test $# != 1; then
        echo >&2 "$FUNCNAME: sandbox name must be a single word"
        return 1
    fi

    if test -e "$sim_base/$1"; then
        echo >&2 "$1 already exists"
        return 1
    fi

    # Create sandbox.
    mkdir "$sim_base"/$1 || return 1

    daemon_opts="--detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file"

    # Create database and start ovsdb-server.
    touch $sim_base/$1/.conf.db.~lock~
    as $1 ovsdb-tool create $sim_base/$1/conf.db "$sim_srcdir/vswitchd/vswitch.ovsschema"
    as $1 ovsdb-server --remote=punix:"$sim_base"/$1/db.sock $daemon_opts

    # Initialize database.
    as $1 ovs-vsctl --no-wait -- init

    # Start ovs-vswitchd.
    as $1 ovs-vswitchd --enable-dummy=system -vvconn -vnetdev_dummy $daemon_opts
}
export -f sim_add

net_add() {
    if test "$1" == --help; then
        cat <<EOF
$FUNCNAME: create a new interconnection network
usage: $FUNCNAME NETWORK

where NETWORK is the name of the new network.  Interconnection networks
are used with net_attach and ovn_attach.
EOF
        return 0
    fi
    if test $# != 1; then
        echo >&2 "$FUNCNAME: missing argument (use --help for help)"
        return 1
    fi

    as main ovs-vsctl add-br "$1"
}
export -f net_add

net_attach() {
    if test "$1" == --help; then
        cat <<EOF
$FUNCNAME: attach the default sandbox to an interconnection network
usage: $FUNCNAME NETWORK BRIDGE

Adds a port to BRIDGE within the default sandbox that connects BRIDGE
to the interconnection network NETWORK.  (Use "as" to set the default
sandbox.)
EOF
        return 0
    fi
    if test $# != 2; then
        echo >&2 "$FUNCNAME: wrong number of arguments (use --help for help)"
        return 1
    fi
    if test $sandbox = main; then
        echo >&2 "$FUNCNAME: can only attach interconnection networks to sandboxes other than main"
        return 1
    fi

    local net=$1 bridge=$2

    port=${sandbox}_$bridge
    as main ovs-vsctl \
        -- add-port $net "$port" \
        -- 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

    ovs-vsctl \
        -- set Interface $bridge options:tx_pcap="$sim_base/$sandbox/$bridge-tx.pcap" options:rxq_pcap="$sim_base/$sandbox/$bridge-rx.pcap" \
        -- add-port $bridge ${bridge}_$net \
        -- 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
}
export -f net_attach

ovn_start_db() {
    local db=$1 model=$2 servers=$3 schema=$4
    local DB=$(echo $db | tr a-z A-Z)
    local schema_name=$(ovsdb-tool schema-name $schema)

    case $model in
	standalone | backup) ;;
	clustered)
	    case $servers in
		[1-9] | [1-9][0-9]) ;;
		*) echo "${db}db servers must be between 1 and 99" >&2
		   exit 1
		   ;;
	    esac
	    ;;
	*)
	    echo "unknown ${db}db model \"$model\"" >&2
	    exit 1
	    ;;
    esac

    ovn_start_ovsdb_server() {
	local i=$1; shift
	as ${db}$i ovsdb-server --detach --no-chdir --pidfile=$db.pid \
	   -vsyslog:off -vconsole:off --log-file="$sim_base"/$db$i/$db.log \
	   --remote=db:$schema_name,${DB}_Global,connections \
	   --private-key=db:$schema_name,SSL,private_key \
	   --certificate=db:$schema_name,SSL,certificate \
	   --ca-cert=db:$schema_name,SSL,ca_cert \
	   --ssl-protocols=db:$schema_name,SSL,ssl_protocols \
	   --ssl-ciphers=db:$schema_name,SSL,ssl_ciphers \
	   --unixctl=${db} --remote=punix:$db.ovsdb \
	   "$sim_base"/$db$i/$db.db "$@"
    }

    ovn_prep_db() {
	local i=$1
	mkdir "$sim_base"/${db}$i
	touch "$sim_base"/${db}$i/.$db.db.~lock~
    }

    local n_remotes=1
    case $model in
	standalone)
	    ovn_prep_db 1
	    ovsdb-tool create "$sim_base"/${db}1/$db.db "$schema"
	    ovn_start_ovsdb_server 1
	    ;;
	backup)
	    for i in 1 2; do
		ovn_prep_db $i
		ovsdb-tool create "$sim_base"/$db$i/$db.db "$schema"
	    done
	    ovn_start_ovsdb_server 1
	    ovn_start_ovsdb_server 2 --sync-from=unix:"$sim_base"/${db}1/$db.ovsdb
	    cat <<EOF
The backup server of OVN $DB can be accessed by:
* ovn-${db}ctl --db=unix:$sim_base/${db}2/$db.ovsdb
* ovs-appctl -t $sim_base/${db}2/${db}
The backup database file is $sim_base/${db}2/$db.db
EOF
	    ;;
	clustered)
	    n_remotes=$servers
	    for i in $(seq $servers); do
		ovn_prep_db $i
		if test $i = 1; then
		    ovsdb-tool create-cluster "$sim_base"/$db$i/$db.db "$schema" unix:"$sim_base"/$db$i/db.raft
		else
		    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
		fi
		ovn_start_ovsdb_server $i
	    done
	    for i in $(seq $servers); do
		ovsdb-client wait unix:"$sim_base"/${db}$i/$db.ovsdb $schema_name connected
	    done
	    ;;
    esac

    remote=unix:"$sim_base"/${db}1/$db.ovsdb
    for i in `seq 2 $n_remotes`; do
	remote=$remote,unix:"$sim_base"/${db}$i/$db.ovsdb
    done
    eval OVN_${DB}_DB=\$remote
    eval export OVN_${DB}_DB
}
export -f ovn_start_db

ovn_start() {
    local nbdb_model=standalone
    local nbdb_servers=3
    local sbdb_model=standalone
    local sbdb_servers=3
    local prev=
    for option; do
	# This option-parsing mechanism borrowed from a Autoconf-generated
	# configure script under the following license:

	# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
	# 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
	# This configure script is free software; the Free Software Foundation
	# gives unlimited permission to copy, distribute and modify it.

	# If the previous option needs an argument, assign it.
	if test -n "$prev"; then
            eval $prev=\$option
            prev=
            continue
	fi
	case $option in
            *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
            *) optarg=yes ;;
	esac

	case $dashdash$option in
            --)
		dashdash=yes ;;
            -h|--help)
		cat <<EOF
$FUNCNAME: start OVN central databases and daemons
usage: $FUNCNAME [OPTION...]

This creates and initializes the central OVN databases (northbound and
southbound), starts their ovsdb-server daemons, and starts the ovn-northd
daemon.

Options:
  --nbdb-model=standalone|backup|clustered    northbound database model
  --nbdb-servers=N     number of servers in nbdb cluster (default: 3)
  --sbdb-model=standalone|backup|clustered    southbound database model
  --sbdb-servers=N     number of servers in sbdb cluster (default: 3)
  -h, --help           Print this usage message.
EOF
		return
		;;

            --nbdb-s*=*)
		nbdb_servers=$optarg
		nbdb_model=clustered
		;;
            --nbdb-s*)
		prev=nbdb_servers
		nbdb_model=clustered
		;;
            --nbdb-m*=*)
		nbdb_model=$optarg
		;;
            --nbdb-m*)
		prev=nbdb_model
		;;
            --sbdb-s*=*)
		sbdb_servers=$optarg
		sbdb_model=clustered
		;;
            --sbdb-s*)
		prev=sbdb_servers
		sbdb_model=clustered
		;;
            --sbdb-m*=*)
		sbdb_model=$optarg
		;;
            --sbdb-m*)
		prev=sbdb_model
		;;
            -*)
		echo "unrecognized option $option (use --help for help)" >&2
		return 1
		;;
            *)
		echo "$option: non-option arguments not supported (use --help for help)" >&2
		return 1
		;;
	esac
	shift
    done

    if test -d ovn-sb || test -d ovn-nb; then
        echo >&2 "OVN already started"
        return 1
    fi

    ovn_start_db nb "$nbdb_model" "$nbdb_servers" "$sim_srcdir"/ovn/ovn-nb.ovsschema
    ovn_start_db sb "$sbdb_model" "$sbdb_servers" "$sim_srcdir"/ovn/ovn-sb.ovsschema

    ovn-nbctl init
    ovn-sbctl init

    mkdir "$sim_base"/northd
    as northd ovn-northd --ovnnb-db="$OVN_NB_DB" --ovnsb-db="$OVN_SB_DB" \
       $daemon_opts
}
export -f ovn_start

ovn_attach() {
    if test "$1" == --help; then
        cat <<EOF
$FUNCNAME: attach default sandbox to an interconnection network for OVN
usage: $FUNCNAME NETWORK BRIDGE IP [MASKLEN]

This starts by doing everything that net_attach does.  Then it configures the
specified IP and MASKLEN (e.g. 192.168.0.1 and 24) on BRIDGE and starts
and configures ovn-controller.

MASKLEN defaults to 24 if it is not specified.
EOF
        return 0
    fi
    if test $# != 3 && test $# != 4; then
        echo >&2 "$FUNCNAME: wrong number of arguments (use --help for help)"
        return 1
    fi

    local net=$1 bridge=$2 ip=$3 masklen=${4-24}
    net_attach $net $bridge || return $?

    ovs-appctl netdev-dummy/ip4addr $bridge $ip/$masklen >/dev/null
    ovs-appctl ovs/route/add $ip/$masklen $bridge > /dev/null
    ovs-vsctl \
        -- set Open_vSwitch . external-ids:system-id=$sandbox \
        -- set Open_vSwitch . external-ids:ovn-remote=$OVN_SB_DB \
        -- set Open_vSwitch . external-ids:ovn-encap-type=geneve \
        -- set Open_vSwitch . external-ids:ovn-encap-ip=$ip\
        -- add-br br-int \
        -- set bridge br-int fail-mode=secure other-config:disable-in-band=true
    ovn-controller --detach --no-chdir --pidfile -vconsole:off -vsyslog:off --log-file
}
export -f ovn_attach

# Easy access to OVS manpages.
mkdir $sim_base/man
mandir=`cd $sim_base/man && pwd`
(cd "$sim_builddir" && ${MAKE-make} install-man install-man-rst mandir=$mandir >/dev/null)
MANPATH=$mandir:; export MANPATH

export scripts
export interactive
rc='
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi

    trap_signals
    sim_PS1=$PS1
    sim_add main
    as main

    for script in $scripts; do
        . $script || exit $?
    done

    $interactive || exit 0

    cat <<EOF
 ______________________________________________________________________
|
| You are running in a nested shell environment meant for Open vSwitch
| and OVN testing in simulation.   The OVS manpages are available via
| "man".  Please see ovs-sim(1) for more information.
|
| Exit the shell to kill the running daemons and leave the simulation
| environment.
EOF
'

set +e
status=0; bash --rcfile <(echo "$rc") || status=$?

if $interactive; then
    cat <<EOF
|______________________________________________________________________

EOF
fi

exit $status