#################### global vars
RUN_DIR=/var/run/openvswitch
-DB_CONF_FILE=/usr/etc/openvswitch/conf.db
+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_CTRL_SOCKET=/var/run/openvswitch/db-ctrl.sock
##
DB_SOCKET=/var/run/openvswitch/db.sock
##
ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
}
+function is_db_running {
+ ovs-appctl --target=$DB_CTRL_SOCKET version >& /dev/null
+}
+
function tapname () {
IP=$1; shift
echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
function wait_server () {
pid_file=$1; shift
- server_name="$@"; shift
+ server_name=$1; shift
+ timeout=$1; shift
+
+ expire=$(($(date +%s) + $timeout))
## wait for it to be up - xxx todo - could use a timeout of some kind
while [ ! -f "$pid_file" ]; do
- echo "Waiting for $server_name to start..." >&2
+ echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
sleep 1;
+ [ $(date +%s) -ge $expire ] && return 1
done
cat "$pid_file"
}
+function wait_device () {
+ tapname=$1; shift
+ timeout=$1; shift
+
+ expire=$(($(date +%s) + $timeout))
+
+ while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
+ echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
+ sleep 1
+ [ $(date +%s) -ge $expire ] && return 1
+ done
+ return 0
+}
+
######################################## startup
function start_db () {
--bootstrap-ca-cert=db:SSL,ca_cert \
--pidfile=$DB_PID_FILE \
--log-file=$DB_LOG \
+ --unixctl=$DB_CTRL_SOCKET \
--detach >& /dev/null
else
echo 'ovsdb-server appears to be running already, *not* starting'
fi
- wait_server $DB_PID_FILE ovsdb-server
+ wait_server $DB_PID_FILE ovsdb-server 30
}
function stop_db () { pkill ovsdb-server; }
[[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
+ # ensure ovsdb-server is running
+ is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
+
if [ ! -f "$SWITCH_PID_FILE" ] ; then
ovs-vswitchd \
--pidfile=$SWITCH_PID_FILE \
else
echo 'ovs-vswitchd appears to be running already, *not* starting'
fi
- wait_server $SWITCH_PID_FILE ovs-vswitchd
+ wait_server $SWITCH_PID_FILE ovs-vswitchd 30
}
function stop_switch () { pkill ovs-vswitchd ; }
# 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=unix:$DB_SOCKET br-exists "$TAPNAME"; then
echo $TAPNAME
exit 0
fi
$IP
$PREFIX
EOF
- while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
- echo "Waiting for $TAPNAME to come UP..." >&2
- sleep 1
- done
- ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
+ wait_device $TAPNAME 60 && \
+ ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
echo $TAPNAME
return 0
}
port=$1; shift
[[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
+ # ensure ovs-vswitchd is running
+ is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
+
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=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
+ ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
fi
ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
return 0
[[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
W=
- is_switch_running || W="--no-wait"
+ if ! is_switch_running; then
+ # we can delete the bridge even if ovs-vswitchd is not running,
+ # but we need a running ovsdb-server
+ is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+ W="--no-wait"
+ fi
- if ovs-vsctl br-exists "$bridge_name"; then
- ovs-vsctl $W del-br $bridge_name
+ if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
+ ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
fi
return 0
}
bridge_name=$1; shift
[[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
+ W=
+ if ! is_switch_running; then
+ # we can delete the port even if ovs-vswitchd is not running,
+ # but we need a running ovsdb-server
+ is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+ W="--no-wait"
+ fi
+
set -e
- if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
- ovs-vsctl del-port "$1"
+ if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$1" >/dev/null 2>&1; then
+ ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$1"
fi
return 0
}