ovsdb: Implement garbage collection.
[cascardo/ovs.git] / xenserver / etc_init.d_openvswitch
1 #!/bin/bash
2 #
3 # openvswitch
4 #
5 # chkconfig: 2345 09 91
6 # description: Manage Open vSwitch kernel modules and user-space daemons
7
8 # Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at:
13 #
14 #     http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 ### BEGIN INIT INFO
22 # Provides:          openvswitch-switch
23 # Required-Start:
24 # Required-Stop:
25 # Default-Start:     2 3 4 5
26 # Default-Stop:      0 1 6
27 # Short-Description: Open vSwitch switch
28 ### END INIT INFO
29
30 # source function library
31 if [ -f /etc/init.d/functions ]; then
32     . /etc/init.d/functions
33 elif [ -f /etc/rc.d/init.d/functions ]; then
34     . /etc/rc.d/init.d/functions
35 elif [ -f /lib/lsb/init-functions ]; then
36     . /lib/lsb/init-functions
37 else
38     echo "$0: missing LSB shell function library" >&2
39     exit 1
40 fi
41
42 if type action >/dev/null 2>&1; then
43     :
44 else
45     # SUSE lacks action
46     action() {
47        STRING=$1
48        shift
49        "$@"
50        rc=$?
51        if [ $rc -eq 0 ] ; then
52             log_success_msg $"$STRING "
53        else
54             log_failure_msg $"$STRING "
55        fi
56        return $rc
57     }
58 fi
59
60 test -e /etc/xensource-inventory && . /etc/xensource-inventory
61 test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
62 if test -e /etc/xensource/network.conf; then
63     NETWORK_MODE=$(cat /etc/xensource/network.conf)
64 fi
65
66 case ${NETWORK_MODE:=openvswitch} in
67     vswitch|openvswitch)
68         ;;
69     bridge)
70         exit 0
71         ;;
72     *)
73         echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
74         exit 0
75         ;;
76 esac
77
78 : ${ENABLE_MONITOR:=y}
79 : ${FORCE_COREFILES:=y}
80
81 # Config variables specific to ovsdb-server
82 : ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,manager_options}
83 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
84 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
85 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
86 : ${OVSDB_SERVER_PRIORITY:=-10}
87 : ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
88 : ${OVSDB_SERVER_FILE_LOGLEVEL:=INFO}
89 : ${OVSDB_SERVER_SYSLOG_LOGLEVEL:=ERR}
90 : ${OVSDB_SERVER_MEMLEAK_LOGFILE:=}
91 : ${OVSDB_SERVER_STRACE_LOG:=}
92 : ${OVSDB_SERVER_STRACE_OPT:=}
93 : ${OVSDB_SERVER_VALGRIND_LOG:=}
94 : ${OVSDB_SERVER_VALGRIND_OPT:=}
95
96 # Config variables specific to ovs-vswitchd
97 : ${VSWITCHD_OVSDB_SERVER:=unix:/var/run/openvswitch/db.sock}
98 : ${VSWITCHD_OVSDB_SCHEMA:=/usr/share/openvswitch/vswitch.ovsschema}
99 : ${VSWITCHD_PIDFILE:=/var/run/openvswitch/ovs-vswitchd.pid}
100 : ${VSWITCHD_RUN_DIR:=/var/xen/openvswitch}
101 : ${VSWITCHD_PRIORITY:=-10}
102 : ${VSWITCHD_MLOCKALL:=yes}
103 : ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
104 : ${VSWITCHD_FILE_LOGLEVEL:=INFO}
105 : ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
106 : ${VSWITCHD_MEMLEAK_LOGFILE:=}
107 : ${VSWITCHD_STRACE_LOG:=}
108 : ${VSWITCHD_STRACE_OPT:=}
109 : ${VSWITCHD_VALGRIND_LOG:=}
110 : ${VSWITCHD_VALGRIND_OPT:=}
111
112 # Full paths to executables & modules
113 ovsdb_server="/usr/sbin/ovsdb-server"
114 ovsdb_tool="/usr/bin/ovsdb-tool"
115 vswitchd="/usr/sbin/ovs-vswitchd"
116 dpctl="/usr/bin/ovs-dpctl"
117 appctl="/usr/bin/ovs-appctl"
118 ofctl="/usr/bin/ovs-ofctl"
119 vsctl="/usr/bin/ovs-vsctl"
120
121 if test "$ENABLE_MONITOR" = "y"; then
122     monitor_opt="--monitor"
123 else
124     monitor_opt=
125 fi
126
127 function hup_monitor_external_ids {
128     if [ -e /var/run/openvswitch/ovs-xapi-sync.pid ]; then
129         action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-xapi-sync.pid`
130     fi
131 }
132
133 function turn_on_corefiles {
134     ulimit -Sc 67108864
135 }
136
137 function remove_all_dp {
138     for dp in $($dpctl dump-dps); do
139         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
140     done
141 }
142
143 function insert_modules_if_required {
144     if test -e /sys/module/bridge; then
145         bridges=`echo /sys/class/net/*/bridge | sed 's,/sys/class/net/,,g;s,/bridge,,g'`
146         if test "$bridges" != "*"; then
147             log_warning_msg "not removing bridge module because bridges exist ($bridges)"
148         else
149             action "removing bridge module" rmmod bridge
150         fi
151     fi
152     if ! lsmod | grep -q "openvswitch_mod"; then
153         action "Inserting llc module" modprobe llc
154         action "Inserting openvswitch module" modprobe openvswitch_mod
155     fi
156 }
157
158 function remove_modules {
159     if lsmod | grep -q "openvswitch_mod"; then
160         action "Removing openvswitch module" rmmod openvswitch_mod.ko
161     fi
162 }
163
164 function start_daemon {
165     local DAEMON=$1
166     shift
167     local BINARY=$1
168
169     # cd to daemon's run_dir so core files get dumped into a sensible place.
170     eval local run_dir=\$${DAEMON}_RUN_DIR
171     if [ ! -d "$run_dir" ]; then
172         install -d -m 755 -o root -g root "$run_dir"
173     fi
174     cd "$run_dir"
175     
176     # Configure log levels.
177     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
178     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
179     eval local logfile=\$${DAEMON}_LOGFILE
180     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
181     if test -n "$file_loglevel" && test -n "$logfile"; then
182         install -d -m 755 -o root -g root `dirname "$logfile"`
183         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
184     fi
185
186     # Configure leak checker.
187     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
188     if test -n "$memleak_logfile"; then
189         set -- "$@" --check-leaks="$memleak_logfile"
190         if test -e "$memleak_logfile"; then
191             mv "$memleak_logfile" "$memleak_logfile.prev"
192         fi
193     fi
194
195     # Configure debugging wrappers.
196     eval local strace_log=\$${DAEMON}_STRACE_LOG
197     eval local strace_opt=\$${DAEMON}_STRACE_OPT
198     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
199     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
200     if test -n "$strace_log" && test -n "$valgrind_log"; then
201         printf "Can not start with both VALGRIND and STRACE\n"
202         exit 1
203     elif test -n "$strace_log"; then
204         local mode=strace
205         set -- strace -o "$strace_log" $strace_opt "$@"
206     elif test -n "$valgrind_log"; then
207         local mode=valgrind
208         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
209     else
210         local mode=production
211         eval local pidfile=\$${DAEMON}_PIDFILE
212         install -d -m 755 -o root -g root `dirname $pidfile`
213         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
214     fi
215
216     # Configure niceness.
217     eval local priority=\$${DAEMON}_PRIORITY
218     if test -n "$priority"; then
219         set -- nice -n $priority "$@"
220     fi
221
222     if test $mode = production; then
223         action "Starting `basename $BINARY`" "$@"
224     else
225         # Start in background and force a "success" message
226         action "Starting `basename $BINARY` with $mode debugging" true
227         ("$@") &
228     fi
229 }
230
231 function start_ovsdb_server {
232     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
233     for remote in $OVSDB_SERVER_REMOTES; do
234         set -- "$@" --remote="$remote"
235     done
236     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
237     start_daemon OVSDB_SERVER "$@"
238 }
239
240 function start_vswitchd {
241     local mlockall_opt=
242     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
243         mlockall_opt="--mlockall"
244     fi
245
246     start_daemon VSWITCHD "$vswitchd" $mlockall_opt "$VSWITCHD_OVSDB_SERVER"
247  }
248
249 function stop_daemon {
250     local DAEMON=$1
251     local BINARY=$2
252     eval local pidfile=\$${DAEMON}_PIDFILE
253     if test -f "$pidfile"; then
254         local pid=$(cat "$pidfile")
255         action "Killing `basename $BINARY` ($pid)" kill $pid
256         for delay in .1 .25 .65 1 1 1 1; do
257             if kill -0 $pid >/dev/null 2>&1; then
258                 sleep $delay
259             else
260                 break
261             fi
262         done
263         rm -f "$pidfile"
264     fi
265 }
266
267 function restart_approval {
268     if test ! -t 0; then
269         # Don't prompt if invoked non-interactively.
270         return 0
271     fi
272     cat <<EOF
273
274 WARNING!!!
275
276 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
277 provided as a convenience for those situations in which it does work.
278
279 EOF
280     read -s -r -n 1 -p "Continue with restart (y/N): " response
281     printf "\n"
282     case "$response" in
283         y|Y)
284             return 0
285             ;;
286         *)
287             return 1
288             ;;
289     esac
290 }
291
292 function set_system_ids {
293     if [ -f /etc/xensource-inventory ]; then
294         OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
295         action "Configuring Open vSwitch system IDs" true
296         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
297             ovs-version="$OVS_VERSION" \
298             system-type="$PRODUCT_BRAND" \
299             system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
300             external-ids:system-id="$INSTALLATION_UUID" \
301             external-ids:xs-system-uuid="$INSTALLATION_UUID"
302     else
303         if test -f /etc/openvswitch/install_uuid.conf; then
304             . /etc/openvswitch/install_uuid.conf
305         elif INSTALLATION_UUID=`uuidgen`; then
306             echo "INSTALLATION_UUID=$INSTALLATION_UUID" > /etc/openvswitch/install_uuid.conf
307         else
308             log_failure_msg "missing uuidgen, could not generate system UUID"
309             return
310         fi
311         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
312             external-ids:system-id="$INSTALLATION_UUID"
313         action "Configuring Open vSwitch system IDs" true
314     fi
315 }
316
317 function start {
318     if [ "$FORCE_COREFILES" = "y" ]; then
319         turn_on_corefiles
320     fi
321
322     insert_modules_if_required
323
324     # Increase the limit on the number of open file descriptors since
325     # ovs-vswitchd needs a few per bridge
326     ulimit -n 4096
327
328     # Allow GRE traffic.
329     iptables -I INPUT -p gre -j ACCEPT
330
331     schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
332     if [ ! -e "$OVSDB_SERVER_DB" ]; then
333         warning "$OVSDB_SERVER_DB does not exist"
334         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
335
336         action "Creating empty database $OVSDB_SERVER_DB" true
337         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
338     elif test "X`$ovsdb_tool needs-conversion "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"`" != Xno; then
339         # Back up the old version.
340         version=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
341         cksum=`$ovsdb_tool db-cksum "$OVSDB_SERVER_DB" | awk '{print $1}'`
342         cp "$OVSDB_SERVER_DB" "$OVSDB_SERVER_DB.backup$version-$cksum"
343
344         # Compact database.  This is important if the old schema did not enable
345         # garbage collection (i.e. if it did not have any tables with "isRoot":
346         # true) but the new schema does.  In that situation the old database
347         # may contain a transaction that creates a record followed by a
348         # transaction that creates the first use of the record.  Replaying that
349         # series of transactions against the new database schema (as "convert"
350         # does) would cause the record to be dropped by the first transaction,
351         # then the second transaction would cause a referential integrity
352         # failure (for a strong reference).
353         $ovsdb_tool -vANY:console:emer compact "$OVSDB_SERVER_DB"
354
355         # Upgrade or downgrade schema.
356         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
357     fi
358
359     start_ovsdb_server
360     $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
361     if [ ! -e /var/run/openvswitch.booted ]; then
362         touch /var/run/openvswitch.booted
363         for bridge in $($vsctl list-br); do
364             $vsctl --no-wait --timeout=5 del-br $bridge
365         done
366     fi
367
368     set_system_ids
369
370     start_vswitchd
371
372     if [ -f /etc/xensource-inventory ]; then
373         # Start daemon to monitor external ids
374         PYTHONPATH=/usr/share/openvswitch/python \
375                    /usr/share/openvswitch/scripts/ovs-xapi-sync \
376                    --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
377     fi
378
379     touch /var/lock/subsys/openvswitch
380 }
381
382 function stop {
383     stop_daemon VSWITCHD "$vswitchd"
384     stop_daemon OVSDB_SERVER "$ovsdb_server"
385     if [ -e /var/run/openvswitch/ovs-xapi-sync.pid ]; then
386         kill `cat /var/run/openvswitch/ovs-xapi-sync.pid`
387     fi
388     rm -f /var/lock/subsys/openvswitch
389 }
390
391 function restart {
392     if restart_approval; then
393         stop
394         start
395     fi
396 }
397
398 function internal_interfaces {
399     # Outputs a list of internal interfaces:
400     #
401     #   - There is an internal interface for every bridge, whether it has
402     #     an Interface record or not and whether the Interface record's
403     #     'type' is properly set.
404     #
405     #   - There is an internal interface for each Interface record whose
406     #     'type' is 'internal'.
407     #
408     # But ignore interfaces that don't really exist.
409     for d in `(ovs-vsctl --bare \
410                 -- --columns=name find Interface type=internal \
411                 -- list-br) | sort -u`
412     do
413         if test -e "/sys/class/net/$d"; then
414             printf "%s " "$d"
415         fi
416     done
417 }
418
419 function force_reload_kmod {
420     ifaces=$(internal_interfaces)
421     action "Configured internal interfaces: $ifaces" true
422
423     stop
424
425     script=$(mktemp)
426     action "Save interface configuration to $script" true
427     if ! /usr/share/openvswitch/scripts/ovs-save $ifaces > $script; then
428         warning "Failed to save configuration, not replacing kernel module"
429         start
430         exit 1
431     fi
432     chmod +x $script
433
434     action "Destroy datapaths" remove_all_dp
435
436     remove_modules
437
438     start
439
440     action "Restore interface configuration from $script" $script
441 }
442
443 case "$1" in
444     start)
445         start
446         ;;
447     stop)
448         stop
449         ;;
450     restart)
451         restart
452         ;;
453     reload|force-reload)
454         # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
455         # configuration up-to-date all the time.  HUP ovs-xapi-sync so it
456         # re-runs.
457         hup_monitor_external_ids
458         ;;
459     strace-vswitchd)
460         shift
461         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
462         ;;
463     status)
464         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
465         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd
466         ;;
467     version)
468         /usr/sbin/ovsdb-server -V
469         /usr/sbin/ovs-vswitchd -V
470         ;;
471     force-reload-kmod)
472         force_reload_kmod
473         ;;
474     help)
475         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
476         ;;
477     *)
478         printf "Unknown command: $1\n"
479         exit 1
480         ;;
481 esac