74ec2699be6f0f531c9522492873018ac03d2011
[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 default=/etc/default/openflow-switch
38 if [ -f $default ] ; then
39         . $default
40 fi
41
42 set -e
43
44 running_pid()
45 {
46     # Check if a given process pid's cmdline matches a given name
47     pid=$1
48     name=$2
49     [ -z "$pid" ] && return 1 
50     [ ! -d /proc/$pid ] &&  return 1
51     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
52     # Is this the expected child?
53     [ "$cmd" != "$name" ] &&  return 1
54     return 0
55 }
56
57 running()
58 {
59 # Check if the process is running looking at /proc
60 # (works for all users)
61
62     # No pidfile, probably no daemon present
63     [ ! -f "$PIDFILE" ] && return 1
64     # Obtain the pid and check it against the binary name
65     pid=`cat $PIDFILE`
66     running_pid $pid $NAME || return 1
67     return 0
68 }
69
70 force_stop() {
71 # Forcefully kill the process
72     [ ! -f "$PIDFILE" ] && return
73     if running ; then
74         kill -15 $pid
75         # Is it really dead?
76         [ -n "$DODTIME" ] && sleep "$DODTIME"s
77         if running ; then
78             kill -9 $pid
79             [ -n "$DODTIME" ] && sleep "$DODTIME"s
80             if running ; then
81                 echo "Cannot kill $LABEL (pid=$pid)!"
82                 exit 1
83             fi
84         fi
85     fi
86     rm -f $PIDFILE
87     return 0
88 }
89
90 must_succeed() {
91     echo -n "$1: "
92     shift
93     if "$@"; then
94         echo "success."
95     else
96         echo " ERROR."
97         exit 1
98     fi
99 }
100
101 check_op() {
102     echo -n "$1: "
103     shift
104     if "$@"; then
105         echo "success."
106     else
107         echo " ERROR."
108     fi
109 }
110
111 configure_ssl() {
112     if test ! -e "$PRIVKEY" || test ! -e "$CERT" || test ! -e "$CACERT"; then
113         if test ! -e "$PRIVKEY"; then
114             echo "$PRIVKEY: private key missing" >&2
115         fi
116         if test ! -e "$CERT"; then
117             echo "$CERT: certificate for private key missing" >&2
118         fi
119         if test ! -e "$CACERT"; then
120             echo "$CACERT: CA certificate missing" >&2
121         fi
122         echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
123         if test "$MODE" = discovery; then
124             echo "You may also delete or rename $PRIVKEY to disable SSL requirement" >&2
125         fi
126         exit 1
127     fi
128     SSL_OPTS="--private-key=$PRIVKEY --certificate=$CERT --ca-cert=$CACERT"
129 }
130
131 case "$1" in
132     start)
133         if test -z "$NETDEVS"; then
134             echo "$default: No network devices configured, switch disabled" >&2
135             echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
136             exit 0
137         fi
138         if test "$MODE" = discovery; then
139             unset CONTROLLER
140         elif test "$MODE" = in-band || test "$MODE" = out-of-band; then
141             if test -z "$CONTROLLER"; then
142                 echo "$default: No controller configured and not configured for discovery, switch disabled" >&2
143                 echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
144                 exit 0
145             fi
146         else
147             echo "$default: MODE must set to 'discovery', 'in-band', or 'out-of-band'" >&2
148             echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
149             exit 1
150         fi
151         : ${PRIVKEY:=/etc/openflow-switch/of0-privkey.pem}
152         : ${CERT:=/etc/openflow-switch/of0-cert.pem}
153         : ${CACERT:=/etc/openflow-switch/cacert.pem}
154         case $CONTROLLER in
155             '')
156                 # Discovery mode.
157                 if test -e "$PRIVKEY"; then
158                     configure_ssl
159                 fi
160                 ;;
161             tcp:*)
162                 ;;
163             ssl:*)
164                 configure_ssl
165                 ;;
166             *)
167                 echo "$default: CONTROLLER must be in the form 'ssl:HOST[:PORT]' or 'tcp:HOST[:PORT]' when not in discovery mode" >&2
168                 echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
169                 exit 1
170         esac
171
172         echo -n "Loading openflow_mod: "
173         if modprobe openflow_mod; then
174             echo "success."
175         else
176             echo " ERROR."
177             echo "openflow_mod has probably not been built for this kernel."
178             if ! test -d /usr/share/doc/openflow-datapath-source; then
179                 echo "Install the openflow-datapath-source package, then read"
180                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
181             else
182                 echo "For instructions, read"
183                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
184             fi
185             exit 1
186         fi
187
188         must_succeed "Adding datapath" dpctl adddp nl:0
189         for netdev in $NETDEVS; do
190             must_succeed "Adding $netdev to datapath" dpctl addif nl:0 $netdev
191         done
192
193         if test "$MODE" = in-band; then
194             if test "$SWITCH_IP" = dhcp; then
195                 must_succeed "Temporarily disabling of0" ifconfig of0 down
196             else
197                 must_succeed "Configuring of0 as $SWITCH_IP" ifconfig of0 $SWITCH_IP
198             fi
199         else
200             must_succeed "Disabling of0" ifconfig of0 down
201         fi
202
203         echo -n "Starting $DESC: "
204         start-stop-daemon --start --quiet --pidfile $PIDFILE \
205             --exec $DAEMON -- nl:0 $CONTROLLER --detach --pidfile=$PIDFILE \
206             $DAEMON_OPTS $SSL_OPTS
207         if running; then
208             echo "$NAME."
209         else
210             echo " ERROR."
211         fi
212
213         if test "$MODE" = in-band && test "$SWITCH_IP" = dhcp; then
214             echo -n "Starting dhclient on of0: "
215             start-stop-daemon --start --quiet --pidfile $DHCLIENT_PIDFILE \
216                 --exec /sbin/dhclient -- -q -pf $DHCLIENT_PIDFILE of0
217             if running; then
218                 echo "dhclient."
219             else
220                 echo " ERROR."
221             fi
222         fi
223         ;;
224     stop)
225         if test -e /var/run/dhclient.of0.pid; then
226             echo -n "Stopping dhclient on of0: "
227             start-stop-daemon --stop --quiet --oknodo \
228                 --pidfile $DHCLIENT_PIDFILE --exec /sbin/dhclient
229             echo "dhclient."
230         fi            
231
232         echo -n "Stopping $DESC: "
233         start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
234             --exec $DAEMON
235         echo "$NAME."
236
237         for netdev in $NETDEVS; do
238             check_op "Removing $netdev from datapath" dpctl delif nl:0 $netdev
239         done
240         check_op "Deleting datapath" dpctl deldp nl:0
241         ;;
242     force-stop)
243         echo -n "Forcefully stopping $DESC: "
244         force_stop
245         if ! running; then
246             echo "$NAME."
247         else
248             echo " ERROR."
249         fi
250         ;;
251     reload)
252         ;;
253     force-reload)
254         start-stop-daemon --stop --test --quiet --pidfile \
255             $PIDFILE --exec $DAEMON \
256             && $0 restart \
257             || exit 0
258         ;;
259     restart)
260         $0 stop || true
261         $0 start
262         ;;
263     status)
264         echo -n "$LABEL is "
265         if running ;  then
266             echo "running"
267         else
268             echo " not running."
269             exit 1
270         fi
271         ;;
272     *)
273         N=/etc/init.d/$NAME
274         echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
275         exit 1
276         ;;
277 esac
278
279 exit 0