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_CTL_PATTERN='ovsdb-server.*.ctl'
##
DB_SOCKET=/var/run/openvswitch/db.sock
##
exit 1
}
+function get_params {
+ params=$1; shift
+ err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
+ for p in $(echo $params); do
+ [[ -z "$@" ]] && error "$err_msg"
+ pname=$(echo -n $p|perl -pe 's/\W/_/g')
+ eval $pname="$1"; shift
+ done
+ [[ -n "$@" ]] && error "$err_msg"
+}
+
function is_switch_running {
ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
}
######################################## startup
function start_db () {
-
- [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
+ get_params "" "$@"
## init conf
conf_dir=$(dirname $DB_CONF_FILE)
echo 'ovsdb-server appears to be running already, *not* starting'
fi
wait_server $DB_PID_FILE ovsdb-server 30
+ echo $DB_PID_FILE
}
-function stop_db () { pkill ovsdb-server; }
-
-
function start_switch () {
-
- [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
+ get_params "" "$@"
# ensure ovsdb-server is running
is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
wait_server $SWITCH_PID_FILE ovs-vswitchd 30
}
-function stop_switch () { pkill ovs-vswitchd ; }
+# first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
+# quick and dirty : we locate the control file through a search in /var/run
+# caller should be requested to remember and provide this pid instead
+function stop_db () {
+ controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
+ [ -f $controlfile ] && ovs-appctl --target=$controlfile exit
+}
+
+function stop_switch () {
+ ovs-appctl --target=$SWITCH_SOCKET exit || :
+}
function status () {
pids=$(pgrep '^ovs')
#################### create functions
function create_bridge () {
- [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
- ip_prefix=$1; shift
- [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
+ get_params "IP/PREFIX" "$@"
- IP=${ip_prefix%/*}
- PREFIX=${ip_prefix#*/}
+ IP=${IP_PREFIX%/*}
+ PREFIX=${IP_PREFIX#*/}
set -e
# ensure ovs-vswitchd is running
# we're clear
TAPNAME=$(pltap-ovs)
+ trap kill_pltap_ovs EXIT
# xxx wouldn't that be safer if left-aligned ?
vsysc vif_up << EOF
$TAPNAME
function create_port () {
- [[ -z "$@" ]] && error "$COMMAND create-port <bridge> <port>"
- bridge=$1; shift
- [[ -z "$@" ]] && error "$COMMAND create-port <bridge> <port>"
- port=$1; shift
- [[ -n "$@" ]] && error "$COMMAND create-port <bridge> <port>"
+ get_params "bridge port" "$@"
# ensure ovs-vswitchd is running
is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
function set_remote_endpoint () {
- [[ -z "$@" ]] && error "$COMMAND set-remote-endpoint <local_port> <remote_ip> <remote_UDP_port>"
- port=$1; shift
- [[ -z "$@" ]] && error "$COMMAND set-remote-endpoint <local_port> <remote_ip> <remote_UDP_port>"
- remote_ip=$1; shift
- [[ -z "$@" ]] && error "$COMMAND set-remote-endpoint <local_port> <remote_ip> <remote_UDP_port>"
- remote_port=$1; shift
- [[ -n "$@" ]] && error "$COMMAND set-remote-endpoint <local_port> <remote_ip> <remote_UDP_port>"
+ get_params "local_port remote_ip remote_UDP_port" "$@"
# ensure ovs-vswitchd is running
is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
set -e
- ovs-vsctl --db=unix:$DB_SOCKET set interface "$port" \
+ ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
options:remote_ip=$remote_ip \
- options:remote_port=$remote_port
+ options:remote_port=$remote_UDP_port
return 0
}
#################### del functions
function del_bridge () {
- [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
- bridge_name=$1; shift
- [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
+ get_params "bridge_name" "$@"
W=
if ! is_switch_running; then
}
function del_port () {
- [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
- bridge_name=$1; shift
- [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
+
+ get_params "port" "$@"
W=
if ! is_switch_running; then
fi
set -e
- 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"
+ if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
+ ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
fi
return 0
}
+function show () {
+
+ get_params "" "$@"
+
+ is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+
+ ovs-vsctl --db=unix:$DB_SOCKET show
+}
+
####################
SUPPORTED_SUBCOMMANDS="start stop status
start_db stop_db start_switch stop_switch
create_bridge create_port del_bridge del_port
-set_remote_endpoint"
+show set_remote_endpoint"
function main () {
message="Usage: $COMMAND <subcommand> ...
$SUPPORTED_SUBCOMMANDS"
[[ -z "$@" ]] && error "$message"
- subcommand=$1; shift
+ SUBCOMMAND=$1; shift
# support dashes instead of underscores
- subcommand=$(echo $subcommand | sed -e s,-,_,g)
+ SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
found=""
- for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
+ for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
[ -z "$found" ] && error $message
- $subcommand "$@"
+ $SUBCOMMAND "$@"
}
main "$@"