예제 #1
0
    def add_flow(self, datapath, priority=ofproto_v1_3.OFP_DEFAULT_PRIORITY, match=None,
                  actions=None,idle_timeout=0, hard_timeout=0, buffer_id=ofproto_v1_3.OFP_NO_BUFFER):
        ofp = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        if actions != None:
            inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]
        else:
            inst = []

        cookie = random.randint(0, 0xffffffffffffffff)

        mod = ofp_parser.OFPFlowMod(datapath=datapath, priority=priority,
                buffer_id=buffer_id,cookie=cookie,
                match=match, idle_timeout=idle_timeout,
                hard_timeout=hard_timeout, instructions=inst,
                flags=ofp.OFPFF_SEND_FLOW_REM)

        datapath.send_msg(mod)

        match_str = ofctl_v1_3.match_to_str(match),
        self.switch_flows[datapath.id].append({'cookie':cookie,
                                               'match':match_str,
                                               'actions':actions,
                                               'priority':priority})

        LOG.debug("Flow inserted to switch %x: cookie=%s, match=%s, actions=%s, priority=%d",
                                  datapath.id, str(cookie), match_str, str(actions), priority)
    def flow_removed_handler(self, ev):
        msg = ev.msg
        dpid = msg.datapath.id
        cookie = msg.cookie
        match_str = ofctl_v1_3.match_to_str(msg.match)
        index_to_delete = None

        # Ensure that the flow removed is for a known switch
        if dpid not in self.switch_flows:
            return

        for index, flow in enumerate(self.switch_flows[dpid]):
            if flow['cookie'] == cookie:
                index_to_delete = index
                break

        if index_to_delete is not None:
            del self.switch_flows[dpid][index_to_delete]
            LOG.debug("Flow removed on switch %d: match=%s, cookie=%s", dpid,
                      match_str, cookie)
예제 #3
0
    def flow_removed_handler(self, ev):
        msg = ev.msg
        dpid = msg.datapath.id
        cookie = msg.cookie
        match_str = ofctl_v1_3.match_to_str(msg.match)
        index_to_delete = None

        # Ensure that the flow removed is for a known switch
        if dpid not in self.switch_flows:
            return

        for index, flow in enumerate(self.switch_flows[dpid]):
            if flow['cookie'] == cookie:
                index_to_delete = index
                break

        if index_to_delete is not None:
            del self.switch_flows[dpid][index_to_delete]
            LOG.debug("Flow removed on switch %d: match=%s, cookie=%s",
                    dpid, match_str, cookie)
예제 #4
0
 def flow_stats_reply_handler(self, ev):
     global get_data_in_progress
     global get_return_data
     flow_rules = []
     for stats in ev.msg.body:
         actions = ofctl_v1_3.actions_to_str(stats.instructions)
         match = ofctl_v1_3.match_to_str(stats.match)
         if not actions:
             actions = "DROP"
         elif "CONTROLLER" in actions[0]:
             actions = "CONTROLLER"
         else:
             actions = "ALLOW"
         if 'nw_proto' in match:
             protocol = self.inverse_proto(int(match['nw_proto']))
             match['nw_proto'] = protocol
         match["actions"] = actions
         if "dl_type" in match:
             del match["dl_type"]
         #match.update(actions)
         flow_rules.append(match)
     get_return_data = list(flow_rules)
     get_data_in_progress = False
예제 #5
0
    def stats_reply_handler(self, ev):
        LOG.debug("Réception de réponse aux statistiques de flow")
        flows = []
        for stats in ev.msg.body:
            actions = ofctl_v1_3.actions_to_str(stats.instructions)
            match = ofctl_v1_3.match_to_str(stats.match)

            s = {'priority': stats.priority,
                 'cookie': stats.cookie,
                 'idle_timeout': stats.idle_timeout,
                 'hard_timeout': stats.hard_timeout,
                 'actions': actions,
                 'match': match,
                 'byte_count': stats.byte_count,
                 'duration_sec': stats.duration_sec,
                 'duration_nsec': stats.duration_nsec,
                 'packet_count': stats.packet_count,
                 'table_id': stats.table_id,
                 'length': stats.length,
                 'flags': stats.flags}
            if str(s["table_id"]) in self.CONF.send_stats.table_id:
                flows.append(s)

        flows = {str(ev.msg.datapath.id): flows}
        LOG.debug("Envoi des stats sur les serveurs")
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        content = json.dumps(flows)
        for server in self.servers:
            try:
                if self.datetime is not None:
                    r = requests.post(server+"/stats/reply", data=content, headers=headers, params={"time": self.datetime.isoformat()})
                    if r.status_code != requests.codes.ok:
                        LOG.error("Erreur n° " + str(r.status_code) + " sur le serveur " + server)
            except requests.ConnectionError:
                LOG.error("Erreur de connexion au serveur " + server)
            except requests.Timeout:
                LOG.error("Timeout du serveur " + server)