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_LOG=@LOGDIR@/ovs-db.log
14 DB_SOCKET=$RUN_DIR/db.sock
16 SWITCH_LOG=@LOGDIR@/ovs-switch.log
18 #################### helper functions
20 function kill_pltap_ovs () {
21 killall pltap-ovs 2>/dev/null || :
31 err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
32 for p in $(echo $params); do
33 [[ -z "$@" ]] && error "$err_msg"
34 pname=$(echo -n $p|perl -pe 's/\W/_/g')
35 eval $pname="$1"; shift
37 [[ -n "$@" ]] && error "$err_msg"
40 function is_switch_running {
41 ovs-appctl --target=ovs-vswitchd version >& /dev/null
44 function is_db_running {
45 ovs-appctl --target=ovsdb-server version >& /dev/null
50 echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
53 function wait_server () {
58 expire=$(($(date +%s) + $timeout))
60 ## wait for it to be up - xxx todo - could use a timeout of some kind
61 while [ ! -f "$pid_file" ]; do
62 echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
64 [ $(date +%s) -ge $expire ] && return 1
69 function wait_device () {
73 expire=$(($(date +%s) + $timeout))
75 while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
76 echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
78 [ $(date +%s) -ge $expire ] && return 1
83 ######################################## startup
84 function start_db () {
88 conf_dir=$(dirname $DB_CONF_FILE)
89 [ -d $conf_dir ] || mkdir -p $conf_dir
90 [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
93 [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
96 [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
97 [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
100 if [ ! -f "$RUN_DIR/ovsdb-server.pid" ]; then
101 ovsdb-server $DB_CONF_FILE \
102 --remote=punix:$DB_SOCKET \
103 --remote=db:Open_vSwitch,manager_options \
104 --private-key=db:SSL,private_key \
105 --certificate=db:SSL,certificate \
106 --bootstrap-ca-cert=db:SSL,ca_cert \
109 --detach >& /dev/null
111 echo 'ovsdb-server appears to be running already, *not* starting'
113 wait_server $RUN_DIR/ovsdb-server.pid ovsdb-server 30
116 function start_switch () {
119 # ensure ovsdb-server is running
120 is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
122 if [ ! -f "$RUN_DIR/ovs-vswitchd.pid" ] ; then
125 --log-file=$SWITCH_LOG \
127 unix:$DB_SOCKET >& /dev/null
129 echo 'ovs-vswitchd appears to be running already, *not* starting'
131 wait_server $RUN_DIR/ovs-vswitchd.pid ovs-vswitchd 30
134 function stop_db () {
135 ovs-appctl --target=ovsdb-server exit || :
138 function stop_switch () {
139 ovs-appctl --target=ovs-vswitchd exit || :
144 [ -n "$pids" ] && ps $pids
157 #################### create functions
158 function create_bridge () {
160 get_params "bridge IP/PREFIX" "$@"
163 PREFIX=${IP_PREFIX#*/}
166 if ! is_switch_running; then
167 # we can create the bridge even if ovs-vswitchd is not running,
168 # but we need a running ovsdb-server
169 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
175 ovs-vsctl --db=unix:$DB_SOCKET $W -- --may-exist add-br "$bridge" \
176 -- set bridge "$bridge" datapath_type=planetlab \
177 -- set interface "$bridge" options:local_ip="$IP" option:local_netmask="$PREFIX"
180 function create_port () {
182 get_params "bridge port" "$@"
184 # ensure ovs-vswitchd is running
185 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
188 ovs-vsctl --db=unix:$DB_SOCKET -- --may-exist add-port "$bridge" "$port" -- set interface "$port" type=tunnel
189 ovs-appctl --target=ovs-vswitchd netdev-tunnel/get-port "$port"
192 function set_remote_endpoint () {
194 get_params "local_port remote_ip remote_UDP_port" "$@"
196 # ensure ovs-vswitchd is running
197 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
200 ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
201 options:remote_ip=$remote_ip \
202 options:remote_port=$remote_UDP_port
205 function set_controller () {
207 get_params "bridge_name controller" "$@"
209 # ensure ovs-vswitchd is running
210 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
213 ovs-vsctl --db=unix:$DB_SOCKET set-controller "$bridge_name" "$controller"
216 function del_controller () {
218 get_params "bridge_name" "$@"
220 # ensure ovs-vswitchd is running
221 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
224 ovs-vsctl --db=unix:$DB_SOCKET del-controller "$bridge_name"
227 #################### del functions
228 function del_bridge () {
230 get_params "bridge_name" "$@"
233 if ! is_switch_running; then
234 # we can delete the bridge even if ovs-vswitchd is not running,
235 # but we need a running ovsdb-server
236 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
240 ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-br $bridge_name
243 function del_port () {
245 get_params "port" "$@"
248 if ! is_switch_running; then
249 # we can delete the port even if ovs-vswitchd is not running,
250 # but we need a running ovsdb-server
251 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
255 ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-port "$port"
262 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
264 ovs-vsctl --db=unix:$DB_SOCKET show
267 ### for demos - connect to an ndnmap deployment to visualize links bandwidth
268 # this expects 3 arguments
269 # an interface name, L<id1>-<id2> based on your ids in conf.mk
270 # the hostname for a ndnmap deployment
271 # a linkid, this is the id that this link has in your ndnmap scenario (hard-coded in some json file)
272 # this one-shot function writes the current statistics onto the ndnmap site
273 # it needs to be called regularly so that ndnmap can do the bw computations
274 # would make sense for the caller to redirect stderr onto some relevant location
275 function gmap_report_once () {
279 rx_bytes=$(ovs-appctl netdev-tunnel/get-rx-bytes $iface)
280 tx_bytes=$(ovs-appctl netdev-tunnel/get-tx-bytes $iface)
281 rx_bits=$(($rx_bytes*8))
282 tx_bits=$(($tx_bytes*8))
283 now=$(date +%s).$(date +%N)
284 trigger=http://${hostname}/bw/${linkid}/${now}/${rx_bits}/${tx_bits}
285 # curl -s -L $trigger | grep -q "Got it" || echo Missed event with $trigger
287 curl -s -L $trigger >& /dev/null
290 ### the front end, manages pid and so on
291 function gmap_report () {
296 [ -z "$looptime" ] && looptime=1
297 pid_file=/var/run/openvswitch/gmap-$iface.pid
298 if [ -f $pid_file ] ; then
300 [ -n "$pid" ] && kill $pid >& /dev/null
304 gmap_report_once $iface $hostname $linkid
307 # this is the pid for the background process
312 SUPPORTED_SUBCOMMANDS="start stop status
313 start_db stop_db start_switch stop_switch
314 create_bridge create_port del_bridge del_port
315 show set_remote_endpoint set_controller del_controller gmap_report"
318 message="Usage: $COMMAND <subcommand> ...
319 Supported subcommands are (dash or underscore is the same):
320 $SUPPORTED_SUBCOMMANDS"
321 [[ -z "$@" ]] && error "$message"
324 # support dashes instead of underscores
325 SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
327 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
329 [ -z "$found" ] && error $message