def all_prefix_routes(self, prefix): assert_prefix_address_family(prefix, self.address_family) prefix_str = ip_prefix_str(prefix) if self.destinations.has_key(prefix_str): destination = self.destinations[prefix_str] for rib_route in destination.rib_routes: yield rib_route
def get_route(self, rte_prefix, owner): packet_common.assert_prefix_address_family(rte_prefix, self.address_family) prefix = packet_common.ip_prefix_str(rte_prefix) if self.destinations.has_key(prefix): return self.destinations.get(prefix).get_route(owner) else: return None
def put_route(self, rte): """ Add a RibRoute object to the Destination object associated to the prefix. :param rte: (RibRoute) the object to add to the Destination associated to the prefix :return: """ packet_common.assert_prefix_address_family(rte.prefix, self.address_family) rte.stale = False self.debug("Put %s", rte) # If there is no Destination object for the prefix, create a new Destination object # for the given prefix and insert it in the Trie prefix = packet_common.ip_prefix_str(rte.prefix) if not self.destinations.has_key(prefix): prefix_destination = _Destination(self, rte.prefix) self.destinations.insert(prefix, prefix_destination) else: prefix_destination = self.destinations.get(prefix) # Insert desired route in destination object best_changed = prefix_destination.put_route(rte) # Get children prefixes before performing actions on the prefix # (it can be deleted from the Trie) children_prefixes = self.destinations.children( packet_common.ip_prefix_str(prefix_destination.prefix)) # If best route changed in Destination object if best_changed: # Update prefix in the fib self.fib.put_route(prefix_destination.best_route) # Try to delete superfluous children if not self._delete_superfluous_children(prefix_destination, children_prefixes): # If children have not been deleted, update them self._update_prefix_children(children_prefixes)
def all_prefix_routes(self, rte_prefix): packet_common.assert_prefix_address_family(rte_prefix, self.address_family) prefix = packet_common.ip_prefix_str(rte_prefix) if self.destinations.has_key(prefix): destination = self.destinations[prefix] for rte in destination.routes: yield rte
def put_route(self, fib_rte): packet_common.assert_prefix_address_family(fib_rte.prefix, self.address_family) self.debug("Put %s", fib_rte) self.fib_routes[fib_rte.prefix] = fib_rte if self.kernel is not None: self.kernel.put_route(fib_rte)
def put_route(self, rte): packet_common.assert_prefix_address_family(rte.prefix, self.address_family) self.debug("Put %s", rte) fib_rte = fib_route.FibRoute(rte.prefix, rte.next_hops) self.routes[rte.prefix] = fib_rte if self.kernel is not None: self.kernel.put_route(fib_rte)
def del_route(self, prefix): # Returns True if the route was present in the table and False if not. packet_common.assert_prefix_address_family(prefix, self.address_family) if prefix not in self.fib_routes: self.debug("Attempted delete %s (not present)", prefix) return False self.debug("Delete %s", prefix) del self.fib_routes[prefix] if self.kernel is not None: self.kernel.del_route(prefix) return True
def put_route(self, rte): packet_common.assert_prefix_address_family(rte.prefix, self.address_family) rte.stale = False prefix = rte.prefix self.debug("Put %s", rte) if rte.prefix in self.destinations: destination = self.destinations[prefix] else: destination = _Destination(prefix) self.destinations[prefix] = destination destination.put_route(rte, self.fib)
def test_asserts(): packet_common.add_missing_methods_to_thrift() route_table = mkrt(constants.ADDRESS_FAMILY_IPV4) # Passing the wrong prefix type to assert_prefix_address_family asserts with pytest.raises(Exception): packet_common.assert_prefix_address_family( "1.2.3.0/24", constants.ADDRESS_FAMILY_IPV4) with pytest.raises(Exception): packet_common.assert_prefix_address_family( mkp("1.2.3.0/24"), constants.ADDRESS_FAMILY_IPV6) with pytest.raises(Exception): packet_common.assert_prefix_address_family( mkp("::1.2.3.0/24"), constants.ADDRESS_FAMILY_IPV4) with pytest.raises(Exception): packet_common.assert_prefix_address_family(mkp("1.2.3.0/24"), 999) # Passing the wrong prefix type to the Route constructor asserts with pytest.raises(Exception): _rte = rib_route.RibRoute("1.2.3.0/24", N, []) # Passing the wrong prefix type to get_route asserts with pytest.raises(Exception): _rte = route_table.get_route("1.2.3.0/24", N) # Passing the wrong prefix type to del_route asserts with pytest.raises(Exception): _deleted = route_table.del_route("1.2.3.0/24", N) # The address family of the route must match the address family of the table with pytest.raises(Exception): route_table.put_route( mkr("1111:1111:1111:1111:0000:0000:0000:0000/64", N))
def put_route(self, rib_route): assert_prefix_address_family(rib_route.prefix, self.address_family) rib_route.stale = False self.debug("Put %s", rib_route) prefix = rib_route.prefix prefix_str = ip_prefix_str(prefix) if not self.destinations.has_key(prefix_str): destination = Destination(self, prefix) self.destinations.insert(prefix_str, destination) else: destination = self.destinations.get(prefix_str) best_changed = destination.put_route(rib_route) if best_changed: child_prefix_strs = self.destinations.children(prefix_str) self.update_fib(prefix, child_prefix_strs)
def del_route(self, prefix, owner): # Returns True if the route was present in the table and False if not. packet_common.assert_prefix_address_family(prefix, self.address_family) if prefix in self.destinations: destination = self.destinations[prefix] deleted = destination.del_route(owner, self.fib) if destination.routes == []: del self.destinations[prefix] else: deleted = False if deleted: self.debug("Delete %s", prefix) else: self.debug("Attempted delete %s (not present)", prefix) return deleted
def del_route(self, rte_prefix, owner): """ Delete given prefix and owner RibRoute object. If no more RibRoute objects are available, also delete Destination from trie and from the FIB. :param rte_prefix: (string) prefix to delete :param owner: (int) owner of the prefix :return: (boolean) if the route has been deleted or not """ packet_common.assert_prefix_address_family(rte_prefix, self.address_family) destination_deleted = False best_changed = False children_prefixes = None prefix = packet_common.ip_prefix_str(rte_prefix) # Check if the prefix is stored in the trie if self.destinations.has_key(prefix): # Get children prefixes before performing actions on the prefix # (it can be deleted from the Trie) children_prefixes = self.destinations.children(prefix) destination = self.destinations.get(prefix) # Delete route from the Destination object deleted, best_changed = destination.del_route(owner) # Route was not present in Destination object, nothing to do if deleted: self.debug("Delete %s", prefix) else: self.debug("Attempted delete %s (not present)", prefix) return deleted if not destination.routes: # No more routes available for current prefix, delete it from trie and FIB self.destinations.delete(prefix) self.fib.del_route(rte_prefix) destination_deleted = True elif best_changed: # Best route changed, push it in the FIB self.fib.put_route(destination.best_route) else: deleted = False if deleted and (best_changed or destination_deleted): # If route has been deleted and an event occurred # (best changed or destination deleted), update children self._update_prefix_children(children_prefixes) return deleted
def del_route(self, prefix, owner): assert_prefix_address_family(prefix, self.address_family) prefix_str = ip_prefix_str(prefix) if self.destinations.has_key(prefix_str): destination = self.destinations.get(prefix_str) child_prefix_strs = self.destinations.children(prefix_str) deleted, best_changed = destination.del_route(owner) # If that was the last route for the destination, delete the destination itself if not destination.rib_routes: del self.destinations[prefix_str] else: deleted = False best_changed = False if deleted: self.debug("Delete %s", prefix) else: self.debug("Attempted delete %s (not present)", prefix) if best_changed: self.update_fib(prefix, child_prefix_strs) return deleted
def get_route(self, prefix, owner): packet_common.assert_prefix_address_family(prefix, self.address_family) if prefix in self.destinations: return self.destinations[prefix].get_route(owner) else: return None
def get_route(self, prefix): packet_common.assert_prefix_address_family(prefix, self.address_family) if prefix in self.fib_routes: return self.fib_routes[prefix] else: return None
def all_prefix_routes(self, prefix): packet_common.assert_prefix_address_family(prefix, self.address_family) if prefix in self.destinations: destination = self.destinations[prefix] for rte in destination.routes: yield rte
def get_route(self, prefix, owner): assert_prefix_address_family(prefix, self.address_family) prefix = ip_prefix_str(prefix) if self.destinations.has_key(prefix): return self.destinations.get(prefix).get_route(owner) return None