Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """IP4 VRF Multi-instance Test Case HLD: |
| 3 | |
| 4 | **NOTES:** |
| 5 | - higher number of pg-ip4 interfaces causes problems => only 15 pg-ip4 \ |
| 6 | interfaces in 5 VRFs are tested |
| 7 | - jumbo packets in configuration with 15 pg-ip4 interfaces leads to \ |
| 8 | problems too |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 9 | |
| 10 | **config 1** |
| 11 | - add 15 pg-ip4 interfaces |
| 12 | - configure 5 hosts per pg-ip4 interface |
| 13 | - configure 4 VRFs |
| 14 | - add 3 pg-ip4 interfaces per VRF |
| 15 | |
| 16 | **test 1** |
| 17 | - send IP4 packets between all pg-ip4 interfaces in all VRF groups |
| 18 | |
| 19 | **verify 1** |
| 20 | - check VRF data by parsing output of ip_fib_dump API command |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 21 | - all packets received correctly in case of pg-ip4 interfaces in the same |
| 22 | VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 23 | - no packet received in case of pg-ip4 interfaces not in VRF |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 24 | - no packet received in case of pg-ip4 interfaces in different VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 25 | |
| 26 | **config 2** |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 27 | - reset 2 VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 28 | |
| 29 | **test 2** |
| 30 | - send IP4 packets between all pg-ip4 interfaces in all VRF groups |
| 31 | |
| 32 | **verify 2** |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 33 | - all packets received correctly in case of pg-ip4 interfaces in the same |
| 34 | VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 35 | - no packet received in case of pg-ip4 interfaces not in VRF |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 36 | - no packet received in case of pg-ip4 interfaces in different VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 37 | |
| 38 | **config 3** |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 39 | - add 1 of reset VRFs and 1 new VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 40 | |
| 41 | **test 3** |
| 42 | - send IP4 packets between all pg-ip4 interfaces in all VRF groups |
| 43 | |
| 44 | **verify 3** |
| 45 | - check VRF data by parsing output of ip_fib_dump API command |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 46 | - all packets received correctly in case of pg-ip4 interfaces in the same |
| 47 | VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 48 | - no packet received in case of pg-ip4 interfaces not in VRF |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 49 | - no packet received in case of pg-ip4 interfaces in different VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 50 | |
| 51 | **config 4** |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 52 | - reset all created VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 53 | |
| 54 | **test 4** |
| 55 | - send IP4 packets between all pg-ip4 interfaces in all VRF groups |
| 56 | |
| 57 | **verify 4** |
| 58 | - check VRF data by parsing output of ip_fib_dump API command |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 59 | - all packets received correctly in case of pg-ip4 interfaces in the same |
| 60 | VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 61 | - no packet received in case of pg-ip4 interfaces not in VRF |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 62 | - no packet received in case of pg-ip4 interfaces in different VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 63 | """ |
| 64 | |
| 65 | import unittest |
| 66 | import random |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 67 | import socket |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 68 | |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 69 | import scapy.compat |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 70 | from scapy.packet import Raw |
| 71 | from scapy.layers.l2 import Ether |
| 72 | from scapy.layers.inet import IP, UDP, ARP |
| 73 | |
| 74 | from framework import VppTestCase, VppTestRunner |
| 75 | from util import ppp |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 76 | from vrf import VRFState |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def is_ipv4_misc(p): |
| 80 | """ Is packet one of uninteresting IPv4 broadcasts? """ |
| 81 | if p.haslayer(ARP): |
| 82 | return True |
| 83 | return False |
| 84 | |
| 85 | |
| 86 | class TestIp4VrfMultiInst(VppTestCase): |
| 87 | """ IP4 VRF Multi-instance Test Case """ |
| 88 | |
| 89 | @classmethod |
| 90 | def setUpClass(cls): |
| 91 | """ |
| 92 | Perform standard class setup (defined by class method setUpClass in |
| 93 | class VppTestCase) before running the test case, set test case related |
| 94 | variables and configure VPP. |
| 95 | """ |
| 96 | super(TestIp4VrfMultiInst, cls).setUpClass() |
| 97 | |
| 98 | # Test variables |
| 99 | cls.hosts_per_pg = 5 |
| 100 | cls.nr_of_vrfs = 5 |
| 101 | cls.pg_ifs_per_vrf = 3 |
| 102 | |
| 103 | try: |
| 104 | # Create pg interfaces |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 105 | cls.create_pg_interfaces( |
| 106 | range(cls.nr_of_vrfs * cls.pg_ifs_per_vrf)) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 107 | |
| 108 | # Packet flows mapping pg0 -> pg1, pg2 etc. |
| 109 | cls.flows = dict() |
| 110 | for i in range(len(cls.pg_interfaces)): |
| 111 | multiplicand = i / cls.pg_ifs_per_vrf |
| 112 | pg_list = [ |
| 113 | cls.pg_interfaces[multiplicand * cls.pg_ifs_per_vrf + j] |
| 114 | for j in range(cls.pg_ifs_per_vrf) |
| 115 | if (multiplicand * cls.pg_ifs_per_vrf + j) != i] |
| 116 | cls.flows[cls.pg_interfaces[i]] = pg_list |
| 117 | |
| 118 | # Packet sizes - jumbo packet (9018 bytes) skipped |
| 119 | cls.pg_if_packet_sizes = [64, 512, 1518] |
| 120 | |
| 121 | # Set up all interfaces |
| 122 | for pg_if in cls.pg_interfaces: |
| 123 | pg_if.admin_up() |
| 124 | pg_if.generate_remote_hosts(cls.hosts_per_pg) |
| 125 | |
| 126 | # Create list of VRFs |
| 127 | cls.vrf_list = list() |
| 128 | |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 129 | # Create list of reset VRFs |
| 130 | cls.vrf_reset_list = list() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 131 | |
| 132 | # Create list of pg_interfaces in VRFs |
| 133 | cls.pg_in_vrf = list() |
| 134 | |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 135 | # Create list of pg_interfaces not in VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 136 | cls.pg_not_in_vrf = [pg_if for pg_if in cls.pg_interfaces] |
| 137 | |
| 138 | # Create mapping of pg_interfaces to VRF IDs |
| 139 | cls.pg_if_by_vrf_id = dict() |
| 140 | for i in range(cls.nr_of_vrfs): |
| 141 | vrf_id = i + 1 |
| 142 | pg_list = [ |
| 143 | cls.pg_interfaces[i * cls.pg_ifs_per_vrf + j] |
| 144 | for j in range(cls.pg_ifs_per_vrf)] |
| 145 | cls.pg_if_by_vrf_id[vrf_id] = pg_list |
| 146 | |
| 147 | except Exception: |
| 148 | super(TestIp4VrfMultiInst, cls).tearDownClass() |
| 149 | raise |
| 150 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 151 | @classmethod |
| 152 | def tearDownClass(cls): |
| 153 | super(TestIp4VrfMultiInst, cls).tearDownClass() |
| 154 | |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 155 | def setUp(self): |
Jan Gelety | 3900fe5 | 2017-01-26 15:46:19 +0100 | [diff] [blame] | 156 | """ |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 157 | Clear trace and packet infos before running each test. |
| 158 | """ |
| 159 | super(TestIp4VrfMultiInst, self).setUp() |
| 160 | self.reset_packet_infos() |
| 161 | |
| 162 | def tearDown(self): |
| 163 | """ |
| 164 | Show various debug prints after each test. |
| 165 | """ |
| 166 | super(TestIp4VrfMultiInst, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame^] | 167 | |
| 168 | def show_commands_at_teardown(self): |
| 169 | self.logger.info(self.vapi.ppcli("show ip fib")) |
| 170 | self.logger.info(self.vapi.ppcli("show ip arp")) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 171 | |
| 172 | def create_vrf_and_assign_interfaces(self, count, start=1): |
Jan Gelety | 3900fe5 | 2017-01-26 15:46:19 +0100 | [diff] [blame] | 173 | """ |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 174 | Create required number of FIB tables / VRFs, put 3 pg-ip4 interfaces |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 175 | to every FIB table / VRF. |
| 176 | |
| 177 | :param int count: Number of FIB tables / VRFs to be created. |
| 178 | :param int start: Starting number of the FIB table / VRF ID. \ |
| 179 | (Default value = 1) |
| 180 | """ |
| 181 | |
| 182 | for i in range(count): |
| 183 | vrf_id = i + start |
| 184 | pg_if = self.pg_if_by_vrf_id[vrf_id][0] |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 185 | dest_addr = pg_if.local_ip4n |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 186 | dest_addr_len = 24 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 187 | self.vapi.ip_table_add_del(is_add=1, table_id=vrf_id) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 188 | self.logger.info("IPv4 VRF ID %d created" % vrf_id) |
| 189 | if vrf_id not in self.vrf_list: |
| 190 | self.vrf_list.append(vrf_id) |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 191 | if vrf_id in self.vrf_reset_list: |
| 192 | self.vrf_reset_list.remove(vrf_id) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 193 | for j in range(self.pg_ifs_per_vrf): |
| 194 | pg_if = self.pg_if_by_vrf_id[vrf_id][j] |
| 195 | pg_if.set_table_ip4(vrf_id) |
| 196 | self.logger.info("pg-interface %s added to IPv4 VRF ID %d" |
| 197 | % (pg_if.name, vrf_id)) |
| 198 | if pg_if not in self.pg_in_vrf: |
| 199 | self.pg_in_vrf.append(pg_if) |
| 200 | if pg_if in self.pg_not_in_vrf: |
| 201 | self.pg_not_in_vrf.remove(pg_if) |
| 202 | pg_if.config_ip4() |
Neale Ranns | baf2e90 | 2017-02-25 04:20:00 -0800 | [diff] [blame] | 203 | pg_if.configure_ipv4_neighbors() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 204 | self.logger.debug(self.vapi.ppcli("show ip fib")) |
| 205 | self.logger.debug(self.vapi.ppcli("show ip arp")) |
| 206 | |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 207 | def reset_vrf_and_remove_from_vrf_list(self, vrf_id): |
Jan Gelety | 3900fe5 | 2017-01-26 15:46:19 +0100 | [diff] [blame] | 208 | """ |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 209 | Reset required FIB table / VRF and remove it from VRF list. |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 210 | |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 211 | :param int vrf_id: The FIB table / VRF ID to be reset. |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 212 | """ |
| 213 | # self.vapi.reset_vrf(vrf_id, is_ipv6=0) |
| 214 | self.vapi.reset_fib(vrf_id, is_ipv6=0) |
| 215 | if vrf_id in self.vrf_list: |
| 216 | self.vrf_list.remove(vrf_id) |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 217 | if vrf_id not in self.vrf_reset_list: |
| 218 | self.vrf_reset_list.append(vrf_id) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 219 | for j in range(self.pg_ifs_per_vrf): |
| 220 | pg_if = self.pg_if_by_vrf_id[vrf_id][j] |
Neale Ranns | 4008ac9 | 2017-02-13 23:20:04 -0800 | [diff] [blame] | 221 | pg_if.unconfig_ip4() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 222 | if pg_if in self.pg_in_vrf: |
| 223 | self.pg_in_vrf.remove(pg_if) |
| 224 | if pg_if not in self.pg_not_in_vrf: |
| 225 | self.pg_not_in_vrf.append(pg_if) |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 226 | self.logger.info("IPv4 VRF ID %d reset finished" % vrf_id) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 227 | self.logger.debug(self.vapi.ppcli("show ip fib")) |
| 228 | self.logger.debug(self.vapi.ppcli("show ip arp")) |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 229 | self.vapi.ip_table_add_del(is_add=0, table_id=vrf_id) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 230 | |
| 231 | def create_stream(self, src_if, packet_sizes): |
| 232 | """ |
| 233 | Create input packet stream for defined interface using hosts list. |
| 234 | |
| 235 | :param object src_if: Interface to create packet stream for. |
| 236 | :param list packet_sizes: List of required packet sizes. |
| 237 | :return: Stream of packets. |
| 238 | """ |
| 239 | pkts = [] |
| 240 | src_hosts = src_if.remote_hosts |
| 241 | for dst_if in self.flows[src_if]: |
| 242 | for dst_host in dst_if.remote_hosts: |
| 243 | src_host = random.choice(src_hosts) |
| 244 | pkt_info = self.create_packet_info(src_if, dst_if) |
| 245 | payload = self.info_to_payload(pkt_info) |
| 246 | p = (Ether(dst=src_if.local_mac, src=src_host.mac) / |
| 247 | IP(src=src_host.ip4, dst=dst_host.ip4) / |
| 248 | UDP(sport=1234, dport=1234) / |
| 249 | Raw(payload)) |
| 250 | pkt_info.data = p.copy() |
| 251 | size = random.choice(packet_sizes) |
| 252 | self.extend_packet(p, size) |
| 253 | pkts.append(p) |
| 254 | self.logger.debug("Input stream created for port %s. Length: %u pkt(s)" |
| 255 | % (src_if.name, len(pkts))) |
| 256 | return pkts |
| 257 | |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 258 | def create_stream_crosswise_vrf(self, src_if, vrf_id, packet_sizes): |
| 259 | """ |
| 260 | Create input packet stream for negative test for leaking across |
| 261 | different VRFs for defined interface using hosts list. |
| 262 | |
| 263 | :param object src_if: Interface to create packet stream for. |
| 264 | :param int vrf_id: The FIB table / VRF ID where src_if is assigned. |
| 265 | :param list packet_sizes: List of required packet sizes. |
| 266 | :return: Stream of packets. |
| 267 | """ |
| 268 | pkts = [] |
| 269 | src_hosts = src_if.remote_hosts |
| 270 | vrf_lst = list(self.vrf_list) |
| 271 | vrf_lst.remove(vrf_id) |
| 272 | for vrf in vrf_lst: |
| 273 | for dst_if in self.pg_if_by_vrf_id[vrf]: |
| 274 | for dst_host in dst_if.remote_hosts: |
| 275 | src_host = random.choice(src_hosts) |
| 276 | pkt_info = self.create_packet_info(src_if, dst_if) |
| 277 | payload = self.info_to_payload(pkt_info) |
| 278 | p = (Ether(dst=src_if.local_mac, src=src_host.mac) / |
| 279 | IP(src=src_host.ip4, dst=dst_host.ip4) / |
| 280 | UDP(sport=1234, dport=1234) / |
| 281 | Raw(payload)) |
| 282 | pkt_info.data = p.copy() |
| 283 | size = random.choice(packet_sizes) |
| 284 | self.extend_packet(p, size) |
| 285 | pkts.append(p) |
| 286 | self.logger.debug("Input stream created for port %s. Length: %u pkt(s)" |
| 287 | % (src_if.name, len(pkts))) |
| 288 | return pkts |
| 289 | |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 290 | def verify_capture(self, pg_if, capture): |
| 291 | """ |
| 292 | Verify captured input packet stream for defined interface. |
| 293 | |
| 294 | :param object pg_if: Interface to verify captured packet stream for. |
| 295 | :param list capture: Captured packet stream. |
| 296 | """ |
| 297 | last_info = dict() |
| 298 | for i in self.pg_interfaces: |
| 299 | last_info[i.sw_if_index] = None |
| 300 | dst_sw_if_index = pg_if.sw_if_index |
| 301 | for packet in capture: |
| 302 | try: |
| 303 | ip = packet[IP] |
| 304 | udp = packet[UDP] |
Paul Vinciguerra | eaea421 | 2019-03-06 11:58:06 -0800 | [diff] [blame] | 305 | payload_info = self.payload_to_info(packet[Raw]) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 306 | packet_index = payload_info.index |
| 307 | self.assertEqual(payload_info.dst, dst_sw_if_index) |
| 308 | self.logger.debug("Got packet on port %s: src=%u (id=%u)" % |
| 309 | (pg_if.name, payload_info.src, packet_index)) |
| 310 | next_info = self.get_next_packet_info_for_interface2( |
| 311 | payload_info.src, dst_sw_if_index, |
| 312 | last_info[payload_info.src]) |
| 313 | last_info[payload_info.src] = next_info |
| 314 | self.assertIsNotNone(next_info) |
| 315 | self.assertEqual(packet_index, next_info.index) |
| 316 | saved_packet = next_info.data |
| 317 | # Check standard fields |
| 318 | self.assertEqual(ip.src, saved_packet[IP].src) |
| 319 | self.assertEqual(ip.dst, saved_packet[IP].dst) |
| 320 | self.assertEqual(udp.sport, saved_packet[UDP].sport) |
| 321 | self.assertEqual(udp.dport, saved_packet[UDP].dport) |
| 322 | except: |
| 323 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 324 | raise |
| 325 | for i in self.pg_interfaces: |
| 326 | remaining_packet = self.get_next_packet_info_for_interface2( |
| 327 | i, dst_sw_if_index, last_info[i.sw_if_index]) |
| 328 | self.assertIsNone( |
| 329 | remaining_packet, |
| 330 | "Port %u: Packet expected from source %u didn't arrive" % |
| 331 | (dst_sw_if_index, i.sw_if_index)) |
| 332 | |
| 333 | def verify_vrf(self, vrf_id): |
| 334 | """ |
| 335 | Check if the FIB table / VRF ID is configured. |
| 336 | |
| 337 | :param int vrf_id: The FIB table / VRF ID to be verified. |
| 338 | :return: 1 if the FIB table / VRF ID is configured, otherwise return 0. |
| 339 | """ |
| 340 | ip_fib_dump = self.vapi.ip_fib_dump() |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 341 | vrf_exist = False |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 342 | vrf_count = 0 |
| 343 | for ip_fib_details in ip_fib_dump: |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 344 | if ip_fib_details.table_id == vrf_id: |
| 345 | if not vrf_exist: |
| 346 | vrf_exist = True |
| 347 | addr = socket.inet_ntoa(ip_fib_details.address) |
| 348 | found = False |
| 349 | for pg_if in self.pg_if_by_vrf_id[vrf_id]: |
| 350 | if found: |
| 351 | break |
| 352 | for host in pg_if.remote_hosts: |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 353 | if scapy.compat.raw(addr) == \ |
| 354 | scapy.compat.raw(host.ip4): |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 355 | vrf_count += 1 |
| 356 | found = True |
| 357 | break |
| 358 | if not vrf_exist and vrf_count == 0: |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 359 | self.logger.info("IPv4 VRF ID %d is not configured" % vrf_id) |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 360 | return VRFState.not_configured |
| 361 | elif vrf_exist and vrf_count == 0: |
| 362 | self.logger.info("IPv4 VRF ID %d has been reset" % vrf_id) |
| 363 | return VRFState.reset |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 364 | else: |
| 365 | self.logger.info("IPv4 VRF ID %d is configured" % vrf_id) |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 366 | return VRFState.configured |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 367 | |
| 368 | def run_verify_test(self): |
| 369 | """ |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 370 | Create packet streams for all configured pg interfaces, send all \ |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 371 | prepared packet streams and verify that: |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 372 | - all packets received correctly on all pg-ip4 interfaces assigned |
| 373 | to VRFs |
| 374 | - no packet received on all pg-ip4 interfaces not assigned to VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 375 | |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 376 | :raise RuntimeError: If no packet captured on pg-ip4 interface assigned |
| 377 | to VRF or if any packet is captured on pg-ip4 interface not |
| 378 | assigned to VRF. |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 379 | """ |
| 380 | # Test |
| 381 | # Create incoming packet streams for packet-generator interfaces |
| 382 | for pg_if in self.pg_interfaces: |
| 383 | pkts = self.create_stream(pg_if, self.pg_if_packet_sizes) |
| 384 | pg_if.add_stream(pkts) |
| 385 | |
| 386 | # Enable packet capture and start packet sending |
| 387 | self.pg_enable_capture(self.pg_interfaces) |
| 388 | self.pg_start() |
| 389 | |
| 390 | # Verify |
| 391 | # Verify outgoing packet streams per packet-generator interface |
| 392 | for pg_if in self.pg_interfaces: |
| 393 | if pg_if in self.pg_in_vrf: |
| 394 | capture = pg_if.get_capture(remark="interface is in VRF") |
| 395 | self.verify_capture(pg_if, capture) |
| 396 | elif pg_if in self.pg_not_in_vrf: |
| 397 | pg_if.assert_nothing_captured(remark="interface is not in VRF", |
| 398 | filter_out_fn=is_ipv4_misc) |
| 399 | self.logger.debug("No capture for interface %s" % pg_if.name) |
| 400 | else: |
| 401 | raise Exception("Unknown interface: %s" % pg_if.name) |
| 402 | |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 403 | def run_crosswise_vrf_test(self): |
| 404 | """ |
| 405 | Create packet streams for every pg-ip4 interface in VRF towards all |
| 406 | pg-ip4 interfaces in other VRFs, send all prepared packet streams and \ |
| 407 | verify that: |
| 408 | - no packet received on all configured pg-ip4 interfaces |
| 409 | |
| 410 | :raise RuntimeError: If any packet is captured on any pg-ip4 interface. |
| 411 | """ |
| 412 | # Test |
| 413 | # Create incoming packet streams for packet-generator interfaces |
| 414 | for vrf_id in self.vrf_list: |
| 415 | for pg_if in self.pg_if_by_vrf_id[vrf_id]: |
| 416 | pkts = self.create_stream_crosswise_vrf( |
| 417 | pg_if, vrf_id, self.pg_if_packet_sizes) |
| 418 | pg_if.add_stream(pkts) |
| 419 | |
| 420 | # Enable packet capture and start packet sending |
| 421 | self.pg_enable_capture(self.pg_interfaces) |
| 422 | self.pg_start() |
| 423 | |
| 424 | # Verify |
| 425 | # Verify outgoing packet streams per packet-generator interface |
| 426 | for pg_if in self.pg_interfaces: |
| 427 | pg_if.assert_nothing_captured(remark="interface is in other VRF", |
| 428 | filter_out_fn=is_ipv4_misc) |
| 429 | self.logger.debug("No capture for interface %s" % pg_if.name) |
| 430 | |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 431 | def test_ip4_vrf_01(self): |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 432 | """ IP4 VRF Multi-instance test 1 - create 4 VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 433 | """ |
| 434 | # Config 1 |
| 435 | # Create 4 VRFs |
| 436 | self.create_vrf_and_assign_interfaces(4) |
| 437 | |
| 438 | # Verify 1 |
| 439 | for vrf_id in self.vrf_list: |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 440 | self.assert_equal(self.verify_vrf(vrf_id), |
| 441 | VRFState.configured, VRFState) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 442 | |
| 443 | # Test 1 |
| 444 | self.run_verify_test() |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 445 | self.run_crosswise_vrf_test() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 446 | |
| 447 | def test_ip4_vrf_02(self): |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 448 | """ IP4 VRF Multi-instance test 2 - reset 2 VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 449 | """ |
| 450 | # Config 2 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 451 | # Reset 2 VRFs |
| 452 | self.reset_vrf_and_remove_from_vrf_list(1) |
| 453 | self.reset_vrf_and_remove_from_vrf_list(2) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 454 | |
| 455 | # Verify 2 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 456 | for vrf_id in self.vrf_reset_list: |
| 457 | self.assert_equal(self.verify_vrf(vrf_id), |
| 458 | VRFState.reset, VRFState) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 459 | for vrf_id in self.vrf_list: |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 460 | self.assert_equal(self.verify_vrf(vrf_id), |
| 461 | VRFState.configured, VRFState) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 462 | |
| 463 | # Test 2 |
| 464 | self.run_verify_test() |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 465 | self.run_crosswise_vrf_test() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 466 | |
| 467 | def test_ip4_vrf_03(self): |
| 468 | """ IP4 VRF Multi-instance 3 - add 2 VRFs |
| 469 | """ |
| 470 | # Config 3 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 471 | # Add 1 of reset VRFs and 1 new VRF |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 472 | self.create_vrf_and_assign_interfaces(1) |
| 473 | self.create_vrf_and_assign_interfaces(1, start=5) |
| 474 | |
| 475 | # Verify 3 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 476 | for vrf_id in self.vrf_reset_list: |
| 477 | self.assert_equal(self.verify_vrf(vrf_id), |
| 478 | VRFState.reset, VRFState) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 479 | for vrf_id in self.vrf_list: |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 480 | self.assert_equal(self.verify_vrf(vrf_id), |
| 481 | VRFState.configured, VRFState) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 482 | |
| 483 | # Test 3 |
| 484 | self.run_verify_test() |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 485 | self.run_crosswise_vrf_test() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 486 | |
| 487 | def test_ip4_vrf_04(self): |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 488 | """ IP4 VRF Multi-instance test 4 - reset 4 VRFs |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 489 | """ |
| 490 | # Config 4 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 491 | # Reset all VRFs (i.e. no VRF except VRF=0 configured) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 492 | for i in range(len(self.vrf_list)): |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 493 | self.reset_vrf_and_remove_from_vrf_list(self.vrf_list[0]) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 494 | |
| 495 | # Verify 4 |
Jan Gelety | 95c87b5 | 2017-02-27 10:46:14 +0100 | [diff] [blame] | 496 | for vrf_id in self.vrf_reset_list: |
| 497 | self.assert_equal(self.verify_vrf(vrf_id), |
| 498 | VRFState.reset, VRFState) |
| 499 | vrf_list_length = len(self.vrf_list) |
| 500 | self.assertEqual( |
| 501 | vrf_list_length, 0, |
| 502 | "List of configured VRFs is not empty: %s != 0" % vrf_list_length) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 503 | |
| 504 | # Test 4 |
| 505 | self.run_verify_test() |
Jan Gelety | d16ba62 | 2018-06-21 16:57:47 +0200 | [diff] [blame] | 506 | self.run_crosswise_vrf_test() |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 507 | |
| 508 | |
| 509 | if __name__ == '__main__': |
| 510 | unittest.main(testRunner=VppTestRunner) |