2 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 */*) dir0
=`echo "$0" | sed 's,/[^/]*$,,'` ;;
20 .
"$dir0/ovs-lib" ||
exit 1
22 for dir
in "$sbindir" "$bindir" /sbin
/bin
/usr
/sbin
/usr
/bin
; do
33 # Keep track of removed vports so we can reload them if needed
37 # Try loading openvswitch again.
38 action
"Inserting openvswitch module" modprobe openvswitch
40 for vport
in $removed_vports; do
41 # Don't treat failures to load vports as fatal error
42 action
"Inserting $vport module" modprobe
$vport || true
46 insert_mod_if_required
() {
47 # If this kernel has no module support, expect we're done.
48 if test ! -e /proc
/modules
50 log_success_msg
"Kernel has no loadable module support. Skipping modprobe"
54 # If openvswitch is already loaded then we're done.
55 test -e /sys
/module
/openvswitch
&& return 0
57 # Load openvswitch. If that's successful then we're done.
58 insert_mods
&& return 0
60 # If the bridge module is loaded, then that might be blocking
61 # openvswitch. Try to unload it, if there are no bridges.
62 test -e /sys
/module
/bridge ||
return 1
63 bridges
=`echo /sys/class/net/*/bridge | sed 's,/sys/class/net/,,g;s,/bridge,,g'`
64 if test "$bridges" != "*"; then
65 log_warning_msg
"not removing bridge module because bridges exist ($bridges)"
68 action
"removing bridge module" rmmod bridge ||
return 1
70 # Try loading openvswitch again.
75 ovs-vsctl
--no-wait "$@"
79 set ovs_vsctl
set Open_vSwitch .
81 OVS_VERSION
=`ovs-vswitchd --version | sed 's/.*) //;1q'`
82 set "$@" ovs-version
="$OVS_VERSION"
86 id_file
=$etcdir/system-id.conf
87 uuid_file
=$etcdir/install_uuid.conf
88 if test -e "$id_file"; then
89 SYSTEM_ID
=`cat "$id_file"`
90 elif test -e "$uuid_file"; then
91 # Migrate from old file name.
93 SYSTEM_ID
=$INSTALLATION_UUID
94 echo "$SYSTEM_ID" > "$id_file"
95 elif SYSTEM_ID
=`uuidgen`; then
96 echo "$SYSTEM_ID" > "$id_file"
98 log_failure_msg
"missing uuidgen, could not generate system ID"
103 log_failure_msg
"system ID not configured, please use --system-id"
109 set "$@" external-ids
:system-id
="\"$SYSTEM_ID\""
111 set "$@" external-ids
:hostname
="\"$(hostname -f)\""
113 set "$@" external-ids
:rundir
="\"$rundir\""
115 if test X
"$SYSTEM_TYPE" != X
; then
116 set "$@" system-type
="\"$SYSTEM_TYPE\""
118 log_failure_msg
"no default system type, please use --system-type"
121 if test X
"$SYSTEM_VERSION" != X
; then
122 set "$@" system-version
="\"$SYSTEM_VERSION\""
124 log_failure_msg
"no default system version, please use --system-version"
127 action
"Configuring Open vSwitch system IDs" "$@" $extra_ids
130 check_force_cores
() {
131 if test X
"$FORCE_COREFILES" = Xyes
; then
136 del_transient_ports
() {
137 for port
in `ovs-vsctl --bare -- --columns=name find port other_config:transient=true`; do
138 ovs_vsctl
-- del-port
"$port"
145 if daemon_is_running ovsdb-server
; then
146 log_success_msg
"ovsdb-server is already running"
148 # Create initial database or upgrade database schema.
149 upgrade_db
$DB_FILE $DB_SCHEMA ||
return 1
151 # Start ovsdb-server.
152 set ovsdb-server
"$DB_FILE"
153 for db
in $EXTRA_DBS; do
159 if test ! -f "$db"; then
160 log_warning_msg
"$db (from \$EXTRA_DBS) does not exist."
161 elif ovsdb-tool db-version
"$db" >/dev
/null
; then
164 log_warning_msg
"$db (from \$EXTRA_DBS) cannot be read as a database (see error message above)"
167 if test X
"$SELF_CONFINEMENT" = Xno
; then
168 set "$@" --no-self-confinement
170 set "$@" -vconsole:emer
-vsyslog:err
-vfile:info
171 set "$@" --remote=punix
:"$DB_SOCK"
172 set "$@" --private-key=db
:Open_vSwitch
,SSL
,private_key
173 set "$@" --certificate=db
:Open_vSwitch
,SSL
,certificate
174 set "$@" --bootstrap-ca-cert=db
:Open_vSwitch
,SSL
,ca_cert
175 [ "$OVS_USER" != "" ] && set "$@" --user "$OVS_USER"
177 start_daemon
"$OVSDB_SERVER_PRIORITY" "$OVSDB_SERVER_WRAPPER" "$@" \
180 # Initialize database settings.
181 ovs_vsctl
-- init
-- set Open_vSwitch . db-version
="$schemaver" \
183 set_system_ids ||
return 1
184 if test X
"$DELETE_BRIDGES" = Xyes
; then
185 for bridge
in `ovs_vsctl list-br`; do
186 ovs_vsctl del-br
$bridge
189 if test X
"$DELETE_TRANSIENT_PORTS" = Xyes
; then
196 if test X
"$OVSDB_SERVER" = Xyes
; then
197 do_start_ovsdb ||
return 1
203 # Now that ovs-vswitchd has started and completed its initial
204 # configuration, tell ovsdb-server to connect to the remote managers. We
205 # used to do this at ovsdb-server startup time, but waiting for
206 # ovs-vswitchd to finish configuring means that remote managers see less
207 # churn in the database at startup or restart. (For example, managers
208 # won't briefly see empty datapath-id or ofport columns for records that
210 if test X
"$OVSDB_SERVER" = Xyes ||
test X
"$OVS_VSWITCHD" = Xyes
; then
211 if daemon_is_running ovsdb-server \
212 && daemon_is_running ovs-vswitchd
; then
213 action
"Enabling remote OVSDB managers" \
214 ovs-appctl
-t ovsdb-server ovsdb-server
/add-remote \
215 db
:Open_vSwitch
,Open_vSwitch
,manager_options
220 do_start_forwarding
() {
223 insert_mod_if_required ||
return 1
225 if daemon_is_running ovs-vswitchd
; then
226 log_success_msg
"ovs-vswitchd is already running"
228 # Increase the limit on the number of open file descriptors.
229 # On Linux, ovs-vswitchd needs about three file descriptors
230 # per bridge and "n-handler-threads" file descriptors per bridge
231 # port, so this allows a very large number of bridges and ports.
233 if [ $
(ulimit -n) -lt $MAXFD ]; then
237 # Start ovs-vswitchd.
238 set ovs-vswitchd unix
:"$DB_SOCK"
239 set "$@" -vconsole:emer
-vsyslog:err
-vfile:info
240 if test X
"$MLOCKALL" != Xno
; then
243 if test X
"$SELF_CONFINEMENT" = Xno
; then
244 set "$@" --no-self-confinement
246 [ "$OVS_USER" != "" ] && set "$@" --user "$OVS_USER"
248 start_daemon
"$OVS_VSWITCHD_PRIORITY" "$OVS_VSWITCHD_WRAPPER" "$@" ||
253 start_forwarding
() {
254 if test X
"$OVS_VSWITCHD" = Xyes
; then
255 do_start_forwarding ||
return 1
265 if test X
"$OVSDB_SERVER" = Xyes
; then
266 stop_daemon ovsdb-server
271 if test X
"$OVS_VSWITCHD" = Xyes
; then
272 stop_daemon ovs-vswitchd
276 ## ----------------- ##
277 ## force-reload-kmod ##
278 ## ----------------- ##
280 internal_interfaces
() {
281 # Outputs a list of internal interfaces:
283 # - There is an internal interface for every bridge, whether it
284 # has an Interface record or not and whether the Interface
285 # record's 'type' is properly set or not.
287 # - There is an internal interface for each Interface record whose
288 # 'type' is 'internal'.
290 # But ignore interfaces that don't really exist.
291 for d
in `(ovs_vsctl --bare \
292 -- --columns=name find Interface type=internal \
293 -- list-br) | sort -u`
295 if test -e "/sys/class/net/$d"; then
302 bridges
=`ovs_vsctl -- --real list-br`
303 if [ -n "${bridges}" ] && \
304 "$datadir/scripts/ovs-save" "$1" ${bridges} > "$2"; then
308 [ -z "${bridges}" ] && return 0
311 save_flows_if_required
() {
312 if test X
"$DELETE_BRIDGES" != Xyes
; then
313 action
"Saving flows" ovs_save save-flows
"${script_flows}"
318 "$datadir/scripts/ovs-save" save-interfaces
${ifaces} \
319 > "${script_interfaces}"
322 flow_restore_wait
() {
323 if test X
"$OVS_VSWITCHD" = Xyes
; then
324 ovs_vsctl
set open_vswitch . other_config
:flow-restore-wait
="true"
328 flow_restore_complete
() {
329 if test X
"$OVS_VSWITCHD" = Xyes
; then
330 ovs_vsctl
--if-exists remove open_vswitch . other_config \
331 flow-restore-wait
="true"
336 [ -x "${script_flows}" ] && \
337 action
"Restoring saved flows" "${script_flows}"
340 restore_interfaces
() {
341 [ ! -x "${script_interfaces}" ] && return 0
342 action
"Restoring interface configuration" "${script_interfaces}"
344 if test $rc = 0; then
349 log
="logger -p daemon.$level -t ovs-save"
350 $log "interface restore script exited with status $rc:"
351 $log -f "$script_interfaces"
354 init_restore_scripts
() {
355 script_interfaces
=`mktemp`
356 script_flows
=`mktemp`
357 trap 'rm -f "${script_interfaces}" "${script_flows}"' 0
360 force_reload_kmod
() {
362 if test X
"$OVS_VSWITCHD" != Xyes
; then
363 log_failure_msg
"Reloading of kmod without ovs-vswitchd is an error"
367 ifaces
=`internal_interfaces`
368 action
"Detected internal interfaces: $ifaces" true
371 save_flows_if_required
373 # Restart the database first, since a large database may take a
374 # while to load, and we want to minimize forwarding disruption.
376 start_ovsdb ||
return 1
380 if action
"Saving interface configuration" save_interfaces
; then
383 log_warning_msg
"Failed to save configuration, not replacing kernel module"
388 chmod +x
"$script_interfaces"
390 for dp
in `ovs-dpctl dump-dps`; do
391 action
"Removing datapath: $dp" ovs-dpctl del-dp
"$dp"
394 for vport
in `awk '/^vport_/ { print $1 }' /proc/modules`; do
395 action
"Removing $vport module" rmmod
$vport
396 if ! grep -q $vport /proc
/modules
; then
397 removed_vports
="$removed_vports $vport"
401 if test -e /sys
/module
/openvswitch
; then
402 action
"Removing openvswitch module" rmmod openvswitch
405 # Start vswitchd by asking it to wait till flow restore is finished.
407 start_forwarding ||
return 1
409 # Restore saved flows and inform vswitchd that we are done.
411 flow_restore_complete
416 "$datadir/scripts/ovs-check-dead-ifs"
424 if daemon_is_running ovsdb-server
&& daemon_is_running ovs-vswitchd
; then
426 if test X
"$OVS_VSWITCHD" = Xyes
; then
427 save_flows_if_required
431 # Restart the database first, since a large database may take a
432 # while to load, and we want to minimize forwarding disruption.
434 start_ovsdb ||
return 1
438 # Start vswitchd by asking it to wait till flow restore is finished.
440 start_forwarding ||
return 1
442 # Restore saved flows and inform vswitchd that we are done.
444 flow_restore_complete
448 ## --------------- ##
449 ## enable-protocol ##
450 ## --------------- ##
453 # Translate the protocol name to a number, because "iptables -n -L" prints
454 # some protocols by name (despite the -n) and therefore we need to look for
457 # (iptables -S output is more uniform but old iptables doesn't have it.)
458 protonum
=`grep "^$PROTOCOL[ ]" /etc/protocols | awk '{print $2}'`
459 if expr X
"$protonum" : X
'[0-9]\{1,\}$' > /dev
/null
; then :; else
460 log_failure_msg
"unknown protocol $PROTOCOL"
465 match
="(\$2 == \"$PROTOCOL\" || \$2 == $protonum)"
466 insert
="iptables -I INPUT -p $PROTOCOL"
467 if test X
"$DPORT" != X
; then
468 name
="$name to port $DPORT"
469 match
="$match && /dpt:$DPORT/"
470 insert
="$insert --dport $DPORT"
472 if test X
"$SPORT" != X
; then
473 name
="$name from port $SPORT"
474 match
="$match && /spt:$SPORT/"
475 insert
="$insert --sport $SPORT"
477 insert
="$insert -j ACCEPT"
479 if (iptables
-n -L INPUT
) >/dev
/null
2>&1; then
480 if iptables
-n -L INPUT |
awk "$match { n++ } END { exit n == 0 }"
482 # There's already a rule for this protocol. Don't override it.
483 log_success_msg
"iptables already has a rule for $name, not explicitly enabling"
485 action
"Enabling $name with iptables" $insert
487 elif (iptables
--version) >/dev
/null
2>&1; then
488 action
"cannot list iptables rules, not adding a rule for $name"
490 action
"iptables binary not installed, not adding a rule for $name"
502 DELETE_TRANSIENT_PORTS
=no
512 OVSDB_SERVER_PRIORITY
=-10
513 OVS_VSWITCHD_PRIORITY
=-10
514 OVSDB_SERVER_WRAPPER
=
515 OVS_VSWITCHD_WRAPPER
=
517 DB_FILE
=$dbdir/conf.db
518 DB_SOCK
=$rundir/db.sock
519 DB_SCHEMA
=$datadir/vswitch.ovsschema
526 type_file
=$etcdir/system-type.conf
527 version_file
=$etcdir/system-version.conf
529 if test -e "$type_file" ; then
530 SYSTEM_TYPE
=`cat $type_file`
531 SYSTEM_VERSION
=`cat $version_file`
532 elif test -e "@sysconfdir@/os-release"; then
533 SYSTEM_TYPE
=`. '@sysconfdir@/os-release' && echo "$ID"`
534 SYSTEM_VERSION
=`. '@sysconfdir@/os-release' && echo "$VERSION_ID"`
535 elif (lsb_release
--id) >/dev
/null
2>&1; then
536 SYSTEM_TYPE
=`lsb_release --id -s`
537 system_release
=`lsb_release --release -s`
538 system_codename
=`lsb_release --codename -s`
539 SYSTEM_VERSION
="${system_release}-${system_codename}"
542 SYSTEM_VERSION
=unknown
549 $0: controls Open vSwitch daemons
550 usage: $0 [OPTIONS] COMMAND
552 This program is intended to be invoked internally by Open vSwitch startup
553 scripts. System administrators should not normally invoke it directly.
556 start start Open vSwitch daemons
557 stop stop Open vSwitch daemons
558 restart stop and start Open vSwitch daemons
559 status check whether Open vSwitch daemons are running
560 version print versions of Open vSwitch daemons
561 load-kmod insert modules if not already present
562 force-reload-kmod save OVS network device state, stop OVS, unload kernel
563 module, reload kernel module, start OVS, restore state
564 enable-protocol enable protocol specified in options with iptables
565 help display this help message
567 One of the following options is required for "start", "restart" and "force-reload-kmod":
568 --system-id=UUID set specific ID to uniquely identify this system
569 --system-id=random use a random but persistent UUID to identify this system
571 Other important options for "start", "restart" and "force-reload-kmod":
572 --system-type=TYPE set system type (e.g. "XenServer")
573 --system-version=VERSION set system version (e.g. "5.6.100-39265p")
574 --external-id="key=value"
575 add given key-value pair to Open_vSwitch external-ids
576 --delete-bridges delete all bridges just before starting ovs-vswitchd
577 --ovs-user="user[:group]" pass the --user flag to ovs daemons
579 Less important options for "start", "restart" and "force-reload-kmod":
580 --daemon-cwd=DIR set working dir for OVS daemons (default: $DAEMON_CWD)
581 --no-force-corefiles do not force on core dumps for OVS daemons
582 --no-mlockall do not lock all of ovs-vswitchd into memory
583 --ovsdb-server-priority=NICE set ovsdb-server's niceness (default: $OVSDB_SERVER_PRIORITY)
584 --ovs-vswitchd-priority=NICE set ovs-vswitchd's niceness (default: $OVS_VSWITCHD_PRIORITY)
586 Debugging options for "start", "restart" and "force-reload-kmod":
587 --ovsdb-server-wrapper=WRAPPER
588 --ovs-vswitchd-wrapper=WRAPPER
589 run specified daemon under WRAPPER (either 'valgrind' or 'strace')
591 File location options:
592 --db-file=FILE database file name (default: $DB_FILE)
593 --db-sock=SOCKET JSON-RPC socket name (default: $DB_SOCK)
594 --db-schema=FILE database schema file name (default: $DB_SCHEMA)
596 Options for "enable-protocol":
597 --protocol=PROTOCOL protocol to enable with iptables (default: gre)
598 --sport=PORT source port to match (for tcp or udp protocol)
599 --dport=PORT ddestination port to match (for tcp or udp protocol)
602 -h, --help display this help message
603 -V, --version display version information
605 Default directories with "configure" option and environment variable override:
606 logs: @LOGDIR@ (--with-logdir, OVS_LOGDIR)
607 pidfiles and sockets: @RUNDIR@ (--with-rundir, OVS_RUNDIR)
608 conf.db: @DBDIR@ (--with-dbdir, OVS_DBDIR)
609 system configuration: @sysconfdir@ (--sysconfdir, OVS_SYSCONFDIR)
610 data files: @pkgdatadir@ (--pkgdatadir, OVS_PKGDATADIR)
611 user binaries: @bindir@ (--bindir, OVS_BINDIR)
612 system binaries: @sbindir@ (--sbindir, OVS_SBINDIR)
614 Please report bugs to bugs@openvswitch.org (see REPORTING-BUGS for details).
621 var
=`echo "$option" | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
622 eval set=\
${$var+yes}
623 eval old_value
=\$
$var
624 if test X
$set = X || \
625 (test $type = bool
&& \
626 test X
"$old_value" != Xno
&& test X
"$old_value" != Xyes
); then
627 echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
634 echo ovsdb-server ovs-vswitchd
647 echo "$0 (Open vSwitch) $VERSION"
651 value
=`expr X"$arg" : 'X[^=]*=\(.*\)'`
654 extra_ids
="$extra_ids external-ids:$value"
657 echo >&2 "$0: --external-id argument not in the form \"key=value\""
663 option
=`expr X"$arg" : 'X--\([^=]*\)'`
664 value
=`expr X"$arg" : 'X[^=]*=\(.*\)'`
669 option
=`expr X"$arg" : 'X--no-\(.*\)'`
675 option
=`expr X"$arg" : 'X--\(.*\)'`
681 echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
685 if test X
"$command" = X
; then
688 echo >&2 "$0: exactly one non-option argument required (use --help for help)"
696 start_ovsdb ||
exit 1
697 start_forwarding ||
exit 1
709 for daemon
in `daemons`; do
710 daemon_status
$daemon || rc
=$?
715 for daemon
in `daemons`; do
723 insert_mod_if_required
732 echo >&2 "$0: missing command name (use --help for help)"
736 echo >&2 "$0: unknown command \"$command\" (use --help for help)"