|
| 1 | +import pytest |
| 2 | +import time |
| 3 | +import random |
| 4 | +import logging |
| 5 | +from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest |
| 6 | +from scapy.layers.l2 import Ether |
| 7 | + |
| 8 | +from srv6_utils import runSendReceive |
| 9 | +from common.helpers.voq_helpers import get_neighbor_info |
| 10 | +from ptf.testutils import simple_ipv6_sr_packet |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +pytestmark = [ |
| 15 | + pytest.mark.asic("mellanox", "broadcom"), |
| 16 | + pytest.mark.topology("t0", "t1") |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def get_ptf_src_port_and_dut_port_and_neighbor(dut, tbinfo): |
| 21 | + """Get the PTF port mapping for the duthost or an asic of the duthost""" |
| 22 | + dut_mg_facts = dut.get_extended_minigraph_facts(tbinfo) |
| 23 | + ports_map = dut_mg_facts["minigraph_ptf_indices"] |
| 24 | + if len(ports_map) == 0: |
| 25 | + pytest.skip("No PTF ports found for {}".format(dut)) |
| 26 | + |
| 27 | + lldp_table = dut.command("show lldp table")['stdout'].split("\n")[3:] |
| 28 | + neighbor_table = [line.split() for line in lldp_table] |
| 29 | + for entry in neighbor_table: |
| 30 | + intf = entry[0] |
| 31 | + if intf in ports_map: |
| 32 | + return intf, ports_map[intf], entry[1] # local intf, ptf_src_port, neighbor hostname |
| 33 | + |
| 34 | + dut_port, ptf_src_port = random.choice(ports_map) |
| 35 | + return dut_port, ptf_src_port, None |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("with_srh", [True, False]) |
| 39 | +def test_srv6_uN_forwarding(duthosts, enum_frontend_dut_hostname, enum_frontend_asic_index, |
| 40 | + ptfadapter, tbinfo, nbrhosts, with_srh): |
| 41 | + duthost = duthosts[enum_frontend_dut_hostname] |
| 42 | + asic_index = enum_frontend_asic_index |
| 43 | + |
| 44 | + if duthost.is_multi_asic: |
| 45 | + cli_options = " -n " + duthost.get_namespace_from_asic_id(asic_index) |
| 46 | + dut_asic = duthost.asic_instance[asic_index] |
| 47 | + dut_mac = dut_asic.get_router_mac() |
| 48 | + dut_port, ptf_src_port, neighbor = get_ptf_src_port_and_dut_port_and_neighbor(dut_asic, tbinfo) |
| 49 | + else: |
| 50 | + cli_options = '' |
| 51 | + dut_mac = duthost._get_router_mac() |
| 52 | + dut_port, ptf_src_port, neighbor = get_ptf_src_port_and_dut_port_and_neighbor(duthost, tbinfo) |
| 53 | + |
| 54 | + logger.info("Doing test on DUT port {} | PTF port {}".format(dut_port, ptf_src_port)) |
| 55 | + |
| 56 | + # get neighbor IP |
| 57 | + lines = duthost.command("show ipv6 bgp sum")['stdout'].split("\n") |
| 58 | + for line in lines: |
| 59 | + if neighbor in line: |
| 60 | + neighbor_ip = line.split()[0] |
| 61 | + assert neighbor_ip |
| 62 | + |
| 63 | + # use DUT portchannel if applicable |
| 64 | + pc_info = duthost.command("show int portchannel")['stdout'] |
| 65 | + if dut_port in pc_info: |
| 66 | + lines = pc_info.split("\n") |
| 67 | + for line in lines: |
| 68 | + if dut_port in line: |
| 69 | + dut_port = line.split()[1] |
| 70 | + |
| 71 | + sonic_db_cli = "sonic-db-cli" + cli_options |
| 72 | + |
| 73 | + # add a locator configuration entry |
| 74 | + duthost.command(sonic_db_cli + " CONFIG_DB HSET SRV6_MY_LOCATORS\\|loc1 prefix fcbb:bbbb:1::") |
| 75 | + # add a uN sid configuration entry |
| 76 | + duthost.command(sonic_db_cli + |
| 77 | + " CONFIG_DB HSET SRV6_MY_SIDS\\|loc1\\|fcbb:bbbb:1::/48 action uN decap_dscp_mode pipe") |
| 78 | + # add the static route for IPv6 forwarding towards PTF's uSID |
| 79 | + duthost.command(sonic_db_cli + " CONFIG_DB HSET STATIC_ROUTE\\|default\\|fcbb:bbbb:2::/48 nexthop {} ifname {}" |
| 80 | + .format(neighbor_ip, dut_port)) |
| 81 | + time.sleep(5) |
| 82 | + |
| 83 | + for i in range(0, 10): |
| 84 | + if with_srh: |
| 85 | + injected_pkt = simple_ipv6_sr_packet( |
| 86 | + eth_dst=dut_mac, |
| 87 | + eth_src=ptfadapter.dataplane.get_mac(0, ptf_src_port).decode(), |
| 88 | + ipv6_src=ptfadapter.ptf_ipv6, |
| 89 | + ipv6_dst="fcbb:bbbb:1:2::", |
| 90 | + srh_seg_left=1, |
| 91 | + srh_nh=41, |
| 92 | + inner_frame=IPv6()/ICMPv6EchoRequest(seq=i) |
| 93 | + ) |
| 94 | + else: |
| 95 | + injected_pkt = Ether(dst=dut_mac, src=ptfadapter.dataplane.get_mac(0, ptf_src_port).decode()) \ |
| 96 | + / IPv6(src=ptfadapter.ptf_ipv6, dst="fcbb:bbbb:1:2::") \ |
| 97 | + / IPv6() / ICMPv6EchoRequest(seq=i) |
| 98 | + |
| 99 | + expected_pkt = injected_pkt.copy() |
| 100 | + expected_pkt['Ether'].dst = get_neighbor_info(neighbor_ip, nbrhosts)['mac'] |
| 101 | + expected_pkt['Ether'].src = dut_mac |
| 102 | + expected_pkt['IPv6'].dst = "fcbb:bbbb:2::" |
| 103 | + expected_pkt['IPv6'].hlim -= 1 |
| 104 | + logger.debug("Expected packet #{}: {}".format(i, expected_pkt.summary())) |
| 105 | + runSendReceive(injected_pkt, ptf_src_port, expected_pkt, [ptf_src_port], True, ptfadapter) |
| 106 | + |
| 107 | + # delete the SRv6 configuration |
| 108 | + duthost.command(sonic_db_cli + " CONFIG_DB DEL SRV6_MY_LOCATORS\\|loc1") |
| 109 | + duthost.command(sonic_db_cli + " CONFIG_DB DEL SRV6_MY_SIDS\\|loc1\\|fcbb:bbbb:1::/48") |
| 110 | + duthost.command(sonic_db_cli + " CONFIG_DB DEL STATIC_ROUTE\\|default\\|fcbb:bbbb:2::/48") |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("with_srh", [True, False]) |
| 114 | +def test_srv6_uN_decap_pipe_mode(duthosts, enum_frontend_dut_hostname, enum_frontend_asic_index, |
| 115 | + ptfadapter, tbinfo, nbrhosts, with_srh): |
| 116 | + duthost = duthosts[enum_frontend_dut_hostname] |
| 117 | + asic_index = enum_frontend_asic_index |
| 118 | + |
| 119 | + if duthost.is_multi_asic: |
| 120 | + cli_options = " -n " + duthost.get_namespace_from_asic_id(asic_index) |
| 121 | + dut_asic = duthost.asic_instance[asic_index] |
| 122 | + dut_mac = dut_asic.get_router_mac() |
| 123 | + dut_port, ptf_src_port, neighbor = get_ptf_src_port_and_dut_port_and_neighbor(dut_asic, tbinfo) |
| 124 | + else: |
| 125 | + cli_options = '' |
| 126 | + dut_mac = duthost._get_router_mac() |
| 127 | + dut_port, ptf_src_port, neighbor = get_ptf_src_port_and_dut_port_and_neighbor(duthost, tbinfo) |
| 128 | + |
| 129 | + logger.info("Doing test on DUT port {} | PTF port {}".format(dut_port, ptf_src_port)) |
| 130 | + |
| 131 | + # get neighbor IP |
| 132 | + lines = duthost.command("show ipv6 bgp sum")['stdout'].split("\n") |
| 133 | + for line in lines: |
| 134 | + if neighbor in line: |
| 135 | + neighbor_ip = line.split()[0] |
| 136 | + assert neighbor_ip |
| 137 | + |
| 138 | + # use DUT portchannel if applicable |
| 139 | + pc_info = duthost.command("show int portchannel")['stdout'] |
| 140 | + if dut_port in pc_info: |
| 141 | + lines = pc_info.split("\n") |
| 142 | + for line in lines: |
| 143 | + if dut_port in line: |
| 144 | + dut_port = line.split()[1] |
| 145 | + |
| 146 | + sonic_db_cli = "sonic-db-cli" + cli_options |
| 147 | + |
| 148 | + # add a locator configuration entry |
| 149 | + duthost.command(sonic_db_cli + " CONFIG_DB HSET SRV6_MY_LOCATORS\\|loc1 prefix fcbb:bbbb:1::") |
| 150 | + # add a uN sid configuration entry |
| 151 | + duthost.command(sonic_db_cli + |
| 152 | + " CONFIG_DB HSET SRV6_MY_SIDS\\|loc1\\|fcbb:bbbb:1::/48 action uN decap_dscp_mode pipe") |
| 153 | + |
| 154 | + time.sleep(5) |
| 155 | + |
| 156 | + for i in range(0, 10): |
| 157 | + if with_srh: |
| 158 | + injected_pkt = simple_ipv6_sr_packet( |
| 159 | + eth_dst=dut_mac, |
| 160 | + eth_src=ptfadapter.dataplane.get_mac(0, ptf_src_port).decode(), |
| 161 | + ipv6_src=ptfadapter.ptf_ipv6, |
| 162 | + ipv6_dst="fcbb:bbbb:1::", |
| 163 | + srh_seg_left=1, |
| 164 | + srh_nh=41, |
| 165 | + inner_frame=IPv6(dst=neighbor_ip, src=ptfadapter.ptf_ipv6)/ICMPv6EchoRequest(seq=i) |
| 166 | + ) |
| 167 | + else: |
| 168 | + injected_pkt = Ether(dst=dut_mac, src=ptfadapter.dataplane.get_mac(0, ptf_src_port).decode()) \ |
| 169 | + / IPv6(src=ptfadapter.ptf_ipv6, dst="fcbb:bbbb:1::") \ |
| 170 | + / IPv6(dst=neighbor_ip, src=ptfadapter.ptf_ipv6) / ICMPv6EchoRequest(seq=i) |
| 171 | + |
| 172 | + expected_pkt = Ether(dst=get_neighbor_info(neighbor_ip, nbrhosts)['mac'], src=dut_mac) / \ |
| 173 | + IPv6(dst=neighbor_ip, src=ptfadapter.ptf_ipv6)/ICMPv6EchoRequest(seq=i) |
| 174 | + logger.debug("Expected packet #{}: {}".format(i, expected_pkt.summary())) |
| 175 | + runSendReceive(injected_pkt, ptf_src_port, expected_pkt, [ptf_src_port], True, ptfadapter) |
| 176 | + |
| 177 | + # delete the SRv6 configuration |
| 178 | + duthost.command(sonic_db_cli + " CONFIG_DB DEL SRV6_MY_LOCATORS\\|loc1") |
| 179 | + duthost.command(sonic_db_cli + " CONFIG_DB DEL SRV6_MY_SIDS\\|loc1\\|fcbb:bbbb:1::/48") |
0 commit comments