84fac40cf695eae81b8824e43d9f3b56ec8e0cc1
[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 $(echo $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 "IP/PREFIX" "$@"
161
162     IP=${IP_PREFIX%/*}
163     PREFIX=${IP_PREFIX#*/}
164
165     set -e
166     # ensure ovs-vswitchd is running
167     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
168
169     # check whether the address is already assigned
170     TAPNAME=$(tapname $IP)
171     if [ ! -z "$TAPNAME" ]; then
172         if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
173             echo $TAPNAME
174             exit 0
175         fi
176         kill_pltap_ovs
177         error "$IP already assigned to $TAPNAME"
178     fi
179
180     # we're clear
181     TAPNAME=$(pltap-ovs)
182     trap kill_pltap_ovs EXIT
183     # xxx wouldn't that be safer if left-aligned ?
184     vsysc vif_up << EOF
185         $TAPNAME
186         $IP
187         $PREFIX
188 EOF
189     wait_device $TAPNAME 60 && \
190         ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
191     echo $TAPNAME
192     return 0
193 }
194
195 function create_port () {
196
197     get_params "bridge port" "$@"
198
199     # ensure ovs-vswitchd is running
200     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
201
202     set -e
203     if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
204         ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
205     fi
206     ovs-appctl --target=ovs-vswitchd netdev-tunnel/get-port "$port"
207     return 0
208 }
209
210 function set_remote_endpoint () {
211
212     get_params "local_port remote_ip remote_UDP_port" "$@"
213
214     # ensure ovs-vswitchd is running
215     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
216
217     set -e
218     ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
219         options:remote_ip=$remote_ip \
220         options:remote_port=$remote_UDP_port
221     return 0
222 }
223
224 #################### del functions
225 function del_bridge () {
226     
227     get_params "bridge_name" "$@"
228
229     W=
230     if ! is_switch_running; then
231         # we can delete the bridge even if ovs-vswitchd is not running,
232         # but we need a running ovsdb-server
233         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
234         W="--no-wait"
235     fi
236
237     if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
238         ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
239     fi
240     return 0
241 }
242
243 function del_port () {
244     
245     get_params "port" "$@"
246
247     W=
248     if ! is_switch_running; then
249         # we can delete the port even if ovs-vswitchd is not running,
250         # but we need a running ovsdb-server
251         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
252         W="--no-wait"
253     fi
254
255     set -e
256     if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
257         ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
258     fi
259     return 0
260 }
261
262 function show () {
263
264     get_params "" "$@"
265
266     is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
267
268     ovs-vsctl --db=unix:$DB_SOCKET show
269 }
270
271 ####################
272 SUPPORTED_SUBCOMMANDS="start stop status 
273 start_db stop_db start_switch stop_switch
274 create_bridge create_port del_bridge del_port
275 show set_remote_endpoint"
276
277 function main () {
278         message="Usage: $COMMAND <subcommand> ...
279 Supported subcommands are (dash or underscore is the same):
280 $SUPPORTED_SUBCOMMANDS"
281         [[ -z "$@" ]] && error "$message"
282
283         SUBCOMMAND=$1; shift
284         # support dashes instead of underscores
285         SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
286         found=""
287         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
288
289         [ -z "$found" ] && error $message
290
291         $SUBCOMMAND "$@"
292 }
293
294 main "$@"