def __init__ (self, switch=None, **kw): EventMixin.__init__(self) self.flow_table = FlowTable() self.switch = switch # a list of pending flow table entries : tuples (ADD|REMOVE, entry) self._pending = [] # a map of pending barriers barrier_xid-> ([entry1,entry2]) self._pending_barrier_to_ops = {} # a map of pending barriers per request entry -> (barrier_xid, time) self._pending_op_to_barrier = {} self.listenTo(switch)
def GenerateACL(): acl_rules = open(r'..\Data\acl3_4k_rules.txt') table = FlowTable() i = 3874 while True: r = acl_rules.readline().split() if not r: break table.add_entry( TableEntry(priority=i, cookie=0x1, match=ofp_match(dl_src=EthAddr("00:00:00:00:00:01"), nw_src=r[0], nw_dst=r[1]), actions=[ofp_action_output(port=5)])) #table.add_entry(TableEntry(priority=i, cookie=0x1, match=ofp_match(dl_src=EthAddr("00:00:00:00:00:01"),nw_src="53.45.14.183/32",nw_dst="18.184.25.126/32"), actions=[ofp_action_output(port=5)])) i = i - 1 return table
def table(): t = FlowTable() t.add_entry( TableEntry(priority=6, cookie=0x1, match=ofp_match(dl_src=EthAddr("00:00:00:00:00:01"), nw_src="1.2.3.4"), actions=[ofp_action_output(port=5)])) t.add_entry( TableEntry(priority=5, cookie=0x2, match=ofp_match(dl_src=EthAddr("00:00:00:00:00:02"), nw_src="1.2.3.0/24"), actions=[ofp_action_output(port=6)])) t.add_entry( TableEntry(priority=1, cookie=0x3, match=ofp_match(), actions=[])) return t
def __init__(self, dpid, name=None, ports=4, miss_send_len=128, max_buffers=100, max_entries=0x7fFFffFF, features=None): """ Initialize switch - ports is a list of ofp_phy_ports or a number of ports - miss_send_len is number of bytes to send to controller on table miss - max_buffers is number of buffered packets to store - max_entries is max flows entries per table """ if name is None: name = dpid_to_str(dpid) self.name = name if isinstance(ports, int): ports = _generate_ports(num_ports=ports, dpid=dpid) self.dpid = dpid self.max_buffers = max_buffers self.max_entries = max_entries self.miss_send_len = miss_send_len self._has_sent_hello = False self.table = FlowTable() self.table.addListeners(self) self._lookup_count = 0 self._matched_count = 0 self.log = logging.getLogger(self.name) self._connection = None # buffer for packets during packet_in self._packet_buffer = [] # Map port_no -> openflow.pylibopenflow_01.ofp_phy_ports self.ports = {} self.port_stats = {} for port in ports: self.ports[port.port_no] = port self.port_stats[port.port_no] = ofp_port_stats( port_no=port.port_no) if features is not None: self.features = features else: # Set up default features self.features = SwitchFeatures() self.features.cap_flow_stats = True self.features.cap_table_stats = True self.features.cap_port_stats = True #self.features.cap_stp = True #self.features.cap_ip_reasm = True #self.features.cap_queue_stats = True #self.features.cap_arp_match_ip = True self.features.act_output = True self.features.act_enqueue = True self.features.act_strip_vlan = True self.features.act_set_vlan_vid = True self.features.act_set_vlan_pcp = True self.features.act_set_dl_dst = True self.features.act_set_dl_src = True self.features.act_set_nw_dst = True self.features.act_set_nw_src = True self.features.act_set_nw_tos = True self.features.act_set_tp_dst = True self.features.act_set_tp_src = True #self.features.act_vendor = True # Set up handlers for incoming OpenFlow messages # That is, self.ofp_handlers[OFPT_FOO] = self._rx_foo self.ofp_handlers = {} for value, name in ofp_type_map.iteritems(): name = name.split("OFPT_", 1)[-1].lower() h = getattr(self, "_rx_" + name, None) if not h: continue assert of._message_type_to_class[value]._from_controller, name self.ofp_handlers[value] = h # Set up handlers for actions # That is, self.action_handlers[OFPAT_FOO] = self._action_foo #TODO: Refactor this with above self.action_handlers = {} for value, name in ofp_action_type_map.iteritems(): name = name.split("OFPAT_", 1)[-1].lower() h = getattr(self, "_action_" + name, None) if not h: continue if getattr(self.features, "act_" + name) is False: continue self.action_handlers[value] = h # Set up handlers for stats handlers # That is, self.stats_handlers[OFPST_FOO] = self._stats_foo #TODO: Refactor this with above self.stats_handlers = {} for value, name in ofp_stats_type_map.iteritems(): name = name.split("OFPST_", 1)[-1].lower() h = getattr(self, "_stats_" + name, None) if not h: continue self.stats_handlers[value] = h # Set up handlers for flow mod handlers # That is, self.flow_mod_handlers[OFPFC_FOO] = self._flow_mod_foo #TODO: Refactor this with above self.flow_mod_handlers = {} for name, value in ofp_flow_mod_command_rev_map.iteritems(): name = name.split("OFPFC_", 1)[-1].lower() h = getattr(self, "_flow_mod_" + name, None) if not h: continue self.flow_mod_handlers[value] = h
def decode_flow_table(self, json): ft = FlowTable() for e in json["entries"]: ft.add_entry(self.decode_entry(e)) return ft