da49eb24d36fae42fd1b57a39c09df90402d8125
[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 s/dp_hash=0x[0-9a-f]*\//dp_hash=0x0\//
16 s/recirc_id=0x[0-9a-f]*,/recirc_id=0x0,/
17 '
18 }
19
20 # Filter (multiline) vconn debug messages from ovs-vswitchd.log.
21 # Use with vconn_sub() and ofctl_strip()
22 print_vconn_debug () { awk -F\| < ovs-vswitchd.log '
23 BEGIN { prt=0 }
24 /\|vconn\|DBG\|/ { sub(/[ \t]*$/, ""); print $3 "|" $4 "|" $5; prt=1; next }
25 $4 != "" { prt=0; next }
26 prt==1 { sub(/[ \t]*$/, ""); print $0 }
27 '
28 }
29
30 vconn_sub() {
31     sed '
32 s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
33 '
34 }
35 ]
36
37 # PARSE_LISTENING_PORT LOGFILE VARIABLE
38 #
39 # Parses the TCP or SSL port on which a server is listening from
40 # LOGFILE, given that the server was told to listen on a kernel-chosen
41 # port, and assigns the port number to shell VARIABLE.  You should
42 # specify the listening remote as ptcp:0:127.0.0.1 or
43 # pssl:0:127.0.0.1, or the equivalent with [::1] instead of 127.0.0.1.
44 #
45 # Here's an example of how to use this with ovsdb-server:
46 #
47 #    ovsdb-server --log-file --remote=ptcp:0:127.0.0.1 ...
48 #    PARSE_LISTENING_PORT([ovsdb-server.log], [TCP_PORT])
49 #    # Now $TCP_PORT holds the listening port.
50 m4_define([PARSE_LISTENING_PORT],
51     [OVS_WAIT_UNTIL([$2=`sed -n 's/.*0:.*: listening on port \([[0-9]]*\)$/\1/p' "$1"` && test X != X"[$]$2"])])
52
53 start_daemon () {
54     "$@" -vconsole:off --detach --no-chdir --pidfile --log-file
55     pid=`cat "$OVS_RUNDIR"/$1.pid`
56     on_exit "kill $pid"
57 }
58
59 # sim_add SANDBOX
60 #
61 # Starts a new simulated Open vSwitch instance named SANDBOX.  Files related to
62 # the instance, such as logs, databases, sockets, and pidfiles, are created in
63 # a subdirectory of the main test directory also named SANDBOX.  Afterward, the
64 # "as" command (see below) can be used to run Open vSwitch utilities in the
65 # context of the new sandbox.
66 #
67 # The new sandbox starts out without any bridges.  Use ovs-vsctl in the context
68 # of the new sandbox to create a bridge, e.g.:
69 #
70 #     sim_add hv0           # Create sandbox hv0.
71 #     as hv0                # Set hv0 as default sandbox.
72 #     ovs-vsctl add-br br0  # Add bridge br0 inside hv0.
73 #
74 # or:
75 #
76 #     sim_add hv0
77 #     as hv0 ovs-vsctl add-br br0
78 sims=
79 sim_add () {
80    echo "adding simulator '$1'"
81
82    sims="$sims $1"
83
84    # Create sandbox.
85    local d="$ovs_base"/$1
86    mkdir "$d" || return 1
87    ovs_setenv $1
88
89    # Create database and start ovsdb-server.
90    : > "$d"/.conf.db.~lock~
91    as $1 ovsdb-tool create "$d"/conf.db "$abs_top_srcdir"/vswitchd/vswitch.ovsschema || return 1
92    as $1 start_daemon ovsdb-server --remote=punix:"$d"/db.sock || return 1
93
94    # Initialize database.
95    as $1 ovs-vsctl --no-wait -- init || return 1
96
97    # Start ovs-vswitchd
98    as $1 start_daemon ovs-vswitchd --enable-dummy=system -vvconn -vofproto_dpif -vunixctl
99 }
100
101 # "as $1" sets the OVS_*DIR environment variables to point to $ovs_base/$1.
102 #
103 # "as $1 COMMAND..." sets those variables in a subshell and invokes COMMAND
104 # there.
105 as() {
106     if test "X$2" != X; then
107         (ovs_setenv $1; shift; $@)
108     else
109         ovs_setenv $1
110     fi
111 }
112
113 # OVN_CLEANUP_VSWITCH(sim)
114 #
115 # Gracefully terminate vswitch daemons in the
116 # specified sandbox.
117 m4_define([OVN_CLEANUP_VSWITCH],[
118     as $1
119     OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
120     OVS_APP_EXIT_AND_WAIT([ovsdb-server])
121 ])
122
123 # OVN_CLEANUP_SBOX(sbox)
124 #
125 # Gracefully terminate OVN daemons in the specified
126 # sandbox instance. The sandbox name "vtep" is treated
127 # as a special case, and is assumed to have ovn-controller-vtep
128 # and ovs-vtep daemons running instead of ovn-controller.
129 m4_define([OVN_CLEANUP_SBOX],[
130     as $1
131     if test "$1" = "vtep"; then
132         OVS_APP_EXIT_AND_WAIT([ovn-controller-vtep])
133         OVS_APP_EXIT_AND_WAIT([ovs-vtep])
134     else
135         OVS_APP_EXIT_AND_WAIT([ovn-controller])
136     fi
137     OVN_CLEANUP_VSWITCH([$1])
138 ])
139
140 # OVN_CLEANUP(sim [, sim ...])
141 #
142 # Gracefully terminate all OVN daemons, including those in the
143 # specified sandbox instances.
144 m4_define([OVN_CLEANUP],[
145     m4_foreach([sbox], [$@], [
146         OVN_CLEANUP_SBOX([sbox])
147     ])
148     as ovn-sb
149     OVS_APP_EXIT_AND_WAIT([ovsdb-server])
150
151     as ovn-nb
152     OVS_APP_EXIT_AND_WAIT([ovsdb-server])
153
154     as northd
155     OVS_APP_EXIT_AND_WAIT([ovn-northd])
156
157     OVN_CLEANUP_VSWITCH([main])
158 ])
159
160 # ovn_init_db DATABASE
161 #
162 # Creates and initializes the given DATABASE (one of "ovn-sb" or "ovn-nb"),
163 # starts its ovsdb-server instance, and sets the appropriate environment
164 # variable (OVN_SB_DB or OVN_NB_DB) so that ovn-sbctl or ovn-nbctl uses the
165 # database by default.
166 #
167 # Usually invoked from ovn_start.
168 ovn_init_db () {
169     echo "creating $1 database"
170     local d=$ovs_base/$1
171     mkdir "$d" || return 1
172     : > "$d"/.$1.db.~lock~
173     as $1 ovsdb-tool create "$d"/$1.db "$abs_top_srcdir"/ovn/$1.ovsschema
174     as $1 start_daemon ovsdb-server --remote=punix:"$d"/$1.sock "$d"/$1.db
175     local var=`echo $1_db | tr a-z- A-Z_`
176     AS_VAR_SET([$var], [unix:$ovs_base/$1/$1.sock]); export $var
177 }
178
179 # ovn_start
180 #
181 # Creates and initializes ovn-sb and ovn-nb databases and starts their
182 # ovsdb-server instance, sets appropriate environment variables so that
183 # ovn-sbctl and ovn-nbctl use them by default, and starts ovn-northd running
184 # against them.
185 ovn_start () {
186     ovn_init_db ovn-sb
187     ovn_init_db ovn-nb
188
189     echo "starting ovn-northd"
190     mkdir "$ovs_base"/northd
191     as northd start_daemon ovn-northd \
192                --ovnnb-db=unix:"$ovs_base"/ovn-nb/ovn-nb.sock \
193                --ovnsb-db=unix:"$ovs_base"/ovn-sb/ovn-sb.sock
194 }
195
196 # Interconnection networks.
197 #
198 # When multiple sandboxed Open vSwitch instances exist, one will inevitably
199 # want to connect them together.  These commands allow for that.  Conceptually,
200 # an interconnection network is a switch for which these functions make it easy
201 # to plug into other switches in other sandboxed Open vSwitch instances.
202 # Interconnection networks are implemented as bridges in a switch named "main",
203 # so to use interconnection networks please avoid working with that switch
204 # directly.
205
206 # net_add NETWORK
207 #
208 # Creates a new interconnection network named NETWORK.
209 net_add () {
210     test -d "$ovs_base"/main || sim_add main || return 1
211     as main ovs-vsctl add-br "$1"
212 }
213
214 # net_attach NETWORK BRIDGE
215 #
216 # Adds a new port to BRIDGE in the default sandbox (as set with as()) and plugs
217 # it into the NETWORK interconnection network.  NETWORK must already have been
218 # created by a previous invocation of net_add.  The default sandbox must not be
219 # "main".
220 net_attach () {
221     local net=$1 bridge=$2
222
223     local port=${sandbox}_$bridge
224     as main ovs-vsctl \
225         -- add-port $net $port \
226         -- 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" \
227         || return 1
228
229     ovs-vsctl \
230         -- set Interface $bridge options:tx_pcap="$ovs_base/$sandbox/$bridge-tx.pcap" options:rxq_pcap="$ovs_base/$sandbox/$bridge-rx.pcap" \
231         -- add-port $bridge ${bridge}_$net \
232         -- 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" \
233         || return 1
234 }
235
236 # ovn_attach NETWORK BRIDGE IP [MASKLEN]
237 #
238 # First, this command attaches BRIDGE to interconnection network NETWORK, just
239 # like "net_attach NETWORK BRIDGE".  Second, it configures (simulated) IP
240 # address IP (with network mask length MASKLEN, which defaults to 24) on
241 # BRIDGE.  Finally, it configures the Open vSwitch database to work with OVN
242 # and starts ovn-controller.
243 ovn_attach() {
244     local net=$1 bridge=$2 ip=$3 masklen=${4-24}
245     net_attach $net $bridge || return 1
246
247     mac=`ovs-vsctl get Interface $bridge mac_in_use | sed s/\"//g`
248     arp_table="$arp_table $sandbox,$bridge,$ip,$mac"
249     ovs-appctl netdev-dummy/ip4addr $bridge $ip/$masklen >/dev/null || return 1
250     ovs-appctl ovs/route/add $ip/$masklen $bridge >/dev/null || return 1
251     ovs-vsctl \
252         -- set Open_vSwitch . external-ids:system-id=$sandbox \
253         -- set Open_vSwitch . external-ids:ovn-remote=unix:$ovs_base/ovn-sb/ovn-sb.sock \
254         -- set Open_vSwitch . external-ids:ovn-encap-type=geneve,vxlan \
255         -- set Open_vSwitch . external-ids:ovn-encap-ip=$ip \
256         -- add-br br-int \
257         -- set bridge br-int fail-mode=secure other-config:disable-in-band=true \
258         || return 1
259     start_daemon ovn-controller || return 1
260 }
261
262 # ovn_populate_arp
263 #
264 # This pre-populates the ARP tables of all of the OVN instances that have been
265 # started with ovn_attach().  That means that packets sent from one hypervisor
266 # to another never get dropped or delayed by ARP resolution, which makes
267 # testing easier.
268 ovn_populate_arp() {
269     for e1 in $arp_table; do
270         set `echo $e1 | sed 's/,/ /g'`; sb1=$1 br1=$2 ip=$3 mac=$4
271         for e2 in $arp_table; do
272             set `echo $e2 | sed 's/,/ /g'`; sb2=$1 br2=$2
273             if test $sb1,$br1 != $sb2,$br2; then
274                 as $sb2 ovs-appctl tnl/neigh/set $br2 $ip $mac
275             fi
276         done
277     done
278 }
279
280 # Strips 'xid=0x1234' from ovs-ofctl output.
281 strip_xids () {
282     sed 's/ (xid=0x[[0-9a-fA-F]]*)//'
283 }
284
285 # Changes all 'used:...' to say 'used:0.0', to make output easier to compare.
286 strip_used () {
287     sed 's/used:[[0-9]]\.[[0-9]]*/used:0.0/'
288 }
289
290 # Strips 'ufid:...' from output, to make it easier to compare.
291 # (ufids are random.)
292 strip_ufid () {
293     sed 's/ufid:[[-0-9a-f]]* //'
294 }
295 m4_divert_pop([PREPARE_TESTS])
296
297 m4_define([TESTABLE_LOG], [-vPATTERN:ANY:'%c|%p|%m'])
298
299 # _OVS_VSWITCHD_START([vswitchd-aux-args])
300 #
301 # Creates an empty database and starts ovsdb-server.
302 # Starts ovs-vswitchd, with additional arguments 'vswitchd-aux-args'.
303 #
304 m4_define([_OVS_VSWITCHD_START],
305   [dnl Create database.
306    touch .conf.db.~lock~
307    AT_CHECK([ovsdb-tool create conf.db $abs_top_srcdir/vswitchd/vswitch.ovsschema])
308
309    dnl Start ovsdb-server.
310    AT_CHECK([ovsdb-server --detach --no-chdir --pidfile --log-file --remote=punix:$OVS_RUNDIR/db.sock], [0], [], [stderr])
311    on_exit "kill `cat ovsdb-server.pid`"
312    AT_CHECK([[sed < stderr '
313 /vlog|INFO|opened log file/d
314 /ovsdb_server|INFO|ovsdb-server (Open vSwitch)/d']])
315    AT_CAPTURE_FILE([ovsdb-server.log])
316
317    dnl Initialize database.
318    AT_CHECK([ovs-vsctl --no-wait init])
319
320    dnl Start ovs-vswitchd.
321    AT_CHECK([ovs-vswitchd $1 --detach --no-chdir --pidfile --log-file -vvconn -vofproto_dpif -vunixctl], [0], [], [stderr])
322    AT_CAPTURE_FILE([ovs-vswitchd.log])
323    on_exit "kill `cat ovs-vswitchd.pid`"
324    AT_CHECK([[sed < stderr '
325 /ovs_numa|INFO|Discovered /d
326 /vlog|INFO|opened log file/d
327 /vswitchd|INFO|ovs-vswitchd (Open vSwitch)/d
328 /reconnect|INFO|/d
329 /ofproto|INFO|using datapath ID/d
330 /netdev_linux|INFO|.*device has unknown hardware address family/d
331 /ofproto|INFO|datapath ID changed to fedcba9876543210/d
332 /dpdk|INFO|DPDK Disabled - to change this requires a restart./d']])
333 ])
334
335 # OVS_VSWITCHD_START([vsctl-args], [vsctl-output], [=override],
336 #                    [vswitchd-aux-args])
337 #
338 # Creates a database and starts ovsdb-server, starts ovs-vswitchd
339 # connected to that database, calls ovs-vsctl to create a bridge named
340 # br0 with predictable settings, passing 'vsctl-args' as additional
341 # commands to ovs-vsctl.  If 'vsctl-args' causes ovs-vsctl to provide
342 # output (e.g. because it includes "create" commands) then 'vsctl-output'
343 # specifies the expected output after filtering through uuidfilt.pl.
344 #
345 # If a test needs to use "system" devices (as dummies), then specify
346 # =override (literally) as the third argument.  Otherwise, system devices
347 # won't work at all (which makes sense because tests should not access a
348 # system's real Ethernet devices).
349 #
350 # 'vswitchd-aux-args' provides a way to pass extra command line arguments
351 # to ovs-vswitchd
352 m4_define([OVS_VSWITCHD_START],
353   [_OVS_VSWITCHD_START([--enable-dummy$3 --disable-system $4])
354    AT_CHECK([add_of_br 0 $1 m4_if([$2], [], [], [| ${PERL} $srcdir/uuidfilt.pl])], [0], [$2])
355 ])
356
357 # check_logs scans through all *.log files (except '*.log' and testsuite.log)
358 # and reports all WARN, ERR, EMER log entries.  User can add custom sed filters
359 # in $1.
360 m4_divert_push([PREPARE_TESTS])
361 check_logs () {
362     local logs
363     for log in *.log; do
364         case ${log} in # (
365             '*.log'|testsuite.log) ;; # (
366             *) logs="${logs} ${log}" ;;
367         esac
368     done
369
370     sed -n "$1
371 /timeval.*Unreasonably long [[0-9]]*ms poll interval/d
372 /timeval.*faults: [[0-9]]* minor, [[0-9]]* major/d
373 /timeval.*disk: [[0-9]]* reads, [[0-9]]* writes/d
374 /timeval.*context switches: [[0-9]]* voluntary, [[0-9]]* involuntary/d
375 /ovs_rcu.*blocked [[0-9]]* ms waiting for .* to quiesce/d
376 /|WARN|/p
377 /|ERR|/p
378 /|EMER|/p" ${logs}
379 }
380
381 # add_of_br BRNUM [ARG...]
382 add_of_br () {
383     local brnum=$1; shift
384     local br=br$brnum
385     local dpid=fedcba987654321$brnum
386     local mac=aa:55:aa:55:00:0$brnum
387     ovs-vsctl --timeout=20 \
388         -- add-br $br \
389         -- set bridge $br datapath-type=dummy \
390                           fail-mode=secure \
391                           other-config:datapath-id=$dpid \
392                           other-config:hwaddr=$mac \
393                           protocols="[[OpenFlow10,OpenFlow11,OpenFlow12,\
394                                        OpenFlow13,OpenFlow14,OpenFlow15,\
395                                        OpenFlow16]]" \
396         -- "$@"
397 }
398
399 # add_of_ports__ PORT_TYPE [--pcap] BRIDGE PNUM...
400 #
401 # Creates PORT_TYPE interfaces in BRIDGE named pPNUM, OpenFlow port number
402 # PNUM, and datapath port number PNUM (the latter is a consequence of
403 # the dummy implementation, which tries to assign datapath port
404 # numbers based on port names).
405 #
406 # If --pcap is supplied then packets received from the interface will
407 # be written to $port-rx.pcap and those sent to it to $port-tx.pcap.
408 add_of_ports__ () {
409     local args
410     local pcap=false
411     local ptype=$1
412     shift
413     if test "$1" = --pcap; then
414         pcap=:
415     shift
416     fi
417     local br=$1; shift
418     for pnum; do
419         AS_VAR_APPEND([args], [" -- add-port $br p$pnum -- set Interface p$pnum type=$ptype ofport_request=$pnum"])
420     if $pcap; then
421         AS_VAR_APPEND([args], [" -- set Interface p$pnum options:rxq_pcap=p$pnum-rx.pcap options:tx_pcap=p$pnum-tx.pcap"])
422     fi
423     done
424     echo ovs-vsctl $args
425     ovs-vsctl $args
426 }
427
428 # add_of_ports [--pcap] BRIDGE PNUM...
429 #
430 add_of_ports () {
431     add_of_ports__ dummy $@
432 }
433
434 # add_pmd_of_ports [--pcap] BRIDGE PNUM...
435 #
436 add_pmd_of_ports () {
437     add_of_ports__ dummy-pmd $@
438 }
439
440 m4_divert_pop([PREPARE_TESTS])
441
442 # OVS_VSWITCHD_STOP([WHITELIST])
443 #
444 # Gracefully stops ovs-vswitchd and ovsdb-server, checking their log files
445 # for messages with severity WARN or higher and signaling an error if any
446 # is present.  The optional WHITELIST may contain shell-quoted "sed"
447 # commands to delete any warnings that are actually expected, e.g.:
448 #
449 #   OVS_VSWITCHD_STOP(["/expected error/d"])
450 m4_define([OVS_VSWITCHD_STOP],
451   [AT_CHECK([check_logs $1])
452    OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
453    OVS_APP_EXIT_AND_WAIT([ovsdb-server])])
454
455 m4_define([OVS_VSWITCHD_DISABLE_TUNNEL_PUSH_POP],
456   [AT_CHECK([ovs-appctl ofproto/tnl-push-pop off], [0], [dnl
457 Tunnel push-pop off
458 ])])
459
460 # WAIT_FOR_DUMMY_PORTS(NETDEV_DUMMY_PORT[, NETDEV_DUMMY_PORT...])
461 #
462 # Wait until the netdev dummy ports are connected to each other
463 m4_define([WAIT_FOR_DUMMY_PORTS], \
464   [m4_foreach([dummy_port], [$@],
465       [  \
466          OVS_WAIT_WHILE([ovs-appctl netdev-dummy/conn-state dummy_port \
467                   | grep 'unknown\|disconnected'])])])