#!/usr/bin/env python3
""" BFD tests """

from __future__ import division

import binascii
from collections import namedtuple
import hashlib
import ipaddress
import reprlib
import time
import unittest
from random import randint, shuffle, getrandbits
from socket import AF_INET, AF_INET6, inet_ntop
from struct import pack, unpack

import scapy.compat
from scapy.layers.inet import UDP, IP
from scapy.layers.inet6 import IPv6
from scapy.layers.l2 import Ether, GRE
from scapy.packet import Raw

from config import config
from bfd import (
    VppBFDAuthKey,
    BFD,
    BFDAuthType,
    VppBFDUDPSession,
    BFDDiagCode,
    BFDState,
    BFD_vpp_echo,
)
from framework import tag_fixme_vpp_workers
from framework import VppTestCase, VppTestRunner
from framework import tag_run_solo
from util import ppp
from vpp_ip import DpoProto
from vpp_ip_route import VppIpRoute, VppRoutePath
from vpp_lo_interface import VppLoInterface
from vpp_papi_provider import UnexpectedApiReturnValueError, CliFailedCommandError
from vpp_pg_interface import CaptureTimeoutError, is_ipv6_misc
from vpp_gre_interface import VppGreInterface
from vpp_papi import VppEnum

USEC_IN_SEC = 1000000


class AuthKeyFactory(object):
    """Factory class for creating auth keys with unique conf key ID"""

    def __init__(self):
        self._conf_key_ids = {}

    def create_random_key(self, test, auth_type=BFDAuthType.keyed_sha1):
        """create a random key with unique conf key id"""
        conf_key_id = randint(0, 0xFFFFFFFF)
        while conf_key_id in self._conf_key_ids:
            conf_key_id = randint(0, 0xFFFFFFFF)
        self._conf_key_ids[conf_key_id] = 1
        key = scapy.compat.raw(
            bytearray([randint(0, 255) for _ in range(randint(1, 20))])
        )
        return VppBFDAuthKey(
            test=test, auth_type=auth_type, conf_key_id=conf_key_id, key=key
        )


class BFDAPITestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD) - API"""

    pg0 = None
    pg1 = None

    @classmethod
    def setUpClass(cls):
        super(BFDAPITestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces(range(2))
            for i in cls.pg_interfaces:
                i.config_ip4()
                i.config_ip6()
                i.resolve_arp()

        except Exception:
            super(BFDAPITestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFDAPITestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDAPITestCase, self).setUp()
        self.factory = AuthKeyFactory()

    def test_add_bfd(self):
        """create a BFD session"""
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()

    def test_double_add(self):
        """create the same BFD session twice (negative case)"""
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        session.add_vpp_config()

        with self.vapi.assert_negative_api_retval():
            session.add_vpp_config()

        session.remove_vpp_config()

    def test_add_bfd6(self):
        """create IPv6 BFD session"""
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip6, af=AF_INET6)
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()

    def test_mod_bfd(self):
        """modify BFD session parameters"""
        session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip4,
            desired_min_tx=50000,
            required_min_rx=10000,
            detect_mult=1,
        )
        session.add_vpp_config()
        s = session.get_bfd_udp_session_dump_entry()
        self.assert_equal(
            session.desired_min_tx, s.desired_min_tx, "desired min transmit interval"
        )
        self.assert_equal(
            session.required_min_rx, s.required_min_rx, "required min receive interval"
        )
        self.assert_equal(session.detect_mult, s.detect_mult, "detect mult")
        session.modify_parameters(
            desired_min_tx=session.desired_min_tx * 2,
            required_min_rx=session.required_min_rx * 2,
            detect_mult=session.detect_mult * 2,
        )
        s = session.get_bfd_udp_session_dump_entry()
        self.assert_equal(
            session.desired_min_tx, s.desired_min_tx, "desired min transmit interval"
        )
        self.assert_equal(
            session.required_min_rx, s.required_min_rx, "required min receive interval"
        )
        self.assert_equal(session.detect_mult, s.detect_mult, "detect mult")

    def test_upd_bfd(self):
        """Create/Modify w/ Update BFD session parameters"""
        session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip4,
            desired_min_tx=50000,
            required_min_rx=10000,
            detect_mult=1,
        )
        session.upd_vpp_config()
        s = session.get_bfd_udp_session_dump_entry()
        self.assert_equal(
            session.desired_min_tx, s.desired_min_tx, "desired min transmit interval"
        )
        self.assert_equal(
            session.required_min_rx, s.required_min_rx, "required min receive interval"
        )

        self.assert_equal(session.detect_mult, s.detect_mult, "detect mult")
        session.upd_vpp_config(
            desired_min_tx=session.desired_min_tx * 2,
            required_min_rx=session.required_min_rx * 2,
            detect_mult=session.detect_mult * 2,
        )
        s = session.get_bfd_udp_session_dump_entry()
        self.assert_equal(
            session.desired_min_tx, s.desired_min_tx, "desired min transmit interval"
        )
        self.assert_equal(
            session.required_min_rx, s.required_min_rx, "required min receive interval"
        )
        self.assert_equal(session.detect_mult, s.detect_mult, "detect mult")

    def test_add_sha1_keys(self):
        """add SHA1 keys"""
        key_count = 10
        keys = [self.factory.create_random_key(self) for i in range(0, key_count)]
        for key in keys:
            self.assertFalse(key.query_vpp_config())
        for key in keys:
            key.add_vpp_config()
        for key in keys:
            self.assertTrue(key.query_vpp_config())
        # remove randomly
        indexes = list(range(key_count))
        shuffle(indexes)
        removed = []
        for i in indexes:
            key = keys[i]
            key.remove_vpp_config()
            removed.append(i)
            for j in range(key_count):
                key = keys[j]
                if j in removed:
                    self.assertFalse(key.query_vpp_config())
                else:
                    self.assertTrue(key.query_vpp_config())
        # should be removed now
        for key in keys:
            self.assertFalse(key.query_vpp_config())
        # add back and remove again
        for key in keys:
            key.add_vpp_config()
        for key in keys:
            self.assertTrue(key.query_vpp_config())
        for key in keys:
            key.remove_vpp_config()
        for key in keys:
            self.assertFalse(key.query_vpp_config())

    def test_add_bfd_sha1(self):
        """create a BFD session (SHA1)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4, sha1_key=key)
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()
        session.add_vpp_config()
        self.logger.debug("Session state is %s", session.state)
        session.remove_vpp_config()

    def test_double_add_sha1(self):
        """create the same BFD session twice (negative case) (SHA1)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4, sha1_key=key)
        session.add_vpp_config()
        with self.assertRaises(Exception):
            session.add_vpp_config()

    def test_add_auth_nonexistent_key(self):
        """create BFD session using non-existent SHA1 (negative case)"""
        session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip4,
            sha1_key=self.factory.create_random_key(self),
        )
        with self.assertRaises(Exception):
            session.add_vpp_config()

    def test_shared_sha1_key(self):
        """single SHA1 key shared by multiple BFD sessions"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        sessions = [
            VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4, sha1_key=key),
            VppBFDUDPSession(
                self, self.pg0, self.pg0.remote_ip6, sha1_key=key, af=AF_INET6
            ),
            VppBFDUDPSession(self, self.pg1, self.pg1.remote_ip4, sha1_key=key),
            VppBFDUDPSession(
                self, self.pg1, self.pg1.remote_ip6, sha1_key=key, af=AF_INET6
            ),
        ]
        for s in sessions:
            s.add_vpp_config()
        removed = 0
        for s in sessions:
            e = key.get_bfd_auth_keys_dump_entry()
            self.assert_equal(
                e.use_count, len(sessions) - removed, "Use count for shared key"
            )
            s.remove_vpp_config()
            removed += 1
        e = key.get_bfd_auth_keys_dump_entry()
        self.assert_equal(
            e.use_count, len(sessions) - removed, "Use count for shared key"
        )

    def test_activate_auth(self):
        """activate SHA1 authentication"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        session.add_vpp_config()
        session.activate_auth(key)

    def test_deactivate_auth(self):
        """deactivate SHA1 authentication"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        session.add_vpp_config()
        session.activate_auth(key)
        session.deactivate_auth()

    def test_change_key(self):
        """change SHA1 key"""
        key1 = self.factory.create_random_key(self)
        key2 = self.factory.create_random_key(self)
        while key2.conf_key_id == key1.conf_key_id:
            key2 = self.factory.create_random_key(self)
        key1.add_vpp_config()
        key2.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4, sha1_key=key1)
        session.add_vpp_config()
        session.activate_auth(key2)

    def test_set_del_udp_echo_source(self):
        """set/del udp echo source"""
        self.create_loopback_interfaces(1)
        self.loopback0 = self.lo_interfaces[0]
        self.loopback0.admin_up()
        echo_source = self.vapi.bfd_udp_get_echo_source()
        self.assertFalse(echo_source.is_set)
        self.assertFalse(echo_source.have_usable_ip4)
        self.assertFalse(echo_source.have_usable_ip6)

        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        echo_source = self.vapi.bfd_udp_get_echo_source()
        self.assertTrue(echo_source.is_set)
        self.assertEqual(echo_source.sw_if_index, self.loopback0.sw_if_index)
        self.assertFalse(echo_source.have_usable_ip4)
        self.assertFalse(echo_source.have_usable_ip6)

        self.loopback0.config_ip4()
        echo_ip4 = ipaddress.IPv4Address(
            int(ipaddress.IPv4Address(self.loopback0.local_ip4)) ^ 1
        ).packed
        echo_source = self.vapi.bfd_udp_get_echo_source()
        self.assertTrue(echo_source.is_set)
        self.assertEqual(echo_source.sw_if_index, self.loopback0.sw_if_index)
        self.assertTrue(echo_source.have_usable_ip4)
        self.assertEqual(echo_source.ip4_addr.packed, echo_ip4)
        self.assertFalse(echo_source.have_usable_ip6)

        self.loopback0.config_ip6()
        echo_ip6 = ipaddress.IPv6Address(
            int(ipaddress.IPv6Address(self.loopback0.local_ip6)) ^ 1
        ).packed

        echo_source = self.vapi.bfd_udp_get_echo_source()
        self.assertTrue(echo_source.is_set)
        self.assertEqual(echo_source.sw_if_index, self.loopback0.sw_if_index)
        self.assertTrue(echo_source.have_usable_ip4)
        self.assertEqual(echo_source.ip4_addr.packed, echo_ip4)
        self.assertTrue(echo_source.have_usable_ip6)
        self.assertEqual(echo_source.ip6_addr.packed, echo_ip6)

        self.vapi.bfd_udp_del_echo_source()
        echo_source = self.vapi.bfd_udp_get_echo_source()
        self.assertFalse(echo_source.is_set)
        self.assertFalse(echo_source.have_usable_ip4)
        self.assertFalse(echo_source.have_usable_ip6)


class BFDTestSession(object):
    """BFD session as seen from test framework side"""

    def __init__(
        self,
        test,
        interface,
        af,
        detect_mult=3,
        sha1_key=None,
        bfd_key_id=None,
        our_seq_number=None,
        tunnel_header=None,
        phy_interface=None,
    ):
        self.test = test
        self.af = af
        self.sha1_key = sha1_key
        self.bfd_key_id = bfd_key_id
        self.interface = interface
        if phy_interface:
            self.phy_interface = phy_interface
        else:
            self.phy_interface = self.interface
        self.udp_sport = randint(49152, 65535)
        if our_seq_number is None:
            self.our_seq_number = randint(0, 40000000)
        else:
            self.our_seq_number = our_seq_number
        self.vpp_seq_number = None
        self.my_discriminator = 0
        self.desired_min_tx = 300000
        self.required_min_rx = 300000
        self.required_min_echo_rx = None
        self.detect_mult = detect_mult
        self.diag = BFDDiagCode.no_diagnostic
        self.your_discriminator = None
        self.state = BFDState.down
        self.auth_type = BFDAuthType.no_auth
        self.tunnel_header = tunnel_header
        self.tx_packets = 0
        self.rx_packets = 0
        self.tx_packets_echo = 0
        self.rx_packets_echo = 0

    def inc_seq_num(self):
        """increment sequence number, wrapping if needed"""
        if self.our_seq_number == 0xFFFFFFFF:
            self.our_seq_number = 0
        else:
            self.our_seq_number += 1

    def update(
        self,
        my_discriminator=None,
        your_discriminator=None,
        desired_min_tx=None,
        required_min_rx=None,
        required_min_echo_rx=None,
        detect_mult=None,
        diag=None,
        state=None,
        auth_type=None,
    ):
        """update BFD parameters associated with session"""
        if my_discriminator is not None:
            self.my_discriminator = my_discriminator
        if your_discriminator is not None:
            self.your_discriminator = your_discriminator
        if required_min_rx is not None:
            self.required_min_rx = required_min_rx
        if required_min_echo_rx is not None:
            self.required_min_echo_rx = required_min_echo_rx
        if desired_min_tx is not None:
            self.desired_min_tx = desired_min_tx
        if detect_mult is not None:
            self.detect_mult = detect_mult
        if diag is not None:
            self.diag = diag
        if state is not None:
            self.state = state
        if auth_type is not None:
            self.auth_type = auth_type

    def fill_packet_fields(self, packet):
        """set packet fields with known values in packet"""
        bfd = packet[BFD]
        if self.my_discriminator:
            self.test.logger.debug(
                "BFD: setting packet.my_discriminator=%s", self.my_discriminator
            )
            bfd.my_discriminator = self.my_discriminator
        if self.your_discriminator:
            self.test.logger.debug(
                "BFD: setting packet.your_discriminator=%s", self.your_discriminator
            )
            bfd.your_discriminator = self.your_discriminator
        if self.required_min_rx:
            self.test.logger.debug(
                "BFD: setting packet.required_min_rx_interval=%s", self.required_min_rx
            )
            bfd.required_min_rx_interval = self.required_min_rx
        if self.required_min_echo_rx:
            self.test.logger.debug(
                "BFD: setting packet.required_min_echo_rx=%s", self.required_min_echo_rx
            )
            bfd.required_min_echo_rx_interval = self.required_min_echo_rx
        if self.desired_min_tx:
            self.test.logger.debug(
                "BFD: setting packet.desired_min_tx_interval=%s", self.desired_min_tx
            )
            bfd.desired_min_tx_interval = self.desired_min_tx
        if self.detect_mult:
            self.test.logger.debug(
                "BFD: setting packet.detect_mult=%s", self.detect_mult
            )
            bfd.detect_mult = self.detect_mult
        if self.diag:
            self.test.logger.debug("BFD: setting packet.diag=%s", self.diag)
            bfd.diag = self.diag
        if self.state:
            self.test.logger.debug("BFD: setting packet.state=%s", self.state)
            bfd.state = self.state
        if self.auth_type:
            # this is used by a negative test-case
            self.test.logger.debug("BFD: setting packet.auth_type=%s", self.auth_type)
            bfd.auth_type = self.auth_type

    def create_packet(self):
        """create a BFD packet, reflecting the current state of session"""
        if self.sha1_key:
            bfd = BFD(flags="A")
            bfd.auth_type = self.sha1_key.auth_type
            bfd.auth_len = BFD.sha1_auth_len
            bfd.auth_key_id = self.bfd_key_id
            bfd.auth_seq_num = self.our_seq_number
            bfd.length = BFD.sha1_auth_len + BFD.bfd_pkt_len
        else:
            bfd = BFD()
        packet = Ether(
            src=self.phy_interface.remote_mac, dst=self.phy_interface.local_mac
        )
        if self.tunnel_header:
            packet = packet / self.tunnel_header
        if self.af == AF_INET6:
            packet = (
                packet
                / IPv6(
                    src=self.interface.remote_ip6,
                    dst=self.interface.local_ip6,
                    hlim=255,
                )
                / UDP(sport=self.udp_sport, dport=BFD.udp_dport)
                / bfd
            )
        else:
            packet = (
                packet
                / IP(
                    src=self.interface.remote_ip4, dst=self.interface.local_ip4, ttl=255
                )
                / UDP(sport=self.udp_sport, dport=BFD.udp_dport)
                / bfd
            )
        self.test.logger.debug("BFD: Creating packet")
        self.fill_packet_fields(packet)
        if self.sha1_key:
            hash_material = (
                scapy.compat.raw(packet[BFD])[:32]
                + self.sha1_key.key
                + b"\0" * (20 - len(self.sha1_key.key))
            )
            self.test.logger.debug(
                "BFD: Calculated SHA1 hash: %s"
                % hashlib.sha1(hash_material).hexdigest()
            )
            packet[BFD].auth_key_hash = hashlib.sha1(hash_material).digest()
        return packet

    def send_packet(self, packet=None, interface=None):
        """send packet on interface, creating the packet if needed"""
        if packet is None:
            packet = self.create_packet()
        if interface is None:
            interface = self.phy_interface
        self.test.logger.debug(ppp("Sending packet:", packet))
        interface.add_stream(packet)
        self.tx_packets += 1
        self.test.pg_start()

    def verify_sha1_auth(self, packet):
        """Verify correctness of authentication in BFD layer."""
        bfd = packet[BFD]
        self.test.assert_equal(bfd.auth_len, 28, "Auth section length")
        self.test.assert_equal(bfd.auth_type, self.sha1_key.auth_type, BFDAuthType)
        self.test.assert_equal(bfd.auth_key_id, self.bfd_key_id, "Key ID")
        self.test.assert_equal(bfd.auth_reserved, 0, "Reserved")
        if self.vpp_seq_number is None:
            self.vpp_seq_number = bfd.auth_seq_num
            self.test.logger.debug(
                "Received initial sequence number: %s" % self.vpp_seq_number
            )
        else:
            recvd_seq_num = bfd.auth_seq_num
            self.test.logger.debug(
                "Received followup sequence number: %s" % recvd_seq_num
            )
            if self.vpp_seq_number < 0xFFFFFFFF:
                if self.sha1_key.auth_type == BFDAuthType.meticulous_keyed_sha1:
                    self.test.assert_equal(
                        recvd_seq_num, self.vpp_seq_number + 1, "BFD sequence number"
                    )
                else:
                    self.test.assert_in_range(
                        recvd_seq_num,
                        self.vpp_seq_number,
                        self.vpp_seq_number + 1,
                        "BFD sequence number",
                    )
            else:
                if self.sha1_key.auth_type == BFDAuthType.meticulous_keyed_sha1:
                    self.test.assert_equal(recvd_seq_num, 0, "BFD sequence number")
                else:
                    self.test.assertIn(
                        recvd_seq_num,
                        (self.vpp_seq_number, 0),
                        "BFD sequence number not one of "
                        "(%s, 0)" % self.vpp_seq_number,
                    )
            self.vpp_seq_number = recvd_seq_num
        # last 20 bytes represent the hash - so replace them with the key,
        # pad the result with zeros and hash the result
        hash_material = (
            bfd.original[:-20]
            + self.sha1_key.key
            + b"\0" * (20 - len(self.sha1_key.key))
        )
        expected_hash = hashlib.sha1(hash_material).hexdigest()
        self.test.assert_equal(
            binascii.hexlify(bfd.auth_key_hash), expected_hash.encode(), "Auth key hash"
        )

    def verify_bfd(self, packet):
        """Verify correctness of BFD layer."""
        bfd = packet[BFD]
        self.test.assert_equal(bfd.version, 1, "BFD version")
        self.test.assert_equal(
            bfd.your_discriminator, self.my_discriminator, "BFD - your discriminator"
        )
        if self.sha1_key:
            self.verify_sha1_auth(packet)


def bfd_session_up(test):
    """Bring BFD session up"""
    test.logger.info("BFD: Waiting for slow hello")
    p = wait_for_bfd_packet(test, 2, is_tunnel=test.vpp_session.is_tunnel)
    old_offset = None
    if hasattr(test, "vpp_clock_offset"):
        old_offset = test.vpp_clock_offset
    test.vpp_clock_offset = time.time() - float(p.time)
    test.logger.debug("BFD: Calculated vpp clock offset: %s", test.vpp_clock_offset)
    if old_offset:
        test.assertAlmostEqual(
            old_offset,
            test.vpp_clock_offset,
            delta=0.5,
            msg="vpp clock offset not stable (new: %s, old: %s)"
            % (test.vpp_clock_offset, old_offset),
        )
    test.logger.info("BFD: Sending Init")
    test.test_session.update(
        my_discriminator=randint(0, 40000000),
        your_discriminator=p[BFD].my_discriminator,
        state=BFDState.init,
    )
    if (
        test.test_session.sha1_key
        and test.test_session.sha1_key.auth_type == BFDAuthType.meticulous_keyed_sha1
    ):
        test.test_session.inc_seq_num()
    test.test_session.send_packet()
    test.logger.info("BFD: Waiting for event")
    e = test.vapi.wait_for_event(1, "bfd_udp_session_event")
    verify_event(test, e, expected_state=BFDState.up)
    test.logger.info("BFD: Session is Up")
    test.test_session.update(state=BFDState.up)
    if (
        test.test_session.sha1_key
        and test.test_session.sha1_key.auth_type == BFDAuthType.meticulous_keyed_sha1
    ):
        test.test_session.inc_seq_num()
    test.test_session.send_packet()
    test.assert_equal(test.vpp_session.state, BFDState.up, BFDState)


def bfd_session_down(test):
    """Bring BFD session down"""
    test.assert_equal(test.vpp_session.state, BFDState.up, BFDState)
    test.test_session.update(state=BFDState.down)
    if (
        test.test_session.sha1_key
        and test.test_session.sha1_key.auth_type == BFDAuthType.meticulous_keyed_sha1
    ):
        test.test_session.inc_seq_num()
    test.test_session.send_packet()
    test.logger.info("BFD: Waiting for event")
    e = test.vapi.wait_for_event(1, "bfd_udp_session_event")
    verify_event(test, e, expected_state=BFDState.down)
    test.logger.info("BFD: Session is Down")
    test.assert_equal(test.vpp_session.state, BFDState.down, BFDState)


def verify_bfd_session_config(test, session, state=None):
    dump = session.get_bfd_udp_session_dump_entry()
    test.assertIsNotNone(dump)
    # since dump is not none, we have verified that sw_if_index and addresses
    # are valid (in get_bfd_udp_session_dump_entry)
    if state:
        test.assert_equal(dump.state, state, "session state")
    test.assert_equal(
        dump.required_min_rx, session.required_min_rx, "required min rx interval"
    )
    test.assert_equal(
        dump.desired_min_tx, session.desired_min_tx, "desired min tx interval"
    )
    test.assert_equal(dump.detect_mult, session.detect_mult, "detect multiplier")
    if session.sha1_key is None:
        test.assert_equal(dump.is_authenticated, 0, "is_authenticated flag")
    else:
        test.assert_equal(dump.is_authenticated, 1, "is_authenticated flag")
        test.assert_equal(dump.bfd_key_id, session.bfd_key_id, "bfd key id")
        test.assert_equal(
            dump.conf_key_id, session.sha1_key.conf_key_id, "config key id"
        )


def verify_ip(test, packet):
    """Verify correctness of IP layer."""
    if test.vpp_session.af == AF_INET6:
        ip = packet[IPv6]
        local_ip = test.vpp_session.interface.local_ip6
        remote_ip = test.vpp_session.interface.remote_ip6
        test.assert_equal(ip.hlim, 255, "IPv6 hop limit")
    else:
        ip = packet[IP]
        local_ip = test.vpp_session.interface.local_ip4
        remote_ip = test.vpp_session.interface.remote_ip4
        test.assert_equal(ip.ttl, 255, "IPv4 TTL")
    test.assert_equal(ip.src, local_ip, "IP source address")
    test.assert_equal(ip.dst, remote_ip, "IP destination address")


def verify_udp(test, packet):
    """Verify correctness of UDP layer."""
    udp = packet[UDP]
    test.assert_equal(udp.dport, BFD.udp_dport, "UDP destination port")
    test.assert_in_range(
        udp.sport, BFD.udp_sport_min, BFD.udp_sport_max, "UDP source port"
    )


def verify_event(test, event, expected_state):
    """Verify correctness of event values."""
    e = event
    test.logger.debug("BFD: Event: %s" % reprlib.repr(e))
    test.assert_equal(
        e.sw_if_index, test.vpp_session.interface.sw_if_index, "BFD interface index"
    )

    test.assert_equal(
        str(e.local_addr), test.vpp_session.local_addr, "Local IPv6 address"
    )
    test.assert_equal(str(e.peer_addr), test.vpp_session.peer_addr, "Peer IPv6 address")
    test.assert_equal(e.state, expected_state, BFDState)


def wait_for_bfd_packet(test, timeout=1, pcap_time_min=None, is_tunnel=False):
    """wait for BFD packet and verify its correctness

    :param timeout: how long to wait
    :param pcap_time_min: ignore packets with pcap timestamp lower than this

    :returns: tuple (packet, time spent waiting for packet)
    """
    test.logger.info("BFD: Waiting for BFD packet")
    deadline = time.time() + timeout
    counter = 0
    while True:
        counter += 1
        # sanity check
        test.assert_in_range(counter, 0, 100, "number of packets ignored")
        time_left = deadline - time.time()
        if time_left < 0:
            raise CaptureTimeoutError("Packet did not arrive within timeout")
        p = test.pg0.wait_for_packet(timeout=time_left)
        test.test_session.rx_packets += 1
        test.logger.debug(ppp("BFD: Got packet:", p))
        if pcap_time_min is not None and p.time < pcap_time_min:
            test.logger.debug(
                ppp(
                    "BFD: ignoring packet (pcap time %s < "
                    "pcap time min %s):" % (p.time, pcap_time_min),
                    p,
                )
            )
        else:
            break
    if is_tunnel:
        # strip an IP layer and move to the next
        p = p[IP].payload

    bfd = p[BFD]
    if bfd is None:
        raise Exception(ppp("Unexpected or invalid BFD packet:", p))
    if bfd.payload:
        raise Exception(ppp("Unexpected payload in BFD packet:", bfd))
    verify_ip(test, p)
    verify_udp(test, p)
    test.test_session.verify_bfd(p)
    return p


BFDStats = namedtuple("BFDStats", "rx rx_echo tx tx_echo")


def bfd_grab_stats_snapshot(test, bs_idx=0, thread_index=None):
    s = test.statistics
    ti = thread_index
    if ti is None:
        rx = s["/bfd/rx-session-counters"][:, bs_idx].sum_packets()
        rx_echo = s["/bfd/rx-session-echo-counters"][:, bs_idx].sum_packets()
        tx = s["/bfd/tx-session-counters"][:, bs_idx].sum_packets()
        tx_echo = s["/bfd/tx-session-echo-counters"][:, bs_idx].sum_packets()
    else:
        rx = s["/bfd/rx-session-counters"][ti, bs_idx].sum_packets()
        rx_echo = s["/bfd/rx-session-echo-counters"][ti, bs_idx].sum_packets()
        tx = s["/bfd/tx-session-counters"][ti, bs_idx].sum_packets()
        tx_echo = s["/bfd/tx-session-echo-counters"][ti, bs_idx].sum_packets()
    return BFDStats(rx, rx_echo, tx, tx_echo)


def bfd_stats_diff(stats_before, stats_after):
    rx = stats_after.rx - stats_before.rx
    rx_echo = stats_after.rx_echo - stats_before.rx_echo
    tx = stats_after.tx - stats_before.tx
    tx_echo = stats_after.tx_echo - stats_before.tx_echo
    return BFDStats(rx, rx_echo, tx, tx_echo)


@tag_run_solo
class BFD4TestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD)"""

    pg0 = None
    vpp_clock_offset = None
    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFD4TestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces([0])
            cls.create_loopback_interfaces(1)
            cls.loopback0 = cls.lo_interfaces[0]
            cls.loopback0.config_ip4()
            cls.loopback0.admin_up()
            cls.pg0.config_ip4()
            cls.pg0.configure_ipv4_neighbors()
            cls.pg0.admin_up()
            cls.pg0.resolve_arp()

        except Exception:
            super(BFD4TestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFD4TestCase, cls).tearDownClass()

    def setUp(self):
        super(BFD4TestCase, self).setUp()
        self.factory = AuthKeyFactory()
        self.vapi.want_bfd_events()
        self.pg0.enable_capture()
        try:
            self.bfd_udp4_sessions = self.statistics["/bfd/udp4/sessions"]
            self.bfd_udp6_sessions = self.statistics["/bfd/udp6/sessions"]
            self.vpp_session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
            self.vpp_session.add_vpp_config()
            self.vpp_session.admin_up()
            self.test_session = BFDTestSession(self, self.pg0, AF_INET)
        except BaseException:
            self.vapi.want_bfd_events(enable_disable=0)
            raise

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=0)
        self.vapi.collect_events()  # clear the event queue
        super(BFD4TestCase, self).tearDown()

    def test_session_up(self):
        """bring BFD session up"""
        bfd_session_up(self)
        bfd_udp4_sessions = self.statistics["/bfd/udp4/sessions"]
        bfd_udp6_sessions = self.statistics["/bfd/udp6/sessions"]
        self.assert_equal(bfd_udp4_sessions - self.bfd_udp4_sessions, 1)
        self.assert_equal(bfd_udp6_sessions, self.bfd_udp6_sessions)

    def test_session_up_by_ip(self):
        """bring BFD session up - first frame looked up by address pair"""
        self.logger.info("BFD: Sending Slow control frame")
        self.test_session.update(my_discriminator=randint(0, 40000000))
        self.test_session.send_packet()
        self.pg0.enable_capture()
        p = self.pg0.wait_for_packet(1)
        self.assert_equal(
            p[BFD].your_discriminator,
            self.test_session.my_discriminator,
            "BFD - your discriminator",
        )
        self.assert_equal(p[BFD].state, BFDState.init, BFDState)
        self.test_session.update(
            your_discriminator=p[BFD].my_discriminator, state=BFDState.up
        )
        self.logger.info("BFD: Waiting for event")
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.init)
        self.logger.info("BFD: Sending Up")
        self.test_session.send_packet()
        self.logger.info("BFD: Waiting for event")
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.up)
        self.logger.info("BFD: Session is Up")
        self.test_session.update(state=BFDState.up)
        self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_session_down(self):
        """bring BFD session down"""
        bfd_session_up(self)
        bfd_session_down(self)

    def test_hold_up(self):
        """hold BFD session up"""
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            wait_for_bfd_packet(self)
            self.test_session.send_packet()
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_slow_timer(self):
        """verify slow periodic control frames while session down"""
        packet_count = 3
        self.logger.info("BFD: Waiting for %d BFD packets", packet_count)
        prev_packet = wait_for_bfd_packet(self, 2)
        for dummy in range(packet_count):
            next_packet = wait_for_bfd_packet(self, 2)
            time_diff = next_packet.time - prev_packet.time
            # spec says the range should be <0.75, 1>, allow extra 0.05 margin
            # to work around timing issues
            self.assert_in_range(time_diff, 0.70, 1.05, "time between slow packets")
            prev_packet = next_packet

    def test_zero_remote_min_rx(self):
        """no packets when zero remote required min rx interval"""
        bfd_session_up(self)
        self.test_session.update(required_min_rx=0)
        self.test_session.send_packet()
        for dummy in range(self.test_session.detect_mult):
            self.sleep(
                self.vpp_session.required_min_rx / USEC_IN_SEC,
                "sleep before transmitting bfd packet",
            )
            self.test_session.send_packet()
            try:
                p = wait_for_bfd_packet(self, timeout=0)
                self.logger.error(ppp("Received unexpected packet:", p))
            except CaptureTimeoutError:
                pass
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")
        self.test_session.update(required_min_rx=300000)
        for dummy in range(3):
            self.test_session.send_packet()
            wait_for_bfd_packet(
                self, timeout=self.test_session.required_min_rx / USEC_IN_SEC
            )
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_conn_down(self):
        """verify session goes down after inactivity"""
        bfd_session_up(self)
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        self.sleep(detection_time, "waiting for BFD session time-out")
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.down)

    def test_peer_discr_reset_sess_down(self):
        """peer discriminator reset after session goes down"""
        bfd_session_up(self)
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        self.sleep(detection_time, "waiting for BFD session time-out")
        self.test_session.my_discriminator = 0
        wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)

    def test_large_required_min_rx(self):
        """large remote required min rx interval"""
        bfd_session_up(self)
        p = wait_for_bfd_packet(self)
        interval = 3000000
        self.test_session.update(required_min_rx=interval)
        self.test_session.send_packet()
        time_mark = time.time()
        count = 0
        # busy wait here, trying to collect a packet or event, vpp is not
        # allowed to send packets and the session will timeout first - so the
        # Up->Down event must arrive before any packets do
        while time.time() < time_mark + interval / USEC_IN_SEC:
            try:
                p = wait_for_bfd_packet(self, timeout=0)
                # if vpp managed to send a packet before we did the session
                # session update, then that's fine, ignore it
                if p.time < time_mark - self.vpp_clock_offset:
                    continue
                self.logger.error(ppp("Received unexpected packet:", p))
                count += 1
            except CaptureTimeoutError:
                pass
            events = self.vapi.collect_events()
            if len(events) > 0:
                verify_event(self, events[0], BFDState.down)
                break
        self.assert_equal(count, 0, "number of packets received")

    def test_immediate_remote_min_rx_reduction(self):
        """immediately honor remote required min rx reduction"""
        self.vpp_session.remove_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, desired_min_tx=10000
        )
        self.pg0.enable_capture()
        self.vpp_session.add_vpp_config()
        self.test_session.update(desired_min_tx=1000000, required_min_rx=1000000)
        bfd_session_up(self)
        reference_packet = wait_for_bfd_packet(self)
        time_mark = time.time()
        interval = 300000
        self.test_session.update(required_min_rx=interval)
        self.test_session.send_packet()
        extra_time = time.time() - time_mark
        p = wait_for_bfd_packet(self)
        # first packet is allowed to be late by time we spent doing the update
        # calculated in extra_time
        self.assert_in_range(
            p.time - reference_packet.time,
            0.95 * 0.75 * interval / USEC_IN_SEC,
            1.05 * interval / USEC_IN_SEC + extra_time,
            "time between BFD packets",
        )
        reference_packet = p
        for dummy in range(3):
            p = wait_for_bfd_packet(self)
            diff = p.time - reference_packet.time
            self.assert_in_range(
                diff,
                0.95 * 0.75 * interval / USEC_IN_SEC,
                1.05 * interval / USEC_IN_SEC,
                "time between BFD packets",
            )
            reference_packet = p

    def test_modify_req_min_rx_double(self):
        """modify session - double required min rx"""
        bfd_session_up(self)
        p = wait_for_bfd_packet(self)
        self.test_session.update(desired_min_tx=10000, required_min_rx=10000)
        self.test_session.send_packet()
        # double required min rx
        self.vpp_session.modify_parameters(
            required_min_rx=2 * self.vpp_session.required_min_rx
        )
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        # poll bit needs to be set
        self.assertIn("P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet")
        # finish poll sequence with final packet
        final = self.test_session.create_packet()
        final[BFD].flags = "F"
        timeout = (
            self.test_session.detect_mult
            * max(self.test_session.desired_min_tx, self.vpp_session.required_min_rx)
            / USEC_IN_SEC
        )
        self.test_session.send_packet(final)
        time_mark = time.time()
        e = self.vapi.wait_for_event(2 * timeout, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.down)
        time_to_event = time.time() - time_mark
        self.assert_in_range(
            time_to_event, 0.9 * timeout, 1.1 * timeout, "session timeout"
        )

    def test_modify_req_min_rx_halve(self):
        """modify session - halve required min rx"""
        self.vpp_session.modify_parameters(
            required_min_rx=2 * self.vpp_session.required_min_rx
        )
        bfd_session_up(self)
        p = wait_for_bfd_packet(self)
        self.test_session.update(desired_min_tx=10000, required_min_rx=10000)
        self.test_session.send_packet()
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        # halve required min rx
        old_required_min_rx = self.vpp_session.required_min_rx
        self.vpp_session.modify_parameters(
            required_min_rx=self.vpp_session.required_min_rx // 2
        )
        # now we wait 0.8*3*old-req-min-rx and the session should still be up
        self.sleep(
            0.8 * self.vpp_session.detect_mult * old_required_min_rx / USEC_IN_SEC,
            "wait before finishing poll sequence",
        )
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")
        p = wait_for_bfd_packet(self)
        # poll bit needs to be set
        self.assertIn("P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet")
        # finish poll sequence with final packet
        final = self.test_session.create_packet()
        final[BFD].flags = "F"
        self.test_session.send_packet(final)
        # now the session should time out under new conditions
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        before = time.time()
        e = self.vapi.wait_for_event(2 * detection_time, "bfd_udp_session_event")
        after = time.time()
        self.assert_in_range(
            after - before,
            0.9 * detection_time,
            1.1 * detection_time,
            "time before bfd session goes down",
        )
        verify_event(self, e, expected_state=BFDState.down)

    def test_modify_detect_mult(self):
        """modify detect multiplier"""
        bfd_session_up(self)
        p = wait_for_bfd_packet(self)
        self.vpp_session.modify_parameters(detect_mult=1)
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(
            self.vpp_session.detect_mult, p[BFD].detect_mult, "detect mult"
        )
        # poll bit must not be set
        self.assertNotIn(
            "P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet"
        )
        self.vpp_session.modify_parameters(detect_mult=10)
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(
            self.vpp_session.detect_mult, p[BFD].detect_mult, "detect mult"
        )
        # poll bit must not be set
        self.assertNotIn(
            "P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet"
        )

    def test_queued_poll(self):
        """test poll sequence queueing"""
        bfd_session_up(self)
        p = wait_for_bfd_packet(self)
        self.vpp_session.modify_parameters(
            required_min_rx=2 * self.vpp_session.required_min_rx
        )
        p = wait_for_bfd_packet(self)
        poll_sequence_start = time.time()
        poll_sequence_length_min = 0.5
        send_final_after = time.time() + poll_sequence_length_min
        # poll bit needs to be set
        self.assertIn("P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet")
        self.assert_equal(
            p[BFD].required_min_rx_interval,
            self.vpp_session.required_min_rx,
            "BFD required min rx interval",
        )
        self.vpp_session.modify_parameters(
            required_min_rx=2 * self.vpp_session.required_min_rx
        )
        # 2nd poll sequence should be queued now
        # don't send the reply back yet, wait for some time to emulate
        # longer round-trip time
        packet_count = 0
        while time.time() < send_final_after:
            self.test_session.send_packet()
            p = wait_for_bfd_packet(self)
            self.assert_equal(
                len(self.vapi.collect_events()), 0, "number of bfd events"
            )
            self.assert_equal(
                p[BFD].required_min_rx_interval,
                self.vpp_session.required_min_rx,
                "BFD required min rx interval",
            )
            packet_count += 1
            # poll bit must be set
            self.assertIn(
                "P", p.sprintf("%BFD.flags%"), "Poll bit not set in BFD packet"
            )
        final = self.test_session.create_packet()
        final[BFD].flags = "F"
        self.test_session.send_packet(final)
        # finish 1st with final
        poll_sequence_length = time.time() - poll_sequence_start
        # vpp must wait for some time before starting new poll sequence
        poll_no_2_started = False
        for dummy in range(2 * packet_count):
            p = wait_for_bfd_packet(self)
            self.assert_equal(
                len(self.vapi.collect_events()), 0, "number of bfd events"
            )
            if "P" in p.sprintf("%BFD.flags%"):
                poll_no_2_started = True
                if time.time() < poll_sequence_start + poll_sequence_length:
                    raise Exception("VPP started 2nd poll sequence too soon")
                final = self.test_session.create_packet()
                final[BFD].flags = "F"
                self.test_session.send_packet(final)
                break
            else:
                self.test_session.send_packet()
        self.assertTrue(poll_no_2_started, "2nd poll sequence not performed")
        # finish 2nd with final
        final = self.test_session.create_packet()
        final[BFD].flags = "F"
        self.test_session.send_packet(final)
        p = wait_for_bfd_packet(self)
        # poll bit must not be set
        self.assertNotIn("P", p.sprintf("%BFD.flags%"), "Poll bit set in BFD packet")

    # returning inconsistent results requiring retries in per-patch tests
    @unittest.skipUnless(config.extended, "part of extended tests")
    def test_poll_response(self):
        """test correct response to control frame with poll bit set"""
        bfd_session_up(self)
        poll = self.test_session.create_packet()
        poll[BFD].flags = "P"
        self.test_session.send_packet(poll)
        final = wait_for_bfd_packet(
            self, pcap_time_min=time.time() - self.vpp_clock_offset
        )
        self.assertIn("F", final.sprintf("%BFD.flags%"))

    def test_no_periodic_if_remote_demand(self):
        """no periodic frames outside poll sequence if remote demand set"""
        bfd_session_up(self)
        demand = self.test_session.create_packet()
        demand[BFD].flags = "D"
        self.test_session.send_packet(demand)
        transmit_time = (
            0.9
            * max(self.vpp_session.required_min_rx, self.test_session.desired_min_tx)
            / USEC_IN_SEC
        )
        count = 0
        for dummy in range(self.test_session.detect_mult * 2):
            self.sleep(transmit_time)
            self.test_session.send_packet(demand)
            try:
                p = wait_for_bfd_packet(self, timeout=0)
                self.logger.error(ppp("Received unexpected packet:", p))
                count += 1
            except CaptureTimeoutError:
                pass
        events = self.vapi.collect_events()
        for e in events:
            self.logger.error("Received unexpected event: %s", e)
        self.assert_equal(count, 0, "number of packets received")
        self.assert_equal(len(events), 0, "number of events received")

    def test_echo_looped_back(self):
        """echo packets looped back"""
        bfd_session_up(self)
        stats_before = bfd_grab_stats_snapshot(self)
        self.pg0.enable_capture()
        echo_packet_count = 10
        # random source port low enough to increment a few times..
        udp_sport_tx = randint(1, 50000)
        udp_sport_rx = udp_sport_tx
        echo_packet = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4)
            / UDP(dport=BFD.udp_dport_echo)
            / Raw("this should be looped back")
        )
        for dummy in range(echo_packet_count):
            self.sleep(0.01, "delay between echo packets")
            echo_packet[UDP].sport = udp_sport_tx
            udp_sport_tx += 1
            self.logger.debug(ppp("Sending packet:", echo_packet))
            self.pg0.add_stream(echo_packet)
            self.pg_start()
            self.logger.debug(self.vapi.ppcli("show trace"))
        counter = 0
        bfd_control_packets_rx = 0
        while counter < echo_packet_count:
            p = self.pg0.wait_for_packet(1)
            self.logger.debug(ppp("Got packet:", p))
            ether = p[Ether]
            self.assert_equal(self.pg0.remote_mac, ether.dst, "Destination MAC")
            self.assert_equal(self.pg0.local_mac, ether.src, "Source MAC")
            ip = p[IP]
            self.assert_equal(self.pg0.remote_ip4, ip.dst, "Destination IP")
            udp = p[UDP]
            if udp.dport == BFD.udp_dport:
                bfd_control_packets_rx += 1
                continue
            self.assert_equal(self.pg0.remote_ip4, ip.src, "Source IP")
            self.assert_equal(udp.dport, BFD.udp_dport_echo, "UDP destination port")
            self.assert_equal(udp.sport, udp_sport_rx, "UDP source port")
            udp_sport_rx += 1
            # need to compare the hex payload here, otherwise BFD_vpp_echo
            # gets in way
            self.assertEqual(
                scapy.compat.raw(p[UDP].payload),
                scapy.compat.raw(echo_packet[UDP].payload),
                "Received packet is not the echo packet sent",
            )
            counter += 1
        self.assert_equal(
            udp_sport_tx,
            udp_sport_rx,
            "UDP source port (== ECHO packet identifier for test purposes)",
        )
        stats_after = bfd_grab_stats_snapshot(self)
        diff = bfd_stats_diff(stats_before, stats_after)
        self.assertEqual(0, diff.rx, "RX counter bumped but no BFD packets sent")
        self.assertEqual(bfd_control_packets_rx, diff.tx, "TX counter incorrect")
        self.assertEqual(
            0, diff.rx_echo, "RX echo counter bumped but no BFD session exists"
        )
        self.assertEqual(
            0, diff.tx_echo, "TX echo counter bumped but no BFD session exists"
        )

    def test_echo(self):
        """echo function"""
        stats_before = bfd_grab_stats_snapshot(self)
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.test_session.send_packet()
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        # echo shouldn't work without echo source set
        for dummy in range(10):
            sleep = self.vpp_session.required_min_rx / USEC_IN_SEC
            self.sleep(sleep, "delay before sending bfd packet")
            self.test_session.send_packet()
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(
            p[BFD].required_min_rx_interval,
            self.vpp_session.required_min_rx,
            "BFD required min rx interval",
        )
        self.test_session.send_packet()
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        echo_seen = False
        # should be turned on - loopback echo packets
        for dummy in range(3):
            loop_until = time.time() + 0.75 * detection_time
            while time.time() < loop_until:
                p = self.pg0.wait_for_packet(1)
                self.logger.debug(ppp("Got packet:", p))
                if p[UDP].dport == BFD.udp_dport_echo:
                    self.assert_equal(p[IP].dst, self.pg0.local_ip4, "BFD ECHO dst IP")
                    self.assertNotEqual(
                        p[IP].src,
                        self.loopback0.local_ip4,
                        "BFD ECHO src IP equal to loopback IP",
                    )
                    self.logger.debug(ppp("Looping back packet:", p))
                    self.assert_equal(
                        p[Ether].dst,
                        self.pg0.remote_mac,
                        "ECHO packet destination MAC address",
                    )
                    p[Ether].dst = self.pg0.local_mac
                    self.pg0.add_stream(p)
                    self.test_session.rx_packets_echo += 1
                    self.test_session.tx_packets_echo += 1
                    self.pg_start()
                    echo_seen = True
                elif p.haslayer(BFD):
                    self.test_session.rx_packets += 1
                    if echo_seen:
                        self.assertGreaterEqual(
                            p[BFD].required_min_rx_interval, 1000000
                        )
                    if "P" in p.sprintf("%BFD.flags%"):
                        final = self.test_session.create_packet()
                        final[BFD].flags = "F"
                        self.test_session.send_packet(final)
                else:
                    raise Exception(ppp("Received unknown packet:", p))

                self.assert_equal(
                    len(self.vapi.collect_events()), 0, "number of bfd events"
                )
            self.test_session.send_packet()
        self.assertTrue(echo_seen, "No echo packets received")

        stats_after = bfd_grab_stats_snapshot(self)
        diff = bfd_stats_diff(stats_before, stats_after)
        # our rx is vpp tx and vice versa, also tolerate one packet off
        self.assert_in_range(
            self.test_session.tx_packets, diff.rx - 1, diff.rx + 1, "RX counter"
        )
        self.assert_in_range(
            self.test_session.rx_packets, diff.tx - 1, diff.tx + 1, "TX counter"
        )
        self.assert_in_range(
            self.test_session.tx_packets_echo,
            diff.rx_echo - 1,
            diff.rx_echo + 1,
            "RX echo counter",
        )
        self.assert_in_range(
            self.test_session.rx_packets_echo,
            diff.tx_echo - 1,
            diff.tx_echo + 1,
            "TX echo counter",
        )

    def test_echo_fail(self):
        """session goes down if echo function fails"""
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.test_session.send_packet()
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        # echo function should be used now, but we will drop the echo packets
        verified_diag = False
        for dummy in range(3):
            loop_until = time.time() + 0.75 * detection_time
            while time.time() < loop_until:
                p = self.pg0.wait_for_packet(1)
                self.logger.debug(ppp("Got packet:", p))
                if p[UDP].dport == BFD.udp_dport_echo:
                    # dropped
                    pass
                elif p.haslayer(BFD):
                    if "P" in p.sprintf("%BFD.flags%"):
                        self.assertGreaterEqual(
                            p[BFD].required_min_rx_interval, 1000000
                        )
                        final = self.test_session.create_packet()
                        final[BFD].flags = "F"
                        self.test_session.send_packet(final)
                    if p[BFD].state == BFDState.down:
                        self.assert_equal(
                            p[BFD].diag, BFDDiagCode.echo_function_failed, BFDDiagCode
                        )
                        verified_diag = True
                else:
                    raise Exception(ppp("Received unknown packet:", p))
            self.test_session.send_packet()
        events = self.vapi.collect_events()
        self.assert_equal(len(events), 1, "number of bfd events")
        self.assert_equal(events[0].state, BFDState.down, BFDState)
        self.assertTrue(verified_diag, "Incorrect diagnostics code received")

    def test_echo_stop(self):
        """echo function stops if peer sets required min echo rx zero"""
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.test_session.send_packet()
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        # wait for first echo packet
        while True:
            p = self.pg0.wait_for_packet(1)
            self.logger.debug(ppp("Got packet:", p))
            if p[UDP].dport == BFD.udp_dport_echo:
                self.logger.debug(ppp("Looping back packet:", p))
                p[Ether].dst = self.pg0.local_mac
                self.pg0.add_stream(p)
                self.pg_start()
                break
            elif p.haslayer(BFD):
                # ignore BFD
                pass
            else:
                raise Exception(ppp("Received unknown packet:", p))
        self.test_session.update(required_min_echo_rx=0)
        self.test_session.send_packet()
        # echo packets shouldn't arrive anymore
        for dummy in range(5):
            wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
            self.test_session.send_packet()
            events = self.vapi.collect_events()
            self.assert_equal(len(events), 0, "number of bfd events")

    def test_echo_source_removed(self):
        """echo function stops if echo source is removed"""
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.test_session.send_packet()
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        # wait for first echo packet
        while True:
            p = self.pg0.wait_for_packet(1)
            self.logger.debug(ppp("Got packet:", p))
            if p[UDP].dport == BFD.udp_dport_echo:
                self.logger.debug(ppp("Looping back packet:", p))
                p[Ether].dst = self.pg0.local_mac
                self.pg0.add_stream(p)
                self.pg_start()
                break
            elif p.haslayer(BFD):
                # ignore BFD
                pass
            else:
                raise Exception(ppp("Received unknown packet:", p))
        self.vapi.bfd_udp_del_echo_source()
        self.test_session.send_packet()
        # echo packets shouldn't arrive anymore
        for dummy in range(5):
            wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
            self.test_session.send_packet()
            events = self.vapi.collect_events()
            self.assert_equal(len(events), 0, "number of bfd events")

    def test_stale_echo(self):
        """stale echo packets don't keep a session up"""
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        self.test_session.send_packet()
        # should be turned on - loopback echo packets
        echo_packet = None
        timeout_at = None
        timeout_ok = False
        for dummy in range(10 * self.vpp_session.detect_mult):
            p = self.pg0.wait_for_packet(1)
            if p[UDP].dport == BFD.udp_dport_echo:
                if echo_packet is None:
                    self.logger.debug(ppp("Got first echo packet:", p))
                    echo_packet = p
                    timeout_at = (
                        time.time()
                        + self.vpp_session.detect_mult
                        * self.test_session.required_min_echo_rx
                        / USEC_IN_SEC
                    )
                else:
                    self.logger.debug(ppp("Got followup echo packet:", p))
                self.logger.debug(ppp("Looping back first echo packet:", p))
                echo_packet[Ether].dst = self.pg0.local_mac
                self.pg0.add_stream(echo_packet)
                self.pg_start()
            elif p.haslayer(BFD):
                self.logger.debug(ppp("Got packet:", p))
                if "P" in p.sprintf("%BFD.flags%"):
                    final = self.test_session.create_packet()
                    final[BFD].flags = "F"
                    self.test_session.send_packet(final)
                if p[BFD].state == BFDState.down:
                    self.assertIsNotNone(
                        timeout_at,
                        "Session went down before first echo packet received",
                    )
                    now = time.time()
                    self.assertGreaterEqual(
                        now,
                        timeout_at,
                        "Session timeout at %s, but is expected at %s"
                        % (now, timeout_at),
                    )
                    self.assert_equal(
                        p[BFD].diag, BFDDiagCode.echo_function_failed, BFDDiagCode
                    )
                    events = self.vapi.collect_events()
                    self.assert_equal(len(events), 1, "number of bfd events")
                    self.assert_equal(events[0].state, BFDState.down, BFDState)
                    timeout_ok = True
                    break
            else:
                raise Exception(ppp("Received unknown packet:", p))
            self.test_session.send_packet()
        self.assertTrue(timeout_ok, "Expected timeout event didn't occur")

    def test_invalid_echo_checksum(self):
        """echo packets with invalid checksum don't keep a session up"""
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        self.test_session.send_packet()
        # should be turned on - loopback echo packets
        timeout_at = None
        timeout_ok = False
        for dummy in range(10 * self.vpp_session.detect_mult):
            p = self.pg0.wait_for_packet(1)
            if p[UDP].dport == BFD.udp_dport_echo:
                self.logger.debug(ppp("Got echo packet:", p))
                if timeout_at is None:
                    timeout_at = (
                        time.time()
                        + self.vpp_session.detect_mult
                        * self.test_session.required_min_echo_rx
                        / USEC_IN_SEC
                    )
                p[BFD_vpp_echo].checksum = getrandbits(64)
                p[Ether].dst = self.pg0.local_mac
                self.logger.debug(ppp("Looping back modified echo packet:", p))
                self.pg0.add_stream(p)
                self.pg_start()
            elif p.haslayer(BFD):
                self.logger.debug(ppp("Got packet:", p))
                if "P" in p.sprintf("%BFD.flags%"):
                    final = self.test_session.create_packet()
                    final[BFD].flags = "F"
                    self.test_session.send_packet(final)
                if p[BFD].state == BFDState.down:
                    self.assertIsNotNone(
                        timeout_at,
                        "Session went down before first echo packet received",
                    )
                    now = time.time()
                    self.assertGreaterEqual(
                        now,
                        timeout_at,
                        "Session timeout at %s, but is expected at %s"
                        % (now, timeout_at),
                    )
                    self.assert_equal(
                        p[BFD].diag, BFDDiagCode.echo_function_failed, BFDDiagCode
                    )
                    events = self.vapi.collect_events()
                    self.assert_equal(len(events), 1, "number of bfd events")
                    self.assert_equal(events[0].state, BFDState.down, BFDState)
                    timeout_ok = True
                    break
            else:
                raise Exception(ppp("Received unknown packet:", p))
            self.test_session.send_packet()
        self.assertTrue(timeout_ok, "Expected timeout event didn't occur")

    def test_admin_up_down(self):
        """put session admin-up and admin-down"""
        bfd_session_up(self)
        self.vpp_session.admin_down()
        self.pg0.enable_capture()
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.admin_down)
        for dummy in range(2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.admin_down, BFDState)
        # try to bring session up - shouldn't be possible
        self.test_session.update(state=BFDState.init)
        self.test_session.send_packet()
        for dummy in range(2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.admin_down, BFDState)
        self.vpp_session.admin_up()
        self.test_session.update(state=BFDState.down)
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.down)
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(p[BFD].state, BFDState.down, BFDState)
        self.test_session.send_packet()
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(p[BFD].state, BFDState.init, BFDState)
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.init)
        self.test_session.update(state=BFDState.up)
        self.test_session.send_packet()
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(p[BFD].state, BFDState.up, BFDState)
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.up)

    def test_config_change_remote_demand(self):
        """configuration change while peer in demand mode"""
        bfd_session_up(self)
        demand = self.test_session.create_packet()
        demand[BFD].flags = "D"
        self.test_session.send_packet(demand)
        self.vpp_session.modify_parameters(
            required_min_rx=2 * self.vpp_session.required_min_rx
        )
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        # poll bit must be set
        self.assertIn("P", p.sprintf("%BFD.flags%"), "Poll bit not set")
        # terminate poll sequence
        final = self.test_session.create_packet()
        final[BFD].flags = "D+F"
        self.test_session.send_packet(final)
        # vpp should be quiet now again
        transmit_time = (
            0.9
            * max(self.vpp_session.required_min_rx, self.test_session.desired_min_tx)
            / USEC_IN_SEC
        )
        count = 0
        for dummy in range(self.test_session.detect_mult * 2):
            self.sleep(transmit_time)
            self.test_session.send_packet(demand)
            try:
                p = wait_for_bfd_packet(self, timeout=0)
                self.logger.error(ppp("Received unexpected packet:", p))
                count += 1
            except CaptureTimeoutError:
                pass
        events = self.vapi.collect_events()
        for e in events:
            self.logger.error("Received unexpected event: %s", e)
        self.assert_equal(count, 0, "number of packets received")
        self.assert_equal(len(events), 0, "number of events received")

    def test_intf_deleted(self):
        """interface with bfd session deleted"""
        intf = VppLoInterface(self)
        intf.config_ip4()
        intf.admin_up()
        sw_if_index = intf.sw_if_index
        vpp_session = VppBFDUDPSession(self, intf, intf.remote_ip4)
        vpp_session.add_vpp_config()
        vpp_session.admin_up()
        intf.remove_vpp_config()
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        self.assert_equal(e.sw_if_index, sw_if_index, "sw_if_index")
        self.assertFalse(vpp_session.query_vpp_config())


@tag_run_solo
@tag_fixme_vpp_workers
class BFD6TestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD) (IPv6)"""

    pg0 = None
    vpp_clock_offset = None
    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFD6TestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces([0])
            cls.pg0.config_ip6()
            cls.pg0.configure_ipv6_neighbors()
            cls.pg0.admin_up()
            cls.pg0.resolve_ndp()
            cls.create_loopback_interfaces(1)
            cls.loopback0 = cls.lo_interfaces[0]
            cls.loopback0.config_ip6()
            cls.loopback0.admin_up()

        except Exception:
            super(BFD6TestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFD6TestCase, cls).tearDownClass()

    def setUp(self):
        super(BFD6TestCase, self).setUp()
        self.factory = AuthKeyFactory()
        self.vapi.want_bfd_events()
        self.pg0.enable_capture()
        try:
            self.bfd_udp4_sessions = self.statistics["/bfd/udp4/sessions"]
            self.bfd_udp6_sessions = self.statistics["/bfd/udp6/sessions"]
            self.vpp_session = VppBFDUDPSession(
                self, self.pg0, self.pg0.remote_ip6, af=AF_INET6
            )
            self.vpp_session.add_vpp_config()
            self.vpp_session.admin_up()
            self.test_session = BFDTestSession(self, self.pg0, AF_INET6)
            self.logger.debug(self.vapi.cli("show adj nbr"))
        except BaseException:
            self.vapi.want_bfd_events(enable_disable=0)
            raise

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=0)
        self.vapi.collect_events()  # clear the event queue
        super(BFD6TestCase, self).tearDown()

    def test_session_up(self):
        """bring BFD session up"""
        bfd_session_up(self)
        bfd_udp4_sessions = self.statistics["/bfd/udp4/sessions"]
        bfd_udp6_sessions = self.statistics["/bfd/udp6/sessions"]
        self.assert_equal(bfd_udp4_sessions, self.bfd_udp4_sessions)
        self.assert_equal(bfd_udp6_sessions - self.bfd_udp6_sessions, 1)

    def test_session_up_by_ip(self):
        """bring BFD session up - first frame looked up by address pair"""
        self.logger.info("BFD: Sending Slow control frame")
        self.test_session.update(my_discriminator=randint(0, 40000000))
        self.test_session.send_packet()
        self.pg0.enable_capture()
        p = self.pg0.wait_for_packet(1)
        self.assert_equal(
            p[BFD].your_discriminator,
            self.test_session.my_discriminator,
            "BFD - your discriminator",
        )
        self.assert_equal(p[BFD].state, BFDState.init, BFDState)
        self.test_session.update(
            your_discriminator=p[BFD].my_discriminator, state=BFDState.up
        )
        self.logger.info("BFD: Waiting for event")
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.init)
        self.logger.info("BFD: Sending Up")
        self.test_session.send_packet()
        self.logger.info("BFD: Waiting for event")
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        verify_event(self, e, expected_state=BFDState.up)
        self.logger.info("BFD: Session is Up")
        self.test_session.update(state=BFDState.up)
        self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_hold_up(self):
        """hold BFD session up"""
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            wait_for_bfd_packet(self)
            self.test_session.send_packet()
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_echo_looped_back(self):
        """echo packets looped back"""
        bfd_session_up(self)
        stats_before = bfd_grab_stats_snapshot(self)
        self.pg0.enable_capture()
        echo_packet_count = 10
        # random source port low enough to increment a few times..
        udp_sport_tx = randint(1, 50000)
        udp_sport_rx = udp_sport_tx
        echo_packet = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6)
            / UDP(dport=BFD.udp_dport_echo)
            / Raw("this should be looped back")
        )
        for dummy in range(echo_packet_count):
            self.sleep(0.01, "delay between echo packets")
            echo_packet[UDP].sport = udp_sport_tx
            udp_sport_tx += 1
            self.logger.debug(ppp("Sending packet:", echo_packet))
            self.pg0.add_stream(echo_packet)
            self.pg_start()
        counter = 0
        bfd_control_packets_rx = 0
        while counter < echo_packet_count:
            p = self.pg0.wait_for_packet(1)
            self.logger.debug(ppp("Got packet:", p))
            ether = p[Ether]
            self.assert_equal(self.pg0.remote_mac, ether.dst, "Destination MAC")
            self.assert_equal(self.pg0.local_mac, ether.src, "Source MAC")
            ip = p[IPv6]
            self.assert_equal(self.pg0.remote_ip6, ip.dst, "Destination IP")
            udp = p[UDP]
            if udp.dport == BFD.udp_dport:
                bfd_control_packets_rx += 1
                continue
            self.assert_equal(self.pg0.remote_ip6, ip.src, "Source IP")
            self.assert_equal(udp.dport, BFD.udp_dport_echo, "UDP destination port")
            self.assert_equal(udp.sport, udp_sport_rx, "UDP source port")
            udp_sport_rx += 1
            # need to compare the hex payload here, otherwise BFD_vpp_echo
            # gets in way
            self.assertEqual(
                scapy.compat.raw(p[UDP].payload),
                scapy.compat.raw(echo_packet[UDP].payload),
                "Received packet is not the echo packet sent",
            )
            counter += 1
        self.assert_equal(
            udp_sport_tx,
            udp_sport_rx,
            "UDP source port (== ECHO packet identifier for test purposes)",
        )
        stats_after = bfd_grab_stats_snapshot(self)
        diff = bfd_stats_diff(stats_before, stats_after)
        self.assertEqual(0, diff.rx, "RX counter bumped but no BFD packets sent")
        self.assertEqual(bfd_control_packets_rx, diff.tx, "TX counter incorrect")
        self.assertEqual(
            0, diff.rx_echo, "RX echo counter bumped but no BFD session exists"
        )
        self.assertEqual(
            0, diff.tx_echo, "TX echo counter bumped but no BFD session exists"
        )

    def test_echo(self):
        """echo function"""
        stats_before = bfd_grab_stats_snapshot(self)
        bfd_session_up(self)
        self.test_session.update(required_min_echo_rx=150000)
        self.test_session.send_packet()
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        # echo shouldn't work without echo source set
        for dummy in range(10):
            sleep = self.vpp_session.required_min_rx / USEC_IN_SEC
            self.sleep(sleep, "delay before sending bfd packet")
            self.test_session.send_packet()
        p = wait_for_bfd_packet(self, pcap_time_min=time.time() - self.vpp_clock_offset)
        self.assert_equal(
            p[BFD].required_min_rx_interval,
            self.vpp_session.required_min_rx,
            "BFD required min rx interval",
        )
        self.test_session.send_packet()
        self.vapi.bfd_udp_set_echo_source(sw_if_index=self.loopback0.sw_if_index)
        echo_seen = False
        # should be turned on - loopback echo packets
        for dummy in range(3):
            loop_until = time.time() + 0.75 * detection_time
            while time.time() < loop_until:
                p = self.pg0.wait_for_packet(1)
                self.logger.debug(ppp("Got packet:", p))
                if p[UDP].dport == BFD.udp_dport_echo:
                    self.assert_equal(
                        p[IPv6].dst, self.pg0.local_ip6, "BFD ECHO dst IP"
                    )
                    self.assertNotEqual(
                        p[IPv6].src,
                        self.loopback0.local_ip6,
                        "BFD ECHO src IP equal to loopback IP",
                    )
                    self.logger.debug(ppp("Looping back packet:", p))
                    self.assert_equal(
                        p[Ether].dst,
                        self.pg0.remote_mac,
                        "ECHO packet destination MAC address",
                    )
                    self.test_session.rx_packets_echo += 1
                    self.test_session.tx_packets_echo += 1
                    p[Ether].dst = self.pg0.local_mac
                    self.pg0.add_stream(p)
                    self.pg_start()
                    echo_seen = True
                elif p.haslayer(BFD):
                    self.test_session.rx_packets += 1
                    if echo_seen:
                        self.assertGreaterEqual(
                            p[BFD].required_min_rx_interval, 1000000
                        )
                    if "P" in p.sprintf("%BFD.flags%"):
                        final = self.test_session.create_packet()
                        final[BFD].flags = "F"
                        self.test_session.send_packet(final)
                else:
                    raise Exception(ppp("Received unknown packet:", p))

                self.assert_equal(
                    len(self.vapi.collect_events()), 0, "number of bfd events"
                )
            self.test_session.send_packet()
        self.assertTrue(echo_seen, "No echo packets received")

        stats_after = bfd_grab_stats_snapshot(self)
        diff = bfd_stats_diff(stats_before, stats_after)
        # our rx is vpp tx and vice versa, also tolerate one packet off
        self.assert_in_range(
            self.test_session.tx_packets, diff.rx - 1, diff.rx + 1, "RX counter"
        )
        self.assert_in_range(
            self.test_session.rx_packets, diff.tx - 1, diff.tx + 1, "TX counter"
        )
        self.assert_in_range(
            self.test_session.tx_packets_echo,
            diff.rx_echo - 1,
            diff.rx_echo + 1,
            "RX echo counter",
        )
        self.assert_in_range(
            self.test_session.rx_packets_echo,
            diff.tx_echo - 1,
            diff.tx_echo + 1,
            "TX echo counter",
        )

    def test_intf_deleted(self):
        """interface with bfd session deleted"""
        intf = VppLoInterface(self)
        intf.config_ip6()
        intf.admin_up()
        sw_if_index = intf.sw_if_index
        vpp_session = VppBFDUDPSession(self, intf, intf.remote_ip6, af=AF_INET6)
        vpp_session.add_vpp_config()
        vpp_session.admin_up()
        intf.remove_vpp_config()
        e = self.vapi.wait_for_event(1, "bfd_udp_session_event")
        self.assert_equal(e.sw_if_index, sw_if_index, "sw_if_index")
        self.assertFalse(vpp_session.query_vpp_config())


@tag_run_solo
class BFDFIBTestCase(VppTestCase):
    """BFD-FIB interactions (IPv6)"""

    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFDFIBTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(BFDFIBTestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDFIBTestCase, self).setUp()
        self.create_pg_interfaces(range(1))

        self.vapi.want_bfd_events()
        self.pg0.enable_capture()

        for i in self.pg_interfaces:
            i.admin_up()
            i.config_ip6()
            i.configure_ipv6_neighbors()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=False)

        super(BFDFIBTestCase, self).tearDown()

    @staticmethod
    def pkt_is_not_data_traffic(p):
        """not data traffic implies BFD or the usual IPv6 ND/RA"""
        if p.haslayer(BFD) or is_ipv6_misc(p):
            return True
        return False

    def test_session_with_fib(self):
        """BFD-FIB interactions"""

        # packets to match against both of the routes
        p = [
            (
                Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
                / IPv6(src="3001::1", dst="2001::1")
                / UDP(sport=1234, dport=1234)
                / Raw(b"\xa5" * 100)
            ),
            (
                Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
                / IPv6(src="3001::1", dst="2002::1")
                / UDP(sport=1234, dport=1234)
                / Raw(b"\xa5" * 100)
            ),
        ]

        # A recursive and a non-recursive route via a next-hop that
        # will have a BFD session
        ip_2001_s_64 = VppIpRoute(
            self,
            "2001::",
            64,
            [VppRoutePath(self.pg0.remote_ip6, self.pg0.sw_if_index)],
        )
        ip_2002_s_64 = VppIpRoute(
            self, "2002::", 64, [VppRoutePath(self.pg0.remote_ip6, 0xFFFFFFFF)]
        )
        ip_2001_s_64.add_vpp_config()
        ip_2002_s_64.add_vpp_config()

        # bring the session up now the routes are present
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip6, af=AF_INET6
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        self.test_session = BFDTestSession(self, self.pg0, AF_INET6)

        # session is up - traffic passes
        bfd_session_up(self)

        self.pg0.add_stream(p)
        self.pg_start()
        for packet in p:
            captured = self.pg0.wait_for_packet(
                1, filter_out_fn=self.pkt_is_not_data_traffic
            )
            self.assertEqual(captured[IPv6].dst, packet[IPv6].dst)

        # session is up - traffic is dropped
        bfd_session_down(self)

        self.pg0.add_stream(p)
        self.pg_start()
        with self.assertRaises(CaptureTimeoutError):
            self.pg0.wait_for_packet(1, self.pkt_is_not_data_traffic)

        # session is up - traffic passes
        bfd_session_up(self)

        self.pg0.add_stream(p)
        self.pg_start()
        for packet in p:
            captured = self.pg0.wait_for_packet(
                1, filter_out_fn=self.pkt_is_not_data_traffic
            )
            self.assertEqual(captured[IPv6].dst, packet[IPv6].dst)


@unittest.skipUnless(config.extended, "part of extended tests")
class BFDTunTestCase(VppTestCase):
    """BFD over GRE tunnel"""

    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFDTunTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(BFDTunTestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDTunTestCase, self).setUp()
        self.create_pg_interfaces(range(1))

        self.vapi.want_bfd_events()
        self.pg0.enable_capture()

        for i in self.pg_interfaces:
            i.admin_up()
            i.config_ip4()
            i.resolve_arp()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=0)

        super(BFDTunTestCase, self).tearDown()

    @staticmethod
    def pkt_is_not_data_traffic(p):
        """not data traffic implies BFD or the usual IPv6 ND/RA"""
        if p.haslayer(BFD) or is_ipv6_misc(p):
            return True
        return False

    def test_bfd_o_gre(self):
        """BFD-o-GRE"""

        # A GRE interface over which to run a BFD session
        gre_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4)
        gre_if.add_vpp_config()
        gre_if.admin_up()
        gre_if.config_ip4()

        # bring the session up now the routes are present
        self.vpp_session = VppBFDUDPSession(
            self, gre_if, gre_if.remote_ip4, is_tunnel=True
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()

        self.test_session = BFDTestSession(
            self,
            gre_if,
            AF_INET,
            tunnel_header=(IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / GRE()),
            phy_interface=self.pg0,
        )

        # packets to match against both of the routes
        p = [
            (
                Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
                / IP(src=self.pg0.remote_ip4, dst=gre_if.remote_ip4)
                / UDP(sport=1234, dport=1234)
                / Raw(b"\xa5" * 100)
            )
        ]

        # session is up - traffic passes
        bfd_session_up(self)

        self.send_and_expect(self.pg0, p, self.pg0)

        # bring session down
        bfd_session_down(self)


@tag_run_solo
class BFDSHA1TestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD) (SHA1 auth)"""

    pg0 = None
    vpp_clock_offset = None
    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFDSHA1TestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces([0])
            cls.pg0.config_ip4()
            cls.pg0.admin_up()
            cls.pg0.resolve_arp()

        except Exception:
            super(BFDSHA1TestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFDSHA1TestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDSHA1TestCase, self).setUp()
        self.factory = AuthKeyFactory()
        self.vapi.want_bfd_events()
        self.pg0.enable_capture()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=False)
        self.vapi.collect_events()  # clear the event queue
        super(BFDSHA1TestCase, self).tearDown()

    def test_session_up(self):
        """bring BFD session up"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)

    def test_hold_up(self):
        """hold BFD session up"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            wait_for_bfd_packet(self)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_hold_up_meticulous(self):
        """hold BFD session up - meticulous auth"""
        key = self.factory.create_random_key(self, BFDAuthType.meticulous_keyed_sha1)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        # specify sequence number so that it wraps
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
            our_seq_number=0xFFFFFFFF - 4,
        )
        bfd_session_up(self)
        for dummy in range(30):
            wait_for_bfd_packet(self)
            self.test_session.inc_seq_num()
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_send_bad_seq_number(self):
        """session is not kept alive by msgs with bad sequence numbers"""
        key = self.factory.create_random_key(self, BFDAuthType.meticulous_keyed_sha1)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        send_until = time.time() + 2 * detection_time
        while time.time() < send_until:
            self.test_session.send_packet()
            self.sleep(
                0.7 * self.vpp_session.required_min_rx / USEC_IN_SEC,
                "time between bfd packets",
            )
        e = self.vapi.collect_events()
        # session should be down now, because the sequence numbers weren't
        # updated
        self.assert_equal(len(e), 1, "number of bfd events")
        verify_event(self, e[0], expected_state=BFDState.down)

    def execute_rogue_session_scenario(
        self,
        vpp_bfd_udp_session,
        legitimate_test_session,
        rogue_test_session,
        rogue_bfd_values=None,
    ):
        """execute a rogue session interaction scenario

        1. create vpp session, add config
        2. bring the legitimate session up
        3. copy the bfd values from legitimate session to rogue session
        4. apply rogue_bfd_values to rogue session
        5. set rogue session state to down
        6. send message to take the session down from the rogue session
        7. assert that the legitimate session is unaffected
        """

        self.vpp_session = vpp_bfd_udp_session
        self.vpp_session.add_vpp_config()
        self.test_session = legitimate_test_session
        # bring vpp session up
        bfd_session_up(self)
        # send packet from rogue session
        rogue_test_session.update(
            my_discriminator=self.test_session.my_discriminator,
            your_discriminator=self.test_session.your_discriminator,
            desired_min_tx=self.test_session.desired_min_tx,
            required_min_rx=self.test_session.required_min_rx,
            detect_mult=self.test_session.detect_mult,
            diag=self.test_session.diag,
            state=self.test_session.state,
            auth_type=self.test_session.auth_type,
        )
        if rogue_bfd_values:
            rogue_test_session.update(**rogue_bfd_values)
        rogue_test_session.update(state=BFDState.down)
        rogue_test_session.send_packet()
        wait_for_bfd_packet(self)
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)

    def test_mismatch_auth(self):
        """session is not brought down by unauthenticated msg"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        legitimate_test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=key, bfd_key_id=vpp_session.bfd_key_id
        )
        rogue_test_session = BFDTestSession(self, self.pg0, AF_INET)
        self.execute_rogue_session_scenario(
            vpp_session, legitimate_test_session, rogue_test_session
        )

    def test_mismatch_bfd_key_id(self):
        """session is not brought down by msg with non-existent key-id"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        # pick a different random bfd key id
        x = randint(0, 255)
        while x == vpp_session.bfd_key_id:
            x = randint(0, 255)
        legitimate_test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=key, bfd_key_id=vpp_session.bfd_key_id
        )
        rogue_test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=key, bfd_key_id=x
        )
        self.execute_rogue_session_scenario(
            vpp_session, legitimate_test_session, rogue_test_session
        )

    def test_mismatched_auth_type(self):
        """session is not brought down by msg with wrong auth type"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        legitimate_test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=key, bfd_key_id=vpp_session.bfd_key_id
        )
        rogue_test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=key, bfd_key_id=vpp_session.bfd_key_id
        )
        self.execute_rogue_session_scenario(
            vpp_session,
            legitimate_test_session,
            rogue_test_session,
            {"auth_type": BFDAuthType.keyed_md5},
        )

    def test_restart(self):
        """simulate remote peer restart and resynchronization"""
        key = self.factory.create_random_key(self, BFDAuthType.meticulous_keyed_sha1)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
            our_seq_number=0,
        )
        bfd_session_up(self)
        # don't send any packets for 2*detection_time
        detection_time = (
            self.test_session.detect_mult
            * self.vpp_session.required_min_rx
            / USEC_IN_SEC
        )
        self.sleep(2 * detection_time, "simulating peer restart")
        events = self.vapi.collect_events()
        self.assert_equal(len(events), 1, "number of bfd events")
        verify_event(self, events[0], expected_state=BFDState.down)
        self.test_session.update(state=BFDState.down)
        # reset sequence number
        self.test_session.our_seq_number = 0
        self.test_session.vpp_seq_number = None
        # now throw away any pending packets
        self.pg0.enable_capture()
        self.test_session.my_discriminator = 0
        bfd_session_up(self)


@tag_run_solo
class BFDAuthOnOffTestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD) (changing auth)"""

    pg0 = None
    vpp_session = None
    test_session = None

    @classmethod
    def setUpClass(cls):
        super(BFDAuthOnOffTestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces([0])
            cls.pg0.config_ip4()
            cls.pg0.admin_up()
            cls.pg0.resolve_arp()

        except Exception:
            super(BFDAuthOnOffTestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFDAuthOnOffTestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDAuthOnOffTestCase, self).setUp()
        self.factory = AuthKeyFactory()
        self.vapi.want_bfd_events()
        self.pg0.enable_capture()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.want_bfd_events(enable_disable=False)
        self.vapi.collect_events()  # clear the event queue
        super(BFDAuthOnOffTestCase, self).tearDown()

    def test_auth_on_immediate(self):
        """turn auth on without disturbing session state (immediate)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(self, self.pg0, AF_INET)
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.vpp_session.activate_auth(key)
        self.test_session.bfd_key_id = self.vpp_session.bfd_key_id
        self.test_session.sha1_key = key
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_auth_off_immediate(self):
        """turn auth off without disturbing session state (immediate)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        # self.vapi.want_bfd_events(enable_disable=0)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.inc_seq_num()
            self.test_session.send_packet()
        self.vpp_session.deactivate_auth()
        self.test_session.bfd_key_id = None
        self.test_session.sha1_key = None
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.inc_seq_num()
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_auth_change_key_immediate(self):
        """change auth key without disturbing session state (immediate)"""
        key1 = self.factory.create_random_key(self)
        key1.add_vpp_config()
        key2 = self.factory.create_random_key(self)
        key2.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key1
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key1,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.vpp_session.activate_auth(key2)
        self.test_session.bfd_key_id = self.vpp_session.bfd_key_id
        self.test_session.sha1_key = key2
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_auth_on_delayed(self):
        """turn auth on without disturbing session state (delayed)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(self, self.pg0, AF_INET)
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            wait_for_bfd_packet(self)
            self.test_session.send_packet()
        self.vpp_session.activate_auth(key, delayed=True)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.test_session.bfd_key_id = self.vpp_session.bfd_key_id
        self.test_session.sha1_key = key
        self.test_session.send_packet()
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_auth_off_delayed(self):
        """turn auth off without disturbing session state (delayed)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.vpp_session.deactivate_auth(delayed=True)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.test_session.bfd_key_id = None
        self.test_session.sha1_key = None
        self.test_session.send_packet()
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")

    def test_auth_change_key_delayed(self):
        """change auth key without disturbing session state (delayed)"""
        key1 = self.factory.create_random_key(self)
        key1.add_vpp_config()
        key2 = self.factory.create_random_key(self)
        key2.add_vpp_config()
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key1
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        self.test_session = BFDTestSession(
            self,
            self.pg0,
            AF_INET,
            sha1_key=key1,
            bfd_key_id=self.vpp_session.bfd_key_id,
        )
        bfd_session_up(self)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.vpp_session.activate_auth(key2, delayed=True)
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.test_session.bfd_key_id = self.vpp_session.bfd_key_id
        self.test_session.sha1_key = key2
        self.test_session.send_packet()
        for dummy in range(self.test_session.detect_mult * 2):
            p = wait_for_bfd_packet(self)
            self.assert_equal(p[BFD].state, BFDState.up, BFDState)
            self.test_session.send_packet()
        self.assert_equal(self.vpp_session.state, BFDState.up, BFDState)
        self.assert_equal(len(self.vapi.collect_events()), 0, "number of bfd events")


@tag_run_solo
class BFDCLITestCase(VppTestCase):
    """Bidirectional Forwarding Detection (BFD) (CLI)"""

    pg0 = None

    @classmethod
    def setUpClass(cls):
        super(BFDCLITestCase, cls).setUpClass()
        cls.vapi.cli("set log class bfd level debug")
        try:
            cls.create_pg_interfaces((0,))
            cls.pg0.config_ip4()
            cls.pg0.config_ip6()
            cls.pg0.resolve_arp()
            cls.pg0.resolve_ndp()

        except Exception:
            super(BFDCLITestCase, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(BFDCLITestCase, cls).tearDownClass()

    def setUp(self):
        super(BFDCLITestCase, self).setUp()
        self.factory = AuthKeyFactory()
        self.pg0.enable_capture()

    def tearDown(self):
        try:
            self.vapi.want_bfd_events(enable_disable=False)
        except UnexpectedApiReturnValueError:
            # some tests aren't subscribed, so this is not an issue
            pass
        self.vapi.collect_events()  # clear the event queue
        super(BFDCLITestCase, self).tearDown()

    def cli_verify_no_response(self, cli):
        """execute a CLI, asserting that the response is empty"""
        self.assert_equal(self.vapi.cli(cli), "", "CLI command response")

    def cli_verify_response(self, cli, expected):
        """execute a CLI, asserting that the response matches expectation"""
        try:
            reply = self.vapi.cli(cli)
        except CliFailedCommandError as cli_error:
            reply = str(cli_error)
        self.assert_equal(reply.strip(), expected, "CLI command response")

    def test_show(self):
        """show commands"""
        k1 = self.factory.create_random_key(self)
        k1.add_vpp_config()
        k2 = self.factory.create_random_key(
            self, auth_type=BFDAuthType.meticulous_keyed_sha1
        )
        k2.add_vpp_config()
        s1 = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        s1.add_vpp_config()
        s2 = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip6, af=AF_INET6, sha1_key=k2
        )
        s2.add_vpp_config()
        self.logger.info(self.vapi.ppcli("show bfd keys"))
        self.logger.info(self.vapi.ppcli("show bfd sessions"))
        self.logger.info(self.vapi.ppcli("show bfd"))

    def test_set_del_sha1_key(self):
        """set/delete SHA1 auth key"""
        k = self.factory.create_random_key(self)
        self.registry.register(k, self.logger)
        self.cli_verify_no_response(
            "bfd key set conf-key-id %s type keyed-sha1 secret %s"
            % (
                k.conf_key_id,
                "".join("{:02x}".format(scapy.compat.orb(c)) for c in k.key),
            )
        )
        self.assertTrue(k.query_vpp_config())
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=k
        )
        self.vpp_session.add_vpp_config()
        self.test_session = BFDTestSession(
            self, self.pg0, AF_INET, sha1_key=k, bfd_key_id=self.vpp_session.bfd_key_id
        )
        self.vapi.want_bfd_events()
        bfd_session_up(self)
        bfd_session_down(self)
        # try to replace the secret for the key - should fail because the key
        # is in-use
        k2 = self.factory.create_random_key(self)
        self.cli_verify_response(
            "bfd key set conf-key-id %s type keyed-sha1 secret %s"
            % (
                k.conf_key_id,
                "".join("{:02x}".format(scapy.compat.orb(c)) for c in k2.key),
            ),
            "bfd key set: `bfd_auth_set_key' API call failed, "
            "rv=-103:BFD object in use",
        )
        # manipulating the session using old secret should still work
        bfd_session_up(self)
        bfd_session_down(self)
        self.vpp_session.remove_vpp_config()
        self.cli_verify_no_response("bfd key del conf-key-id %s" % k.conf_key_id)
        self.assertFalse(k.query_vpp_config())

    def test_set_del_meticulous_sha1_key(self):
        """set/delete meticulous SHA1 auth key"""
        k = self.factory.create_random_key(
            self, auth_type=BFDAuthType.meticulous_keyed_sha1
        )
        self.registry.register(k, self.logger)
        self.cli_verify_no_response(
            "bfd key set conf-key-id %s type meticulous-keyed-sha1 secret %s"
            % (
                k.conf_key_id,
                "".join("{:02x}".format(scapy.compat.orb(c)) for c in k.key),
            )
        )
        self.assertTrue(k.query_vpp_config())
        self.vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip6, af=AF_INET6, sha1_key=k
        )
        self.vpp_session.add_vpp_config()
        self.vpp_session.admin_up()
        self.test_session = BFDTestSession(
            self, self.pg0, AF_INET6, sha1_key=k, bfd_key_id=self.vpp_session.bfd_key_id
        )
        self.vapi.want_bfd_events()
        bfd_session_up(self)
        bfd_session_down(self)
        # try to replace the secret for the key - should fail because the key
        # is in-use
        k2 = self.factory.create_random_key(self)
        self.cli_verify_response(
            "bfd key set conf-key-id %s type keyed-sha1 secret %s"
            % (
                k.conf_key_id,
                "".join("{:02x}".format(scapy.compat.orb(c)) for c in k2.key),
            ),
            "bfd key set: `bfd_auth_set_key' API call failed, "
            "rv=-103:BFD object in use",
        )
        # manipulating the session using old secret should still work
        bfd_session_up(self)
        bfd_session_down(self)
        self.vpp_session.remove_vpp_config()
        self.cli_verify_no_response("bfd key del conf-key-id %s" % k.conf_key_id)
        self.assertFalse(k.query_vpp_config())

    def test_add_mod_del_bfd_udp(self):
        """create/modify/delete IPv4 BFD UDP session"""
        vpp_session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        self.registry.register(vpp_session, self.logger)
        cli_add_cmd = (
            "bfd udp session add interface %s local-addr %s "
            "peer-addr %s desired-min-tx %s required-min-rx %s "
            "detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                vpp_session.desired_min_tx,
                vpp_session.required_min_rx,
                vpp_session.detect_mult,
            )
        )
        self.cli_verify_no_response(cli_add_cmd)
        # 2nd add should fail
        self.cli_verify_response(
            cli_add_cmd,
            "bfd udp session add: `bfd_add_add_session' API call"
            " failed, rv=-101:Duplicate BFD object",
        )
        verify_bfd_session_config(self, vpp_session)
        mod_session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip4,
            required_min_rx=2 * vpp_session.required_min_rx,
            desired_min_tx=3 * vpp_session.desired_min_tx,
            detect_mult=4 * vpp_session.detect_mult,
        )
        self.cli_verify_no_response(
            "bfd udp session mod interface %s local-addr %s peer-addr %s "
            "desired-min-tx %s required-min-rx %s detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                mod_session.desired_min_tx,
                mod_session.required_min_rx,
                mod_session.detect_mult,
            )
        )
        verify_bfd_session_config(self, mod_session)
        cli_del_cmd = (
            "bfd udp session del interface %s local-addr %s "
            "peer-addr %s" % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        self.cli_verify_no_response(cli_del_cmd)
        # 2nd del is expected to fail
        self.cli_verify_response(
            cli_del_cmd,
            "bfd udp session del: `bfd_udp_del_session' API call"
            " failed, rv=-102:No such BFD object",
        )
        self.assertFalse(vpp_session.query_vpp_config())

    def test_add_mod_del_bfd_udp6(self):
        """create/modify/delete IPv6 BFD UDP session"""
        vpp_session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip6, af=AF_INET6)
        self.registry.register(vpp_session, self.logger)
        cli_add_cmd = (
            "bfd udp session add interface %s local-addr %s "
            "peer-addr %s desired-min-tx %s required-min-rx %s "
            "detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip6,
                self.pg0.remote_ip6,
                vpp_session.desired_min_tx,
                vpp_session.required_min_rx,
                vpp_session.detect_mult,
            )
        )
        self.cli_verify_no_response(cli_add_cmd)
        # 2nd add should fail
        self.cli_verify_response(
            cli_add_cmd,
            "bfd udp session add: `bfd_add_add_session' API call"
            " failed, rv=-101:Duplicate BFD object",
        )
        verify_bfd_session_config(self, vpp_session)
        mod_session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip6,
            af=AF_INET6,
            required_min_rx=2 * vpp_session.required_min_rx,
            desired_min_tx=3 * vpp_session.desired_min_tx,
            detect_mult=4 * vpp_session.detect_mult,
        )
        self.cli_verify_no_response(
            "bfd udp session mod interface %s local-addr %s peer-addr %s "
            "desired-min-tx %s required-min-rx %s detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip6,
                self.pg0.remote_ip6,
                mod_session.desired_min_tx,
                mod_session.required_min_rx,
                mod_session.detect_mult,
            )
        )
        verify_bfd_session_config(self, mod_session)
        cli_del_cmd = (
            "bfd udp session del interface %s local-addr %s "
            "peer-addr %s" % (self.pg0.name, self.pg0.local_ip6, self.pg0.remote_ip6)
        )
        self.cli_verify_no_response(cli_del_cmd)
        # 2nd del is expected to fail
        self.cli_verify_response(
            cli_del_cmd,
            "bfd udp session del: `bfd_udp_del_session' API call"
            " failed, rv=-102:No such BFD object",
        )
        self.assertFalse(vpp_session.query_vpp_config())

    def test_add_mod_del_bfd_udp_auth(self):
        """create/modify/delete IPv4 BFD UDP session (authenticated)"""
        key = self.factory.create_random_key(self)
        key.add_vpp_config()
        vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        self.registry.register(vpp_session, self.logger)
        cli_add_cmd = (
            "bfd udp session add interface %s local-addr %s "
            "peer-addr %s desired-min-tx %s required-min-rx %s "
            "detect-mult %s conf-key-id %s bfd-key-id %s"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                vpp_session.desired_min_tx,
                vpp_session.required_min_rx,
                vpp_session.detect_mult,
                key.conf_key_id,
                vpp_session.bfd_key_id,
            )
        )
        self.cli_verify_no_response(cli_add_cmd)
        # 2nd add should fail
        self.cli_verify_response(
            cli_add_cmd,
            "bfd udp session add: `bfd_add_add_session' API call"
            " failed, rv=-101:Duplicate BFD object",
        )
        verify_bfd_session_config(self, vpp_session)
        mod_session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip4,
            sha1_key=key,
            bfd_key_id=vpp_session.bfd_key_id,
            required_min_rx=2 * vpp_session.required_min_rx,
            desired_min_tx=3 * vpp_session.desired_min_tx,
            detect_mult=4 * vpp_session.detect_mult,
        )
        self.cli_verify_no_response(
            "bfd udp session mod interface %s local-addr %s peer-addr %s "
            "desired-min-tx %s required-min-rx %s detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                mod_session.desired_min_tx,
                mod_session.required_min_rx,
                mod_session.detect_mult,
            )
        )
        verify_bfd_session_config(self, mod_session)
        cli_del_cmd = (
            "bfd udp session del interface %s local-addr %s "
            "peer-addr %s" % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        self.cli_verify_no_response(cli_del_cmd)
        # 2nd del is expected to fail
        self.cli_verify_response(
            cli_del_cmd,
            "bfd udp session del: `bfd_udp_del_session' API call"
            " failed, rv=-102:No such BFD object",
        )
        self.assertFalse(vpp_session.query_vpp_config())

    def test_add_mod_del_bfd_udp6_auth(self):
        """create/modify/delete IPv6 BFD UDP session (authenticated)"""
        key = self.factory.create_random_key(
            self, auth_type=BFDAuthType.meticulous_keyed_sha1
        )
        key.add_vpp_config()
        vpp_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip6, af=AF_INET6, sha1_key=key
        )
        self.registry.register(vpp_session, self.logger)
        cli_add_cmd = (
            "bfd udp session add interface %s local-addr %s "
            "peer-addr %s desired-min-tx %s required-min-rx %s "
            "detect-mult %s conf-key-id %s bfd-key-id %s"
            % (
                self.pg0.name,
                self.pg0.local_ip6,
                self.pg0.remote_ip6,
                vpp_session.desired_min_tx,
                vpp_session.required_min_rx,
                vpp_session.detect_mult,
                key.conf_key_id,
                vpp_session.bfd_key_id,
            )
        )
        self.cli_verify_no_response(cli_add_cmd)
        # 2nd add should fail
        self.cli_verify_response(
            cli_add_cmd,
            "bfd udp session add: `bfd_add_add_session' API call"
            " failed, rv=-101:Duplicate BFD object",
        )
        verify_bfd_session_config(self, vpp_session)
        mod_session = VppBFDUDPSession(
            self,
            self.pg0,
            self.pg0.remote_ip6,
            af=AF_INET6,
            sha1_key=key,
            bfd_key_id=vpp_session.bfd_key_id,
            required_min_rx=2 * vpp_session.required_min_rx,
            desired_min_tx=3 * vpp_session.desired_min_tx,
            detect_mult=4 * vpp_session.detect_mult,
        )
        self.cli_verify_no_response(
            "bfd udp session mod interface %s local-addr %s peer-addr %s "
            "desired-min-tx %s required-min-rx %s detect-mult %s"
            % (
                self.pg0.name,
                self.pg0.local_ip6,
                self.pg0.remote_ip6,
                mod_session.desired_min_tx,
                mod_session.required_min_rx,
                mod_session.detect_mult,
            )
        )
        verify_bfd_session_config(self, mod_session)
        cli_del_cmd = (
            "bfd udp session del interface %s local-addr %s "
            "peer-addr %s" % (self.pg0.name, self.pg0.local_ip6, self.pg0.remote_ip6)
        )
        self.cli_verify_no_response(cli_del_cmd)
        # 2nd del is expected to fail
        self.cli_verify_response(
            cli_del_cmd,
            "bfd udp session del: `bfd_udp_del_session' API call"
            " failed, rv=-102:No such BFD object",
        )
        self.assertFalse(vpp_session.query_vpp_config())

    def test_auth_on_off(self):
        """turn authentication on and off"""
        key = self.factory.create_random_key(
            self, auth_type=BFDAuthType.meticulous_keyed_sha1
        )
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        auth_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        session.add_vpp_config()
        cli_activate = (
            "bfd udp session auth activate interface %s local-addr %s "
            "peer-addr %s conf-key-id %s bfd-key-id %s"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                key.conf_key_id,
                auth_session.bfd_key_id,
            )
        )
        self.cli_verify_no_response(cli_activate)
        verify_bfd_session_config(self, auth_session)
        self.cli_verify_no_response(cli_activate)
        verify_bfd_session_config(self, auth_session)
        cli_deactivate = (
            "bfd udp session auth deactivate interface %s local-addr %s "
            "peer-addr %s " % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        self.cli_verify_no_response(cli_deactivate)
        verify_bfd_session_config(self, session)
        self.cli_verify_no_response(cli_deactivate)
        verify_bfd_session_config(self, session)

    def test_auth_on_off_delayed(self):
        """turn authentication on and off (delayed)"""
        key = self.factory.create_random_key(
            self, auth_type=BFDAuthType.meticulous_keyed_sha1
        )
        key.add_vpp_config()
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        auth_session = VppBFDUDPSession(
            self, self.pg0, self.pg0.remote_ip4, sha1_key=key
        )
        session.add_vpp_config()
        cli_activate = (
            "bfd udp session auth activate interface %s local-addr %s "
            "peer-addr %s conf-key-id %s bfd-key-id %s delayed yes"
            % (
                self.pg0.name,
                self.pg0.local_ip4,
                self.pg0.remote_ip4,
                key.conf_key_id,
                auth_session.bfd_key_id,
            )
        )
        self.cli_verify_no_response(cli_activate)
        verify_bfd_session_config(self, auth_session)
        self.cli_verify_no_response(cli_activate)
        verify_bfd_session_config(self, auth_session)
        cli_deactivate = (
            "bfd udp session auth deactivate interface %s local-addr %s "
            "peer-addr %s delayed yes"
            % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        self.cli_verify_no_response(cli_deactivate)
        verify_bfd_session_config(self, session)
        self.cli_verify_no_response(cli_deactivate)
        verify_bfd_session_config(self, session)

    def test_admin_up_down(self):
        """put session admin-up and admin-down"""
        session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4)
        session.add_vpp_config()
        cli_down = (
            "bfd udp session set-flags admin down interface %s local-addr %s "
            "peer-addr %s " % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        cli_up = (
            "bfd udp session set-flags admin up interface %s local-addr %s "
            "peer-addr %s " % (self.pg0.name, self.pg0.local_ip4, self.pg0.remote_ip4)
        )
        self.cli_verify_no_response(cli_down)
        verify_bfd_session_config(self, session, state=BFDState.admin_down)
        self.cli_verify_no_response(cli_up)
        verify_bfd_session_config(self, session, state=BFDState.down)

    def test_set_del_udp_echo_source(self):
        """set/del udp echo source"""
        self.create_loopback_interfaces(1)
        self.loopback0 = self.lo_interfaces[0]
        self.loopback0.admin_up()
        self.cli_verify_response("show bfd echo-source", "UDP echo source is not set.")
        cli_set = "bfd udp echo-source set interface %s" % self.loopback0.name
        self.cli_verify_no_response(cli_set)
        self.cli_verify_response(
            "show bfd echo-source",
            "UDP echo source is: %s\n"
            "IPv4 address usable as echo source: none\n"
            "IPv6 address usable as echo source: none" % self.loopback0.name,
        )
        self.loopback0.config_ip4()
        echo_ip4 = str(
            ipaddress.IPv4Address(
                int(ipaddress.IPv4Address(self.loopback0.local_ip4)) ^ 1
            )
        )
        self.cli_verify_response(
            "show bfd echo-source",
            "UDP echo source is: %s\n"
            "IPv4 address usable as echo source: %s\n"
            "IPv6 address usable as echo source: none"
            % (self.loopback0.name, echo_ip4),
        )
        echo_ip6 = str(
            ipaddress.IPv6Address(
                int(ipaddress.IPv6Address(self.loopback0.local_ip6)) ^ 1
            )
        )
        self.loopback0.config_ip6()
        self.cli_verify_response(
            "show bfd echo-source",
            "UDP echo source is: %s\n"
            "IPv4 address usable as echo source: %s\n"
            "IPv6 address usable as echo source: %s"
            % (self.loopback0.name, echo_ip4, echo_ip6),
        )
        cli_del = "bfd udp echo-source del"
        self.cli_verify_no_response(cli_del)
        self.cli_verify_response("show bfd echo-source", "UDP echo source is not set.")


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)
