+# Filter (multiline) vconn debug messages from ovs-vswitchd.log.
+# Use with vconn_sub() and ofctl_strip()
+print_vconn_debug () { awk -F\| < ovs-vswitchd.log '
+BEGIN { prt=0 }
+/\|vconn\|DBG\|/ { sub(/[ \t]*$/, ""); print $3 "|" $4 "|" $5; prt=1; next }
+$4 != "" { prt=0; next }
+prt==1 { sub(/[ \t]*$/, ""); print $0 }
+'
+}
+
+vconn_sub() {
+ sed '
+s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
+s/No error/Success/
+'
+}
+]
+
+# PARSE_LISTENING_PORT LOGFILE VARIABLE
+#
+# Parses the TCP or SSL port on which a server is listening from
+# LOGFILE, given that the server was told to listen on a kernel-chosen
+# port, and assigns the port number to shell VARIABLE. You should
+# specify the listening remote as ptcp:0:127.0.0.1 or
+# pssl:0:127.0.0.1, or the equivalent with [::1] instead of 127.0.0.1.
+#
+# Here's an example of how to use this with ovsdb-server:
+#
+# ovsdb-server --log-file --remote=ptcp:0:127.0.0.1 ...
+# PARSE_LISTENING_PORT([ovsdb-server.log], [TCP_PORT])
+# # Now $TCP_PORT holds the listening port.
+m4_define([PARSE_LISTENING_PORT],
+ [OVS_WAIT_UNTIL([$2=`sed -n 's/.*0:.*: listening on port \([[0-9]]*\)$/\1/p' "$1"` && test X != X"[$]$2"])])
+
+start_daemon () {
+ "$@" -vconsole:off --detach --no-chdir --pidfile --log-file
+ pid=`cat "$OVS_RUNDIR"/$1.pid`
+ on_exit "kill $pid"
+}
+
+# sim_add SANDBOX
+#
+# Starts a new simulated Open vSwitch instance named SANDBOX. Files related to
+# the instance, such as logs, databases, sockets, and pidfiles, are created in
+# a subdirectory of the main test directory also named SANDBOX. Afterward, the
+# "as" command (see below) can be used to run Open vSwitch utilities in the
+# context of the new sandbox.
+#
+# The new sandbox starts out without any bridges. Use ovs-vsctl in the context
+# of the new sandbox to create a bridge, e.g.:
+#
+# sim_add hv0 # Create sandbox hv0.
+# as hv0 # Set hv0 as default sandbox.
+# ovs-vsctl add-br br0 # Add bridge br0 inside hv0.
+#
+# or:
+#
+# sim_add hv0
+# as hv0 ovs-vsctl add-br br0
+sims=
+sim_add () {
+ echo "adding simulator '$1'"
+
+ sims="$sims $1"
+
+ # Create sandbox.
+ local d="$ovs_base"/$1
+ mkdir "$d" || return 1
+ ovs_setenv $1
+
+ # Create database and start ovsdb-server.
+ : > "$d"/.conf.db.~lock~
+ as $1 ovsdb-tool create "$d"/conf.db "$abs_top_srcdir"/vswitchd/vswitch.ovsschema || return 1
+ as $1 start_daemon ovsdb-server --remote=punix:"$d"/db.sock || return 1
+
+ # Initialize database.
+ as $1 ovs-vsctl --no-wait -- init || return 1
+
+ # Start ovs-vswitchd
+ as $1 start_daemon ovs-vswitchd --enable-dummy=system -vvconn -vofproto_dpif -vunixctl
+}
+
+# "as $1" sets the OVS_*DIR environment variables to point to $ovs_base/$1.
+#
+# "as $1 COMMAND..." sets those variables in a subshell and invokes COMMAND
+# there.
+as() {
+ if test "X$1" != X; then
+ (ovs_setenv $1; shift; $@)
+ else
+ ovs_setenv $1
+ fi
+}
+
+# ovn_init_db DATABASE
+#
+# Creates and initializes the given DATABASE (one of "ovn-sb" or "ovn-nb"),
+# starts its ovsdb-server instance, and sets the appropriate environment
+# variable (OVN_SB_DB or OVN_NB_DB) so that ovn-sbctl or ovn-nbctl uses the
+# database by default.
+#
+# Usually invoked from ovn_start.
+ovn_init_db () {
+ echo "creating $1 database"
+ local d=$ovs_base/$1
+ mkdir "$d" || return 1
+ : > "$d"/.$1.db.~lock~
+ as $1 ovsdb-tool create "$d"/$1.db "$abs_top_srcdir"/ovn/$1.ovsschema
+ as $1 start_daemon ovsdb-server --remote=punix:"$d"/$1.sock "$d"/$1.db
+ local var=`echo $1_db | tr a-z- A-Z_`
+ AS_VAR_SET([$var], [unix:$ovs_base/$1/$1.sock]); export $var
+}
+
+# ovn_start
+#
+# Creates and initializes ovn-sb and ovn-nb databases and starts their
+# ovsdb-server instance, sets appropriate environment variables so that
+# ovn-sbctl and ovn-nbctl use them by default, and starts ovn-northd running
+# against them.
+ovn_start () {
+ ovn_init_db ovn-sb
+ ovn_init_db ovn-nb
+
+ echo "starting ovn-northd"
+ mkdir "$ovs_base"/northd
+ as northd start_daemon ovn-northd \
+ --ovnnb-db=unix:"$ovs_base"/ovn-nb/ovn-nb.sock \
+ --ovnsb-db=unix:"$ovs_base"/ovn-sb/ovn-sb.sock
+}
+
+# Interconnection networks.
+#
+# When multiple sandboxed Open vSwitch instances exist, one will inevitably
+# want to connect them together. These commands allow for that. Conceptually,
+# an interconnection network is a switch for which these functions make it easy
+# to plug into other switches in other sandboxed Open vSwitch instances.
+# Interconnection networks are implemented as bridges in a switch named "main",
+# so to use interconnection networks please avoid working with that switch
+# directly.
+
+# net_add NETWORK
+#
+# Creates a new interconnection network named NETWORK.
+net_add () {
+ test -d "$ovs_base"/main || sim_add main || return 1
+ as main ovs-vsctl add-br "$1"
+}
+
+# net_attach NETWORK BRIDGE
+#
+# Adds a new port to BRIDGE in the default sandbox (as set with as()) and plugs
+# it into the NETWORK interconnection network. NETWORK must already have been
+# created by a previous invocation of net_add. The default sandbox must not be
+# "main".
+net_attach () {
+ local net=$1 bridge=$2
+
+ local port=${sandbox}_$bridge
+ as main ovs-vsctl \
+ -- add-port $net $port \
+ -- set Interface $port options:pstream="punix:$ovs_base/main/$port.sock" options:rxq_pcap="$ovs_base/main/$port-rx.pcap" options:tx_pcap="$ovs_base/main/$port-tx.pcap" \
+ || return 1