COMMAND=$0
+#################### global vars
+RUN_DIR=/var/run/openvswitch
+DB_CONF_FILE=/etc/openvswitch/conf.db
+DB_SCHEMA=/usr/share/openvswitch/vswitch.ovsschema
+DB_PID_FILE=/var/run/openvswitch/db.pid
+DB_LOG=/var/log/ovs-db.log
+##
+DB_SOCKET=/var/run/openvswitch/db.sock
+##
+SWITCH_PID_FILE=/var/run/openvswitch/switch.pid
+SWITCH_LOG=/var/log/ovs-switch.log
+SWITCH_SOCKET=/var/run/openvswitch/switch.sock
+
#################### helper functions
function kill_pltap_ovs () {
}
function is_switch_running {
- ovs-appctl version >/dev/null 2>&1
+ ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
}
function tapname () {
server_name="$@"; shift
## wait for it to be up - xxx todo - could use a timeout of some kind
- while [ ! -f "pid_file" ]; do
+ while [ ! -f "$pid_file" ]; do
echo "Waiting for $server_name to start..." >&2
sleep 1;
done
- cat "$PID_FILE"
+ cat "$pid_file"
+}
+
+######################################## startup
+function start_db () {
+
+ [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
+
+ ## init conf
+ conf_dir=$(dirname $DB_CONF_FILE)
+ [ -d $conf_dir ] || mkdir -p $conf_dir
+ [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
+
+ ## init run
+ [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
+
+ ## check
+ [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
+ [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
+
+ ## run the stuff
+ if [ ! -f "$DB_PID_FILE" ]; then
+ ovsdb-server --remote=punix:$DB_SOCKET \
+ --remote=db:Open_vSwitch,manager_options \
+ --private-key=db:SSL,private_key \
+ --certificate=db:SSL,certificate \
+ --bootstrap-ca-cert=db:SSL,ca_cert \
+ --pidfile=$DB_PID_FILE \
+ --log-file=$DB_LOG \
+ --detach >& /dev/null
+ else
+ echo 'ovsdb-server appears to be running already, *not* starting'
+ fi
+ wait_server $DB_PID_FILE ovsdb-server
+}
+
+function stop_db () { pkill ovsdb-server; }
+
+
+function start_switch () {
+
+ [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
+
+ if [ ! -f "$SWITCH_PID_FILE" ] ; then
+ ovs-vswitchd \
+ --pidfile=$SWITCH_PID_FILE \
+ --log-file=$SWITCH_LOG \
+ --unixctl=$SWITCH_SOCKET \
+ --detach \
+ unix:$DB_SOCKET >& /dev/null
+ else
+ echo 'ovs-vswitchd appears to be running already, *not* starting'
+ fi
+ wait_server $SWITCH_PID_FILE ovs-vswitchd
+}
+
+function stop_switch () { pkill ovs-vswitchd ; }
+
+function status () {
+ pids=$(pgrep '^ovs')
+ [ -n "$pids" ] && ps $pids
}
+function start () {
+ start_db
+ start_switch
+}
+
+function stop () {
+ stop_switch
+ stop_db
+}
+#################### create functions
function create_bridge () {
[[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
# check whether the address is already assigned
TAPNAME=$(tapname $IP)
if [ ! -z "$TAPNAME" ]; then
- if ovs-vsctl br-exists "$TAPNAME"; then
+ if ovs-vsctl --db=$DB_SOCKET br-exists "$TAPNAME"; then
echo $TAPNAME
exit 0
fi
echo "Waiting for $TAPNAME to come UP..." >&2
sleep 1
done
- ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
+ ovs-vsctl --db=$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
echo $TAPNAME
return 0
}
[[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
set -e
- if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
- ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
+ if ! ovs-vsctl --db=$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
+ ovs-vsctl --db=$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
fi
- ovs-appctl netdev-tunnel/get-port "$port"
+ ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
return 0
}
-
+#################### del functions
function del_bridge () {
[[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
W=
is_switch_running || W="--no-wait"
- if ovs-vsctl br-exists "$bridge_name"; then
- ovs-vsctl $W del-br $bridge_name
+ if ovs-vsctl --db=$DB_SOCKET br-exists "$bridge_name"; then
+ ovs-vsctl --db=$DB_SOCKET $W del-br $bridge_name
fi
return 0
}
[[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
set -e
- if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
- ovs-vsctl del-port "$1"
+ if ovs-vsctl --db=$DB_SOCKET port-to-br "$1" >/dev/null 2>&1; then
+ ovs-vsctl --db=$DB_SOCKET del-port "$1"
fi
return 0
}
-
-CONF_FILE=/usr/etc/openvswitch/conf.db
-RUN_DIR=/usr/var/run/openvswitch
-DB_PID_FILE=/usr/var/run/openvswitch/ovsdb-server.pid
-
-function start_db () {
-
- [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
-
- ## init conf
- conf_dir=$(dirname $CONF_FILE)
- [ -d $conf_dir ] || mkdir -p $conf_dir
- [ -f $CONF_FILE ] || ovsdb-tool create $CONF_FILE /usr/share/openvswitch/vswitch.ovsschema
-
- ## init run
- [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
-
- ## check
- [ -f $CONF_FILE ] || { echo "Could not initialize $CONF_FILE - exiting" ; exit 1 ; }
- [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
-
- ## run the stuff
- if [ ! -f "$DB_PID_FILE" ]; then
- ovsdb-server --remote=punix:/usr/var/run/openvswitch/db.sock \
- --remote=db:Open_vSwitch,manager_options \
- --private-key=db:SSL,private_key \
- --certificate=db:SSL,certificate \
- --bootstrap-ca-cert=db:SSL,ca_cert \
- --pidfile=$DB_PID_FILE --detach
- fi
- pid=$(wait_server $DB_PID_FILE ovsdb-server)
- echo $pid
-}
-
-SWITCH_PID_FILE=/usr/var/run/openvswitch/ovs-vswitchd.pid
-
-function start_switch () {
-
- [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
-
- [ -f "$SWITCH_PID_FILE" ] || ovs-vswitchd --pidfile=$SWITCH_PID_FILE --detach --log-file >/dev/null
- wait_server $SWITCH_PID_FILE
-}
-
-SUBCOMMANDS="create_bridge create_port del_bridge del_port start_db start_switch"
+####################
+SUPPORTED_SUBCOMMANDS="start stop status
+start_db stop_db start_switch stop_switch
+create_bridge create_port del_bridge del_port"
function main () {
message="Usage: $COMMAND <subcommand> ...
Supported subcommands are (dash or underscore is the same):
-$SUBCOMMANDS"
+$SUPPORTED_SUBCOMMANDS"
[[ -z "$@" ]] && error "$message"
subcommand=$1; shift
# support dashes instead of underscores
subcommand=$(echo $subcommand | sed -e s,-,_,)
found=""
- for supported in $SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes ; echo sc=X${subcommand}X sup=Y${supported}Y; done
- echo subcommand=$subcommand
- echo found=$found
+ for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
[ -z "$found" ] && error $message