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