Ejemplo n.º 1
0
    def _verify_acl_traffic(self, setup, direction, ptfadapter, pkt, dropped,
                            ip_version):
        exp_pkt = self.expected_mask_routed_packet(pkt, ip_version)

        if ip_version == "ipv4":
            downstream_dst_port = DOWNSTREAM_IP_PORT_MAP.get(
                pkt[packet.IP].dst)
        else:
            downstream_dst_port = DOWNSTREAM_IP_PORT_MAP.get(
                pkt[packet.IPv6].dst)
        ptfadapter.dataplane.flush()
        testutils.send(ptfadapter, self.src_port, pkt)
        if direction == "uplink->downlink" and downstream_dst_port:
            if dropped:
                testutils.verify_no_packet(ptfadapter, exp_pkt,
                                           downstream_dst_port)
            else:
                testutils.verify_packet(ptfadapter, exp_pkt,
                                        downstream_dst_port)
        else:
            if dropped:
                testutils.verify_no_packet_any(ptfadapter,
                                               exp_pkt,
                                               ports=self.get_dst_ports(
                                                   setup, direction))
            else:
                testutils.verify_packet_any_port(ptfadapter,
                                                 exp_pkt,
                                                 ports=self.get_dst_ports(
                                                     setup, direction),
                                                 timeout=20)
Ejemplo n.º 2
0
def run_test_ipv4(ptfadapter, facts):
    logger.info("Running test with ipv4 packets")
    pkt = testutils.simple_udp_packet(eth_dst=facts['src_router_mac'],
                                      eth_src=facts['src_host_mac'],
                                      ip_src=facts['dst_host_ipv4'],
                                      ip_dst=facts['dst_host_ipv4'],
                                      ip_ttl=DEFAULT_HLIM_TTL)
    logger.info("\nSend Packet:\neth_dst: {}, eth_src: {}, ipv4 ip: {}".format(
        facts['src_router_mac'], facts['src_host_mac'],
        facts['dst_host_ipv4']))

    testutils.send(ptfadapter, facts['src_port_ids'][0], pkt)

    exp_pkt = testutils.simple_udp_packet(eth_dst=facts['dst_host_mac'],
                                          eth_src=facts['dst_router_mac'],
                                          ip_src=facts['dst_host_ipv4'],
                                          ip_dst=facts['dst_host_ipv4'],
                                          ip_ttl=DEFAULT_HLIM_TTL - 1)
    logger.info(
        "\nExpect Packet:\neth_dst: {}, eth_src: {}, ipv4 ip: {}".format(
            facts['dst_host_mac'], facts['dst_router_mac'],
            facts['dst_host_ipv4']))

    testutils.verify_packet_any_port(ptfadapter,
                                     exp_pkt,
                                     facts['dst_port_ids'],
                                     timeout=WAIT_EXPECTED_PACKET_TIMEOUT)
Ejemplo n.º 3
0
def generate_and_verify_traffic(duthost, ptfadapter, ip_dst, expected_ports, ipv6=False):
    if ipv6:
        pkt = testutils.simple_tcpv6_packet(
            eth_dst=duthost.facts["router_mac"],
            eth_src=ptfadapter.dataplane.get_mac(0, 0),
            ipv6_src='2001:db8:85a3::8a2e:370:7334',
            ipv6_dst=ip_dst,
            ipv6_hlim=64,
            tcp_sport=1234,
            tcp_dport=4321)
    else:
        pkt = testutils.simple_tcp_packet(
            eth_dst=duthost.facts["router_mac"],
            eth_src=ptfadapter.dataplane.get_mac(0, 0),
            ip_src='1.1.1.1',
            ip_dst=ip_dst,
            ip_ttl=64,
            tcp_sport=1234,
            tcp_dport=4321)

    exp_pkt = pkt.copy()
    exp_pkt = mask.Mask(exp_pkt)
    exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst')
    exp_pkt.set_do_not_care_scapy(packet.Ether, 'src')
    if ipv6:
        exp_pkt.set_do_not_care_scapy(packet.IPv6, 'hlim')
        exp_pkt.set_do_not_care_scapy(packet.IPv6, 'chksum')
    else:
        exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl')
        exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum')

    testutils.send(ptfadapter, 5, pkt)
    testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=expected_ports)
def send_and_verify_packet(ptfadapter, packet, expected_packet, tx_port, rx_ports, exp_recv):
    ptfadapter.dataplane.flush()
    testutils.send(ptfadapter, pkt=packet, port_id=tx_port)
    if exp_recv:
        testutils.verify_packet_any_port(ptfadapter, pkt=expected_packet, ports=rx_ports, timeout=5)
    else:
        testutils.verify_no_packet_any(ptfadapter, pkt=expected_packet, ports=rx_ports)
Ejemplo n.º 5
0
def send_recv_eth(ptfadapter, source_ports, source_mac, dest_ports, dest_mac, src_vlan, dst_vlan):
    """
    send ethernet packet and verify it on dest_port
    :param ptfadapter: PTF adapter object
    :param source_port: source port
    :param source_mac: source MAC
    :param dest_port: destination port to receive packet on
    :param dest_mac: destination MAC
    :param vlan_id: VLAN id
    :return:
    """
    pkt = simple_eth_packet(
        eth_dst=dest_mac,
        eth_src=source_mac,
        vlan_vid=src_vlan
    )
    exp_pkt = simple_eth_packet(
        eth_dst=dest_mac,
        eth_src=source_mac,
        vlan_vid=dst_vlan
    )
    if dst_vlan:
        # expect to receive tagged packet:
        # sonic device might modify the 802.1p field,
        # need to use Mask to ignore the priority field.
        exp_pkt = Mask(exp_pkt)
        exp_pkt.set_do_not_care_scapy(scapy.Dot1Q, "prio")
    logger.debug('send packet src port {} smac: {} dmac: {} vlan: {} verifying on dst port {}'.format(
        source_ports, source_mac, dest_mac, src_vlan, dest_ports))
    testutils.send(ptfadapter, source_ports[0], pkt)
    testutils.verify_packet_any_port(ptfadapter, exp_pkt, dest_ports, timeout=FDB_WAIT_EXPECTED_PACKET_TIMEOUT)
Ejemplo n.º 6
0
def send_recv_ping_packet(ptfadapter, ptf_send_port, ptf_recv_ports, dst_mac,
                          src_ip, dst_ip):
    pkt = testutils.simple_icmp_packet(eth_dst=dst_mac,
                                       ip_src=src_ip,
                                       ip_dst=dst_ip,
                                       icmp_type=8,
                                       icmp_data="")

    ext_pkt = pkt.copy()
    ext_pkt['Ether'].src = dst_mac

    masked_exp_pkt = Mask(ext_pkt)
    masked_exp_pkt.set_do_not_care_scapy(scapy.Ether, "dst")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "tos")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "len")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "id")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "flags")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "frag")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "ttl")
    masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "chksum")

    masked_exp_pkt.set_do_not_care_scapy(scapy.ICMP, "code")
    masked_exp_pkt.set_do_not_care_scapy(scapy.ICMP, "chksum")
    masked_exp_pkt.set_do_not_care_scapy(scapy.ICMP, "id")
    masked_exp_pkt.set_do_not_care_scapy(scapy.ICMP, "seq")

    logger.info(
        'send ping request packet send port {}, recv port {}, dmac: {}, dip: {}'
        .format(ptf_send_port, ptf_recv_ports, dst_mac, dst_ip))
    testutils.send(ptfadapter, ptf_send_port, pkt)
    testutils.verify_packet_any_port(ptfadapter,
                                     masked_exp_pkt,
                                     ptf_recv_ports,
                                     timeout=WAIT_EXPECTED_PACKET_TIMEOUT)
Ejemplo n.º 7
0
def run_test_ipv4(ptfadapter, gather_facts):
    logger.info("Running test with ipv4 packets")
    dst_host_ipv4 = str(
        ip_address(unicode(gather_facts['dst_router_ipv4'])) + 1)
    pkt = testutils.simple_udp_packet(eth_dst=gather_facts['src_router_mac'],
                                      eth_src=gather_facts['src_host_mac'],
                                      ip_src=dst_host_ipv4,
                                      ip_dst=dst_host_ipv4,
                                      ip_ttl=DEFAULT_HLIM_TTL)
    logger.info("\nSend Packet:\neth_dst: {}, eth_src: {}, ipv6 ip: {}".format(
        gather_facts['src_router_mac'], gather_facts['src_host_mac'],
        dst_host_ipv4))

    testutils.send(ptfadapter, int(gather_facts['src_port_ids'][0]), pkt)

    pkt = testutils.simple_udp_packet(eth_dst=gather_facts['dst_host_mac'],
                                      eth_src=gather_facts['dst_router_mac'],
                                      ip_src=dst_host_ipv4,
                                      ip_dst=dst_host_ipv4,
                                      ip_ttl=DEFAULT_HLIM_TTL - 1)
    logger.info(
        "\nExpect Packet:\neth_dst: {}, eth_src: {}, ipv6 ip: {}".format(
            gather_facts['dst_host_mac'], gather_facts['dst_router_mac'],
            dst_host_ipv4))

    port_list = [int(port) for port in gather_facts['dst_port_ids']]
    testutils.verify_packet_any_port(ptfadapter,
                                     pkt,
                                     port_list,
                                     timeout=WAIT_EXPECTED_PACKET_TIMEOUT)
Ejemplo n.º 8
0
def send_recv_eth(duthost, ptfadapter, source_ports, source_mac, dest_ports,
                  dest_mac, src_vlan, dst_vlan):
    """
    send ethernet packet and verify it on dest_port
    :param ptfadapter: PTF adapter object
    :param source_port: source port
    :param source_mac: source MAC
    :param dest_port: destination port to receive packet on
    :param dest_mac: destination MAC
    :param vlan_id: VLAN id
    :return:
    """
    pkt = simple_eth_packet(eth_dst=dest_mac,
                            eth_src=source_mac,
                            vlan_vid=src_vlan)
    exp_pkt = simple_eth_packet(eth_dst=dest_mac,
                                eth_src=source_mac,
                                vlan_vid=dst_vlan)
    if dst_vlan:
        # expect to receive tagged packet:
        # sonic device might modify the 802.1p field,
        # need to use Mask to ignore the priority field.
        exp_pkt = Mask(exp_pkt)
        exp_pkt.set_do_not_care_scapy(scapy.Dot1Q, "prio")
    logger.debug(
        'send packet src port {} smac: {} dmac: {} vlan: {} verifying on dst port {}'
        .format(source_ports, source_mac, dest_mac, src_vlan, dest_ports))

    # fdb test will send lots of pkts between paired ports, it's hard to guarantee there is no congestion
    # on server side during this period. So tolerant to retry 3 times before complain the assert.

    retry_count = 3
    for _ in range(retry_count):
        try:
            ptfadapter.dataplane.flush()
            testutils.send(ptfadapter, source_ports[0], pkt)
            if len(dest_ports) == 1:
                testutils.verify_packet(
                    ptfadapter,
                    exp_pkt,
                    dest_ports[0],
                    timeout=FDB_WAIT_EXPECTED_PACKET_TIMEOUT)
            else:
                testutils.verify_packet_any_port(
                    ptfadapter,
                    exp_pkt,
                    dest_ports,
                    timeout=FDB_WAIT_EXPECTED_PACKET_TIMEOUT)
            break
        except:
            pass
    else:
        result = duthost.command("show mac", module_ignore_errors=True)
        logger.debug("Show mac results {}".format(result['stdout']))
        pytest_assert(
            False, "Expected packet was not received on ports {}"
            "Dest MAC in fdb is {}".format(
                dest_ports,
                dest_mac.lower() in result['stdout'].lower()))
Ejemplo n.º 9
0
def generate_and_verify_traffic(duthost, ptfadapter, src_port, dst_port,
                                ip_src, ip_dst, pkt_action):
    """
    Send ICMP request packet from PTF to DUT and
    verify that DUT sends/doesn't send ICMP reply packet to PTF.

    Args:
        duthost: DUT host object
        ptfadapter: PTF adapter
        src_port: Port of PTF
        dst_port: Port of DUT
        ip_src: Source IP address of PTF
        ip_dst: Destination IP address of DUT
        pkt_action: Packet action (forwarded or drop)
    """
    router_mac = get_mac_dut(duthost, dst_port)
    src_port_number = int(get_port_number(src_port))
    src_mac = ptfadapter.dataplane.get_mac(0, src_port_number)
    # Get VLAN ID from name of sub-port
    vlan_vid = int(src_port.split('.')[1])

    ip_src = ip_src.split('/')[0]
    ip_dst = ip_dst.split('/')[0]

    # Create ICMP request packet
    pkt = create_packet(eth_dst=router_mac,
                        eth_src=src_mac,
                        ip_src=ip_src,
                        ip_dst=ip_dst,
                        vlan_vid=vlan_vid,
                        dl_vlan_enable=True)

    # Define ICMP reply packet
    exp_pkt = create_packet(eth_src=router_mac,
                            eth_dst=src_mac,
                            ip_src=ip_dst,
                            ip_dst=ip_src,
                            vlan_vid=vlan_vid,
                            dl_vlan_enable=True,
                            icmp_type=0)

    masked_exp_pkt = mask.Mask(exp_pkt)
    masked_exp_pkt.set_do_not_care_scapy(packet.IP, "id")
    masked_exp_pkt.set_do_not_care_scapy(packet.IP, "chksum")
    masked_exp_pkt.set_do_not_care_scapy(packet.IP, "ttl")
    masked_exp_pkt.set_do_not_care_scapy(packet.ICMP, "chksum")

    ptfadapter.dataplane.flush()
    testutils.send_packet(ptfadapter, src_port_number, pkt)

    dst_port_list = [src_port_number]

    if pkt_action == ACTION_FWD:
        testutils.verify_packet_any_port(ptfadapter, masked_exp_pkt,
                                         dst_port_list)
    elif pkt_action == ACTION_DROP:
        testutils.verify_no_packet_any(ptfadapter, masked_exp_pkt,
                                       dst_port_list)
Ejemplo n.º 10
0
    def _verify_acl_traffic(self, setup, direction, ptfadapter, pkt, dropped):
        exp_pkt = self.expected_mask_routed_packet(pkt)

        ptfadapter.dataplane.flush()
        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)

        if dropped:
            testutils.verify_no_packet_any(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))
        else:
            testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))
Ejemplo n.º 11
0
    def test_rules_priority_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test rules priorities, forward rule case """

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['IP'].src = '20.0.0.7'
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(20)
Ejemplo n.º 12
0
    def test_icmp_source_ip_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test ICMP source IP matched packet forwarded """

        pkt = self.icmp_packet(setup, direction, ptfadapter)
        pkt['IP'].src = '20.0.0.4'
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(12)
Ejemplo n.º 13
0
    def test_l4_dport_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test L4 destination port matched packet forwarded """

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['TCP'].dport = 0x1217
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(5)
Ejemplo n.º 14
0
    def test_l4_sport_range_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test L4 source port range matched packet forwarded """

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['TCP'].sport = 0x123A
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(10)
Ejemplo n.º 15
0
    def test_ip_proto_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test IP protocol matched packet forwarded"""

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['IP'].proto = 0x7E
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(5)
Ejemplo n.º 16
0
    def test_tcp_flags_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test TCP flags matched packet forwarded """

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['TCP'].flags = 0x1B
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(6)
Ejemplo n.º 17
0
    def test_dest_ip_match_forwarded(self, setup, direction, ptfadapter, counters_sanity_check):
        """ test destination IP matched packet forwarded """

        pkt = self.tcp_packet(setup, direction, ptfadapter)
        pkt['IP'].dst = DST_IP_TOR_FORWARDED if direction == 'spine->tor' else DST_IP_SPINE_FORWARDED
        exp_pkt = self.expected_mask_routed_packet(pkt)

        testutils.send(ptfadapter, self.get_src_port(setup, direction), pkt)
        testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=self.get_dst_ports(setup, direction))

        counters_sanity_check.append(2 if direction == 'spine->tor' else 3)
Ejemplo n.º 18
0
def test_bgpmon(duthost, common_setup_teardown, ptfadapter):
    """
    Add a bgp monitor on ptf and verify that DUT is attempting to establish connection to it
    """
    local_addr, peer_addr, peer_ports = common_setup_teardown
    exp_packet = build_syn_pkt(local_addr, peer_addr)
    # Load bgp monitor config
    logger.info(
        "Configured bgpmon and verifying packet on {}".format(peer_ports))
    duthost.command("sonic-cfggen -j {} -w".format(BGPMON_CONFIG_FILE))
    # Verify syn packet on ptf
    testutils.verify_packet_any_port(test=ptfadapter,
                                     pkt=exp_packet,
                                     ports=peer_ports,
                                     timeout=BGP_CONNECT_TIMEOUT)
Ejemplo n.º 19
0
    def testDhcpPacketForwarding(self, duthost, testPorts, ptfadapter,
                                 pktInfo):
        """
        Validates that DHCP Discover/Offer/Request/Ack (DORA) packets are forwarded through T1 devices

         Args:
            duthost(Ansible Fixture): instance of SonicHost class of DUT
            testPorts(Ansible Fixture, dict): contains downstream/upstream test ports information
            ptfadapter(Ansible Fixture): instance of PTF Adapter
            pktInfo(Pytest Params<dict>): test parameters containing information on which ports used for
                                          sending/receiving DHCP packet and
                                          DHCP packet to send
        """
        ptfadapter.dataplane.flush()

        dhcpPacket = pktInfo["pktGen"](duthost.facts["router_mac"])
        testutils.send(ptfadapter, random.choice(testPorts[pktInfo["txDir"]]),
                       dhcpPacket)

        # Update fields of the forwarded packet
        dhcpPacket[scapy.Ether].src = duthost.facts["router_mac"]
        dhcpPacket[
            scapy.IP].ttl = dhcpPacket[scapy.IP].ttl - duthost.ttl_decr_value

        expectedDhcpPacket = Mask(dhcpPacket)
        expectedDhcpPacket.set_do_not_care_scapy(scapy.Ether, "dst")
        expectedDhcpPacket.set_do_not_care_scapy(scapy.IP, "chksum")

        _, receivedPacket = testutils.verify_packet_any_port(
            ptfadapter, expectedDhcpPacket, ports=testPorts[pktInfo["rxDir"]])
        logger.info("Received packet: %s",
                    scapy.Ether(receivedPacket).summary())
Ejemplo n.º 20
0
 def __exit__(self, *exc_info):
     if exc_info[0]:
         return
     try:
         port_index, rec_pkt = testutils.verify_packet_any_port(
             ptfadapter,
             self.exp_pkt,
             ports=self.listen_ports
         )
     except AssertionError as detail:
         logging.debug("Error occurred in polling for tunnel traffic", exc_info=True)
         if "Did not receive expected packet on any of ports" in str(detail):
             if self.existing:
                 raise detail
         else:
             raise detail
     else:
         self.rec_pkt = Ether(rec_pkt)
         rec_port = self.listen_ports[port_index]
         logging.debug("Receive encap packet from PTF interface %s", "eth%s" % rec_port)
         logging.debug("Encapsulated packet:\n%s", self._dump_show_str(self.rec_pkt))
         if not self.existing:
             raise RuntimeError("Detected tunnel traffic from host %s." % self.standby_tor.hostname)
         ttl_check_res = self._check_ttl(self.rec_pkt)
         tos_check_res = self._check_tos(self.rec_pkt)
         check_res = []
         if ttl_check_res:
             check_res.append(ttl_check_res)
         if tos_check_res:
             check_res.append(tos_check_res)
         if check_res:
             raise ValueError(", ".join(check_res) + ".")
Ejemplo n.º 21
0
    def test_push_label(self, setup, ptfadapter):
        """ test push label """

        dst_pid = setup['dst_pid']
        src_pid = setup['src_pid']

        self.config_interface_mpls(setup, LABEL_PUSH_ROUTES)

        time.sleep(2)

        pkt = self.icmp_packet(setup, ptfadapter)
        epkt = pkt.copy()
        pkt1 = epkt['IP']
        epkt['Ethernet'].type = 0x8847
        epkt['Ethernet'].remove_payload()
        mp = MPLS(label=1000002, s=1, ttl=255)
        mp.remove_payload()
        epkt /= mp
        epkt /= pkt1
        exp_pkt = self.expected_mask_mpls_push_packet(epkt, 1000001)

        ptfadapter.dataplane.flush()
        testutils.send(ptfadapter, src_pid, pkt)

        try:
            res = testutils.verify_packet_any_port(ptfadapter,
                                                   exp_pkt,
                                                   ports=[dst_pid])
            logger.info(res)
        except Exception as e:
            self.teardown_labels(setup)
            pytest.fail('MPLS push test failed \n' + str(e))

        self.teardown_labels(setup)
Ejemplo n.º 22
0
    def test_swap_labelstack(self, setup, ptfadapter):
        """ test swap labelstack """

        dst_pid = setup['dst_pid']
        src_pid = setup['src_pid']

        self.config_interface_mpls(setup, LABEL_SWAP_ROUTES)

        time.sleep(2)

        pkt = self.mpls_stack_packet(setup, ptfadapter)
        exp_pkt = self.expected_mask_mpls_swap_packet(pkt, 1000002)

        ptfadapter.dataplane.flush()
        testutils.send(ptfadapter, src_pid, pkt)

        try:
            res = testutils.verify_packet_any_port(ptfadapter,
                                                   exp_pkt,
                                                   ports=[dst_pid])
            logger.info(res)
        except Exception as e:
            self.teardown_labels(setup)
            pytest.fail('MPLS swap labelstack test failed \n' + str(e))

        self.teardown_labels(setup)
Ejemplo n.º 23
0
def test_decap_active_tor(apply_active_state_to_orchagent,
                          build_encapsulated_packet, rand_selected_interface,
                          ptfadapter, tbinfo, rand_selected_dut,
                          tunnel_traffic_monitor):
    tor = rand_selected_dut
    encapsulated_packet = build_encapsulated_packet
    iface, _ = rand_selected_interface

    exp_ptf_port_index = get_ptf_server_intf_index(tor, tbinfo, iface)
    exp_pkt = build_expected_packet_to_server(encapsulated_packet)

    ptfadapter.dataplane.flush()
    ptf_t1_intf = random.choice(get_t1_ptf_ports(tor, tbinfo))
    logging.info("send encapsulated packet from ptf t1 interface %s",
                 ptf_t1_intf)
    testutils.send(ptfadapter,
                   int(ptf_t1_intf.strip("eth")),
                   encapsulated_packet,
                   count=10)
    _, rec_pkt = testutils.verify_packet_any_port(ptfadapter,
                                                  exp_pkt,
                                                  ports=[exp_ptf_port_index])
    rec_pkt = Ether(rec_pkt)
    logging.info("received decap packet:\n%s",
                 tunnel_traffic_monitor._dump_show_str(rec_pkt))
    exp_ttl = encapsulated_packet[IP].payload[IP].ttl - 1
    exp_tos = encapsulated_packet[IP].payload[IP].tos
    if rec_pkt[IP].ttl != exp_ttl:
        pytest.fail("the expected ttl should be %s" % exp_ttl)
    if rec_pkt[IP].tos != exp_tos:
        pytest.fail("the expected tos should be %s" % exp_tos)
Ejemplo n.º 24
0
    def send_and_verify(self,
                        dst_ip,
                        expected_ports,
                        src_port,
                        outer_pkt='ipv4',
                        triple_encap=False,
                        outer_ttl=None,
                        inner_ttl=None):
        '''
        @summary: This function builds encap packet, send and verify their arrival.
        @dst_ip: the destination ip for the inner IP header
        @expected_ports: list of ports that a packet can arrived from
        @src_port: the physical port that the packet will be sent from
        @triple_encap: True to send triple encapsulated packet
        @outer_ttl: TTL for the outer layer
        @inner_ttl: TTL for the inner layer
        '''

        pkt, exp_pkt = self.create_encap_packet(dst_ip, outer_pkt,
                                                triple_encap, outer_ttl,
                                                inner_ttl)
        masked_exp_pkt = Mask(exp_pkt)
        masked_exp_pkt.set_do_not_care_scapy(scapy.Ether, "dst")
        masked_exp_pkt.set_do_not_care_scapy(scapy.Ether, "src")

        #send and verify the return packets
        send_packet(self, src_port, pkt)
        logging.info(".....Sending packet from port" + str(src_port) + " to " +
                     dst_ip + ", Triple_encap: " + str(triple_encap))
        matched, received = verify_packet_any_port(self, masked_exp_pkt,
                                                   expected_ports)
        assert received
        return matched, received
Ejemplo n.º 25
0
def send_recv_eth(ptfadapter, source_port, source_mac, dest_port, dest_mac):
    """
    send ethernet packet and verify it on dest_port
    :param ptfadapter: PTF adapter object
    :param source_port: source port
    :param source_mac: source MAC
    :param dest_port: destination port to receive packet on
    :param dest_mac: destination MAC
    :return:
    """
    pkt = testutils.simple_eth_packet(eth_dst=dest_mac,
                                      eth_src=source_mac,
                                      eth_type=DEFAULT_FDB_ETHERNET_TYPE)
    logger.debug(
        'send packet src port {} smac: {} dmac: {} verifying on dst port {}'.
        format(source_port, source_mac, dest_mac, dest_port))
    testutils.send(ptfadapter, source_port, pkt)
    testutils.verify_packet_any_port(ptfadapter, pkt, [dest_port])
Ejemplo n.º 26
0
    def test_push_label(self, setup, ptfadapter):
        """ test push MPLS label """

        duthost = setup['duthost']

        # Copy APP_DB config to DUT
        duthost.copy(src=os.path.join(ADD_DIR, LABEL_PUSH_ROUTES),
                     dest=setup['dut_tmp_dir'])
        label_add_dut_path = os.path.join(setup['dut_tmp_dir'],
                                          LABEL_PUSH_ROUTES)

        # Apply routes with swssconfig
        result = duthost.shell(
            'docker exec -i swss swssconfig /dev/stdin < {}'.format(
                label_add_dut_path),
            module_ignore_errors=True)
        if result['rc'] != 0:
            pytest.fail(
                'Failed to apply labelroute configuration file: {}'.format(
                    result['stderr']))

        # Create packet for sending and masked expected packet on receiving port
        pkt = self.icmp_packet(setup, ptfadapter)
        ## Add MPLS header in expected packet
        epkt = pkt.copy()
        pkt1 = epkt['IP']
        epkt['Ether'].type = 0x8847
        epkt['Ether'].remove_payload()
        mp = MPLS(label=1000002, s=1, ttl=255)
        mp.remove_payload()
        epkt /= mp
        epkt /= pkt1
        exp_pkt = self.expected_mask_mpls_push_packet(epkt, 1000001)

        ptfadapter.dataplane.flush()

        # Send pkt from spine port 10
        testutils.send(ptfadapter, '10', pkt)

        # Capture and verify packets on tor port 25
        res = testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=[25])

        # Copy Delete MPLS configs to DUT after test
        duthost.copy(src=os.path.join(ADD_DIR, LABEL_DEL_ROUTES),
                     dest=setup['dut_tmp_dir'])
        label_del_dut_path = os.path.join(setup['dut_tmp_dir'],
                                          LABEL_DEL_ROUTES)

        # Apply routes with swssconfig
        result = duthost.shell(
            'docker exec -i swss swssconfig /dev/stdin < {}'.format(
                label_del_dut_path),
            module_ignore_errors=True)
        if result['rc'] != 0:
            pytest.fail(
                'Failed to apply labelroute configuration file: {}'.format(
                    result['stderr']))
    def send_and_check_mirror_packets(self,
                                      setup,
                                      mirror_session,
                                      ptfadapter,
                                      duthost,
                                      mirror_packet,
                                      src_port=None,
                                      dest_ports=None,
                                      expect_recv=True):
        expected_mirror_packet = self._get_expected_mirror_packet(
            mirror_session, setup, duthost, mirror_packet)

        if not src_port:
            src_port = self._get_random_src_port(setup)

        if not dest_ports:
            dest_ports = [
                self._get_monitor_port(setup, mirror_session, duthost)
            ]

        ptfadapter.dataplane.flush()
        testutils.send(ptfadapter, src_port, mirror_packet)

        if expect_recv:
            _, received_packet = testutils.verify_packet_any_port(
                ptfadapter, expected_mirror_packet, ports=dest_ports)
            logging.info("Received packet: %s",
                         packet.Ether(received_packet).summary())

            inner_packet = self._extract_mirror_payload(
                received_packet, len(mirror_packet))
            logging.info("Received inner packet: %s", inner_packet.summary())

            inner_packet = Mask(inner_packet)

            # For egress mirroring, we expect the DUT to have modified the packet
            # before forwarding it. Specifically:
            #
            # - In L2 the SMAC and DMAC will change.
            # - In L3 the TTL and checksum will change.
            #
            # We know what the TTL and SMAC should be after going through the pipeline,
            # but DMAC and checksum are trickier. For now, update the TTL and SMAC, and
            # mask off the DMAC and IP Checksum to verify the packet contents.
            if self.mirror_type() == "egress":
                mirror_packet[packet.IP].ttl -= 1
                mirror_packet[packet.Ether].src = setup["router_mac"]

                inner_packet.set_do_not_care_scapy(packet.Ether, "dst")
                inner_packet.set_do_not_care_scapy(packet.IP, "chksum")

            logging.info("Expected inner packet: %s", mirror_packet.summary())
            pytest_assert(inner_packet.pkt_match(mirror_packet),
                          "Mirror payload does not match received packet")
        else:
            testutils.verify_no_packet_any(ptfadapter, expected_mirror_packet,
                                           dest_ports)
Ejemplo n.º 28
0
    def runTestIpv4(self):
        self.test.log("Run IPv4 based test")

        pkt = simple_udp_packet(eth_dst=self.srcRouterMac,
                                eth_src=self.srcHostMac,
                                ip_src=self.dstHostIpv4,
                                ip_dst=self.dstHostIpv4,
                                ip_ttl=self.pktTtlHlim)
        send(self.test, int(self.srcPortIds[0]), pkt)

        pkt = simple_udp_packet(eth_dst=self.dstHostMac,
                                eth_src=self.dstRouterMac,
                                ip_src=self.dstHostIpv4,
                                ip_dst=self.dstHostIpv4,
                                ip_ttl=self.pktTtlHlim-1)

        verify_packet_any_port(self.test, pkt, [int(port) for port in self.dstPortIds])

        self.test.log("IPv4 based test: done")
Ejemplo n.º 29
0
def test_dscp_to_queue_during_decap_on_active(
        ptfhost, setup_dualtor_tor_active, build_encapsulated_ip_packet,
        request, rand_selected_interface, ptfadapter, tbinfo,
        rand_selected_dut, tunnel_traffic_monitor, duthosts,
        rand_one_dut_hostname):
    """
    Test if DSCP to Q mapping for inner header is matching with outer header during decap on active
    """
    tor = rand_selected_dut
    encapsulated_packet = build_encapsulated_ip_packet
    iface, _ = rand_selected_interface

    exp_ptf_port_index = get_ptf_server_intf_index(tor, tbinfo, iface)
    exp_pkt = build_expected_packet_to_server(encapsulated_packet)

    # Clear queue counters
    duthost = duthosts[rand_one_dut_hostname]
    duthost.shell('sonic-clear queuecounters')
    logging.info("Clearing queue counters before starting traffic")

    with stop_garp(ptfhost):
        ptfadapter.dataplane.flush()
        ptf_t1_intf = random.choice(get_t1_ptf_ports(tor, tbinfo))
        logging.info("send encapsulated packet from ptf t1 interface %s",
                     ptf_t1_intf)
        testutils.send(ptfadapter,
                       int(ptf_t1_intf.strip("eth")),
                       encapsulated_packet,
                       count=10)

        exp_tos = encapsulated_packet[IP].payload[IP].tos
        exp_dscp = exp_tos >> 2
        exp_queue = derive_queue_id_from_dscp(exp_dscp)

        _, rec_pkt = testutils.verify_packet_any_port(
            ptfadapter, exp_pkt, ports=[exp_ptf_port_index], timeout=10)
        rec_pkt = Ether(rec_pkt)
        logging.info("received decap packet:\n%s",
                     dump_scapy_packet_show_output(rec_pkt))

        time.sleep(10)
        rec_queue = get_queue_id_of_received_packet(duthosts,
                                                    rand_one_dut_hostname,
                                                    rand_selected_interface)

        if rec_queue == None or rec_queue != exp_queue:
            pytest.fail(
                "the expected Queue : {} not matching with received Queue : {}"
                .format(exp_queue, rec_queue))
        else:
            logging.info(
                "the expected Queue : {} matching with received Queue : {}".
                format(exp_queue, rec_queue))
Ejemplo n.º 30
0
    def test_swap_labelstack(self, setup, ptfadapter):
        """ test swap for stack of 3 MPLS label """

        duthost = setup['duthost']

        # Copy APP_DB config to DUT
        duthost.copy(src=os.path.join(ADD_DIR, LABEL_SWAP_ROUTES),
                     dest=setup['dut_tmp_dir'])
        label_add_dut_path = os.path.join(setup['dut_tmp_dir'],
                                          LABEL_SWAP_ROUTES)

        # Apply routes with swssconfig
        result = duthost.shell(
            'docker exec -i swss swssconfig /dev/stdin < {}'.format(
                label_add_dut_path),
            module_ignore_errors=True)
        if result['rc'] != 0:
            pytest.fail(
                'Failed to apply labelroute configuration file: {}'.format(
                    result['stderr']))

        # Create packet for sending and masked expected packet on receiving port
        pkt = self.mpls_stack_packet(setup, ptfadapter)
        exp_pkt = self.expected_mask_mpls_swap_packet(pkt, 1000002)

        ptfadapter.dataplane.flush()

        # Send pkt from spine port 10
        testutils.send(ptfadapter, '10', pkt)

        # Capture and verify packets on tor port 25
        res = testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=[25])

        # Copy Delete MPLS configs to DUT after test
        duthost.copy(src=os.path.join(ADD_DIR, LABEL_DEL_ROUTES),
                     dest=setup['dut_tmp_dir'])
        label_del_dut_path = os.path.join(setup['dut_tmp_dir'],
                                          LABEL_DEL_ROUTES)

        # Apply routes with swssconfig
        result = duthost.shell(
            'docker exec -i swss swssconfig /dev/stdin < {}'.format(
                label_del_dut_path),
            module_ignore_errors=True)
        if result['rc'] != 0:
            pytest.fail(
                'Failed to apply labelroute configuration file: {}'.format(
                    result['stderr']))