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