3 # This is a shell function library sourced by some Open vSwitch scripts.
4 # It is not intended to be invoked on its own.
6 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at:
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
20 ## ----------------- ##
21 ## configure options ##
22 ## ----------------- ##
24 # All of these should be substituted by the Makefile at build time.
25 logdir=${OVS_LOGDIR-'@LOGDIR@'} # /var/log/openvswitch
26 rundir=${OVS_RUNDIR-'@RUNDIR@'} # /var/run/openvswitch
27 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'} # /etc
28 etcdir=$sysconfdir/openvswitch # /etc/openvswitch
29 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'} # /usr/share/openvswitch
30 bindir=${OVS_BINDIR-'@bindir@'} # /usr/bin
31 sbindir=${OVS_SBINDIR-'@sbindir@'} # /usr/sbin
33 # /etc/openvswitch or /var/lib/openvswitch
34 if test X"$OVS_DBDIR" != X; then
36 elif test X"$OVS_SYSCONFDIR" != X; then
37 dbdir=$OVS_SYSCONFDIR/openvswitch
43 echo "$@" >> "${logdir}/ovs-ctl.log"
49 # In case of running the daemon with strace, piping the o/p causes
50 # the script to block (strace probably does not close the inherited
51 # pipe). So, do not log the o/p to ovs-ctl.log.
52 "${datadir}/scripts/ovs-ctl" "$@"
55 # In case of the command 'status', we should return the exit status
56 # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
57 display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
59 if test -w "${logdir}/ovs-ctl.log"; then
60 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
67 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
68 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
77 LC_ALL=C; export LC_ALL
83 # Use the system's own implementations if it has any.
84 if test -e /etc/init.d/functions; then
85 . /etc/init.d/functions
86 elif test -e /etc/rc.d/init.d/functions; then
87 . /etc/rc.d/init.d/functions
88 elif test -e /lib/lsb/init-functions; then
89 . /lib/lsb/init-functions
92 # Implement missing functions (e.g. OpenSUSE lacks 'action').
93 if type log_success_msg >/dev/null 2>&1; then :; else
98 if type log_failure_msg >/dev/null 2>&1; then :; else
100 printf '%s ... failed!\n' "$*"
103 if type log_warning_msg >/dev/null 2>&1; then :; else
105 printf '%s ... (warning).\n' "$*"
108 if type action >/dev/null 2>&1; then :; else
114 if test $rc = 0; then
115 log_success_msg "$STRING"
117 log_failure_msg "$STRING"
128 # This is better than "kill -0" because it doesn't require permission to
129 # send a signal (so daemon_status in particular works as non-root).
134 [ "$1" = "`cat /proc/$2/comm`" ]
137 # version_geq version_a version_b
139 # Compare (dot separated) version numbers. Returns true (exit code 0) if
140 # version_a is greater or equal than version_b, otherwise false (exit code 1).
143 n1 = split($1, a, ".");
144 n2 = split($2, b, ".");
145 n = (n1 > n2) ? n1 : n2;
146 for (i = 1; i <= n; i++) {
147 if (a[i]+0 < b[i]+0) exit 1
148 if (a[i]+0 > b[i]+0) exit 0
155 INSTALL_MODE="${2:-755}"
158 [ "$OVS_USER" != "" ] && INSTALL_USER="${OVS_USER%:*}"
159 [ "${OVS_USER##*:}" != "" ] && INSTALL_GROUP="${OVS_USER##*:}"
161 if test ! -d "$DIR"; then
162 install -d -m "$INSTALL_MODE" -o "$INSTALL_USER" -g "$INSTALL_GROUP" "$DIR"
163 restorecon "$DIR" >/dev/null 2>&1
174 # drop core files in a sensible place
175 install_dir "$DAEMON_CWD"
180 install_dir "$logdir" "750"
181 set "$@" --log-file="$logdir/$daemon.log"
183 # pidfile and monitoring
184 install_dir "$rundir"
185 set "$@" --pidfile="$rundir/$daemon.pid"
187 test X"$MONITOR" = Xno || set "$@" --monitor
192 if (valgrind --version) > /dev/null 2>&1; then
193 set valgrind -q --leak-check=full --time-stamp=yes \
194 --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
196 log_failure_msg "valgrind not installed, running $daemon without it"
200 if (strace -V) > /dev/null 2>&1; then
201 strace="strace -tt -T -s 256 -ff"
202 if (strace -DV) > /dev/null 2>&1; then
204 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
208 log_failure_msg "strace not installed, running $daemon without it"
212 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
217 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
222 if test X"$priority" != X; then
223 set nice -n "$priority" "$@"
226 action "Starting $daemon" "$@" || return 1
228 if test X"$strace" != X; then
229 # Strace doesn't have the -D option so we attach after the fact.
230 setsid $strace -o "$logdir/$daemon.strace.log" \
231 -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
236 if test -e "$rundir/$1.pid"; then
237 if pid=`cat "$rundir/$1.pid"`; then
239 graceful="EXIT .1 .25 .65 1"
240 actions="TERM .1 .25 .65 1 1 1 1 \
241 KILL 1 1 1 2 10 15 30 \
243 version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
244 | awk 'NR==1{print $NF}'`
246 # Use `ovs-appctl exit` only if the running daemon version
247 # is >= 2.5.90. This script might be used during upgrade to
248 # stop older versions of daemons which do not behave correctly
249 # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
251 if version_geq "$version" "2.5.90"; then
252 actions="$graceful $actions"
254 for action in $actions; do
255 if pid_exists "$pid" >/dev/null 2>&1; then :; else
260 action "Exiting $1 ($pid)" \
261 ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit $2
264 action "Killing $1 ($pid)" kill $pid
267 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
270 log_failure_msg "Killing $1 ($pid) failed"
280 log_success_msg "$1 is not running"
284 pidfile=$rundir/$1.pid
285 if test -e "$pidfile"; then
286 if pid=`cat "$pidfile"`; then
287 if pid_exists "$pid"; then
288 echo "$1 is running with pid $pid"
291 echo "Pidfile for $1 ($pidfile) is stale"
294 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
297 echo "$1 is not running"
302 daemon_is_running () {
303 pidfile=$rundir/$1.pid
304 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
307 # Prints commands needed to move the ip address from interface $1 to interface
310 if [ -z "$1" ] || [ -z "$2" ]; then
316 # IP addresses (including IPv6).
317 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
318 ip addr show dev $dev | while read addr; do
321 # Check and trim family.
329 # Trim device off the end--"ip" insists on having "dev" precede it.
331 while test $# != 0; do
334 # XXX: According to 'man ip-address', "dynamic" is only
335 # used for ipv6 addresses. But, atleast on RHEL 7.4
336 # (iproute-3.10.0-87), it is being used for ipv4
337 # addresses assigned with dhcp.
338 if [ "$family" = "inet" ]; then
342 # Omit kernel-maintained route.
346 if test "$2" = link -a "$family" != inet6; then
347 # Omit route derived from IP address, e.g.
348 # 172.16.0.0/16 derived from 172.16.12.34,
349 # but preserve IPv6 link-local address.
354 # Address label string
355 label=`echo $1 | sed "s/$dev/$dst/"`
356 addrcmd="$addrcmd label $label"
361 addrcmd="$addrcmd $1"
364 if test "$1" != "$dev"; then
365 addrcmd="$addrcmd $1"
368 echo ip -f $family addr add $addrcmd dev $dst
372 # Prints commands needed to move the ip route of interface $1 to interface $2
374 if [ -z "$1" ] || [ -z "$2" ]; then
379 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
380 ip route show dev $dev | while read route; do
381 # "proto kernel" routes are installed by the kernel automatically.
383 *" proto kernel "*) continue ;;
386 echo "ip route add $route dev $dst"
391 if [ "$OVS_USER" != "" ]; then
392 local uid=$(id -u "${OVS_USER%:*}")
393 local gid=$(id -g "${OVS_USER%:*}")
394 local groups=$(id -G "${OVS_USER%:*}" | tr ' ' ',')
395 setpriv --reuid "$uid" --regid "$gid" --groups "$groups" ovsdb-tool -vconsole:off "$@"
397 ovsdb-tool -vconsole:off "$@"
404 action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
408 # Back up the old version.
409 version=`ovsdb_tool db-version "$DB_FILE"`
410 cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
411 backup=$DB_FILE.backup$version-$cksum
412 action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
419 schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
420 if test ! -e "$DB_FILE"; then
421 log_warning_msg "$DB_FILE does not exist"
422 install_dir `dirname $DB_FILE`
423 create_db "$DB_FILE" "$DB_SCHEMA"
424 elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" = Xyes; then
425 backup_db || return 1
427 # Compact database. This is important if the old schema did not enable
428 # garbage collection (i.e. if it did not have any tables with "isRoot":
429 # true) but the new schema does. In that situation the old database
430 # may contain a transaction that creates a record followed by a
431 # transaction that creates the first use of the record. Replaying that
432 # series of transactions against the new database schema (as "convert"
433 # does) would cause the record to be dropped by the first transaction,
434 # then the second transaction would cause a referential integrity
435 # failure (for a strong reference).
437 # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
438 # understand some feature of the schema used in the OVSDB version that
439 # we're downgrading from, so we don't give up on error.
440 action "Compacting database" ovsdb_tool compact "$DB_FILE"
442 # Upgrade or downgrade schema.
443 if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
446 log_warning_msg "Schema conversion failed, using empty database instead"
448 create_db "$DB_FILE" "$DB_SCHEMA"
454 local DB_SCHEMA=$1 DB_SERVER=$2
455 local schema_name=$(ovsdb-tool schema-name $1) || return 1
457 action "Waiting for $schema_name to come up" ovsdb-client -t 30 wait "$DB_SERVER" "$schema_name" connected || return $?
458 local db_version=$(ovsdb-client -t 10 get-schema-version "$DB_SERVER" "$schema_name") || return $?
459 local target_version=$(ovsdb-tool schema-version "$DB_SCHEMA") || return $?
461 if ovsdb-tool compare-versions "$db_version" == "$target_version"; then
463 elif ovsdb-tool compare-versions "$db_version" ">" "$target_version"; then
464 log_warning_msg "Database $schema_name has newer schema version ($db_version) than our local schema ($target_version), possibly an upgrade is partially complete?"
466 action "Upgrading database $schema_name from schema version $db_version to $target_version" ovsdb-client -t 30 convert "$DB_SERVER" "$DB_SCHEMA"
475 if test ! -e "$DB_FILE"; then
476 action "Creating cluster database $DB_FILE" ovsdb_tool create-cluster "$DB_FILE" "$DB_SCHEMA" "$LOCAL_ADDR"
477 elif ovsdb_tool db-is-standalone "$DB_FILE"; then
478 # Convert standalone database to clustered.
479 backup_db || return 1
480 action "Creating cluster database $DB_FILE from existing one" \
481 ovsdb_tool create-cluster "$DB_FILE" "$backup" "$LOCAL_ADDR"
491 if test ! -e "$DB_FILE"; then
492 ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR" "$REMOTE_ADDR"
493 elif ovsdb_tool db-is-standalone "$DB_FILE"; then
494 # Backup standalone database and join cluster.
495 backup_db || return 1
496 action "Joining $DB_FILE to cluster" \
497 ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR"
502 ovs-vsctl --no-wait "$@"
505 ## ----------------- ##
506 ## force-reload-kmod ##
507 ## ----------------- ##
510 "$dir0/ovs-kmod-ctl" "$@"
513 internal_interfaces () {
514 # Outputs a list of internal interfaces:
516 # - There is an internal interface for every bridge, whether it
517 # has an Interface record or not and whether the Interface
518 # record's 'type' is properly set or not.
520 # - There is an internal interface for each Interface record whose
521 # 'type' is 'internal'.
523 # But ignore interfaces that don't really exist.
524 for d in `(ovs_vsctl --bare \
525 -- --columns=name find Interface type=internal \
526 -- list-br) | sort -u`
528 if test -e "/sys/class/net/$d"; then
535 bridges=`ovs_vsctl -- --real list-br`
536 if [ -n "${bridges}" ] && \
537 "$datadir/scripts/ovs-save" "$1" ${bridges} > "$2"; then
541 [ -z "${bridges}" ] && return 0
544 save_flows_if_required () {
545 if test X"$DELETE_BRIDGES" != Xyes; then
546 action "Saving flows" ovs_save save-flows "${script_flows}"
551 "$datadir/scripts/ovs-save" save-interfaces ${ifaces} \
552 > "${script_interfaces}"
555 flow_restore_wait () {
556 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
557 ovs_vsctl set open_vswitch . other_config:flow-restore-wait="true"
561 flow_restore_complete () {
562 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
563 ovs_vsctl --if-exists remove open_vswitch . other_config \
564 flow-restore-wait="true"
569 [ -x "${script_flows}" ] && \
570 action "Restoring saved flows" "${script_flows}"
573 restore_interfaces () {
574 [ ! -x "${script_interfaces}" ] && return 0
575 action "Restoring interface configuration" "${script_interfaces}"
577 if test $rc = 0; then
582 log="logger -p daemon.$level -t ovs-save"
583 $log "interface restore script exited with status $rc:"
584 $log -f "$script_interfaces"
587 init_restore_scripts () {
588 script_interfaces=`mktemp`
589 script_flows=`mktemp`
590 trap 'rm -f "${script_interfaces}" "${script_flows}"' 0
593 force_reload_kmod () {
595 if test X"${OVS_VSWITCHD:-yes}" != Xyes; then
596 log_failure_msg "Reloading of kmod without ovs-vswitchd is an error"
600 ifaces=`internal_interfaces`
601 action "Detected internal interfaces: $ifaces" true
604 save_flows_if_required
606 # Restart the database first, since a large database may take a
607 # while to load, and we want to minimize forwarding disruption.
609 start_ovsdb || return 1
611 if [ -n "$(ovs-dpctl show)" ]; then
612 action "Flush old conntrack entries" ovs-appctl dpctl/flush-conntrack
616 if action "Saving interface configuration" save_interfaces; then
619 log_warning_msg "Failed to save configuration, not replacing kernel module"
624 chmod +x "$script_interfaces"
626 for dp in `ovs-dpctl dump-dps`; do
627 action "Removing datapath: $dp" ovs-dpctl del-dp "$dp"
632 # Start vswitchd by asking it to wait till flow restore is finished.
634 start_forwarding || return 1
636 # Restore saved flows and inform vswitchd that we are done.
638 flow_restore_complete
643 "$datadir/scripts/ovs-check-dead-ifs"
651 if daemon_is_running ovsdb-server && daemon_is_running ovs-vswitchd; then
653 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
654 save_flows_if_required
658 # Restart the database first, since a large database may take a
659 # while to load, and we want to minimize forwarding disruption.
661 start_ovsdb || return 1
665 # Start vswitchd by asking it to wait till flow restore is finished.
667 start_forwarding || return 1
669 # Restore saved flows and inform vswitchd that we are done.
671 flow_restore_complete