def install_forward(self, datapath): match = ofparser.OFPMatch() actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=10, match=match, instructions=inst) datapath.send_msg(mod)
def add_flow(self, datapath, table_id, priority, match, actions): global inc inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=table_id, priority=priority, match=match, instructions=inst, hard_timeout=3) datapath.send_msg(mod) print "load balancer flow added"
def add_flow(self, datapath, table_id, priority, match, actions): if len(actions) > 0: inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] else: inst = [] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=table_id, priority=priority, match=match, instructions=inst) datapath.send_msg(mod)
def unknown_flow(self, ev): switch = ev.msg.datapath data = ev.msg.data parsed_data = packet.Packet(data) in_port = ev.msg.match['in_port'] first_eth = parsed_data.get_protocols(eth.ethernet)[0] self.learn(switch, in_port, parsed_data) #print "PAcket in --->",parsed_data # Send where? if first_eth.dst in self.mac_tables[switch.id]: out_port = self.mac_tables[switch.id][first_eth.dst] else: out_port = of13.OFPP_FLOOD # Flow definition match = parser13.OFPMatch(eth_dst=first_eth.dst) actions = [parser13.OFPActionOutput(out_port)] instr = [ parser13.OFPInstructionActions(of13.OFPIT_APPLY_ACTIONS, actions) ] # Do we use BID? if ev.msg.buffer_id == of13.OFP_NO_BUFFER: # We got no buffer_id # ..so we send actual data self.send_packet_out(switch, out_port=out_port, data=data, actions=actions, in_port=in_port) if out_port != of13.OFPP_FLOOD: # ...and install a rule if it isn't a FLOOD self.send_new_flow(switch, match=match, instr=instr) else: # We have buffer id if out_port != of13.OFPP_FLOOD: # ...and are not FLOODing # so we install a rule and send packet out in one line self.send_new_flow(switch, match=match, instr=instr, buffer_id=ev.msg.buffer_id) else: # or just send a packet out, without a rule self.send_packet_out(switch, out_port=out_port, buffer_id=ev.msg.buffer_id, actions=actions, in_port=in_port)
def send_flow(self, path, prio, mtch, acts, buid=None): cmd = [parser.OFPInstructionActions(proto.OFPIT_APPLY_ACTIONS, acts)] if buid: mod = parser.OFPFlowMod(datapath=path, priority=prio, match=mtch, instructions=cmd, buffer_id=buid) else: mod = parser.OFPFlowMod(datapath=path, priority=prio, match=mtch, instructions=cmd) path.send_msg(mod)
def unknown_switch(self, ev): switch = ev.msg.datapath #ADD the Switch self.switches[ switch. id] = switch #Storing datapath corresponding to datapath.id print self.switches # Build a default rule actions_controller = [parser13.OFPActionOutput(of13.OFPP_CONTROLLER)] instr = [ parser13.OFPInstructionActions(of13.OFPIT_APPLY_ACTIONS, actions_controller) ] # Send it self.send_new_flow(switch=switch, instr=instr, priority=0, timeout=0)
def add_flow1(self, datapath, table_id, priority, match, actions): inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=table_id, priority=priority, hard_timeout=0, match=match, instructions=inst) #print mod datapath.send_msg(mod) #print datapath print "normal flow added"
def _get_ue_ul_of_rule(self, ue, path, anchor): """ Write the OpenFlow rules along the computed routing path """ ofrs = {} try: # Forwarding based on destination MAC unicast addr in_port = [ self.switches[path[0].link.src.dpid].ap_conf.port.port_no ] for p in path: for inp in in_port: out_port = p.link.src.port_no if inp == out_port: out_port = ofproto.OFPP_IN_PORT match = ofp_parser.OFPMatch( in_port=inp, eth_type=ether.ETH_TYPE_IPV6, ipv6_src=(':'.join(anchor.nw_prefix), ipv6_utils.ipv6_mask_from_cidr( anchor.nw_prefix_len))) actions = [ ofp_parser.OFPActionSetField( eth_src=self.switches[p.link.src.dpid].hw_addr), ofp_parser.OFPActionSetField( eth_dst=self.switches[p.link.dst.dpid].hw_addr), ofp_parser.OFPActionOutput(out_port) ] instructions = [ ofp_parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions) ] ofr = OFRule(self.switches[p.link.src.dpid], match, actions, instructions, table_id=OF_TABLE_UES) ofrs[ofr.key] = ofr in_port = [p.link.dst.port_no] except IndexError: self.logger.debug("Index error in _get_ue_ul_of_rule") return ofrs
def set_flow(self, datapath, match, actions, priority=0, hard_timeout=600, idle_timeout=60): inst = [ parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] flowmod = parser.OFPFlowMod(datapath, match=match, instructions=inst, priority=priority, hard_timeout=hard_timeout, idle_timeout=idle_timeout) datapath.send_msg(flowmod)
def switch_features_handler(self, event): msg = event.msg datapath = msg.datapath LOG.info("Configuring switch %d..." % datapath.id) """ Set table as stateful """ req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=0, stateful=1) datapath.send_msg(req) """ Set lookup extractor = {ip_src} """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC], table_id=0) datapath.send_msg(req) """ Set update extractor = {ip_src} (same as lookup) """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC], table_id=0) datapath.send_msg(req) """ Table 0 """ """ ARP packets flooding """ match = ofparser.OFPMatch(eth_type=0x0806) actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] self.add_flow(datapath=datapath, table_id=0, priority=10, match=match, actions=actions) """ Drop IP-dst Broadcast """ match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst="255.255.255.255") actions = [] self.add_flow(datapath=datapath, table_id=0, priority=10, match=match, actions=actions) match = ofparser.OFPMatch(eth_type=0x0800) actions = [bebaparser.OFPExpActionIncState(table_id=0)] inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=0, match=match, instructions=inst) datapath.send_msg(mod)
def _get_neigh_of_rule(self, node): """ Write the OpenFlow rule for forwarding the packet to the node (ipv6, hw_addr) lookup and eth_dst address translation """ ofrs = {} match = ofp_parser.OFPMatch(eth_type = ether.ETH_TYPE_IPV6, ipv6_dst = node.ipv6_addr) actions = [ofp_parser.OFPActionSetField(eth_src = node.attachment.switch.hw_addr), ofp_parser.OFPActionSetField(eth_dst = node.hw_addr), ofp_parser.OFPActionOutput(node.attachment.port.port_no)] instructions = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)] ofr = OFRule(node.attachment.switch, match, actions, instructions, table_id = OF_TABLE_NEIGH) ofrs[ofr.key] = ofr return ofrs
def _send_flow_mod(self, obj): inst_type = False table_id = 0 flow_mod = obj.get("OFPFlowMod") datapath = self.dpstore.get(flow_mod.get("datapath_id")) datapath = datapath.get("dp_obj") instructions = flow_mod.get("instructions") # FIXME: put this in a instruction parser for instruction in instructions: if "OFPInstructionActions" in instruction: inst_type = "OFPInstructionActions" actions = self._parse_action( instruction.get("OFPInstructionActions").get("actions")) if "OFPInstructionGotoTable" in instruction: inst_type = "OFPInstructionGotoTable" table_id = instruction.get("OFPInstructionGotoTable").get( "table_id") goto = ofproto_v1_3_parser.OFPInstructionGotoTable(table_id) match = flow_mod.get("match") match = self._pars_match(match) if inst_type == "OFPInstructionActions": inst = [ ofproto_v1_3_parser.OFPInstructionActions( ofproto_v1_3.OFPIT_APPLY_ACTIONS, actions) ] if inst_type == "OFPInstructionGotoTable": inst = [goto] if int(flow_mod.get("table_id")) >= 0: table_id = int(flow_mod.get("table_id")) mod = ofproto_v1_3_parser.OFPFlowMod(datapath=datapath, priority=int( flow_mod.get("priority")), match=match, instructions=inst, table_id=table_id) datapath.send_msg(mod)
def install_flow(self, dp, port, ip, priority, match_field): ofproto = dp.ofproto parser = dp.ofproto_parser match = parser.OFPMatch(**{ 'eth_type' : ether_types.ETH_TYPE_IP, match_field : ip} ) action = parser.OFPActionOutput(port = port, max_len = 65535) # Flow rule's encapsulation # The OpenFlow message we send to the switch to install a flow msg = parser.OFPFlowMod( dp, match = match, # Here we embed the action in the instruction instructions = [parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, [action] )], priority = priority
def _write_ap_rules(self, switch): """ Initialiaze APs OpenFlow rules """ # Send Router Solicitations to the Controller match = ofp_parser.OFPMatch(in_port=switch.ap_conf.port.port_no, eth_type=ether.ETH_TYPE_IPV6, ip_proto=inet.IPPROTO_ICMPV6, icmpv6_type=icmpv6.ND_ROUTER_SOLICIT) actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER)] instructions = [ ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] req = EventWriteOFRule( OFRule(switch, match, actions, instructions, table_id=OF_TABLE_NEIGH)) self.send_event(req.dst, req)
def add_flow(self, datapath, priority, match, actions): inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, cookie=0, cookie_mask=0, table_id=0, command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0, priority=priority, buffer_id=ofproto.OFP_NO_BUFFER, out_port=ofproto.OFPP_ANY, out_group=ofproto.OFPG_ANY, flags=0, match=match, instructions=inst) datapath.send_msg(mod)
def program_flow(self, dp, match, actions, priority = 0, hard_timeout = 600, idle_timeout = 60 ): """ Programs a new flow into a switch. Programming a new flow with the exact same match of an existing one will replace the existing flow. """ flowmod = parser.OFPFlowMod( dp, match = match, instructions = [ parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions ) ], priority = priority, hard_timeout = hard_timeout, idle_timeout = idle_timeout ) dp.send_msg(flowmod)
def _write_gw_rules(self, switch): """ Initialiaze Gateway OpenFlow rules """ # Send unkown IP dst packets to the controller match = ofp_parser.OFPMatch( eth_dst=switch.hw_addr, eth_type=ether.ETH_TYPE_IPV6, ipv6_src=(':'.join(switch.gw_conf.nw_prefix), ipv6_utils.ipv6_mask_from_cidr( switch.gw_conf.nw_prefix_len))) actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER)] instructions = [ ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] req = EventWriteOFRule( OFRule(switch, match, actions, instructions, table_id=OF_TABLE_UNKNOWN)) self.send_event(req.dst, req)
def add_flow(self, datapath, table_id, priority, match, actions): """ Add flow to the BEBA switch Parameters: - datapath = datapath to use - table_id = table_id to use - priority = priority of the rule, the rule with highest priority is selected - match = structure for description of match rules - actions = list of required actions """ if len(actions) > 0: inst = [ ofparser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions) ] else: inst = [] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=table_id, priority=priority, match=match, instructions=inst) datapath.send_msg(mod)
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] pkt_tcp = pkt.get_protocol(tcp.tcp) ip = pkt.get_protocol(ipv4.ipv4) if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return dst = eth.dst src = eth.src dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) # learn a mac address to avoid FLOOD next time. Private_key = 0 table_id = 1 #l = [257,260] last_item = 4 # we have to assign the correct user ports self.mac_to_port[dpid][src] = in_port if dst in self.mac_to_port[dpid]: out_port = self.mac_to_port[dpid][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] priority = 1 if ip and dpid in self.Edgeswitch and in_port in range( 2, last_item + 1): if (dpid, src, ip.src, in_port) in self.Hostinfo: if src in self.Key.keys(): file = open("/home/abdullah/ryu/Key.txt", "r") contents = file.read() dictionary = ast.literal_eval(contents) file.close() self.Key = dictionary if self.Key[src][1] != 0: # I have to waite the user answer t = self.Key[src] lst = list(t) lst[3] = lst[1]**lst[2] % 21841 #private key t = tuple(lst) self.Key[src] = t Private_key = lst[3] else: return print ip.src, "is an authenticated user and its location", dpid, in_port else: return if msg.table_id == 0 and pkt_tcp.bits == 2: if Private_key != 0: match = parser.OFPMatch(in_port=in_port, eth_src=src, eth_type=0x8847, mpls_label=Private_key) actions = [ parser.OFPActionPopMpls(), parser.OFPActionOutput(out_port) ] inst = [ parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = parser.OFPFlowMod(datapath=datapath, table_id=0, priority=10, match=match, instructions=inst) datapath.send_msg(mod) self.Flowcounter[dpid] += 1 actions = [] match = parser.OFPMatch(in_port=in_port, eth_type=0x0800, ip_proto=6, tcp_flags=2) self.add_flow(datapath, 5, match, actions, table_id=0) return else: match = parser.OFPMatch(in_port=in_port, eth_type=0x0800, ip_proto=6, tcp_flags=2) actions = [parser.OFPActionOutput(out_port)] inst = [ parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = parser.OFPFlowMod(datapath=datapath, table_id=0, priority=10, match=match, instructions=inst) datapath.send_msg(mod) self.Flowcounter[dpid] += 1 print pkt return elif dpid in self.Edgeswitch: self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) table_id = 1 else: self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) table_id = 0 # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: if Private_key != 0 and dpid in self.Edgeswitch: match = parser.OFPMatch(in_port=in_port, eth_src=src) actions = [] self.add_flow(datapath, 5, match, actions, table_id=1) match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src, eth_type=0x8847, mpls_label=Private_key) priority = 10 table_id = 1 actions = [ parser.OFPActionPopMpls(), parser.OFPActionOutput(out_port) ] else: match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & pa enumerate(cket_out if msg.buffer_id != ofproto.OFP_NO_BUFFER: self.add_flow(datapath, priority, match, actions, msg.buffer_id, table_id=table_id) return else: self.add_flow(datapath, priority, match, actions, table_id=table_id) else: pass data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
parser.OFPInstructionGotoTable(2) ] ), parser.OFPFlowStats( table_id=1, priority=0, match=parser.OFPMatch(), instructions=[] ), # Table 2 parser.OFPFlowStats( table_id=2, priority=10, match=parser.OFPMatch(ipv4_dst=1, tcp_dst=81), instructions=[ parser.OFPInstructionActions(ofproto_v1_3.OFPIT_WRITE_ACTIONS, actions=[parser.OFPActionOutput(1)]) ] ), parser.OFPFlowStats( table_id=2, priority=10, match=parser.OFPMatch(ipv4_dst=2, tcp_dst=80), instructions=[ parser.OFPInstructionActions(ofproto_v1_3.OFPIT_WRITE_ACTIONS, actions=[parser.OFPActionOutput(2)]) ] ), parser.OFPFlowStats( table_id=2, priority=0, match=parser.OFPMatch(),
def function_dynamic_nat(self, datapath): """ Set table 1 as stateful """ req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=1, stateful=1) datapath.send_msg(req) """ Set table 2 restore """ req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=2, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################################### """ Tab1 """ """ Set lookup extractor = {OXM_OF_METADATA} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_METADATA], table_id=1) datapath.send_msg(req) """ Set update extractor = {OXM_OF_METADATA} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_METADATA], table_id=1) datapath.send_msg(req) """ Tab2 """ """ Set lookup extractor = {OXM_OF_IPV4_SRC, OXM_OF_IP_PROTO, OXM_OF_TCP_SRC} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IP_PROTO, ofproto.OXM_OF_TCP_SRC ], table_id=2) datapath.send_msg(req) """ Set lookup extractor = {OXM_OF_IPV4_DST, OXM_OF_IP_PROTO, OXM_OF_TCP_DST} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ ofproto.OXM_OF_IPV4_DST, ofproto.OXM_OF_IP_PROTO, ofproto.OXM_OF_TCP_DST ], table_id=2) datapath.send_msg(req) """ Tab3 """ ########################### SET STATE TABLE 1 ############################################ for stateVal in range(1, 21): state = bebaparser.OFPExpMsgSetFlowState( datapath=datapath, state=2000 + stateVal, keys=[stateVal, 0, 0, 0, 0, 0, 0, 0], table_id=1) datapath.send_msg(state) ########################### SET HF DATA VARIABLE TAB 1 ############################################ ''' GD[0] = state_label''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=1, global_data_variable_id=0, value=0) datapath.send_msg(req) ''' HF[0] = OXM_EXP_STATE [state_label] ''' req = bebaparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=1, extractor_id=0, field=bebaproto.OXM_EXP_STATE) datapath.send_msg(req) ########################### SET HF DATA VARIABLE TAB 2 ############################################ ''' HF[0] = OXM_OF_IPV4_SRC [id_pkt] ''' req = bebaparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=2, extractor_id=0, field=ofproto.OXM_OF_IPV4_SRC) datapath.send_msg(req) ''' HF[1] = OXM_OF_TCP_SRC [id_pkt] ''' req = bebaparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=2, extractor_id=1, field=ofproto.OXM_OF_TCP_SRC) datapath.send_msg(req) ########################### SET HF DATA VARIABLE TAB 3 ############################################ ''' HF[0] = OXM_OF_METADATA [metadata] ''' req = bebaparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=3, extractor_id=0, field=ofproto.OXM_OF_METADATA) datapath.send_msg(req) ################################# REGOLE ############################################ ''' ####################### TAB 0 ''' match = ofparser.OFPMatch(state=0, in_port=LAN_PORT) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=0), bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_SUM, output_gd_id=1, operand_1_gd_id=1, operand_2_cost=1), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=1, dst_field=ofproto.OXM_OF_METADATA) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(1) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=89, match=match, instructions=inst) datapath.send_msg(mod) # Line 12 match = ofparser.OFPMatch(state=1, in_port=LAN_PORT) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=88, match=match, instructions=inst) datapath.send_msg(mod) # Line 13 match = ofparser.OFPMatch(state=0, in_port=INTERNET_PORT, eth_type=0x0800, ipv4_dst='1.0.0.1') actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(2) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=87, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 1 ''' # Line 0 # HF[0] = state_label # GD[0] = state_label + 0 => GD[0] + HF[0] # state_label -> metadata => GD[0] = HF[0] match = ofparser.OFPMatch(in_port=3) actions = [ bebaparser.OFPExpActionSetDataVariable(table_id=1, opcode=bebaproto.OPCODE_SUM, output_gd_id=0, operand_1_hf_id=0, operand_2_cost=0), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_METADATA) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(2) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, priority=100, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 2 restore''' # Line 0 # ip.src -> R0 => HF[0] -> FD[0] => FD[0] = HF[0] + 0 # tcp.src -> R1 => HF[1] -> FD[1] => FD[1] = HF[1] + 0 match = ofparser.OFPMatch(state=0, in_port=LAN_PORT) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=2), bebaparser.OFPExpActionSetDataVariable(table_id=2, opcode=bebaproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=0, operand_2_cost=0), bebaparser.OFPExpActionSetDataVariable(table_id=2, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_hf_id=1, operand_2_cost=0) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=2, priority=100, match=match, instructions=inst) datapath.send_msg(mod) # Line 1 # ip.dst = R0 => IPV4_DST = FD[0] # tcp.dst = R1 => TCP_DST = FD[1] match = ofparser.OFPMatch(state=1, in_port=INTERNET_PORT) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=2), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_IPV4_DST), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=1, dst_field=ofproto.OXM_OF_TCP_DST) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=2, priority=99, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 3 translate ''' # Line 3 # metadata(b16,b31) -> R1 => metadata(b16,b31) -> FD[1] => FD[1] = HF[0] # ip.src = 10.0.0.1 # tcp.src = R1 => TCP_SRC = FD[1] match = ofparser.OFPMatch(state=0, in_port=LAN_PORT, eth_type=0x0800) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=3), bebaparser.OFPExpActionSetDataVariable(table_id=3, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_hf_id=0, operand_2_cost=0), ofparser.OFPActionSetField(ipv4_src="1.0.0.1"), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=1, dst_field=ofproto.OXM_OF_TCP_SRC) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=97, match=match, instructions=inst) datapath.send_msg(mod) # Line 4 # ip.src = 10.0.0.1 # tcp.src = R1 => TCP_SRC = FD[1] match = ofparser.OFPMatch(state=1, in_port=LAN_PORT, eth_type=0x0800) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=3), ofparser.OFPActionSetField(ipv4_src='1.0.0.1'), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=1, dst_field=ofproto.OXM_OF_TCP_SRC) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=96, match=match, instructions=inst) datapath.send_msg(mod)
def function_load_balancer(self, datapath): """ Tab0 """ """ Set lookup extractor = {BiFlow} """ # GIA CONFIGURATA """ Set table 3 translate """ req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=3, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################################### """ Tab3 """ """ Set lookup extractor = {OXM_OF_IPV4_SRC, OXM_OF_TCP_SRC} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_TCP_SRC], table_id=3) datapath.send_msg(req) """ Set update extractor = {OXM_OF_IPV4_SRC, OXM_OF_TCP_SRC} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_TCP_SRC], table_id=3) datapath.send_msg(req) ########################### SET GD E HF DATA VARIABLE TAB 3 ############################################ ''' GD[0] = 0''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=0, value=0) datapath.send_msg(req) ''' GD[1] = LAN_DUE 10.0.0.2 hexadecimal''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=1, value=0x0200000a) datapath.send_msg(req) ''' GD[2] = LAN_TRE 10.0.0.3 hexadecimal''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=2, value=0x0300000a) datapath.send_msg(req) ''' GD[3] = PORT 80 ''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=3, value=0x5000) datapath.send_msg(req) ################################# RULES ############################################ ''' ####################### TAB 0 ''' # Line 7 match = ofparser.OFPMatch(state=0, in_port=INTERNET_PORT, eth_type=0x0800, ipv4_dst='1.0.0.1', ip_proto=6, tcp_dst=80) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=0), bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_SUM, output_gd_id=0, operand_1_gd_id=0, operand_2_cost=1), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_METADATA) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=93, match=match, instructions=inst) datapath.send_msg(mod) # Line 8 match = ofparser.OFPMatch(state=1, in_port=INTERNET_PORT, eth_type=0x0800, ipv4_dst='1.0.0.1', ip_proto=6, tcp_dst=80) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=92, match=match, instructions=inst) datapath.send_msg(mod) # Line 9 match = ofparser.OFPMatch(state=0, in_port=LAN_PORT, eth_type=0x0800, ipv4_src='10.0.0.2', ip_proto=6, tcp_src=80) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=91, match=match, instructions=inst) datapath.send_msg(mod) # Line 10 match = ofparser.OFPMatch(state=0, in_port=LAN_PORT, eth_type=0x0800, ipv4_src='10.0.0.3', ip_proto=6, tcp_src=80) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=90, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 3 translate ''' # Line 0 # ip.dst = 10.0.0.2 # tcp.dst = 80 # 10.0.0.2 -> R0 => 10.0.0.2 -> FD[0] => FD[0] = GD[0] + 10.0.0.2 => 0 + 10.0.0.2 # 80 -> R1 => 80 -> FD[1] => GD[0] + 80 => 0 + 80 match = ofparser.OFPMatch(state=0, in_port=INTERNET_PORT, metadata=(0, 0x000000001), eth_type=0x0800, ip_proto=6) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=3), ofparser.OFPActionSetField(ipv4_dst='10.0.0.2'), ofparser.OFPActionSetField(tcp_dst=80), # tolto per test bebaparser.OFPExpActionSetDataVariable(table_id=3, opcode=bebaproto.OPCODE_SUM, output_fd_id=0, operand_1_gd_id=1, operand_2_cost=0), bebaparser.OFPExpActionSetDataVariable(table_id=3, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_gd_id=0, operand_2_cost=80) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=100, match=match, instructions=inst) datapath.send_msg(mod) # Line 1 # ip.dst = 10.0.0.3 # tcp.dst = 80 # 10.0.0.3 -> R0 => 10.0.0.3 -> FD[0] => FD[0] = GD[0] + 10.0.0.3 => 0 + 10.0.0.3 # 80 -> R1 => 80 -> FD[1] => GD[0] + 80 => 0 + 80 match = ofparser.OFPMatch(state=0, in_port=INTERNET_PORT, metadata=(1, 0x000000001), eth_type=0x0800, ip_proto=6) actions = [ bebaparser.OFPExpActionSetState(state=1, table_id=3), ofparser.OFPActionSetField(ipv4_dst='10.0.0.3'), ofparser.OFPActionSetField(tcp_dst=80), # tolto per test bebaparser.OFPExpActionSetDataVariable(table_id=3, opcode=bebaproto.OPCODE_SUM, output_fd_id=0, operand_1_gd_id=2, operand_2_cost=0), bebaparser.OFPExpActionSetDataVariable(table_id=3, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_gd_id=0, operand_2_cost=80) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=99, match=match, instructions=inst) datapath.send_msg(mod) # Line 2 # ip.dst = R0 => IPV4_DST = FD[0] # tcp.dst = R1 => TCP_DST = FD[1] match = ofparser.OFPMatch(state=1, in_port=INTERNET_PORT) actions = [ bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_IPV4_DST), bebaparser.OFPExpActionWriteContextToField( src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR, src_id=1, dst_field=ofproto.OXM_OF_TCP_DST) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=98, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 4 forward ''' # Line 4 match = ofparser.OFPMatch(in_port=LAN_PORT, eth_type=0x0800, ipv4_src='10.0.0.2', ip_proto=6, tcp_src=80) actions = [ ofparser.OFPActionSetField(ipv4_src='1.0.0.1'), ofparser.OFPActionSetField(tcp_src=80), ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"), ofparser.OFPActionOutput(INTERNET_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=96, match=match, actions=actions) # Line 5 match = ofparser.OFPMatch(in_port=LAN_PORT, eth_type=0x0800, ipv4_src='10.0.0.3', ip_proto=6, tcp_src=80) actions = [ ofparser.OFPActionSetField(ipv4_src='1.0.0.1'), ofparser.OFPActionSetField(tcp_src=80), ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"), ofparser.OFPActionOutput(INTERNET_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=95, match=match, actions=actions) # Line 6 match = ofparser.OFPMatch() actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"), ofparser.OFPActionOutput(INTERNET_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=94, match=match, actions=actions)
def function_lan_dmz_isolation(self, datapath): """ Set table 0 as stateful """ req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=0, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################################### """ Tab0 """ """ Set lookup extractor = {BiFlow} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST, ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST ], table_id=0, biflow=1) datapath.send_msg(req) """ Set lookup extractor = {BiFlow} """ req = bebaparser.OFPExpMsgKeyExtract( datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST, ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST ], table_id=0, biflow=1) datapath.send_msg(req) """ Tab4 """ """ Stateless """ ########################### SET GD DATA VARIABLE TAB 0 ############################################ ''' GD[0] = 0''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=0, value=0) datapath.send_msg(req) ''' GD[1] = 0 ''' req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=0, value=0) datapath.send_msg(req) ################################# REGOLE ############################################ match = ofparser.OFPMatch(eth_type=0x0806) actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=100, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 0 ''' # Line 0 match = ofparser.OFPMatch(state=0, in_port=DMZ_PORT, eth_type=0x0800, ipv4_dst=('10.0.0.0', '255.255.255.0')) actions = [bebaparser.OFPExpActionSetState(state=11, table_id=0)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=0, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=100, match=match, instructions=inst) datapath.send_msg(mod) # Line 1 match = ofparser.OFPMatch(state=0, in_port=LAN_PORT, eth_type=0x0800, ipv4_dst=('8.0.0.0', '255.255.255.0')) actions = [bebaparser.OFPExpActionSetState(state=12, table_id=0)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=0, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=99, match=match, instructions=inst) datapath.send_msg(mod) # Line 2 match = ofparser.OFPMatch(state=11, in_port=DMZ_PORT, eth_type=0x0800, ipv4_dst=('10.0.0.0', '255.255.255.0')) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=0, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=98, match=match, instructions=inst) datapath.send_msg(mod) # Line 3 match = ofparser.OFPMatch(state=11, in_port=LAN_PORT, eth_type=0x0800, ipv4_dst=('8.0.0.0', '255.255.255.0')) actions = [bebaparser.OFPExpActionSetState(state=2, table_id=0)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=1, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=97, match=match, instructions=inst) datapath.send_msg(mod) # Line 4 match = ofparser.OFPMatch(state=12, in_port=LAN_PORT, eth_type=0x0800, ipv4_dst=('8.0.0.0', '255.255.255.0')) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=0, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=96, match=match, instructions=inst) datapath.send_msg(mod) # Line 5 match = ofparser.OFPMatch(state=12, in_port=DMZ_PORT, eth_type=0x0800, ipv4_dst=('10.0.0.0', '255.255.255.0')) actions = [bebaparser.OFPExpActionSetState(state=2, table_id=0)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=1, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=95, match=match, instructions=inst) datapath.send_msg(mod) # Line 6 match = ofparser.OFPMatch(state=2) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=1, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=94, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 1 ''' # NOT USED IN THIS USE CASE ''' ####################### TAB 2 restore''' # NOT USED IN THIS USE CASE ''' ####################### TAB 3 translate ''' # NOT USED IN THIS USE CASE ''' ####################### TAB 4 forward ''' # Line 0 match = ofparser.OFPMatch(in_port=DMZ_PORT, metadata=(0, 0x00000000F), eth_type=0x0800, ipv4_dst=('10.0.0.0', '255.255.255.0')) actions = [] self.add_flow(datapath=datapath, table_id=4, priority=100, match=match, actions=actions) # Line 1 match = ofparser.OFPMatch(in_port=DMZ_PORT, metadata=(1, 0x00000000F), eth_type=0x0800, ipv4_dst='10.0.0.2') actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:03"), ofparser.OFPActionOutput(LAN_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=99, match=match, actions=actions) # Line 1 BIS match = ofparser.OFPMatch(in_port=DMZ_PORT, metadata=(1, 0x00000000F), eth_type=0x0800, ipv4_dst='10.0.0.3') actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:04"), ofparser.OFPActionOutput(LAN_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=99, match=match, actions=actions) # Line 2 match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='8.0.0.2') actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:02"), ofparser.OFPActionOutput(DMZ_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=98, match=match, actions=actions) # Line 3 match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='10.0.0.2') actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:03"), ofparser.OFPActionOutput(LAN_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=97, match=match, actions=actions) # Line 3 BIS match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='10.0.0.3') actions = [ ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:04"), ofparser.OFPActionOutput(LAN_PORT) ] self.add_flow(datapath=datapath, table_id=4, priority=97, match=match, actions=actions)
def install_transport_bad(self, datapath): """ Set table 0 as stateful solo per usare GD, fa le somme del percorso """ req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=0, stateful=1) datapath.send_msg(req) """ Ci sta una table NULLA per i bug di OpenFLow con le Label MPLS """ """ Set table 2 as stateful, verifica le condizioni sul percorso <=""" req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=2, stateful=1) datapath.send_msg(req) """ Set table 3 as stateful """ req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=3, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################ """ Tab0 """ """ Non mi interessa """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=0) datapath.send_msg(req) """ Non mi interessa """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=0) datapath.send_msg(req) """ Tab2 """ """ Set lookup extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=2) datapath.send_msg(req) """ Set update extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=2) datapath.send_msg(req) """ Tab3 """ """ Set lookup extractor = {MAC_DST} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_DST], table_id=3) datapath.send_msg(req) """ Set update extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=3) datapath.send_msg(req) ########################### SET HF GD DATA VARIABLE TAB 0 ############################################ ''' HF[1] = OXM_OF_MPLS_TC [pesoArchi] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=0, extractor_id=1, field=ofproto.OXM_OF_MPLS_TC) datapath.send_msg(req) ''' GD[0] = 0 ''' req = osparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=0, value=0) datapath.send_msg(req) ########################### SET HF GD DATA VARIABLE TAB 2 ############################################ ''' HF[1] = OXM_OF_MPLS_TC [pesoArchi] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=2, extractor_id=1, field=ofproto.OXM_OF_MPLS_TC) datapath.send_msg(req) ########################### SET CONDITION TAB 2 ############################################ # condition 3: MPLS_TC <= COSTO MEMORIZZATO (FD[0]) ? # condition 3: HF[1] <= FD[0] ? req = osparser.OFPExpMsgSetCondition(datapath=datapath, table_id=2, condition_id=0, condition=osproto.CONDITION_LTE, operand_1_hf_id=1, operand_2_fd_id=0) datapath.send_msg(req) '''####################### TAB 0 ''' ''' somma il costo del link di ingresso al valore memorizzato nel pacchetto mpls_tc + 1 ''' """ Riga 1 """ # GD[0] = HF[1] + 1 -> MPLS_TC + 1 # HF [1] = GD[0] -> MPLS_TC = GD[0] # GOTO Tab 2 match = ofparser.OFPMatch(eth_type=0x8847) actions = [ osparser.OFPExpActionSetDataVariable(table_id=0, opcode=osproto.OPCODE_SUM, output_gd_id=0, operand_1_hf_id=1, operand_2_cost=2), osparser.OFPExpActionWriteContextToField( src_type=osproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_MPLS_TC) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(1) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=1198, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 1 NULLA serve solo per i bug di OpenFlow, servono 2 stage xke le modifiche MPLS siano visibili''' # Non fa niente, ci sta solo per risolvere bug (presunti) di OpenFlow match = ofparser.OFPMatch(eth_type=0x8847) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(2) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, priority=0, match=match, instructions=inst) datapath.send_msg(mod) '''# ####################### TAB 2 ''' ''' C[0] verifica se il costo memorizzato nel pacchetto e' <= di quello gia conosciuto (in pratica se il pacchetto ha fatto un percorso migliore) ''' """ Riga 1 """ # C[0]: MPLS_TC > COSTO MEMORIZZATO -> HF[1] > FD[0] # MetaData: 1 -> Pacchetto duplicato # azione DROP match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=0) actions = [osparser.OFPExpActionSetState(state=1, table_id=2)] self.add_flow(datapath=datapath, table_id=2, priority=198, match=match, actions=actions) """ Riga 2 """ # C[0]: MPLS_TC <= COSTO MEMORIZZATO -> HF[1] <= FD[0] # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=1) actions = [ osparser.OFPExpActionSetState(state=1, table_id=2), osparser.OFPExpActionSetDataVariable(table_id=2, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=2, priority=98, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 3 """ # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=0, eth_type=0x8847) actions = [ osparser.OFPExpActionSetState(state=1, table_id=2), osparser.OFPExpActionSetDataVariable(table_id=2, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=2, priority=8, match=match, instructions=inst) datapath.send_msg(mod) '''# ####################### TAB 3 semplicemente MAC Learning ''' # for each input port, for each state for i in range(1, N + 1): for s in range(N + 1): match = ofparser.OFPMatch(in_port=i, state=s) if s == 0: out_port = ofproto.OFPP_FLOOD else: out_port = s # actions = [osparser.OFPExpActionSetState(state=i, table_id=3, hard_timeout=10), actions = [ osparser.OFPExpActionSetState(state=i, table_id=3), ofparser.OFPActionOutput(out_port) ] self.add_flow(datapath=datapath, table_id=3, priority=0, match=match, actions=actions)
def install_edge(self, datapath): """ Table 0 is stateless """ """ Set table 1 as stateful solo per usare GD""" req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=1, stateful=1) datapath.send_msg(req) """ Set table 3 as stateful """ req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=3, stateful=1) datapath.send_msg(req) """ Set table 4 as stateful """ req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=4, stateful=1) datapath.send_msg(req) """ Set table 5 as stateful """ req = osparser.OFPExpMsgConfigureStatefulTable(datapath=datapath, table_id=5, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################ """ Tab1 """ """ Non mi interessa """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=1) datapath.send_msg(req) """ Non mi interessa """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=1) datapath.send_msg(req) """ Tab3 """ """ Set lookup extractor = {MPLS_label} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_MPLS_LABEL], table_id=3) datapath.send_msg(req) """ Set update extractor = {MPLS_label} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_MPLS_LABEL], table_id=3) datapath.send_msg(req) """ Tab4 """ """ Set lookup extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=4) datapath.send_msg(req) """ Set update extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=4) datapath.send_msg(req) """ Tab5 """ """ Set lookup extractor = {MAC_DST} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_DST], table_id=5) datapath.send_msg(req) """ Set update extractor = {MAC_SRC} """ req = osparser.OFPExpMsgKeyExtract( datapath=datapath, command=osproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=5) datapath.send_msg(req) ########################### SET HF GD DATA VARIABLE TAB 1 ############################################ ''' GD[0] = datapath.id<<6 + numero crescente''' req = osparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=1, global_data_variable_id=0, value=(datapath.id << 6) + 1) datapath.send_msg(req) ########################### SET HF GD DATA VARIABLE TAB 3 ############################################ ''' HF[0] = OXM_OF_MPLS_LABEL [id_pkt] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=3, extractor_id=0, field=ofproto.OXM_OF_MPLS_LABEL) datapath.send_msg(req) ''' HF[1] = OXM_OF_MPLS_TC [pesoArchi] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=3, extractor_id=1, field=ofproto.OXM_OF_MPLS_TC) datapath.send_msg(req) ''' GD[0] = 0 ''' req = osparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=0, value=0) datapath.send_msg(req) ''' GD[1] = datapath.id ''' req = osparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=3, global_data_variable_id=1, value=0) datapath.send_msg(req) ########################### SET HF GD DATA VARIABLE TAB 4 ############################################ ''' HF[0] = OXM_OF_MPLS_LABEL [id_pkt] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=4, extractor_id=0, field=ofproto.OXM_OF_MPLS_LABEL) datapath.send_msg(req) ''' HF[1] = OXM_OF_MPLS_TC [pesoArchi] ''' req = osparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=4, extractor_id=1, field=ofproto.OXM_OF_MPLS_TC) datapath.send_msg(req) ''' GD[0] = datapath.id ''' req = osparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=4, global_data_variable_id=0, value=1) datapath.send_msg(req) ########################### SET CONDITION TAB 4 ############################################ # condition 0: MPLS_TC <= COSTO MEMORIZZATO (FD[0]) ? # condition 0: HF[1] <= FD[0] ? req = osparser.OFPExpMsgSetCondition(datapath=datapath, table_id=4, condition_id=0, condition=osproto.CONDITION_LTE, operand_1_hf_id=1, operand_2_fd_id=0) datapath.send_msg(req) # condition 1: MPLS_TC <= 1 --> ovvero e il primo hop ? # condition 1: HF[1] <= GD[0] ? req = osparser.OFPExpMsgSetCondition(datapath=datapath, table_id=4, condition_id=1, condition=osproto.CONDITION_LTE, operand_1_hf_id=1, operand_2_gd_id=0) datapath.send_msg(req) ''' ####################### TAB 0 PushLabelMPLS ''' # Se il pacchetto proviene da una porta host (HOST_PORT) push label mpls e GOTO Tab 1 match = ofparser.OFPMatch(in_port=HOST_PORT) actions = [ofparser.OFPActionPushMpls()] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(1) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=8, match=match, instructions=inst) datapath.send_msg(mod) # Se proviene da un'altra porta ha gia la label MPLS GOTO Tab 1 giusto per dirlo esplicitamente perche ci andrebbe da solo CREDO NON DEVO FARE LA PushMpls match = ofparser.OFPMatch() actions = [] #se proviene da un altra porta ha gia la label MPLS inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(1) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=0, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 1 marca il pkt con ID_PKT ''' # Setta la label_mpls se non e gia stata configurata con il valore GD[0] + 1 -> (id_switch << 6) + 1 match = ofparser.OFPMatch(eth_type=0x8847, mpls_label=0) actions = [ osparser.OFPExpActionWriteContextToField( src_type=osproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_MPLS_LABEL), osparser.OFPExpActionSetDataVariable(table_id=1, opcode=osproto.OPCODE_SUM, output_gd_id=0, operand_1_gd_id=0, operand_2_cost=1) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(2) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, priority=8, match=match, instructions=inst) datapath.send_msg(mod) match = ofparser.OFPMatch(eth_type=0x8847) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(2) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, priority=0, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 2 NULLA serve solo per i bug di OpenFlow, servono 2 stage xke le modifiche MPLS siano visibili''' # Non fa niente, ci sta solo per risolvere bug (presunti) di OpenFlow match = ofparser.OFPMatch(eth_type=0x8847) actions = [] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(3) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=2, priority=0, match=match, instructions=inst) datapath.send_msg(mod) '''####################### TAB 3 Verifica i duplicati, lo stato e' dato da mpls_label''' ''' somma il costo del link di ingresso al valore memorizzato nel pacchetto mpls_tc + 1 ''' ''' scrive il campo metada = 1 se e' un pacchetto duplicato ovvero nello stato 1 ''' """ Riga 1 """ # GD[0] = HF[1] + 1 -> MPLS_TC + 1 # HF [1] = GD[0] -> MPLS_TC = GD[0] # WriteMetadata = 1 -> pacchetto duplicato # SetState(1) # GOTO Tab 2 match = ofparser.OFPMatch(state=1, eth_type=0x8847) actions = [ osparser.OFPExpActionSetDataVariable(table_id=3, opcode=osproto.OPCODE_SUM, output_gd_id=0, operand_1_hf_id=1, operand_2_cost=1), osparser.OFPExpActionWriteContextToField( src_type=osproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_MPLS_TC), # osparser.OFPExpActionSetState(state=1, table_id=3, idle_timeout=15)] osparser.OFPExpActionSetState(state=1, table_id=3) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=1, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=1198, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 2 """ # GD[0] = HF[1] + 1 -> MPLS_TC + 1 # HF [1] = GD[0] -> MPLS_TC = GD[0] # SetState(1) # GOTO Tab 4 match = ofparser.OFPMatch(state=0, eth_type=0x8847) actions = [ osparser.OFPExpActionSetDataVariable(table_id=3, opcode=osproto.OPCODE_SUM, output_gd_id=0, operand_1_hf_id=1, operand_2_cost=1), osparser.OFPExpActionWriteContextToField( src_type=osproto.SOURCE_TYPE_GLOBAL_DATA_VAR, src_id=0, dst_field=ofproto.OXM_OF_MPLS_TC), # osparser.OFPExpActionSetState(state=1, table_id=3, idle_timeout=15)] osparser.OFPExpActionSetState(state=1, table_id=3) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(4) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=3, priority=198, match=match, instructions=inst) datapath.send_msg(mod) '''# ####################### TAB 4 verifica le condizioni C[0] e C[1]''' ''' C[0] verifica se il costo memorizzato nel pacchetto e' <= di quello gia conosciuto (in pratica se il pacchetto ha fatto un percorso migliore) ''' ''' C[1] serve semplicemente a capire se e' lo switch direttametne collegato all'host che parla e quindi non bisogna fare il pop della label MPLS ''' ''' le righe BIS verificano la C[1] e non eseguono il POP della label MPLS ''' ''' metadata = 1 e' un pacchetto duplicato quindi DROP ''' ''' nel caso imposta metadata = 3 cioe' non e' un pacchetto duplicato ma il costo e' maggiore di quello conosciuto, inoltro senza aggiornare stato ''' """ Riga 1 """ # C[0]: MPLS_TC > COSTO MEMORIZZATO -> HF[1] > FD[0] # MetaData: 1 -> Pacchetto duplicato # azione DROP match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=0, metadata=1) actions = [osparser.OFPExpActionSetState(state=1, table_id=4)] self.add_flow(datapath=datapath, table_id=4, priority=1198, match=match, actions=actions) """ Riga 2 """ # C[0]: MPLS_TC > COSTO MEMORIZZATO -> HF[1] > FD[0] # MetaData: 0 -> Pacchetto NON duplicato # SetState(1) # WriteMetadata = 3 -> pacchetto da percorso peggiore ma NON duplicato # azione GOTO Tab 3 match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=0, condition1=0, metadata=0) actions = [ osparser.OFPExpActionSetState(state=1, table_id=4), ofparser.OFPActionPopMpls() ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=3, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=198, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 2 BIS """ # C[0]: MPLS_TC > COSTO MEMORIZZATO -> HF[1] > FD[0] # MetaData: 0 -> Pacchetto NON duplicato # SetState(1) # WriteMetadata = 3 -> pacchetto da percorso peggiore ma NON duplicato # azione GOTO Tab 3 match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=0, condition1=1, metadata=0) actions = [osparser.OFPExpActionSetState(state=1, table_id=4)] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionWriteMetadata(metadata=3, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=198, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 3 """ # C[0]: MPLS_TC <= COSTO MEMORIZZATO -> HF[1] <= FD[0] # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=1, condition1=0) actions = [ osparser.OFPExpActionSetState(state=1, table_id=4), osparser.OFPExpActionSetDataVariable(table_id=4, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0), ofparser.OFPActionPopMpls() ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=98, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 3 BIS """ # C[0]: MPLS_TC <= COSTO MEMORIZZATO -> HF[1] <= FD[0] # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=1, eth_type=0x8847, condition0=1, condition1=1) actions = [ osparser.OFPExpActionSetState(state=1, table_id=4), osparser.OFPExpActionSetDataVariable(table_id=4, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=98, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 4 """ # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=0, eth_type=0x8847, condition1=0) actions = [ osparser.OFPExpActionSetState(state=1, table_id=4), osparser.OFPExpActionSetDataVariable(table_id=4, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0), ofparser.OFPActionPopMpls() ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=8, match=match, instructions=inst) datapath.send_msg(mod) """ Riga 4 BIS """ # FD[0] = HF[1] -> COSTO MEMORIZZATO = MPLS_TC # SetState(1) # azione GOTO Tab 3 match = ofparser.OFPMatch(state=0, eth_type=0x8847, condition1=1) actions = [ osparser.OFPExpActionSetState(state=1, table_id=4), osparser.OFPExpActionSetDataVariable(table_id=4, opcode=osproto.OPCODE_SUM, output_fd_id=0, operand_1_hf_id=1, operand_2_cost=0) ] inst = [ ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(5) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=4, priority=8, match=match, instructions=inst) datapath.send_msg(mod) '''# ####################### TAB 5 semplicemente MAC Learning ''' ''' Mac Learning ampliato per gestire i metadata = 1 o metadata = 3 ''' ''' metadata = 1 se e' arrivato fin qui significa che il costo nel pacchetto e' minore di quello conosciuto (ha seguito un percorso migliore) AGGIORNARE e DROP''' ''' metadata = 3 non e' un pacchetto duplicato ma il costo del percorso seguito e' peggiore di quello conosciuto NON AGGIORNARE e INOLTRA ''' # Per ogni input port, per ogni stato for i in range(1, N + 1): for s in range(N + 1): match = ofparser.OFPMatch(in_port=i, state=s) if s == 0: out_port = ofproto.OFPP_FLOOD #serve la flood, dipende se sto su S1 o S6 else: out_port = s # actions = [osparser.OFPExpActionSetState(state=i, table_id=5, hard_timeout=10), actions = [ osparser.OFPExpActionSetState(state=i, table_id=5), ofparser.OFPActionOutput(out_port) ] self.add_flow(datapath=datapath, table_id=5, priority=0, match=match, actions=actions) # Configuro le entry con azione DROP per i pacchetti duplicati (con metadata = 1) match = ofparser.OFPMatch(in_port=i, metadata=1) # actions = [osparser.OFPExpActionSetState(state=i, table_id=5, hard_timeout=10)] actions = [osparser.OFPExpActionSetState(state=i, table_id=5)] self.add_flow(datapath=datapath, table_id=5, priority=1198, match=match, actions=actions) # Per ogni stato for s in range(N + 1): match = ofparser.OFPMatch(state=s, metadata=3) if s == 0: out_port = ofproto.OFPP_FLOOD #serve la flood, dipende se sto su S1 o S6 else: out_port = s # Configuro le entry per l'output(in_port) senza aggiornare lo stato actions = [ofparser.OFPActionOutput(out_port)] self.add_flow(datapath=datapath, table_id=5, priority=198, match=match, actions=actions)
def load_fsm(self, datapath): LOG.info("Loading Table 0 normal FSM ...") ##=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# ## state INIT - ANY """ Match a first packet of a new TCP flow (regardless of TCP flags) """ match = ofparser.OFPMatch(eth_type=0x0800, ip_proto=6, state=self.INIT) """ Forward the packet to the corresponding output interface and create entries for both directions of given flow in the OPEN state (forward all consecutive packets). ( TODO - hard-coded output) """ actions = [ ofparser.OFPActionOutput(2), # Create entry for direction of incoming packet osparser.OFPExpActionSetState( state=self.OPEN, table_id=0, # TODO - TIMEOUTS idle_timeout=10, bit=0), # Create entry for opposite direction since response is expected osparser.OFPExpActionSetState( state=self.OPEN, table_id=0, # TODO - TIMEOUTS idle_timeout=10, bit=1) ] """ Apply forward actions and the creation of entries, pass the first packet to the table1 for the new TCP connections statistics computation. """ inst = [ ofparser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions), ofparser.OFPInstructionGotoTable(table_id=1) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=100, match=match, instructions=inst) datapath.send_msg(mod) ##=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# ## state OPEN - ANY """ Forward all consecutive packets of already seen flow by matching on previously created entries. """ match = ofparser.OFPMatch(eth_type=0x0800, ip_proto=6, state=self.OPEN) """ Just output packet to the corresponding output interface. ( TODO - hard-coded output) """ actions = [ ofparser.OFPActionOutput(2), ofparser.OFPActionOutput(1), # Refresh timeouts only osparser.OFPExpActionSetState( state=self.OPEN, table_id=0, # TODO - TIMEOUTS idle_timeout=10, bit=0), # Refresh timeouts only osparser.OFPExpActionSetState( state=self.OPEN, table_id=0, # TODO - TIMEOUTS idle_timeout=10, bit=1) ] inst = [ ofparser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions) ] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=100, match=match, instructions=inst) datapath.send_msg(mod) LOG.info("Done.")
def process_packet(self, datapath, act_state, flags, output_ports, ch_state_src, idle_to_src, hard_to_src, ch_state_dst, idle_to_dst, hard_to_dst, priority, count_in): """ Match packet - ethernet, TCP protocol, state (parameter), optional flags (parameter). """ if flags == F_DONT_CARE: match = ofparser.OFPMatch(eth_type=0x0800, ip_proto=6, state=act_state) else: match = ofparser.OFPMatch(eth_type=0x0800, ip_proto=6, state=act_state, tcp_flags=flags) """ Set actions: - Output ports (parameter - list). - SetState for both directions (parameters). """ actions = [] for port in output_ports: actions.append(ofparser.OFPActionOutput(port)) if ch_state_src != self.CH_STATE_NONE: actions.append( osparser.OFPExpActionSetState( state=ch_state_src, table_id=0, # TODO - TIMEOUTS idle_timeout=idle_to_src, hard_timeout=hard_to_src, bit=0)) if ch_state_dst != self.CH_STATE_NONE: actions.append( osparser.OFPExpActionSetState( state=ch_state_dst, table_id=0, # TODO - TIMEOUTS idle_timeout=idle_to_dst, hard_timeout=hard_to_dst, bit=1)) """ Set instructions: - Apply previously defined actions. - Optionally pass packet to table1 for counting. """ inst = [ ofparser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions) ] if count_in: inst.append(ofparser.OFPInstructionGotoTable(table_id=1)) """ Prepare and send message. """ mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=priority, match=match, instructions=inst) datapath.send_msg(mod)
def switch_features_handler(self, event): """ Switche sent his features, check if OpenState supported """ msg = event.msg datapath = msg.datapath LOG.info("Configuring switch %d..." % datapath.id) """ Set table 0 as stateful """ req = bebaparser.OFPExpMsgConfigureStatefulTable( datapath=datapath, table_id=0, stateful=1) datapath.send_msg(req) """ Set table 1 as stateful """ req = bebaparser.OFPExpMsgConfigureStatefulTable( datapath=datapath, table_id=1, stateful=1) datapath.send_msg(req) ############################### LOOKUP/UPDATE ################ """ Set lookup extractor = {eth_dst} """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST], table_id=0, biflow=1) datapath.send_msg(req) """ Set update extractor = {eth_src} """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST], table_id=0, biflow=1) datapath.send_msg(req) """ Set lookup extractor = {eth_dst} """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=1) datapath.send_msg(req) """ Set update extractor = {eth_src} """ req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath, command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR, fields=[ofproto.OXM_OF_ETH_SRC], table_id=1) datapath.send_msg(req) ########################### SET GD DATA VARIABLE ############################################ # req = bebaparser.OFPExpMsgHeaderFieldExtract( # datapath=datapath, # table_id=0, # extractor_id=0, # field=ofproto.OXM_OF_IPV4_SRC # ) # datapath.send_msg(req) req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=0, value=313 ) datapath.send_msg(req) req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=2, value=22 ) datapath.send_msg(req) req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=4, value=44 ) datapath.send_msg(req) req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=0, global_data_variable_id=5, value=55 ) datapath.send_msg(req) ########################### SET GD DATA VARIABLE ############################################ req = bebaparser.OFPExpMsgsSetGlobalDataVariable( datapath=datapath, table_id=1, global_data_variable_id=3, value=55 ) datapath.send_msg(req) ########################### SET HF DATA VARIABLE TAB 1 ############################################ # SI PUO FARE??? ''' HF[0] = OXM_OF_METADATA [id_pkt] ''' req = bebaparser.OFPExpMsgHeaderFieldExtract( datapath=datapath, table_id=1, extractor_id=0, field=ofproto.OXM_OF_ETH_SRC ) datapath.send_msg(req) # ''' HF[0] = OXM_OF_METADATA [id_pkt] ''' # req = bebaparser.OFPExpMsgHeaderFieldExtract( # datapath=datapath, # table_id=1, # extractor_id=0, # field=bebaproto.OXM_EXP_STATE # ) # datapath.send_msg(req) # aggiunta cosi tanto per fare un nuovo commit # ''' HF[0] = OXM_OF_METADATA [id_pkt] ''' # req = bebaparser.OFPExpMsgHeaderFieldExtract( # datapath=datapath, # table_id=1, # extractor_id=0, # field=bebaproto.OXM_EXP_STATE # ) # datapath.send_msg(req) ''' ####################### TAB 0 NULLA serve solo per i bug di OpenFlow, servono 2 stage xke le modifiche MPLS siano visibili''' # Non fa niente, ci sta solo per risolvere bug (presunti) di OpenFlow # match = ofparser.OFPMatch(condition0=0) # actions = [bebaparser.OFPExpActionSetState(state=1, table_id=0), # ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] # inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] # mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, # priority=0, match=match, instructions=inst) # datapath.send_msg(mod) # match = ofparser.OFPMatch()#condition0=1) # actions = [bebaparser.OFPExpActionSetState(state=1, table_id=0), # bebaparser.OFPExpActionSetDataVariable(table_id=0, port_id=1, opcode=bebaproto.OPCODE_SUM, output_mem_pd_id=0, operand_1_mem_pd_id=1, operand_2_mem_pd_id=1), # # bebaparser.OFPExpActionWriteContextToField(src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,src_id=0,dst_field=ofproto.OXM_OF_MPLS_LABEL), # bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_SUM, output_gd_id=3, operand_1_hf_id=0, operand_2_cost=0), # ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] # inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] # mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, # priority=0, match=match, instructions=inst) # datapath.send_msg(mod) # match = ofparser.OFPMatch(eth_type=0x0800 ,ipv4_src=('10.0.0.0','255.255.255.0')) match = ofparser.OFPMatch() actions = [bebaparser.OFPExpActionSetState(state=1, table_id=0), # bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_hf_id=0, operand_2_cost=3)] # bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_SUM, output_fd_id=2, operand_1_gd_id=0, operand_2_gd_id=2)] bebaparser.OFPExpActionWriteContextToField(src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,src_id=0,dst_field=ofproto.OXM_OF_METADATA)] inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), # ofparser.OFPInstructionWriteMetadata(metadata=13, metadata_mask=0xFFFFFFFF), ofparser.OFPInstructionGotoTable(1)] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=0, priority=0, match=match, instructions=inst) datapath.send_msg(mod) ''' ####################### TAB 1 ''' match = ofparser.OFPMatch()#state=2) actions = [bebaparser.OFPExpActionSetState(state=2, table_id=1), bebaparser.OFPExpActionSetDataVariable(table_id=1, opcode=bebaproto.OPCODE_SUM, output_fd_id=1, operand_1_hf_id=0, operand_2_cost=1), ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)] # bebaparser.OFPExpActionSetDataVariable(table_id=1, opcode=bebaproto.OPCODE_SUM, output_gd_id=0, operand_1_gd_id=1, operand_2_cost=3)] inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, priority=10, match=match, instructions=inst) datapath.send_msg(mod) # match = ofparser.OFPMatch(metadata=313) # actions = [bebaparser.OFPExpActionSetState(state=2, table_id=1), # bebaparser.OFPExpActionSetDataVariable(table_id=1, opcode=bebaproto.OPCODE_SUM, output_gd_id=0, operand_1_gd_id=3, operand_2_cost=3)] # inst = [ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)] # mod = ofparser.OFPFlowMod(datapath=datapath, table_id=1, # priority=0, match=match, instructions=inst) # datapath.send_msg(mod) # # for each input port, for each state # for i in range(1, N+1): # for s in range(N+1): # match = ofparser.OFPMatch(in_port=i, state=s) # if s == 0: # out_port = ofproto.OFPP_FLOOD # else: # out_port = s # actions = [bebaparser.OFPExpActionSetState(state=i, table_id=0, hard_timeout=10), # ofparser.OFPActionOutput(out_port)] # self.add_flow(datapath=datapath, table_id=0, priority=0, # match=match, actions=actions) """ Need to drop some packets for DEMO puporses only (avoid learning before manual send_eth)"""
def build(self): return [ofp.OFPInstructionActions(ofproto_v1_3.OFPIT_APPLY_ACTIONS, self.actions_fields)]
def _apply_actions(actions): return parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)