Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 2 | |
| 3 | import unittest |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 4 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 5 | from framework import VppTestCase |
| 6 | from asfframework import VppTestRunner |
| 7 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 8 | |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 9 | import scapy.compat |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 10 | from scapy.packet import Raw |
| 11 | from scapy.layers.l2 import Ether, Dot1Q |
| 12 | from scapy.layers.inet6 import IPv6, UDP, IPv6ExtHdrSegmentRouting |
| 13 | from scapy.layers.inet import IP, UDP |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 14 | from config import config |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 15 | |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 16 | from util import ppp |
| 17 | |
| 18 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 19 | @unittest.skipIf("srv6-as" in config.excluded_plugins, "Exclude srv6-as plugin tests") |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 20 | class TestSRv6As(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 21 | """SRv6 Static Proxy plugin Test Case""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 22 | |
| 23 | @classmethod |
| 24 | def setUpClass(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 25 | super(TestSRv6As, self).setUpClass() |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 26 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 27 | @classmethod |
| 28 | def tearDownClass(cls): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 29 | super(TestSRv6As, cls).tearDownClass() |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 30 | |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 31 | def setUp(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 32 | """Perform test setup before each test case.""" |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 33 | super(TestSRv6As, self).setUp() |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 34 | |
| 35 | # packet sizes, inclusive L2 overhead |
| 36 | self.pg_packet_sizes = [64, 512, 1518, 9018] |
| 37 | |
| 38 | # reset packet_infos |
| 39 | self.reset_packet_infos() |
| 40 | |
| 41 | def tearDown(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 42 | """Clean up test setup after each test case.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 43 | self.teardown_interfaces() |
| 44 | |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 45 | super(TestSRv6As, self).tearDown() |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 46 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 47 | def configure_interface( |
| 48 | self, interface, ipv6=False, ipv4=False, ipv6_table_id=0, ipv4_table_id=0 |
| 49 | ): |
| 50 | """Configure interface. |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 51 | :param ipv6: configure IPv6 on interface |
| 52 | :param ipv4: configure IPv4 on interface |
| 53 | :param ipv6_table_id: FIB table_id for IPv6 |
| 54 | :param ipv4_table_id: FIB table_id for IPv4 |
| 55 | """ |
| 56 | self.logger.debug("Configuring interface %s" % (interface.name)) |
| 57 | if ipv6: |
| 58 | self.logger.debug("Configuring IPv6") |
| 59 | interface.set_table_ip6(ipv6_table_id) |
| 60 | interface.config_ip6() |
| 61 | interface.resolve_ndp(timeout=5) |
| 62 | if ipv4: |
| 63 | self.logger.debug("Configuring IPv4") |
| 64 | interface.set_table_ip4(ipv4_table_id) |
| 65 | interface.config_ip4() |
| 66 | interface.resolve_arp() |
| 67 | interface.admin_up() |
| 68 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 69 | def setup_interfaces(self, ipv6=[], ipv4=[], ipv6_table_id=[], ipv4_table_id=[]): |
| 70 | """Create and configure interfaces. |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 71 | |
| 72 | :param ipv6: list of interface IPv6 capabilities |
| 73 | :param ipv4: list of interface IPv4 capabilities |
| 74 | :param ipv6_table_id: list of intf IPv6 FIB table_ids |
| 75 | :param ipv4_table_id: list of intf IPv4 FIB table_ids |
| 76 | :returns: List of created interfaces. |
| 77 | """ |
| 78 | # how many interfaces? |
| 79 | if len(ipv6): |
| 80 | count = len(ipv6) |
| 81 | else: |
| 82 | count = len(ipv4) |
| 83 | self.logger.debug("Creating and configuring %d interfaces" % (count)) |
| 84 | |
| 85 | # fill up ipv6 and ipv4 lists if needed |
| 86 | # not enabled (False) is the default |
| 87 | if len(ipv6) < count: |
| 88 | ipv6 += (count - len(ipv6)) * [False] |
| 89 | if len(ipv4) < count: |
| 90 | ipv4 += (count - len(ipv4)) * [False] |
| 91 | |
| 92 | # fill up table_id lists if needed |
| 93 | # table_id 0 (global) is the default |
| 94 | if len(ipv6_table_id) < count: |
| 95 | ipv6_table_id += (count - len(ipv6_table_id)) * [0] |
| 96 | if len(ipv4_table_id) < count: |
| 97 | ipv4_table_id += (count - len(ipv4_table_id)) * [0] |
| 98 | |
| 99 | # create 'count' pg interfaces |
| 100 | self.create_pg_interfaces(range(count)) |
| 101 | |
| 102 | # setup all interfaces |
| 103 | for i in range(count): |
| 104 | intf = self.pg_interfaces[i] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 105 | self.configure_interface( |
| 106 | intf, ipv6[i], ipv4[i], ipv6_table_id[i], ipv4_table_id[i] |
| 107 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 108 | |
| 109 | if any(ipv6): |
| 110 | self.logger.debug(self.vapi.cli("show ip6 neighbors")) |
| 111 | if any(ipv4): |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 112 | self.logger.debug(self.vapi.cli("show ip4 neighbors")) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 113 | self.logger.debug(self.vapi.cli("show interface")) |
| 114 | self.logger.debug(self.vapi.cli("show hardware")) |
| 115 | |
| 116 | return self.pg_interfaces |
| 117 | |
| 118 | def teardown_interfaces(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | """Unconfigure and bring down interface.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 120 | self.logger.debug("Tearing down interfaces") |
| 121 | # tear down all interfaces |
| 122 | # AFAIK they cannot be deleted |
| 123 | for i in self.pg_interfaces: |
| 124 | self.logger.debug("Tear down interface %s" % (i.name)) |
| 125 | i.admin_down() |
| 126 | i.unconfig() |
| 127 | i.set_table_ip4(0) |
| 128 | i.set_table_ip6(0) |
| 129 | |
| 130 | def test_SRv6_End_AS_IPv6_noSRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 131 | """Test SRv6 End.AS behavior with IPv6 traffic and no SRH rewrite.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 132 | self.run_SRv6_End_AS_IPv6( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 133 | sid_list=["a1::", "a2::a6", "a3::"], |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 134 | test_sid_index=1, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 135 | rewrite_src_addr="a2::", |
| 136 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 137 | |
| 138 | def test_SRv6_End_AS_IPv6_SRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 139 | """Test SRv6 End.AS behavior with IPv6 traffic and SRH rewrite.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 140 | self.run_SRv6_End_AS_IPv6( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 141 | sid_list=["a1::a6", "a2::", "a3::"], |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 142 | test_sid_index=0, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 143 | rewrite_src_addr="a1::", |
| 144 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 145 | |
| 146 | def test_SRv6_End_AS_IPv4_noSRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 147 | """Test SRv6 End.AS behavior with IPv4 traffic and no SRH rewrite.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 148 | self.run_SRv6_End_AS_IPv4( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 149 | sid_list=["a1::", "a2::a6", "a3::"], |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 150 | test_sid_index=1, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 151 | rewrite_src_addr="a2::", |
| 152 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 153 | |
| 154 | def test_SRv6_End_AS_IPv4_SRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 155 | """Test SRv6 End.AS behavior with IPv4 traffic and SRH rewrite.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 156 | self.run_SRv6_End_AS_IPv4( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 157 | sid_list=["a1::a6", "a2::", "a3::"], |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 158 | test_sid_index=0, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 159 | rewrite_src_addr="a1::", |
| 160 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 161 | |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 162 | def test_SRv6_End_AS_L2_noSRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 163 | """Test SRv6 End.AS behavior with L2 traffic and no SRH rewrite.""" |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 164 | self.run_SRv6_End_AS_L2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 165 | sid_list=["a1::", "a2::a6", "a3::"], |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 166 | test_sid_index=1, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 167 | rewrite_src_addr="a2::", |
| 168 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 169 | |
| 170 | def test_SRv6_End_AS_L2_SRH(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 171 | """Test SRv6 End.AS behavior with L2 traffic and SRH rewrite.""" |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 172 | self.run_SRv6_End_AS_L2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 173 | sid_list=["a1::a6", "a2::", "a3::"], |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 174 | test_sid_index=0, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 175 | rewrite_src_addr="a1::", |
| 176 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 177 | |
| 178 | def run_SRv6_End_AS_L2(self, sid_list, test_sid_index, rewrite_src_addr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 179 | """Run SRv6 End.AS test with L2 traffic.""" |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 180 | self.rewrite_src_addr = rewrite_src_addr |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 181 | self.rewrite_sid_list = sid_list[test_sid_index + 1 : :] |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 182 | |
| 183 | # send traffic to one destination interface |
| 184 | # source and destination interfaces are IPv6 only |
| 185 | self.setup_interfaces(ipv6=[True, False]) |
| 186 | |
| 187 | # configure route to next segment |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 188 | route = VppIpRoute( |
| 189 | self, |
| 190 | sid_list[test_sid_index + 1], |
| 191 | 128, |
| 192 | [VppRoutePath(self.pg0.remote_ip6, self.pg0.sw_if_index)], |
| 193 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 194 | route.add_vpp_config() |
| 195 | |
| 196 | # configure SRv6 localSID behavior |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 197 | cli_str = ( |
| 198 | "sr localsid address " |
| 199 | + sid_list[test_sid_index] |
| 200 | + " behavior end.as" |
| 201 | + " oif " |
| 202 | + self.pg1.name |
| 203 | + " iif " |
| 204 | + self.pg1.name |
| 205 | + " src " |
| 206 | + self.rewrite_src_addr |
| 207 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 208 | for s in self.rewrite_sid_list: |
| 209 | cli_str += " next " + s |
| 210 | self.vapi.cli(cli_str) |
| 211 | |
| 212 | # log the localsids |
| 213 | self.logger.debug(self.vapi.cli("show sr localsid")) |
| 214 | |
| 215 | # send one packet per packet size |
| 216 | count = len(self.pg_packet_sizes) |
| 217 | |
| 218 | # prepare L2 in SRv6 headers |
| 219 | packet_header1 = self.create_packet_header_IPv6_SRH_L2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 220 | sidlist=sid_list[::-1], segleft=len(sid_list) - test_sid_index - 1, vlan=0 |
| 221 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 222 | |
| 223 | # generate packets (pg0->pg1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 224 | pkts1 = self.create_stream( |
| 225 | self.pg0, self.pg1, packet_header1, self.pg_packet_sizes, count |
| 226 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 227 | |
| 228 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 229 | self.send_and_verify_pkts( |
| 230 | self.pg0, pkts1, self.pg1, self.compare_rx_tx_packet_End_AS_L2_out |
| 231 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 232 | |
| 233 | # log the localsid counters |
| 234 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 235 | |
| 236 | # prepare L2 header for returning packets |
| 237 | packet_header2 = self.create_packet_header_L2() |
| 238 | |
| 239 | # generate returning packets (pg1->pg0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 240 | pkts2 = self.create_stream( |
| 241 | self.pg1, self.pg0, packet_header2, self.pg_packet_sizes, count |
| 242 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 243 | |
| 244 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 245 | self.send_and_verify_pkts( |
| 246 | self.pg1, pkts2, self.pg0, self.compare_rx_tx_packet_End_AS_L2_in |
| 247 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 248 | |
| 249 | # log the localsid counters |
| 250 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 251 | |
| 252 | # remove SRv6 localSIDs |
| 253 | self.vapi.cli("sr localsid del address " + sid_list[test_sid_index]) |
| 254 | |
| 255 | # cleanup interfaces |
| 256 | self.teardown_interfaces() |
| 257 | |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 258 | def run_SRv6_End_AS_IPv6(self, sid_list, test_sid_index, rewrite_src_addr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 259 | """Run SRv6 End.AS test with IPv6 traffic.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 260 | self.rewrite_src_addr = rewrite_src_addr |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 261 | self.rewrite_sid_list = sid_list[test_sid_index + 1 : :] |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 262 | |
| 263 | # send traffic to one destination interface |
| 264 | # source and destination interfaces are IPv6 only |
| 265 | self.setup_interfaces(ipv6=[True, True]) |
| 266 | |
| 267 | # configure route to next segment |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 268 | route = VppIpRoute( |
| 269 | self, |
| 270 | sid_list[test_sid_index + 1], |
| 271 | 128, |
| 272 | [VppRoutePath(self.pg0.remote_ip6, self.pg0.sw_if_index)], |
| 273 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 274 | route.add_vpp_config() |
| 275 | |
| 276 | # configure SRv6 localSID behavior |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 277 | cli_str = ( |
| 278 | "sr localsid address " |
| 279 | + sid_list[test_sid_index] |
| 280 | + " behavior end.as" |
| 281 | + " nh " |
| 282 | + self.pg1.remote_ip6 |
| 283 | + " oif " |
| 284 | + self.pg1.name |
| 285 | + " iif " |
| 286 | + self.pg1.name |
| 287 | + " src " |
| 288 | + self.rewrite_src_addr |
| 289 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 290 | for s in self.rewrite_sid_list: |
| 291 | cli_str += " next " + s |
| 292 | self.vapi.cli(cli_str) |
| 293 | |
| 294 | # log the localsids |
| 295 | self.logger.debug(self.vapi.cli("show sr localsid")) |
| 296 | |
| 297 | # send one packet per packet size |
| 298 | count = len(self.pg_packet_sizes) |
| 299 | |
| 300 | # prepare IPv6 in SRv6 headers |
| 301 | packet_header1 = self.create_packet_header_IPv6_SRH_IPv6( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 302 | sidlist=sid_list[::-1], segleft=len(sid_list) - test_sid_index - 1 |
| 303 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 304 | |
| 305 | # generate packets (pg0->pg1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 306 | pkts1 = self.create_stream( |
| 307 | self.pg0, self.pg1, packet_header1, self.pg_packet_sizes, count |
| 308 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 309 | |
| 310 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 311 | self.send_and_verify_pkts( |
| 312 | self.pg0, pkts1, self.pg1, self.compare_rx_tx_packet_End_AS_IPv6_out |
| 313 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 314 | |
| 315 | # log the localsid counters |
| 316 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 317 | |
| 318 | # prepare IPv6 header for returning packets |
| 319 | packet_header2 = self.create_packet_header_IPv6() |
| 320 | |
| 321 | # generate returning packets (pg1->pg0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 322 | pkts2 = self.create_stream( |
| 323 | self.pg1, self.pg0, packet_header2, self.pg_packet_sizes, count |
| 324 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 325 | |
| 326 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 327 | self.send_and_verify_pkts( |
| 328 | self.pg1, pkts2, self.pg0, self.compare_rx_tx_packet_End_AS_IPv6_in |
| 329 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 330 | |
| 331 | # log the localsid counters |
| 332 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 333 | |
| 334 | # remove SRv6 localSIDs |
| 335 | self.vapi.cli("sr localsid del address " + sid_list[test_sid_index]) |
| 336 | |
| 337 | # cleanup interfaces |
| 338 | self.teardown_interfaces() |
| 339 | |
| 340 | def run_SRv6_End_AS_IPv4(self, sid_list, test_sid_index, rewrite_src_addr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 341 | """Run SRv6 End.AS test with IPv4 traffic.""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 342 | self.rewrite_src_addr = rewrite_src_addr |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 343 | self.rewrite_sid_list = sid_list[test_sid_index + 1 : :] |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 344 | |
| 345 | # send traffic to one destination interface |
| 346 | # source and destination interfaces are IPv6 only |
| 347 | self.setup_interfaces(ipv6=[True, False], ipv4=[True, True]) |
| 348 | |
| 349 | # configure route to next segment |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 350 | route = VppIpRoute( |
| 351 | self, |
| 352 | sid_list[test_sid_index + 1], |
| 353 | 128, |
| 354 | [VppRoutePath(self.pg0.remote_ip6, self.pg0.sw_if_index)], |
| 355 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 356 | route.add_vpp_config() |
| 357 | |
| 358 | # configure SRv6 localSID behavior |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 359 | cli_str = ( |
| 360 | "sr localsid address " |
| 361 | + sid_list[test_sid_index] |
| 362 | + " behavior end.as" |
| 363 | + " nh " |
| 364 | + self.pg1.remote_ip4 |
| 365 | + " oif " |
| 366 | + self.pg1.name |
| 367 | + " iif " |
| 368 | + self.pg1.name |
| 369 | + " src " |
| 370 | + self.rewrite_src_addr |
| 371 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 372 | for s in self.rewrite_sid_list: |
| 373 | cli_str += " next " + s |
| 374 | self.vapi.cli(cli_str) |
| 375 | |
| 376 | # log the localsids |
| 377 | self.logger.debug(self.vapi.cli("show sr localsid")) |
| 378 | |
| 379 | # send one packet per packet size |
| 380 | count = len(self.pg_packet_sizes) |
| 381 | |
| 382 | # prepare IPv4 in SRv6 headers |
| 383 | packet_header1 = self.create_packet_header_IPv6_SRH_IPv4( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 384 | sidlist=sid_list[::-1], segleft=len(sid_list) - test_sid_index - 1 |
| 385 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 386 | |
| 387 | # generate packets (pg0->pg1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 388 | pkts1 = self.create_stream( |
| 389 | self.pg0, self.pg1, packet_header1, self.pg_packet_sizes, count |
| 390 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 391 | |
| 392 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 393 | self.send_and_verify_pkts( |
| 394 | self.pg0, pkts1, self.pg1, self.compare_rx_tx_packet_End_AS_IPv4_out |
| 395 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 396 | |
| 397 | # log the localsid counters |
| 398 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 399 | |
| 400 | # prepare IPv6 header for returning packets |
| 401 | packet_header2 = self.create_packet_header_IPv4() |
| 402 | |
| 403 | # generate returning packets (pg1->pg0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 404 | pkts2 = self.create_stream( |
| 405 | self.pg1, self.pg0, packet_header2, self.pg_packet_sizes, count |
| 406 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 407 | |
| 408 | # send packets and verify received packets |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 409 | self.send_and_verify_pkts( |
| 410 | self.pg1, pkts2, self.pg0, self.compare_rx_tx_packet_End_AS_IPv4_in |
| 411 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 412 | |
| 413 | # log the localsid counters |
| 414 | self.logger.info(self.vapi.cli("show sr localsid")) |
| 415 | |
| 416 | # remove SRv6 localSIDs |
| 417 | self.vapi.cli("sr localsid del address " + sid_list[test_sid_index]) |
| 418 | |
| 419 | # cleanup interfaces |
| 420 | self.teardown_interfaces() |
| 421 | |
| 422 | def compare_rx_tx_packet_End_AS_IPv6_in(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 423 | """Compare input and output packet after passing End.AS |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 424 | |
| 425 | :param tx_pkt: transmitted packet |
| 426 | :param rx_pkt: received packet |
| 427 | """ |
| 428 | |
| 429 | # get first (outer) IPv6 header of rx'ed packet |
| 430 | rx_ip = rx_pkt.getlayer(IPv6) |
| 431 | rx_srh = None |
| 432 | |
| 433 | tx_ip = tx_pkt.getlayer(IPv6) |
| 434 | |
| 435 | # expected segment-list (SRH order) |
| 436 | tx_seglist = self.rewrite_sid_list[::-1] |
| 437 | |
| 438 | # received ip.src should be equal to SR Policy source |
| 439 | self.assertEqual(rx_ip.src, self.rewrite_src_addr) |
| 440 | # received ip.dst should be equal to expected sidlist[lastentry] |
| 441 | self.assertEqual(rx_ip.dst, tx_seglist[-1]) |
| 442 | |
| 443 | if len(tx_seglist) > 1: |
| 444 | # rx'ed packet should have SRH |
| 445 | self.assertTrue(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 446 | # get SRH |
| 447 | rx_srh = rx_pkt.getlayer(IPv6ExtHdrSegmentRouting) |
| 448 | # rx'ed seglist should be equal to expected seglist |
| 449 | self.assertEqual(rx_srh.addresses, tx_seglist) |
| 450 | # segleft should be equal to size expected seglist-1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 451 | self.assertEqual(rx_srh.segleft, len(tx_seglist) - 1) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 452 | # segleft should be equal to lastentry |
| 453 | self.assertEqual(rx_srh.segleft, rx_srh.lastentry) |
| 454 | # get payload |
| 455 | payload = rx_srh.payload |
| 456 | else: |
| 457 | # rx'ed packet should NOT have SRH |
| 458 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 459 | # get payload |
| 460 | payload = rx_ip.payload |
| 461 | |
| 462 | # the whole rx'ed pkt beyond SRH should be equal to tx'ed pkt |
| 463 | # except for the hop-limit field |
| 464 | # -> update tx'ed hlim to the expected hlim |
| 465 | tx_ip.hlim = tx_ip.hlim - 1 |
| 466 | |
| 467 | self.assertEqual(payload, tx_ip) |
| 468 | |
| 469 | self.logger.debug("packet verification: SUCCESS") |
| 470 | |
| 471 | def compare_rx_tx_packet_End_AS_IPv4_in(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 472 | """Compare input and output packet after passing End.AS |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 473 | |
| 474 | :param tx_pkt: transmitted packet |
| 475 | :param rx_pkt: received packet |
| 476 | """ |
| 477 | |
| 478 | # get first (outer) IPv6 header of rx'ed packet |
| 479 | rx_ip = rx_pkt.getlayer(IPv6) |
| 480 | rx_srh = None |
| 481 | |
| 482 | tx_ip = tx_pkt.getlayer(IP) |
| 483 | |
| 484 | # expected segment-list (SRH order) |
| 485 | tx_seglist = self.rewrite_sid_list[::-1] |
| 486 | |
| 487 | # received ip.src should be equal to SR Policy source |
| 488 | self.assertEqual(rx_ip.src, self.rewrite_src_addr) |
| 489 | # received ip.dst should be equal to expected sidlist[lastentry] |
| 490 | self.assertEqual(rx_ip.dst, tx_seglist[-1]) |
| 491 | |
| 492 | if len(tx_seglist) > 1: |
| 493 | # rx'ed packet should have SRH and IPv4 header |
| 494 | self.assertTrue(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 495 | self.assertTrue(rx_ip.payload.haslayer(IP)) |
| 496 | # get SRH |
| 497 | rx_srh = rx_pkt.getlayer(IPv6ExtHdrSegmentRouting) |
| 498 | # rx'ed seglist should be equal to seglist |
| 499 | self.assertEqual(rx_srh.addresses, tx_seglist) |
| 500 | # segleft should be equal to size seglist-1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 501 | self.assertEqual(rx_srh.segleft, len(tx_seglist) - 1) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 502 | # segleft should be equal to lastentry |
| 503 | self.assertEqual(rx_srh.segleft, rx_srh.lastentry) |
| 504 | payload = rx_srh.payload |
| 505 | else: |
| 506 | # rx'ed packet should NOT have SRH |
| 507 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 508 | # get payload |
| 509 | payload = rx_ip.payload |
| 510 | |
| 511 | # the whole rx'ed pkt beyond SRH should be equal to tx'ed pkt |
| 512 | # except for the ttl field and ip checksum |
| 513 | # -> adjust tx'ed ttl to expected ttl |
| 514 | tx_ip.ttl = tx_ip.ttl - 1 |
| 515 | # -> set tx'ed ip checksum to None and let scapy recompute |
| 516 | tx_ip.chksum = None |
| 517 | # read back the pkt (with str()) to force computing these fields |
| 518 | # probably other ways to accomplish this are possible |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 519 | tx_ip = IP(scapy.compat.raw(tx_ip)) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 520 | |
| 521 | self.assertEqual(payload, tx_ip) |
| 522 | |
| 523 | self.logger.debug("packet verification: SUCCESS") |
| 524 | |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 525 | def compare_rx_tx_packet_End_AS_L2_in(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 526 | """Compare input and output packet after passing End.AS |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 527 | |
| 528 | :param tx_pkt: transmitted packet |
| 529 | :param rx_pkt: received packet |
| 530 | """ |
| 531 | |
| 532 | # get first (outer) IPv6 header of rx'ed packet |
| 533 | rx_ip = rx_pkt.getlayer(IPv6) |
| 534 | rx_srh = None |
| 535 | |
| 536 | tx_ether = tx_pkt.getlayer(Ether) |
| 537 | |
| 538 | # expected segment-list (SRH order) |
| 539 | tx_seglist = self.rewrite_sid_list[::-1] |
| 540 | |
| 541 | # received ip.src should be equal to SR Policy source |
| 542 | self.assertEqual(rx_ip.src, self.rewrite_src_addr) |
| 543 | # received ip.dst should be equal to expected sidlist[lastentry] |
| 544 | self.assertEqual(rx_ip.dst, tx_seglist[-1]) |
| 545 | |
| 546 | if len(tx_seglist) > 1: |
| 547 | # rx'ed packet should have SRH |
| 548 | self.assertTrue(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 549 | # get SRH |
| 550 | rx_srh = rx_pkt.getlayer(IPv6ExtHdrSegmentRouting) |
| 551 | # rx'ed seglist should be equal to seglist |
| 552 | self.assertEqual(rx_srh.addresses, tx_seglist) |
| 553 | # segleft should be equal to size seglist-1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 554 | self.assertEqual(rx_srh.segleft, len(tx_seglist) - 1) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 555 | # segleft should be equal to lastentry |
| 556 | self.assertEqual(rx_srh.segleft, rx_srh.lastentry) |
pcamaril | 30e7671 | 2020-02-04 08:36:51 +0100 | [diff] [blame] | 557 | # nh should be "No Next Header" (143) |
| 558 | self.assertEqual(rx_srh.nh, 143) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 559 | # get payload |
| 560 | payload = rx_srh.payload |
| 561 | else: |
| 562 | # rx'ed packet should NOT have SRH |
| 563 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 564 | # get payload |
| 565 | payload = rx_ip.payload |
| 566 | |
| 567 | # the whole rx'ed pkt beyond SRH should be equal to tx'ed pkt |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 568 | self.assertEqual(Ether(scapy.compat.raw(payload)), tx_ether) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 569 | |
| 570 | self.logger.debug("packet verification: SUCCESS") |
| 571 | |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 572 | def compare_rx_tx_packet_End_AS_IPv6_out(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 573 | """Compare input and output packet after passing End.AS with IPv6 |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 574 | |
| 575 | :param tx_pkt: transmitted packet |
| 576 | :param rx_pkt: received packet |
| 577 | """ |
| 578 | |
| 579 | # get first (outer) IPv6 header of rx'ed packet |
| 580 | rx_ip = rx_pkt.getlayer(IPv6) |
| 581 | |
| 582 | tx_ip = tx_pkt.getlayer(IPv6) |
| 583 | tx_ip2 = tx_pkt.getlayer(IPv6, 2) |
| 584 | |
| 585 | # verify if rx'ed packet has no SRH |
| 586 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 587 | |
| 588 | # the whole rx_ip pkt should be equal to tx_ip2 |
| 589 | # except for the hlim field |
| 590 | # -> adjust tx'ed hlim to expected hlim |
| 591 | tx_ip2.hlim = tx_ip2.hlim - 1 |
| 592 | |
| 593 | self.assertEqual(rx_ip, tx_ip2) |
| 594 | |
| 595 | self.logger.debug("packet verification: SUCCESS") |
| 596 | |
| 597 | def compare_rx_tx_packet_End_AS_IPv4_out(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 598 | """Compare input and output packet after passing End.AS with IPv4 |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 599 | |
| 600 | :param tx_pkt: transmitted packet |
| 601 | :param rx_pkt: received packet |
| 602 | """ |
| 603 | |
| 604 | # get IPv4 header of rx'ed packet |
| 605 | rx_ip = rx_pkt.getlayer(IP) |
| 606 | |
| 607 | tx_ip = tx_pkt.getlayer(IPv6) |
| 608 | tx_ip2 = tx_pkt.getlayer(IP) |
| 609 | |
| 610 | # verify if rx'ed packet has no SRH |
| 611 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 612 | |
| 613 | # the whole rx_ip pkt should be equal to tx_ip2 |
| 614 | # except for the ttl field and ip checksum |
| 615 | # -> adjust tx'ed ttl to expected ttl |
| 616 | tx_ip2.ttl = tx_ip2.ttl - 1 |
| 617 | # -> set tx'ed ip checksum to None and let scapy recompute |
| 618 | tx_ip2.chksum = None |
| 619 | # read back the pkt (with str()) to force computing these fields |
| 620 | # probably other ways to accomplish this are possible |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 621 | tx_ip2 = IP(scapy.compat.raw(tx_ip2)) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 622 | |
| 623 | self.assertEqual(rx_ip, tx_ip2) |
| 624 | |
| 625 | self.logger.debug("packet verification: SUCCESS") |
| 626 | |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 627 | def compare_rx_tx_packet_End_AS_L2_out(self, tx_pkt, rx_pkt): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 628 | """Compare input and output packet after passing End.AS with L2 |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 629 | |
| 630 | :param tx_pkt: transmitted packet |
| 631 | :param rx_pkt: received packet |
| 632 | """ |
| 633 | |
| 634 | # get IPv4 header of rx'ed packet |
| 635 | rx_eth = rx_pkt.getlayer(Ether) |
| 636 | |
| 637 | tx_ip = tx_pkt.getlayer(IPv6) |
| 638 | # we can't just get the 2nd Ether layer |
| 639 | # get the Raw content and dissect it as Ether |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 640 | tx_eth1 = Ether(scapy.compat.raw(tx_pkt[Raw])) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 641 | |
| 642 | # verify if rx'ed packet has no SRH |
| 643 | self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting)) |
| 644 | |
| 645 | # the whole rx_eth pkt should be equal to tx_eth1 |
| 646 | self.assertEqual(rx_eth, tx_eth1) |
| 647 | |
| 648 | self.logger.debug("packet verification: SUCCESS") |
| 649 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 650 | def create_stream(self, src_if, dst_if, packet_header, packet_sizes, count): |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 651 | """Create SRv6 input packet stream for defined interface. |
| 652 | |
| 653 | :param VppInterface src_if: Interface to create packet stream for |
| 654 | :param VppInterface dst_if: destination interface of packet stream |
| 655 | :param packet_header: Layer3 scapy packet headers, |
| 656 | L2 is added when not provided, |
| 657 | Raw(payload) with packet_info is added |
| 658 | :param list packet_sizes: packet stream pckt sizes,sequentially applied |
| 659 | to packets in stream have |
| 660 | :param int count: number of packets in packet stream |
| 661 | :return: list of packets |
| 662 | """ |
| 663 | self.logger.info("Creating packets") |
| 664 | pkts = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 665 | for i in range(0, count - 1): |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 666 | payload_info = self.create_packet_info(src_if, dst_if) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 667 | self.logger.debug("Creating packet with index %d" % (payload_info.index)) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 668 | payload = self.info_to_payload(payload_info) |
| 669 | # add L2 header if not yet provided in packet_header |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 670 | if packet_header.getlayer(0).name == "Ethernet": |
| 671 | p = packet_header / Raw(payload) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 672 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 673 | p = ( |
| 674 | Ether(dst=src_if.local_mac, src=src_if.remote_mac) |
| 675 | / packet_header |
| 676 | / Raw(payload) |
| 677 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 678 | size = packet_sizes[i % len(packet_sizes)] |
| 679 | self.logger.debug("Packet size %d" % (size)) |
| 680 | self.extend_packet(p, size) |
| 681 | # we need to store the packet with the automatic fields computed |
| 682 | # read back the dumped packet (with str()) |
| 683 | # to force computing these fields |
| 684 | # probably other ways are possible |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 685 | p = Ether(scapy.compat.raw(p)) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 686 | payload_info.data = p.copy() |
| 687 | self.logger.debug(ppp("Created packet:", p)) |
| 688 | pkts.append(p) |
| 689 | self.logger.info("Done creating packets") |
| 690 | return pkts |
| 691 | |
| 692 | def send_and_verify_pkts(self, input, pkts, output, compare_func): |
| 693 | """Send packets and verify received packets using compare_func |
| 694 | |
| 695 | :param input: ingress interface of DUT |
| 696 | :param pkts: list of packets to transmit |
| 697 | :param output: egress interface of DUT |
| 698 | :param compare_func: function to compare in and out packets |
| 699 | """ |
| 700 | # add traffic stream to input interface |
| 701 | input.add_stream(pkts) |
| 702 | |
| 703 | # enable capture on all interfaces |
| 704 | self.pg_enable_capture(self.pg_interfaces) |
| 705 | |
| 706 | # start traffic |
| 707 | self.logger.info("Starting traffic") |
| 708 | self.pg_start() |
| 709 | |
| 710 | # get output capture |
| 711 | self.logger.info("Getting packet capture") |
| 712 | capture = output.get_capture() |
| 713 | |
| 714 | # assert nothing was captured on input interface |
| 715 | # input.assert_nothing_captured() |
| 716 | |
| 717 | # verify captured packets |
| 718 | self.verify_captured_pkts(output, capture, compare_func) |
| 719 | |
| 720 | def create_packet_header_IPv6(self): |
| 721 | """Create packet header: IPv6 header, UDP header |
| 722 | |
| 723 | :param dst: IPv6 destination address |
| 724 | |
| 725 | IPv6 source address is 1234::1 |
| 726 | IPv6 destination address is 4321::1 |
| 727 | UDP source port and destination port are 1234 |
| 728 | """ |
| 729 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 730 | p = IPv6(src="1234::1", dst="4321::1") / UDP(sport=1234, dport=1234) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 731 | return p |
| 732 | |
| 733 | def create_packet_header_IPv6_SRH_IPv6(self, sidlist, segleft): |
| 734 | """Create packet header: IPv6 encapsulated in SRv6: |
| 735 | IPv6 header with SRH, IPv6 header, UDP header |
| 736 | |
| 737 | :param list sidlist: segment list of outer IPv6 SRH |
| 738 | :param int segleft: segments-left field of outer IPv6 SRH |
| 739 | |
| 740 | Outer IPv6 source address is set to 5678::1 |
| 741 | Outer IPv6 destination address is set to sidlist[segleft] |
| 742 | IPv6 source addresses is 1234::1 |
| 743 | IPv6 destination address is 4321::1 |
| 744 | UDP source port and destination port are 1234 |
| 745 | """ |
| 746 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 747 | p = ( |
| 748 | IPv6(src="5678::1", dst=sidlist[segleft]) |
| 749 | / IPv6ExtHdrSegmentRouting(addresses=sidlist, segleft=segleft, nh=41) |
| 750 | / IPv6(src="1234::1", dst="4321::1") |
| 751 | / UDP(sport=1234, dport=1234) |
| 752 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 753 | return p |
| 754 | |
| 755 | def create_packet_header_IPv4(self): |
| 756 | """Create packet header: IPv4 header, UDP header |
| 757 | |
| 758 | :param dst: IPv4 destination address |
| 759 | |
| 760 | IPv4 source address is 123.1.1.1 |
| 761 | IPv4 destination address is 124.1.1.1 |
| 762 | UDP source port and destination port are 1234 |
| 763 | """ |
| 764 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 765 | p = IP(src="123.1.1.1", dst="124.1.1.1") / UDP(sport=1234, dport=1234) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 766 | return p |
| 767 | |
| 768 | def create_packet_header_IPv6_SRH_IPv4(self, sidlist, segleft): |
| 769 | """Create packet header: IPv4 encapsulated in SRv6: |
| 770 | IPv6 header with SRH, IPv4 header, UDP header |
| 771 | |
| 772 | :param ipv4address dst: inner IPv4 destination address |
| 773 | :param list sidlist: segment list of outer IPv6 SRH |
| 774 | :param int segleft: segments-left field of outer IPv6 SRH |
| 775 | |
| 776 | Outer IPv6 destination address is set to sidlist[segleft] |
| 777 | IPv6 source address is 1234::1 |
| 778 | IPv4 source address is 123.1.1.1 |
| 779 | IPv4 destination address is 124.1.1.1 |
| 780 | UDP source port and destination port are 1234 |
| 781 | """ |
| 782 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 783 | p = ( |
| 784 | IPv6(src="1234::1", dst=sidlist[segleft]) |
| 785 | / IPv6ExtHdrSegmentRouting(addresses=sidlist, segleft=segleft, nh=4) |
| 786 | / IP(src="123.1.1.1", dst="124.1.1.1") |
| 787 | / UDP(sport=1234, dport=1234) |
| 788 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 789 | return p |
| 790 | |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 791 | def create_packet_header_L2(self, vlan=0): |
| 792 | """Create packet header: L2 header |
| 793 | |
| 794 | :param vlan: if vlan!=0 then add 802.1q header |
| 795 | """ |
| 796 | # Note: the dst addr ('00:55:44:33:22:11') is used in |
| 797 | # the compare function compare_rx_tx_packet_T_Encaps_L2 |
| 798 | # to detect presence of L2 in SRH payload |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 799 | p = Ether(src="00:11:22:33:44:55", dst="00:55:44:33:22:11") |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 800 | etype = 0x8137 # IPX |
| 801 | if vlan: |
| 802 | # add 802.1q layer |
| 803 | p /= Dot1Q(vlan=vlan, type=etype) |
| 804 | else: |
| 805 | p.type = etype |
| 806 | return p |
| 807 | |
| 808 | def create_packet_header_IPv6_SRH_L2(self, sidlist, segleft, vlan=0): |
| 809 | """Create packet header: L2 encapsulated in SRv6: |
| 810 | IPv6 header with SRH, L2 |
| 811 | |
| 812 | :param list sidlist: segment list of outer IPv6 SRH |
| 813 | :param int segleft: segments-left field of outer IPv6 SRH |
| 814 | :param vlan: L2 vlan; if vlan!=0 then add 802.1q header |
| 815 | |
| 816 | Outer IPv6 destination address is set to sidlist[segleft] |
| 817 | IPv6 source address is 1234::1 |
| 818 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 819 | eth = Ether(src="00:11:22:33:44:55", dst="00:55:44:33:22:11") |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 820 | etype = 0x8137 # IPX |
| 821 | if vlan: |
| 822 | # add 802.1q layer |
| 823 | eth /= Dot1Q(vlan=vlan, type=etype) |
| 824 | else: |
| 825 | eth.type = etype |
| 826 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 827 | p = ( |
| 828 | IPv6(src="1234::1", dst=sidlist[segleft]) |
| 829 | / IPv6ExtHdrSegmentRouting(addresses=sidlist, segleft=segleft, nh=143) |
| 830 | / eth |
| 831 | ) |
Francois Clad | b02f3b7 | 2018-07-04 11:47:21 +0200 | [diff] [blame] | 832 | return p |
| 833 | |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 834 | def get_payload_info(self, packet): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 835 | """Extract the payload_info from the packet""" |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 836 | # in most cases, payload_info is in packet[Raw] |
| 837 | # but packet[Raw] gives the complete payload |
| 838 | # (incl L2 header) for the T.Encaps L2 case |
| 839 | try: |
Paul Vinciguerra | eaea421 | 2019-03-06 11:58:06 -0800 | [diff] [blame] | 840 | payload_info = self.payload_to_info(packet[Raw]) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 841 | |
| 842 | except: |
| 843 | # remote L2 header from packet[Raw]: |
| 844 | # take packet[Raw], convert it to an Ether layer |
| 845 | # and then extract Raw from it |
| 846 | payload_info = self.payload_to_info( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 847 | Ether(scapy.compat.raw(packet[Raw]))[Raw] |
| 848 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 849 | |
| 850 | return payload_info |
| 851 | |
| 852 | def verify_captured_pkts(self, dst_if, capture, compare_func): |
| 853 | """ |
| 854 | Verify captured packet stream for specified interface. |
| 855 | Compare ingress with egress packets using the specified compare fn |
| 856 | |
| 857 | :param dst_if: egress interface of DUT |
| 858 | :param capture: captured packets |
| 859 | :param compare_func: function to compare in and out packet |
| 860 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 861 | self.logger.info( |
| 862 | "Verifying capture on interface %s using function %s" |
| 863 | % (dst_if.name, compare_func.__name__) |
| 864 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 865 | |
| 866 | last_info = dict() |
| 867 | for i in self.pg_interfaces: |
| 868 | last_info[i.sw_if_index] = None |
| 869 | dst_sw_if_index = dst_if.sw_if_index |
| 870 | |
| 871 | for packet in capture: |
| 872 | try: |
| 873 | # extract payload_info from packet's payload |
| 874 | payload_info = self.get_payload_info(packet) |
| 875 | packet_index = payload_info.index |
| 876 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 877 | self.logger.debug("Verifying packet with index %d" % (packet_index)) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 878 | # packet should have arrived on the expected interface |
| 879 | self.assertEqual(payload_info.dst, dst_sw_if_index) |
| 880 | self.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 881 | "Got packet on interface %s: src=%u (idx=%u)" |
| 882 | % (dst_if.name, payload_info.src, packet_index) |
| 883 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 884 | |
| 885 | # search for payload_info with same src and dst if_index |
| 886 | # this will give us the transmitted packet |
| 887 | next_info = self.get_next_packet_info_for_interface2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 888 | payload_info.src, dst_sw_if_index, last_info[payload_info.src] |
| 889 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 890 | last_info[payload_info.src] = next_info |
| 891 | # next_info should not be None |
| 892 | self.assertTrue(next_info is not None) |
| 893 | # index of tx and rx packets should be equal |
| 894 | self.assertEqual(packet_index, next_info.index) |
| 895 | # data field of next_info contains the tx packet |
| 896 | txed_packet = next_info.data |
| 897 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 898 | self.logger.debug( |
| 899 | ppp("Transmitted packet:", txed_packet) |
| 900 | ) # ppp=Pretty Print Packet |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 901 | |
| 902 | self.logger.debug(ppp("Received packet:", packet)) |
| 903 | |
| 904 | # compare rcvd packet with expected packet using compare_func |
| 905 | compare_func(txed_packet, packet) |
| 906 | |
| 907 | except: |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 908 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 909 | raise |
| 910 | |
| 911 | # have all expected packets arrived? |
| 912 | for i in self.pg_interfaces: |
| 913 | remaining_packet = self.get_next_packet_info_for_interface2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 914 | i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index] |
| 915 | ) |
| 916 | self.assertTrue( |
| 917 | remaining_packet is None, |
| 918 | "Interface %s: Packet expected from interface %s " |
| 919 | "didn't arrive" % (dst_if.name, i.name), |
| 920 | ) |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 921 | |
| 922 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 923 | if __name__ == "__main__": |
Francois Clad | 0928da9 | 2018-07-09 16:45:23 +0200 | [diff] [blame] | 924 | unittest.main(testRunner=VppTestRunner) |