Esempio n. 1
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))
Esempio n. 2
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))
Esempio n. 3
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))
Esempio n. 4
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))
Esempio n. 5
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))
Esempio n. 6
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))
Esempio n. 7
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))
Esempio n. 8
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))
Esempio n. 9
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))
Esempio n. 10
0
 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'')
Esempio n. 11
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))
Esempio n. 12
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))
Esempio n. 13
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))
Esempio n. 14
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))
Esempio n. 15
0
 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'')
Esempio n. 16
0
    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))
Esempio n. 17
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))
Esempio n. 18
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)))
Esempio n. 19
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',
            'flowspec_nlri_ipv4',
            'flowspec_nlri_vpn4',
            'flowspec_nlri_ipv6',
            'flowspec_nlri_vpn6',
            'flowspec_nlri_l2vpn',
            'flowspec_action_traffic_rate',
            'flowspec_action_traffic_action',
            'flowspec_action_redirect',
            'flowspec_action_traffic_marking',
        ]

        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)))
Esempio n. 20
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)))
Esempio n. 21
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", hex(msg.version), hex(msg.msg_type),
         hex(msg.msg_len), hex(msg.xid),
         ofp.ofp_msg_type_to_str(msg.msg_type))
     if msg.type == ofp.OFPET_EXPERIMENTER:
         self.logger.debug(
             "OFPErrorExperimenterMsg(type=%s, exp_type=%s,"
             " experimenter=%s, data=b'%s')", hex(msg.type),
             hex(msg.exp_type), hex(msg.experimenter),
             utils.binary_str(msg.data))
     else:
         self.logger.debug(
             "OFPErrorMsg(type=%s, code=%s, data=b'%s')\n"
             " |-- type: %s\n"
             " |-- code: %s", 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 msg.type == ofp.OFPET_HELLO_FAILED:
         self.logger.debug(" `-- data: %s", msg.data.decode('ascii'))
     elif 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.")
Esempio n. 22
0
 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'')