def test_cli_route_prefix_table(): kern = kernel.Kernel(log=None, log_id="", table_name="main") if not kern.platform_supported: return # We assume that the main route table always has a default route, and we look for this: # +--------------------------+------------------+ # | Table | Main | # | Address Family | IPv4 | # | Destination | 0.0.0.0/0 | # | Type | Unicast | # | Protocol | Boot | << or Dhcp in some deployments # | Scope | Universe | # | Next-hops | eth0 172.17.0.1 | # | Priority | | # | Preference | | # | Preferred Source Address | | # | Source | | # | Flow | | # | Encapsulation Type | | # | Encapsulation | | # | Metrics | | # | Type of Service | 0 | # | Flags | 0 | # +--------------------------+------------------+ default_prefix = packet_common.make_ip_prefix("0.0.0.0/0") tab_str = kern.cli_route_prefix_table(254, default_prefix).to_string() pattern = (r"[|] Table +[|] Main +[|]\n" r"[|] Address Family +[|] IPv4 +[|]\n" r"[|] Destination +[|] 0\.0\.0\.0/0 +[|]\n" r"[|] Type +[|] Unicast +[|]\n" r"[|] Protocol +[|] (Boot|Dhcp) +[|]\n" r"[|] Scope +[|] Universe +[|]\n") assert re.search(pattern, tab_str) is not None
def test_put_del_route_unreachable(): kern = kernel.Kernel(log=None, log_id="", table_name="main") if not kern.platform_supported: return # Add unreachable prefix in the kernel routing table (no next hops) prefix = packet_common.make_ip_prefix("99.99.99.99/32") nhops = [] rte = fib_route.FibRoute(prefix, nhops) assert kern.put_route(rte) tab_str = kern.cli_route_prefix_table(254, prefix).to_string() pattern = (r"[|] Table +[|] Main +[|]\n" r"[|] Address Family +[|] IPv4 +[|]\n" r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n" r"[|] Type +[|] Unreachable +[|]\n" r"[|] Protocol +[|] RIFT +[|]\n" r"[|] Scope +[|] Universe +[|]\n" r"[|] Next-hops +[|] +[|]\n" r"[|] Priority +[|] 199 +[|]\n") assert re.search(pattern, tab_str) is not None # Replace unreachable route with a route containing one next hop new_nhops = [next_hop.NextHop("lo", packet_common.make_ip_address("127.0.0.1"))] rte = fib_route.FibRoute(prefix, new_nhops) assert kern.put_route(rte) tab_str = kern.cli_route_prefix_table(254, prefix).to_string() pattern = (r"[|] Table +[|] Main +[|]\n" r"[|] Address Family +[|] IPv4 +[|]\n" r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n" r"[|] Type +[|] Unicast +[|]\n" r"[|] Protocol +[|] RIFT +[|]\n" r"[|] Scope +[|] Universe +[|]\n" r"[|] Next-hops +[|] lo 127.0.0.1 +[|]\n" r"[|] Priority +[|] 199 +[|]\n") assert re.search(pattern, tab_str) is not None # Delete next hops assert kern.del_route(prefix)
def test_put_del_route(): kern = Kernel(simulated_interfaces=False, log=None, log_id="", table_name="5") if not kern.platform_supported: return # Put route with one next-hop (with interface but no address) prefix = make_ip_prefix("99.99.99.99/32") nhops = [NextHop(False, "lo", None, None)] rte = FibRoute(prefix, nhops) assert kern.put_route(rte) # Delete route just added assert kern.del_route(prefix) # Put route with one next-hop (with interface and address) prefix = make_ip_prefix("99.99.99.99/32") address = make_ip_address("127.0.0.1") nhops = [NextHop(False, "lo", address, None)] rte = FibRoute(prefix, nhops) assert kern.put_route(rte) # Delete route just added assert kern.del_route(prefix) # Put ECMP route with multiple next-hop (with interface and address) prefix = make_ip_prefix("99.99.99.99/32") address1 = make_ip_address("127.0.0.1") address2 = make_ip_address("127.0.0.2") nhops = [ NextHop(False, "lo", address1, None), NextHop(False, "lo", address2, None) ] rte = FibRoute(prefix, nhops) assert kern.put_route(rte) # Do we display the ECMP route properly? tab_str = kern.cli_route_prefix_table(5, prefix).to_string() pattern = (r"[|] Table +[|] 5 +[|]\n" r"[|] Address Family +[|] IPv4 +[|]\n" r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n" r"[|] Type +[|] Unicast +[|]\n" r"[|] Protocol +[|] RIFT +[|]\n" r"[|] Scope +[|] Universe +[|]\n" r"[|] Next-hops +[|] lo 127\.0\.0\.1 1 +[|]\n" r"[|] +[|] lo 127\.0\.0\.2 1 +[|]\n") assert re.search(pattern, tab_str) is not None # Delete route just added assert kern.del_route(prefix)
def test_put_del_route_errors(): kern = kernel.Kernel(log=None, log_id="", table_name="main") if not kern.platform_supported: return # Attempt to add route with nonsense next-hop interface prefix = packet_common.make_ip_prefix("99.99.99.99/32") nhops = [next_hop.NextHop("nonsense", None)] rte = fib_route.FibRoute(prefix, nhops) assert not kern.put_route(rte)
def test_put_del_route_errors(): kern = Kernel(simulated_interfaces=False, log=None, log_id="", table_name="main") if not kern.platform_supported: return # Attempt to add route with nonsense next-hop interface prefix = make_ip_prefix("99.99.99.99/32") nhops = [NextHop(False, "nonsense", None, None)] rte = FibRoute(prefix, nhops) assert not kern.put_route(rte)
def _delete_superfluous_children(self, prefix_dest, children_prefixes): """ Delete superfluous children of the given prefix from the RIB and the FIB when it is unreachable :param prefix_dest: (Destination) object to check for superfluous children :param children_prefixes: (list) children of given prefix :return: (boolean) if children of the given prefix have been removed or not """ best_route = prefix_dest.best_route if (not best_route.positive_next_hops and best_route.negative_next_hops) \ and not best_route.next_hops and prefix_dest.parent_prefix_dest: for child_prefix in children_prefixes: self.fib.del_route(packet_common.make_ip_prefix(child_prefix)) return True return False
def del_all_rift_routes(self): if not self.platform_supported or self._simulated_interfaces: return if self._table_nr == -1: return routes = self.ipr.get_routes() for route in routes: if route["proto"] != RTPROT_RIFT: continue route_table_nr = route.get_attr('RTA_TABLE') if (self._table_nr is not None) and (self._table_nr != route_table_nr): continue prefix = packet_common.make_ip_prefix( self.kernel_route_dst_prefix_str(route)) self.del_route(prefix)
def route_row_key(row): table_nr = int(row[0]) family_str = row[1] prefix_str = row[2] prefix = packet_common.make_ip_prefix(prefix_str) return (table_nr, family_str, prefix)
def mkp(prefix_str): return packet_common.make_ip_prefix(prefix_str)
def mkp(prefix_str): return make_ip_prefix(prefix_str)