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
14 DB_CTL_PATTERN='ovsdb-server.*.ctl'
16 DB_SOCKET=/var/run/openvswitch/db.sock
18 SWITCH_PID_FILE=/var/run/openvswitch/switch.pid
19 SWITCH_LOG=/var/log/ovs-switch.log
20 SWITCH_SOCKET=/var/run/openvswitch/switch.sock
22 #################### helper functions
24 function kill_pltap_ovs () {
25 killall pltap-ovs 2>/dev/null || :
33 function is_switch_running {
34 ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
39 echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
42 function wait_server () {
47 expire=$(($(date +%s) + $timeout))
49 ## wait for it to be up - xxx todo - could use a timeout of some kind
50 while [ ! -f "$pid_file" ]; do
51 echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
53 [ $(date +%s) -ge $expire ] && return 1
58 function wait_device () {
62 expire=$(($(date +%s) + $timeout))
64 while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
65 echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
67 [ $(date +%s) -ge $expire ] && return 1
72 ######################################## startup
73 function start_db () {
75 [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
78 conf_dir=$(dirname $DB_CONF_FILE)
79 [ -d $conf_dir ] || mkdir -p $conf_dir
80 [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
83 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
86 [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
87 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
90 if [ ! -f "$DB_PID_FILE" ]; then
91 ovsdb-server --remote=punix:$DB_SOCKET \
92 --remote=db:Open_vSwitch,manager_options \
93 --private-key=db:SSL,private_key \
94 --certificate=db:SSL,certificate \
95 --bootstrap-ca-cert=db:SSL,ca_cert \
96 --pidfile=$DB_PID_FILE \
100 echo 'ovsdb-server appears to be running already, *not* starting'
102 wait_server $DB_PID_FILE ovsdb-server 30
106 function start_switch () {
108 [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
110 if [ ! -f "$SWITCH_PID_FILE" ] ; then
112 --pidfile=$SWITCH_PID_FILE \
113 --log-file=$SWITCH_LOG \
114 --unixctl=$SWITCH_SOCKET \
116 unix:$DB_SOCKET >& /dev/null
118 echo 'ovs-vswitchd appears to be running already, *not* starting'
120 wait_server $SWITCH_PID_FILE ovs-vswitchd 30
123 # first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
124 # quick and dirty : we locate the control file through a search in /var/run
125 # caller should be requested to remember and provide this pid instead
126 function stop_db () {
127 controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
128 [ -f $controlfile ] && ovs-appctl --target=$controlfile exit
131 function stop_switch () {
132 ovs-appctl --target=$SWITCH_SOCKET exit || :
137 [ -n "$pids" ] && ps $pids
150 #################### create functions
151 function create_bridge () {
153 [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
155 [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
158 PREFIX=${ip_prefix#*/}
161 # ensure ovs-vswitchd is running
162 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
164 # check whether the address is already assigned
165 TAPNAME=$(tapname $IP)
166 if [ ! -z "$TAPNAME" ]; then
167 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
172 error "$IP already assigned to $TAPNAME"
177 # xxx wouldn't that be safer if left-aligned ?
183 wait_device $TAPNAME 60 && \
184 ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
189 function create_port () {
191 [[ -z "$@" ]] && error "$COMMAND create-port <bridge> <port>"
193 [[ -z "$@" ]] && error "$COMMAND create-port <bridge> <port>"
195 [[ -n "$@" ]] && error "$COMMAND create-port <bridge> <port>"
198 if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
199 ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
201 ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
205 #################### del functions
206 function del_bridge () {
208 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
209 bridge_name=$1; shift
210 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
213 is_switch_running || W="--no-wait"
215 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
216 ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
221 function del_port () {
222 [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
223 bridge_name=$1; shift
224 [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
227 if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$1" >/dev/null 2>&1; then
228 ovs-vsctl --db=unix:$DB_SOCKET del-port "$1"
234 SUPPORTED_SUBCOMMANDS="start stop status
235 start_db stop_db start_switch stop_switch
236 create_bridge create_port del_bridge del_port"
239 message="Usage: $COMMAND <subcommand> ...
240 Supported subcommands are (dash or underscore is the same):
241 $SUPPORTED_SUBCOMMANDS"
242 [[ -z "$@" ]] && error "$message"
245 # support dashes instead of underscores
246 subcommand=$(echo $subcommand | sed -e s,-,_,)
248 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
250 [ -z "$found" ] && error $message