ovn-sbctl: Use environment var OVN_SB_DB to find the database by default.
[cascardo/ovs.git] / tests / ofproto-macros.at
1 m4_divert_push([PREPARE_TESTS])
2 [
3 # Strips out uninteresting parts of ovs-ofctl output, as well as parts
4 # that vary from one run to another.
5 ofctl_strip () {
6     sed '
7 s/ (xid=0x[0-9a-fA-F]*)//
8 s/ duration=[0-9.]*s,//
9 s/ cookie=0x0,//
10 s/ table=0,//
11 s/ n_packets=0,//
12 s/ n_bytes=0,//
13 s/ idle_age=[0-9]*,//
14 s/ hard_age=[0-9]*,//
15 '
16 }
17
18 # Filter (multiline) vconn debug messages from ovs-vswitchd.log.
19 # Use with vconn_windows_sub() and ofctl_strip()
20 print_vconn_debug () { awk -F\| < ovs-vswitchd.log '
21 BEGIN { prt=0 }
22 /\|vconn\|DBG\|/ { sub(/[ \t]*$/, ""); print $3 "|" $4 "|" $5; prt=1; next }
23 $4 != "" { prt=0; next }
24 prt==1 { sub(/[ \t]*$/, ""); print $0 }
25 '
26 }
27
28 vconn_windows_sub() {
29     sed '
30 s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
31 s/No error/Success/
32 '
33 }
34
35 # parse_listening_port [SERVER]
36 #
37 # Parses the TCP or SSL port on which a server is listening from the
38 # log, given that the server was told to listen on a kernel-chosen
39 # port, file provided on stdin, and prints the port number on stdout.
40 # You should specify the listening remote as ptcp:0:127.0.0.1 or
41 # pssl:0:127.0.0.1, or the equivalent with [::1] instead of 127.0.0.1.
42 #
43 # Here's an example of how to use this with ovsdb-server:
44 #
45 #    ovsdb-server --log-file --remote=ptcp:0:127.0.0.1 ...
46 #    TCP_PORT=`parse_listening_port < ovsdb-server.log`
47 parse_listening_port () {
48     sed -n 's/.*0:.*: listening on port \([0-9]*\)$/\1/p'
49 }]
50
51 start_daemon () {
52     "$@" -vconsole:off --detach --no-chdir --pidfile --log-file
53     pid=`cat "$OVS_RUNDIR"/$1.pid`
54     on_exit "kill $pid"
55 }
56
57 # sim_add SANDBOX
58 #
59 # Starts a new simulated Open vSwitch instance named SANDBOX.  Files related to
60 # the instance, such as logs, databases, sockets, and pidfiles, are created in
61 # a subdirectory of the main test directory also named SANDBOX.  Afterward, the
62 # "as" command (see below) can be used to run Open vSwitch utilities in the
63 # context of the new sandbox.
64 #
65 # The new sandbox starts out without any bridges.  Use ovs-vsctl in the context
66 # of the new sandbox to create a bridge, e.g.:
67 #
68 #     sim_add hv0           # Create sandbox hv0.
69 #     as hv0                # Set hv0 as default sandbox.
70 #     ovs-vsctl add-br br0  # Add bridge br0 inside hv0.
71 #
72 # or:
73 #
74 #     sim_add hv0
75 #     as hv0 ovs-vsctl add-br br0
76 sims=
77 sim_add () {
78    echo "adding simulator '$1'"
79
80    sims="$sims $1"
81
82    # Create sandbox.
83    local d="$ovs_base"/$1
84    mkdir "$d" || return 1
85    ovs_setenv $1
86
87    # Create database and start ovsdb-server.
88    : > "$d"/.conf.db.~lock~
89    as $1 ovsdb-tool create "$d"/conf.db "$abs_top_srcdir"/vswitchd/vswitch.ovsschema || return 1
90    as $1 start_daemon ovsdb-server --remote=punix:"$d"/db.sock || return 1
91
92    # Initialize database.
93    as $1 ovs-vsctl --no-wait -- init || return 1
94
95    # Start ovs-vswitchd
96    as $1 start_daemon ovs-vswitchd --enable-dummy=system -vvconn -vofproto_dpif
97 }
98
99 # "as $1" sets the OVS_*DIR environment variables to point to $ovs_base/$1.
100 #
101 # "as $1 COMMAND..." sets those variables in a subshell and invokes COMMAND
102 # there.
103 as() {
104     if test "X$1" != X; then
105         (ovs_setenv $1; shift; $@)
106     else
107         ovs_setenv $1
108     fi
109 }
110
111 # ovn_init_db DATABASE
112 #
113 # Creates and initializes the given DATABASE (one of "ovn-sb" or "ovn-nb"),
114 # starts its ovsdb-server instance, and sets the appropriate environment
115 # variable (OVN_SB_DB or OVN_NB_DB) so that ovn-sbctl or ovn-nbctl uses the
116 # database by default.
117 #
118 # Usually invoked from ovn_start.
119 ovn_init_db () {
120     echo "creating $1 database"
121     local d=$ovs_base/$1
122     mkdir "$d" || return 1
123     : > "$d"/.$1.db.~lock~
124     as $1 ovsdb-tool create "$d"/$1.db "$abs_top_srcdir"/ovn/$1.ovsschema
125     as $1 start_daemon ovsdb-server --remote=punix:"$d"/$1.sock "$d"/$1.db
126     local var=`echo $1_db | tr a-z- A-Z_`
127     AS_VAR_SET([$var], [unix:$ovs_base/$1/$1.sock]); export $var
128 }
129
130 # ovn_start
131 #
132 # Creates and initializes ovn-sb and ovn-nb databases and starts their
133 # ovsdb-server instance, sets appropriate environment variables so that
134 # ovn-sbctl and ovn-nbctl use them by default, and starts ovn-northd running
135 # against them.
136 ovn_start () {
137     ovn_init_db ovn-sb
138     ovn_init_db ovn-nb
139
140     echo "starting ovn-northd"
141     mkdir "$ovs_base"/northd
142     as northd start_daemon ovn-northd \
143                --ovnnb-db=unix:"$ovs_base"/ovn-nb/ovn-nb.sock \
144                --ovnsb-db=unix:"$ovs_base"/ovn-sb/ovn-sb.sock
145 }
146
147 # Interconnection networks.
148 #
149 # When multiple sandboxed Open vSwitch instances exist, one will inevitably
150 # want to connect them together.  These commands allow for that.  Conceptually,
151 # an interconnection network is a switch for which these functions make it easy
152 # to plug into other switches in other sandboxed Open vSwitch instances.
153 # Interconnection networks are implemented as bridges in a switch named "main",
154 # so to use interconnection networks please avoid working with that switch
155 # directly.
156
157 # net_add NETWORK
158 #
159 # Creates a new interconnection network named NETWORK.
160 net_add () {
161     test -d "$ovs_base"/main || sim_add main || return 1
162     as main ovs-vsctl add-br "$1"
163 }
164
165 # net_attach NETWORK BRIDGE
166 #
167 # Adds a new port to BRIDGE in the default sandbox (as set with as()) and plugs
168 # it into the NETWORK interconnection network.  NETWORK must already have been
169 # created by a previous invocation of net_add.  The default sandbox must not be
170 # "main".
171 net_attach () {
172     local net=$1 bridge=$2
173
174     local port=${sandbox}_$bridge
175     as main ovs-vsctl \
176         -- add-port $net $port \
177         -- 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" \
178         || return 1
179
180     ovs-vsctl \
181         -- set Interface $bridge options:tx_pcap="$ovs_base/$sandbox/$bridge-tx.pcap" options:rxq_pcap="$ovs_base/$sandbox/$bridge-rx.pcap" \
182         -- add-port $bridge ${bridge}_$net \
183         -- set Interface ${bridge}_$net options:stream="unix:$ovs_base/main/$port.sock" options:rxq_pcap="$ovs_base/$sandbox/${bridge}_$net-rx.pcap" options:tx_pcap="$ovs_base/$sandbox/${bridge}_$net-tx.pcap" \
184         || return 1
185 }
186
187 # ovn_attach NETWORK BRIDGE IP [MASKLEN]
188 #
189 # First, this command attaches BRIDGE to interconnection network NETWORK, just
190 # like "net_attach NETWORK BRIDGE".  Second, it configures (simulated) IP
191 # address IP (with network mask length MASKLEN, which defaults to 24) on
192 # BRIDGE.  Finally, it configures the Open vSwitch database to work with OVN
193 # and starts ovn-controller.
194 ovn_attach() {
195     local net=$1 bridge=$2 ip=$3 masklen=${4-24}
196     net_attach $net $bridge || return 1
197
198     mac=`ovs-vsctl get Interface $bridge mac_in_use | sed s/\"//g`
199     arp_table="$arp_table $sandbox,$bridge,$ip,$mac"
200     ovs-appctl netdev-dummy/ip4addr $bridge $ip/$masklen >/dev/null || return 1
201     ovs-appctl ovs/route/add $ip/$masklen $bridge >/dev/null || return 1
202     ovs-vsctl \
203         -- set Open_vSwitch . external-ids:system-id=$sandbox \
204         -- set Open_vSwitch . external-ids:ovn-remote=unix:$ovs_base/ovn-sb/ovn-sb.sock \
205         -- set Open_vSwitch . external-ids:ovn-encap-type=geneve \
206         -- set Open_vSwitch . external-ids:ovn-encap-ip=$ip \
207         -- add-br br-int \
208         -- set bridge br-int fail-mode=secure other-config:disable-in-band=true \
209         || return 1
210     start_daemon ovn-controller || return 1
211 }
212
213 # ovn_populate_arp
214 #
215 # This pre-populates the ARP tables of all of the OVN instances that have been
216 # started with ovn_attach().  That means that packets sent from one hypervisor
217 # to another never get dropped or delayed by ARP resolution, which makes
218 # testing easier.
219 ovn_populate_arp() {
220     for e1 in $arp_table; do
221         set `echo $e1 | sed 's/,/ /g'`; sb1=$1 br1=$2 ip=$3 mac=$4
222         for e2 in $arp_table; do
223             set `echo $e2 | sed 's/,/ /g'`; sb2=$1 br2=$2
224             if test $sb1,$br1 != $sb2,$br2; then
225                 as $sb2 ovs-appctl tnl/arp/set $br2 $ip $mac
226             fi
227         done
228     done
229 }
230 m4_divert_pop([PREPARE_TESTS])
231
232 m4_define([STRIP_XIDS], [[sed 's/ (xid=0x[0-9a-fA-F]*)//']])
233 m4_define([STRIP_DURATION], [[sed 's/\bduration=[0-9.]*s/duration=?s/']])
234 m4_define([STRIP_USED], [[sed 's/used:[0-9]\.[0-9]*/used:0.0/']])
235 m4_define([STRIP_UFID], [[sed 's/ufid:[-0-9a-f]* //']])
236 m4_define([TESTABLE_LOG], [-vPATTERN:ANY:'%c|%p|%m'])
237
238 # _OVS_VSWITCHD_START([vswitchd-aux-args])
239 #
240 # Creates an empty database and starts ovsdb-server.
241 # Starts ovs-vswitchd, with additional arguments 'vswitchd-aux-args'.
242 #
243 m4_define([_OVS_VSWITCHD_START],
244   [dnl Create database.
245    touch .conf.db.~lock~
246    AT_CHECK([ovsdb-tool create conf.db $abs_top_srcdir/vswitchd/vswitch.ovsschema])
247
248    dnl Start ovsdb-server.
249    AT_CHECK([ovsdb-server --detach --no-chdir --pidfile --log-file --remote=punix:$OVS_RUNDIR/db.sock], [0], [], [stderr])
250    on_exit "kill `cat ovsdb-server.pid`"
251    AT_CHECK([[sed < stderr '
252 /vlog|INFO|opened log file/d
253 /ovsdb_server|INFO|ovsdb-server (Open vSwitch)/d']])
254    AT_CAPTURE_FILE([ovsdb-server.log])
255
256    dnl Initialize database.
257    AT_CHECK([ovs-vsctl --no-wait init])
258
259    dnl Start ovs-vswitchd.
260    AT_CHECK([ovs-vswitchd $1 --detach --no-chdir --pidfile --log-file -vvconn -vofproto_dpif], [0], [], [stderr])
261    AT_CAPTURE_FILE([ovs-vswitchd.log])
262    on_exit "kill `cat ovs-vswitchd.pid`"
263    AT_CHECK([[sed < stderr '
264 /ovs_numa|INFO|Discovered /d
265 /vlog|INFO|opened log file/d
266 /vswitchd|INFO|ovs-vswitchd (Open vSwitch)/d
267 /reconnect|INFO|/d
268 /ofproto|INFO|using datapath ID/d
269 /ofproto|INFO|datapath ID changed to fedcba9876543210/d']])
270 ])
271
272 # OVS_VSWITCHD_START([vsctl-args], [vsctl-output], [=override])
273 #
274 # Creates a database and starts ovsdb-server, starts ovs-vswitchd
275 # connected to that database, calls ovs-vsctl to create a bridge named
276 # br0 with predictable settings, passing 'vsctl-args' as additional
277 # commands to ovs-vsctl.  If 'vsctl-args' causes ovs-vsctl to provide
278 # output (e.g. because it includes "create" commands) then 'vsctl-output'
279 # specifies the expected output after filtering through uuidfilt.pl.
280 #
281 # If a test needs to use "system" devices (as dummies), then specify
282 # =override (literally) as the third argument.  Otherwise, system devices
283 # won't work at all (which makes sense because tests should not access a
284 # system's real Ethernet devices).
285 m4_define([OVS_VSWITCHD_START],
286   [_OVS_VSWITCHD_START([--enable-dummy$3 --disable-system])
287
288    dnl Add bridges, ports, etc.
289    AT_CHECK([ovs-vsctl -- add-br br0 -- set bridge br0 datapath-type=dummy other-config:datapath-id=fedcba9876543210 other-config:hwaddr=aa:55:aa:55:00:00 protocols=[[OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15]] fail-mode=secure -- $1 m4_if([$2], [], [], [| ${PERL} $srcdir/uuidfilt.pl])], [0], [$2])
290 ])
291
292 # check_logs scans through all *.log files (except '*.log' and testsuite.log)
293 # and reports all WARN, ERR, EMER log entries.  User can add custom sed filters
294 # in $1.
295 m4_divert_push([PREPARE_TESTS])
296 check_logs () {
297     local logs
298     for log in *.log; do
299         case ${log} in # (
300             '*.log'|testsuite.log) ;; # (
301             *) logs="${logs} ${log}" ;;
302         esac
303     done
304
305     sed -n "$1
306 /timeval.*Unreasonably long [[0-9]]*ms poll interval/d
307 /timeval.*faults: [[0-9]]* minor, [[0-9]]* major/d
308 /timeval.*disk: [[0-9]]* reads, [[0-9]]* writes/d
309 /timeval.*context switches: [[0-9]]* voluntary, [[0-9]]* involuntary/d
310 /ovs_rcu.*blocked [[0-9]]* ms waiting for .* to quiesce/d
311 /|WARN|/p
312 /|ERR|/p
313 /|EMER|/p" ${logs}
314 }
315 m4_divert_pop([PREPARE_TESTS])
316
317 # OVS_VSWITCHD_STOP([WHITELIST])
318 #
319 # Gracefully stops ovs-vswitchd and ovsdb-server, checking their log files
320 # for messages with severity WARN or higher and signaling an error if any
321 # is present.  The optional WHITELIST may contain shell-quoted "sed"
322 # commands to delete any warnings that are actually expected, e.g.:
323 #
324 #   OVS_VSWITCHD_STOP(["/expected error/d"])
325 m4_define([OVS_VSWITCHD_STOP],
326   [AT_CHECK([check_logs $1])
327    AT_CHECK([ovs-appctl -t ovs-vswitchd exit])
328    AT_CHECK([ovs-appctl -t ovsdb-server exit])])
329
330 m4_define([OVS_VSWITCHD_DISABLE_TUNNEL_PUSH_POP],
331   [AT_CHECK([ovs-appctl ofproto/tnl-push-pop off], [0], [dnl
332 Tunnel push-pop off
333 ])])
334
335 # ADD_OF_PORTS(BRIDGE, OF_PORT[, OF_PORT...])
336 #
337 # Creates a dummy interface with an OpenFlow port number of OF_PORT and
338 # name of p{OF_PORT}.  The dummy implementation will treat the OF_PORT
339 # as the datapath port number, which as the effect of making the
340 # OpenFlow and datapath numbers the same.
341 m4_define([ADD_OF_PORTS],
342  [ovs-vsctl m4_foreach([of_port], m4_cdr($@),
343     [ \
344     -- add-port $1 p[]of_port -- set Interface p[]of_port type=dummy ofport_request=of_port])])
345
346 # WAIT_FOR_DUMMY_PORTS(NETDEV_DUMMY_PORT[, NETDEV_DUMMY_PORT...])
347 #
348 # Wait until the netdev dummy ports are connected to each other
349 m4_define([WAIT_FOR_DUMMY_PORTS], \
350   [m4_foreach([dummy_port], [$@],
351       [  \
352          OVS_WAIT_WHILE([ovs-appctl netdev-dummy/conn-state dummy_port \
353                   | grep 'unknown\|disconnected'])])])