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