]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-lib.in
ovs-vsctl: Add datapath_type column to show command.
[mirror_ovs.git] / utilities / ovs-lib.in
1 # -*- sh -*-
2 # vi:syntax=sh
3 # This is a shell function library sourced by some Open vSwitch scripts.
4 # It is not intended to be invoked on its own.
5
6 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
7 #
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:
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
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.
19
20 ## ----------------- ##
21 ## configure options ##
22 ## ----------------- ##
23
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
32
33 # /etc/openvswitch or /var/lib/openvswitch
34 if test X"$OVS_DBDIR" != X; then
35 dbdir=$OVS_DBDIR
36 elif test X"$OVS_SYSCONFDIR" != X; then
37 dbdir=$OVS_SYSCONFDIR/openvswitch
38 else
39 dbdir='@DBDIR@'
40 fi
41
42 ovs_ctl_log () {
43 echo "$@" >> "${logdir}/ovs-ctl.log"
44 }
45
46 ovs_ctl () {
47 case "$@" in
48 *"=strace"*)
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" "$@"
53 ;;
54 "status")
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`
58 rc=$?
59 if test -w "${logdir}/ovs-ctl.log"; then
60 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
61 else
62 echo "${display}"
63 fi
64 return ${rc}
65 ;;
66 *)
67 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
68 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
69 ;;
70 esac
71 }
72
73 VERSION='@VERSION@'
74
75 DAEMON_CWD=/
76
77 LC_ALL=C; export LC_ALL
78
79 ## ------------- ##
80 ## LSB functions ##
81 ## ------------- ##
82
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
90 fi
91
92 # Implement missing functions (e.g. OpenSUSE lacks 'action').
93 if type log_success_msg >/dev/null 2>&1; then :; else
94 log_success_msg () {
95 printf '%s.\n' "$*"
96 }
97 fi
98 if type log_failure_msg >/dev/null 2>&1; then :; else
99 log_failure_msg () {
100 printf '%s ... failed!\n' "$*"
101 }
102 fi
103 if type log_warning_msg >/dev/null 2>&1; then :; else
104 log_warning_msg () {
105 printf '%s ... (warning).\n' "$*"
106 }
107 fi
108 if type action >/dev/null 2>&1; then :; else
109 action () {
110 STRING=$1
111 shift
112 "$@"
113 rc=$?
114 if test $rc = 0; then
115 log_success_msg "$STRING"
116 else
117 log_failure_msg "$STRING"
118 fi
119 return $rc
120 }
121 fi
122
123 ## ------- ##
124 ## Daemons ##
125 ## ------- ##
126
127 pid_exists () {
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).
130 test -d /proc/"$1"
131 }
132
133 pid_comm_check () {
134 [ "$1" = "`cat /proc/$2/comm`" ]
135 }
136
137 # version_geq version_a version_b
138 #
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).
141 version_geq() {
142 echo $1 $2 | awk '{
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
149 }
150 }'
151 }
152
153 install_dir () {
154 DIR="$1"
155 INSTALL_MODE="${2:-755}"
156 INSTALL_USER="root"
157 INSTALL_GROUP="root"
158 [ "$OVS_USER" != "" ] && INSTALL_USER="${OVS_USER%:*}"
159 [ "${OVS_USER##*:}" != "" ] && INSTALL_GROUP="${OVS_USER##*:}"
160
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
164 fi
165 }
166
167 start_daemon () {
168 priority=$1
169 wrapper=$2
170 shift; shift
171 daemon=$1
172 strace=""
173
174 # drop core files in a sensible place
175 install_dir "$DAEMON_CWD"
176 set "$@" --no-chdir
177 cd "$DAEMON_CWD"
178
179 # log file
180 install_dir "$logdir" "750"
181 set "$@" --log-file="$logdir/$daemon.log"
182
183 # pidfile and monitoring
184 install_dir "$rundir"
185 set "$@" --pidfile="$rundir/$daemon.pid"
186 set "$@" --detach
187 test X"$MONITOR" = Xno || set "$@" --monitor
188
189 # wrapper
190 case $wrapper in
191 valgrind)
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" "$@"
195 else
196 log_failure_msg "valgrind not installed, running $daemon without it"
197 fi
198 ;;
199 strace)
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
203 # Has the -D option.
204 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
205 strace=""
206 fi
207 else
208 log_failure_msg "strace not installed, running $daemon without it"
209 fi
210 ;;
211 glibc)
212 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
213 ;;
214 '')
215 ;;
216 *)
217 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
218 ;;
219 esac
220
221 # priority
222 if test X"$priority" != X; then
223 set nice -n "$priority" "$@"
224 fi
225
226 action "Starting $daemon" "$@" || return 1
227
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 &
232 fi
233 }
234
235 stop_daemon () {
236 if test -e "$rundir/$1.pid"; then
237 if pid=`cat "$rundir/$1.pid"`; then
238 if pid_exists "$pid" >/dev/null 2>&1; then :; else
239 rm -f $rundir/$1.$pid.ctl $rundir/$1.$pid
240 return 0
241 fi
242
243 graceful="EXIT .1 .25 .65 1"
244 actions="TERM .1 .25 .65 1 1 1 1 \
245 KILL 1 1 1 2 10 15 30 \
246 FAIL"
247 version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
248 | awk 'NR==1{print $NF}'`
249
250 # Use `ovs-appctl exit` only if the running daemon version
251 # is >= 2.5.90. This script might be used during upgrade to
252 # stop older versions of daemons which do not behave correctly
253 # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
254 # internal ports).
255 if version_geq "$version" "2.5.90"; then
256 actions="$graceful $actions"
257 fi
258 for action in $actions; do
259 if pid_exists "$pid" >/dev/null 2>&1; then :; else
260 return 0
261 fi
262 case $action in
263 EXIT)
264 action "Exiting $1 ($pid)" \
265 ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit $2
266 ;;
267 TERM)
268 action "Killing $1 ($pid)" kill $pid
269 ;;
270 KILL)
271 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
272 ;;
273 FAIL)
274 log_failure_msg "Killing $1 ($pid) failed"
275 return 1
276 ;;
277 *)
278 sleep $action
279 ;;
280 esac
281 done
282 fi
283 fi
284 log_success_msg "$1 is not running"
285 }
286
287 daemon_status () {
288 pidfile=$rundir/$1.pid
289 if test -e "$pidfile"; then
290 if pid=`cat "$pidfile"`; then
291 if pid_exists "$pid"; then
292 echo "$1 is running with pid $pid"
293 return 0
294 else
295 echo "Pidfile for $1 ($pidfile) is stale"
296 fi
297 else
298 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
299 fi
300 else
301 echo "$1 is not running"
302 fi
303 return 1
304 }
305
306 daemon_is_running () {
307 pidfile=$rundir/$1.pid
308 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
309 } >/dev/null 2>&1
310
311 # Prints commands needed to move the ip address from interface $1 to interface
312 # $2
313 move_ip_address () {
314 if [ -z "$1" ] || [ -z "$2" ]; then
315 return
316 fi
317 dev="$1"
318 dst="$2"
319
320 # IP addresses (including IPv6).
321 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
322 ip addr show dev $dev | while read addr; do
323 set -- $addr
324
325 # Check and trim family.
326 family=$1
327 shift
328 case $family in
329 inet | inet6) ;;
330 *) continue ;;
331 esac
332
333 # Trim device off the end--"ip" insists on having "dev" precede it.
334 addrcmd=
335 while test $# != 0; do
336 case $1 in
337 dynamic)
338 # XXX: According to 'man ip-address', "dynamic" is only
339 # used for ipv6 addresses. But, atleast on RHEL 7.4
340 # (iproute-3.10.0-87), it is being used for ipv4
341 # addresses assigned with dhcp.
342 if [ "$family" = "inet" ]; then
343 shift
344 continue
345 fi
346 # Omit kernel-maintained route.
347 continue 2
348 ;;
349 scope)
350 if test "$2" = link -a "$family" != inet6; then
351 # Omit route derived from IP address, e.g.
352 # 172.16.0.0/16 derived from 172.16.12.34,
353 # but preserve IPv6 link-local address.
354 continue 2
355 fi
356 ;;
357 "$dev"|"$dev:"*)
358 # Address label string
359 label=`echo $1 | sed "s/$dev/$dst/"`
360 addrcmd="$addrcmd label $label"
361 shift
362 continue
363 ;;
364 esac
365 addrcmd="$addrcmd $1"
366 shift
367 done
368 if test "$1" != "$dev"; then
369 addrcmd="$addrcmd $1"
370 fi
371
372 echo ip -f $family addr add $addrcmd dev $dst
373 done
374 }
375
376 # Prints commands needed to move the ip route of interface $1 to interface $2
377 move_ip_routes () {
378 if [ -z "$1" ] || [ -z "$2" ]; then
379 return
380 fi
381 dev="$1"
382 dst="$2"
383 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
384 ip route show dev $dev | while read route; do
385 # "proto kernel" routes are installed by the kernel automatically.
386 case $route in
387 *" proto kernel "*) continue ;;
388 esac
389
390 echo "ip route add $route dev $dst"
391 done
392 }
393
394 run_as_ovsuser() {
395 if [ "$OVS_USER" != "" ]; then
396 local uid=$(id -u "${OVS_USER%:*}")
397 local gid=$(id -g "${OVS_USER%:*}")
398 local groups=$(id -G "${OVS_USER%:*}" | tr ' ' ',')
399 setpriv --reuid "$uid" --regid "$gid" --groups "$groups" "$@"
400 else
401 "$@"
402 fi
403 }
404
405 ovsdb_tool () {
406 run_as_ovsuser ovsdb-tool -vconsole:off "$@"
407 }
408
409 create_db () {
410 DB_FILE="$1"
411 DB_SCHEMA="$2"
412 action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
413 }
414
415 backup_db () {
416 # Back up the old version.
417 version=`ovsdb_tool db-version "$DB_FILE"`
418 cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
419 backup=$DB_FILE.backup$version-$cksum
420 action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
421 }
422
423 upgrade_db () {
424 DB_FILE="$1"
425 DB_SCHEMA="$2"
426
427 schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
428 if test ! -e "$DB_FILE"; then
429 log_warning_msg "$DB_FILE does not exist"
430 install_dir `dirname $DB_FILE`
431 create_db "$DB_FILE" "$DB_SCHEMA"
432 elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" = Xyes; then
433 backup_db || return 1
434
435 # Compact database. This is important if the old schema did not enable
436 # garbage collection (i.e. if it did not have any tables with "isRoot":
437 # true) but the new schema does. In that situation the old database
438 # may contain a transaction that creates a record followed by a
439 # transaction that creates the first use of the record. Replaying that
440 # series of transactions against the new database schema (as "convert"
441 # does) would cause the record to be dropped by the first transaction,
442 # then the second transaction would cause a referential integrity
443 # failure (for a strong reference).
444 #
445 # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
446 # understand some feature of the schema used in the OVSDB version that
447 # we're downgrading from, so we don't give up on error.
448 action "Compacting database" ovsdb_tool compact "$DB_FILE"
449
450 # Upgrade or downgrade schema.
451 if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
452 :
453 else
454 log_warning_msg "Schema conversion failed, using empty database instead"
455 rm -f "$DB_FILE"
456 create_db "$DB_FILE" "$DB_SCHEMA"
457 fi
458 fi
459 }
460
461 upgrade_cluster () {
462 local DB_SCHEMA=$1 DB_SERVER=$2
463 local schema_name=$(ovsdb-tool schema-name $1) || return 1
464
465 action "Waiting for $schema_name to come up" ovsdb-client -t 30 wait "$DB_SERVER" "$schema_name" connected || return $?
466 local db_version=$(ovsdb-client -t 10 get-schema-version "$DB_SERVER" "$schema_name") || return $?
467 local target_version=$(ovsdb-tool schema-version "$DB_SCHEMA") || return $?
468
469 if ovsdb-tool compare-versions "$db_version" == "$target_version"; then
470 :
471 elif ovsdb-tool compare-versions "$db_version" ">" "$target_version"; then
472 log_warning_msg "Database $schema_name has newer schema version ($db_version) than our local schema ($target_version), possibly an upgrade is partially complete?"
473 else
474 action "Upgrading database $schema_name from schema version $db_version to $target_version" ovsdb-client -t 30 convert "$DB_SERVER" "$DB_SCHEMA"
475 fi
476 }
477
478 create_cluster () {
479 DB_FILE="$1"
480 DB_SCHEMA="$2"
481 LOCAL_ADDR="$3"
482
483 if test ! -e "$DB_FILE"; then
484 action "Creating cluster database $DB_FILE" ovsdb_tool create-cluster "$DB_FILE" "$DB_SCHEMA" "$LOCAL_ADDR"
485 elif ovsdb_tool db-is-standalone "$DB_FILE"; then
486 # Convert standalone database to clustered.
487 backup_db || return 1
488 action "Creating cluster database $DB_FILE from existing one" \
489 ovsdb_tool create-cluster "$DB_FILE" "$backup" "$LOCAL_ADDR"
490 fi
491 }
492
493 join_cluster() {
494 DB_FILE="$1"
495 SCHEMA_NAME="$2"
496 LOCAL_ADDR="$3"
497 REMOTE_ADDR="$4"
498
499 if test ! -e "$DB_FILE"; then
500 ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR" "$REMOTE_ADDR"
501 elif ovsdb_tool db-is-standalone "$DB_FILE"; then
502 # Backup standalone database and join cluster.
503 backup_db || return 1
504 action "Joining $DB_FILE to cluster" \
505 ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR"
506 fi
507 }
508
509 ovs_vsctl () {
510 ovs-vsctl --no-wait "$@"
511 }
512
513 ## ----------------- ##
514 ## force-reload-kmod ##
515 ## ----------------- ##
516
517 ovs_kmod_ctl () {
518 "$dir0/ovs-kmod-ctl" "$@"
519 }
520
521 internal_interfaces () {
522 # Outputs a list of internal interfaces:
523 #
524 # - There is an internal interface for every bridge, whether it
525 # has an Interface record or not and whether the Interface
526 # record's 'type' is properly set or not.
527 #
528 # - There is an internal interface for each Interface record whose
529 # 'type' is 'internal'.
530 #
531 # But ignore interfaces that don't really exist.
532 for d in `(ovs_vsctl --bare \
533 -- --columns=name find Interface type=internal \
534 -- list-br) | sort -u`
535 do
536 if test -e "/sys/class/net/$d"; then
537 printf "%s " "$d"
538 fi
539 done
540 }
541
542 ovs_save () {
543 bridges=`ovs_vsctl -- --real list-br`
544 if [ -n "${bridges}" ] && \
545 "$datadir/scripts/ovs-save" "$1" ${bridges} > "$2"; then
546 chmod +x "$2"
547 return 0
548 fi
549 [ -z "${bridges}" ] && return 0
550 }
551
552 save_flows_if_required () {
553 if test X"$DELETE_BRIDGES" != Xyes; then
554 action "Saving flows" ovs_save save-flows "${script_flows}"
555 fi
556 }
557
558 save_interfaces () {
559 "$datadir/scripts/ovs-save" save-interfaces ${ifaces} \
560 > "${script_interfaces}"
561 }
562
563 flow_restore_wait () {
564 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
565 ovs_vsctl set open_vswitch . other_config:flow-restore-wait="true"
566 fi
567 }
568
569 flow_restore_complete () {
570 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
571 ovs_vsctl --if-exists remove open_vswitch . other_config \
572 flow-restore-wait="true"
573 fi
574 }
575
576 restore_flows () {
577 [ -x "${script_flows}" ] && \
578 action "Restoring saved flows" "${script_flows}"
579 }
580
581 restore_interfaces () {
582 [ ! -x "${script_interfaces}" ] && return 0
583 action "Restoring interface configuration" "${script_interfaces}"
584 rc=$?
585 if test $rc = 0; then
586 level=debug
587 else
588 level=err
589 fi
590 log="logger -p daemon.$level -t ovs-save"
591 $log "interface restore script exited with status $rc:"
592 $log -f "$script_interfaces"
593 }
594
595 init_restore_scripts () {
596 script_interfaces=`mktemp`
597 script_flows=`mktemp`
598 trap 'rm -f "${script_interfaces}" "${script_flows}"' 0
599 }
600
601 force_reload_kmod () {
602
603 if test X"${OVS_VSWITCHD:-yes}" != Xyes; then
604 log_failure_msg "Reloading of kmod without ovs-vswitchd is an error"
605 exit 1
606 fi
607
608 ifaces=`internal_interfaces`
609 action "Detected internal interfaces: $ifaces" true
610
611 init_restore_scripts
612 save_flows_if_required
613
614 # Restart the database first, since a large database may take a
615 # while to load, and we want to minimize forwarding disruption.
616 stop_ovsdb
617 start_ovsdb || return 1
618
619 stop_forwarding
620
621 if action "Saving interface configuration" save_interfaces; then
622 :
623 else
624 log_warning_msg "Failed to save configuration, not replacing kernel module"
625 start_forwarding
626 add_managers
627 exit 1
628 fi
629 chmod +x "$script_interfaces"
630
631 for dp in `ovs-dpctl dump-dps`; do
632 action "Removing datapath: $dp" ovs-dpctl del-dp "$dp"
633 done
634
635 if test -e /sys/module/ip_gre; then
636 action "Forcing removal of ip_gre module" rmmod ip_gre
637 fi
638
639 if test -e /sys/module/gre; then
640 action "Forcing removal of gre module" rmmod gre
641 fi
642
643 ovs_kmod_ctl remove
644
645 # Start vswitchd by asking it to wait till flow restore is finished.
646 flow_restore_wait
647 start_forwarding || return 1
648
649 # Restore saved flows and inform vswitchd that we are done.
650 restore_flows
651 flow_restore_complete
652 add_managers
653
654 restore_interfaces
655
656 "$datadir/scripts/ovs-check-dead-ifs"
657 }
658
659 ## ------- ##
660 ## restart ##
661 ## ------- ##
662
663 restart () {
664 if daemon_is_running ovsdb-server && daemon_is_running ovs-vswitchd; then
665 init_restore_scripts
666 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
667 save_flows_if_required
668 fi
669 fi
670
671 # Restart the database first, since a large database may take a
672 # while to load, and we want to minimize forwarding disruption.
673 stop_ovsdb
674 start_ovsdb || return 1
675
676 stop_forwarding
677
678 # Start vswitchd by asking it to wait till flow restore is finished.
679 flow_restore_wait
680 start_forwarding || return 1
681
682 # Restore saved flows and inform vswitchd that we are done.
683 restore_flows
684 flow_restore_complete
685 add_managers
686 }