bugfixes
[sliver-openvswitch.git] / planetlab / scripts / sliver-ovs
1 #!/bin/bash
2 # -*-shell-mode-*-
3
4 ### expected to be run as root
5
6 COMMAND=$0
7
8 #################### helper functions
9
10 function kill_pltap_ovs () {
11     killall pltap-ovs 2>/dev/null || :
12 }
13
14 function error {
15     echo "$@" >&2
16     exit 1
17 }
18
19 function is_switch_running {
20     ovs-appctl version >/dev/null 2>&1
21 }
22
23 function tapname () {
24     IP=$1; shift
25     echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
26 }
27     
28 function wait_server () {
29     pid_file=$1; shift
30     server_name="$@"; shift
31
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
35         sleep 1;
36     done
37     cat "$pid_file"
38 }
39
40
41 function create_bridge () {
42     
43     [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
44     ip_prefix=$1; shift
45     [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
46
47     IP=${ip_prefix%/*}
48     PREFIX=${ip_prefix#*/}
49
50     set -e
51     # ensure ovs-vswitchd is running
52     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
53
54     # check whether the address is already assigned
55     TAPNAME=$(tapname $IP)
56     if [ ! -z "$TAPNAME" ]; then
57         if ovs-vsctl br-exists "$TAPNAME"; then
58             echo $TAPNAME
59             exit 0
60         fi
61         kill_pltap_ovs
62         error "$IP already assigned to $TAPNAME"
63     fi
64
65     # we're clear
66     TAPNAME=$(pltap-ovs)
67     # xxx wouldn't that be safer if left-aligned ?
68     vsysc vif_up << EOF
69         $TAPNAME
70         $IP
71         $PREFIX
72 EOF
73     while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
74         echo "Waiting for $TAPNAME to come UP..." >&2
75         sleep 1
76     done
77     ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
78     echo $TAPNAME
79     return 0
80 }
81
82 function create_port () {
83
84     bridge=$1; shift
85     [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
86     port=$1; shift
87     [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
88
89     set -e
90     if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
91         ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
92     fi
93     ovs-appctl netdev-tunnel/get-port "$port"
94     return 0
95 }
96
97
98 function del_bridge () {
99     
100     [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
101     bridge_name=$1; shift
102     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
103
104     W=
105     is_switch_running || W="--no-wait"
106
107     if ovs-vsctl br-exists "$bridge_name"; then
108         ovs-vsctl $W del-br $bridge_name
109     fi
110     return 0
111 }
112
113 function del_port () {
114     [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
115     bridge_name=$1; shift
116     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
117
118     set -e
119     if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
120         ovs-vsctl del-port "$1"
121     fi
122     return 0
123 }
124
125
126 CONF_FILE=/usr/etc/openvswitch/conf.db
127 RUN_DIR=/var/run/openvswitch
128 DB_PID_FILE=/var/run/openvswitch/ovsdb-server.pid
129
130 function start_db () {
131
132     [[ -n "$@" ]] && error "Usage: $COMMAND start-db"
133
134     ## init conf
135     conf_dir=$(dirname $CONF_FILE)
136     [ -d $conf_dir ] || mkdir -p $conf_dir
137     [ -f $CONF_FILE ] || ovsdb-tool create $CONF_FILE /usr/share/openvswitch/vswitch.ovsschema
138
139     ## init run
140     [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
141
142     ## check 
143     [ -f $CONF_FILE ] || { echo "Could not initialize $CONF_FILE - exiting" ; exit 1 ; }
144     [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
145
146     ## run the stuff
147     if [ ! -f "$DB_PID_FILE" ]; then
148         ovsdb-server --remote=punix:/var/run/openvswitch/db.sock \
149             --remote=db:Open_vSwitch,manager_options \
150             --private-key=db:SSL,private_key \
151             --certificate=db:SSL,certificate \
152             --bootstrap-ca-cert=db:SSL,ca_cert \
153             --pidfile=$DB_PID_FILE --detach
154     else
155         echo 'ovsdb-server appears to be running already, *not* starting'
156     fi
157     wait_server $DB_PID_FILE ovsdb-server
158 }
159
160 SWITCH_PID_FILE=/usr/var/run/openvswitch/ovs-vswitchd.pid
161
162 function start_switch () {
163
164     [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
165
166     if [ ! -f "$SWITCH_PID_FILE" ] ; then
167         ovs-vswitchd --pidfile=$SWITCH_PID_FILE --detach --log-file >/dev/null
168     else
169         echo 'ovs-vswitchd appears to be running already, *not* starting'
170     fi
171     wait_server $SWITCH_PID_FILE ovs-vswitchd
172 }
173
174 SUBCOMMANDS="create_bridge create_port del_bridge del_port start_db start_switch"
175
176 function main () {
177         message="Usage: $COMMAND <subcommand> ...
178 Supported subcommands are (dash or underscore is the same):
179 $SUBCOMMANDS"
180         [[ -z "$@" ]] && error "$message"
181
182         subcommand=$1; shift
183         # support dashes instead of underscores
184         subcommand=$(echo $subcommand | sed -e s,-,_,)
185         found=""
186         for supported in $SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
187
188         [ -z "$found" ] && error $message
189
190         $subcommand "$@"
191 }
192
193 main "$@"