]> git.proxmox.com Git - mirror_ovs.git/blame - tests/ofproto-macros.at
OVN: fix DNAT/SNAT system-ovn unit tests
[mirror_ovs.git] / tests / ofproto-macros.at
CommitLineData
254750ce
BP
1m4_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.
5ofctl_strip () {
6 sed '
7s/ (xid=0x[0-9a-fA-F]*)//
8s/ duration=[0-9.]*s,//
9s/ cookie=0x0,//
10s/ table=0,//
11s/ n_packets=0,//
12s/ n_bytes=0,//
b9718b7b
BP
13s/ idle_age=[0-9]*,//
14s/ hard_age=[0-9]*,//
e2167cb7
AZ
15s/dp_hash=0x[0-9a-f]*\//dp_hash=0x0\//
16s/recirc_id=0x[0-9a-f]*,/recirc_id=0x0,/
25070e04
JR
17s/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z|//
18s/dir\/[0-9]*\/br0.mgmt/dir\/XXXX\/br0.mgmt/
254750ce 19'
1e04fcc8
BP
20}
21
db5076ee 22# Filter (multiline) vconn debug messages from ovs-vswitchd.log.
12e2ba2e 23# Use with vconn_sub() and ofctl_strip()
db5076ee
JR
24print_vconn_debug () { awk -F\| < ovs-vswitchd.log '
25BEGIN { prt=0 }
26/\|vconn\|DBG\|/ { sub(/[ \t]*$/, ""); print $3 "|" $4 "|" $5; prt=1; next }
27$4 != "" { prt=0; next }
28prt==1 { sub(/[ \t]*$/, ""); print $0 }
29'
30}
31
12e2ba2e 32vconn_sub() {
7da07bce
GS
33 sed '
34s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
14347f3e 35s/unix#[0-9]*:/unix:/
7da07bce
GS
36'
37}
fb28ef2d 38]
7da07bce 39
fb28ef2d 40# PARSE_LISTENING_PORT LOGFILE VARIABLE
1e04fcc8 41#
fb28ef2d
BP
42# Parses the TCP or SSL port on which a server is listening from
43# LOGFILE, given that the server was told to listen on a kernel-chosen
44# port, and assigns the port number to shell VARIABLE. You should
45# specify the listening remote as ptcp:0:127.0.0.1 or
e731d71b 46# pssl:0:127.0.0.1, or the equivalent with [::1] instead of 127.0.0.1.
1e04fcc8
BP
47#
48# Here's an example of how to use this with ovsdb-server:
49#
1e04fcc8 50# ovsdb-server --log-file --remote=ptcp:0:127.0.0.1 ...
fb28ef2d
BP
51# PARSE_LISTENING_PORT([ovsdb-server.log], [TCP_PORT])
52# # Now $TCP_PORT holds the listening port.
53m4_define([PARSE_LISTENING_PORT],
54 [OVS_WAIT_UNTIL([$2=`sed -n 's/.*0:.*: listening on port \([[0-9]]*\)$/\1/p' "$1"` && test X != X"[$]$2"])])
f295c17b
BP
55
56start_daemon () {
57 "$@" -vconsole:off --detach --no-chdir --pidfile --log-file
46fc80ce
JS
58 pidfile="$OVS_RUNDIR"/$1.pid
59 on_exit "test -e \"$pidfile\" && kill \`cat \"$pidfile\"\`"
f295c17b
BP
60}
61
62# sim_add SANDBOX
63#
64# Starts a new simulated Open vSwitch instance named SANDBOX. Files related to
65# the instance, such as logs, databases, sockets, and pidfiles, are created in
66# a subdirectory of the main test directory also named SANDBOX. Afterward, the
67# "as" command (see below) can be used to run Open vSwitch utilities in the
68# context of the new sandbox.
69#
70# The new sandbox starts out without any bridges. Use ovs-vsctl in the context
71# of the new sandbox to create a bridge, e.g.:
72#
73# sim_add hv0 # Create sandbox hv0.
74# as hv0 # Set hv0 as default sandbox.
75# ovs-vsctl add-br br0 # Add bridge br0 inside hv0.
76#
77# or:
78#
79# sim_add hv0
80# as hv0 ovs-vsctl add-br br0
81sims=
82sim_add () {
83 echo "adding simulator '$1'"
84
85 sims="$sims $1"
86
87 # Create sandbox.
88 local d="$ovs_base"/$1
89 mkdir "$d" || return 1
90 ovs_setenv $1
91
92 # Create database and start ovsdb-server.
93 : > "$d"/.conf.db.~lock~
94 as $1 ovsdb-tool create "$d"/conf.db "$abs_top_srcdir"/vswitchd/vswitch.ovsschema || return 1
95 as $1 start_daemon ovsdb-server --remote=punix:"$d"/db.sock || return 1
96
97 # Initialize database.
98 as $1 ovs-vsctl --no-wait -- init || return 1
99
100 # Start ovs-vswitchd
bb338451 101 as $1 start_daemon ovs-vswitchd --enable-dummy=system -vvconn -vofproto_dpif -vunixctl
f295c17b
BP
102}
103
104# "as $1" sets the OVS_*DIR environment variables to point to $ovs_base/$1.
105#
106# "as $1 COMMAND..." sets those variables in a subshell and invokes COMMAND
107# there.
108as() {
5074bfc4 109 if test "X$2" != X; then
dc5a1213 110 (ovs_setenv $1; shift; "$@")
f295c17b
BP
111 else
112 ovs_setenv $1
113 fi
114}
115
8a32aaa5
BP
116# Strips 'xid=0x1234' from ovs-ofctl output.
117strip_xids () {
118 sed 's/ (xid=0x[[0-9a-fA-F]]*)//'
119}
3a18d0c2
BP
120
121# Changes all 'used:...' to say 'used:0.0', to make output easier to compare.
122strip_used () {
123 sed 's/used:[[0-9]]\.[[0-9]]*/used:0.0/'
124}
125
06db81cc
JS
126# Removes all 'duration=...' to make output easier to compare.
127strip_duration () {
128 sed 's/duration=[[0-9]]*\.[[0-9]]*s,//'
129}
130
3a18d0c2
BP
131# Strips 'ufid:...' from output, to make it easier to compare.
132# (ufids are random.)
133strip_ufid () {
134 sed 's/ufid:[[-0-9a-f]]* //'
135}
254750ce
BP
136m4_divert_pop([PREPARE_TESTS])
137
01b389b1 138m4_define([TESTABLE_LOG], [-vPATTERN:ANY:'%c|%p|%m'])
0fbc9f11 139
02e6f2f7 140# _OVS_VSWITCHD_START([vswitchd-aux-args])
9da5b93a 141#
02e6f2f7
AZ
142# Creates an empty database and starts ovsdb-server.
143# Starts ovs-vswitchd, with additional arguments 'vswitchd-aux-args'.
1c3e353d 144#
02e6f2f7 145m4_define([_OVS_VSWITCHD_START],
6132b241 146 [dnl Create database.
fda1672c 147 touch .conf.db.~lock~
f973f2af 148 AT_CHECK([ovsdb-tool create conf.db $abs_top_srcdir/vswitchd/vswitch.ovsschema])
04c956fc
BP
149
150 dnl Start ovsdb-server.
77a922c7 151 AT_CHECK([ovsdb-server --detach --no-chdir --pidfile --log-file --remote=punix:$OVS_RUNDIR/db.sock], [0], [], [stderr])
53eb8cb8 152 on_exit "kill `cat ovsdb-server.pid`"
08b9b190
EJ
153 AT_CHECK([[sed < stderr '
154/vlog|INFO|opened log file/d
155/ovsdb_server|INFO|ovsdb-server (Open vSwitch)/d']])
04c956fc
BP
156 AT_CAPTURE_FILE([ovsdb-server.log])
157
158 dnl Initialize database.
fe5ca4a7 159 AT_CHECK([ovs-vsctl --no-wait init $2])
04c956fc
BP
160
161 dnl Start ovs-vswitchd.
bb338451 162 AT_CHECK([ovs-vswitchd $1 --detach --no-chdir --pidfile --log-file -vvconn -vofproto_dpif -vunixctl], [0], [], [stderr])
04c956fc 163 AT_CAPTURE_FILE([ovs-vswitchd.log])
7689bcf0 164 on_exit "kill_ovs_vswitchd `cat ovs-vswitchd.pid`"
04c956fc 165 AT_CHECK([[sed < stderr '
7c5a3bbf 166/ovs_numa|INFO|Discovered /d
04c956fc 167/vlog|INFO|opened log file/d
08b9b190 168/vswitchd|INFO|ovs-vswitchd (Open vSwitch)/d
04c956fc 169/reconnect|INFO|/d
e0e2410d 170/dpif_netlink|INFO|Generic Netlink family .ovs_datapath. does not exist/d
04c956fc 171/ofproto|INFO|using datapath ID/d
c9697f35 172/netdev_linux|INFO|.*device has unknown hardware address family/d
bab69409 173/ofproto|INFO|datapath ID changed to fedcba9876543210/d
fe5ca4a7 174/dpdk|INFO|DPDK Disabled - Use other_config:dpdk-init to enable/d
cf114a7f 175/netlink_socket|INFO|netlink: could not enable listening to all nsid/d
d00eeded 176/probe tc:/d
fe5ca4a7 177/tc: Using policy/d']])
02e6f2f7
AZ
178])
179
f7cfe5c2
DDP
180# OVS_VSWITCHD_START([vsctl-args], [vsctl-output], [=override],
181# [vswitchd-aux-args])
02e6f2f7
AZ
182#
183# Creates a database and starts ovsdb-server, starts ovs-vswitchd
184# connected to that database, calls ovs-vsctl to create a bridge named
185# br0 with predictable settings, passing 'vsctl-args' as additional
186# commands to ovs-vsctl. If 'vsctl-args' causes ovs-vsctl to provide
187# output (e.g. because it includes "create" commands) then 'vsctl-output'
c724bd67 188# specifies the expected output after filtering through uuidfilt.
02e6f2f7
AZ
189#
190# If a test needs to use "system" devices (as dummies), then specify
191# =override (literally) as the third argument. Otherwise, system devices
192# won't work at all (which makes sense because tests should not access a
193# system's real Ethernet devices).
f7cfe5c2
DDP
194#
195# 'vswitchd-aux-args' provides a way to pass extra command line arguments
196# to ovs-vswitchd
02e6f2f7 197m4_define([OVS_VSWITCHD_START],
03ccfe48 198 [_OVS_VSWITCHD_START([--enable-dummy$3 --disable-system --disable-system-route $4])
c724bd67 199 AT_CHECK([add_of_br 0 $1 m4_if([$2], [], [], [| uuidfilt])], [0], [$2])
04c956fc
BP
200])
201
cadf7ec9
AW
202# check_logs scans through all *.log files (except '*.log' and testsuite.log)
203# and reports all WARN, ERR, EMER log entries. User can add custom sed filters
204# in $1.
6e1fb7e1
BP
205m4_divert_push([PREPARE_TESTS])
206check_logs () {
cadf7ec9
AW
207 local logs
208 for log in *.log; do
209 case ${log} in # (
210 '*.log'|testsuite.log) ;; # (
211 *) logs="${logs} ${log}" ;;
212 esac
213 done
214
f0e65e67
BP
215 # We most notably ignore 'Broken pipe' warnings. These often and
216 # intermittently appear in ovsdb-server.log, because *ctl commands
217 # (e.g. ovs-vsctl, ovn-nbctl) exit right after committing a change to the
218 # database. However, in reaction, some daemon may immediately update the
219 # database, and this later update may cause database sending update back to
220 # *ctl command if *ctl has not exited yet. If *ctl command exits before
cb334fff
IM
221 # the database calls send, the send fails with 'Broken pipe' or
222 # 'not connected' depending on system. Also removes all 'connection reset'
223 # warning logs for similar reasons (EPIPE, ENOTCONN or ECONNRESET can be
224 # returned on a send depending on whether the peer had unconsumed data
225 # when it closed the socket).
bd3f1861
BP
226 #
227 # We also ignore "Dropped # log messages..." messages. Otherwise, even if
228 # we ignore the messages that were rate-limited, we can end up failing just
229 # because of the announcement that rate-limiting happened (and in a racy,
230 # timing-dependent way, too).
6e1fb7e1 231 sed -n "$1
f0e65e67
BP
232/reset by peer/d
233/Broken pipe/d
cb334fff 234/is not connected/d
98b07853
BP
235/timeval.*Unreasonably long [[0-9]]*ms poll interval/d
236/timeval.*faults: [[0-9]]* minor, [[0-9]]* major/d
237/timeval.*disk: [[0-9]]* reads, [[0-9]]* writes/d
238/timeval.*context switches: [[0-9]]* voluntary, [[0-9]]* involuntary/d
e45d584e 239/ovs_rcu.*blocked [[0-9]]* ms waiting for .* to quiesce/d
bd3f1861 240/Dropped [[0-9]]* log messages/d
6e1fb7e1
BP
241/|WARN|/p
242/|ERR|/p
cadf7ec9 243/|EMER|/p" ${logs}
6e1fb7e1 244}
ca5792f0 245
77ab5fd2
BP
246# add_of_br BRNUM [ARG...]
247add_of_br () {
248 local brnum=$1; shift
249 local br=br$brnum
250 local dpid=fedcba987654321$brnum
251 local mac=aa:55:aa:55:00:0$brnum
7ae1f322 252 ovs-vsctl --timeout=20 \
77ab5fd2 253 -- add-br $br \
5a0e4aec
BP
254 -- set bridge $br datapath-type=dummy \
255 fail-mode=secure \
256 other-config:datapath-id=$dpid \
257 other-config:hwaddr=$mac \
258 protocols="[[OpenFlow10,OpenFlow11,OpenFlow12,\
29718ad4 259 OpenFlow13,OpenFlow14,OpenFlow15]]" \
77ab5fd2
BP
260 -- "$@"
261}
262
e539ba87 263# add_of_ports__ PORT_TYPE [--pcap] BRIDGE PNUM...
ca5792f0 264#
e539ba87 265# Creates PORT_TYPE interfaces in BRIDGE named pPNUM, OpenFlow port number
ca5792f0
BP
266# PNUM, and datapath port number PNUM (the latter is a consequence of
267# the dummy implementation, which tries to assign datapath port
268# numbers based on port names).
77ab5fd2
BP
269#
270# If --pcap is supplied then packets received from the interface will
271# be written to $port-rx.pcap and those sent to it to $port-tx.pcap.
e539ba87 272add_of_ports__ () {
ca5792f0 273 local args
77ab5fd2 274 local pcap=false
e539ba87
IM
275 local ptype=$1
276 shift
77ab5fd2
BP
277 if test "$1" = --pcap; then
278 pcap=:
e539ba87 279 shift
77ab5fd2 280 fi
ca5792f0 281 local br=$1; shift
31a6d029 282 for pnum; do
e539ba87
IM
283 AS_VAR_APPEND([args], [" -- add-port $br p$pnum -- set Interface p$pnum type=$ptype ofport_request=$pnum"])
284 if $pcap; then
285 AS_VAR_APPEND([args], [" -- set Interface p$pnum options:rxq_pcap=p$pnum-rx.pcap options:tx_pcap=p$pnum-tx.pcap"])
286 fi
ca5792f0 287 done
77ab5fd2 288 echo ovs-vsctl $args
ca5792f0
BP
289 ovs-vsctl $args
290}
e539ba87
IM
291
292# add_of_ports [--pcap] BRIDGE PNUM...
293#
294add_of_ports () {
295 add_of_ports__ dummy $@
296}
297
298# add_pmd_of_ports [--pcap] BRIDGE PNUM...
299#
300add_pmd_of_ports () {
301 add_of_ports__ dummy-pmd $@
302}
303
6e1fb7e1
BP
304m4_divert_pop([PREPARE_TESTS])
305
306# OVS_VSWITCHD_STOP([WHITELIST])
307#
308# Gracefully stops ovs-vswitchd and ovsdb-server, checking their log files
309# for messages with severity WARN or higher and signaling an error if any
310# is present. The optional WHITELIST may contain shell-quoted "sed"
311# commands to delete any warnings that are actually expected, e.g.:
312#
313# OVS_VSWITCHD_STOP(["/expected error/d"])
04c956fc 314m4_define([OVS_VSWITCHD_STOP],
6e1fb7e1 315 [AT_CHECK([check_logs $1])
d9c8c57c
LR
316 OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
317 OVS_APP_EXIT_AND_WAIT([ovsdb-server])])
f7b8e494 318
a36de779 319m4_define([OVS_VSWITCHD_DISABLE_TUNNEL_PUSH_POP],
80dc5c84
AZ
320 [AT_CHECK([ovs-appctl dpif/set-dp-features br0 tnl_push_pop false], [0])
321])
a36de779 322
76c22ad5
AZ
323# WAIT_FOR_DUMMY_PORTS(NETDEV_DUMMY_PORT[, NETDEV_DUMMY_PORT...])
324#
325# Wait until the netdev dummy ports are connected to each other
326m4_define([WAIT_FOR_DUMMY_PORTS], \
327 [m4_foreach([dummy_port], [$@],
328 [ \
5a0e4aec 329 OVS_WAIT_WHILE([ovs-appctl netdev-dummy/conn-state dummy_port \
76c22ad5 330 | grep 'unknown\|disconnected'])])])
ca3574d5 331
ca3574d5 332