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