#!/usr/bin/env python3 import typing from socket import AF_INET6, inet_pton, inet_ntop import unittest from ipaddress import ip_address, IPv4Network, IPv6Network from scapy.packet import Raw from scapy.layers.l2 import Ether, ARP, Dot1Q from scapy.layers.inet import IP, UDP, ICMP from scapy.layers.inet6 import ( IPv6, ICMPv6ND_NS, ICMPv6NDOptSrcLLAddr, ICMPv6ND_NA, ICMPv6EchoRequest, ) from scapy.utils6 import in6_getnsma, in6_getnsmac from scapy.layers.vxlan import VXLAN from scapy.data import ETH_P_IP, ETH_P_IPV6 from framework import tag_fixme_vpp_workers from framework import VppTestCase, VppTestRunner from vpp_object import VppObject from vpp_interface import VppInterface from vpp_ip_route import ( VppIpRoute, VppRoutePath, VppIpTable, VppIpInterfaceAddress, VppIpInterfaceBind, find_route, FibPathProto, FibPathType, ) from vpp_l2 import ( VppBridgeDomain, VppBridgeDomainPort, VppBridgeDomainArpEntry, VppL2FibEntry, find_bridge_domain_port, VppL2Vtr, ) from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint from vpp_ip import DpoProto, get_dpo_proto from vpp_papi import VppEnum, MACAddress from vpp_vxlan_gbp_tunnel import find_vxlan_gbp_tunnel, INDEX_INVALID, \ VppVxlanGbpTunnel from vpp_neighbor import VppNeighbor from vpp_acl import AclRule, VppAcl NUM_PKTS = 67 def find_gbp_endpoint(test, sw_if_index=None, ip=None, mac=None, tep=None, sclass=None, flags=None): if ip: vip = ip if mac: vmac = MACAddress(mac) eps = test.vapi.gbp_endpoint_dump() for ep in eps: if tep: src = tep[0] dst = tep[1] if src != str(ep.endpoint.tun.src) or \ dst != str(ep.endpoint.tun.dst): continue if sw_if_index: if ep.endpoint.sw_if_index != sw_if_index: continue if sclass: if ep.endpoint.sclass != sclass: continue if flags: if flags != (flags & ep.endpoint.flags): continue if ip: for eip in ep.endpoint.ips: if vip == str(eip): return True if mac: if vmac == ep.endpoint.mac: return True return False def find_gbp_vxlan(test: VppTestCase, vni): ts = test.vapi.gbp_vxlan_tunnel_dump() for t in ts: if t.tunnel.vni == vni: return True return False class VppGbpEndpoint(VppObject): """ GBP Endpoint """ @property def mac(self): return str(self.vmac) @property def ip4(self): return self._ip4 @property def fip4(self): return self._fip4 @property def ip6(self): return self._ip6 @property def fip6(self): return self._fip6 @property def ips(self): return [self.ip4, self.ip6] @property def fips(self): return [self.fip4, self.fip6] def __init__(self, test, itf, epg, recirc, ip4, fip4, ip6, fip6, flags=0, tun_src="0.0.0.0", tun_dst="0.0.0.0", mac=True): self._test = test self.itf = itf self.handle = None self.epg = epg self.recirc = recirc self._ip4 = ip4 self._fip4 = fip4 self._ip6 = ip6 self._fip6 = fip6 if mac: self.vmac = MACAddress(self.itf.remote_mac) else: self.vmac = MACAddress("00:00:00:00:00:00") self.flags = flags self.tun_src = tun_src self.tun_dst = tun_dst def encode(self): ips = [self.ip4, self.ip6] return { "sw_if_index": self.itf.sw_if_index, "ips": ips, "n_ips": len(ips), "mac": self.vmac.packed, "sclass": self.epg.sclass, "flags": self.flags, "tun": { "src": self.tun_src, "dst": self.tun_dst, }, } def add_vpp_config(self): res = self._test.vapi.gbp_endpoint_add( endpoint=self.encode(), ) self.handle = res.handle self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): self._test.vapi.gbp_endpoint_del(handle=self.handle) def object_id(self): return "gbp-endpoint:[%d==%d:%s:%d]" % (self.handle, self.itf.sw_if_index, self.ip4, self.epg.sclass) def query_vpp_config(self): return find_gbp_endpoint(self._test, self.itf.sw_if_index, self.ip4) class VppGbpRecirc(VppObject): """ GBP Recirculation Interface """ def __init__(self, test, epg, recirc, is_ext=False): self._test = test self.recirc = recirc self.epg = epg self.is_ext = is_ext def encode(self): return { "is_ext": self.is_ext, "sw_if_index": self.recirc.sw_if_index, "sclass": self.epg.sclass, } def add_vpp_config(self): self._test.vapi.gbp_recirc_add_del( 1, recirc=self.encode(), ) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): self._test.vapi.gbp_recirc_add_del( 0,
from abc import abstractmethod, ABCMeta
from vpp_pg_interface import is_ipv6_misc
from vpp_interface import VppInterface
class VppTunnelInterface(VppInterface):
""" VPP tunnel interface abstration """
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, test, parent_if):
super(VppTunnelInterface, self).__init__(test)
self.parent_if = parent_if
@property
def local_mac(self):
return self.parent_if.local_mac
@property
def remote_mac(self):
return self.parent_if.remote_mac
def enable_capture(self):
return self.parent_if.enable_capture()
def add_stream(self, pkts):
return self.parent_if.add_stream(pkts)
def get_capture(self, expected_count=None, remark=None, timeout=1,
filter_out_fn=is_ipv6_misc):
return self.parent_if.get_capture(expected_count, remark, timeout,
filter_out_fn)