specify where the switch daemon creates its control socket and pass
[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=/usr/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 #################### create functions
114 function create_bridge () {
115     
116     [[ -z "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
117     ip_prefix=$1; shift
118     [[ -n "$@" ]] && error "Usage: ${COMMAND} create-bridge <IP/PREFIX>"
119
120     IP=${ip_prefix%/*}
121     PREFIX=${ip_prefix#*/}
122
123     set -e
124     # ensure ovs-vswitchd is running
125     is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
126
127     # check whether the address is already assigned
128     TAPNAME=$(tapname $IP)
129     if [ ! -z "$TAPNAME" ]; then
130         if ovs-vsctl br-exists "$TAPNAME"; then
131             echo $TAPNAME
132             exit 0
133         fi
134         kill_pltap_ovs
135         error "$IP already assigned to $TAPNAME"
136     fi
137
138     # we're clear
139     TAPNAME=$(pltap-ovs)
140     # xxx wouldn't that be safer if left-aligned ?
141     vsysc vif_up << EOF
142         $TAPNAME
143         $IP
144         $PREFIX
145 EOF
146     while ! ip link show up | egrep -q "^[0-9]+: +$TAPNAME:"; do
147         echo "Waiting for $TAPNAME to come UP..." >&2
148         sleep 1
149     done
150     ovs-vsctl add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
151     echo $TAPNAME
152     return 0
153 }
154
155 function create_port () {
156
157     bridge=$1; shift
158     [[ -z "$@" ]] || error "$COMMAND create-port <bridge> <port>"
159     port=$1; shift
160     [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
161
162     set -e
163     if ! ovs-vsctl list-ports "$bridge" | grep -q "^$port\$"; then
164         ovs-vsctl add-port "$bridge" "$port" -- set interface "$port" type=tunnel
165     fi
166     ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
167     return 0
168 }
169
170 #################### del functions
171 function del_bridge () {
172     
173     [[ -z "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
174     bridge_name=$1; shift
175     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
176
177     W=
178     is_switch_running || W="--no-wait"
179
180     if ovs-vsctl br-exists "$bridge_name"; then
181         ovs-vsctl $W del-br $bridge_name
182     fi
183     return 0
184 }
185
186 function del_port () {
187     [[ -z "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
188     bridge_name=$1; shift
189     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
190
191     set -e
192     if ovs-vsctl port-to-br "$1" >/dev/null 2>&1; then
193         ovs-vsctl del-port "$1"
194     fi
195     return 0
196 }
197
198 ####################
199 SUPPORTED_SUBCOMMANDS="start_db stop_db start_switch stop_switch status create_bridge create_port del_bridge del_port"
200
201 function main () {
202         message="Usage: $COMMAND <subcommand> ...
203 Supported subcommands are (dash or underscore is the same):
204 $SUPPORTED_SUBCOMMANDS"
205         [[ -z "$@" ]] && error "$message"
206
207         subcommand=$1; shift
208         # support dashes instead of underscores
209         subcommand=$(echo $subcommand | sed -e s,-,_,)
210         found=""
211         for supported in $SUPPORTED_SUBCOMMANDS; do [ "$subcommand" = "$supported" ] && found=yes; done
212
213         [ -z "$found" ] && error $message
214
215         $subcommand "$@"
216 }
217
218 main "$@"