Exemplo n.º 1
0
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 (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
Exemplo n.º 2
0
def of_demo_17():
    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 17 Start")
    print ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")



    ctrl = Controller(ctrlIpAddr, ctrlPortNum, ctrlUname, ctrlPswd)
    ofswitch = OFSwitch(ctrl, nodeName)

    # --- Flow Match: Ethernet Type
    #                 IPv6 Source Address
    #                 IPv6 Destination Address
    #                 IP DSCP
    #                 UDP Source Port
    #                 UDP Destination Port
    eth_type = ETH_TYPE_IPv6
    ipv6_src = "fe80::2acf:e9ff:fe21:6431/128"
    ipv6_dst = "aabb:1234:2acf:e9ff::fe21:6431/64"
    ip_dscp = IP_DSCP_CS1  # 'Class Selector' = 'Priority'
    ip_proto = IP_PROTO_UDP
    udp_src_port = 25364
    udp_dst_port = 7777

    # --- Flow Actions: Output (CONTROLLER)
    output_port = "CONTROLLER"

    print ("<<< 'Controller': %s, 'OpenFlow' switch: '%s'"
           % (ctrlIpAddr, nodeName))

    print "\n"
    print ("<<< Set OpenFlow flow on the Controller")
    print ("        Match:  Ethernet Type (%s)\n"
           "                IPv6 Source Address (%s)\n"
           "                IPv6 Destination Address (%s)\n"
           "                IP DSCP (%s)\n"
           "                UDP Source Port (%s)\n"
           "                UDP Destination Port (%s)"
           % (hex(eth_type), ipv6_src, ipv6_dst,
              ip_dscp, udp_src_port, udp_dst_port))
    print ("        Action: Output (to %s)"
           % (output_port))

    time.sleep(rundelay)

    flow_entry = FlowEntry()
    flow_entry.set_flow_name(flow_name="demo17.py")
    table_id = 0
    flow_id = 23
    flow_entry.set_flow_table_id(table_id)
    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(flow_priority=1015)

    # --- Instruction: 'Apply-actions'
    #     Actions:     'Output'
    instruction = Instruction(instruction_order=0)
    action = OutputAction(order=0, port=output_port)
    instruction.add_apply_action(action)
    flow_entry.add_instruction(instruction)

    # --- Match Fields: Ethernet Type
    #                   IPv6 Source Address
    #                   IPv6 Destination Address
    #                   IP protocol number (UDP)
    #                   IP DSCP
    #                   UDP Source Port
    #                   UDP Destination Port
    match = Match()
    match.set_eth_type(eth_type)
    match.set_ipv6_src(ipv6_src)
    match.set_ipv6_dst(ipv6_dst)
    match.set_ip_proto(ip_proto)
    match.set_ip_dscp(ip_dscp)
    match.set_udp_src_port(udp_src_port)
    match.set_udp_dst_port(udp_dst_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 (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
Exemplo n.º 3
0
def of_demo_10():
    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 10 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 DSCP
    #                 IP ECN
    #                 UDP Source Port Number
    #                 UDP Destination Port Number
    #                 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 = "20:14:29:01:19:61"
    ipv4_src = "192.1.2.3/10"
    ipv4_dst = "172.168.5.6/18"
    ip_proto = IP_PROTO_UDP
    ip_dscp = IP_DSCP_CS1   # 'Class Selector' = 'Priority'
    ip_ecn = IP_ECN_CE      # Congestion Encountered
    udp_src_port = 25364
    udp_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"
           "                IP ECN (%s)\n"
           "                UDP Source Port Number (%s)\n"
           "                UDP Destination Port Number (%s)\n"
           "                Input Port (%s)"
           % (hex(eth_type), eth_src,
              eth_dst, ipv4_src, ipv4_dst,
              ip_proto, ip_dscp, ip_ecn,
              udp_src_port, udp_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 = 17
    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(flow_priority=1008)

    # --- 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
    #                   UDP Source Port Number
    #                   UDP 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_ip_ecn(ip_ecn)
    match.set_udp_src_port(udp_src_port)
    match.set_udp_dst_port(udp_dst_port)
    match.set_in_port(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 (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
Exemplo n.º 4
0
 #                   IP DSCP
 #                   IP ECN
 #                   UDP Source Port Number
 #                   UDP 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_ip_ecn(ip_ecn)    
 match.set_udp_src_port(udp_src_port)
 match.set_udp_dst_port(udp_dst_port)
 match.set_in_port(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) == True):
     print ("<<< Flow successfully added to the Controller")
 else:
     print ("\n")
     print ("!!!Demo terminated, reason: %s" % status.brief().lower())