Simplify code in stream_recv().
[sliver-openvswitch.git] / debian / openflow-switch.init
1 #! /bin/sh
2 #
3 # /etc/init.d/openflow-switch
4 #
5 # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
6 # Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
7 # Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
8 # Modified for openflow-switch.
9 #
10 # Version:      @(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
11 #
12 ### BEGIN INIT INFO
13 # Provides:          openflow-switch
14 # Required-Start:    $network $named $remote_fs $syslog
15 # Required-Stop:
16 # Default-Start:     2 3 4 5
17 # Default-Stop:      0 1 6
18 # Short-Description: OpenFlow switch
19 ### END INIT INFO
20
21 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
22 DAEMON=/usr/sbin/secchan
23 NAME=secchan
24 DESC=secchan
25
26 test -x $DAEMON || exit 0
27
28 LOGDIR=/var/log/openflow
29 PIDFILE=/var/run/$NAME.pid
30 DHCLIENT_PIDFILE=/var/run/dhclient.of0.pid
31 DODTIME=1                   # Time to wait for the server to die, in seconds
32                             # If this value is set too low you might not
33                             # let some servers to die gracefully and
34                             # 'restart' will not work
35
36 # Include secchan defaults if available
37 unset NETDEVS
38 unset MODE
39 unset SWITCH_IP
40 unset CONTROLLER
41 unset PRIVKEY
42 unset CERT
43 unset CACERT
44 unset CACERT_MODE
45 unset MGMT_VCONNS
46 unset COMMANDS
47 unset DAEMON_OPTS
48 unset CORE_LIMIT
49 default=/etc/default/openflow-switch
50 if [ -f $default ] ; then
51         . $default
52 fi
53
54 set -e
55
56 running_pid()
57 {
58     # Check if a given process pid's cmdline matches a given name
59     pid=$1
60     name=$2
61     [ -z "$pid" ] && return 1 
62     [ ! -d /proc/$pid ] &&  return 1
63     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
64     # Is this the expected child?
65     case $cmd in
66         $name|*/$name)
67             return 0
68             ;;
69         *)
70             return 1
71             ;;
72     esac
73 }
74
75 running()
76 {
77 # Check if the process is running looking at /proc
78 # (works for all users)
79
80     # No pidfile, probably no daemon present
81     [ ! -f "$PIDFILE" ] && return 1
82     # Obtain the pid and check it against the binary name
83     pid=`cat $PIDFILE`
84     running_pid $pid $NAME || return 1
85     return 0
86 }
87
88 force_stop() {
89 # Forcefully kill the process
90     [ ! -f "$PIDFILE" ] && return
91     if running ; then
92         kill -15 $pid
93         # Is it really dead?
94         [ -n "$DODTIME" ] && sleep "$DODTIME"s
95         if running ; then
96             kill -9 $pid
97             [ -n "$DODTIME" ] && sleep "$DODTIME"s
98             if running ; then
99                 echo "Cannot kill $NAME (pid=$pid)!"
100                 exit 1
101             fi
102         fi
103     fi
104     rm -f $PIDFILE
105     return 0
106 }
107
108 must_succeed() {
109     echo -n "$1: "
110     shift
111     if "$@"; then
112         echo "success."
113     else
114         echo " ERROR."
115         exit 1
116     fi
117 }
118
119 check_op() {
120     echo -n "$1: "
121     shift
122     if "$@"; then
123         echo "success."
124     else
125         echo " ERROR."
126     fi
127 }
128
129 configure_ssl() {
130     if (test "$CACERT_MODE" != secure && test "$CACERT_MODE" != bootstrap) \
131        || test ! -e "$PRIVKEY" || test ! -e "$CERT" \
132        || (test ! -e "$CACERT" && test "$CACERT_MODE" != bootstrap); then
133         if test "$CACERT_MODE" != secure && test "$CACERT_MODE" != bootstrap
134         then
135             echo "CACERT_MODE is not set to 'secure' or 'bootstrap'"
136         fi
137         if test ! -e "$PRIVKEY"; then
138             echo "$PRIVKEY: private key missing" >&2
139         fi
140         if test ! -e "$CERT"; then
141             echo "$CERT: certificate for private key missing" >&2
142         fi
143         if test ! -e "$CACERT" && test "$CACERT_MODE" != bootstrap; then
144             echo "$CACERT: CA certificate missing (and CA certificate bootstrapping not enabled)" >&2
145         fi
146         echo "Run ofp-switch-setup (in the openflow-switch-config package) or edit /etc/default/openflow-switch to configure" >&2
147         if test "$MODE" = discovery; then
148             echo "You may also delete or rename $PRIVKEY to disable SSL requirement" >&2
149         fi
150         exit 1
151     fi
152
153     SSL_OPTS="--private-key=$PRIVKEY --certificate=$CERT"
154     if test ! -e "$CACERT" && test "$CACERT_MODE" = bootstrap; then
155         SSL_OPTS="$SSL_OPTS --bootstrap-ca-cert=$CACERT"
156     else
157         SSL_OPTS="$SSL_OPTS --ca-cert=$CACERT"
158     fi
159 }
160
161 case "$1" in
162     start)
163         if test -z "$NETDEVS"; then
164             echo "$default: No network devices configured, switch disabled" >&2
165             echo "Run ofp-switch-setup (in the openflow-switch-config package) or edit /etc/default/openflow-switch to configure" >&2
166             exit 0
167         fi
168         if test "$MODE" = discovery; then
169             unset CONTROLLER
170         elif test "$MODE" = in-band || test "$MODE" = out-of-band; then
171             if test -z "$CONTROLLER"; then
172                 echo "$default: No controller configured and not configured for discovery, switch disabled" >&2
173                 echo "Run ofp-switch-setup (in the openflow-switch-config package) or edit /etc/default/openflow-switch to configure" >&2
174                 exit 0
175             fi
176         else
177             echo "$default: MODE must set to 'discovery', 'in-band', or 'out-of-band'" >&2
178             echo "Run ofp-switch-setup (in the openflow-switch-config package) or edit /etc/default/openflow-switch to configure" >&2
179             exit 1
180         fi
181         : ${PRIVKEY:=/etc/openflow-switch/of0-privkey.pem}
182         : ${CERT:=/etc/openflow-switch/of0-cert.pem}
183         : ${CACERT:=/etc/openflow-switch/cacert.pem}
184         case $CONTROLLER in
185             '')
186                 # Discovery mode.
187                 if test -e "$PRIVKEY"; then
188                     configure_ssl
189                 fi
190                 ;;
191             tcp:*)
192                 ;;
193             ssl:*)
194                 configure_ssl
195                 ;;
196             *)
197                 echo "$default: CONTROLLER must be in the form 'ssl:HOST[:PORT]' or 'tcp:HOST[:PORT]' when not in discovery mode" >&2
198                 echo "Run ofp-switch-setup (in the openflow-switch-config package) or edit /etc/default/openflow-switch to configure" >&2
199                 exit 1
200         esac
201
202         echo -n "Loading openflow_mod: "
203         if grep -q '^openflow_mod$' /proc/modules; then
204             echo "already loaded, nothing to do."
205         elif modprobe openflow_mod; then
206             echo "success."
207         else
208             echo "ERROR."
209             echo "openflow_mod has probably not been built for this kernel."
210             if ! test -d /usr/share/doc/openflow-datapath-source; then
211                 echo "Install the openflow-datapath-source package, then read"
212                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
213             else
214                 echo "For instructions, read"
215                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
216             fi
217             exit 1
218         fi
219
220         must_succeed "Adding datapath" dpctl adddp nl:0
221         for netdev in $NETDEVS; do
222             must_succeed "Adding $netdev to datapath" dpctl addif nl:0 $netdev
223         done
224
225         if test "$MODE" = in-band; then
226             if test "$SWITCH_IP" = dhcp; then
227                 must_succeed "Temporarily disabling of0" ifconfig of0 down
228             else
229                 COMMAND="ifconfig of0 $SWITCH_IP"
230                 if test -n "$SWITCH_NETMASK"; then
231                     COMMAND="$COMMAND netmask $SWITCH_NETMASK"
232                 fi
233                 must_succeed "Configuring of0: $COMMAND" $COMMAND
234                 if test -n "$SWITCH_GATEWAY"; then
235                     # This can fail because the route already exists,
236                     # so we don't insist that it succeed.
237                     COMMAND="route add default gw $SWITCH_GATEWAY"
238                     check_op "Adding default route: $COMMAND" $COMMAND
239                 fi
240             fi
241         else
242             must_succeed "Disabling of0" ifconfig of0 down
243         fi
244
245         MGMT_OPTS=
246         for vconn in $MGMT_VCONNS; do
247             MGMT_OPTS="$MGMT_OPTS --listen=$vconn"
248         done
249
250         MONITOR_OPT=
251         if test -n "$MONITOR_VCONN"; then
252             MONITOR_OPT="--monitor=$MONITOR_VCONN"
253         fi
254
255         COMMAND_OPT=
256         if test -n "$COMMANDS"; then
257             COMMAND_OPT="--command-acl=$COMMANDS"
258         fi
259
260         if test "$MODE" = out-of-band; then
261             DAEMON_OPTS="$DAEMON_OPTS --out-of-band"
262         fi
263
264         if test -n "$CORE_LIMIT"; then
265             check_op "Setting core limit to $CORE_LIMIT" ulimit -c "$CORE_LIMIT"
266         fi
267
268         echo -n "Starting $DESC: "
269         start-stop-daemon --start --quiet --pidfile $PIDFILE \
270             --exec $DAEMON -- nl:0 $CONTROLLER --detach --pidfile=$PIDFILE \
271             --verbose=ANY:console:emer --verbose=ANY:syslog:err --log-file \
272             $DAEMON_OPTS $MGMT_OPTS $MONITOR_OPT $SSL_OPTS "$COMMAND_OPT"
273         if running; then
274             echo "$NAME."
275         else
276             echo " ERROR."
277         fi
278
279         if test "$MODE" = in-band && test "$SWITCH_IP" = dhcp; then
280             echo -n "Starting dhclient on of0: "
281             start-stop-daemon --start --quiet --pidfile $DHCLIENT_PIDFILE \
282                 --exec /sbin/dhclient -- -q -pf $DHCLIENT_PIDFILE of0
283             if running; then
284                 echo "dhclient."
285             else
286                 echo " ERROR."
287             fi
288         fi
289         ;;
290     stop)
291         if test -e /var/run/dhclient.of0.pid; then
292             echo -n "Stopping dhclient on of0: "
293             start-stop-daemon --stop --quiet --oknodo \
294                 --pidfile $DHCLIENT_PIDFILE --exec /sbin/dhclient
295             echo "dhclient."
296         fi            
297
298         echo -n "Stopping $DESC: "
299         start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
300             --exec $DAEMON
301         echo "$NAME."
302
303         for netdev in $NETDEVS; do
304             check_op "Removing $netdev from datapath" dpctl delif nl:0 $netdev
305         done
306         check_op "Deleting datapath" dpctl deldp nl:0
307         check_op "Unloading kernel module" modprobe -r openflow_mod
308         ;;
309     force-stop)
310         echo -n "Forcefully stopping $DESC: "
311         force_stop
312         if ! running; then
313             echo "$NAME."
314         else
315             echo " ERROR."
316         fi
317         ;;
318     reload)
319         ;;
320     force-reload)
321         start-stop-daemon --stop --test --quiet --pidfile \
322             $PIDFILE --exec $DAEMON \
323             && $0 restart \
324             || exit 0
325         ;;
326     restart)
327         $0 stop || true
328         $0 start
329         ;;
330     status)
331         echo -n "$NAME is "
332         if running ;  then
333             echo "running"
334         else
335             echo " not running."
336             exit 1
337         fi
338         ;;
339     *)
340         N=/etc/init.d/$NAME
341         echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
342         exit 1
343         ;;
344 esac
345
346 exit 0