""" IP Routes object abstractions for representing IP routes in VPP """ from vpp_object import VppObject from vpp_ip import DpoProto, INVALID_INDEX, VppIpAddressUnion, VppIpMPrefix from ipaddress import ip_network, ip_address, IPv4Network, IPv6Network from vpp_papi_exceptions import UnexpectedApiReturnValueError # from vnet/vnet/mpls/mpls_types.h MPLS_IETF_MAX_LABEL = 0xFFFFF MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 try: text_type = unicode except NameError: text_type = str class FibPathProto: FIB_PATH_NH_PROTO_IP4 = 0 FIB_PATH_NH_PROTO_IP6 = 1 FIB_PATH_NH_PROTO_MPLS = 2 FIB_PATH_NH_PROTO_ETHERNET = 3 FIB_PATH_NH_PROTO_BIER = 4 FIB_PATH_NH_PROTO_NSH = 5 class FibPathType: FIB_PATH_TYPE_NORMAL = 0 FIB_PATH_TYPE_LOCAL = 1 FIB_PATH_TYPE_DROP = 2 FIB_PATH_TYPE_UDP_ENCAP = 3 FIB_PATH_TYPE_BIER_IMP = 4 FIB_PATH_TYPE_ICMP_UNREACH = 5 FIB_PATH_TYPE_ICMP_PROHIBIT = 6 FIB_PATH_TYPE_SOURCE_LOOKUP = 7 FIB_PATH_TYPE_DVR = 8 FIB_PATH_TYPE_INTERFACE_RX = 9 FIB_PATH_TYPE_CLASSIFY = 10 class FibPathFlags: FIB_PATH_FLAG_NONE = 0 FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2 FIB_PATH_FLAG_POP_PW_CW = 4 class MplsLspMode: PIPE = 0 UNIFORM = 1 def mk_network(addr, len): if ip_address(text_type(addr)).version == 4: return IPv4Network("%s/%d" % (addr, len), strict=False) else: return IPv6Network("%s/%d" % (addr, len), strict=False) def ip_to_dpo_proto(addr): if addr.version == 6: return DpoProto.DPO_PROTO_IP6 else: return DpoProto.DPO_PROTO_IP4 def address_proto(ip_addr): if ip_addr.ip_addr.version == 4: return FibPathProto.FIB_PATH_NH_PROTO_IP4 else: return FibPathProto.FIB_PATH_NH_PROTO_IP6 def find_route(test, addr, len, table_id=0, sw_if_index=None): prefix = mk_network(addr, len) if 4 == prefix.version: routes = test.vapi.ip_route_dump(table_id, False) else: routes = test.vapi.ip_route_dump(table_id, True) for e in routes: if table_id == e.route.table_id and str(e.route.prefix) == str(prefix): if not sw_if_index: return True else: # should be only one path if the user is looking # for the interface the route is reachable through if e.route.n_paths != 1: return False else: return e.route.paths[0].sw_if_index == sw_if_index return False def find_route_in_dump(dump, route, table): for r in dump: if table.table_id == r.route.table_id and route.prefix == r.route.prefix: if len(route.paths) == r.route.n_paths: return True return False def find_mroute_in_dump(dump, route, table): for r in dump: if table.table_id == r.route.table_id and route.prefix == r.route.prefix: return True return False def find_mroute(test, grp_addr, src_addr, grp_addr_len, table_id=0): ip_mprefix = VppIpMPrefix(text_type(src_addr), text_type(grp_addr), grp_addr_len) if 4 == ip_mprefix.version: routes = test.vapi.ip_mroute_dump(table_id, False) else: routes = test.vapi.ip_mroute_dump(table_id, True) for e in routes: if table_id == e.route.table_id and ip_mprefix == e.route.prefix: return True return False def find_mpls_route(test, table_id, label, eos_bit, paths=None): dump = test.vapi.mpls_route_dump(table_id) for e in dump: if ( label == e.mr_route.mr_label and eos_bit == e.mr_route.mr_eos and table_id == e.mr_route.mr_table_id ): if not paths: return True else: if len(paths) != len(e.mr_route.mr_paths): return False for i in range(len(paths)): if paths[i] != e.mr_route.mr_paths[i]: return False return True return False def fib_interface_ip_prefix(test, addr, len, sw_if_index): # can't use python net here since we need the host bits in the prefix prefix = "%s/%d" % (addr, len) addrs = test.vapi.ip_address_dump( sw_if_index, is_ipv6=(6 == ip_address(addr).version) ) for a in addrs: if a.sw_if_index == sw_if_index and str(a.prefix) == prefix: return True return False class VppIpTable(VppObject): def __init__(self, test, table_id, is_ip6=0, register=True): self._test = test self.table_id = table_id self.is_ip6 = is_ip6 self.register = register def add_vpp_config(self): self._test.vapi.ip_table_add_del( is_add=1, table={"is_ip6": self.is_ip6, "table_id": self.table_id} ) if self.register: self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): self._test.vapi.ip_table_add_del( is_add=0, table={"is_ip6": self.is_ip6, "table_id": self.table_id} ) def replace_begin(self): self._test.vapi.ip_table_replace_begin( table={"is_ip6": self.is_ip6, "table_id": self.table_id} ) def replace_end(self): self._test.vapi.ip_table_replace_end( table={"is_ip6": self.is_ip6, "table_id": self.table_id} ) def flush(self): self._test.vapi.ip_table_flush( table={"is_ip6": self.is_ip6, "table_id": self.table_id} ) def dump(self): return self._test.vapi.ip_route_dump(self.table_id, self.is_ip6) def mdump(self): return self._test.vapi.ip_mroute_dump(self.table_id, self.is_ip6) def query_vpp_config(self): if self.table_id == 0: # the default table always exists return False # find the default route return find_route( self._test, "::" if self.is_ip6 else "0.0.0.0", 0, self.table_id ) def object_id(self): return "table-%s-%d" % ("v6" if self.is_ip6 == 1 else "v4", self.table_id) class VppIpInterfaceAddress(VppObject): def __init__(self, test, intf, addr, len, bind=None): self._test = test self.intf = intf self.addr = addr self.len = len self.prefix = "%s/%d" % (addr, len) self.host_len = ip_network(self.prefix, strict=False).max_prefixlen self.table_id = 0 if bind: self.table_id = bind.table.table_id def add_vpp_config(self): self._test.vapi.sw_interface_add_del_address( sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=1 ) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): self._test.vapi.sw_interface_add_del_address( sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=0 ) def query_vpp_config(self): # search for the IP address mapping and the two expected # FIB entries v = ip_address(self.addr).version if (v == 4 and self.len < 31) or (v == 6 and self.len < 127): return ( fib_interface_ip_prefix( self._test, self.addr, self.len, self.intf.sw_if_index ) & find_route( self._test, self.addr, self.len, table_id=self.table_id, sw_if_index=self.intf.sw_if_index, ) & find_route( self._test, self.addr, self.host_len, table_id=self.table_id, sw_if_index=self.intf.sw_if_index, ) ) else: return fib_interface_ip_prefix( self._test, self.addr, self.len, self.intf.sw_if_index ) & find_route( self._test, self.addr, self.host_len, table_id=self.table_id, sw_if_index=self.intf.sw_if_index, ) def object_id(self): return "interface-ip-%s-%d-%s" % (self.intf, self.table_id, self.prefix) class VppIp6LinkLocalAddress(VppObject): def __init__(self, test, intf, addr): self._test = test self.intf = intf self.addr = addr def add_vpp_config(self): self._test.vapi.sw_interface_ip6_set_link_local_address( sw_if_index=self.intf.sw_if_index, ip=self.addr ) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): # link locals can't be removed, only changed pass def query_vpp_config(self): # no API to query return False def object_id(self): return "ip6-link-local-%s-%s" % (self.intf, self.addr) class VppIpInterfaceBind(VppObject): def __init__(self, test, intf, table): self._test = test self.intf = intf self.table = table def add_vpp_config(self): if self.table.is_ip6: self.intf.set_table_ip6(self.table.table_id) else: self.intf.set_table_ip4(self.table.table_id) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): if 0 == self.table.table_id: return if self.table.is_ip6: self.intf.set_table_ip6(0) else: self.intf.set_table_ip4(0) def query_vpp_config(self): if 0 == self.table.table_id: return False try: return ( self._test.vapi.sw_interface_get_table( self.intf.sw_if_index, self.table.is_ip6 ).vrf_id == self.table.table_id ) except UnexpectedApiReturnValueError as e: if e.retval == -2: # INVALID_SW_IF_INDEX return False raise def object_id(self): return "interface-bind-%s-%s" % (self.intf, self.table) class VppMplsLabel: def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0): self.value = value self.mode = mode self.ttl = ttl self.exp = exp def encode(self): is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1 return { "label": self.value, "ttl": self.ttl, "exp": self.exp, "is_uniform": is_uniform, } def __eq__(self, other): if isinstance(other, self.__class__): return ( self.value == other.value and self.ttl == other.ttl and self.exp == other.exp and self.mode == other.mode ) elif hasattr(other, "label"): return ( self.value == other.label and self.ttl == other.ttl and self.exp == other.exp
Csit1234