4 ### expected to be run as root
8 #################### global vars
10 DB_CONF_FILE=@DBDIR@/conf.db
11 DB_SCHEMA=@pkgdatadir@/vswitch.ovsschema
12 DB_PID_FILE=$RUN_DIR/db.pid
13 DB_LOG=@LOGDIR@/ovs-db.log
14 DB_CTL_PATTERN='ovsdb-server.*.ctl'
16 DB_SOCKET=$RUN_DIR/db.sock
18 SWITCH_PID_FILE=$RUN_DIR/switch.pid
19 SWITCH_LOG=@LOGDIR@/ovs-switch.log
20 SWITCH_SOCKET=@RUNDIR@/switch.sock
22 #################### helper functions
24 function kill_pltap_ovs () {
25 killall pltap-ovs 2>/dev/null || :
35 err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
36 for p in $(echo $params); do
37 [[ -z "$@" ]] && error "$err_msg"
38 pname=$(echo -n $p|perl -pe 's/\W/_/g')
39 eval $pname="$1"; shift
41 [[ -n "$@" ]] && error "$err_msg"
44 function is_switch_running {
45 ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
48 function is_db_running {
49 ovs-appctl --target=$DB_CTRL_SOCKET version >& /dev/null
54 echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
57 function wait_server () {
62 expire=$(($(date +%s) + $timeout))
64 ## wait for it to be up - xxx todo - could use a timeout of some kind
65 while [ ! -f "$pid_file" ]; do
66 echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
68 [ $(date +%s) -ge $expire ] && return 1
73 function wait_device () {
77 expire=$(($(date +%s) + $timeout))
79 while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
80 echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
82 [ $(date +%s) -ge $expire ] && return 1
87 ######################################## startup
88 function start_db () {
92 conf_dir=$(dirname $DB_CONF_FILE)
93 [ -d $conf_dir ] || mkdir -p $conf_dir
94 [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
97 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
100 [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
101 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
104 if [ ! -f "$DB_PID_FILE" ]; then
105 ovsdb-server --remote=punix:$DB_SOCKET \
106 --remote=db:Open_vSwitch,manager_options \
107 --private-key=db:SSL,private_key \
108 --certificate=db:SSL,certificate \
109 --bootstrap-ca-cert=db:SSL,ca_cert \
110 --pidfile=$DB_PID_FILE \
112 --unixctl=$DB_CTRL_SOCKET \
113 --detach >& /dev/null
115 echo 'ovsdb-server appears to be running already, *not* starting'
117 wait_server $DB_PID_FILE ovsdb-server 30
121 function start_switch () {
124 # ensure ovsdb-server is running
125 is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
127 if [ ! -f "$SWITCH_PID_FILE" ] ; then
129 --pidfile=$SWITCH_PID_FILE \
130 --log-file=$SWITCH_LOG \
131 --unixctl=$SWITCH_SOCKET \
133 unix:$DB_SOCKET >& /dev/null
135 echo 'ovs-vswitchd appears to be running already, *not* starting'
137 wait_server $SWITCH_PID_FILE ovs-vswitchd 30
140 # first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
141 # quick and dirty : we locate the control file through a search in /var/run
142 # caller should be requested to remember and provide this pid instead
143 function stop_db () {
144 controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
145 [ -f $controlfile ] && ovs-appctl --target=$controlfile exit
148 function stop_switch () {
149 ovs-appctl --target=$SWITCH_SOCKET exit || :
154 [ -n "$pids" ] && ps $pids
167 #################### create functions
168 function create_bridge () {
170 get_params "IP/PREFIX" "$@"
173 PREFIX=${IP_PREFIX#*/}
176 # ensure ovs-vswitchd is running
177 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
179 # check whether the address is already assigned
180 TAPNAME=$(tapname $IP)
181 if [ ! -z "$TAPNAME" ]; then
182 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
187 error "$IP already assigned to $TAPNAME"
192 trap kill_pltap_ovs EXIT
193 # xxx wouldn't that be safer if left-aligned ?
199 wait_device $TAPNAME 60 && \
200 ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
205 function create_port () {
207 get_params "bridge port" "$@"
209 # ensure ovs-vswitchd is running
210 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
213 if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
214 ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
216 ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
220 function set_remote_endpoint () {
222 get_params "local_port remote_ip remote_UDP_port" "$@"
224 # ensure ovs-vswitchd is running
225 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
228 ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
229 options:remote_ip=$remote_ip \
230 options:remote_port=$remote_UDP_port
234 #################### del functions
235 function del_bridge () {
237 get_params "bridge_name" "$@"
240 if ! is_switch_running; then
241 # we can delete the bridge even if ovs-vswitchd is not running,
242 # but we need a running ovsdb-server
243 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
247 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
248 ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
253 function del_port () {
255 get_params "port" "$@"
258 if ! is_switch_running; then
259 # we can delete the port even if ovs-vswitchd is not running,
260 # but we need a running ovsdb-server
261 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
266 if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
267 ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
276 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
278 ovs-vsctl --db=unix:$DB_SOCKET show
282 SUPPORTED_SUBCOMMANDS="start stop status
283 start_db stop_db start_switch stop_switch
284 create_bridge create_port del_bridge del_port
285 show set_remote_endpoint"
288 message="Usage: $COMMAND <subcommand> ...
289 Supported subcommands are (dash or underscore is the same):
290 $SUPPORTED_SUBCOMMANDS"
291 [[ -z "$@" ]] && error "$message"
294 # support dashes instead of underscores
295 SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
297 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
299 [ -z "$found" ] && error $message