288 lines
8.2 KiB
Bash
Executable File
288 lines
8.2 KiB
Bash
Executable File
#! /bin/sh
|
|
#
|
|
# /etc/init.d/openflow-switch
|
|
#
|
|
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
|
|
# Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
|
|
# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
|
|
# Modified for openflow-switch.
|
|
#
|
|
# Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl
|
|
#
|
|
### BEGIN INIT INFO
|
|
# Provides: openflow-switch
|
|
# Required-Start: $network $named $remote_fs $syslog
|
|
# Required-Stop:
|
|
# Default-Start: 2 3 4 5
|
|
# Default-Stop: 0 1 6
|
|
# Short-Description: OpenFlow switch
|
|
### END INIT INFO
|
|
|
|
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
|
DAEMON=/usr/sbin/secchan
|
|
NAME=secchan
|
|
DESC=secchan
|
|
|
|
test -x $DAEMON || exit 0
|
|
|
|
LOGDIR=/var/log/openflow
|
|
PIDFILE=/var/run/$NAME.pid
|
|
MGMTSOCK=/var/run/$NAME.socket
|
|
DHCLIENT_PIDFILE=/var/run/dhclient.of0.pid
|
|
DODTIME=1 # Time to wait for the server to die, in seconds
|
|
# If this value is set too low you might not
|
|
# let some servers to die gracefully and
|
|
# 'restart' will not work
|
|
|
|
# Include secchan defaults if available
|
|
default=/etc/default/openflow-switch
|
|
if [ -f $default ] ; then
|
|
. $default
|
|
fi
|
|
|
|
set -e
|
|
|
|
running_pid()
|
|
{
|
|
# Check if a given process pid's cmdline matches a given name
|
|
pid=$1
|
|
name=$2
|
|
[ -z "$pid" ] && return 1
|
|
[ ! -d /proc/$pid ] && return 1
|
|
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
|
|
# Is this the expected child?
|
|
case $cmd in
|
|
$name|*/$name)
|
|
return 0
|
|
;;
|
|
*)
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
running()
|
|
{
|
|
# Check if the process is running looking at /proc
|
|
# (works for all users)
|
|
|
|
# No pidfile, probably no daemon present
|
|
[ ! -f "$PIDFILE" ] && return 1
|
|
# Obtain the pid and check it against the binary name
|
|
pid=`cat $PIDFILE`
|
|
running_pid $pid $NAME || return 1
|
|
return 0
|
|
}
|
|
|
|
force_stop() {
|
|
# Forcefully kill the process
|
|
[ ! -f "$PIDFILE" ] && return
|
|
if running ; then
|
|
kill -15 $pid
|
|
# Is it really dead?
|
|
[ -n "$DODTIME" ] && sleep "$DODTIME"s
|
|
if running ; then
|
|
kill -9 $pid
|
|
[ -n "$DODTIME" ] && sleep "$DODTIME"s
|
|
if running ; then
|
|
echo "Cannot kill $LABEL (pid=$pid)!"
|
|
exit 1
|
|
fi
|
|
fi
|
|
fi
|
|
rm -f $PIDFILE
|
|
return 0
|
|
}
|
|
|
|
must_succeed() {
|
|
echo -n "$1: "
|
|
shift
|
|
if "$@"; then
|
|
echo "success."
|
|
else
|
|
echo " ERROR."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
check_op() {
|
|
echo -n "$1: "
|
|
shift
|
|
if "$@"; then
|
|
echo "success."
|
|
else
|
|
echo " ERROR."
|
|
fi
|
|
}
|
|
|
|
configure_ssl() {
|
|
if test ! -e "$PRIVKEY" || test ! -e "$CERT" || test ! -e "$CACERT"; then
|
|
if test ! -e "$PRIVKEY"; then
|
|
echo "$PRIVKEY: private key missing" >&2
|
|
fi
|
|
if test ! -e "$CERT"; then
|
|
echo "$CERT: certificate for private key missing" >&2
|
|
fi
|
|
if test ! -e "$CACERT"; then
|
|
echo "$CACERT: CA certificate missing" >&2
|
|
fi
|
|
echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
|
|
if test "$MODE" = discovery; then
|
|
echo "You may also delete or rename $PRIVKEY to disable SSL requirement" >&2
|
|
fi
|
|
exit 1
|
|
fi
|
|
SSL_OPTS="--private-key=$PRIVKEY --certificate=$CERT --ca-cert=$CACERT"
|
|
}
|
|
|
|
case "$1" in
|
|
start)
|
|
if test -z "$NETDEVS"; then
|
|
echo "$default: No network devices configured, switch disabled" >&2
|
|
echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
|
|
exit 0
|
|
fi
|
|
if test "$MODE" = discovery; then
|
|
unset CONTROLLER
|
|
elif test "$MODE" = in-band || test "$MODE" = out-of-band; then
|
|
if test -z "$CONTROLLER"; then
|
|
echo "$default: No controller configured and not configured for discovery, switch disabled" >&2
|
|
echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
|
|
exit 0
|
|
fi
|
|
else
|
|
echo "$default: MODE must set to 'discovery', 'in-band', or 'out-of-band'" >&2
|
|
echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
|
|
exit 1
|
|
fi
|
|
: ${PRIVKEY:=/etc/openflow-switch/of0-privkey.pem}
|
|
: ${CERT:=/etc/openflow-switch/of0-cert.pem}
|
|
: ${CACERT:=/etc/openflow-switch/cacert.pem}
|
|
case $CONTROLLER in
|
|
'')
|
|
# Discovery mode.
|
|
if test -e "$PRIVKEY"; then
|
|
configure_ssl
|
|
fi
|
|
;;
|
|
tcp:*)
|
|
;;
|
|
ssl:*)
|
|
configure_ssl
|
|
;;
|
|
*)
|
|
echo "$default: CONTROLLER must be in the form 'ssl:HOST[:PORT]' or 'tcp:HOST[:PORT]' when not in discovery mode" >&2
|
|
echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
|
|
exit 1
|
|
esac
|
|
|
|
echo -n "Loading openflow_mod: "
|
|
if modprobe openflow_mod; then
|
|
echo "success."
|
|
else
|
|
echo " ERROR."
|
|
echo "openflow_mod has probably not been built for this kernel."
|
|
if ! test -d /usr/share/doc/openflow-datapath-source; then
|
|
echo "Install the openflow-datapath-source package, then read"
|
|
echo "/usr/share/doc/openflow-datapath-source/README.Debian"
|
|
else
|
|
echo "For instructions, read"
|
|
echo "/usr/share/doc/openflow-datapath-source/README.Debian"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
must_succeed "Adding datapath" dpctl adddp nl:0
|
|
for netdev in $NETDEVS; do
|
|
must_succeed "Adding $netdev to datapath" dpctl addif nl:0 $netdev
|
|
done
|
|
|
|
if test "$MODE" = in-band; then
|
|
if test "$SWITCH_IP" = dhcp; then
|
|
must_succeed "Temporarily disabling of0" ifconfig of0 down
|
|
else
|
|
must_succeed "Configuring of0 as $SWITCH_IP" ifconfig of0 $SWITCH_IP
|
|
fi
|
|
else
|
|
must_succeed "Disabling of0" ifconfig of0 down
|
|
fi
|
|
|
|
echo -n "Starting $DESC: "
|
|
start-stop-daemon --start --quiet --pidfile $PIDFILE \
|
|
--exec $DAEMON -- nl:0 $CONTROLLER --detach --pidfile=$PIDFILE \
|
|
--verbose=ANY:console:emer --listen=punix:$MGMTSOCK \
|
|
$DAEMON_OPTS $SSL_OPTS
|
|
if running; then
|
|
echo "$NAME."
|
|
else
|
|
echo " ERROR."
|
|
fi
|
|
|
|
if test "$MODE" = in-band && test "$SWITCH_IP" = dhcp; then
|
|
echo -n "Starting dhclient on of0: "
|
|
start-stop-daemon --start --quiet --pidfile $DHCLIENT_PIDFILE \
|
|
--exec /sbin/dhclient -- -q -pf $DHCLIENT_PIDFILE of0
|
|
if running; then
|
|
echo "dhclient."
|
|
else
|
|
echo " ERROR."
|
|
fi
|
|
fi
|
|
;;
|
|
stop)
|
|
if test -e /var/run/dhclient.of0.pid; then
|
|
echo -n "Stopping dhclient on of0: "
|
|
start-stop-daemon --stop --quiet --oknodo \
|
|
--pidfile $DHCLIENT_PIDFILE --exec /sbin/dhclient
|
|
echo "dhclient."
|
|
fi
|
|
|
|
echo -n "Stopping $DESC: "
|
|
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
|
|
--exec $DAEMON
|
|
echo "$NAME."
|
|
|
|
for netdev in $NETDEVS; do
|
|
check_op "Removing $netdev from datapath" dpctl delif nl:0 $netdev
|
|
done
|
|
check_op "Deleting datapath" dpctl deldp nl:0
|
|
;;
|
|
force-stop)
|
|
echo -n "Forcefully stopping $DESC: "
|
|
force_stop
|
|
if ! running; then
|
|
echo "$NAME."
|
|
else
|
|
echo " ERROR."
|
|
fi
|
|
;;
|
|
reload)
|
|
;;
|
|
force-reload)
|
|
start-stop-daemon --stop --test --quiet --pidfile \
|
|
$PIDFILE --exec $DAEMON \
|
|
&& $0 restart \
|
|
|| exit 0
|
|
;;
|
|
restart)
|
|
$0 stop || true
|
|
$0 start
|
|
;;
|
|
status)
|
|
echo -n "$LABEL is "
|
|
if running ; then
|
|
echo "running"
|
|
else
|
|
echo " not running."
|
|
exit 1
|
|
fi
|
|
;;
|
|
*)
|
|
N=/etc/init.d/$NAME
|
|
echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
exit 0
|