コード例 #1
0
ファイル: message_unittests.py プロジェクト: CPqD/oftest
 def runTest(self):
     msg = message.flow_stats_entry()
     match = ofp.ofp_match()
     match.wildcards &= ~ofp.OFPFW_IN_PORT
     act = action.action_output()
     act.port = 3
     msg.match = match
     pkt = msg.pack()
     self.assertEqual(len(pkt), 136)
     inst = instruction.instruction_apply_actions()
     self.assertTrue(inst.actions.add(act), "Could not add action")
     self.assertTrue(msg.instructions.add(inst), "Could not add instructions")
     #self.assertTrue(msg.actions.add(act), "Could not add action")
     pkt = msg.pack()
     # 160 = 136 for flow_stats_entry and 24 for instruction_list
     self.assertEqual(len(pkt), 160)
     rep = message.flow_stats_reply()
     self.assertEqual(len(rep.pack()),12)
     rep.stats.append(msg)
     self.assertEqual(len(rep.pack()),172)
コード例 #2
0
ファイル: parse.py プロジェクト: CPqD/oftest
def packet_to_flow_match(packet, pkt_format="L2"):
    """
    Create a flow match that matches packet with the given wildcards

    @param packet The packet to use as a flow template
    @param pkt_format Currently only L2 is supported.  Will indicate the 
    overall packet type for parsing
    @return An ofp_match object if successful.  None if format is not
    recognized.  The wildcards of the match will be cleared for the
    values extracted from the packet.

    @todo check min length of packet
    @todo Check if packet is other than L2 format
    @todo Implement ICMP and ARP fields
    """

    #@todo check min length of packet
    if pkt_format.upper() != "L2":
        parse_logger.error("Only L2 supported for packet_to_flow")
        return None

    if type(packet) == type(""):
        ether = scapy.Ether(packet)
    else:
        ether = packet

    # For now, assume ether IP packet and ignore wildcards
    try:
        (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether)
    except:
        parse_logger.error("packet_to_flow_match: Classify error")
        return None

    match = cstruct.ofp_match()
    match.wildcards = cstruct.OFPFW_ALL
    #@todo Check if packet is other than L2 format
    match.dl_dst = parse_mac(ether.dst)
    match.wildcards &= ~cstruct.OFPFW_DL_DST
    match.dl_src = parse_mac(ether.src)
    match.wildcards &= ~cstruct.OFPFW_DL_SRC
    match.dl_type = ether.type
    match.wildcards &= ~cstruct.OFPFW_DL_TYPE

    if dot1q:
        match.dl_vlan = dot1q.vlan
        match.dl_vlan_pcp = dot1q.prio
        match.dl_type = dot1q.type
    else:
        match.dl_vlan = cstruct.OFP_VLAN_NONE
        match.dl_vlan_pcp = 0
    match.wildcards &= ~cstruct.OFPFW_DL_VLAN
    match.wildcards &= ~cstruct.OFPFW_DL_VLAN_PCP

    if ip:
        match.nw_src = parse_ip(ip.src)
        match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK
        match.nw_dst = parse_ip(ip.dst)
        match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK
        match.nw_tos = ip.tos
        match.wildcards &= ~cstruct.OFPFW_NW_TOS

    if tcp:
        match.nw_proto = 6
        match.wildcards &= ~cstruct.OFPFW_NW_PROTO
    elif not tcp and udp:
        tcp = udp
        match.nw_proto = 17
        match.wildcards &= ~cstruct.OFPFW_NW_PROTO

    if tcp:
        match.tp_src = tcp.sport
        match.wildcards &= ~cstruct.OFPFW_TP_SRC
        match.tp_dst = tcp.dport
        match.wildcards &= ~cstruct.OFPFW_TP_DST

    if icmp:
        match.nw_proto = 1
        match.tp_src = icmp.type
        match.tp_dst = icmp.code
        match.wildcards &= ~cstruct.OFPFW_NW_PROTO

    if arp:
        match.nw_proto = arp.op
        match.wildcards &= ~cstruct.OFPFW_NW_PROTO
        match.nw_src = parse_ip(arp.psrc)
        match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK
        match.nw_dst = parse_ip(arp.pdst)
        match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK

    return match
コード例 #3
0
ファイル: message_unittests.py プロジェクト: CPqD/oftest
 def runTest(self):
     match = ofp.ofp_match()
     self.assertEqual(len(match.pack()), 88)