Merge branch 'master' of ssh://git.onelab.eu/git/sliver-openvswitch
[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_CTL_PATTERN='ovsdb-server.*.ctl'
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     echo $DB_PID_FILE
119 }
120
121 function start_switch () {
122     get_params "" "$@"
123
124     # ensure ovsdb-server is running
125     is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
126
127     if [ ! -f "$SWITCH_PID_FILE" ] ; then
128         ovs-vswitchd \
129             --pidfile=$SWITCH_PID_FILE \
130             --log-file=$SWITCH_LOG \
131             --unixctl=$SWITCH_SOCKET \
132             --detach \
133             unix:$DB_SOCKET >& /dev/null
134     else
135         echo 'ovs-vswitchd appears to be running already, *not* starting'
136     fi
137     wait_server $SWITCH_PID_FILE ovs-vswitchd 30
138 }
139
140 # first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
141 # quick and dirty : we locate the control file through a search in /var/run
142 # caller should be requested to remember and provide this pid instead
143 function stop_db () { 
144     controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
145     [ -f $controlfile ] && ovs-appctl --target=$controlfile exit 
146 }
147
148 function stop_switch () { 
149     ovs-appctl --target=$SWITCH_SOCKET exit || :
150 }
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     trap kill_pltap_ovs EXIT
193     # xxx wouldn't that be safer if left-aligned ?
194     vsysc vif_up << EOF
195         $TAPNAME
196         $IP
197         $PREFIX
198 EOF
199     wait_device $TAPNAME 60 && \
200         ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
201     echo $TAPNAME
202     return 0
203 }
204
205 function create_port () {
206
207     get_params "bridge port" "$@"
208
209     # ensure ovs-vswitchd is running
210     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
211
212     set -e
213     if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
214         ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
215     fi
216     ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
217     return 0
218 }
219
220 function set_remote_endpoint () {
221
222     get_params "local_port remote_ip remote_UDP_port" "$@"
223
224     # ensure ovs-vswitchd is running
225     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
226
227     set -e
228     ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
229         options:remote_ip=$remote_ip \
230         options:remote_port=$remote_UDP_port
231     return 0
232 }
233
234 #################### del functions
235 function del_bridge () {
236     
237     get_params "bridge_name" "$@"
238
239     W=
240     if ! is_switch_running; then
241         # we can delete the bridge even if ovs-vswitchd is not running,
242         # but we need a running ovsdb-server
243         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
244         W="--no-wait"
245     fi
246
247     if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
248         ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
249     fi
250     return 0
251 }
252
253 function del_port () {
254     
255     get_params "port" "$@"
256
257     W=
258     if ! is_switch_running; then
259         # we can delete the port even if ovs-vswitchd is not running,
260         # but we need a running ovsdb-server
261         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
262         W="--no-wait"
263     fi
264
265     set -e
266     if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
267         ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
268     fi
269     return 0
270 }
271
272 function show () {
273
274     get_params "" "$@"
275
276     is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
277
278     ovs-vsctl --db=unix:$DB_SOCKET show
279 }
280
281 ####################
282 SUPPORTED_SUBCOMMANDS="start stop status 
283 start_db stop_db start_switch stop_switch
284 create_bridge create_port del_bridge del_port
285 show set_remote_endpoint"
286
287 function main () {
288         message="Usage: $COMMAND <subcommand> ...
289 Supported subcommands are (dash or underscore is the same):
290 $SUPPORTED_SUBCOMMANDS"
291         [[ -z "$@" ]] && error "$message"
292
293         SUBCOMMAND=$1; shift
294         # support dashes instead of underscores
295         SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
296         found=""
297         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
298
299         [ -z "$found" ] && error $message
300
301         $SUBCOMMAND "$@"
302 }
303
304 main "$@"