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