4 ### expected to be run as root
8 #################### global vars
9 RUN_DIR=/var/run/openvswitch
10 DB_CONF_FILE=/usr/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
113 #################### create functions
114 function create_bridge () {
116 [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
118 [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
121 PREFIX=${ip_prefix#*/}
124 # ensure ovs-vswitchd is running
125 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
127 # check whether the address is already assigned
128 TAPNAME=$(tapname $IP)
129 if [ ! -z "$TAPNAME" ]; then
130 if ovs-vsctl br-exists "$TAPNAME"; then
135 error "$IP already assigned to $TAPNAME"
140 # xxx wouldn't that be safer if left-aligned ?
146 while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
147 echo "Waiting for $TAPNAME to come UP..." >&2
150 ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
155 function create_port () {
158 [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
160 [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
163 if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
164 ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
166 ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
170 #################### del functions
171 function del_bridge () {
173 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
174 bridge_name=$1; shift
175 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
178 is_switch_running || W="--no-wait"
180 if ovs-vsctl br-exists "$bridge_name"; then
181 ovs-vsctl $W del-br $bridge_name
186 function del_port () {
187 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
188 bridge_name=$1; shift
189 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
192 if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
193 ovs-vsctl del-port "$1"
199 SUPPORTED_SUBCOMMANDS="start_db stop_db start_switch stop_switch status create_bridge create_port del_bridge del_port"
202 message="Usage: $COMMAND <subcommand> ...
203 Supported subcommands are (dash or underscore is the same):
204 $SUPPORTED_SUBCOMMANDS"
205 [[ -z "$@" ]] && error "$message"
208 # support dashes instead of underscores
209 subcommand=$(echo $subcommand | sed -e s,-,_,)
211 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
213 [ -z "$found" ] && error $message