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