tell ovsdb-server where the db is
[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_PID_FILE=$RUN_DIR/db.pid
13 DB_LOG=@LOGDIR@/ovs-db.log
14 DB_CTL_PATTERN='ovsdb-server.*.ctl'
15 ##
16 DB_SOCKET=$RUN_DIR/db.sock
17 ##
18 SWITCH_PID_FILE=$RUN_DIR/switch.pid
19 SWITCH_LOG=@LOGDIR@/ovs-switch.log
20 SWITCH_SOCKET=@RUNDIR@/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 $DB_CONF_FILE \
106             --remote=punix:$DB_SOCKET \
107             --remote=db:Open_vSwitch,manager_options \
108             --private-key=db:SSL,private_key \
109             --certificate=db:SSL,certificate \
110             --bootstrap-ca-cert=db:SSL,ca_cert \
111             --pidfile=$DB_PID_FILE \
112             --log-file=$DB_LOG \
113             --unixctl=$DB_CTRL_SOCKET \
114             --detach >& /dev/null
115     else
116         echo 'ovsdb-server appears to be running already, *not* starting'
117     fi
118     wait_server $DB_PID_FILE ovsdb-server 30
119     echo $DB_PID_FILE
120 }
121
122 function start_switch () {
123     get_params "" "$@"
124
125     # ensure ovsdb-server is running
126     is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
127
128     if [ ! -f "$SWITCH_PID_FILE" ] ; then
129         ovs-vswitchd \
130             --pidfile=$SWITCH_PID_FILE \
131             --log-file=$SWITCH_LOG \
132             --unixctl=$SWITCH_SOCKET \
133             --detach \
134             unix:$DB_SOCKET >& /dev/null
135     else
136         echo 'ovs-vswitchd appears to be running already, *not* starting'
137     fi
138     wait_server $SWITCH_PID_FILE ovs-vswitchd 30
139 }
140
141 # first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
142 # quick and dirty : we locate the control file through a search in /var/run
143 # caller should be requested to remember and provide this pid instead
144 function stop_db () { 
145     controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
146     [ -f $controlfile ] && ovs-appctl --target=$controlfile exit 
147 }
148
149 function stop_switch () { 
150     ovs-appctl --target=$SWITCH_SOCKET exit || :
151 }
152
153 function status () {
154     pids=$(pgrep '^ovs')
155     [ -n "$pids" ] && ps $pids
156 }
157
158 function start () {
159     start_db
160     start_switch
161 }
162
163 function stop () {
164     stop_switch
165     stop_db
166 }
167
168 #################### create functions
169 function create_bridge () {
170     
171     get_params "IP/PREFIX" "$@"
172
173     IP=${IP_PREFIX%/*}
174     PREFIX=${IP_PREFIX#*/}
175
176     set -e
177     # ensure ovs-vswitchd is running
178     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
179
180     # check whether the address is already assigned
181     TAPNAME=$(tapname $IP)
182     if [ ! -z "$TAPNAME" ]; then
183         if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
184             echo $TAPNAME
185             exit 0
186         fi
187         kill_pltap_ovs
188         error "$IP already assigned to $TAPNAME"
189     fi
190
191     # we're clear
192     TAPNAME=$(pltap-ovs)
193     trap kill_pltap_ovs EXIT
194     # xxx wouldn't that be safer if left-aligned ?
195     vsysc vif_up << EOF
196         $TAPNAME
197         $IP
198         $PREFIX
199 EOF
200     wait_device $TAPNAME 60 && \
201         ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
202     echo $TAPNAME
203     return 0
204 }
205
206 function create_port () {
207
208     get_params "bridge port" "$@"
209
210     # ensure ovs-vswitchd is running
211     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
212
213     set -e
214     if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
215         ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
216     fi
217     ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
218     return 0
219 }
220
221 function set_remote_endpoint () {
222
223     get_params "local_port remote_ip remote_UDP_port" "$@"
224
225     # ensure ovs-vswitchd is running
226     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
227
228     set -e
229     ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
230         options:remote_ip=$remote_ip \
231         options:remote_port=$remote_UDP_port
232     return 0
233 }
234
235 #################### del functions
236 function del_bridge () {
237     
238     get_params "bridge_name" "$@"
239
240     W=
241     if ! is_switch_running; then
242         # we can delete the bridge even if ovs-vswitchd is not running,
243         # but we need a running ovsdb-server
244         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
245         W="--no-wait"
246     fi
247
248     if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
249         ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
250     fi
251     return 0
252 }
253
254 function del_port () {
255     
256     get_params "port" "$@"
257
258     W=
259     if ! is_switch_running; then
260         # we can delete the port even if ovs-vswitchd is not running,
261         # but we need a running ovsdb-server
262         is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
263         W="--no-wait"
264     fi
265
266     set -e
267     if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
268         ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
269     fi
270     return 0
271 }
272
273 function show () {
274
275     get_params "" "$@"
276
277     is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
278
279     ovs-vsctl --db=unix:$DB_SOCKET show
280 }
281
282 ####################
283 SUPPORTED_SUBCOMMANDS="start stop status 
284 start_db stop_db start_switch stop_switch
285 create_bridge create_port del_bridge del_port
286 show set_remote_endpoint"
287
288 function main () {
289         message="Usage: $COMMAND <subcommand> ...
290 Supported subcommands are (dash or underscore is the same):
291 $SUPPORTED_SUBCOMMANDS"
292         [[ -z "$@" ]] && error "$message"
293
294         SUBCOMMAND=$1; shift
295         # support dashes instead of underscores
296         SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
297         found=""
298         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
299
300         [ -z "$found" ] && error $message
301
302         $SUBCOMMAND "$@"
303 }
304
305 main "$@"