Example #1
0
    def test_parser(self):
        files = [
            'bgp4-open',
            'bgp4-update',
            'bgp4-update_ipv6',
            'bgp4-update_vpnv6',
            'bgp4-keepalive',
            'evpn_esi_arbitrary',
            'evpn_esi_lacp',
            'evpn_esi_l2_bridge',
            'evpn_esi_mac_base',
            'evpn_esi_router_id',
            'evpn_esi_as_based',
            'evpn_nlri_eth_a-d',
            'evpn_nlri_mac_ip_ad',
            'evpn_nlri_inc_multi_eth_tag',
            'evpn_nlri_eth_seg',
            'evpn_nlri_ip_prefix',
        ]

        for f in files:
            LOG.debug('*** testing %s ...', f)
            for _, buf in pcaplib.Reader(
                    open(BGP4_PACKET_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if BGP message can be parsed as expected.
                pkt = packet.Packet(buf)
                ok_(isinstance(pkt.protocols[-1], bgp.BGPMessage),
                    'Failed to parse BGP message: %s' % pkt)

                # Checks if BGP message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #2
0
    def test_pcap(self):
        files = [
            'zebra_v2',
            'zebra_v3',
        ]

        for f in files:
            zebra_pcap_file = os.path.join(PCAP_DATA_DIR, f + '.pcap')
            # print('*** testing %s' % zebra_pcap_file)

            for _, buf in pcaplib.Reader(open(zebra_pcap_file, 'rb')):
                # Checks if Zebra message can be parsed as expected.
                pkt = packet.Packet(buf)
                zebra_pkts = pkt.get_protocols(zebra.ZebraMessage)
                for zebra_pkt in zebra_pkts:
                    ok_(isinstance(zebra_pkt, zebra.ZebraMessage),
                        'Failed to parse Zebra message: %s' % pkt)
                ok_(not isinstance(pkt.protocols[-1],
                                   (six.binary_type, bytearray)),
                    'Some messages could not be parsed: %s' % pkt)

                # Checks if Zebra message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #3
0
    def test_parser(self):
        files = [
            'bgp4-open',
            'bgp4-update',
            'bgp4-update_ipv6',
            'bgp4-update_vpnv6',
            'bgp4-keepalive',
            'evpn_esi_arbitrary',
            'evpn_esi_lacp',
            'evpn_esi_l2_bridge',
            'evpn_esi_mac_base',
            'evpn_esi_router_id',
            'evpn_esi_as_based',
            'evpn_nlri_eth_a-d',
            'evpn_nlri_mac_ip_ad',
            'evpn_nlri_inc_multi_eth_tag',
            'evpn_nlri_eth_seg',
            'evpn_nlri_ip_prefix',
        ]

        for f in files:
            LOG.debug('*** testing %s ...', f)
            for _, buf in pcaplib.Reader(
                    open(BGP4_PACKET_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if BGP message can be parsed as expected.
                pkt = packet.Packet(buf)
                ok_(isinstance(pkt.protocols[-1], bgp.BGPMessage),
                    'Failed to parse BGP message: %s' % pkt)

                # Checks if BGP message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #4
0
    def test_pcap(self):
        files = [
            'zebra_v2',
            'zebra_v3',
            'zebra_v4_frr_v2',  # API version 4 on FRRouting v2.0
        ]

        for f in files:
            zebra_pcap_file = os.path.join(PCAP_DATA_DIR, f + '.pcap')
            # print('*** testing %s' % zebra_pcap_file)

            for _, buf in pcaplib.Reader(open(zebra_pcap_file, 'rb')):
                # Checks if Zebra message can be parsed as expected.
                pkt = packet.Packet(buf)
                zebra_pkts = pkt.get_protocols(zebra.ZebraMessage)
                for zebra_pkt in zebra_pkts:
                    ok_(isinstance(zebra_pkt, zebra.ZebraMessage),
                        'Failed to parse Zebra message: %s' % pkt)
                ok_(not isinstance(pkt.protocols[-1],
                                   (six.binary_type, bytearray)),
                    'Some messages could not be parsed: %s' % pkt)

                # Checks if Zebra message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #5
0
    def test_writer(self):
        files = [
            'rib.20161101.0000_pick.bz2',
            'updates.20161101.0000.bz2',
        ]

        for f in files:
            # print('\n*** testing mrtlib.Writer with %s ...' % f)
            input_file = os.path.join(MRT_DATA_DIR, f)
            input_buf = bz2.BZ2File(input_file, 'rb').read()
            input_records = list(mrtlib.Reader(bz2.BZ2File(input_file, 'rb')))

            counter = 0
            f = io.BytesIO()
            mrt_writer = mrtlib.Writer(f)
            for record in input_records:
                # print('* No.%d\n%s' % (counter, record))
                mrt_writer.write(record)
                counter += 1

            output_buf = f.getvalue()

            eq_(binary_str(input_buf), binary_str(output_buf))

            mrt_writer.close()

            eq_(True, mrt_writer._f.closed)
Example #6
0
    def test_writer(self):
        files = [
            'rib.20161101.0000_pick.bz2',
            'updates.20161101.0000.bz2',
        ]

        for f in files:
            # print('\n*** testing mrtlib.Writer with %s ...' % f)
            input_file = os.path.join(MRT_DATA_DIR, f)
            input_buf = bz2.BZ2File(input_file, 'rb').read()
            input_records = list(mrtlib.Reader(bz2.BZ2File(input_file, 'rb')))

            counter = 0
            f = io.BytesIO()
            mrt_writer = mrtlib.Writer(f)
            for record in input_records:
                # print('* No.%d\n%s' % (counter, record))
                mrt_writer.write(record)
                counter += 1

            output_buf = f.getvalue()

            eq_(binary_str(input_buf), binary_str(output_buf))

            mrt_writer.close()

            eq_(True, mrt_writer._f.closed)
Example #7
0
    def test_parser(self):
        body = zebra.ZebraRedistributeAdd.parse(self.buf, version=3)

        eq_(self.route_type, body.route_type)

        buf = body.serialize(version=3)

        eq_(binary_str(self.buf), binary_str(buf))
Example #8
0
    def test_parser(self):
        body = zebra.ZebraVrfAdd.parse(self.buf)

        eq_(self.vrf_name, body.vrf_name)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #9
0
    def test_parser(self):
        body = zebra.ZebraVrfAdd.parse(self.buf)

        eq_(self.vrf_name, body.vrf_name)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #10
0
    def test_parser(self):
        body = zebra.ZebraRedistributeAdd.parse(self.buf, version=3)

        eq_(self.route_type, body.route_type)

        buf = body.serialize(version=3)

        eq_(binary_str(self.buf), binary_str(buf))
Example #11
0
    def test_parser(self):
        body = zebra.ZebraInterfaceEnableRadv.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.interval, body.interval)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #12
0
    def test_parser(self):
        body = zebra.ZebraInterfaceVrfUpdate.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.vrf_id, body.vrf_id)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
 def test_flowspec_user_interface_vpnv6(self):
     rules = RULES_BASE + [
         # dst_prefix='2001:2/128/32'
         bgp.FlowSpecIPv6DestPrefix(
             addr='2001::2', offset=32, length=128),
         # src_prefix='3002::3/128'
         bgp.FlowSpecIPv6SrcPrefix(
             addr='3002::3', length=128),
         # ip_proto='6'
         bgp.FlowSpecNextHeader(
             operator=bgp.FlowSpecNextHeader.EQ, value=6),
         # fragment='LF'
         bgp.FlowSpecIPv6Fragment(
             operator=0,  # Partial match
             value=bgp.FlowSpecFragment.LF),
         # fragment='==FF'
         bgp.FlowSpecIPv6Fragment(
             operator=bgp.FlowSpecFragment.MATCH,
             value=bgp.FlowSpecFragment.FF),
         # fragment='&==ISF'
         bgp.FlowSpecIPv6Fragment(
             operator=(bgp.FlowSpecFragment.AND |
                       bgp.FlowSpecFragment.MATCH),
             value=bgp.FlowSpecFragment.ISF),
         # fragment='!=LF'
         bgp.FlowSpecIPv6Fragment(
             operator=bgp.FlowSpecFragment.NOT,
             value=bgp.FlowSpecFragment.LF),
         # flowlabel='100'
         bgp.FlowSpecIPv6FlowLabel(
             operator=bgp.FlowSpecIPv6FlowLabel.EQ,
             value=100),
     ]
     msg = bgp.FlowSpecVPNv6NLRI.from_user(
         route_dist='65001:250',
         dst_prefix='2001::2/128/32',
         src_prefix='3002::3/128',
         next_header='6',
         port='>=8000 & <=9000 | ==80',
         dst_port='8080 >9000&<9050 | <=1000',
         src_port='<=9090 & >=9080 <10100 & >10000',
         icmp_type=0,
         icmp_code=6,
         tcp_flags='SYN+ACK & !=URGENT',
         packet_len='1000 & 1100',
         dscp='22 24',
         fragment='LF ==FF&==ISF | !=LF',
         flow_label=100,
     )
     msg2 = bgp.FlowSpecVPNv6NLRI(route_dist='65001:250', rules=rules)
     binmsg = msg.serialize()
     binmsg2 = msg2.serialize()
     eq_(str(msg), str(msg2))
     eq_(binary_str(binmsg), binary_str(binmsg2))
     msg3, rest = bgp.FlowSpecVPNv6NLRI.parser(binmsg)
     eq_(str(msg), str(msg3))
     eq_(rest, b'')
Example #14
0
    def test_parser(self):
        body = zebra.ZebraInterfaceVrfUpdate.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.vrf_id, body.vrf_id)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #15
0
    def test_parser(self):
        body = zebra.ZebraInterfaceEnableRadv.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.interval, body.interval)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #16
0
    def test_parser(self):
        body = zebra.ZebraInterfaceNbrAddressAdd.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #17
0
    def test_parser(self):
        body = zebra.ZebraInterfaceNbrAddressAdd.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #18
0
    def test_parser(self):
        body = zebra.ZebraIPv4NexthopLookupMRib.parse(self.buf)

        eq_(self.addr, body.addr)
        eq_(self.distance, body.distance)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #19
0
    def test_parser(self):
        body = zebra.ZebraIPv4NexthopLookupMRib.parse(self.buf)

        eq_(self.addr, body.addr)
        eq_(self.distance, body.distance)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #20
0
    def test_parser(self):
        body = zebra.ZebraIPv4ImportLookup.parse(self.buf)

        eq_(self.prefix, body.prefix)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))
        eq_(self.from_zebra, body.from_zebra)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #21
0
    def test_parser(self):
        body = zebra.ZebraIPv4ImportLookup.parse(self.buf)

        eq_(self.prefix, body.prefix)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))
        eq_(self.from_zebra, body.from_zebra)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #22
0
    def test_parser(self):
        body = zebra.ZebraNexthopUpdate.parse(self.buf)

        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))
        eq_(self.nexthop_type, body.nexthops[0].type)
        eq_(self.ifindex, body.nexthops[0].ifindex)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #23
0
    def test_parser(self):
        body = zebra.ZebraInterfaceBfdDestinationUpdate.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.dst_family, body.dst_family)
        eq_(self.dst_prefix, body.dst_prefix)
        eq_(self.status, body.status)
        eq_(self.src_family, body.src_family)
        eq_(self.src_prefix, body.src_prefix)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #24
0
    def test_parser(self):
        body = zebra.ZebraInterfaceBfdDestinationUpdate.parse(self.buf)

        eq_(self.ifindex, body.ifindex)
        eq_(self.dst_family, body.dst_family)
        eq_(self.dst_prefix, body.dst_prefix)
        eq_(self.status, body.status)
        eq_(self.src_family, body.src_family)
        eq_(self.src_prefix, body.src_prefix)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #25
0
    def test_parser(self):
        body = zebra.ZebraNexthopUpdate.parse(self.buf)

        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)
        eq_(self.metric, body.metric)
        eq_(self.nexthop_num, len(body.nexthops))
        eq_(self.nexthop_type, body.nexthops[0].type)
        eq_(self.ifindex, body.nexthops[0].ifindex)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #26
0
    def test_parser(self):
        body = zebra.ZebraMplsLabelsAdd.parse(self.buf)

        eq_(self.route_type, body.route_type)
        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)
        eq_(self.gate_addr, body.gate_addr)
        eq_(self.distance, body.distance)
        eq_(self.in_label, body.in_label)
        eq_(self.out_label, body.out_label)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #27
0
    def test_parser(self):
        body = zebra.ZebraMplsLabelsAdd.parse(self.buf)

        eq_(self.route_type, body.route_type)
        eq_(self.family, body.family)
        eq_(self.prefix, body.prefix)
        eq_(self.gate_addr, body.gate_addr)
        eq_(self.distance, body.distance)
        eq_(self.in_label, body.in_label)
        eq_(self.out_label, body.out_label)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
 def test_flowspec_user_interface_vpv4(self):
     rules = RULES_BASE + [
         # dst_prefix='10.0.0.0/24
         bgp.FlowSpecDestPrefix(addr='10.0.0.0', length=24),
         # src_prefix='20.0.0.1/24'
         bgp.FlowSpecSrcPrefix(addr='20.0.0.0', length=24),
         # ip_proto='6'
         bgp.FlowSpecIPProtocol(
             operator=bgp.FlowSpecIPProtocol.EQ, value=6),
         # fragment='LF'
         bgp.FlowSpecFragment(
             operator=0,  # Partial match
             value=bgp.FlowSpecFragment.LF),
         # fragment='==FF'
         bgp.FlowSpecFragment(
             operator=bgp.FlowSpecFragment.MATCH,
             value=bgp.FlowSpecFragment.FF),
         # fragment='&==ISF'
         bgp.FlowSpecFragment(
             operator=(bgp.FlowSpecFragment.AND |
                       bgp.FlowSpecFragment.MATCH),
             value=bgp.FlowSpecFragment.ISF),
         # fragment='!=DF'
         bgp.FlowSpecFragment(
             operator=bgp.FlowSpecFragment.NOT,
             value=bgp.FlowSpecFragment.DF)
     ]
     msg = bgp.FlowSpecVPNv4NLRI.from_user(
         route_dist='65001:250',
         dst_prefix='10.0.0.0/24',
         src_prefix='20.0.0.0/24',
         ip_proto='6',
         port='>=8000 & <=9000 | ==80',
         dst_port='8080 >9000&<9050 | <=1000',
         src_port='<=9090 & >=9080 <10100 & >10000',
         icmp_type=0,
         icmp_code=6,
         tcp_flags='SYN+ACK & !=URGENT',
         packet_len='1000 & 1100',
         dscp='22 24',
         fragment='LF ==FF&==ISF | !=DF')
     msg2 = bgp.FlowSpecVPNv4NLRI(route_dist='65001:250', rules=rules)
     binmsg = msg.serialize()
     binmsg2 = msg2.serialize()
     eq_(str(msg), str(msg2))
     eq_(binary_str(binmsg), binary_str(binmsg2))
     msg3, rest = bgp.FlowSpecVPNv4NLRI.parser(binmsg)
     eq_(str(msg), str(msg3))
     eq_(rest, b'')
Example #29
0
    def test_parser(self):
        body = zebra.ZebraBfdDestinationDeregister.parse(self.buf)

        eq_(self.pid, body.pid)
        eq_(self.dst_family, body.dst_family)
        eq_(self.dst_prefix, body.dst_prefix)
        eq_(self.multi_hop, body.multi_hop)
        eq_(self.src_family, body.src_family)
        eq_(self.src_prefix, body.src_prefix)
        eq_(self.multi_hop_count, body.multi_hop_count)
        eq_(self.ifname, body.ifname)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #30
0
    def test_parser(self):
        body = zebra.ZebraBfdDestinationDeregister.parse(self.buf)

        eq_(self.pid, body.pid)
        eq_(self.dst_family, body.dst_family)
        eq_(self.dst_prefix, body.dst_prefix)
        eq_(self.multi_hop, body.multi_hop)
        eq_(self.src_family, body.src_family)
        eq_(self.src_prefix, body.src_prefix)
        eq_(self.multi_hop_count, body.multi_hop_count)
        eq_(self.ifname, body.ifname)

        buf = body.serialize()

        eq_(binary_str(self.buf), binary_str(buf))
Example #31
0
 def test_binary_str_string(self):
     """
     Test binary_str() with str type.
     """
     expected_result = '\\x01\\x02\\x03\\x04'
     data = b'\x01\x02\x03\x04'
     eq_(expected_result, utils.binary_str(data))
Example #32
0
 def test_binary_str_bytearray(self):
     """
     Test binary_str() with bytearray type.
     """
     expected_result = '\\x01\\x02\\x03\\x04'
     data = bytearray(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.binary_str(data))
Example #33
0
 def test_binary_str_string(self):
     """
     Test binary_str() with str type.
     """
     expected_result = '\\x01\\x02\\x03\\x04'
     data = b'\x01\x02\x03\x04'
     eq_(expected_result, utils.binary_str(data))
Example #34
0
 def test_binary_str_bytearray(self):
     """
     Test binary_str() with bytearray type.
     """
     expected_result = '\\x01\\x02\\x03\\x04'
     data = bytearray(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.binary_str(data))
Example #35
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     ofp = msg.datapath.ofproto
     self.logger.debug(
         "EventOFPErrorMsg received.\n"
         "version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
         " `-- msg_type: %s\n"
         "OFPErrorMsg(type=%s, code=%s, data=b'%s')\n"
         " |-- type: %s\n"
         " |-- code: %s", hex(msg.version), hex(msg.msg_type),
         hex(msg.msg_len), hex(msg.xid),
         ofp.ofp_msg_type_to_str(msg.msg_type), hex(msg.type),
         hex(msg.code), utils.binary_str(msg.data),
         ofp.ofp_error_type_to_str(msg.type),
         ofp.ofp_error_code_to_str(msg.type, msg.code))
     if len(msg.data) >= ofp.OFP_HEADER_SIZE:
         (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
         self.logger.debug(
             " `-- data: version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
             "     `-- msg_type: %s", hex(version), hex(msg_type),
             hex(msg_len), hex(xid), ofp.ofp_msg_type_to_str(msg_type))
     else:
         self.logger.warning(
             "The data field sent from the switch is too short: "
             "len(msg.data) < OFP_HEADER_SIZE\n"
             "The OpenFlow Spec says that the data field should contain "
             "at least 64 bytes of the failed request.\n"
             "Please check the settings or implementation of your switch.")
Example #36
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     ofp = msg.datapath.ofproto
     self.logger.debug(
         "EventOFPErrorMsg received.\n"
         "version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
         " `-- msg_type: %s\n"
         "OFPErrorMsg(type=%s, code=%s, data=b'%s')\n"
         " |-- type: %s\n"
         " |-- code: %s",
         hex(msg.version), hex(msg.msg_type), hex(msg.msg_len),
         hex(msg.xid), ofp.ofp_msg_type_to_str(msg.msg_type),
         hex(msg.type), hex(msg.code), utils.binary_str(msg.data),
         ofp.ofp_error_type_to_str(msg.type),
         ofp.ofp_error_code_to_str(msg.type, msg.code))
     if len(msg.data) >= ofp.OFP_HEADER_SIZE:
         (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
         self.logger.debug(
             " `-- data: version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
             "     `-- msg_type: %s",
             hex(version), hex(msg_type), hex(msg_len), hex(xid),
             ofp.ofp_msg_type_to_str(msg_type))
     else:
         self.logger.warning(
             "The data field sent from the switch is too short: "
             "len(msg.data) < OFP_HEADER_SIZE\n"
             "The OpenFlow Spec says that the data field should contain "
             "at least 64 bytes of the failed request.\n"
             "Please check the settings or implementation of your switch.")
Example #37
0
 def test_binary_str_bytes(self):
     """
     Test binary_str() with bytes type. (Python3 only)
     """
     if six.PY2:
         return
     expected_result = '\\x01\\x02\\x03\\x04'
     data = bytes(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.binary_str(data))
Example #38
0
 def test_binary_str_bytes(self):
     """
     Test binary_str() with bytes type. (Python3 only)
     """
     if six.PY2:
         return
     expected_result = '\\x01\\x02\\x03\\x04'
     data = bytes(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.binary_str(data))
    def _test_pcap_single(f):
        zebra_pcap_file = os.path.join(PCAP_DATA_DIR, f + '.pcap')
        # print('*** testing %s' % zebra_pcap_file)

        for _, buf in pcaplib.Reader(open(zebra_pcap_file, 'rb')):
            # Checks if Zebra message can be parsed as expected.
            pkt = packet.Packet(buf)
            zebra_pkts = pkt.get_protocols(zebra.ZebraMessage)
            for zebra_pkt in zebra_pkts:
                ok_(isinstance(zebra_pkt, zebra.ZebraMessage),
                    'Failed to parse Zebra message: %s' % pkt)
            ok_(not isinstance(pkt.protocols[-1],
                               (six.binary_type, bytearray)),
                'Some messages could not be parsed in %s: %s' % (f, pkt))

            # Checks if Zebra message can be serialized as expected.
            pkt.serialize()
            eq_(binary_str(buf), binary_str(pkt.data))
Example #40
0
    def test_parser(self):
        files = [
            'geneve_unknown',
        ]

        for f in files:
            # print('*** testing %s ...' % f)
            for _, buf in pcaplib.Reader(
                    open(GENEVE_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if message can be parsed as expected.
                pkt = packet.Packet(buf)
                geneve_pkt = pkt.get_protocol(geneve.geneve)
                ok_(isinstance(geneve_pkt, geneve.geneve),
                    'Failed to parse Geneve message: %s' % pkt)

                # Checks if message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #41
0
    def test_pcap(self):
        files = [
            'openflow_flowmod',
            'openflow_flowstats_req',
            'openflow_invalid_version',
        ]

        for f in files:
            # print('*** testing %s ...' % f)
            for _, buf in pcaplib.Reader(
                    open(OPENFLOW_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if message can be parsed as expected.
                pkt = packet.Packet(buf)
                openflow_pkt = pkt.get_protocol(openflow.openflow)
                ok_(isinstance(openflow_pkt, openflow.openflow),
                    'Failed to parse OpenFlow message: %s' % pkt)

                # Checks if message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #42
0
    def test_pcap(self):
        files = [
            'openflow_flowmod',
            'openflow_flowstats_req',
            'openflow_invalid_version',
        ]

        for f in files:
            # print('*** testing %s ...' % f)
            for _, buf in pcaplib.Reader(
                    open(OPENFLOW_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if message can be parsed as expected.
                pkt = packet.Packet(buf)
                openflow_pkt = pkt.get_protocol(openflow.openflow)
                ok_(isinstance(openflow_pkt, openflow.openflow),
                    'Failed to parse OpenFlow message: %s' % pkt)

                # Checks if message can be serialized as expected.
                pkt.serialize()
                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
Example #43
0
    def test_parser(self):
        files = [
            'gre_full_options',
            'gre_no_option',
            'gre_nvgre_option',
        ]

        for f in files:
            # print('*** testing %s ...' % f)
            for _, buf in pcaplib.Reader(
                    open(GENEVE_DATA_DIR + f + '.pcap', 'rb')):
                # Checks if message can be parsed as expected.
                pkt = packet.Packet(buf)
                gre_pkt = pkt.get_protocol(gre.gre)
                ok_(isinstance(gre_pkt, gre.gre),
                    'Failed to parse Gre message: %s' % pkt)

                # Checks if message can be serialized as expected.
                pkt.serialize()

                eq_(buf, pkt.data,
                    "b'%s' != b'%s'" % (binary_str(buf), binary_str(pkt.data)))
 def test_flowspec_user_interface_l2vpn(self):
     rules = RULES_L2VPN_BASE
     msg = bgp.FlowSpecL2VPNNLRI.from_user(
         route_dist='65001:250',
         ether_type=0x0800,
         src_mac='12:34:56:78:90:AB',
         dst_mac='BE:EF:C0:FF:EE:DD',
         llc_dsap=0x42,
         llc_ssap=0x42,
         llc_control=100,
         snap=0x12345,
         vlan_id='>4000',
         vlan_cos='>=3',
         inner_vlan_id='<3000',
         inner_vlan_cos='<=5',
     )
     msg2 = bgp.FlowSpecL2VPNNLRI(route_dist='65001:250', rules=rules)
     binmsg = msg.serialize()
     binmsg2 = msg2.serialize()
     eq_(str(msg), str(msg2))
     eq_(binary_str(binmsg), binary_str(binmsg2))
     msg3, rest = bgp.FlowSpecL2VPNNLRI.parser(binmsg)
     eq_(str(msg), str(msg3))
     eq_(rest, b'')
Example #45
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     ofp = msg.datapath.ofproto
     (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
     self.logger.debug('EventOFPErrorMsg received.')
     self.logger.debug('version=%s, msg_type=%s, msg_len=%s, xid=%s',
                       hex(msg.version), hex(msg.msg_type),
                       hex(msg.msg_len), hex(msg.xid))
     self.logger.debug(' `-- msg_type: %s',
                       ofp.ofp_msg_type_to_str(msg.msg_type))
     self.logger.debug("OFPErrorMsg(type=%s, code=%s, data=b'%s')",
                       hex(msg.type), hex(msg.code),
                       utils.binary_str(msg.data))
     self.logger.debug(' |-- type: %s', ofp.ofp_error_type_to_str(msg.type))
     self.logger.debug(' |-- code: %s',
                       ofp.ofp_error_code_to_str(msg.type, msg.code))
     self.logger.debug(
         ' `-- data: version=%s, msg_type=%s, msg_len=%s, xid=%s',
         hex(version), hex(msg_type), hex(msg_len), hex(xid))
     self.logger.debug('     `-- msg_type: %s',
                       ofp.ofp_msg_type_to_str(msg_type))
Example #46
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     ofp = msg.datapath.ofproto
     (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
     self.logger.debug('EventOFPErrorMsg received.')
     self.logger.debug(
         'version=%s, msg_type=%s, msg_len=%s, xid=%s', hex(msg.version),
         hex(msg.msg_type), hex(msg.msg_len), hex(msg.xid))
     self.logger.debug(
         ' `-- msg_type: %s', ofp.ofp_msg_type_to_str(msg.msg_type))
     self.logger.debug(
         "OFPErrorMsg(type=%s, code=%s, data=b'%s')", hex(msg.type),
         hex(msg.code), utils.binary_str(msg.data))
     self.logger.debug(
         ' |-- type: %s', ofp.ofp_error_type_to_str(msg.type))
     self.logger.debug(
         ' |-- code: %s', ofp.ofp_error_code_to_str(msg.type, msg.code))
     self.logger.debug(
         ' `-- data: version=%s, msg_type=%s, msg_len=%s, xid=%s',
         hex(version), hex(msg_type), hex(msg_len), hex(xid))
     self.logger.debug(
         '     `-- msg_type: %s', ofp.ofp_msg_type_to_str(msg_type))
Example #47
0
 def test_serialize_with_big_endian(self):
     buf = self.hdr.serialize()
     eq_(binary_str(self.buf_big), binary_str(buf))
Example #48
0
 def test_serialize_with_little_endian(self):
     buf = self.hdr.serialize()
     eq_(binary_str(self.buf_little), binary_str(buf))
Example #49
0
    def test_user_interface(self):
        rules = [
            # dst_prefix='10.0.0.0/24
            bgp.FlowSpecDestPrefix(addr='10.0.0.0', length=24),
            # src_prefix='20.0.0.1/24'
            bgp.FlowSpecSrcPrefix(addr='20.0.0.0', length=24),
            # ip_proto='6'
            bgp.FlowSpecIPProtocol(operator=bgp.FlowSpecIPProtocol.EQ,
                                   value=6),
            # port='>=8000'
            bgp.FlowSpecPort(operator=(bgp.FlowSpecPort.GT
                                       | bgp.FlowSpecPort.EQ),
                             value=8000),
            # port='&<=9000'
            bgp.FlowSpecPort(
                operator=(bgp.FlowSpecPort.AND | bgp.FlowSpecPort.LT
                          | bgp.FlowSpecPort.EQ),
                value=9000),
            # port='==80'
            bgp.FlowSpecPort(operator=bgp.FlowSpecPort.EQ, value=80),
            # dst_port=8080
            bgp.FlowSpecDestPort(operator=bgp.FlowSpecDestPort.EQ, value=8080),
            # dst_port='>9000'
            bgp.FlowSpecDestPort(operator=bgp.FlowSpecDestPort.GT, value=9000),
            # dst_port='&<9050'
            bgp.FlowSpecDestPort(operator=(bgp.FlowSpecDestPort.AND
                                           | bgp.FlowSpecDestPort.LT),
                                 value=9050),
            # dst_port='<=1000'
            bgp.FlowSpecDestPort(operator=(bgp.FlowSpecDestPort.LT
                                           | bgp.FlowSpecDestPort.EQ),
                                 value=1000),
            # src_port='<=9090'
            bgp.FlowSpecSrcPort(operator=(bgp.FlowSpecSrcPort.LT
                                          | bgp.FlowSpecSrcPort.EQ),
                                value=9090),
            # src_port='& >=9080'
            bgp.FlowSpecSrcPort(
                operator=(bgp.FlowSpecSrcPort.AND | bgp.FlowSpecSrcPort.GT
                          | bgp.FlowSpecSrcPort.EQ),
                value=9080),
            # src_port='<10100'
            bgp.FlowSpecSrcPort(operator=bgp.FlowSpecSrcPort.LT, value=10100),
            # src_port='>10000'
            bgp.FlowSpecSrcPort(operator=(bgp.FlowSpecSrcPort.AND
                                          | bgp.FlowSpecSrcPort.GT),
                                value=10000),
            # icmp_type=0
            bgp.FlowSpecIcmpType(operator=bgp.FlowSpecIcmpType.EQ, value=0),
            # icmp_code=6
            bgp.FlowSpecIcmpCode(operator=bgp.FlowSpecIcmpCode.EQ, value=6),
            # tcp_flags='ACK+FIN'
            bgp.FlowSpecTCPFlags(
                operator=0,  # Partial match
                value=(bgp.FlowSpecTCPFlags.SYN | bgp.FlowSpecTCPFlags.ACK)),
            # tcp_flags='&!=URGENT'
            bgp.FlowSpecTCPFlags(operator=(bgp.FlowSpecTCPFlags.AND
                                           | bgp.FlowSpecTCPFlags.NOT),
                                 value=bgp.FlowSpecTCPFlags.URGENT),
            # packet_len=1000
            bgp.FlowSpecPacketLen(operator=bgp.FlowSpecPacketLen.EQ,
                                  value=1000),
            # packet_len=1100
            bgp.FlowSpecPacketLen(operator=(bgp.FlowSpecTCPFlags.AND
                                            | bgp.FlowSpecPacketLen.EQ),
                                  value=1100),
            # dscp=22
            bgp.FlowSpecDSCP(operator=bgp.FlowSpecDSCP.EQ, value=22),
            # dscp=24
            bgp.FlowSpecDSCP(operator=bgp.FlowSpecDSCP.EQ, value=24),
            # fragment='LF'
            bgp.FlowSpecFragment(
                operator=0,  # Partial match
                value=bgp.FlowSpecFragment.LF),
            # fragment='==FF'
            bgp.FlowSpecFragment(operator=bgp.FlowSpecFragment.MATCH,
                                 value=bgp.FlowSpecFragment.FF),
            # fragment='&==ISF'
            bgp.FlowSpecFragment(operator=(bgp.FlowSpecFragment.AND
                                           | bgp.FlowSpecFragment.MATCH),
                                 value=bgp.FlowSpecFragment.ISF),
            # fragment='!=DF'
            bgp.FlowSpecFragment(operator=bgp.FlowSpecFragment.NOT,
                                 value=bgp.FlowSpecFragment.DF)
        ]

        msg = bgp.FlowSpecIPv4NLRI.from_user(
            dst_prefix='10.0.0.0/24',
            src_prefix='20.0.0.0/24',
            ip_proto='6',
            port='>=8000 & <=9000 | ==80',
            dst_port='8080 >9000&<9050 | <=1000',
            src_port='<=9090 & >=9080 <10100 & >10000',
            icmp_type=0,
            icmp_code=6,
            tcp_flags='SYN+ACK & !=URGENT',
            packet_len='1000 & 1100',
            dscp='22 24',
            fragment='LF ==FF&==ISF | !=DF')
        msg2 = bgp.FlowSpecIPv4NLRI(rules=rules)
        binmsg = msg.serialize()
        binmsg2 = msg2.serialize()
        eq_(str(msg), str(msg2))
        eq_(binary_str(binmsg), binary_str(binmsg2))
        msg3, rest = bgp.FlowSpecIPv4NLRI.parser(binmsg)
        eq_(str(msg), str(msg3))
        eq_(rest, b'')

        msg = bgp.FlowSpecVPNv4NLRI.from_user(
            route_dist='65001:250',
            dst_prefix='10.0.0.0/24',
            src_prefix='20.0.0.0/24',
            ip_proto='6',
            port='>=8000 & <=9000 | ==80',
            dst_port='8080 >9000&<9050 | <=1000',
            src_port='<=9090 & >=9080 <10100 & >10000',
            icmp_type=0,
            icmp_code=6,
            tcp_flags='SYN+ACK & !=URGENT',
            packet_len='1000 & 1100',
            dscp='22 24',
            fragment='LF ==FF&==ISF | !=DF')
        msg2 = bgp.FlowSpecVPNv4NLRI(route_dist='65001:250', rules=rules)
        binmsg = msg.serialize()
        binmsg2 = msg2.serialize()
        eq_(str(msg), str(msg2))
        eq_(binary_str(binmsg), binary_str(binmsg2))
        msg3, rest = bgp.FlowSpecVPNv4NLRI.parser(binmsg)
        eq_(str(msg), str(msg3))
        eq_(rest, b'')