c77132a308a223df904efc0cab6e40490b0ae07d
[sliver-openvswitch.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:    $network $named $remote_fs $syslog
24 # Required-Stop:     $remote_fs
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
31 . /etc/init.d/functions
32
33 . /etc/xensource-inventory
34 test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
35
36 NETWORK_MODE=$(cat /etc/xensource/network.conf)
37 case $NETWORK_MODE in
38     vswitch|openvswitch)
39         ;;
40     bridge)
41         exit 0
42         ;;
43     *)
44         echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
45         exit 0
46         ;;
47 esac
48
49 # General config variables in /etc/sysconfig/openvswitch
50 if test "$PRODUCT_VERSION" = "5.5.0"; then
51     # XenServer 5.5.0 needs ovs-brcompatd and /proc/net simulation.
52     : ${ENABLE_BRCOMPAT:=y}
53     : ${ENABLE_FAKE_PROC_NET:=y}
54 else
55     # Later versions don't need them.
56     : ${ENABLE_BRCOMPAT:=n}
57     : ${ENABLE_FAKE_PROC_NET:=n}
58 fi
59 : ${ENABLE_MONITOR:=y}
60 : ${FORCE_COREFILES:=y}
61
62 # Config variables specific to ovsdb-server
63 : ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,managers}
64 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
65 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
66 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
67 : ${OVSDB_SERVER_PRIORITY:=-10}
68 : ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
69 : ${OVSDB_SERVER_FILE_LOGLEVEL:=INFO}
70 : ${OVSDB_SERVER_SYSLOG_LOGLEVEL:=ERR}
71 : ${OVSDB_SERVER_MEMLEAK_LOGFILE:=}
72 : ${OVSDB_SERVER_STRACE_LOG:=}
73 : ${OVSDB_SERVER_STRACE_OPT:=}
74 : ${OVSDB_SERVER_VALGRIND_LOG:=}
75 : ${OVSDB_SERVER_VALGRIND_OPT:=}
76
77 # Config variables specific to ovs-vswitchd
78 : ${VSWITCHD_OVSDB_SERVER:=unix:/var/run/openvswitch/db.sock}
79 : ${VSWITCHD_OVSDB_SCHEMA:=/usr/share/openvswitch/vswitch.ovsschema}
80 : ${VSWITCHD_PIDFILE:=/var/run/openvswitch/ovs-vswitchd.pid}
81 : ${VSWITCHD_RUN_DIR:=/var/xen/openvswitch}
82 : ${VSWITCHD_PRIORITY:=-10}
83 : ${VSWITCHD_MLOCKALL:=yes}
84 : ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
85 : ${VSWITCHD_FILE_LOGLEVEL:=INFO}
86 : ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
87 : ${VSWITCHD_MEMLEAK_LOGFILE:=}
88 : ${VSWITCHD_STRACE_LOG:=}
89 : ${VSWITCHD_STRACE_OPT:=}
90 : ${VSWITCHD_VALGRIND_LOG:=}
91 : ${VSWITCHD_VALGRIND_OPT:=}
92
93 # Config variables specific to ovs-brcompatd
94 : ${BRCOMPATD_PIDFILE:=/var/run/openvswitch/ovs-brcompatd.pid}
95 : ${BRCOMPATD_RUN_DIR:=/var/xen/openvswitch}
96 : ${BRCOMPATD_PRIORITY:=-10}
97 : ${BRCOMPATD_LOGFILE:=/var/log/openvswitch/ovs-brcompatd.log}
98 : ${BRCOMPATD_FILE_LOGLEVEL:=INFO}
99 : ${BRCOMPATD_SYSLOG_LOGLEVEL:=ERR}
100 : ${BRCOMPATD_MEMLEAK_LOGFILE:=}
101 : ${BRCOMPATD_STRACE_LOG:=}
102 : ${BRCOMPATD_STRACE_OPT:=}
103 : ${BRCOMPATD_VALGRIND_LOG:=}
104 : ${BRCOMPATD_VALGRIND_OPT:=}
105
106 # Full paths to executables & modules
107 ovsdb_server="/usr/sbin/ovsdb-server"
108 ovsdb_tool="/usr/bin/ovsdb-tool"
109 vswitchd="/usr/sbin/ovs-vswitchd"
110 brcompatd="/usr/sbin/ovs-brcompatd"
111 dpctl="/usr/bin/ovs-dpctl"
112 appctl="/usr/bin/ovs-appctl"
113 ofctl="/usr/bin/ovs-ofctl"
114 vsctl="/usr/bin/ovs-vsctl"
115
116 if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
117     if [ "$ENABLE_BRCOMPAT" != "y" ]; then
118         warning "FAKE_PROC_NET required BRCOMPAT which was disabled.  Force enabling."
119         ENABLE_BRCOMPAT="y"
120     fi
121 fi
122
123 if test "$ENABLE_MONITOR" = "y"; then
124     monitor_opt="--monitor"
125 else
126     monitor_opt=
127 fi
128
129 function hup_monitor_external_ids {
130     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
131         action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-external-ids.pid`
132     fi
133 }
134
135 function dp_list {
136     "$dpctl" show | grep '^dp[0-9]\+:' | cut -d':' -f 1
137 }
138
139 function turn_on_corefiles {
140     ulimit -Sc 67108864
141 }
142
143 function remove_all_dp {
144     for dp in $(dp_list); do
145         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
146     done
147 }
148
149 function insert_modules_if_required {
150     if ! lsmod | grep -q "openvswitch_mod"; then
151         action "Inserting llc module" modprobe llc
152         action "Inserting openvswitch module" modprobe openvswitch_mod
153     fi
154     if [ "$ENABLE_BRCOMPAT" = "y" ] && [ -n "$BRCOMPATD_PIDFILE" ] && ! lsmod | grep -q "brcompat_mod"; then
155         action "Inserting brcompat module" modprobe brcompat_mod
156     fi
157 }
158
159 function remove_modules {
160     if lsmod | grep -q "brcompat_mod"; then
161         action "Removing brcompat module" rmmod brcompat_mod.ko
162     fi
163     if lsmod | grep -q "openvswitch_mod"; then
164         action "Removing openvswitch module" rmmod openvswitch_mod.ko
165     fi
166 }
167
168 function start_daemon {
169     local DAEMON=$1
170     shift
171     local BINARY=$1
172
173     # cd to daemon's run_dir so core files get dumped into a sensible place.
174     eval local run_dir=\$${DAEMON}_RUN_DIR
175     if [ ! -d "$run_dir" ]; then
176         install -d -m 755 -o root -g root "$run_dir"
177     fi
178     cd "$run_dir"
179     
180     # Configure log levels.
181     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
182     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
183     eval local logfile=\$${DAEMON}_LOGFILE
184     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
185     if test -n "$file_loglevel" && test -n "$logfile"; then
186         install -d -m 755 -o root -g root `dirname "$logfile"`
187         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
188     fi
189
190     # Configure leak checker.
191     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
192     if test -n "$memleak_logfile"; then
193         set -- "$@" --check-leaks="$memleak_logfile"
194         if test -e "$memleak_logfile"; then
195             mv "$memleak_logfile" "$memleak_logfile.prev"
196         fi
197     fi
198
199     # Configure debugging wrappers.
200     eval local strace_log=\$${DAEMON}_STRACE_LOG
201     eval local strace_opt=\$${DAEMON}_STRACE_OPT
202     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
203     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
204     if test -n "$strace_log" && test -n "$valgrind_log"; then
205         printf "Can not start with both VALGRIND and STRACE\n"
206         exit 1
207     elif test -n "$strace_log"; then
208         local mode=strace
209         set -- strace -o "$strace_log" $strace_opt "$@"
210     elif test -n "$valgrind_log"; then
211         local mode=valgrind
212         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
213     else
214         local mode=production
215         eval local pidfile=\$${DAEMON}_PIDFILE
216         install -d -m 755 -o root -g root `dirname $pidfile`
217         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
218     fi
219
220     # Configure niceness.
221     eval local priority=\$${DAEMON}_PRIORITY
222     if test -n "$priority"; then
223         set -- nice -n $priority "$@"
224     fi
225
226     if test $mode = production; then
227         action "Starting `basename $BINARY`" "$@"
228     else
229         # Start in background and force a "success" message
230         action "Starting `basename $BINARY` with $mode debugging" true
231         ("$@") &
232     fi
233 }
234
235 function start_ovsdb_server {
236     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
237     for remote in $OVSDB_SERVER_REMOTES; do
238         set -- "$@" --remote="$remote"
239     done
240     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
241     start_daemon OVSDB_SERVER "$@"
242 }
243
244 function start_vswitchd {
245     local fake_proc_net_opt=
246     if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
247         fake_proc_net_opt="--fake-proc-net"
248     fi
249
250     local mlockall_opt=
251     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
252         mlockall_opt="--mlockall"
253     fi
254
255     start_daemon VSWITCHD "$vswitchd" $fake_proc_net_opt $mlockall_opt \
256                  "$VSWITCHD_OVSDB_SERVER"
257  }
258
259 function start_brcompatd {
260     start_daemon BRCOMPATD "$brcompatd" \
261                  --appctl-command="$appctl --target=/var/run/openvswitch/ovs-vswitchd.\`cat $VSWITCHD_PIDFILE\`.ctl %s" \
262                  "$VSWITCHD_OVSDB_SERVER"
263 }
264
265 function stop_daemon {
266     local DAEMON=$1
267     local BINARY=$2
268     eval local pidfile=\$${DAEMON}_PIDFILE
269     if test -f "$pidfile"; then
270         local pid=$(cat "$pidfile")
271         action "Killing `basename $BINARY` ($pid)" kill $pid
272         for delay in .1 .25 .65 1 1 1 1; do
273             if kill -0 $pid >/dev/null 2>&1; then
274                 sleep $delay
275             else
276                 break
277             fi
278         done
279         rm -f "$pidfile"
280     fi
281 }
282
283 function restart_approval {
284     if test ! -t 0; then
285         # Don't prompt if invoked non-interactively.
286         return 0
287     fi
288     cat <<EOF
289
290 WARNING!!!
291
292 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
293 provided as a convenience for those situations in which it does work.
294
295 EOF
296     read -s -r -n 1 -p "Continue with restart (y/N): " response
297     printf "\n"
298     case "$response" in
299         y|Y)
300             return 0
301             ;;
302         *)
303             return 1
304             ;;
305     esac
306 }
307
308 function set_system_ids {
309     if [ -f /etc/xensource-inventory ]; then
310         OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
311         action "Configuring Open vSwitch system IDs" true
312         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
313             ovs-version="$OVS_VERSION" \
314             system-type="$PRODUCT_BRAND" \
315             system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
316             external-ids:system-id="$INSTALLATION_UUID" \
317             external-ids:xs-system-uuid="$INSTALLATION_UUID"
318     else
319         action "Configuring Open vSwitch system IDs" false
320     fi
321 }
322
323 function start {
324     if [ "$FORCE_COREFILES" = "y" ]; then
325         turn_on_corefiles
326     fi
327
328     insert_modules_if_required
329
330     # Increase the limit on the number of open file descriptors since
331     # ovs-vswitchd needs a few per bridge
332     ulimit -n 4096
333
334     # Allow GRE traffic.
335     iptables -I INPUT -p gre -j ACCEPT
336
337     schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
338     if [ ! -e "$OVSDB_SERVER_DB" ]; then
339         warning "$OVSDB_SERVER_DB does not exist"
340         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
341
342         action "Creating empty database $OVSDB_SERVER_DB" true
343         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
344     else
345         # If schema version changed, then back up the old version.
346         oldver=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
347         if test "X$oldver" != "X$schemaver"; then
348             backup=$OVSDB_SERVER_DB.backup$oldver
349             action "Backing up $OVSDB_SERVER_DB in $backup before converting from schema version \"$oldver\" to \"$schemaver\"" true
350             cp "$OVSDB_SERVER_DB" "$backup"
351         fi
352
353         # Upgrade or downgrade schema and compact database.
354         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
355     fi
356
357     start_ovsdb_server
358     $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
359     if [ ! -e /var/run/openvswitch.booted ]; then
360         touch /var/run/openvswitch.booted
361         for bridge in $($vsctl list-br); do
362             $vsctl --no-wait --timeout=5 del-br $bridge
363         done
364     fi
365
366     set_system_ids
367
368     start_vswitchd
369     if [ "${ENABLE_BRCOMPAT}" = "y" ] ; then
370         start_brcompatd
371     fi
372
373     # Start daemon to monitor external ids
374     PYTHONPATH=/usr/share/openvswitch/python \
375                /usr/share/openvswitch/scripts/ovs-external-ids \
376                --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
377
378     touch /var/lock/subsys/openvswitch
379 }
380
381 function stop {
382     stop_daemon BRCOMPATD "$brcompatd"
383     stop_daemon VSWITCHD "$vswitchd"
384     stop_daemon OVSDB_SERVER "$ovsdb_server"
385     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
386         kill `cat /var/run/openvswitch/ovs-external-ids.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 case "$1" in
399     start)
400         start
401         ;;
402     stop)
403         stop
404         ;;
405     restart)
406         restart
407         ;;
408     reload|force-reload)
409         # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
410         # configuration up-to-date all the time.  HUP ovs-external-ids so it
411         # re-runs.
412         hup_monitor_external_ids
413         ;;
414     strace-vswitchd)
415         shift
416         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
417         ;;
418     strace-brcompatd)
419         shift
420         strace -p $(cat "$BRCOMPATD_PIDFILE") "$@"
421         ;;
422     status)
423         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
424         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd &&
425         (test "$ENABLE_BRCOMPAT" != "y" || 
426             status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd)
427         ;;
428     version)
429         /usr/sbin/ovsdb-server -V
430         /usr/sbin/ovs-vswitchd -V
431         /usr/sbin/ovs-brcompatd -V
432         ;;
433     help)
434         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
435         ;;
436     *)
437         printf "Unknown command: $1\n"
438         exit 1
439         ;;
440 esac