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