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
40 ######################################## startup
41 RUN_DIR=/var/run/openvswitch
42 DB_CONF_FILE=/usr/etc/openvswitch/conf.db
43 DB_SCHEMA=/usr/share/openvswitch/vswitch.ovsschema
44 DB_PID_FILE=/var/run/openvswitch/ovsdb-server.pid
45 DB_LOG=/var/log/ovs-db.log
47 DB_SOCKET=/var/run/openvswitch/db.sock
49 SWITCH_PID_FILE=/var/run/openvswitch/ovs-vswitchd.pid
50 SWITCH_LOG=/var/log/ovs-switch.log
52 function start_db () {
54 [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
57 conf_dir=$(dirname $DB_CONF_FILE)
58 [ -d $conf_dir ] || mkdir -p $conf_dir
59 [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
62 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
65 [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
66 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
69 if [ ! -f "$DB_PID_FILE" ]; then
70 ovsdb-server --remote=punix:$DB_SOCKET \
71 --remote=db:Open_vSwitch,manager_options \
72 --private-key=db:SSL,private_key \
73 --certificate=db:SSL,certificate \
74 --bootstrap-ca-cert=db:SSL,ca_cert \
75 --pidfile=$DB_PID_FILE \
79 echo 'ovsdb-server appears to be running already, *not* starting'
81 wait_server $DB_PID_FILE ovsdb-server
84 function stop_db () { pkill ovsdb-server; }
87 function start_switch () {
89 [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
91 if [ ! -f "$SWITCH_PID_FILE" ] ; then
93 --pidfile=$SWITCH_PID_FILE \
94 --log-file=$SWITCH_LOG \
96 unix:$DB_SOCKET >& /dev/null
98 echo 'ovs-vswitchd appears to be running already, *not* starting'
100 wait_server $SWITCH_PID_FILE ovs-vswitchd
103 function stop_switch () { pkill ovs-vswitchd ; }
107 [ -n "$pids" ] && ps $pids
110 #################### create functions
111 function create_bridge () {
113 [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
115 [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
118 PREFIX=${ip_prefix#*/}
121 # ensure ovs-vswitchd is running
122 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
124 # check whether the address is already assigned
125 TAPNAME=$(tapname $IP)
126 if [ ! -z "$TAPNAME" ]; then
127 if ovs-vsctl br-exists "$TAPNAME"; then
132 error "$IP already assigned to $TAPNAME"
137 # xxx wouldn't that be safer if left-aligned ?
143 while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
144 echo "Waiting for $TAPNAME to come UP..." >&2
147 ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
152 function create_port () {
155 [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
157 [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
160 if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
161 ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
163 ovs-appctl netdev-tunnel/get-port "$port"
167 #################### del functions
168 function del_bridge () {
170 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
171 bridge_name=$1; shift
172 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
175 is_switch_running || W="--no-wait"
177 if ovs-vsctl br-exists "$bridge_name"; then
178 ovs-vsctl $W del-br $bridge_name
183 function del_port () {
184 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
185 bridge_name=$1; shift
186 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
189 if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
190 ovs-vsctl del-port "$1"
196 SUPPORTED_SUBCOMMANDS="start_db stop_db start_switch stop_switch status create_bridge create_port del_bridge del_port"
199 message="Usage: $COMMAND <subcommand> ...
200 Supported subcommands are (dash or underscore is the same):
201 $SUPPORTED_SUBCOMMANDS"
202 [[ -z "$@" ]] && error "$message"
205 # support dashes instead of underscores
206 subcommand=$(echo $subcommand | sed -e s,-,_,)
208 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
210 [ -z "$found" ] && error $message