m4_define([CONFIGURE_VETH_OFFLOADS],
   [AT_CHECK([ethtool -K $1 tx off], [0], [ignore], [ignore])])

AT_BANNER([OVS-DPDK unit tests])

m4_define([SYSTEM_DPDK_ALLOWED_LOGS],[
\@does not exist. The Open vSwitch kernel module is probably not loaded.@d
\@does not support MTU configuration,@d
\@EAL: No \(available\|free\) .*hugepages reported@d
\@Failed to enable flow control@d
\@Rx checksum offload is not supported on@d
\@TELEMETRY: No legacy callbacks, legacy socket not created@d
])

dnl --------------------------------------------------------------------------
dnl Check if EAL init is successful
AT_SETUP([OVS-DPDK - EAL init])
AT_KEYWORDS([dpdk])
OVS_DPDK_PRE_CHECK()
OVS_DPDK_START()
AT_CHECK([grep "DPDK Enabled - initializing..." ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "EAL" ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "DPDK Enabled - initialized" ovs-vswitchd.log], [], [stdout])
OVS_VSWITCHD_STOP("[SYSTEM_DPDK_ALLOWED_LOGS]")
AT_CLEANUP
dnl --------------------------------------------------------------------------



dnl --------------------------------------------------------------------------
dnl Add standard DPDK PHY port
AT_SETUP([OVS-DPDK - add standard DPDK port])
AT_KEYWORDS([dpdk])

OVS_DPDK_PRE_PHY_SKIP()
OVS_DPDK_START()

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br10 -- set bridge br10 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br10 phy0 -- set Interface phy0 type=dpdk options:dpdk-devargs=$(cat PCI_ADDR)], [], [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])
sleep 2

dnl Clean up
AT_CHECK([ovs-vsctl del-port br10 phy0], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("[SYSTEM_DPDK_ALLOWED_LOGS]")
AT_CLEANUP
dnl --------------------------------------------------------------------------



dnl --------------------------------------------------------------------------
dnl Add vhost-user-client port
AT_SETUP([OVS-DPDK - add vhost-user-client port])
AT_KEYWORDS([dpdk])
OVS_DPDK_PRE_CHECK()
OVS_DPDK_START()

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br10 -- set bridge br10 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br10 dpdkvhostuserclient0 -- set Interface dpdkvhostuserclient0 type=dpdkvhostuserclient options:vhost-server-path=$OVS_RUNDIR/dpdkvhostclient0], [], [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])
sleep 2

dnl Parse log file
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) vhost-user client: socket created" ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "vHost User device 'dpdkvhostuserclient0' created in 'client' mode, using client socket" ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) reconnecting..." ovs-vswitchd.log], [], [stdout])

dnl Clean up
AT_CHECK([ovs-vsctl del-port br10 dpdkvhostuserclient0], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("m4_join([], [SYSTEM_DPDK_ALLOWED_LOGS], [
\@VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) failed to connect: No such file or directory@d
])")
AT_CLEANUP
dnl --------------------------------------------------------------------------



dnl --------------------------------------------------------------------------
dnl Ping vhost-user port
AT_SETUP([OVS-DPDK - ping vhost-user ports])
AT_KEYWORDS([dpdk])
OVS_DPDK_PRE_CHECK()
AT_SKIP_IF([! which dpdk-testpmd >/dev/null 2>/dev/null])
OVS_DPDK_START()

dnl Find number of sockets
AT_CHECK([lscpu], [], [stdout])
AT_CHECK([cat stdout | grep "NUMA node(s)" | awk '{c=1; while (c++<$(3)) {printf "512,"}; print "512"}' > NUMA_NODE])

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br10 -- set bridge br10 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br10 dpdkvhostuser0 -- set Interface dpdkvhostuser0 \
          type=dpdkvhostuser], [],
         [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])

dnl Parse log file
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostuser0) vhost-user server: socket created" \
          ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "Socket $OVS_RUNDIR/dpdkvhostuser0 created for vhost-user port dpdkvhostuser0" \
          ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostuser0) binding succeeded" ovs-vswitchd.log], [],
         [stdout])

dnl Set up namespaces
ADD_NAMESPACES(ns1, ns2)

dnl Add veth device
ADD_VETH(tap1, ns2, br10, "172.31.110.12/24")

dnl Execute testpmd in background
on_exit "pkill -f -x -9 'tail -f /dev/null'"
tail -f /dev/null | dpdk-testpmd --socket-mem="$(cat NUMA_NODE)" --no-pci\
           --vdev="net_virtio_user,path=$OVS_RUNDIR/dpdkvhostuser0" \
           --vdev="net_tap0,iface=tap0" --file-prefix page0 \
           --single-file-segments -- -a >$OVS_RUNDIR/testpmd-dpdkvhostuser0.log 2>&1 &

OVS_WAIT_UNTIL([grep "virtio is now ready for processing" ovs-vswitchd.log])
OVS_WAIT_UNTIL([ip link show dev tap0 | grep -qw LOWER_UP])

dnl Move the tap devices to the namespaces
AT_CHECK([ps aux | grep testpmd], [], [stdout], [stderr])
AT_CHECK([ip link show], [], [stdout], [stderr])
AT_CHECK([ip link set tap0 netns ns1], [], [stdout], [stderr])

AT_CHECK([ip netns exec ns1 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip link show | grep tap0], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip link set tap0 up], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip addr add 172.31.110.11/24 dev tap0], [],
         [stdout], [stderr])

AT_CHECK([ip netns exec ns1 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns2 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ping -c 4 -I tap0 172.31.110.12], [], [stdout],
         [stderr])

dnl Clean up the testpmd now
pkill -f -x -9 'tail -f /dev/null'

dnl Clean up
AT_CHECK([ovs-vsctl del-port br10 dpdkvhostuser0], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("m4_join([], [SYSTEM_DPDK_ALLOWED_LOGS], [
\@VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostuser0) recvmsg failed@d
\@VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostuser0) failed to connect: No such file or directory@d
\@dpdkvhostuser ports are considered deprecated;  please migrate to dpdkvhostuserclient ports.@d
\@failed to enumerate system datapaths: No such file or directory@d
])")
AT_CLEANUP
dnl --------------------------------------------------------------------------

dnl --------------------------------------------------------------------------
dnl Ping vhost-user-client port
AT_SETUP([OVS-DPDK - ping vhost-user-client ports])
AT_KEYWORDS([dpdk])
OVS_DPDK_PRE_CHECK()
AT_SKIP_IF([! which dpdk-testpmd >/dev/null 2>/dev/null])
OVS_DPDK_START()

dnl Find number of sockets
AT_CHECK([lscpu], [], [stdout])
AT_CHECK([cat stdout | grep "NUMA node(s)" | awk '{c=1; while (c++<$(3)) {printf "512,"}; print "512"}' > NUMA_NODE])

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br10 -- set bridge br10 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br10 dpdkvhostuserclient0 -- set Interface \
          dpdkvhostuserclient0 \
          type=dpdkvhostuserclient \
          options:vhost-server-path=$OVS_RUNDIR/dpdkvhostclient0], [],
         [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])

dnl Parse log file
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) vhost-user client: socket created" ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "vHost User device 'dpdkvhostuserclient0' created in 'client' mode, using client socket" ovs-vswitchd.log], [], [stdout])
AT_CHECK([grep "VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) reconnecting..." ovs-vswitchd.log], [], [stdout])

dnl Set up namespaces
ADD_NAMESPACES(ns1, ns2)

dnl Add veth device
ADD_VETH(tap1, ns2, br10, "172.31.110.12/24")

dnl Execute testpmd in background
on_exit "pkill -f -x -9 'tail -f /dev/null'"
tail -f /dev/null | dpdk-testpmd --socket-mem="$(cat NUMA_NODE)" --no-pci\
           --vdev="net_virtio_user,path=$OVS_RUNDIR/dpdkvhostclient0,server=1" \
           --vdev="net_tap0,iface=tap0" --file-prefix page0 \
           --single-file-segments -- -a >$OVS_RUNDIR/testpmd-dpdkvhostuserclient0.log 2>&1 &

OVS_WAIT_UNTIL([grep "virtio is now ready for processing" ovs-vswitchd.log])
OVS_WAIT_UNTIL([ip link show dev tap0 | grep -qw LOWER_UP])

dnl Move the tap devices to the namespaces
AT_CHECK([ps aux | grep testpmd], [], [stdout], [stderr])
AT_CHECK([ip link show], [], [stdout], [stderr])
AT_CHECK([ip link set tap0 netns ns1], [], [stdout], [stderr])

AT_CHECK([ip netns exec ns1 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip link show | grep tap0], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip link set tap0 up], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ip addr add 172.31.110.11/24 dev tap0], [],
         [stdout], [stderr])

AT_CHECK([ip netns exec ns1 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns2 ip link show], [], [stdout], [stderr])
AT_CHECK([ip netns exec ns1 ping -c 4 -I tap0 172.31.110.12], [], [stdout],
         [stderr])

dnl Clean up the testpmd now
pkill -f -x -9 'tail -f /dev/null'

dnl Clean up
AT_CHECK([ovs-vsctl del-port br10 dpdkvhostuserclient0], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("m4_join([], [SYSTEM_DPDK_ALLOWED_LOGS], [
\@VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) recvmsg failed@d
\@VHOST_CONFIG: ($OVS_RUNDIR/dpdkvhostclient0) failed to connect: No such file or directory@d
\@dpdkvhostuser ports are considered deprecated;  please migrate to dpdkvhostuserclient ports.@d
\@failed to enumerate system datapaths: No such file or directory@d
])")
AT_CLEANUP
dnl --------------------------------------------------------------------------

dnl --------------------------------------------------------------------------
dnl Add standard DPDK PHY port
AT_SETUP([OVS-DPDK - MFEX Autovalidator])
AT_KEYWORDS([dpdk])

OVS_DPDK_START()

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br0 -- set bridge br0 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br0 p1 -- set Interface p1 type=dpdk options:dpdk-devargs=net_pcap1,rx_pcap=$srcdir/pcap/mfex_test.pcap,infinite_rx=1], [], [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])

AT_SKIP_IF([! ovs-appctl dpif-netdev/miniflow-parser-get | sed 1,4d | grep "True"], [], [dnl
])

AT_CHECK([ovs-appctl dpif-netdev/dpif-impl-set dpif_avx512], [0], [dnl
DPIF implementation set to dpif_avx512.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set autovalidator], [0], [dnl
Miniflow extract implementation set to autovalidator.
])

OVS_WAIT_UNTIL([test `ovs-vsctl get interface p1 statistics | grep -oP 'rx_packets=\s*\K\d+'` -ge 1000])

dnl Clean up
AT_CHECK([ovs-vsctl del-port br0 p1], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("[SYSTEM_DPDK_ALLOWED_LOGS]")
AT_CLEANUP
dnl --------------------------------------------------------------------------

dnl --------------------------------------------------------------------------
dnl Add standard DPDK PHY port
AT_SETUP([OVS-DPDK - MFEX Autovalidator Fuzzy])
AT_KEYWORDS([dpdk])
AT_SKIP_IF([! $PYTHON3 -c "import scapy"], [], [])
AT_CHECK([$PYTHON3 $srcdir/mfex_fuzzy.py $srcdir], [], [stdout])
OVS_DPDK_START()

dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br0 -- set bridge br0 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br0 p1 -- set Interface p1 type=dpdk options:dpdk-devargs=net_pcap1,rx_pcap=$srcdir/pcap/fuzzy.pcap,infinite_rx=1], [], [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])

AT_SKIP_IF([! ovs-appctl dpif-netdev/miniflow-parser-get | sed 1,4d | grep "True"], [], [dnl
])

AT_CHECK([ovs-appctl dpif-netdev/dpif-impl-set dpif_avx512], [0], [dnl
DPIF implementation set to dpif_avx512.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set autovalidator], [0], [dnl
Miniflow extract implementation set to autovalidator.
])

OVS_WAIT_UNTIL([test `ovs-vsctl get interface p1 statistics | grep -oP 'rx_packets=\s*\K\d+'` -ge 100000])

dnl Clean up
AT_CHECK([ovs-vsctl del-port br0 p1], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("m4_join([], [SYSTEM_DPDK_ALLOWED_LOGS], [
\@upcall: datapath reached the dynamic limit of .* flows.@d
])")
AT_CLEANUP
dnl --------------------------------------------------------------------------

dnl --------------------------------------------------------------------------
AT_SETUP([OVS-DPDK - MFEX Configuration])
AT_KEYWORDS([dpdk])
OVS_DPDK_START()
AT_CHECK([ovs-vsctl --no-wait set Open_vSwitch . other_config:pmd-cpu-mask=0xC])
dnl Add userspace bridge and attach it to OVS
AT_CHECK([ovs-vsctl add-br br0 -- set bridge br0 datapath_type=netdev])
AT_CHECK([ovs-vsctl add-port br0 p1 -- set Interface p1 type=dpdk options:dpdk-devargs=net_pcap1,rx_pcap=$srcdir/pcap/mfex_test.pcap,infinite_rx=1], [], [stdout], [stderr])
AT_CHECK([ovs-vsctl show], [], [stdout])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set scalar 1], [2],
[], [dnl
Error: unknown argument 1.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 6 study 300 xyz], [2],
[], [dnl
Error: invalid study_pkt_cnt value: xyz.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set scalar abcd], [2],
[], [dnl
Error: unknown argument abcd.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 0 scalar abcd], [2],
[], [dnl
Error: unknown argument abcd.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd], [2],
[], [dnl
Error: -pmd option requires a thread id argument.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set tudy abcd], [2],
[], [dnl
Error: unknown argument abcd.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 7 study abcd], [2],
[], [dnl
Error: invalid study_pkt_cnt value: abcd.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 3 study], [0], [dnl
Miniflow extract implementation set to study, on pmd thread 3, studying 128 packets.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 3 study 512], [0], [dnl
Miniflow extract implementation set to study, on pmd thread 3, studying 512 packets.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set study 512], [0], [dnl
Miniflow extract implementation set to study, studying 512 packets.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set study], [0], [dnl
Miniflow extract implementation set to study, studying 128 packets.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 3 autovalidator], [0], [dnl
Miniflow extract implementation set to autovalidator, on pmd thread 3.
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd zero study], [2],
[], [dnl
Error: miniflow extract parser not changed, PMD thread passed is not valid: 'zero'. Pass a valid pmd thread ID.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 1], [2],
[], [dnl
Error: no miniflow extract name provided. Output of miniflow-parser-get shows implementation list.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 1 superstudy], [2],
[], [dnl
Error: unknown miniflow extract implementation superstudy.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set superstudy], [2],
[], [dnl
Error: unknown miniflow extract implementation superstudy.
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl dpif-netdev/miniflow-parser-set -pmd 1 study -pmd], [2],
[], [dnl
Error: invalid study_pkt_cnt value: -pmd.
ovs-appctl: ovs-vswitchd: server returned an error
])

dnl Clean up
AT_CHECK([ovs-vsctl del-port br0 p1], [], [stdout], [stderr])
OVS_VSWITCHD_STOP("m4_join([], [SYSTEM_DPDK_ALLOWED_LOGS], [
\@Error: unknown argument 1.@d
\@Error: invalid study_pkt_cnt value: xyz.@d
\@Error: unknown argument abcd.@d
\@Error: -pmd option requires a thread id argument.@d
\@Error: invalid study_pkt_cnt value: abcd.@d
\@Error: miniflow extract parser not changed, PMD thread passed is not valid: 'zero'. Pass a valid pmd thread ID.@d
\@Error: no miniflow extract name provided. Output of miniflow-parser-get shows implementation list.@d
\@Error: unknown miniflow extract implementation superstudy.@d
\@Error: invalid study_pkt_cnt value: -pmd.@d
])")
AT_CLEANUP dnl
dnl --------------------------------------------------------------------------
