4 ### expected to be run as root
8 #################### global vars
9 RUN_DIR=/var/run/openvswitch
10 DB_CONF_FILE=/etc/openvswitch/conf.db
11 DB_SCHEMA=/usr/share/openvswitch/vswitch.ovsschema
12 DB_PID_FILE=/var/run/openvswitch/db.pid
13 DB_LOG=/var/log/ovs-db.log
15 DB_SOCKET=/var/run/openvswitch/db.sock
17 SWITCH_PID_FILE=/var/run/openvswitch/switch.pid
18 SWITCH_LOG=/var/log/ovs-switch.log
19 SWITCH_SOCKET=/var/run/openvswitch/switch.sock
21 #################### helper functions
23 function kill_pltap_ovs () {
24 killall pltap-ovs 2>/dev/null || :
32 function is_switch_running {
33 ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
38 echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
41 function wait_server () {
43 server_name="$@"; shift
45 ## wait for it to be up - xxx todo - could use a timeout of some kind
46 while [ ! -f "$pid_file" ]; do
47 echo "Waiting for $server_name to start..." >&2
53 ######################################## startup
54 function start_db () {
56 [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
59 conf_dir=$(dirname $DB_CONF_FILE)
60 [ -d $conf_dir ] || mkdir -p $conf_dir
61 [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
64 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
67 [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
68 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
71 if [ ! -f "$DB_PID_FILE" ]; then
72 ovsdb-server --remote=punix:$DB_SOCKET \
73 --remote=db:Open_vSwitch,manager_options \
74 --private-key=db:SSL,private_key \
75 --certificate=db:SSL,certificate \
76 --bootstrap-ca-cert=db:SSL,ca_cert \
77 --pidfile=$DB_PID_FILE \
81 echo 'ovsdb-server appears to be running already, *not* starting'
83 wait_server $DB_PID_FILE ovsdb-server
86 function stop_db () { pkill ovsdb-server; }
89 function start_switch () {
91 [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
93 if [ ! -f "$SWITCH_PID_FILE" ] ; then
95 --pidfile=$SWITCH_PID_FILE \
96 --log-file=$SWITCH_LOG \
97 --unixctl=$SWITCH_SOCKET \
99 unix:$DB_SOCKET >& /dev/null
101 echo 'ovs-vswitchd appears to be running already, *not* starting'
103 wait_server $SWITCH_PID_FILE ovs-vswitchd
106 function stop_switch () { pkill ovs-vswitchd ; }
110 [ -n "$pids" ] && ps $pids
123 #################### create functions
124 function create_bridge () {
126 [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
128 [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
131 PREFIX=${ip_prefix#*/}
134 # ensure ovs-vswitchd is running
135 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
137 # check whether the address is already assigned
138 TAPNAME=$(tapname $IP)
139 if [ ! -z "$TAPNAME" ]; then
140 if ovs-vsctl --db=$DB_SOCKET br-exists "$TAPNAME"; then
145 error "$IP already assigned to $TAPNAME"
150 # xxx wouldn't that be safer if left-aligned ?
156 while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
157 echo "Waiting for $TAPNAME to come UP..." >&2
160 ovs-vsctl --db=$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
165 function create_port () {
168 [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
170 [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
173 if ! ovs-vsctl --db=$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
174 ovs-vsctl --db=$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
176 ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
180 #################### del functions
181 function del_bridge () {
183 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
184 bridge_name=$1; shift
185 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
188 is_switch_running || W="--no-wait"
190 if ovs-vsctl --db=$DB_SOCKET br-exists "$bridge_name"; then
191 ovs-vsctl --db=$DB_SOCKET $W del-br $bridge_name
196 function del_port () {
197 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
198 bridge_name=$1; shift
199 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
202 if ovs-vsctl --db=$DB_SOCKET port-to-br "$1" >/dev/null 2>&1; then
203 ovs-vsctl --db=$DB_SOCKET del-port "$1"
209 SUPPORTED_SUBCOMMANDS="start stop status
210 start_db stop_db start_switch stop_switch
211 create_bridge create_port del_bridge del_port"
214 message="Usage: $COMMAND <subcommand> ...
215 Supported subcommands are (dash or underscore is the same):
216 $SUPPORTED_SUBCOMMANDS"
217 [[ -z "$@" ]] && error "$message"
220 # support dashes instead of underscores
221 subcommand=$(echo $subcommand | sed -e s,-,_,)
223 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
225 [ -z "$found" ] && error $message