Esempio n. 1
0
 def test_basic(self):
     dict_ = {}
     dict_['BestSetSize'] = "best_set_size"
     dict_['CandidatesSetSize'] = "candidates_set_size"
     dict_['HistoryLimit'] = "history_limit"
     dict_['UpdateAfterNumber'] = "update_after_number"
     dict_['UpdateAfterTime'] = "update_after_time"
     dict_['UnwantedASes'] = "1-ff00:0:311,2-ff00:0:312"
     dict_['PropertyRanges'] = {
         'key1': "1-11",
         'key2': "2-12",
         'DelayTime': "0-100"
     }
     dict_['PropertyWeights'] = "property_weights"
     pth_pol2 = PathPolicy()
     pth_pol2.parse_dict(dict_)
     ntools.eq_(pth_pol2.best_set_size, "best_set_size")
     ntools.eq_(pth_pol2.candidates_set_size, "candidates_set_size")
     ntools.eq_(pth_pol2.history_limit, "history_limit")
     ntools.eq_(pth_pol2.update_after_number, "update_after_number")
     ntools.eq_(pth_pol2.update_after_time, "update_after_time")
     ntools.eq_(pth_pol2.property_ranges, {
         'key1': (1, 11),
         'key2': (2, 12),
         'DelayTime': (-1, 101),
     })
     ntools.eq_(pth_pol2.property_weights, "property_weights")
Esempio n. 2
0
 def _setup(self, unwanted=None, reasons=None):
     inst = PathPolicy()
     inst._check_unwanted_ases = create_mock()
     inst._check_unwanted_ases.return_value = unwanted
     inst._check_property_ranges = create_mock()
     inst._check_property_ranges.return_value = reasons
     pcb = create_mock(["short_desc"], class_=PathSegment)
     return inst, pcb
Esempio n. 3
0
 def _setup(self, max_bw=20):
     inst = PathPolicy()
     inst.property_ranges = {
         'PeerLinks': [0, 1], 'HopsLength': [0, 1], 'DelayTime': [0, 1],
         'GuaranteedBandwidth': [0, max_bw],
         'AvailableBandwidth': [0, max_bw], 'TotalBandwidth': [0, max_bw]
     }
     pcb = create_mock(["get_n_peer_links", "get_n_hops", "get_timestamp"])
     return inst, pcb
Esempio n. 4
0
    def __init__(self, server_id, conf_dir, prom_export=None):
        """
        :param str server_id: server identifier.
        :param str conf_dir: configuration directory.
        :param str prom_export: prometheus export address.
        """
        super().__init__(server_id, conf_dir, prom_export=prom_export)
        # TODO: add 2 policies
        self.path_policy = PathPolicy.from_file(
            os.path.join(conf_dir, PATH_POLICY_FILE))
        self.signing_key = get_sig_key(self.conf_dir)
        self.of_gen_key = kdf(self.config.master_as_key, b"Derive OF Key")
        self.hashtree_gen_key = kdf(self.config.master_as_key,
                                    b"Derive hashtree Key")
        logging.info(self.config.__dict__)
        self._hash_tree = None
        self._hash_tree_lock = Lock()
        self._next_tree = None
        self._init_hash_tree()
        self.ifid_state = {}
        for ifid in self.ifid2br:
            self.ifid_state[ifid] = InterfaceState()
        self.ifid_state_lock = RLock()
        self.CTRL_PLD_CLASS_MAP = {
            PayloadClass.PCB: {
                None: self.handle_pcb
            },
            PayloadClass.IFID: {
                None: self.handle_ifid_packet
            },
            PayloadClass.CERT: {
                CertMgmtType.CERT_CHAIN_REQ: self.process_cert_chain_request,
                CertMgmtType.CERT_CHAIN_REPLY: self.process_cert_chain_reply,
                CertMgmtType.TRC_REPLY: self.process_trc_reply,
                CertMgmtType.TRC_REQ: self.process_trc_request,
            },
            PayloadClass.PATH: {
                PMT.IFSTATE_REQ: self._handle_ifstate_request,
                PMT.REVOCATION: self._handle_revocation,
            },
        }
        self.SCMP_PLD_CLASS_MAP = {
            SCMPClass.PATH: {
                SCMPPathClass.REVOKED_IF: self._handle_scmp_revocation,
            },
        }

        zkid = ZkID.from_values(self.addr.isd_as, self.id,
                                [(self.addr.host, self._port)]).pack()
        self.zk = Zookeeper(self.addr.isd_as, BEACON_SERVICE, zkid,
                            self.topology.zookeepers)
        self.zk.retry("Joining party", self.zk.party_setup)
        self.pcb_cache = ZkSharedCache(self.zk, self.ZK_PCB_CACHE_PATH,
                                       self._handle_pcbs_from_zk)
        self.revobjs_cache = ZkSharedCache(self.zk, self.ZK_REVOCATIONS_PATH,
                                           self.process_rev_objects)
        self.local_rev_cache = ExpiringDict(
            1000, HASHTREE_EPOCH_TIME + HASHTREE_EPOCH_TOLERANCE)
        self._rev_seg_lock = RLock()
Esempio n. 5
0
 def _write_conf_policies(self, topo_dicts):
     """
     Write AS configurations and path policies.
     """
     as_confs = {}
     for topo_id, as_topo, base in srv_iter(
             topo_dicts, self.args.output_dir, common=True):
         as_confs.setdefault(topo_id, yaml.dump(
             self._gen_as_conf(as_topo), default_flow_style=False))
         conf_file = os.path.join(base, AS_CONF_FILE)
         write_file(conf_file, as_confs[topo_id])
         # Confirm that config parses cleanly.
         Config.from_file(conf_file)
         copy_file(self.args.path_policy,
                   os.path.join(base, PATH_POLICY_FILE))
     # Confirm that parser actually works on path policy file
     PathPolicy.from_file(self.args.path_policy)
Esempio n. 6
0
    def __init__(self, server_id, conf_dir):
        """
        :param str server_id: server identifier.
        :param str conf_dir: configuration directory.
        """
        super().__init__(server_id, conf_dir)
        # TODO: add 2 policies
        self.path_policy = PathPolicy.from_file(
            os.path.join(conf_dir, PATH_POLICY_FILE))
        self.unverified_beacons = deque()
        self.trc_requests = {}
        self.trcs = {}
        sig_key_file = get_sig_key_file_path(self.conf_dir)
        self.signing_key = base64.b64decode(read_file(sig_key_file))
        self.of_gen_key = PBKDF2(self.config.master_as_key, b"Derive OF Key")
        logging.info(self.config.__dict__)
        self.if2rev_tokens = {}
        self._if_rev_token_lock = threading.Lock()
        self.revs_to_downstream = ExpiringDict(max_len=1000,
                                               max_age_seconds=60)

        self.ifid_state = {}
        for ifid in self.ifid2er:
            self.ifid_state[ifid] = InterfaceState()

        self.CTRL_PLD_CLASS_MAP = {
            PayloadClass.PCB: {
                PCBType.SEGMENT: self.handle_pcb
            },
            PayloadClass.IFID: {
                IFIDType.PAYLOAD: self.handle_ifid_packet
            },
            PayloadClass.CERT: {
                CertMgmtType.CERT_CHAIN_REPLY: self.process_cert_chain_rep,
                CertMgmtType.TRC_REPLY: self.process_trc_rep,
            },
            PayloadClass.PATH: {
                PMT.IFSTATE_REQ: self._handle_ifstate_request
            },
        }

        # Add more IPs here if we support dual-stack
        name_addrs = "\0".join(
            [self.id, str(SCION_UDP_PORT),
             str(self.addr.host)])
        self.zk = Zookeeper(self.addr.isd_as, BEACON_SERVICE, name_addrs,
                            self.topology.zookeepers)
        self.zk.retry("Joining party", self.zk.party_setup)
        self.incoming_pcbs = deque()
        self.pcb_cache = ZkSharedCache(self.zk, self.ZK_PCB_CACHE_PATH,
                                       self.process_pcbs)
        self.revobjs_cache = ZkSharedCache(self.zk, self.ZK_REVOCATIONS_PATH,
                                           self.process_rev_objects)
Esempio n. 7
0
    def test(self):
        """
        Test the main functionalities of the path store.
        """
        path_policy_file = "topology/ISD1/path_policies/ISD1-AD10.json"
        path_policy = PathPolicy.from_file(path_policy_file)
        test_segments = PathStore(path_policy)
        print("Best paths: " + str(len(test_segments.get_best_segments())))
        print("Paths in path store: " + str(len(test_segments.candidates)))
        print("Paths in latest history snapshot: " +
              str(len(test_segments.get_latest_history_snapshot())) + "\n")

        path = 1
        for _ in range(1, 6):
            for _ in range(1, 6):
                pcb = PathSegment()
                pcb.iof = InfoOpaqueField.from_values(OFT.TDC_XOVR, False,
                                                      int(time.time()), path)
                ad_marking = self._create_ad_marking()
                pcb.add_ad(ad_marking)
                print("insert path " + str(path) + ", exp time: " +
                      str(pcb.get_expiration_time()))
                test_segments.add_segment(pcb)
                path += 1
            print("Best paths: " + str(len(test_segments.get_best_segments())))
            print("Paths in path store: " + str(len(test_segments.candidates)))
            print("Paths in latest history snapshot: " +
                  str(len(test_segments.get_latest_history_snapshot())))
            print("Time: " + str(int(time.time())) + "\n")
            time.sleep(5)

        print("Waiting for some paths to expire...")
        time.sleep(25)
        print("Best paths: " + str(len(test_segments.get_best_segments())))
        print("Paths in path store: " + str(len(test_segments.candidates)))
        print("Paths in latest history snapshot: " +
              str(len(test_segments.get_latest_history_snapshot())))
Esempio n. 8
0
 def test_basic(self, init, time_):
     path_policy = PathPolicy()
     path_policy.property_weights['PeerLinks'] = 10
     path_policy.property_weights['HopsLength'] = 1
     path_policy.property_weights['Disjointness'] = 2
     path_policy.property_weights['LastSentTime'] = 3
     path_policy.property_weights['LastSeenTime'] = 4
     path_policy.property_weights['DelayTime'] = 5
     path_policy.property_weights['ExpirationTime'] = 6
     path_policy.property_weights['GuaranteedBandwidth'] = 7
     path_policy.property_weights['AvailableBandwidth'] = 8
     path_policy.property_weights['TotalBandwidth'] = 9
     pth_str_rec = PathStoreRecord("pcb")
     pth_str_rec.peer_links = 10**5
     pth_str_rec.hops_length = (1 / (10**4))
     pth_str_rec.disjointness = 10**3
     pth_str_rec.last_sent_time = -99
     pth_str_rec.last_seen_time = 10
     pth_str_rec.delay_time = 1
     pth_str_rec.expiration_time = 10 / 9
     pth_str_rec.guaranteed_bandwidth = 10**-2
     pth_str_rec.available_bandwidth = 10**-3
     pth_str_rec.total_bandwidth = 10**-4
     time_.return_value = 1
     pth_str_rec.update_fidelity(path_policy)
     ntools.assert_almost_equal(pth_str_rec.fidelity, 1012345.6789)
Esempio n. 9
0
    def __init__(self,
                 server_id,
                 conf_dir,
                 spki_cache_dir=GEN_CACHE_PATH,
                 prom_export=None,
                 sciond_path=None):
        """
        :param str server_id: server identifier.
        :param str conf_dir: configuration directory.
        :param str prom_export: prometheus export address.
        :param str sciond_path: path to sciond socket.
        """
        super().__init__(server_id,
                         conf_dir,
                         spki_cache_dir=spki_cache_dir,
                         prom_export=prom_export,
                         sciond_path=sciond_path)
        self.config = self._load_as_conf()
        self.master_key_0 = get_master_key(self.conf_dir, MASTER_KEY_0)
        self.master_key_1 = get_master_key(self.conf_dir, MASTER_KEY_1)
        # TODO: add 2 policies
        self.path_policy = PathPolicy.from_file(
            os.path.join(conf_dir, PATH_POLICY_FILE))
        self.signing_key = get_sig_key(self.conf_dir)
        self.of_gen_key = kdf(self.master_key_0, b"Derive OF Key")
        # Amount of time units a HOF is valid (time unit is EXP_TIME_UNIT).
        self.default_hof_exp_time = int(self.config.segment_ttl /
                                        EXP_TIME_UNIT)
        self.ifid_state = {}
        for ifid in self.ifid2br:
            self.ifid_state[ifid] = InterfaceState()
        self.ifid_state_lock = RLock()
        self.if_revocations = {}
        self.CTRL_PLD_CLASS_MAP = {
            PayloadClass.PCB: {
                PayloadClass.PCB: self.handle_pcb
            },
            PayloadClass.IFID: {
                PayloadClass.IFID: self.handle_ifid_packet
            },
            PayloadClass.CERT: {
                CertMgmtType.CERT_CHAIN_REQ: self.process_cert_chain_request,
                CertMgmtType.CERT_CHAIN_REPLY: self.process_cert_chain_reply,
                CertMgmtType.TRC_REPLY: self.process_trc_reply,
                CertMgmtType.TRC_REQ: self.process_trc_request,
            },
            PayloadClass.PATH: {
                PMT.IFSTATE_REQ: self._handle_ifstate_request,
                PMT.REVOCATION: self._handle_revocation,
            },
        }
        self.SCMP_PLD_CLASS_MAP = {
            SCMPClass.PATH: {
                SCMPPathClass.REVOKED_IF: self._handle_scmp_revocation,
            },
        }

        zkid = ZkID.from_values(self.addr.isd_as, self.id,
                                [(self.addr.host, self._port)]).pack()
        self.zk = Zookeeper(self.addr.isd_as, self.SERVICE_TYPE, zkid,
                            self.topology.zookeepers)
        self.zk.retry("Joining party", self.zk.party_setup)
        self.pcb_cache = ZkSharedCache(self.zk, self.ZK_PCB_CACHE_PATH,
                                       self._handle_pcbs_from_zk)
        self.revobjs_cache = ZkSharedCache(self.zk, self.ZK_REVOCATIONS_PATH,
                                           self.process_rev_objects)
        self.local_rev_cache = RevCache()
        self._rev_seg_lock = RLock()