Merge branch 'master' into forward-port
[sliver-openvswitch.git] / planetlab / scripts / create_bridge
1 #!/bin/bash
2
3 function error 
4 {
5         echo $1 >&2
6         killall pltap-ovs 2>/dev/null || true
7         exit 1
8 }
9
10 function is_switch_running
11 {
12         ovs-appctl version >/dev/null 2>&1
13 }
14
15 if [ -z "$1" ]; then
16         error "Usage: ${0##*/} <IP/PREFIX>"
17 fi
18
19 # TODO: check paramether validity
20
21 IP=${1%/*}
22 PREFIX=${1#*/}
23
24 set -e
25
26 # ensure ovs-vswitchd is running
27 if ! is_switch_running; then
28         echo "ovs-vswitchd not running" >&2
29         exit 1
30 fi
31         
32
33 # check whether the address is already assigned
34 set -e
35 TAPNAME=$(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
36 if [ ! -z "$TAPNAME" ]; then
37     if ovs-vsctl br-exists "$TAPNAME"; then
38                 echo $TAPNAME
39                 exit 0
40     fi
41     error "$IP already assigned to $TAPNAME"
42 fi
43
44 TAPNAME=$(pltap-ovs)
45 vsysc vif_up << EOF
46         $TAPNAME
47         $IP
48         $PREFIX
49 EOF
50 while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
51         echo "Waiting for $TAPNAME to come UP..." >&2
52         sleep 1
53 done
54 ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
55 echo $TAPNAME