]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-lib.in
cf6b6d296eb5d5865c1cbdf7c02302e8e4af43c3
[ovs.git] / utilities / ovs-lib.in
1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
3
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
21
22 # All of these should be substituted by the Makefile at build time.
23 logdir=${OVS_LOGDIR-'@LOGDIR@'} # /var/log/openvswitch
24 rundir=${OVS_RUNDIR-'@RUNDIR@'} # /var/run/openvswitch
25 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'} # /etc
26 etcdir=$sysconfdir/openvswitch # /etc/openvswitch
27 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'} # /usr/share/openvswitch
28 bindir=${OVS_BINDIR-'@bindir@'} # /usr/bin
29 sbindir=${OVS_SBINDIR-'@sbindir@'} # /usr/sbin
30
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
33 dbdir=$OVS_DBDIR
34 elif test X"$OVS_SYSCONFDIR" != X; then
35 dbdir=$OVS_SYSCONFDIR/openvswitch
36 else
37 dbdir='@DBDIR@'
38 fi
39
40 ovs_ctl_log () {
41 echo "$@" >> "${logdir}/ovs-ctl.log"
42 }
43
44 ovs_ctl () {
45 case "$@" in
46 *"=strace"*)
47 # In case of running the daemon with strace, piping the o/p causes
48 # the script to block (strace probably does not close the inherited
49 # pipe). So, do not log the o/p to ovs-ctl.log.
50 "${datadir}/scripts/ovs-ctl" "$@"
51 ;;
52 "status")
53 # In case of the command 'status', we should return the exit status
54 # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
55 display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
56 rc=$?
57 if test -w "${logdir}/ovs-ctl.log"; then
58 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
59 else
60 echo "${display}"
61 fi
62 return ${rc}
63 ;;
64 *)
65 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
66 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
67 ;;
68 esac
69 }
70
71 VERSION='@VERSION@'
72
73 DAEMON_CWD=/
74
75 LC_ALL=C; export LC_ALL
76
77 ## ------------- ##
78 ## LSB functions ##
79 ## ------------- ##
80
81 # Use the system's own implementations if it has any.
82 if test -e /etc/init.d/functions; then
83 . /etc/init.d/functions
84 elif test -e /etc/rc.d/init.d/functions; then
85 . /etc/rc.d/init.d/functions
86 elif test -e /lib/lsb/init-functions; then
87 . /lib/lsb/init-functions
88 fi
89
90 # Implement missing functions (e.g. OpenSUSE lacks 'action').
91 if type log_success_msg >/dev/null 2>&1; then :; else
92 log_success_msg () {
93 printf '%s.\n' "$*"
94 }
95 fi
96 if type log_failure_msg >/dev/null 2>&1; then :; else
97 log_failure_msg () {
98 printf '%s ... failed!\n' "$*"
99 }
100 fi
101 if type log_warning_msg >/dev/null 2>&1; then :; else
102 log_warning_msg () {
103 printf '%s ... (warning).\n' "$*"
104 }
105 fi
106 if type action >/dev/null 2>&1; then :; else
107 action () {
108 STRING=$1
109 shift
110 "$@"
111 rc=$?
112 if test $rc = 0; then
113 log_success_msg "$STRING"
114 else
115 log_failure_msg "$STRING"
116 fi
117 return $rc
118 }
119 fi
120
121 ## ------- ##
122 ## Daemons ##
123 ## ------- ##
124
125 pid_exists () {
126 # This is better than "kill -0" because it doesn't require permission to
127 # send a signal (so daemon_status in particular works as non-root).
128 test -d /proc/"$1"
129 }
130
131 pid_comm_check () {
132 [ "$1" = "`cat /proc/$2/comm`" ]
133 }
134
135 # version_geq version_a version_b
136 #
137 # Compare (dot separated) version numbers. Returns true (exit code 0) if
138 # version_a is greater or equal than version_b, otherwise false (exit code 1).
139 version_geq() {
140 echo $1 $2 | awk '{
141 n1 = split($1, a, ".");
142 n2 = split($2, b, ".");
143 n = (n1 > n2) ? n1 : n2;
144 for (i = 1; i <= n; i++) {
145 if (a[i]+0 < b[i]+0) exit 1
146 if (a[i]+0 > b[i]+0) exit 0
147 }
148 }'
149 }
150
151 install_dir () {
152 DIR="$1"
153 INSTALL_MODE="${2:-755}"
154 INSTALL_USER="root"
155 INSTALL_GROUP="root"
156 [ "$OVS_USER" != "" ] && INSTALL_USER="${OVS_USER%:*}"
157 [ "${OVS_USER##*:}" != "" ] && INSTALL_GROUP="${OVS_USER##*:}"
158
159 if test ! -d "$DIR"; then
160 install -d -m "$INSTALL_MODE" -o "$INSTALL_USER" -g "$INSTALL_GROUP" "$DIR"
161 restorecon "$DIR" >/dev/null 2>&1
162 fi
163 }
164
165 start_daemon () {
166 priority=$1
167 wrapper=$2
168 shift; shift
169 daemon=$1
170 strace=""
171
172 # drop core files in a sensible place
173 install_dir "$DAEMON_CWD"
174 set "$@" --no-chdir
175 cd "$DAEMON_CWD"
176
177 # log file
178 install_dir "$logdir" "750"
179 set "$@" --log-file="$logdir/$daemon.log"
180
181 # pidfile and monitoring
182 install_dir "$rundir"
183 set "$@" --pidfile="$rundir/$daemon.pid"
184 set "$@" --detach
185 test X"$MONITOR" = Xno || set "$@" --monitor
186
187 # wrapper
188 case $wrapper in
189 valgrind)
190 if (valgrind --version) > /dev/null 2>&1; then
191 set valgrind -q --leak-check=full --time-stamp=yes \
192 --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
193 else
194 log_failure_msg "valgrind not installed, running $daemon without it"
195 fi
196 ;;
197 strace)
198 if (strace -V) > /dev/null 2>&1; then
199 strace="strace -tt -T -s 256 -ff"
200 if (strace -DV) > /dev/null 2>&1; then
201 # Has the -D option.
202 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
203 strace=""
204 fi
205 else
206 log_failure_msg "strace not installed, running $daemon without it"
207 fi
208 ;;
209 glibc)
210 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
211 ;;
212 '')
213 ;;
214 *)
215 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
216 ;;
217 esac
218
219 # priority
220 if test X"$priority" != X; then
221 set nice -n "$priority" "$@"
222 fi
223
224 action "Starting $daemon" "$@" || return 1
225
226 if test X"$strace" != X; then
227 # Strace doesn't have the -D option so we attach after the fact.
228 setsid $strace -o "$logdir/$daemon.strace.log" \
229 -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
230 fi
231 }
232
233 stop_daemon () {
234 if test -e "$rundir/$1.pid"; then
235 if pid=`cat "$rundir/$1.pid"`; then
236
237 graceful="EXIT .1 .25 .65 1"
238 actions="TERM .1 .25 .65 1 1 1 1 \
239 KILL 1 1 1 2 10 15 30 \
240 FAIL"
241 version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
242 | awk 'NR==1{print $NF}'`
243
244 # Use `ovs-appctl exit` only if the running daemon version
245 # is >= 2.5.90. This script might be used during upgrade to
246 # stop older versions of daemons which do not behave correctly
247 # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
248 # internal ports).
249 if version_geq "$version" "2.5.90"; then
250 actions="$graceful $actions"
251 fi
252 for action in $actions; do
253 if pid_exists "$pid" >/dev/null 2>&1; then :; else
254 return 0
255 fi
256 case $action in
257 EXIT)
258 action "Exiting $1 ($pid)" \
259 ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit
260 ;;
261 TERM)
262 action "Killing $1 ($pid)" kill $pid
263 ;;
264 KILL)
265 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
266 ;;
267 FAIL)
268 log_failure_msg "Killing $1 ($pid) failed"
269 return 1
270 ;;
271 *)
272 sleep $action
273 ;;
274 esac
275 done
276 fi
277 fi
278 log_success_msg "$1 is not running"
279 }
280
281 daemon_status () {
282 pidfile=$rundir/$1.pid
283 if test -e "$pidfile"; then
284 if pid=`cat "$pidfile"`; then
285 if pid_exists "$pid"; then
286 echo "$1 is running with pid $pid"
287 return 0
288 else
289 echo "Pidfile for $1 ($pidfile) is stale"
290 fi
291 else
292 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
293 fi
294 else
295 echo "$1 is not running"
296 fi
297 return 1
298 }
299
300 daemon_is_running () {
301 pidfile=$rundir/$1.pid
302 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
303 } >/dev/null 2>&1
304
305 # Prints commands needed to move the ip address from interface $1 to interface
306 # $2
307 move_ip_address () {
308 if [ -z "$1" ] || [ -z "$2" ]; then
309 return
310 fi
311 dev="$1"
312 dst="$2"
313
314 # IP addresses (including IPv6).
315 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
316 ip addr show dev $dev | while read addr; do
317 set -- $addr
318
319 # Check and trim family.
320 family=$1
321 shift
322 case $family in
323 inet | inet6) ;;
324 *) continue ;;
325 esac
326
327 # Trim device off the end--"ip" insists on having "dev" precede it.
328 addrcmd=
329 while test $# != 0; do
330 case $1 in
331 dynamic)
332 # XXX: According to 'man ip-address', "dynamic" is only
333 # used for ipv6 addresses. But, atleast on RHEL 7.4
334 # (iproute-3.10.0-87), it is being used for ipv4
335 # addresses assigned with dhcp.
336 if [ "$family" = "inet" ]; then
337 shift
338 continue
339 fi
340 # Omit kernel-maintained route.
341 continue 2
342 ;;
343 scope)
344 if test "$2" = link -a "$family" != inet6; then
345 # Omit route derived from IP address, e.g.
346 # 172.16.0.0/16 derived from 172.16.12.34,
347 # but preserve IPv6 link-local address.
348 continue 2
349 fi
350 ;;
351 "$dev"|"$dev:"*)
352 # Address label string
353 label=`echo $1 | sed "s/$dev/$dst/"`
354 addrcmd="$addrcmd label $label"
355 shift
356 continue
357 ;;
358 esac
359 addrcmd="$addrcmd $1"
360 shift
361 done
362 if test "$1" != "$dev"; then
363 addrcmd="$addrcmd $1"
364 fi
365
366 echo ip -f $family addr add $addrcmd dev $dst
367 done
368 }
369
370 # Prints commands needed to move the ip route of interface $1 to interface $2
371 move_ip_routes () {
372 if [ -z "$1" ] || [ -z "$2" ]; then
373 return
374 fi
375 dev="$1"
376 dst="$2"
377 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
378 ip route show dev $dev | while read route; do
379 # "proto kernel" routes are installed by the kernel automatically.
380 case $route in
381 *" proto kernel "*) continue ;;
382 esac
383
384 echo "ip route add $route dev $dst"
385 done
386 }
387
388 ovsdb_tool () {
389 if [ "$OVS_USER" != "" ]; then
390 runuser --user "${OVS_USER%:*}" -- ovsdb-tool -vconsole:off "$@"
391 else
392 ovsdb-tool -vconsole:off "$@"
393 fi
394 }
395
396 create_db () {
397 DB_FILE="$1"
398 DB_SCHEMA="$2"
399 action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
400 }
401
402 upgrade_db () {
403 DB_FILE="$1"
404 DB_SCHEMA="$2"
405
406 schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
407 if test ! -e "$DB_FILE"; then
408 log_warning_msg "$DB_FILE does not exist"
409 install_dir `dirname $DB_FILE`
410 create_db "$DB_FILE" "$DB_SCHEMA"
411 elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" = Xyes; then
412 # Back up the old version.
413 version=`ovsdb_tool db-version "$DB_FILE"`
414 cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
415 backup=$DB_FILE.backup$version-$cksum
416 action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
417
418 # Compact database. This is important if the old schema did not enable
419 # garbage collection (i.e. if it did not have any tables with "isRoot":
420 # true) but the new schema does. In that situation the old database
421 # may contain a transaction that creates a record followed by a
422 # transaction that creates the first use of the record. Replaying that
423 # series of transactions against the new database schema (as "convert"
424 # does) would cause the record to be dropped by the first transaction,
425 # then the second transaction would cause a referential integrity
426 # failure (for a strong reference).
427 #
428 # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
429 # understand some feature of the schema used in the OVSDB version that
430 # we're downgrading from, so we don't give up on error.
431 action "Compacting database" ovsdb_tool compact "$DB_FILE"
432
433 # Upgrade or downgrade schema.
434 if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
435 :
436 else
437 log_warning_msg "Schema conversion failed, using empty database instead"
438 rm -f "$DB_FILE"
439 create_db "$DB_FILE" "$DB_SCHEMA"
440 fi
441 fi
442 }
443
444 ovs_vsctl () {
445 ovs-vsctl --no-wait "$@"
446 }
447
448 ## ----------------- ##
449 ## force-reload-kmod ##
450 ## ----------------- ##
451
452 internal_interfaces () {
453 # Outputs a list of internal interfaces:
454 #
455 # - There is an internal interface for every bridge, whether it
456 # has an Interface record or not and whether the Interface
457 # record's 'type' is properly set or not.
458 #
459 # - There is an internal interface for each Interface record whose
460 # 'type' is 'internal'.
461 #
462 # But ignore interfaces that don't really exist.
463 for d in `(ovs_vsctl --bare \
464 -- --columns=name find Interface type=internal \
465 -- list-br) | sort -u`
466 do
467 if test -e "/sys/class/net/$d"; then
468 printf "%s " "$d"
469 fi
470 done
471 }
472
473 ovs_save () {
474 bridges=`ovs_vsctl -- --real list-br`
475 if [ -n "${bridges}" ] && \
476 "$datadir/scripts/ovs-save" "$1" ${bridges} > "$2"; then
477 chmod +x "$2"
478 return 0
479 fi
480 [ -z "${bridges}" ] && return 0
481 }
482
483 save_flows_if_required () {
484 if test X"$DELETE_BRIDGES" != Xyes; then
485 action "Saving flows" ovs_save save-flows "${script_flows}"
486 fi
487 }
488
489 save_interfaces () {
490 "$datadir/scripts/ovs-save" save-interfaces ${ifaces} \
491 > "${script_interfaces}"
492 }
493
494 flow_restore_wait () {
495 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
496 ovs_vsctl set open_vswitch . other_config:flow-restore-wait="true"
497 fi
498 }
499
500 flow_restore_complete () {
501 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
502 ovs_vsctl --if-exists remove open_vswitch . other_config \
503 flow-restore-wait="true"
504 fi
505 }
506
507 restore_flows () {
508 [ -x "${script_flows}" ] && \
509 action "Restoring saved flows" "${script_flows}"
510 }
511
512 restore_interfaces () {
513 [ ! -x "${script_interfaces}" ] && return 0
514 action "Restoring interface configuration" "${script_interfaces}"
515 rc=$?
516 if test $rc = 0; then
517 level=debug
518 else
519 level=err
520 fi
521 log="logger -p daemon.$level -t ovs-save"
522 $log "interface restore script exited with status $rc:"
523 $log -f "$script_interfaces"
524 }
525
526 init_restore_scripts () {
527 script_interfaces=`mktemp`
528 script_flows=`mktemp`
529 trap 'rm -f "${script_interfaces}" "${script_flows}"' 0
530 }
531
532 force_reload_kmod () {
533
534 if test X"${OVS_VSWITCHD:-yes}" != Xyes; then
535 log_failure_msg "Reloading of kmod without ovs-vswitchd is an error"
536 exit 1
537 fi
538
539 ifaces=`internal_interfaces`
540 action "Detected internal interfaces: $ifaces" true
541
542 init_restore_scripts
543 save_flows_if_required
544
545 # Restart the database first, since a large database may take a
546 # while to load, and we want to minimize forwarding disruption.
547 stop_ovsdb
548 start_ovsdb || return 1
549
550 stop_forwarding
551
552 if action "Saving interface configuration" save_interfaces; then
553 :
554 else
555 log_warning_msg "Failed to save configuration, not replacing kernel module"
556 start_forwarding
557 add_managers
558 exit 1
559 fi
560 chmod +x "$script_interfaces"
561
562 for dp in `ovs-dpctl dump-dps`; do
563 action "Removing datapath: $dp" ovs-dpctl del-dp "$dp"
564 done
565
566 for vport in `awk '/^vport_/ { print $1 }' /proc/modules`; do
567 action "Removing $vport module" rmmod $vport
568 done
569
570 if test -e /sys/module/openvswitch; then
571 action "Removing openvswitch module" rmmod openvswitch
572 fi
573
574 # Start vswitchd by asking it to wait till flow restore is finished.
575 flow_restore_wait
576 start_forwarding || return 1
577
578 # Restore saved flows and inform vswitchd that we are done.
579 restore_flows
580 flow_restore_complete
581 add_managers
582
583 restore_interfaces
584
585 "$datadir/scripts/ovs-check-dead-ifs"
586 }
587
588 ## ------- ##
589 ## restart ##
590 ## ------- ##
591
592 restart () {
593 if daemon_is_running ovsdb-server && daemon_is_running ovs-vswitchd; then
594 init_restore_scripts
595 if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
596 save_flows_if_required
597 fi
598 fi
599
600 # Restart the database first, since a large database may take a
601 # while to load, and we want to minimize forwarding disruption.
602 stop_ovsdb
603 start_ovsdb || return 1
604
605 stop_forwarding
606
607 # Start vswitchd by asking it to wait till flow restore is finished.
608 flow_restore_wait
609 start_forwarding || return 1
610
611 # Restore saved flows and inform vswitchd that we are done.
612 restore_flows
613 flow_restore_complete
614 add_managers
615 }