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