|
| 1 | +import pytest |
| 2 | +import logging |
| 3 | +import ptf.testutils as testutils |
| 4 | +import ptf.mask as mask |
| 5 | +import time |
| 6 | + |
| 7 | +from netaddr import IPNetwork, NOHOST |
| 8 | +from tests.common.helpers.assertions import pytest_assert |
| 9 | +from scapy.all import IP, Ether |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | +pytestmark = [ |
| 14 | + pytest.mark.topology('t0') |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope='module') |
| 19 | +def vlan_ports_setup(duthosts, rand_one_dut_hostname): |
| 20 | + """ |
| 21 | + Setup: Brings down all member ports of a VLAN. |
| 22 | + Teardown: Restores the admin state of all member ports of the VLAN selected in the Setup phase. |
| 23 | + """ |
| 24 | + duthost = duthosts[rand_one_dut_hostname] |
| 25 | + vlan_brief = duthost.get_vlan_brief() |
| 26 | + if not vlan_brief: |
| 27 | + pytest.skip("The testbed does not have any VLANs.") |
| 28 | + # Selecting the first VLAN in 'vlan_brief' |
| 29 | + vlan_name = next(iter(vlan_brief)) |
| 30 | + vlan_members = vlan_brief[vlan_name]["members"] |
| 31 | + ifs_status = duthost.get_interfaces_status() |
| 32 | + vlan_up_members = [port for port in vlan_members if ifs_status[port]["admin"] == "up"] |
| 33 | + logger.info(f"Bringing down all member ports of {vlan_name}...") |
| 34 | + for vlan_up_port in vlan_up_members: |
| 35 | + duthost.shell(f"sudo config interface shutdown {vlan_up_port}") |
| 36 | + time.sleep(5) # Sleep for 5 seconds to ensure T1 switches update their routing table |
| 37 | + yield vlan_name |
| 38 | + logger.info(f"Restoring the previous admin state of all member ports of {vlan_name}...") |
| 39 | + for vlan_port in vlan_up_members: |
| 40 | + duthost.shell(f"sudo config interface startup {vlan_port}") |
| 41 | + |
| 42 | + |
| 43 | +def test_vlan_ports_down(vlan_ports_setup, duthosts, rand_one_dut_hostname, nbrhosts, tbinfo, ptfadapter): |
| 44 | + """ |
| 45 | + Asserts the following conditions when all member ports of a VLAN interface are down: |
| 46 | + 1. The VLAN interface's oper status remains Up. |
| 47 | + 2. The VLAN's subnet IP is advertised to the T1 neighbors. |
| 48 | + 3. The IP decapsulation feature works for packets that are sent to the VLAN interfaces's IP address. |
| 49 | + """ |
| 50 | + duthost = duthosts[rand_one_dut_hostname] |
| 51 | + vlan_name = vlan_ports_setup |
| 52 | + ip_interfaces = duthost.show_ip_interface()["ansible_facts"]["ip_interfaces"] |
| 53 | + vlan_info = ip_interfaces[vlan_name] |
| 54 | + logger.info(f"Checking if {vlan_name} is oper UP...") |
| 55 | + # check if the VLAN interface is operationally Up (IPv4) |
| 56 | + pytest_assert(vlan_info["oper_state"] == "up", f"{vlan_name} is operationally down.") |
| 57 | + |
| 58 | + ipv6_interfaces = duthost.show_ipv6_interfaces() |
| 59 | + vlan_info_ipv6 = ipv6_interfaces[vlan_name] |
| 60 | + # check if the VLAN interface is operationally Up (IPv6) |
| 61 | + pytest_assert(vlan_info_ipv6["oper"] == "up", f"{vlan_name} is operationally down.") |
| 62 | + |
| 63 | + logger.info("Checking BGP routes on T1 neighbors...") |
| 64 | + vlan_subnet = str(IPNetwork(f"{vlan_info['ipv4']}/{vlan_info['prefix_len']}", flags=NOHOST)) |
| 65 | + vlan_subnet_ipv6 = str(IPNetwork(vlan_info_ipv6["ipv6 address/mask"], flags=NOHOST)) |
| 66 | + nbrcount = 0 |
| 67 | + for nbrname, nbrhost in nbrhosts.items(): |
| 68 | + nbrhost = nbrhost["host"] |
| 69 | + # check IPv4 routes on nbrhost |
| 70 | + logger.info(f"Checking IPv4 routes on {nbrname}...") |
| 71 | + try: |
| 72 | + vlan_route = nbrhost.get_route(vlan_subnet)["vrfs"]["default"] |
| 73 | + except Exception: |
| 74 | + # nbrhost might be unreachable. Skip it. |
| 75 | + logger.info(f"{nbrname} might be unreachable.") |
| 76 | + continue |
| 77 | + pytest_assert(vlan_route["bgpRouteEntries"], |
| 78 | + f"{vlan_name}'s IPv4 subnet is not advertised to the T1 neighbor {nbrname}.") |
| 79 | + # check IPv6 routes on nbrhost |
| 80 | + logger.info(f"Checking IPv6 routes on {nbrname}...") |
| 81 | + try: |
| 82 | + vlan_route_ipv6 = nbrhost.get_route(vlan_subnet_ipv6)["vrfs"]["default"] |
| 83 | + except Exception: |
| 84 | + # nbrhost might be unreachable. Skip it. |
| 85 | + logger.info(f"{nbrname} might be unreachable.") |
| 86 | + continue |
| 87 | + pytest_assert(vlan_route_ipv6["bgpRouteEntries"], |
| 88 | + f"{vlan_name}'s IPv6 subnet is not advertised to the T1 neighbor {nbrname}.") |
| 89 | + nbrcount += 1 |
| 90 | + if nbrcount == 0: |
| 91 | + pytest.skip("Could not get routing info from any T1 neighbors.") |
| 92 | + if duthost.facts["asic_type"].lower() == "vs": |
| 93 | + logger.info("Skipping IP-in-IP decapsulation test for the 'vs' ASIC type.") |
| 94 | + return |
| 95 | + logger.info("Starting the IP-in-IP decapsulation test...") |
| 96 | + mg_facts = duthost.get_extended_minigraph_facts(tbinfo) |
| 97 | + # Use the first Ethernet port associated with the first portchannel to send test packets to the DUT |
| 98 | + portchannel_info = next(iter(mg_facts["minigraph_portchannels"].values())) |
| 99 | + ptf_src_port = portchannel_info["members"][0] |
| 100 | + ptf_src_port_index = mg_facts["minigraph_ptf_indices"][ptf_src_port] |
| 101 | + ptf_dst_port_indices = list(mg_facts["minigraph_ptf_indices"].values()) |
| 102 | + # Test IPv4 in IPv4 decapsulation. |
| 103 | + # Outer IP packet: |
| 104 | + # src: 1.1.1.1 |
| 105 | + # dst: VLAN interface's IPv4 address |
| 106 | + # Inner IP packet: |
| 107 | + # src: 2.2.2.2 |
| 108 | + # dst: 3.3.3.3 |
| 109 | + # Expectation: The T0 switch (DUT) decapsulates the outer IP packet and sends |
| 110 | + # the inner IP packet to the default gateway (one of the connected T1 switches). |
| 111 | + inner_pkt = testutils.simple_udp_packet(ip_src="2.2.2.2", |
| 112 | + ip_dst="3.3.3.3") |
| 113 | + outer_pkt = testutils.simple_ipv4ip_packet(eth_src=ptfadapter.dataplane.get_mac(0, ptf_src_port_index), |
| 114 | + eth_dst=duthost.facts["router_mac"], |
| 115 | + ip_src="1.1.1.1", |
| 116 | + ip_dst=vlan_info["ipv4"], |
| 117 | + inner_frame=inner_pkt["IP"]) |
| 118 | + exp_pkt = inner_pkt.copy() |
| 119 | + exp_pkt = mask.Mask(exp_pkt) |
| 120 | + exp_pkt.set_do_not_care_packet(Ether, "src") |
| 121 | + exp_pkt.set_do_not_care_packet(Ether, "dst") |
| 122 | + exp_pkt.set_do_not_care_packet(IP, "ttl") |
| 123 | + exp_pkt.set_do_not_care_packet(IP, "chksum") |
| 124 | + exp_pkt.set_do_not_care_packet(IP, "tos") |
| 125 | + logger.info("Sending the IP-in-IP packet...") |
| 126 | + testutils.send(ptfadapter, ptf_src_port_index, outer_pkt) |
| 127 | + logger.info("IP-in-IP packet sent.") |
| 128 | + testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=ptf_dst_port_indices) |
0 commit comments