get_mac subcommand in sliver-ovs
[sliver-openvswitch.git] / planetlab / scripts / sliver-ovs.in
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=@RUNDIR@
10 DB_CONF_FILE=@DBDIR@/conf.db
11 DB_SCHEMA=@pkgdatadir@/vswitch.ovsschema
12 DB_LOG=@LOGDIR@/ovs-db.log
13 ##
14 DB_SOCKET=$RUN_DIR/db.sock
15 ##
16 SWITCH_LOG=@LOGDIR@/ovs-switch.log
17
18 #################### helper functions
19
20 function kill_pltap_ovs () {
21     killall pltap-ovs 2>/dev/null || :
22 }
23
24 function error {
25     echo "$@" >&2
26     exit 1
27 }
28
29 function get_params {
30     params=$1; shift
31     err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
32     for p in $params; do
33         [[ -z "$@" ]] && error "$err_msg"
34         pname=$(echo -n $p|perl -pe 's/\W/_/g')
35         eval $pname="$1"; shift
36     done
37     [[ -n "$@" ]] && error "$err_msg"
38 }
39
40 function is_switch_running {
41     ovs-appctl --target=ovs-vswitchd version >& /dev/null
42 }
43
44 function is_db_running {
45     ovs-appctl --target=ovsdb-server version >& /dev/null
46 }
47
48 function tapname () {
49     IP=$1; shift
50     echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
51 }
52     
53 function wait_server () {
54     pid_file=$1; shift
55     server_name=$1; shift
56     timeout=$1; shift
57
58     expire=$(($(date +%s) + $timeout))
59
60     ## wait for it to be up - xxx todo - could use a timeout of some kind
61     while [ ! -f "$pid_file" ]; do
62         echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
63         sleep 1;
64         [ $(date +%s) -ge $expire ] && return 1
65     done
66     cat "$pid_file"
67 }
68
69 function wait_device () {
70     tapname=$1; shift
71     timeout=$1; shift
72
73     expire=$(($(date +%s) + $timeout))
74
75     while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
76         echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
77         sleep 1
78         [ $(date +%s) -ge $expire ] && return 1
79     done
80     return 0
81 }
82
83 ######################################## startup
84 function start_db () {
85     get_params "" "$@"
86
87     ## init conf
88     conf_dir=$(dirname $DB_CONF_FILE)
89     [ -d $conf_dir ] || mkdir -p $conf_dir
90     [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
91
92     ## init run
93     [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
94
95     ## check 
96     [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
97     [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
98
99     ## run the stuff
100     if [ ! -f "$RUN_DIR/ovsdb-server.pid" ]; then
101         ovsdb-server $DB_CONF_FILE \
102             --remote=punix:$DB_SOCKET \
103             --remote=db:Open_vSwitch,manager_options \
104             --private-key=db:SSL,private_key \
105             --certificate=db:SSL,certificate \
106             --bootstrap-ca-cert=db:SSL,ca_cert \
107             --pidfile \
108             --log-file=$DB_LOG \
109             --detach >& /dev/null
110     else
111         echo 'ovsdb-server appears to be running already, *not* starting'
112     fi
113     wait_server $RUN_DIR/ovsdb-server.pid ovsdb-server 30
114 }
115
116 function start_switch () {
117     get_params "" "$@"
118
119     # ensure ovsdb-server is running
120     is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
121
122     if [ ! -f "$RUN_DIR/ovs-vswitchd.pid" ] ; then
123         ovs-vswitchd \
124             --pidfile \
125             --log-file=$SWITCH_LOG \
126             --detach \
127             unix:$DB_SOCKET >& /dev/null
128     else
129         echo 'ovs-vswitchd appears to be running already, *not* starting'
130     fi
131     wait_server $RUN_DIR/ovs-vswitchd.pid ovs-vswitchd 30
132 }
133
134 function stop_db () { 
135     ovs-appctl --target=ovsdb-server exit || :
136 }
137
138 function stop_switch () { 
139     ovs-appctl --target=ovs-vswitchd exit || :
140 }
141
142 function status () {
143     pids=$(pgrep '^ovs')
144     [ -n "$pids" ] && ps $pids
145 }
146
147 function start () {
148     start_db
149     start_switch
150 }
151
152 function stop () {
153     stop_switch
154     stop_db
155 }
156
157 #################### create functions
158 function create_bridge () {
159     
160     get_params "bridge IP/PREFIX" "$1" "$2"
161     shift; shift;
162
163     IP=${IP_PREFIX%/*}
164     PREFIX=${IP_PREFIX#*/}
165
166     W=
167     if ! is_switch_running; then
168         # we can create the bridge even if ovs-vswitchd is not running,
169         # but we need a running ovsdb-server
170         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
171         W="--no-wait"
172     fi
173
174
175     set -e
176     ovs-vsctl --db=unix:$DB_SOCKET $W -- --may-exist add-br "$bridge" \
177         -- set bridge "$bridge" datapath_type=planetlab \
178         -- set interface "$bridge" options:local_ip="$IP" option:local_netmask="$PREFIX" \
179         -- "$@"
180 }
181
182 function create_port () {
183
184     get_params "bridge port" "$@"
185    
186     W=
187     if ! is_switch_running; then
188         # we can create the port even if ovs-vswitchd is not running,
189         # but we need a running ovsdb-server
190         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
191         W="--no-wait"
192     fi
193
194     set -e
195     ovs-vsctl --db=unix:$DB_SOCKET $W -- --may-exist add-port "$bridge" "$port" \
196         -- set interface "$port" type=tunnel
197 }
198
199 function get_local_endpoint () {
200
201     get_params "local_port" "$@"
202
203     is_switch_running || { echo "ovs-vswitchd not running" >&2; exit 1; }
204
205     set -e
206     ovs-appctl --target=ovs-vswitchd netdev-tunnel/get-port "$local_port"
207 }
208
209 function set_remote_endpoint () {
210
211     get_params "local_port remote_ip remote_UDP_port" "$@"
212
213     W=
214     if ! is_switch_running; then
215         # we can store the info even if ovs-vswitchd is not running,
216         # but we need a running ovsdb-server
217         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
218         W="--no-wait"
219     fi
220
221     set -e
222     ovs-vsctl --db=unix:$DB_SOCKET $W set interface $local_port \
223         options:remote_ip=$remote_ip \
224         options:remote_port=$remote_UDP_port
225 }
226
227 function set_controller () {
228
229     get_params "bridge_name controller" "$@"
230
231     # ensure ovs-vswitchd is running
232     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
233
234     set -e
235     ovs-vsctl --db=unix:$DB_SOCKET set-controller "$bridge_name" "$controller"
236 }
237
238 function del_controller () {
239
240     get_params "bridge_name" "$@"
241
242     # ensure ovs-vswitchd is running
243     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
244
245     set -e
246     ovs-vsctl --db=unix:$DB_SOCKET del-controller "$bridge_name"
247 }
248
249 #################### del functions
250 function del_bridge () {
251     
252     get_params "bridge_name" "$@"
253
254     W=
255     if ! is_switch_running; then
256         # we can delete the bridge even if ovs-vswitchd is not running,
257         # but we need a running ovsdb-server
258         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
259         W="--no-wait"
260     fi
261
262     ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-br $bridge_name
263 }
264
265 function del_port () {
266     
267     get_params "port" "$@"
268
269     W=
270     if ! is_switch_running; then
271         # we can delete the port even if ovs-vswitchd is not running,
272         # but we need a running ovsdb-server
273         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
274         W="--no-wait"
275     fi
276
277     ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-port "$port"
278 }
279
280 function show () {
281
282     get_params "" "$@"
283
284     is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
285
286     ovs-vsctl --db=unix:$DB_SOCKET show
287 }
288
289 function get_local_ip () {
290
291     get_params "bridge" "$@"
292
293     set -e
294     ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge" || return
295     local ip=$(ovs-vsctl get interface "$bridge" options:local_ip)
296     local netmask=$(ovs-vsctl get interface "$bridge" options:local_netmask)
297     eval echo $ip/$netmask
298 }
299
300 function get_local_links () {
301
302     get_params "bridge" "$@"
303
304     set -e
305     ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge" || return
306     ovs-vsctl --db=unix:$DB_SOCKET list-ifaces "$bridge"
307 }
308
309 function get_mac () {
310
311     get_params "bridge" "$@"
312
313     set -e
314     local tap=$(ovs-appctl netdev-pltap/get-tapname "$bridge")
315     ifconfig "$tap" | awk '/HWaddr/ { print $5 }'
316 }
317
318 ### for demos - connect to an ndnmap deployment to visualize links bandwidth
319 # this expects 3 arguments
320 # an interface name, L<id1>-<id2> based on your ids in conf.mk
321 # the hostname for a ndnmap deployment
322 # a linkid, this is the id that this link has in your ndnmap scenario (hard-coded in some json file)
323 # this one-shot function writes the current statistics onto the ndnmap site
324 # it needs to be called regularly so that ndnmap can do the bw computations
325 # would make sense for the caller to redirect stderr onto some relevant location
326 function gmap_probe_once () {
327     iface=$1; shift
328     hostname=$1; shift
329     linkid=$1; shift
330     rx_bytes=$(ovs-appctl netdev-tunnel/get-rx-bytes $iface)
331     tx_bytes=$(ovs-appctl netdev-tunnel/get-tx-bytes $iface)
332     rx_bits=$(($rx_bytes*8))
333     tx_bits=$(($tx_bytes*8))
334     now=$(date +%s).$(date +%N)
335     trigger=http://${hostname}/bw/${linkid}/${now}/${rx_bits}/${tx_bits}
336 #    curl -s -L $trigger | grep -q "Got it" || echo Missed event with $trigger
337 #    echo $trigger
338     curl -s -L $trigger >& /dev/null
339 }
340
341 ### the front end, manages pid and so on
342 function gmap_probe () {
343     iface=$1; shift
344     hostname=$1; shift
345     linkid=$1; shift
346     looptime=$1; shift
347     [ -z "$looptime" ] && looptime=1
348     pid_file=/var/run/openvswitch/gmap-$iface.pid
349     if [ -f $pid_file ] ; then
350         pid=$(cat $pid_file)
351         [ -n "$pid" ] && kill $pid >& /dev/null
352         rm $pid_file
353     fi
354     # close std fds so that ssh invokations can return
355     exec <&-
356     exec >&-
357     while true; do
358         gmap_probe_once $iface $hostname $linkid
359         sleep $looptime
360     done &
361     # this is the pid for the background process
362     echo $! > $pid_file
363 }
364
365 ####################
366 SUPPORTED_SUBCOMMANDS="start stop status 
367 start_db stop_db start_switch stop_switch
368 create_bridge create_port del_bridge del_port
369 show get_local_endpoint set_remote_endpoint
370 set_controller del_controller gmap_probe
371 get_local_ip get_local_links get_mac"
372
373 function main () {
374         message="Usage: $COMMAND <subcommand> ...
375 Supported subcommands are (dash or underscore is the same):
376 $SUPPORTED_SUBCOMMANDS"
377         [[ -z "$@" ]] && error "$message"
378
379         SUBCOMMAND=$1; shift
380         # support dashes instead of underscores
381         SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
382         found=""
383         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
384
385         [ -z "$found" ] && error $message
386
387         $SUBCOMMAND "$@"
388 }
389
390 main "$@"