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