def of_demo_40(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 40 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Modify IP packet example1" priority = 900 cookie = 1300 match_in_port = 10 match_eth_type = ETH_TYPE_IPv4 match_ip_proto = IP_PROTO_TCP match_ipv4_src_addr = "192.1.2.0/24" match_ipv4_dst_addr = "173.194.123.40/32" match_tcp_dst_port = 8080 act_mod_ipv4_src_addr = "212.16.1.8/32" act_mod_ipv4_dst_addr = "52.87.12.11/32" act_mod_tcp_src_port = 8888 act_mod_tcp_dst_port = 9999 act_out_port = 119 print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IP Protocol (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " TCP Destination Port (%s)" % (match_in_port, hex(match_eth_type), match_ip_proto, match_ipv4_src_addr, match_ipv4_dst_addr, match_tcp_dst_port)) print (" Actions: Modify IPv4 Source Address (%s)\n" " Modify IPv4 Destination Address (%s)\n" " Modify TCP Source Port (%s)\n" " Modify TCP Destination Port (%s)\n" " Output (%s)" % (act_mod_ipv4_src_addr, act_mod_ipv4_dst_addr, act_mod_tcp_src_port, act_mod_tcp_dst_port, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetNwSrcAction(action_order) action.set_nw_src(act_mod_ipv4_src_addr) instruction.add_apply_action(action) action_order += 1 action = SetNwDstAction(action_order) action.set_nw_dst(act_mod_ipv4_dst_addr) instruction.add_apply_action(action) action_order += 1 action = SetTpSrcAction(action_order) action.set_tp_src(act_mod_tcp_src_port) instruction.add_apply_action(action) action_order += 1 action = SetTpDstAction(action_order) action.set_tp_dst(act_mod_tcp_dst_port) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ip_proto(match_ip_proto) match.set_ipv4_src(match_ipv4_src_addr) match.set_ipv4_dst(match_ipv4_dst_addr) match.set_tcp_dst_port(match_tcp_dst_port) flow_entry1.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) # --------------------------------------------------- # Second flow entry # --------------------------------------------------- table_id = 0 flow_id += 1 flow_name = "Modify IP packet example2" priority = 900 cookie = 1300 match_in_port = 110 match_eth_type = ETH_TYPE_IPv4 match_ip_proto = IP_PROTO_UDP match_ipv4_src_addr = "10.1.0.0/16" match_ipv4_dst_addr = "168.1.1.101/32" match_udp_dst_port = 1812 act_mod_ipv4_src_addr = "172.101.1.9/32" act_mod_ipv4_dst_addr = "172.101.1.1/32" act_mod_udp_src_port = 5555 act_mod_udp_dst_port = 7777 act_out_port = 120 print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IP Protocol (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " UDP Destination Port (%s)" % (match_in_port, hex(match_eth_type), match_ip_proto, match_ipv4_src_addr, match_ipv4_dst_addr, match_udp_dst_port)) print (" Actions: Set Field (IPv4 Source Address %s)\n" " Set Field (IPv4 Destination Address %s)\n" " Set Field (UDP Source Port %s)\n" " Set Field (UDP Destination Port %s)\n" " Output (%s)" % (act_mod_ipv4_src_addr, act_mod_ipv4_dst_addr, act_mod_udp_src_port, act_mod_udp_dst_port, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry2 = FlowEntry() # Generic attributes of the Flow Entry flow_entry2.set_flow_table_id(table_id) flow_entry2.set_flow_name(flow_name) flow_entry2.set_flow_id(flow_id) flow_entry2.set_flow_cookie(cookie) flow_entry2.set_flow_priority(priority) flow_entry2.set_flow_hard_timeout(0) flow_entry2.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetFieldAction(action_order) action.set_ipv4_src(act_mod_ipv4_src_addr) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_ipv4_dst(act_mod_ipv4_dst_addr) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_udp_src_port(act_mod_udp_src_port) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_udp_dst_port(act_mod_udp_dst_port) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry2.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ip_proto(match_ip_proto) match.set_ipv4_src(match_ipv4_src_addr) match.set_ipv4_dst(match_ipv4_dst_addr) match.set_udp_dst_port(match_udp_dst_port) flow_entry2.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry2.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry2) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print ("\n") print ("<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_26(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 26 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) flow_table_id = 0 flow_id_base = 12 flow_id = flow_id_base flowEntries = [] # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(6001) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_idle_timeout(12000) flow_entry.set_flow_hard_timeout(12000) flow_entry.set_flow_priority(1000) instruction = Instruction(instruction_order=0) action = DropAction(order=0) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_eth_src("00:11:22:33:44:55") match.set_eth_dst("aa:bb:cc:dd:ee:ff") flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(7001) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_idle_timeout(2400) flow_entry.set_flow_hard_timeout(2400) flow_entry.set_flow_priority(2000) instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="CONTROLLER", max_len=60) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_ipv4_src("1.2.3.4/32") match.set_ipv4_dst("192.168.1.11/32") flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(800) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(1800) flow_entry.set_flow_idle_timeout(1800) flow_entry.set_flow_priority(3000) instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port=5) instruction.add_apply_action(action) action = OutputAction(order=1, port=6) instruction.add_apply_action(action) action = OutputAction(order=2, port=7) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_in_port(1) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PushVlanHeaderAction(order=0) action.set_eth_type(ETH_TYPE_QINQ) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_vlan_id(100) instruction.add_apply_action(action) action = PushVlanHeaderAction(order=2) action.set_eth_type(ETH_TYPE_DOT1Q) instruction.add_apply_action(action) action = SetFieldAction(order=3) action.set_vlan_id(998) instruction.add_apply_action(action) action = OutputAction(order=4, port=111) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_vlan_id(998) match.set_in_port(in_port=110) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PushVlanHeaderAction(order=0) action.set_eth_type(ETH_TYPE_QINQ) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_vlan_id(100) instruction.add_apply_action(action) action = PushVlanHeaderAction(order=2) action.set_eth_type(ETH_TYPE_DOT1Q) instruction.add_apply_action(action) action = SetFieldAction(order=3) action.set_vlan_id(998) instruction.add_apply_action(action) action = OutputAction(order=4, port=111) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_vlan_id(998) match.set_in_port(in_port=110) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PopVlanHeaderAction(order=0) instruction.add_apply_action(action) action = OutputAction(order=1, port=110) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_vlan_id(100) match.set_in_port(111) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PopVlanHeaderAction(order=0) instruction.add_apply_action(action) action = OutputAction(order=1, port=110) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_vlan_id(100) match.set_in_port(111) flow_entry.add_match(match) flowEntries.append(flow_entry) print ("\n") print ("<<< Remove configured flows from the Controller") ofswitch.delete_flows(flow_table_id) print ("\n") print ("<<< Set OpenFlow flows on the Controller") print ("\n") print ("<<< Flows to be configured:") flowEntries = sorted(flowEntries, key=lambda fe: fe.get_flow_priority()) for fe in flowEntries: print (" %s" % fe.to_ofp_oxm_syntax()) time.sleep(rundelay) success = True for fe in flowEntries: result = ofswitch.add_modify_flow(fe) status = result.get_status() if(status.eq(STATUS.OK)): pass else: success = False print ("\n") print ("!!!Demo terminated, failed to add flow:\n '%s'" % fe.to_ofp_oxm_syntax()) print (" Failure reason: %s" % status.detailed()) if success: print "\n" print ("<<< Flows successfully added to the Controller") else: print "\n" print ("<<< Removing all flows from the Controller") ofswitch.delete_flows(flow_table_id) exit(0) print ("\n") print ("<<< Get configured flows from the Controller") time.sleep(rundelay) print ("\n") print ("<<< Configured flows:") result = ofswitch.get_configured_FlowEntries(flow_table_id=0) status = result.get_status() if(status.eq(STATUS.OK)): data = result.get_data() flowEntries = sorted(data, key=lambda fe: fe.get_flow_priority()) for fe in flowEntries: print " %s" % fe.to_ofp_oxm_syntax() else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flows by IDs from the Controller:") time.sleep(rundelay) for i in range(flow_id_base, flow_id): result = ofswitch.get_configured_FlowEntry(flow_table_id, i) status = result.get_status() if(status.eq(STATUS.OK)): print (" -- Flow id '%s'" % i) fe = result.get_data() print " %s" % fe.to_ofp_oxm_syntax() else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) print ("<<< Removing all flows from the Controller") ofswitch.delete_flows(flow_table_id) exit(0) time.sleep(rundelay) print ("\n") print ("<<< Remove configured flows from the Controller") ofswitch.delete_flows(flow_table_id) time.sleep(rundelay) print ("\n") print ("<<< Get configured flows from the Controller") result = ofswitch.get_configured_FlowEntries(flow_table_id=0) status = result.get_status() if(status.eq(STATUS.DATA_NOT_FOUND)): print ("\n") print "<<< No configured flows" else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_9(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 9 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # TCP Source Port Number # TCP Destination Port Number # IP DSCP # Input Port # NOTE: Ethernet type MUST be 2048 (0x800) -> IPv4 protocol eth_type = ETH_TYPE_IPv4 eth_src = "00:00:00:11:23:ae" eth_dst = "ff:ff:29:01:19:61" ipv4_src = "17.1.2.3/8" ipv4_dst = "172.168.5.6/16" ip_proto = IP_PROTO_TCP # Assured Forwarding ('class'=1, 'drop precedence'=2) ip_dscp = IP_DSCP_AF12 tcp_src_port = 25364 tcp_dst_port = 8080 input_port = 13 print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Ethernet Type (%s)\n" " Ethernet Source Address (%s)\n" " Ethernet Destination Address (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " IP Protocol Number (%s)\n" " IP DSCP (%s)\n" " TCP Source Port Number (%s)\n" " TCP Destination Port Number (%s)\n" " Input Port (%s)" % (hex(eth_type), eth_src, eth_dst, ipv4_src, ipv4_dst, ip_proto, ip_dscp, tcp_src_port, tcp_dst_port, input_port)) print (" Action: Output (NORMAL)") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 16 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_priority(flow_priority=1007) flow_entry.set_flow_cookie(cookie=101) flow_entry.set_flow_cookie_mask(cookie_mask=255) # --- Instruction: 'Apply-actions' # Action: 'Output' NORMAL instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="NORMAL") instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # IP ECN # TCP Source Port Number # TCP Destination Port Number # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_ip_proto(ip_proto) match.set_ip_dscp(ip_dscp) match.set_tcp_src(tcp_src_port) match.set_tcp_dst(tcp_dst_port) match.set_in_port(input_port) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully removed from the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_8(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 8 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # Input Port # NOTE: Ethernet type MUST be 2048 (0x800) -> IPv4 protocol eth_type = ETH_TYPE_IPv4 eth_src = "00:1c:01:00:23:aa" eth_dst = "00:02:02:60:ff:fe" ipv4_src = "10.0.245.1/24" ipv4_dst = "192.168.1.123/16" ip_proto = IP_PROTO_TLSP ip_dscp = IP_DSCP_CS3 # 'Class Selector' = 'Flash' input_port = 13 print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Ethernet Type (%s)\n" " Ethernet Source Address (%s)\n" " Ethernet Destination Address (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " IP Protocol Number (%s)\n" " IP DSCP (%s)\n" " Input Port (%s)" % (hex(eth_type), eth_src, eth_dst, ipv4_src, ipv4_dst, ip_proto, ip_dscp, input_port)) print (" Action: Output (CONTROLLER)") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 15 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_priority(flow_priority=1006) flow_entry.set_flow_cookie(cookie=100) flow_entry.set_flow_cookie_mask(cookie_mask=255) # --- Instruction: 'Apply-actions' # Action: 'Output' to CONTROLLER instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="CONTROLLER", max_len=60) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_ip_proto(ip_proto) match.set_ip_dscp(ip_dscp) match.set_in_port(input_port) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully removed from the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_40(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 40 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Modify IP packet example1" priority = 900 cookie = 1300 match_in_port = 10 match_eth_type = ETH_TYPE_IPv4 match_ip_proto = IP_PROTO_TCP match_ipv4_src_addr = "192.1.2.0/24" match_ipv4_dst_addr = "173.194.123.40/32" match_tcp_dst_port = 8080 act_mod_ipv4_src_addr = "212.16.1.8/32" act_mod_ipv4_dst_addr = "52.87.12.11/32" act_mod_tcp_src_port = 8888 act_mod_tcp_dst_port = 9999 act_out_port = 119 print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IP Protocol (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " TCP Destination Port (%s)" % (match_in_port, hex(match_eth_type), match_ip_proto, match_ipv4_src_addr, match_ipv4_dst_addr, match_tcp_dst_port)) print( " Actions: Modify IPv4 Source Address (%s)\n" " Modify IPv4 Destination Address (%s)\n" " Modify TCP Source Port (%s)\n" " Modify TCP Destination Port (%s)\n" " Output (%s)" % (act_mod_ipv4_src_addr, act_mod_ipv4_dst_addr, act_mod_tcp_src_port, act_mod_tcp_dst_port, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetNwSrcAction(action_order) action.set_nw_src(act_mod_ipv4_src_addr) instruction.add_apply_action(action) action_order += 1 action = SetNwDstAction(action_order) action.set_nw_dst(act_mod_ipv4_dst_addr) instruction.add_apply_action(action) action_order += 1 action = SetTpSrcAction(action_order) action.set_tp_src(act_mod_tcp_src_port) instruction.add_apply_action(action) action_order += 1 action = SetTpDstAction(action_order) action.set_tp_dst(act_mod_tcp_dst_port) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ip_proto(match_ip_proto) match.set_ipv4_src(match_ipv4_src_addr) match.set_ipv4_dst(match_ipv4_dst_addr) match.set_tcp_dst(match_tcp_dst_port) flow_entry1.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) # --------------------------------------------------- # Second flow entry # --------------------------------------------------- table_id = 0 flow_id += 1 flow_name = "Modify IP packet example2" priority = 900 cookie = 1300 match_in_port = 110 match_eth_type = ETH_TYPE_IPv4 match_ip_proto = IP_PROTO_UDP match_ipv4_src_addr = "10.1.0.0/16" match_ipv4_dst_addr = "168.1.1.101/32" match_udp_dst_port = 1812 act_mod_ipv4_src_addr = "172.101.1.9/32" act_mod_ipv4_dst_addr = "172.101.1.1/32" act_mod_udp_src_port = 5555 act_mod_udp_dst_port = 7777 act_out_port = 120 print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IP Protocol (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " UDP Destination Port (%s)" % (match_in_port, hex(match_eth_type), match_ip_proto, match_ipv4_src_addr, match_ipv4_dst_addr, match_udp_dst_port)) print( " Actions: Set Field (IPv4 Source Address %s)\n" " Set Field (IPv4 Destination Address %s)\n" " Set Field (UDP Source Port %s)\n" " Set Field (UDP Destination Port %s)\n" " Output (%s)" % (act_mod_ipv4_src_addr, act_mod_ipv4_dst_addr, act_mod_udp_src_port, act_mod_udp_dst_port, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry2 = FlowEntry() # Generic attributes of the Flow Entry flow_entry2.set_flow_table_id(table_id) flow_entry2.set_flow_name(flow_name) flow_entry2.set_flow_id(flow_id) flow_entry2.set_flow_cookie(cookie) flow_entry2.set_flow_priority(priority) flow_entry2.set_flow_hard_timeout(0) flow_entry2.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetFieldAction(action_order) action.set_ipv4_src(act_mod_ipv4_src_addr) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_ipv4_dst(act_mod_ipv4_dst_addr) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_udp_src(act_mod_udp_src_port) instruction.add_apply_action(action) action_order += 1 action = SetFieldAction(action_order) action.set_udp_dst(act_mod_udp_dst_port) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry2.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ip_proto(match_ip_proto) match.set_ipv4_src(match_ipv4_src_addr) match.set_ipv4_dst(match_ipv4_dst_addr) match.set_udp_dst(match_udp_dst_port) flow_entry2.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry2.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry2) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print("\n") print( "<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_7(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 7 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # Input Port # NOTE: Ethernet type must be 2048 (0x800) -> IPv4 protocol eth_type = ETH_TYPE_IPv4 eth_src = "00:1a:1b:00:22:aa" eth_dst = "00:2b:00:60:ff:f1" ipv4_src = "44.44.44.1/24" ipv4_dst = "55.55.55.1/16" input_port = 13 print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Ethernet Type (%s)\n" " Ethernet Source Address (%s)\n" " Ethernet Destination Address (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " Input Port (%s)" % (hex(eth_type), eth_src, eth_dst, ipv4_src, ipv4_dst, input_port)) print(" Action: Output (CONTROLLER)") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 15 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_priority(flow_priority=1005) # --- Instruction: 'Apply-actions' # Action: 'Output' to CONTROLLER instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="CONTROLLER", max_len=60) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_in_port(input_port) flow_entry.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully read from the Controller") print("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print( "<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully removed from the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_4(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 4 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: IPv4 Destination Address # NOTE: Ethernet type MUST be 2048 (0x0800) -> IPv4 protocol eth_type = ETH_TYPE_IPv4 ipv4_dst = "10.11.12.13/24" print ("<<< 'Controller': %s, 'OpenFlow' switch: %s" % (ctrlIpAddr, nodeName)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (hex(eth_type), ipv4_dst)) print (" Action: Drop") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 11 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_priority(flow_priority=1000) # --- Instruction: 'Apply-actions' # Action: 'Drop' instruction = Instruction(instruction_order=0) action = DropAction(order=0) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # IPv4 Destination Address match = Match() match.set_eth_type(eth_type) match.set_ipv4_dst(ipv4_dst) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully removed from the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # IP ECN # ICMPv4 Type # ICMPv4 Code # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_ip_proto(ip_proto) match.set_ip_dscp(ip_dscp) match.set_ip_ecn(ip_ecn) match.set_icmpv4_type(icmpv4_type) match.set_icmpv4_code(icmpv4_code) match.set_in_port(input_port) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status()
def of_demo_22(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 22 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Type # Input Port # IPv4 Destination Address eth_type = ETH_TYPE_IPv4 in_port = 13 ipv4_dst = "10.12.5.4/32" # --- Flow Actions: Push MPLS # Set Field # Output push_ether_type = ETH_TYPE_MPLS_UCAST mpls_label = 27 output_port = 14 print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Ethernet Type (%s)\n" " Input Port (%s)\n" " IPv4 Destination Address (%s)" % (hex(eth_type), in_port, ipv4_dst)) print (" Action: Push MPLS Header (Ethernet Type %s)\n" " Set Field (MPLS label %s)\n" " Output (Physical Port number %s)" % (hex(push_ether_type), mpls_label, output_port)) time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_id = 28 flow_entry.set_flow_table_id(table_id) flow_entry.set_flow_name(flow_name="Push MPLS Label") flow_entry.set_flow_id(flow_id) flow_entry.set_flow_hard_timeout(hard_timeout=0) flow_entry.set_flow_idle_timeout(idle_timeout=0) flow_entry.set_flow_priority(flow_priority=1021) flow_entry.set_flow_cookie(cookie=654) flow_entry.set_flow_cookie_mask(cookie_mask=255) # --- Instruction: 'Apply-actions' # Actions: 'Push MPLS Header' # 'Set Field' # 'Output' instruction = Instruction(instruction_order=0) action = PushMplsHeaderAction(order=0) action.set_eth_type(push_ether_type) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_mpls_label(mpls_label) instruction.add_apply_action(action) action = OutputAction(order=2, port=output_port) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Input Port # IPv4 Destination Address match = Match() match.set_eth_type(eth_type) match.set_in_port(in_port) match.set_ipv4_dst(ipv4_dst) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully removed from the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_43(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 43 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Set Queue example" priority = 1000 cookie = 1400 match_in_port = 109 match_eth_type = ETH_TYPE_IPv4 match_ipv4_dst_addr = "10.0.0.0/8" act_queue_id = 7 act_out_port = 112 print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_dst_addr)) print (" Actions: Set Queue (%s)\n" " Output (%s)" % (act_queue_id, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetQueueAction(action_order) action.set_gueue_id(act_queue_id) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_dst(match_ipv4_dst_addr) flow_entry1.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() print ("<<< Get Queues Statistics for Port '%s'") % act_out_port time.sleep(rundelay) queues = [] result = ofswitch.get_port_queues_stats(act_out_port, decode_obj=True) status = result.get_status() if(status.eq(STATUS.OK)): queues = result.get_data() elif(status.eq(STATUS.DATA_NOT_FOUND)): pass else: print ("\n") print ("!!!Error, failed to get queue statistics for port '%s" % act_out_port) print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() s = "Port '{} - Queues Statistics'".format(act_out_port) print " %s\n" % s if queues: for queue in sorted(queues, key=lambda q: q.queue_id()): print " Queue '{}'".format(queue.queue_id()) print "\n".strip() print " Tx Packets : {}".format(queue.tx_pkts()) print " Tx Bytes : {}".format(queue.tx_bytes()) print " Tx Errors : {}".format(queue.tx_errs()) print " Duration : {}s".format(queue.time_alive()) print "\n".strip() else: print " None" print "\n".strip() print ("<<< Get Queue '%s' Statistics for Port '%s'" % (act_queue_id, act_out_port)) time.sleep(rundelay) queue = None result = ofswitch.get_port_queue_stats(port_num=act_out_port, queue_id=act_queue_id, decode_obj=True) status = result.get_status() if(status.eq(STATUS.OK)): queue = result.get_data() elif(status.eq(STATUS.DATA_NOT_FOUND)): pass else: print ("\n") print ("!!!Error, failed to get port '%s' queue '%s' statistics " % act_out_port, act_queue_id) print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() s = "Port '{}' - Queue '{}' Statistics".format(act_out_port, act_queue_id) print " %s\n" % s if queue: print " Tx Packets : {}".format(queue.tx_pkts()) print " Tx Bytes : {}".format(queue.tx_bytes()) print " Tx Errors : {}".format(queue.tx_errs()) print " Duration : {}s".format(queue.time_alive()) print "\n".strip() else: print " None" print ("\n") print ("<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_41(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 41 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Modify IP TTL example1" priority = 800 cookie = 1200 match_in_port = 1 match_eth_type = ETH_TYPE_IPv4 match_ipv4_dst_addr = "10.0.0.0/8" act_mod_ip_ttl = 3 act_out_port = 2 print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_dst_addr)) print(" Actions: Set IP TTL (%s)\n" " Output (%s)" % (act_mod_ip_ttl, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetNwTTLAction(action_order) action.set_ttl(act_mod_ip_ttl) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_dst(match_ipv4_dst_addr) flow_entry1.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) # --------------------------------------------------- # Second flow entry # --------------------------------------------------- table_id = 0 flow_id += 1 flow_name = "Modify IP TTL example2" priority = 800 cookie = 1200 match_in_port = 2 match_eth_type = ETH_TYPE_IPv4 match_ipv4_src_addr = "10.0.0.0/8" act_out_port = 1 print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Source Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_src_addr)) print( " Actions: Decrement IP TTL\n" " Output (%s)" % (act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry2 = FlowEntry() # Generic attributes of the Flow Entry flow_entry2.set_flow_table_id(table_id) flow_entry2.set_flow_name(flow_name) flow_entry2.set_flow_id(flow_id) flow_entry2.set_flow_cookie(cookie) flow_entry2.set_flow_priority(priority) flow_entry2.set_flow_hard_timeout(0) flow_entry2.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = DecNwTTLAction(action_order) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry2.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_src(match_ipv4_src_addr) flow_entry2.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry2.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry2) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print("\n") print( "<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def origApp(srcIp, destIp): f = "cfg.yml" d = {} if load_dict_from_file(f, d) == False: print ("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d["ctrlIpAddr"] ctrlPortNum = d["ctrlPortNum"] ctrlUname = d["ctrlUname"] ctrlPswd = d["ctrlPswd"] nodeName = d["nodeName"] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") rundelay = 0 ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) result = ctrl.get_openflow_nodes_operational_list() status = result.get_status() if status.eq(STATUS.OK) == True: print ('OpenFlow node names (composed as "openflow:datapathid"):') nodenames = result.get_data() print json.dumps(nodenames, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) idx = mktime(gmtime(0)) for n in nodenames: ofswitch = OFSwitch(ctrl, n) # --- Flow Match: # IPv4 Source Address # IPv4 Destination Address # NOTE: Ethernet type MUST be 2048 (0x800) -> IPv4 protocol eth_type = ETH_TYPE_IPv4 ipv4_src = srcIp ipv4_dst = destIp print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, n)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print ( " Match: Ethernet Type (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" % (hex(eth_type), ipv4_src, ipv4_dst) ) print (" Action: Output (NORMAL)") time.sleep(rundelay) for i in range(0, 2): flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 16 + idx idx = idx + 1 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_priority(flow_priority=1007) # --- Instruction: 'Apply-actions' # Action: 'Output' NORMAL instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="NORMAL") instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # IP ECN # TCP Source Port Number # TCP Destination Port Number # Input Port match = Match() match.set_eth_type(eth_type) if i == 0: match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) else: match.set_ipv4_src(ipv4_dst) match.set_ipv4_dst(ipv4_src) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if status.eq(STATUS.OK) == True: print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if status.eq(STATUS.OK) == True: print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_41(): f = "cfg.yml" d = {} if load_dict_from_file(f, d) is False: print ("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d["ctrlIpAddr"] ctrlPortNum = d["ctrlPortNum"] ctrlUname = d["ctrlUname"] ctrlPswd = d["ctrlPswd"] nodeName = d["nodeName"] rundelay = d["rundelay"] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 41 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Modify IP TTL example1" priority = 800 cookie = 1200 match_in_port = 1 match_eth_type = ETH_TYPE_IPv4 match_ipv4_dst_addr = "10.0.0.0/8" act_mod_ip_ttl = 3 act_out_port = 2 print "\n" print ("<<< Set OpenFlow flow on the Controller") print ( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_dst_addr) ) print (" Actions: Set IP TTL (%s)\n" " Output (%s)" % (act_mod_ip_ttl, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetNwTTLAction(action_order) action.set_ttl(act_mod_ip_ttl) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_dst(match_ipv4_dst_addr) flow_entry1.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if status.eq(STATUS.OK): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) # --------------------------------------------------- # Second flow entry # --------------------------------------------------- table_id = 0 flow_id += 1 flow_name = "Modify IP TTL example2" priority = 800 cookie = 1200 match_in_port = 2 match_eth_type = ETH_TYPE_IPv4 match_ipv4_src_addr = "10.0.0.0/8" act_out_port = 1 print "\n" print ("<<< Set OpenFlow flow on the Controller") print ( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Source Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_src_addr) ) print (" Actions: Decrement IP TTL\n" " Output (%s)" % (act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry2 = FlowEntry() # Generic attributes of the Flow Entry flow_entry2.set_flow_table_id(table_id) flow_entry2.set_flow_name(flow_name) flow_entry2.set_flow_id(flow_id) flow_entry2.set_flow_cookie(cookie) flow_entry2.set_flow_priority(priority) flow_entry2.set_flow_hard_timeout(0) flow_entry2.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = DecNwTTLAction(action_order) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry2.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_src(match_ipv4_src_addr) flow_entry2.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry2.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry2) status = result.get_status() if status.eq(STATUS.OK): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print ("\n") print ( "<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName) ) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_11(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 11 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # ICMPv4 Type # ICMPv4 Code # IP DSCP # IP ECN # Input Port # NOTE: Ethernet type MUST be 2048 (0x800) -> IPv4 protocol # IP Protocol Type MUST be 1 -> ICMP eth_type = ETH_TYPE_IPv4 eth_src = "00:00:00:11:23:ae" eth_dst = "00:ff:20:01:1a:3d" ipv4_src = "17.1.2.3/8" ipv4_dst = "172.168.5.6/18" ip_proto = IP_PROTO_ICMP ip_dscp = IP_DSCP_CS2 # 'Class Selector' = 'Immediate' ip_ecn = IP_ECN_CE # Congestion Encountered icmpv4_type = 6 # Alternate Host Address icmpv4_code = 3 # Alternate Address for Host input_port = 10 print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Ethernet Type (%s)\n" " Ethernet Source Address (%s)\n" " Ethernet Destination Address (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " IP Protocol Number (%s)\n" " IP DSCP (%s)\n" " IP ECN (%s)\n" " ICMPv4 Type (%s)\n" " ICMPv4 Code (%s)\n" " Input Port (%s)" % (hex(eth_type), eth_src, eth_dst, ipv4_src, ipv4_dst, ip_proto, ip_dscp, ip_ecn, icmpv4_type, icmpv4_code, input_port)) print(" Action: Output (NORMAL)") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 18 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(1009) # --- Instruction: 'Apply-actions' # Action: 'Output' NORMAL instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="NORMAL") instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # IP ECN # ICMPv4 Type # ICMPv4 Code # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_ip_proto(ip_proto) match.set_ip_dscp(ip_dscp) match.set_ip_ecn(ip_ecn) match.set_icmpv4_type(icmpv4_type) match.set_icmpv4_code(icmpv4_code) match.set_in_port(input_port) flow_entry.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully read from the Controller") print("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print( "<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully removed from the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_43(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 43 Start") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Set Queue example" priority = 1000 cookie = 1400 match_in_port = 109 match_eth_type = ETH_TYPE_IPv4 match_ipv4_dst_addr = "10.0.0.0/8" act_queue_id = 7 act_out_port = 112 print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_eth_type), match_ipv4_dst_addr)) print(" Actions: Set Queue (%s)\n" " Output (%s)" % (act_queue_id, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetQueueAction(action_order) action.set_gueue_id(act_queue_id) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_dst(match_ipv4_dst_addr) flow_entry1.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() print("<<< Get Queues Statistics for Port '%s'") % act_out_port time.sleep(rundelay) queues = [] result = ofswitch.get_port_queues_stats(act_out_port, decode_obj=True) status = result.get_status() if (status.eq(STATUS.OK)): queues = result.get_data() elif (status.eq(STATUS.DATA_NOT_FOUND)): pass else: print("\n") print("!!!Error, failed to get queue statistics for port '%s" % act_out_port) print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() s = "Port '{} - Queues Statistics'".format(act_out_port) print " %s\n" % s if queues: for queue in sorted(queues, key=lambda q: q.queue_id()): print " Queue '{}'".format(queue.queue_id()) print "\n".strip() print " Tx Packets : {}".format(queue.tx_pkts()) print " Tx Bytes : {}".format(queue.tx_bytes()) print " Tx Errors : {}".format(queue.tx_errs()) print " Duration : {}s".format(queue.time_alive()) print "\n".strip() else: print " None" print "\n".strip() print("<<< Get Queue '%s' Statistics for Port '%s'" % (act_queue_id, act_out_port)) time.sleep(rundelay) queue = None result = ofswitch.get_port_queue_stats(port_num=act_out_port, queue_id=act_queue_id, decode_obj=True) status = result.get_status() if (status.eq(STATUS.OK)): queue = result.get_data() elif (status.eq(STATUS.DATA_NOT_FOUND)): pass else: print("\n") print( "!!!Error, failed to get port '%s' queue '%s' statistics " % act_out_port, act_queue_id) print("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print "\n".strip() s = "Port '{}' - Queue '{}' Statistics".format(act_out_port, act_queue_id) print " %s\n" % s if queue: print " Tx Packets : {}".format(queue.tx_pkts()) print " Tx Bytes : {}".format(queue.tx_bytes()) print " Tx Errors : {}".format(queue.tx_errs()) print " Duration : {}s".format(queue.time_alive()) print "\n".strip() else: print " None" print("\n") print( "<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_11(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 11 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # ICMPv4 Type # ICMPv4 Code # IP DSCP # IP ECN # Input Port # NOTE: Ethernet type MUST be 2048 (0x800) -> IPv4 protocol # IP Protocol Type MUST be 1 -> ICMP eth_type = ETH_TYPE_IPv4 eth_src = "00:00:00:11:23:ae" eth_dst = "00:ff:20:01:1a:3d" ipv4_src = "17.1.2.3/8" ipv4_dst = "172.168.5.6/18" ip_proto = IP_PROTO_ICMP ip_dscp = IP_DSCP_CS2 # 'Class Selector' = 'Immediate' ip_ecn = IP_ECN_CE # Congestion Encountered icmpv4_type = 6 # Alternate Host Address icmpv4_code = 3 # Alternate Address for Host input_port = 10 print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Ethernet Type (%s)\n" " Ethernet Source Address (%s)\n" " Ethernet Destination Address (%s)\n" " IPv4 Source Address (%s)\n" " IPv4 Destination Address (%s)\n" " IP Protocol Number (%s)\n" " IP DSCP (%s)\n" " IP ECN (%s)\n" " ICMPv4 Type (%s)\n" " ICMPv4 Code (%s)\n" " Input Port (%s)" % (hex(eth_type), eth_src, eth_dst, ipv4_src, ipv4_dst, ip_proto, ip_dscp, ip_ecn, icmpv4_type, icmpv4_code, input_port)) print (" Action: Output (NORMAL)") time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_entry.set_flow_table_id(table_id) flow_id = 18 flow_entry.set_flow_id(flow_id) flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(1009) # --- Instruction: 'Apply-actions' # Action: 'Output' NORMAL instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="NORMAL") instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Ethernet Source Address # Ethernet Destination Address # IPv4 Source Address # IPv4 Destination Address # IP Protocol Number # IP DSCP # IP ECN # ICMPv4 Type # ICMPv4 Code # Input Port match = Match() match.set_eth_type(eth_type) match.set_eth_src(eth_src) match.set_eth_dst(eth_dst) match.set_ipv4_src(ipv4_src) match.set_ipv4_dst(ipv4_dst) match.set_ip_proto(ip_proto) match.set_ip_dscp(ip_dscp) match.set_ip_ecn(ip_ecn) match.set_icmpv4_type(icmpv4_type) match.set_icmpv4_code(icmpv4_code) match.set_in_port(input_port) flow_entry.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully read from the Controller") print ("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print ("<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully removed from the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_idle_timeout(2400) flow_entry.set_flow_hard_timeout(2400) flow_entry.set_flow_priority(2000) instruction = Instruction(instruction_order = 0) action = OutputAction(order = 0, port="CONTROLLER", max_len=60) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_ipv4_src("1.2.3.4/32") match.set_ipv4_dst("192.168.1.11/32") flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(800) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(1800) flow_entry.set_flow_idle_timeout(1800) flow_entry.set_flow_priority(3000) instruction = Instruction(instruction_order = 0)
def of_demo_26(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 26 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) flow_table_id = 0 flow_id_base = 12 flow_id = flow_id_base flowEntries = [] # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(6001) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_idle_timeout(12000) flow_entry.set_flow_hard_timeout(12000) flow_entry.set_flow_priority(1000) instruction = Instruction(instruction_order=0) action = DropAction(order=0) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_eth_src("00:11:22:33:44:55") match.set_eth_dst("aa:bb:cc:dd:ee:ff") flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(7001) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_idle_timeout(2400) flow_entry.set_flow_hard_timeout(2400) flow_entry.set_flow_priority(2000) instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port="CONTROLLER", max_len=60) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_ipv4_src("1.2.3.4/32") match.set_ipv4_dst("192.168.1.11/32") flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(800) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(1800) flow_entry.set_flow_idle_timeout(1800) flow_entry.set_flow_priority(3000) instruction = Instruction(instruction_order=0) action = OutputAction(order=0, port=5) instruction.add_apply_action(action) action = OutputAction(order=1, port=6) instruction.add_apply_action(action) action = OutputAction(order=2, port=7) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_in_port(1) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PushVlanHeaderAction(order=0) action.set_eth_type(ETH_TYPE_QINQ) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_vlan_id(100) instruction.add_apply_action(action) action = PushVlanHeaderAction(order=2) action.set_eth_type(ETH_TYPE_DOT1Q) instruction.add_apply_action(action) action = SetFieldAction(order=3) action.set_vlan_id(998) instruction.add_apply_action(action) action = OutputAction(order=4, port=111) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_vlan_id(998) match.set_in_port(in_port=110) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PushVlanHeaderAction(order=0) action.set_eth_type(ETH_TYPE_QINQ) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_vlan_id(100) instruction.add_apply_action(action) action = PushVlanHeaderAction(order=2) action.set_eth_type(ETH_TYPE_DOT1Q) instruction.add_apply_action(action) action = SetFieldAction(order=3) action.set_vlan_id(998) instruction.add_apply_action(action) action = OutputAction(order=4, port=111) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_vlan_id(998) match.set_in_port(in_port=110) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PopVlanHeaderAction(order=0) instruction.add_apply_action(action) action = OutputAction(order=1, port=110) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_ARP) match.set_vlan_id(100) match.set_in_port(111) flow_entry.add_match(match) flowEntries.append(flow_entry) # Sample flow entry flow_entry = FlowEntry() flow_entry.set_flow_cookie(1234) flow_entry.set_flow_table_id(flow_table_id) flow_entry.set_flow_id(flow_id) flow_id += 1 flow_entry.set_flow_hard_timeout(0) flow_entry.set_flow_idle_timeout(0) flow_entry.set_flow_priority(4000) instruction = Instruction(instruction_order=0) action = PopVlanHeaderAction(order=0) instruction.add_apply_action(action) action = OutputAction(order=1, port=110) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) match = Match() match.set_eth_type(ETH_TYPE_IPv4) match.set_vlan_id(100) match.set_in_port(111) flow_entry.add_match(match) flowEntries.append(flow_entry) print("\n") print("<<< Remove configured flows from the Controller") ofswitch.delete_flows(flow_table_id) print("\n") print("<<< Set OpenFlow flows on the Controller") print("\n") print("<<< Flows to be configured:") flowEntries = sorted(flowEntries, key=lambda fe: fe.get_flow_priority()) for fe in flowEntries: print(" %s" % fe.to_ofp_oxm_syntax()) time.sleep(rundelay) success = True for fe in flowEntries: result = ofswitch.add_modify_flow(fe) status = result.get_status() if (status.eq(STATUS.OK)): pass else: success = False print("\n") print("!!!Demo terminated, failed to add flow:\n '%s'" % fe.to_ofp_oxm_syntax()) print(" Failure reason: %s" % status.detailed()) if success: print "\n" print("<<< Flows successfully added to the Controller") else: print "\n" print("<<< Removing all flows from the Controller") ofswitch.delete_flows(flow_table_id) exit(0) print("\n") print("<<< Get configured flows from the Controller") time.sleep(rundelay) print("\n") print("<<< Configured flows:") result = ofswitch.get_configured_FlowEntries(flow_table_id=0) status = result.get_status() if (status.eq(STATUS.OK)): data = result.get_data() flowEntries = sorted(data, key=lambda fe: fe.get_flow_priority()) for fe in flowEntries: print " %s" % fe.to_ofp_oxm_syntax() else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print("<<< Get configured flows by IDs from the Controller:") time.sleep(rundelay) for i in range(flow_id_base, flow_id): result = ofswitch.get_configured_FlowEntry(flow_table_id, i) status = result.get_status() if (status.eq(STATUS.OK)): print(" -- Flow id '%s'" % i) fe = result.get_data() print " %s" % fe.to_ofp_oxm_syntax() else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) print("<<< Removing all flows from the Controller") ofswitch.delete_flows(flow_table_id) exit(0) time.sleep(rundelay) print("\n") print("<<< Remove configured flows from the Controller") ofswitch.delete_flows(flow_table_id) time.sleep(rundelay) print("\n") print("<<< Get configured flows from the Controller") result = ofswitch.get_configured_FlowEntries(flow_table_id=0) status = result.get_status() if (status.eq(STATUS.DATA_NOT_FOUND)): print("\n") print "<<< No configured flows" else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) exit(0) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
def of_demo_22(): f = "cfg.yml" d = {} if (load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit() try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print("Failed to get Controller device attributes") exit(0) print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print("<<< Demo 22 Start") print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) # --- Flow Match: Ethernet Type # Input Port # IPv4 Destination Address eth_type = ETH_TYPE_IPv4 in_port = 13 ipv4_dst = "10.12.5.4/32" # --- Flow Actions: Push MPLS # Set Field # Output push_ether_type = ETH_TYPE_MPLS_UCAST mpls_label = 27 output_port = 14 print("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) print "\n" print("<<< Set OpenFlow flow on the Controller") print( " Match: Ethernet Type (%s)\n" " Input Port (%s)\n" " IPv4 Destination Address (%s)" % (hex(eth_type), in_port, ipv4_dst)) print( " Action: Push MPLS Header (Ethernet Type %s)\n" " Set Field (MPLS label %s)\n" " Output (Physical Port number %s)" % (hex(push_ether_type), mpls_label, output_port)) time.sleep(rundelay) flow_entry = FlowEntry() table_id = 0 flow_id = 28 flow_entry.set_flow_table_id(table_id) flow_entry.set_flow_name(flow_name="Push MPLS Label") flow_entry.set_flow_id(flow_id) flow_entry.set_flow_hard_timeout(hard_timeout=0) flow_entry.set_flow_idle_timeout(idle_timeout=0) flow_entry.set_flow_priority(flow_priority=1021) flow_entry.set_flow_cookie(cookie=654) flow_entry.set_flow_cookie_mask(cookie_mask=255) # --- Instruction: 'Apply-actions' # Actions: 'Push MPLS Header' # 'Set Field' # 'Output' instruction = Instruction(instruction_order=0) action = PushMplsHeaderAction(order=0) action.set_eth_type(push_ether_type) instruction.add_apply_action(action) action = SetFieldAction(order=1) action.set_mpls_label(mpls_label) instruction.add_apply_action(action) action = OutputAction(order=2, port=output_port) instruction.add_apply_action(action) flow_entry.add_instruction(instruction) # --- Match Fields: Ethernet Type # Input Port # IPv4 Destination Address match = Match() match.set_eth_type(eth_type) match.set_in_port(in_port) match.set_ipv4_dst(ipv4_dst) flow_entry.add_match(match) print("\n") print("<<< Flow to send:") print flow_entry.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully added to the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.detailed()) exit(0) print("\n") print("<<< Get configured flow from the Controller") time.sleep(rundelay) result = ofswitch.get_configured_flow(table_id, flow_id) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully read from the Controller") print("Flow info:") flow = result.get_data() print json.dumps(flow, indent=4) else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print( "<<< Delete flow with id of '%s' from the Controller's cache " "and from the table '%s' on the '%s' node" % (flow_id, table_id, nodeName)) time.sleep(rundelay) result = ofswitch.delete_flow(flow_entry.get_flow_table_id(), flow_entry.get_flow_id()) status = result.get_status() if (status.eq(STATUS.OK)): print("<<< Flow successfully removed from the Controller") else: print("\n") print("!!!Demo terminated, reason: %s" % status.brief().lower()) exit(0) print("\n") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(">>> Demo End") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
action.set_tos(mod_nw_tos) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_ip_eth_type) match.set_ipv4_dst(match_ipv4_dst) flow_entry1.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if(status.eq(STATUS.OK) == True): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id+1))
def of_demo_31(): f = "cfg.yml" d = {} if(load_dict_from_file(f, d) is False): print("Config file '%s' read error: " % f) exit(0) try: ctrlIpAddr = d['ctrlIpAddr'] ctrlPortNum = d['ctrlPortNum'] ctrlUname = d['ctrlUname'] ctrlPswd = d['ctrlPswd'] nodeName = d['nodeName'] rundelay = d['rundelay'] except: print ("Failed to get Controller device attributes") exit(0) print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") print ("<<< Demo 31 Start") print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd) ofswitch = OFSwitch(ctrl, nodeName) print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'" % (ctrlIpAddr, nodeName)) first_flow_id = 110 # --------------------------------------------------- # First flow entry # --------------------------------------------------- table_id = 0 flow_id = first_flow_id flow_name = "Set IPv4 ToS action" priority = 600 cookie = 1000 match_in_port = 109 match_ip_eth_type = ETH_TYPE_IPv4 match_ipv4_dst = "10.1.2.3/32" mod_nw_tos = 8 act_out_port = 112 print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_ip_eth_type), match_ipv4_dst)) print (" Actions: Set IPv4 ToS (tos %s)\n" " Output (Port number %s)" % (mod_nw_tos, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry1 = FlowEntry() # Generic attributes of the Flow Entry flow_entry1.set_flow_table_id(table_id) flow_entry1.set_flow_name(flow_name) flow_entry1.set_flow_id(flow_id) flow_entry1.set_flow_cookie(cookie) flow_entry1.set_flow_priority(priority) flow_entry1.set_flow_hard_timeout(0) flow_entry1.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetNwTosAction(action_order) action.set_tos(mod_nw_tos) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry1.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_ip_eth_type) match.set_ipv4_dst(match_ipv4_dst) flow_entry1.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry1.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry1) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) # --------------------------------------------------- # Second flow entry # --------------------------------------------------- table_id = 0 flow_id += 1 flow_name = "Set Field (IP DSCP) action" priority = 600 cookie = 1000 match_in_port = 109 match_eth_type = ETH_TYPE_IPv4 match_ipv4_dst = "192.1.2.3/32" mod_ip_dscp = IP_DSCP_CS5 act_out_port = 112 print "\n" print ("<<< Set OpenFlow flow on the Controller") print (" Match: Input Port (%s)\n" " Ethernet Type (%s)\n" " IPv4 Destination Address (%s)" % (match_in_port, hex(match_ip_eth_type), match_ipv4_dst)) print (" Actions: Set Field (IP DSCP %s)\n" " Output (Port number %s)" % (mod_ip_dscp, act_out_port)) time.sleep(rundelay) # Allocate a placeholder for the Flow Entry flow_entry2 = FlowEntry() # Generic attributes of the Flow Entry flow_entry2.set_flow_table_id(table_id) flow_entry2.set_flow_name(flow_name) flow_entry2.set_flow_id(flow_id) flow_entry2.set_flow_cookie(cookie) flow_entry2.set_flow_priority(priority) flow_entry2.set_flow_hard_timeout(0) flow_entry2.set_flow_idle_timeout(0) # Instructions/Actions for the Flow Entry instruction = Instruction(instruction_order=0) action_order = 0 action = SetFieldAction(action_order) action.set_ip_dscp(mod_ip_dscp) instruction.add_apply_action(action) action_order += 1 action = OutputAction(action_order) action.set_outport(act_out_port) instruction.add_apply_action(action) flow_entry2.add_instruction(instruction) # Match Fields for the Flow Entry match = Match() match.set_in_port(match_in_port) match.set_eth_type(match_eth_type) match.set_ipv4_dst(match_ipv4_dst) flow_entry2.add_match(match) print ("\n") print ("<<< Flow to send:") print flow_entry2.get_payload() time.sleep(rundelay) result = ofswitch.add_modify_flow(flow_entry2) status = result.get_status() if(status.eq(STATUS.OK)): print ("<<< Flow successfully added to the Controller") else: print ("\n") print ("!!!Demo terminated, reason: %s" % status.detailed()) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) exit(0) print ("\n") print ("<<< Delete flows from the Controller's cache " "and from the table '%s' on the '%s' node" % (table_id, nodeName)) time.sleep(rundelay) delete_flows(ofswitch, table_id, range(first_flow_id, flow_id + 1)) print ("\n") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print (">>> Demo End") print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")