# _ADD_BR([name])
#
# Expands into the proper ovs-vsctl commands to create a bridge with the
# appropriate type and properties
m4_define([_ADD_BR], [[add-br $1 -- set Bridge $1 protocols=OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15 fail-mode=secure ]])

# OVS_TRAFFIC_VSWITCHD_START([vsctl-args], [vsctl-output], [=override])
#
# Creates a database and starts ovsdb-server, starts ovs-vswitchd
# connected to that database, calls ovs-vsctl to create a bridge named
# br0 with predictable settings, passing 'vsctl-args' as additional
# commands to ovs-vsctl.  If 'vsctl-args' causes ovs-vsctl to provide
# output (e.g. because it includes "create" commands) then 'vsctl-output'
# specifies the expected output after filtering through uuidfilt.
#
# Best-effort loading of all available vport modules is performed.
#
m4_define([OVS_TRAFFIC_VSWITCHD_START],
  [AT_CHECK([modprobe openvswitch])
   on_exit 'modprobe -r openvswitch'
   m4_foreach([mod], [[vport_geneve], [vport_gre], [vport_lisp], [vport_stt], [vport_vxlan]],
              [modprobe -q mod || echo "Module mod not loaded."
               on_exit 'modprobe -q -r mod'
              ])
   on_exit 'ovs-dpctl del-dp ovs-system'
   on_exit 'ovs-appctl dpctl/flush-conntrack'
   _OVS_VSWITCHD_START([])
   dnl Add bridges, ports, etc.
   AT_CHECK([ovs-vsctl -- _ADD_BR([br0]) -- $1 m4_if([$2], [], [], [| uuidfilt])], [0], [$2])
])

# OVS_TRAFFIC_VSWITCHD_STOP([ALLOWLIST], [extra_cmds])
#
# Gracefully stops ovs-vswitchd and ovsdb-server, checking their log files
# for messages with severity WARN or higher and signaling an error if any
# is present.  The optional ALLOWLIST may contain shell-quoted "sed"
# commands to delete any warnings that are actually expected, e.g.:
#
#   OVS_TRAFFIC_VSWITCHD_STOP(["/expected error/d"])
#
# 'extra_cmds' are shell commands to be executed after OVS_VSWITCHD_STOP() is
# invoked. They can be used to perform additional cleanups such as name space
# removal.
m4_define([OVS_TRAFFIC_VSWITCHD_STOP],
  [OVS_VSWITCHD_STOP([$1])
   AT_CHECK([:; $2])
  ])

# CONFIGURE_VETH_OFFLOADS([VETH])
#
# The kernel datapath has no problem with offloads and veths. Nothing
# to do here.
m4_define([CONFIGURE_VETH_OFFLOADS],
)

# CHECK_CONNTRACK()
#
# Perform requirements checks for running conntrack tests, and flush the
# kernel conntrack tables when the test is finished.
#
m4_define([CHECK_CONNTRACK],
    [m4_foreach([mod], [[nf_conntrack_ipv4], [nf_conntrack_ipv6], [nf_nat_ftp],
                        [nf_nat_tftp]],
                [modprobe mod || echo "Module mod not loaded."
                 on_exit 'modprobe -r mod'
                ])
     sysctl -w net.netfilter.nf_conntrack_helper=0
     on_exit 'ovstest test-netlink-conntrack flush'
    ]
)

# CHECK_CONNTRACK_ALG()
#
# Perform requirements checks for running conntrack ALG tests. The kernel
# supports ALG, so no check is needed.
#
m4_define([CHECK_CONNTRACK_ALG])

# CHECK_CONNTRACK_LOCAL_STACK()
#
# Perform requirements checks for running conntrack tests with local stack.
# The kernel always supports reading the connection state of an skb coming
# from an internal port, without an explicit ct() action, so no check is
# needed.
m4_define([CHECK_CONNTRACK_LOCAL_STACK])

# CHECK_CONNTRACK_FRAG_OVERLAP()
#
# The kernel does not support overlapping fragments checking.
m4_define([CHECK_CONNTRACK_FRAG_OVERLAP],
[
    AT_SKIP_IF([:])
])

# CHECK_CONNTRACK_NAT()
#
# Perform requirements checks for running conntrack NAT tests. The kernel
# always supports NAT, so no check is needed.
#
m4_define([CHECK_CONNTRACK_NAT])

# CHECK_CONNTRACK_ZEROIP_SNAT()
#
# Perform requirements checks for running conntrack all-zero IP SNAT tests.
# The kernel always supports all-zero IP SNAT, so no check is needed.
# However, the Windows datapath using the same netlink interface does not.
#
m4_define([CHECK_CONNTRACK_ZEROIP_SNAT],
[
    AT_SKIP_IF([test "$IS_WIN32" = "yes"])
])

# CHECK_CONNTRACK_TIMEOUT()
#
# Perform requirements checks for running conntrack customized timeout tests.
#
m4_define([CHECK_CONNTRACK_TIMEOUT],
[
    AT_SKIP_IF([! cat /boot/config-$(uname -r) | grep NF_CONNTRACK_TIMEOUT | grep '=y' > /dev/null])
    modprobe nfnetlink_cttimeout
    on_exit 'modprobe -r nfnetlink_cttimeout'
])

# CHECK_CT_DPIF_SET_GET_MAXCONNS()
#
# Perform requirements checks for running ovs-dpctl ct-set-maxconns or
# ovs-dpctl ct-get-maxconns. The kernel datapath does not support this
# feature.
m4_define([CHECK_CT_DPIF_SET_GET_MAXCONNS],
[
    AT_SKIP_IF([:])
])

# CHECK_CT_DPIF_GET_NCONNS()
#
# Perform requirements checks for running ovs-dpctl ct-get-nconns. The
# kernel datapath does not support this feature.
m4_define([CHECK_CT_DPIF_GET_NCONNS],
[
    AT_SKIP_IF([:])
])

# DPCTL_SET_MIN_FRAG_SIZE()
#
# The kernel does not support this command.
m4_define([DPCTL_SET_MIN_FRAG_SIZE],
[

])

# DPCTL_MODIFY_FRAGMENTATION()
#
# The kernel does not support this command.
m4_define([DPCTL_MODIFY_FRAGMENTATION],
[

])

# DPCTL_CHECK_FRAGMENTATION_PASS()
#
# The kernel does not support this command.
m4_define([DPCTL_CHECK_FRAGMENTATION_PASS],
[

])

# DPCTL_CHECK_V6_FRAGMENTATION_PASS()
#
# The kernel does not support this command.
m4_define([DPCTL_CHECK_V6_FRAGMENTATION_PASS],
[

])

# DPCTL_CHECK_FRAGMENTATION_FAIL()
#
# The kernel does not support this command.
m4_define([DPCTL_CHECK_FRAGMENTATION_FAIL],
[

])

# OVS_CHECK_MIN_KERNEL([minversion], [minsublevel])
#
# Skip test if kernel version falls below minversion.minsublevel
m4_define([OVS_CHECK_MIN_KERNEL],
[
    version=$(uname -r | sed -e 's/\./ /g' | awk '{print $ 1}')
    sublevel=$(uname -r | sed -e 's/\./ /g' | awk '{print $ 2}')
    AT_SKIP_IF([test $version -lt $1 || ( test $version -eq $1 && test $sublevel -lt $2 )])
])

# OVS_CHECK_KERNEL_EXCL([minversion], [minsublevel], [maxversion], [maxsublevel])
#
# Skip test if kernel version falls between minversion.minsublevel and maxversion.maxsublevel
m4_define([OVS_CHECK_KERNEL_EXCL],
[
    version=$(uname -r | sed -e 's/\./ /g' | awk '{print $ 1}')
    sublevel=$(uname -r | sed -e 's/\./ /g' | awk '{print $ 2}')
    AT_SKIP_IF([ ! ( test $version -lt $1 || ( test $version -eq $1 && test $sublevel -lt $2 ) || test $version -gt $3 || ( test $version -eq $3 && test $sublevel -gt $4 ) ) ])
])

# CHECK_LATER_IPV6_FRAGMENTS()
#
# Upstream kernels beetween 4.20 and 5.19 are not parsing IPv6 fragments
# correctly.  The issue was also backported in some older distribution
# kernels, so kernels below 4.20 are not reliable.
m4_define([CHECK_LATER_IPV6_FRAGMENTS], [OVS_CHECK_MIN_KERNEL(5, 19)])

# VSCTL_ADD_DATAPATH_TABLE()
#
# Create system datapath table "system" for kernel tests in ovsdb
m4_define([VSCTL_ADD_DATAPATH_TABLE],
[
    AT_CHECK([ovs-vsctl -- --id=@m create Datapath datapath_version=0 -- set Open_vSwitch . datapaths:"system"=@m], [0], [stdout])
    DP_TYPE=$(echo "system")
])

# CHECK_L3L4_CONNTRACK_REASM()
#
# Only allow this test to run on the kernel datapath - it is not useful
# or necessary for the userspace datapath as it is checking for a kernel
# specific regression.
m4_define([CHECK_L3L4_CONNTRACK_REASM])

# OVS_CHECK_BAREUDP()
#
# The feature needs to be enabled in the kernel configuration (CONFIG_BAREUDP)
# to work.
m4_define([OVS_CHECK_BAREUDP],
[
    AT_SKIP_IF([! ip link add dev ovs_bareudp0 type bareudp dstport 6635 ethertype mpls_uc 2>&1 >/dev/null])
    AT_CHECK([ip link del dev ovs_bareudp0])
])
