def test_fix_tire_packet(): packet_common.add_missing_methods_to_thrift() tire_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=encoding.ttypes.TIREPacket( headers=set([ max_tie_header(0), max_tie_header(1), max_tie_header(2) ]) ), tie=None ) ) encoded_packet = packet_common.encode_protocol_packet(tire_protocol_packet) decoded_tire_protocol_packet = packet_common.decode_protocol_packet(encoded_packet) assert tire_protocol_packet == decoded_tire_protocol_packet
def test_fix_external_prefixes_tie_packet(): packet_common.add_missing_methods_to_thrift() tie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=None, tie=encoding.ttypes.TIEPacket( header=max_tie_header(), element=encoding.ttypes.TIEElement( node=None, prefixes=None, positive_disaggregation_prefixes=None, negative_disaggregation_prefixes=None, external_prefixes=max_prefix_tie_element(), keyvalues=None ) ) ) ) packet_info = packet_common.encode_protocol_packet(tie_protocol_packet, None) packet_info.update_env_header(0) packet_info.update_outer_sec_env_header(None, 111, 222, 10) message = b''.join(packet_info.message_parts()) decoded_packet_info = packet_common.decode_message(None, None, message, None, None, None, None) assert not decoded_packet_info.error assert packet_info.protocol_packet == decoded_packet_info.protocol_packet
def test_fix_tire_packet(): packet_common.add_missing_methods_to_thrift() tire_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=encoding.ttypes.TIREPacket( headers=set([ max_tie_header_lifetime(0), max_tie_header_lifetime(1), max_tie_header_lifetime(2) ]) ), tie=None ) ) packet_info = packet_common.encode_protocol_packet(tire_protocol_packet, None) packet_info.update_env_header(0) packet_info.update_outer_sec_env_header(None, 111, 222) message = b''.join(packet_info.message_parts()) decoded_packet_info = packet_common.decode_message(None, None, message, None, None, None, None) assert not decoded_packet_info.error assert packet_info.protocol_packet == decoded_packet_info.protocol_packet
def test_fix_key_value_tie_packet(): packet_common.add_missing_methods_to_thrift() tie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=None, tie=encoding.ttypes.TIEPacket( header=max_tie_header(), element=encoding.ttypes.TIEElement( node=None, prefixes=None, positive_disaggregation_prefixes=None, negative_disaggregation_prefixes=None, external_prefixes=None, keyvalues=encoding.ttypes.KeyValueTIEElement( keyvalues={ "one": "een", "two": "twee" } ) ) ) ) ) encoded_packet = packet_common.encode_protocol_packet(tie_protocol_packet) decoded_tie_protocol_packet = packet_common.decode_protocol_packet(encoded_packet) assert tie_protocol_packet == decoded_tie_protocol_packet
def test_fix_lie_packet(): packet_common.add_missing_methods_to_thrift() lie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16), content=encoding.ttypes.PacketContent(lie=encoding.ttypes.LIEPacket( name="name", local_id=packet_common.MAX_U32, flood_port=packet_common.MAX_U16, link_mtu_size=packet_common.MAX_U32, link_bandwidth=packet_common.MAX_U32, neighbor=encoding.ttypes.Neighbor(originator=packet_common.MAX_U64, remote_id=packet_common.MAX_U32), pod=packet_common.MAX_U32, nonce=packet_common.MAX_U16, last_neighbor_nonce=packet_common.MAX_U16, node_capabilities=encoding.ttypes.NodeCapabilities( flood_reduction=True, hierarchy_indications=common.ttypes.HierarchyIndications. leaf_only), link_capabilities=encoding.ttypes.LinkCapabilities(bfd=False, ), holdtime=packet_common.MAX_U16, not_a_ztp_offer=True, you_are_flood_repeater=True, label=packet_common.MAX_U32), tide=None, tire=None, tie=None)) encoded_packet = packet_common.encode_protocol_packet(lie_protocol_packet) decoded_lie_protocol_packet = packet_common.decode_protocol_packet( encoded_packet) assert lie_protocol_packet == decoded_lie_protocol_packet
def send_protocol_packet(self, protocol_packet): if self._tx_fail: self.debug(self._tx_log, "Failed send {}".format(protocol_packet)) else: encoded_protocol_packet = encode_protocol_packet(protocol_packet) self._mcast_send_handler.send_message(encoded_protocol_packet) self.debug(self._tx_log, "Send {}".format(protocol_packet))
def add_to_message(self, tie_header_lifetime): # Send a separate TIE message each time this function is called. # We only look at the TIE-ID in the queue and not at the header. If we have a more # recent version of the TIE in the TIE-DB than the one requested in the queue, send the # one we have. tie_id = tie_header_lifetime.header.tieid node = self._interface.node db_tie_packet_info = node.find_tie_packet_info(tie_id) if db_tie_packet_info is None: if DEBUG_PRINT and self._debug_tie_id(tie_id): # Print a message for debugging print( "{} {}: interface {} could not send tie-id {} (not in tie-db)" .format(self._timestamp(), self._interface.node.name, self._interface.name, tie_id)) return False if DEBUG_PRINT and self._debug_tie_id(tie_id): # Print a message for debugging print("{} {}: interface {} send tie-id {} tie {}".format( self._timestamp(), self._interface.node.name, self._interface.name, tie_id, db_tie_packet_info)) if DEBUG_CHECK_TIE_ENCODING and self._debug_tie_id(tie_id): # Check whehter the pre-computed encoded packet is correct. check_packet_info = packet_common.encode_protocol_packet( db_tie_packet_info.protocol_packet, self._interface.active_outer_key) if (db_tie_packet_info.encoded_protocol_packet != check_packet_info.encoded_protocol_packet): print("{} {}: interface {} tie-id {} encoding is INCORRECT". format(self._timestamp(), self._interface.node.name, self._interface.name, tie_id)) self._interface.send_packet_info(db_tie_packet_info, flood=True) return True
def test_fix_node_tie_packet(): packet_common.add_missing_methods_to_thrift() tie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=None, tie=encoding.ttypes.TIEPacket( header=max_tie_header(), element=encoding.ttypes.TIEElement( node=encoding.ttypes.NodeTIEElement( level=packet_common.MAX_U16, neighbors={ max_system_id(0): max_neighbor(), max_system_id(1): max_neighbor(), max_system_id(2): max_neighbor() }, capabilities=encoding.ttypes.NodeCapabilities( protocol_minor_version=protocol_minor_version, flood_reduction=True, hierarchy_indications=common.ttypes.HierarchyIndications.leaf_only ), flags=encoding.ttypes.NodeFlags( overload=True ), name="name" ), prefixes=None, positive_disaggregation_prefixes=None, negative_disaggregation_prefixes=None, external_prefixes=None, keyvalues=None ) ) ) ) packet_info = packet_common.encode_protocol_packet(tie_protocol_packet, None) packet_info.update_env_header(0) packet_info.update_outer_sec_env_header(None, 111, 222, 10) message = b''.join(packet_info.message_parts()) decoded_packet_info = packet_common.decode_message(None, None, message, None, None, None, None) assert not decoded_packet_info.error assert packet_info.protocol_packet == decoded_packet_info.protocol_packet
def test_fix_lie_packet(): packet_common.add_missing_methods_to_thrift() lie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=encoding.ttypes.LIEPacket( name="name", local_id=packet_common.MAX_U32, flood_port=packet_common.MAX_U16, link_mtu_size=packet_common.MAX_U32, link_bandwidth=packet_common.MAX_U32, neighbor=encoding.ttypes.Neighbor( originator=packet_common.MAX_U64, remote_id=packet_common.MAX_U32 ), pod=packet_common.MAX_U32, # nonce=packet_common.MAX_U16, # last_neighbor_nonce=packet_common.MAX_U16, node_capabilities=encoding.ttypes.NodeCapabilities( protocol_minor_version=protocol_minor_version, flood_reduction=True, hierarchy_indications=common.ttypes.HierarchyIndications.leaf_only ), link_capabilities=encoding.ttypes.LinkCapabilities( bfd=False, ), holdtime=packet_common.MAX_U16, not_a_ztp_offer=True, you_are_flood_repeater=True, label=packet_common.MAX_U32 ), tide=None, tire=None, tie=None ) ) packet_info = packet_common.encode_protocol_packet(lie_protocol_packet, None) packet_info.update_env_header(0) packet_info.update_outer_sec_env_header(None, 111, 222) message = b''.join(packet_info.message_parts()) decoded_packet_info = packet_common.decode_message(None, None, message, None, None, None, None) assert not decoded_packet_info.error assert packet_info.protocol_packet == decoded_packet_info.protocol_packet
def store_tie_packet(test_node, tie_packet, lifetime): header = encoding.ttypes.PacketHeader(sender=test_node.system_id, level=test_node.level_value()) content = encoding.ttypes.PacketContent(tie=tie_packet) protocol_packet = encoding.ttypes.ProtocolPacket(header=header, content=content) packet_info = packet_common.encode_protocol_packet( protocol_packet, test_node.active_origin_key) packet_info.remaining_tie_lifetime = lifetime test_node.store_tie_packet_info(packet_info) tie_id = content.tie.header.tieid if tie_id.originator == MY_SYSTEM_ID: if tie_id.tietype == NODE: # pylint: disable=protected-access if tie_id.direction == SOUTH: test_node._my_south_prefix_tie_packet_info = packet_info elif tie_id.direction == NORTH: test_node._my_north_prefix_tie_packet_info = packet_info
def send_protocol_packet(self, protocol_packet, flood): if flood: handler = self._flood_send_handler else: handler = self._lie_send_handler to_str = "{}:{}".format(handler.remote_address, handler.port) protocol_packet_str = str(protocol_packet) # TODO: Don't do this expensive to_str if logging is not at level debug encoded_protocol_packet = packet_common.encode_protocol_packet(protocol_packet) if self._tx_fail: self.tx_debug("Simulated send failure %s to %s", protocol_packet_str, to_str) else: try: handler.send_message(encoded_protocol_packet) except socket.error as error: self.tx_error("Error \"%s\" sending %s to %s", error, protocol_packet_str, to_str) return self.tx_debug("Send %s to %s", protocol_packet_str, to_str)
def test_fix_node_tie_packet(): packet_common.add_missing_methods_to_thrift() tie_protocol_packet = encoding.ttypes.ProtocolPacket( header=encoding.ttypes.PacketHeader( major_version=packet_common.MAX_U16, minor_version=packet_common.MAX_U16, sender=packet_common.MAX_U64, level=packet_common.MAX_U16 ), content=encoding.ttypes.PacketContent( lie=None, tide=None, tire=None, tie=encoding.ttypes.TIEPacket( header=max_tie_header(), element=encoding.ttypes.TIEElement( node=encoding.ttypes.NodeTIEElement( level=packet_common.MAX_U16, neighbors={ max_system_id(0): max_neighbor(), max_system_id(1): max_neighbor(), max_system_id(2): max_neighbor() }, capabilities=encoding.ttypes.NodeCapabilities( flood_reduction=True, hierarchy_indications=common.ttypes.HierarchyIndications.leaf_only ), flags=encoding.ttypes.NodeFlags( overload=True ), name="name" ), prefixes=None, positive_disaggregation_prefixes=None, negative_disaggregation_prefixes=None, external_prefixes=None, keyvalues=None ) ) ) ) encoded_packet = packet_common.encode_protocol_packet(tie_protocol_packet) decoded_tie_protocol_packet = packet_common.decode_protocol_packet(encoded_packet) assert tie_protocol_packet == decoded_tie_protocol_packet
def check_process_tie_common(test_node, rx_tie_info_list): # pylint:disable=too-many-statements for rx_tie_info in rx_tie_info_list: rx_tie_packet = make_rx_tie_packet(rx_tie_info) rx_tie_id = rx_tie_packet.header.tieid old_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) header = encoding.ttypes.PacketHeader(sender=MY_SYSTEM_ID, level=MY_LEVEL) content = encoding.ttypes.PacketContent(tie=rx_tie_packet) protocol_packet = encoding.ttypes.ProtocolPacket(header=header, content=content) rx_tie_packet_info = packet_common.encode_protocol_packet( protocol_packet, None) packet_common.set_lifetime(rx_tie_packet_info, rx_tie_info[5]) result = test_node.process_rx_tie_packet_info(rx_tie_packet_info) (start_sending_tie_header, ack_tie_header) = result disposition = rx_tie_info[6] if disposition == TIE_SAME: # Acknowledge the TX TIE which is the "same" as the DB TIE # Note: the age in the DB TIE and the RX TIE could be slightly different; the ACK should # contain the DB TIE header. new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert new_db_tie_packet_info is not None assert new_db_tie_packet_info == old_db_tie_packet_info assert ack_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert start_sending_tie_header is None elif disposition == TIE_NEWER: # Start sending the DB TIE new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert new_db_tie_packet_info is not None assert new_db_tie_packet_info == old_db_tie_packet_info assert start_sending_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert ack_tie_header is None elif disposition == TIE_OLDER: # Store the RX TIE in the DB and ACK it new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert old_db_tie_packet_info is not None assert new_db_tie_packet_info is not None assert new_db_tie_packet_info != old_db_tie_packet_info assert new_db_tie_packet_info.protocol_packet.content.tie.header == rx_tie_packet.header assert ack_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert start_sending_tie_header is None elif disposition == TIE_OLDER_SELF: # Re-originate the DB TIE by bumping up the version to RX TIE version plus one new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert new_db_tie_packet_info is not None assert new_db_tie_packet_info.protocol_packet.content.tie.header.seq_nr == rx_tie_packet.header.seq_nr + 1 assert start_sending_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert ack_tie_header is None elif disposition == TIE_MISSING: # Store the RX TIE in the DB and ACK it new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert old_db_tie_packet_info is None assert new_db_tie_packet_info is not None assert new_db_tie_packet_info.protocol_packet.content.tie.header == rx_tie_packet.header assert ack_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert start_sending_tie_header is None elif disposition == TIE_MISSING_SELF: # Re-originate an empty version of the RX TIE with a higher version than the RX TIE new_db_tie_packet_info = test_node.find_tie_packet_info(rx_tie_id) assert old_db_tie_packet_info is None assert new_db_tie_packet_info is not None assert new_db_tie_packet_info.protocol_packet.content.tie.header.seq_nr == rx_tie_packet.header.seq_nr + 1 assert start_sending_tie_header == new_db_tie_packet_info.protocol_packet.content.tie.header assert ack_tie_header is None else: assert False