dce84386108d37bce143c6d61f513f1631e9ad6c
[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 #################### 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_CTRL_SOCKET=/var/run/openvswitch/db-ctrl.sock
15 ##
16 DB_SOCKET=/var/run/openvswitch/db.sock
17 ##
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
21
22 #################### helper functions
23
24 function kill_pltap_ovs () {
25     killall pltap-ovs 2>/dev/null || :
26 }
27
28 function error {
29     echo "$@" >&2
30     exit 1
31 }
32
33 function get_params {
34     params=$1; shift
35     err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
36     for p in $(echo $params); do
37         [[ -z "$@" ]] && error "$err_msg"
38         pname=$(echo -n $p|perl -pe 's/\W/_/g')
39         eval $pname="$1"; shift
40     done
41     [[ -n "$@" ]] && error "$err_msg"
42 }
43
44 function is_switch_running {
45     ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
46 }
47
48 function is_db_running {
49     ovs-appctl --target=$DB_CTRL_SOCKET version >& /dev/null
50 }
51
52 function tapname () {
53     IP=$1; shift
54     echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
55 }
56     
57 function wait_server () {
58     pid_file=$1; shift
59     server_name=$1; shift
60     timeout=$1; shift
61
62     expire=$(($(date +%s) + $timeout))
63
64     ## wait for it to be up - xxx todo - could use a timeout of some kind
65     while [ ! -f "$pid_file" ]; do
66         echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
67         sleep 1;
68         [ $(date +%s) -ge $expire ] && return 1
69     done
70     cat "$pid_file"
71 }
72
73 function wait_device () {
74     tapname=$1; shift
75     timeout=$1; shift
76
77     expire=$(($(date +%s) + $timeout))
78
79     while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
80         echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
81         sleep 1
82         [ $(date +%s) -ge $expire ] && return 1
83     done
84     return 0
85 }
86
87 ######################################## startup
88 function start_db () {
89     get_params "" "$@"
90
91     ## init conf
92     conf_dir=$(dirname $DB_CONF_FILE)
93     [ -d $conf_dir ] || mkdir -p $conf_dir
94     [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
95
96     ## init run
97     [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
98
99     ## check 
100     [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
101     [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
102
103     ## run the stuff
104     if [ ! -f "$DB_PID_FILE" ]; then
105         ovsdb-server --remote=punix:$DB_SOCKET \
106             --remote=db:Open_vSwitch,manager_options \
107             --private-key=db:SSL,private_key \
108             --certificate=db:SSL,certificate \
109             --bootstrap-ca-cert=db:SSL,ca_cert \
110             --pidfile=$DB_PID_FILE \
111             --log-file=$DB_LOG \
112             --unixctl=$DB_CTRL_SOCKET \
113             --detach >& /dev/null
114     else
115         echo 'ovsdb-server appears to be running already, *not* starting'
116     fi
117     wait_server $DB_PID_FILE ovsdb-server 30
118 }
119
120 function stop_db () { 
121     get_params "" "$@"
122
123     pkill ovsdb-server
124 }
125  
126
127
128 function start_switch () {
129     get_params "" "$@"
130
131     # ensure ovsdb-server is running
132     is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
133
134     if [ ! -f "$SWITCH_PID_FILE" ] ; then
135         ovs-vswitchd \
136             --pidfile=$SWITCH_PID_FILE \
137             --log-file=$SWITCH_LOG \
138             --unixctl=$SWITCH_SOCKET \
139             --detach \
140             unix:$DB_SOCKET >& /dev/null
141     else
142         echo 'ovs-vswitchd appears to be running already, *not* starting'
143     fi
144     wait_server $SWITCH_PID_FILE ovs-vswitchd 30
145 }
146
147 function stop_switch () {
148     get_params "" "$@"
149
150     pkill ovs-vswitchd ; }
151
152 function status () {
153     pids=$(pgrep '^ovs')
154     [ -n "$pids" ] && ps $pids
155 }
156
157 function start () {
158     start_db
159     start_switch
160 }
161
162 function stop () {
163     stop_switch
164     stop_db
165 }
166
167 #################### create functions
168 function create_bridge () {
169     
170     get_params "IP/PREFIX" "$@"
171
172     IP=${IP_PREFIX%/*}
173     PREFIX=${IP_PREFIX#*/}
174
175     set -e
176     # ensure ovs-vswitchd is running
177     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
178
179     # check whether the address is already assigned
180     TAPNAME=$(tapname $IP)
181     if [ ! -z "$TAPNAME" ]; then
182         if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
183             echo $TAPNAME
184             exit 0
185         fi
186         kill_pltap_ovs
187         error "$IP already assigned to $TAPNAME"
188     fi
189
190     # we're clear
191     TAPNAME=$(pltap-ovs)
192     # xxx wouldn't that be safer if left-aligned ?
193     vsysc vif_up << EOF
194         $TAPNAME
195         $IP
196         $PREFIX
197 EOF
198     wait_device $TAPNAME 60 && \
199         ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
200     echo $TAPNAME
201     return 0
202 }
203
204 function create_port () {
205
206     get_params "bridge port" "$@"
207
208     # ensure ovs-vswitchd is running
209     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
210
211     set -e
212     if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
213         ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
214     fi
215     ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
216     return 0
217 }
218
219 function set_remote_endpoint () {
220
221     get_params "local_port remote_ip remote_UDP_port" "$@"
222
223     # ensure ovs-vswitchd is running
224     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
225
226     set -e
227     ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
228         options:remote_ip=$remote_ip \
229         options:remote_port=$remote_UDP_port
230     return 0
231 }
232
233 #################### del functions
234 function del_bridge () {
235     
236     get_params "bridge_name" "$@"
237
238     W=
239     if ! is_switch_running; then
240         # we can delete the bridge even if ovs-vswitchd is not running,
241         # but we need a running ovsdb-server
242         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
243         W="--no-wait"
244     fi
245
246     if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
247         ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
248     fi
249     return 0
250 }
251
252 function del_port () {
253     
254     get_params "port" "$@"
255
256     W=
257     if ! is_switch_running; then
258         # we can delete the port even if ovs-vswitchd is not running,
259         # but we need a running ovsdb-server
260         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
261         W="--no-wait"
262     fi
263
264     set -e
265     if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
266         ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
267     fi
268     return 0
269 }
270
271 function show () {
272
273     get_params "" "$@"
274
275     is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
276
277     ovs-vsctl --db=unix:$DB_SOCKET show
278 }
279
280 ####################
281 SUPPORTED_SUBCOMMANDS="start stop status 
282 start_db stop_db start_switch stop_switch
283 create_bridge create_port del_bridge del_port
284 show set_remote_endpoint"
285
286 function main () {
287         message="Usage: $COMMAND <subcommand> ...
288 Supported subcommands are (dash or underscore is the same):
289 $SUPPORTED_SUBCOMMANDS"
290         [[ -z "$@" ]] && error "$message"
291
292         SUBCOMMAND=$1; shift
293         # support dashes instead of underscores
294         SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
295         found=""
296         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
297
298         [ -z "$found" ] && error $message
299
300         $SUBCOMMAND "$@"
301 }
302
303 main "$@"