summaryrefslogtreecommitdiffstats
path: root/src/vnet/flow/flow.h
diff options
context:
space:
mode:
authorChenmin Sun <chenmin.sun@intel.com>2020-07-27 17:40:17 +0800
committerDamjan Marion <dmarion@me.com>2020-08-31 12:14:03 +0000
commit34bfa50b61f8cf454ddce9f378e3d5d29f74a72b (patch)
tree37b9e9256a959ce0dbc0bfc66c13814ff59526eb /src/vnet/flow/flow.h
parent29f3c7d2ecac2f9d80bb33e91bd5d1f9d434768a (diff)
flow: code refactor
This is the code refactor for vnet/flow infra and the dpdk_plugin flow implementation. The main works of the refactor are: 1. Added two base flow type: VNET_FLOW_TYPE_IP4 and VNET_FLOW_TYPE_IP6 as the base the flow type 2. All the other flows are derived from the base flow types 3. Removed some flow types that are not currently supported by the hardware, and VPP won't leverage them either: IP4_GTPU_IP4, IP4_GTPU_IP6, IP6_GTPC, IP6_GTPU, IP6_GTPU_IP4, IP6_GTPU_IP6 4. Re-implemented the vnet/flow cli as well as the dpdk_plugin implementation 5. refine cli prompt 6. refine display info in command "show flow entry" Type: refactor Signed-off-by: Chenmin Sun <chenmin.sun@intel.com> Change-Id: Ica5e61c5881adc73b28335fd83e36ec1cb420c96
Diffstat (limited to 'src/vnet/flow/flow.h')
-rw-r--r--src/vnet/flow/flow.h122
1 files changed, 45 insertions, 77 deletions
diff --git a/src/vnet/flow/flow.h b/src/vnet/flow/flow.h
index b5ec7ccd142..04305edf92a 100644
--- a/src/vnet/flow/flow.h
+++ b/src/vnet/flow/flow.h
@@ -26,86 +26,75 @@
#define foreach_flow_type \
/* l2 flow*/ \
_(ETHERNET, ethernet, "ethernet") \
+ /* l3 IP flow */ \
+ _(IP4, ip4, "ipv4") \
+ _(IP6, ip6, "ipv6") \
+ /* IP tunnel flow */ \
+ _(IP4_L2TPV3OIP, ip4_l2tpv3oip, "ipv4-l2tpv3oip") \
+ _(IP4_IPSEC_ESP, ip4_ipsec_esp, "ipv4-ipsec-esp") \
+ _(IP4_IPSEC_AH, ip4_ipsec_ah, "ipv4-ipsec-ah") \
/* l4 flow*/ \
_(IP4_N_TUPLE, ip4_n_tuple, "ipv4-n-tuple") \
_(IP6_N_TUPLE, ip6_n_tuple, "ipv6-n-tuple") \
_(IP4_N_TUPLE_TAGGED, ip4_n_tuple_tagged, "ipv4-n-tuple-tagged") \
_(IP6_N_TUPLE_TAGGED, ip6_n_tuple_tagged, "ipv6-n-tuple-tagged") \
- /* IP tunnel flow */ \
- _(IP4_L2TPV3OIP, ip4_l2tpv3oip, "ipv4-l2tpv3oip") \
- _(IP4_IPSEC_ESP, ip4_ipsec_esp, "ipv4-ipsec-esp") \
- _(IP4_IPSEC_AH, ip4_ipsec_ah, "ipv4-ipsec-ah") \
/* L4 tunnel flow*/ \
_(IP4_VXLAN, ip4_vxlan, "ipv4-vxlan") \
_(IP6_VXLAN, ip6_vxlan, "ipv6-vxlan") \
_(IP4_GTPC, ip4_gtpc, "ipv4-gtpc") \
- _(IP4_GTPU, ip4_gtpu, "ipv4-gtpu") \
- _(IP4_GTPU_IP4, ip4_gtpu_ip4, "ipv4-gtpu-ipv4") \
- _(IP4_GTPU_IP6, ip4_gtpu_ip6, "ipv4-gtpu-ipv6") \
- _(IP6_GTPC, ip6_gtpc, "ipv6-gtpc") \
- _(IP6_GTPU, ip6_gtpu, "ipv6-gtpu") \
- _(IP6_GTPU_IP4, ip6_gtpu_ip4, "ipv6-gtpu-ipv4") \
- _(IP6_GTPU_IP6, ip6_gtpu_ip6, "ipv6-gtpu-ipv6")
+ _(IP4_GTPU, ip4_gtpu, "ipv4-gtpu")
#define foreach_flow_entry_ethernet \
_fe(ethernet_header_t, eth_hdr)
-#define foreach_flow_entry_ip4_n_tuple \
- _fe(ip4_address_and_mask_t, src_addr) \
- _fe(ip4_address_and_mask_t, dst_addr) \
- _fe(ip_port_and_mask_t, src_port) \
- _fe(ip_port_and_mask_t, dst_port) \
- _fe(ip_protocol_t, protocol)
-
-#define foreach_flow_entry_ip6_n_tuple \
- _fe(ip6_address_and_mask_t, src_addr) \
- _fe(ip6_address_and_mask_t, dst_addr) \
- _fe(ip_port_and_mask_t, src_port) \
- _fe(ip_port_and_mask_t, dst_port) \
- _fe(ip_protocol_t, protocol)
-
-#define foreach_flow_entry_ip4_n_tuple_tagged \
+#define foreach_flow_entry_ip4 \
_fe(ip4_address_and_mask_t, src_addr) \
_fe(ip4_address_and_mask_t, dst_addr) \
- _fe(ip_port_and_mask_t, src_port) \
- _fe(ip_port_and_mask_t, dst_port) \
- _fe(ip_protocol_t, protocol)
+ _fe(ip_prot_and_mask_t, protocol)
-#define foreach_flow_entry_ip6_n_tuple_tagged \
+#define foreach_flow_entry_ip6 \
_fe(ip6_address_and_mask_t, src_addr) \
_fe(ip6_address_and_mask_t, dst_addr) \
- _fe(ip_port_and_mask_t, src_port) \
- _fe(ip_port_and_mask_t, dst_port) \
- _fe(ip_protocol_t, protocol)
+ _fe(ip_prot_and_mask_t, protocol)
#define foreach_flow_entry_ip4_l2tpv3oip \
- _fe(ip4_address_and_mask_t, src_addr) \
- _fe(ip4_address_and_mask_t, dst_addr) \
- _fe(ip_protocol_t, protocol) \
+ foreach_flow_entry_ip4 \
_fe(u32, session_id)
#define foreach_flow_entry_ip4_ipsec_esp \
- _fe(ip4_address_and_mask_t, src_addr) \
- _fe(ip4_address_and_mask_t, dst_addr) \
- _fe(ip_protocol_t, protocol) \
+ foreach_flow_entry_ip4 \
_fe(u32, spi)
#define foreach_flow_entry_ip4_ipsec_ah \
- _fe(ip4_address_and_mask_t, src_addr) \
- _fe(ip4_address_and_mask_t, dst_addr) \
- _fe(ip_protocol_t, protocol) \
+ foreach_flow_entry_ip4 \
_fe(u32, spi)
+#define foreach_flow_entry_ip4_n_tuple \
+ foreach_flow_entry_ip4 \
+ _fe(ip_port_and_mask_t, src_port) \
+ _fe(ip_port_and_mask_t, dst_port)
+
+#define foreach_flow_entry_ip6_n_tuple \
+ foreach_flow_entry_ip6 \
+ _fe(ip_port_and_mask_t, src_port) \
+ _fe(ip_port_and_mask_t, dst_port)
+
+#define foreach_flow_entry_ip4_n_tuple_tagged \
+ foreach_flow_entry_ip4 \
+ _fe(ip_port_and_mask_t, src_port) \
+ _fe(ip_port_and_mask_t, dst_port)
+
+#define foreach_flow_entry_ip6_n_tuple_tagged \
+ foreach_flow_entry_ip6 \
+ _fe(ip_port_and_mask_t, src_port) \
+ _fe(ip_port_and_mask_t, dst_port)
+
#define foreach_flow_entry_ip4_vxlan \
- _fe(ip4_address_t, src_addr) \
- _fe(ip4_address_t, dst_addr) \
- _fe(u16, dst_port) \
+ foreach_flow_entry_ip4_n_tuple \
_fe(u16, vni)
#define foreach_flow_entry_ip6_vxlan \
- _fe(ip6_address_t, src_addr) \
- _fe(ip6_address_t, dst_addr) \
- _fe(u16, dst_port) \
+ foreach_flow_entry_ip6_n_tuple \
_fe(u16, vni)
#define foreach_flow_entry_ip4_gtpc \
@@ -116,34 +105,6 @@
foreach_flow_entry_ip4_n_tuple \
_fe(u32, teid)
-#define foreach_flow_entry_ip4_gtpu_ip4 \
- foreach_flow_entry_ip4_gtpu \
- _fe(ip4_address_and_mask_t, inner_src_addr) \
- _fe(ip4_address_and_mask_t, inner_dst_addr)
-
-#define foreach_flow_entry_ip4_gtpu_ip6 \
- foreach_flow_entry_ip4_gtpu \
- _fe(ip6_address_and_mask_t, inner_src_addr) \
- _fe(ip6_address_and_mask_t, inner_dst_addr)
-
-#define foreach_flow_entry_ip6_gtpc \
- foreach_flow_entry_ip6_n_tuple \
- _fe(u32, teid)
-
-#define foreach_flow_entry_ip6_gtpu \
- foreach_flow_entry_ip6_n_tuple \
- _fe(u32, teid)
-
-#define foreach_flow_entry_ip6_gtpu_ip4 \
- foreach_flow_entry_ip6_gtpu \
- _fe(ip4_address_and_mask_t, inner_src_addr) \
- _fe(ip4_address_and_mask_t, inner_dst_addr)
-
-#define foreach_flow_entry_ip6_gtpu_ip6 \
- foreach_flow_entry_ip6_gtpu \
- _fe(ip6_address_and_mask_t, inner_src_addr) \
- _fe(ip6_address_and_mask_t, inner_dst_addr)
-
#define foreach_flow_action \
_(0, COUNT, "count") \
_(1, MARK, "mark") \
@@ -160,7 +121,6 @@ typedef enum
#undef _
} vnet_flow_action_t;
-
#define foreach_flow_error \
_( -1, NOT_SUPPORTED, "not supported") \
_( -2, ALREADY_DONE, "already done") \
@@ -215,6 +175,14 @@ typedef struct
u16 port, mask;
} ip_port_and_mask_t;
+typedef struct
+{
+ ip_protocol_t prot;
+ /* ip protocol mask should be either 0 or 0xFF */
+ /* other values are meanless */
+ u8 mask;
+} ip_prot_and_mask_t;
+
typedef enum
{
VNET_FLOW_TYPE_UNKNOWN,
t */ .highlight .err { color: #960050; background-color: #1e0010 } /* Error */ .highlight .k { color: #66d9ef } /* Keyword */ .highlight .l { color: #ae81ff } /* Literal */ .highlight .n { color: #f8f8f2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
import binascii
import socket
import abc

import six
from six import moves

from util import Host, mk_ll_addr
from vpp_papi import mac_ntop


@six.add_metaclass(abc.ABCMeta)
class VppInterface(object):
    """Generic VPP interface."""

    @property
    def sw_if_index(self):
        """Interface index assigned by VPP."""
        return self._sw_if_index

    @property
    def remote_mac(self):
        """MAC-address of the remote interface "connected" to this interface"""
        return self._remote_hosts[0].mac

    @property
    def local_mac(self):
        """MAC-address of the VPP interface."""
        return self._local_mac

    @property
    def local_addr(self):
        return self._local_addr

    @property
    def remote_addr(self):
        return self._remote_addr

    @property
    def local_addr_n(self):
        return self._local_addr_n

    @property
    def remote_addr_n(self):
        return self._remote_addr_n

    @property
    def local_ip4(self):
        """Local IPv4 address on VPP interface (string)."""
        return self._local_ip4

    @property
    def local_ip4n(self):
        """Local IPv4 address - raw, suitable as API parameter."""
        return socket.inet_pton(socket.AF_INET, self._local_ip4)

    @property
    def remote_ip4(self):
        """IPv4 address of remote peer "connected" to this interface."""
        return self._remote_hosts[0].ip4

    @property
    def remote_ip4n(self):
        """IPv4 address of remote peer - raw, suitable as API parameter."""
        return socket.inet_pton(socket.AF_INET, self.remote_ip4)

    @property
    def local_ip6(self):
        """Local IPv6 address on VPP interface (string)."""
        return self._local_ip6

    @property
    def local_ip6n(self):
        """Local IPv6 address - raw, suitable as API parameter."""
        return socket.inet_pton(socket.AF_INET6, self.local_ip6)

    @property
    def remote_ip6(self):
        """IPv6 address of remote peer "connected" to this interface."""
        return self._remote_hosts[0].ip6

    @property
    def remote_ip6n(self):
        """IPv6 address of remote peer - raw, suitable as API parameter"""
        return socket.inet_pton(socket.AF_INET6, self.remote_ip6)

    @property
    def local_ip6_ll(self):
        """Local IPv6 link-local address on VPP interface (string)."""
        return self._local_ip6_ll

    @property
    def local_ip6n_ll(self):
        """Local IPv6 link-local address - raw, suitable as API parameter."""
        return self._local_ip6n_ll

    @property
    def remote_ip6_ll(self):
        """Link-local IPv6 address of remote peer
        "connected" to this interface."""
        return self._remote_ip6_ll

    @property
    def remote_ip6n_ll(self):
        """Link-local IPv6 address of remote peer
        - raw, suitable as API parameter"""
        return self._remote_ip6n_ll

    @property
    def name(self):
        """Name of the interface."""
        return self._name

    @property
    def dump(self):
        """RAW result of sw_interface_dump for this interface."""
        return self._dump

    @property
    def test(self):
        """Test case creating this interface."""
        return self._test

    @property
    def remote_hosts(self):
        """Remote hosts list"""
        return self._remote_hosts

    @remote_hosts.setter
    def remote_hosts(self, value):
        """
        :param list value: List of remote hosts.
        """
        self._remote_hosts = value
        self._hosts_by_mac = {}
        self._hosts_by_ip4 = {}
        self._hosts_by_ip6 = {}
        for host in self._remote_hosts:
            self._hosts_by_mac[host.mac] = host
            self._hosts_by_ip4[host.ip4] = host
            self._hosts_by_ip6[host.ip6] = host

    def host_by_mac(self, mac):
        """
        :param mac: MAC address to find host by.
        :return: Host object assigned to interface.
        """
        return self._hosts_by_mac[mac]

    def host_by_ip4(self, ip):
        """
        :param ip: IPv4 address to find host by.
        :return: Host object assigned to interface.
        """
        return self._hosts_by_ip4[ip]

    def host_by_ip6(self, ip):
        """
        :param ip: IPv6 address to find host by.
        :return: Host object assigned to interface.
        """
        return self._hosts_by_ip6[ip]

    def generate_remote_hosts(self, count=1):
        """Generate and add remote hosts for the interface.

        :param int count: Number of generated remote hosts.
        """
        self._remote_hosts = []
        self._hosts_by_mac = {}
        self._hosts_by_ip4 = {}
        self._hosts_by_ip6 = {}
        for i in range(
                2, count + 2):  # 0: network address, 1: local vpp address
            mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
            ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
            ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
            ip6_ll = mk_ll_addr(mac)
            host = Host(mac, ip4, ip6, ip6_ll)
            self._remote_hosts.append(host)
            self._hosts_by_mac[mac] = host
            self._hosts_by_ip4[ip4] = host
            self._hosts_by_ip6[ip6] = host

    @abc.abstractmethod
    def __init__(self, test):
        self._test = test

        self._remote_hosts = []
        self._hosts_by_mac = {}
        self._hosts_by_ip4 = {}
        self._hosts_by_ip6 = {}

    def set_mac(self, mac):
        self._local_mac = str(mac)
        self._local_ip6_ll = mk_ll_addr(self._local_mac)
        self.test.vapi.sw_interface_set_mac_address(
            self.sw_if_index, mac.packed)

    def set_sw_if_index(self, sw_if_index):
        self._sw_if_index = sw_if_index

        self.generate_remote_hosts()

        self._local_ip4 = "172.16.%u.1" % self.sw_if_index
        self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
        self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
        self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
                                                   self._local_ip4_subnet)
        self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
        self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
                                                  self._local_ip4_bcast)
        self.local_ip4_prefix_len = 24
        self.has_ip4_config = False
        self.ip4_table_id = 0

        self._local_ip6 = "fd01:%x::1" % self.sw_if_index
        self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
        self.local_ip6_prefix_len = 64
        self.has_ip6_config = False
        self.ip6_table_id = 0

        self._local_addr = {socket.AF_INET: self.local_ip4,
                            socket.AF_INET6: self.local_ip6}
        self._local_addr_n = {socket.AF_INET: self.local_ip4n,
                              socket.AF_INET6: self.local_ip6n}
        self._remote_addr = {socket.AF_INET: self.remote_ip4,
                             socket.AF_INET6: self.remote_ip6}
        self._remote_addr_n = {socket.AF_INET: self.remote_ip4n,
                               socket.AF_INET6: self.remote_ip6n}

        r = self.test.vapi.sw_interface_dump()
        for intf in r:
            if intf.sw_if_index == self.sw_if_index:
                self._name = intf.interface_name.split(b'\0',
                                                       1)[0].decode('utf8')
                self._local_mac = mac_ntop(intf.l2_address)
                self._dump = intf
                break
        else:
            raise Exception(
                "Could not find interface with sw_if_index %d "
                "in interface dump %s" %
                (self.sw_if_index, moves.reprlib.repr(r)))
        self._local_ip6_ll = mk_ll_addr(self.local_mac)
        self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
                                               self.local_ip6_ll)
        self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
        self._remote_ip6n_ll = socket.inet_pton(socket.AF_INET6,
                                                self.remote_ip6_ll)

    def config_ip4(self):
        """Configure IPv4 address on the VPP interface."""
        self.test.vapi.sw_interface_add_del_address(
            sw_if_index=self.sw_if_index, address=self.local_ip4n,
            address_length=self.local_ip4_prefix_len)
        self.has_ip4_config = True

    def unconfig_ip4(self):
        """Remove IPv4 address on the VPP interface."""
        try:
            if self.has_ip4_config:
                self.test.vapi.sw_interface_add_del_address(
                    sw_if_index=self.sw_if_index, address=self.local_ip4n,
                    address_length=self.local_ip4_prefix_len, is_add=0)
        except AttributeError:
            self.has_ip4_config = False
        self.has_ip4_config = False

    def configure_ipv4_neighbors(self):
        """For every remote host assign neighbor's MAC to IPv4 addresses.

        :param vrf_id: The FIB table / VRF ID. (Default value = 0)
        """
        for host in self._remote_hosts:
            self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
                                               host.mac,
                                               host.ip4)

    def config_ip6(self):
        """Configure IPv6 address on the VPP interface."""
        self.test.vapi.sw_interface_add_del_address(
            sw_if_index=self.sw_if_index, address=self._local_ip6n,
            address_length=self.local_ip6_prefix_len, is_ipv6=1)
        self.has_ip6_config = True

    def unconfig_ip6(self):
        """Remove IPv6 address on the VPP interface."""
        try:
            if self.has_ip6_config:
                self.test.vapi.sw_interface_add_del_address(
                    sw_if_index=self.sw_if_index, address=self.local_ip6n,
                    address_length=self.local_ip6_prefix_len, is_ipv6=1,
                    is_add=0)
        except AttributeError:
            self.has_ip6_config = False
        self.has_ip6_config = False

    def configure_ipv6_neighbors(self):
        """For every remote host assign neighbor's MAC to IPv6 addresses.

        :param vrf_id: The FIB table / VRF ID. (Default value = 0)
        """
        for host in self._remote_hosts:
            self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
                                               host.mac,
                                               host.ip6)

    def unconfig(self):
        """Unconfigure IPv6 and IPv4 address on the VPP interface."""
        self.unconfig_ip4()
        self.unconfig_ip6()

    def set_table_ip4(self, table_id):
        """Set the interface in a IPv4 Table.

        .. note:: Must be called before configuring IP4 addresses.
        """
        self.ip4_table_id = table_id
        self.test.vapi.sw_interface_set_table(
            self.sw_if_index, 0, self.ip4_table_id)

    def set_table_ip6(self, table_id):
        """Set the interface in a IPv6 Table.

        .. note:: Must be called before configuring IP6 addresses.
        """
        self.ip6_table_id = table_id
        self.test.vapi.sw_interface_set_table(
            self.sw_if_index, 1, self.ip6_table_id)

    def disable_ipv6_ra(self):
        """Configure IPv6 RA suppress on the VPP interface."""
        self.test.vapi.sw_interface_ip6nd_ra_config(
            sw_if_index=self.sw_if_index,
            suppress=1)

    def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
        """Configure IPv6 RA suppress on the VPP interface."""
        self.test.vapi.sw_interface_ip6nd_ra_config(
            sw_if_index=self.sw_if_index,
            is_no=no,
            suppress=suppress,
            send_unicast=send_unicast)

    # TODO: This should accept ipaddress object.
    def ip6_ra_prefix(self, address, address_length, is_no=0,
                      off_link=0, no_autoconfig=0, use_default=0):
        """Configure IPv6 RA suppress on the VPP interface."""
        self.test.vapi.sw_interface_ip6nd_ra_prefix(
            sw_if_index=self.sw_if_index,
            prefix={'address': address,
                    'address_length': address_length},
            use_default=use_default,
            off_link=off_link, no_autoconfig=no_autoconfig,
            is_no=is_no)

    def admin_up(self):
        """Put interface ADMIN-UP."""
        self.test.vapi.sw_interface_set_flags(self.sw_if_index,
                                              admin_up_down=1)

    def admin_down(self):
        """Put interface ADMIN-down."""
        self.test.vapi.sw_interface_set_flags(self.sw_if_index,
                                              admin_up_down=0)

    def link_up(self):
        """Put interface link-state-UP."""
        self.test.vapi.cli("test interface link-state %s up" % self.name)

    def link_down(self):
        """Put interface link-state-down."""
        self.test.vapi.cli("test interface link-state %s down" % self.name)

    def ip6_enable(self):
        """IPv6 Enable interface"""
        self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
                                                       enable=1)

    def ip6_disable(self):
        """Put interface ADMIN-DOWN."""
        self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
                                                       enable=0)

    def add_sub_if(self, sub_if):
        """Register a sub-interface with this interface.

        :param sub_if: sub-interface
        """
        if not hasattr(self, 'sub_if'):
            self.sub_if = sub_if
        else:
            if isinstance(self.sub_if, list):
                self.sub_if.append(sub_if)
            else:
                self.sub_if = sub_if

    def enable_mpls(self):
        """Enable MPLS on the VPP interface."""
        self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)

    def disable_mpls(self):
        """Enable MPLS on the VPP interface."""
        self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)

    def is_ip4_entry_in_fib_dump(self, dump):
        for i in dump:
            if i.address == self.local_ip4n and \
                    i.address_length == self.local_ip4_prefix_len and \
                    i.table_id == self.ip4_table_id:
                return True
        return False

    def set_unnumbered(self, ip_sw_if_index):
        """ Set the interface to unnumbered via ip_sw_if_index """
        self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
                                                   self.sw_if_index)

    def unset_unnumbered(self, ip_sw_if_index):
        """ Unset the interface to unnumbered via ip_sw_if_index """
        self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
                                                   self.sw_if_index, is_add=0)

    def set_proxy_arp(self, enable=1):
        """ Set the interface to enable/disable Proxy ARP """
        self.test.vapi.proxy_arp_intfc_enable_disable(
            self.sw_if_index,
            enable)

    def query_vpp_config(self):
        dump = self.test.vapi.sw_interface_dump()
        return self.is_interface_config_in_dump(dump)

    def get_interface_config_from_dump(self, dump):
        for i in dump:
            if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
                    i.sw_if_index == self.sw_if_index:
                return i
        else:
            return None

    def is_interface_config_in_dump(self, dump):
        return self.get_interface_config_from_dump(dump) is not None

    def assert_interface_state(self, admin_up_down, link_up_down,
                               expect_event=False):
        if expect_event:
            event = self.test.vapi.wait_for_event(timeout=1,
                                                  name='sw_interface_event')
            self.test.assert_equal(event.sw_if_index, self.sw_if_index,
                                   "sw_if_index")
            self.test.assert_equal(event.admin_up_down, admin_up_down,
                                   "admin state")
            self.test.assert_equal(event.link_up_down, link_up_down,
                                   "link state")
        dump = self.test.vapi.sw_interface_dump()
        if_state = self.get_interface_config_from_dump(dump)
        self.test.assert_equal(if_state.admin_up_down, admin_up_down,
                               "admin state")
        self.test.assert_equal(if_state.link_up_down, link_up_down,
                               "link state")

    def __str__(self):
        return self.name