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