X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetlab%2Fscripts%2Fsliver-ovs;h=87f173e2b00c9d6fbd6b396bc15ee5ba20b4c508;hb=a03fa095f61bf789af770dff2616ab382327b470;hp=50601d356859e543af02f05872850167062bab35;hpb=f31986ec759a01a9e04adca675129b37a8ed6717;p=sliver-openvswitch.git diff --git a/planetlab/scripts/sliver-ovs b/planetlab/scripts/sliver-ovs index 50601d356..87f173e2b 100755 --- a/planetlab/scripts/sliver-ovs +++ b/planetlab/scripts/sliver-ovs @@ -5,6 +5,20 @@ 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_CTL_PATTERN='ovsdb-server.*.ctl' +## +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 () { @@ -16,8 +30,23 @@ function error { 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 version >/dev/null 2>&1 + ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null +} + +function is_db_running { + ovs-appctl --target=$DB_CTRL_SOCKET version >& /dev/null } function tapname () { @@ -27,25 +56,121 @@ function tapname () { 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 + while [ ! -f "$pid_file" ]; do + 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" + 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 () { + get_params "" "$@" + + ## 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 \ + --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 30 + echo $DB_PID_FILE +} + +function start_switch () { + get_params "" "$@" + + # 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 \ + --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 30 +} + +# 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') + [ -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=$1; shift - [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge " + get_params "IP/PREFIX" "$@" - IP=${ip_prefix%/*} - PREFIX=${ip_prefix#*/} + IP=${IP_PREFIX%/*} + PREFIX=${IP_PREFIX#*/} set -e # ensure ovs-vswitchd is running @@ -54,7 +179,7 @@ function create_bridge () { # 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 @@ -64,127 +189,116 @@ function create_bridge () { # 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 $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 } function create_port () { - bridge=$1; shift - [[ -z "$@" ]] || error "$COMMAND create-port " - port=$1; shift - [[ -n "$@" ]] || error "$COMMAND create-port " + get_params "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 netdev-tunnel/get-port "$port" + ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port" return 0 } +function set_remote_endpoint () { + + 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 $local_port \ + options:remote_ip=$remote_ip \ + options:remote_port=$remote_UDP_port + return 0 +} +#################### del functions function del_bridge () { - [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge " - bridge_name=$1; shift - [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge " + get_params "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 } function del_port () { - [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port " - bridge_name=$1; shift - [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port " + + get_params "port" "$@" - set -e - if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then - ovs-vsctl del-port "$1" + 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 - 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 + set -e + 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 - pid=$(wait_server $DB_PID_FILE ovsdb-server) - echo $pid + return 0 } -SWITCH_PID_FILE=/usr/var/run/openvswitch/ovs-vswitchd.pid +function show () { -function start_switch () { + get_params "" "$@" - [[ -n "$@" ]] && error "Usage: $COMMAND start-switch" + is_db_running || { echo "ovsdb-server not running" >&2; exit 1; } - [ -f "$SWITCH_PID_FILE" ] || ovs-vswitchd --pidfile=$SWITCH_PID_FILE --detach --log-file >/dev/null - wait_server $SWITCH_PID_FILE + ovs-vsctl --db=unix:$DB_SOCKET show } -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 +show set_remote_endpoint" function main () { message="Usage: $COMMAND ... Supported subcommands are (dash or underscore is the same): -$SUBCOMMANDS" +$SUPPORTED_SUBCOMMANDS" [[ -z "$@" ]] && error "$message" - subcommand=$1; shift + SUBCOMMAND=$1; shift # support dashes instead of underscores - subcommand=$(echo $subcommand | sed -e s,-,_,) + SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g) 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 - $subcommand "$@" + $SUBCOMMAND "$@" } main "$@"