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 $DB_CONF_FILE \
106 --remote=punix:$DB_SOCKET \
107 --remote=db:Open_vSwitch,manager_options \
108 --private-key=db:SSL,private_key \
109 --certificate=db:SSL,certificate \
110 --bootstrap-ca-cert=db:SSL,ca_cert \
111 --pidfile=$DB_PID_FILE \
113 --unixctl=$DB_CTRL_SOCKET \
114 --detach >& /dev/null
116 echo 'ovsdb-server appears to be running already, *not* starting'
118 wait_server $DB_PID_FILE ovsdb-server 30
122 function start_switch () {
125 # ensure ovsdb-server is running
126 is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
128 if [ ! -f "$SWITCH_PID_FILE" ] ; then
130 --pidfile=$SWITCH_PID_FILE \
131 --log-file=$SWITCH_LOG \
132 --unixctl=$SWITCH_SOCKET \
134 unix:$DB_SOCKET >& /dev/null
136 echo 'ovs-vswitchd appears to be running already, *not* starting'
138 wait_server $SWITCH_PID_FILE ovs-vswitchd 30
141 # first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
142 # quick and dirty : we locate the control file through a search in /var/run
143 # caller should be requested to remember and provide this pid instead
144 function stop_db () {
145 controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
146 [ -f $controlfile ] && ovs-appctl --target=$controlfile exit
149 function stop_switch () {
150 ovs-appctl --target=$SWITCH_SOCKET exit || :
155 [ -n "$pids" ] && ps $pids
168 #################### create functions
169 function create_bridge () {
171 get_params "IP/PREFIX" "$@"
174 PREFIX=${IP_PREFIX#*/}
177 # ensure ovs-vswitchd is running
178 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
180 # check whether the address is already assigned
181 TAPNAME=$(tapname $IP)
182 if [ ! -z "$TAPNAME" ]; then
183 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
188 error "$IP already assigned to $TAPNAME"
193 trap kill_pltap_ovs EXIT
194 # xxx wouldn't that be safer if left-aligned ?
200 wait_device $TAPNAME 60 && \
201 ovs-vsctl --db=unix:$DB_SOCKET add-br $TAPNAME -- set bridge $TAPNAME datapath_type=planetlab
206 function create_port () {
208 get_params "bridge port" "$@"
210 # ensure ovs-vswitchd is running
211 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
214 if ! ovs-vsctl --db=unix:$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
215 ovs-vsctl --db=unix:$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
217 ovs-appctl --target=$SWITCH_SOCKET netdev-tunnel/get-port "$port"
221 function set_remote_endpoint () {
223 get_params "local_port remote_ip remote_UDP_port" "$@"
225 # ensure ovs-vswitchd is running
226 is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
229 ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
230 options:remote_ip=$remote_ip \
231 options:remote_port=$remote_UDP_port
235 #################### del functions
236 function del_bridge () {
238 get_params "bridge_name" "$@"
241 if ! is_switch_running; then
242 # we can delete the bridge even if ovs-vswitchd is not running,
243 # but we need a running ovsdb-server
244 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
248 if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$bridge_name"; then
249 ovs-vsctl --db=unix:$DB_SOCKET $W del-br $bridge_name
254 function del_port () {
256 get_params "port" "$@"
259 if ! is_switch_running; then
260 # we can delete the port even if ovs-vswitchd is not running,
261 # but we need a running ovsdb-server
262 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
267 if ovs-vsctl --db=unix:$DB_SOCKET port-to-br "$port" >/dev/null 2>&1; then
268 ovs-vsctl --db=unix:$DB_SOCKET $W del-port "$port"
277 is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
279 ovs-vsctl --db=unix:$DB_SOCKET show
283 SUPPORTED_SUBCOMMANDS="start stop status
284 start_db stop_db start_switch stop_switch
285 create_bridge create_port del_bridge del_port
286 show set_remote_endpoint"
289 message="Usage: $COMMAND <subcommand> ...
290 Supported subcommands are (dash or underscore is the same):
291 $SUPPORTED_SUBCOMMANDS"
292 [[ -z "$@" ]] && error "$message"
295 # support dashes instead of underscores
296 SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
298 for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
300 [ -z "$found" ] && error $message