4 ### expected to be run as root
8 #################### helper functions
10 function kill_pltap_ovs () {
11 killall pltap-ovs 2>/dev/null || :
19 function is_switch_running {
20 ovs-appctl version >/dev/null 2>&1
25 echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
28 function wait_server () {
30 server_name="$@"; shift
32 ## wait for it to be up - xxx todo - could use a timeout of some kind
33 while [ ! -f "pid_file" ]; do
34 echo "Waiting for $server_name to start..." >&2
41 function create_bridge () {
43 [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
45 [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
48 PREFIX=${ip_prefix#*/}
51 # ensure ovs-vswitchd is running
52 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
54 # check whether the address is already assigned
55 TAPNAME=$(tapname $IP)
56 if [ ! -z "$TAPNAME" ]; then
57 if ovs-vsctl br-exists "$TAPNAME"; then
62 error "$IP already assigned to $TAPNAME"
67 # xxx wouldn't that be safer if left-aligned ?
73 while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
74 echo "Waiting for $TAPNAME to come UP..." >&2
77 ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
82 function create_port () {
85 [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
87 [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
90 if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
91 ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
93 ovs-appctl netdev-tunnel/get-port "$port"
98 function del_bridge () {
100 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
101 bridge_name=$1; shift
102 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
105 is_switch_running || W="--no-wait"
107 if ovs-vsctl br-exists "$bridge_name"; then
108 ovs-vsctl $W del-br $bridge_name
113 function del_port () {
114 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
115 bridge_name=$1; shift
116 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
119 if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
120 ovs-vsctl del-port "$1"
126 CONF_FILE=/usr/etc/openvswitch/conf.db
127 RUN_DIR=/usr/var/run/openvswitch
128 DB_PID_FILE=/usr/var/run/openvswitch/ovsdb-server.pid
130 function start_db () {
132 [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
135 conf_dir=$(dirname $CONF_FILE)
136 [ -d $conf_dir ] || mkdir -p $conf_dir
137 [ -f $CONF_FILE ] || ovsdb-tool create $CONF_FILE /usr/share/openvswitch/vswitch.ovsschema
140 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
143 [ -f $CONF_FILE ] || { echo "Could not initialize $CONF_FILE - exiting" ; exit 1 ; }
144 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
147 if [ ! -f "$DB_PID_FILE" ]; then
148 ovsdb-server --remote=punix:/usr/var/run/openvswitch/db.sock \
149 --remote=db:Open_vSwitch,manager_options \
150 --private-key=db:SSL,private_key \
151 --certificate=db:SSL,certificate \
152 --bootstrap-ca-cert=db:SSL,ca_cert \
153 --pidfile=$DB_PID_FILE --detach
155 pid=$(wait_server $DB_PID_FILE ovsdb-server)
159 SWITCH_PID_FILE=/usr/var/run/openvswitch/ovs-vswitchd.pid
161 function start_switch () {
163 [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
165 [ -f "$SWITCH_PID_FILE" ] || ovs-vswitchd --pidfile=$SWITCH_PID_FILE --detach --log-file >/dev/null
166 wait_server $SWITCH_PID_FILE
169 SUBCOMMANDS="create_bridge create_port del_bridge del_port start_db start_switch"
172 message="Usage: $COMMAND <subcommand> ...
173 Supported subcommands are:
175 [[ -z "$@" ]] && error "$message"
178 # support dashes instead of underscores
179 subcommand=$(echo $subcommand | sed -e s,-,_,)
181 for supported in $SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes ; echo sc=X${subcommand}X sup=Y${supported}Y; done
182 echo subcommand=$subcommand
185 [ -z "$found" ] && error $message