Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 2 | |
| 3 | import unittest |
| 4 | import random |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 5 | |
| 6 | from scapy.packet import Raw |
| 7 | from scapy.layers.l2 import Ether |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 8 | from scapy.layers.inet import IP, UDP |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 9 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 10 | from framework import VppTestCase |
| 11 | from asfframework import VppTestRunner |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 12 | from util import Host, ppp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 13 | |
| 14 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 15 | class TestL2xc(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 16 | """L2XC Test Case""" |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 17 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 18 | @classmethod |
| 19 | def setUpClass(cls): |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 20 | """ |
| 21 | Perform standard class setup (defined by class method setUpClass in |
| 22 | class VppTestCase) before running the test case, set test case related |
| 23 | variables and configure VPP. |
| 24 | |
| 25 | :var int hosts_nr: Number of hosts to be created. |
| 26 | :var int dl_pkts_per_burst: Number of packets in burst for dual-loop |
| 27 | test. |
| 28 | :var int sl_pkts_per_burst: Number of packets in burst for single-loop |
| 29 | test. |
| 30 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 31 | super(TestL2xc, cls).setUpClass() |
| 32 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 33 | # Test variables |
| 34 | cls.hosts_nr = 10 |
| 35 | cls.dl_pkts_per_burst = 257 |
| 36 | cls.sl_pkts_per_burst = 2 |
| 37 | |
| 38 | try: |
| 39 | # create 4 pg interfaces |
| 40 | cls.create_pg_interfaces(range(4)) |
| 41 | |
| 42 | # packet flows mapping pg0 -> pg1, pg2 -> pg3, etc. |
| 43 | cls.flows = dict() |
| 44 | cls.flows[cls.pg0] = [cls.pg1] |
| 45 | cls.flows[cls.pg1] = [cls.pg0] |
| 46 | cls.flows[cls.pg2] = [cls.pg3] |
| 47 | cls.flows[cls.pg3] = [cls.pg2] |
| 48 | |
| 49 | # packet sizes |
| 50 | cls.pg_if_packet_sizes = [64, 512, 1518, 9018] |
| 51 | |
| 52 | cls.interfaces = list(cls.pg_interfaces) |
| 53 | |
| 54 | # Create bi-directional cross-connects between pg0 and pg1 |
| 55 | cls.vapi.sw_interface_set_l2_xconnect( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 56 | cls.pg0.sw_if_index, cls.pg1.sw_if_index, enable=1 |
| 57 | ) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 58 | cls.vapi.sw_interface_set_l2_xconnect( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | cls.pg1.sw_if_index, cls.pg0.sw_if_index, enable=1 |
| 60 | ) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 61 | |
| 62 | # Create bi-directional cross-connects between pg2 and pg3 |
| 63 | cls.vapi.sw_interface_set_l2_xconnect( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 64 | cls.pg2.sw_if_index, cls.pg3.sw_if_index, enable=1 |
| 65 | ) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 66 | cls.vapi.sw_interface_set_l2_xconnect( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | cls.pg3.sw_if_index, cls.pg2.sw_if_index, enable=1 |
| 68 | ) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 69 | |
| 70 | # mapping between packet-generator index and lists of test hosts |
| 71 | cls.hosts_by_pg_idx = dict() |
| 72 | |
| 73 | # Create host MAC and IPv4 lists |
| 74 | cls.create_host_lists(cls.hosts_nr) |
| 75 | |
| 76 | # setup all interfaces |
| 77 | for i in cls.interfaces: |
| 78 | i.admin_up() |
| 79 | |
| 80 | except Exception: |
| 81 | super(TestL2xc, cls).tearDownClass() |
| 82 | raise |
| 83 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 84 | @classmethod |
| 85 | def tearDownClass(cls): |
| 86 | super(TestL2xc, cls).tearDownClass() |
| 87 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 88 | def setUp(self): |
| 89 | super(TestL2xc, self).setUp() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 90 | self.reset_packet_infos() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 91 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 92 | def tearDown(self): |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 93 | """ |
| 94 | Show various debug prints after each test. |
| 95 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 96 | super(TestL2xc, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 97 | |
| 98 | def show_commands_at_teardown(self): |
| 99 | self.logger.info(self.vapi.ppcli("show l2patch")) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 100 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 101 | @classmethod |
| 102 | def create_host_lists(cls, count): |
| 103 | """ |
| 104 | Method to create required number of MAC and IPv4 addresses. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 105 | Create required number of host MAC addresses and distribute them among |
| 106 | interfaces. Create host IPv4 address for every host MAC address too. |
| 107 | |
| 108 | :param count: Number of hosts to create MAC and IPv4 addresses for. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 109 | """ |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 110 | for pg_if in cls.pg_interfaces: |
| 111 | cls.hosts_by_pg_idx[pg_if.sw_if_index] = [] |
| 112 | hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 113 | for j in range(0, count): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 114 | host = Host( |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 115 | "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j), |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | "172.17.1%02x.%u" % (pg_if.sw_if_index, j), |
| 117 | ) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 118 | hosts.append(host) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 119 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 120 | def create_stream(self, src_if, packet_sizes, packets_per_burst): |
| 121 | """ |
| 122 | Create input packet stream for defined interface. |
| 123 | |
| 124 | :param object src_if: Interface to create packet stream for. |
| 125 | :param list packet_sizes: List of required packet sizes. |
| 126 | :param int packets_per_burst: Number of packets in burst. |
| 127 | :return: Stream of packets. |
| 128 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 129 | pkts = [] |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 130 | for i in range(0, packets_per_burst): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 131 | dst_if = self.flows[src_if][0] |
| 132 | dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index]) |
| 133 | src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index]) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 134 | pkt_info = self.create_packet_info(src_if, dst_if) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 135 | payload = self.info_to_payload(pkt_info) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 136 | p = ( |
| 137 | Ether(dst=dst_host.mac, src=src_host.mac) |
| 138 | / IP(src=src_host.ip4, dst=dst_host.ip4) |
| 139 | / UDP(sport=1234, dport=1234) |
| 140 | / Raw(payload) |
| 141 | ) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 142 | pkt_info.data = p.copy() |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 143 | size = random.choice(packet_sizes) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 144 | self.extend_packet(p, size) |
| 145 | pkts.append(p) |
| 146 | return pkts |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 147 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 148 | def verify_capture(self, pg_if, capture): |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 149 | """ |
| 150 | Verify captured input packet stream for defined interface. |
| 151 | |
| 152 | :param object pg_if: Interface to verify captured packet stream for. |
| 153 | :param list capture: Captured packet stream. |
| 154 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 155 | last_info = dict() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 156 | for i in self.interfaces: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 157 | last_info[i.sw_if_index] = None |
| 158 | dst_sw_if_index = pg_if.sw_if_index |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 159 | for packet in capture: |
| 160 | try: |
| 161 | ip = packet[IP] |
| 162 | udp = packet[UDP] |
Paul Vinciguerra | eaea421 | 2019-03-06 11:58:06 -0800 | [diff] [blame] | 163 | payload_info = self.payload_to_info(packet[Raw]) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 164 | packet_index = payload_info.index |
| 165 | self.assertEqual(payload_info.dst, dst_sw_if_index) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 166 | self.logger.debug( |
| 167 | "Got packet on port %s: src=%u (id=%u)" |
| 168 | % (pg_if.name, payload_info.src, packet_index) |
| 169 | ) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 170 | next_info = self.get_next_packet_info_for_interface2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 171 | payload_info.src, dst_sw_if_index, last_info[payload_info.src] |
| 172 | ) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 173 | last_info[payload_info.src] = next_info |
| 174 | self.assertTrue(next_info is not None) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 175 | self.assertEqual(packet_index, next_info.index) |
| 176 | saved_packet = next_info.data |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 177 | # Check standard fields |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 178 | self.assertEqual(ip.src, saved_packet[IP].src) |
| 179 | self.assertEqual(ip.dst, saved_packet[IP].dst) |
| 180 | self.assertEqual(udp.sport, saved_packet[UDP].sport) |
| 181 | self.assertEqual(udp.dport, saved_packet[UDP].dport) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 182 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 183 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 184 | raise |
| 185 | for i in self.interfaces: |
| 186 | remaining_packet = self.get_next_packet_info_for_interface2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 187 | i, dst_sw_if_index, last_info[i.sw_if_index] |
| 188 | ) |
| 189 | self.assertTrue( |
| 190 | remaining_packet is None, |
| 191 | "Port %u: Packet expected from source %u didn't" |
| 192 | " arrive" % (dst_sw_if_index, i.sw_if_index), |
| 193 | ) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 194 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 195 | def run_l2xc_test(self, pkts_per_burst): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 196 | """L2XC test""" |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 197 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 198 | # Create incoming packet streams for packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 199 | for i in self.interfaces: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 200 | pkts = self.create_stream(i, self.pg_if_packet_sizes, pkts_per_burst) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 201 | i.add_stream(pkts) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 202 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 203 | # Enable packet capturing and start packet sending |
| 204 | self.pg_enable_capture(self.pg_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 205 | self.pg_start() |
| 206 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 207 | # Verify outgoing packet streams per packet-generator interface |
| 208 | for i in self.pg_interfaces: |
| 209 | capture = i.get_capture() |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 210 | self.logger.info("Verifying capture on interface %s" % i.name) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 211 | self.verify_capture(i, capture) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 212 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 213 | def test_l2xc_sl(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 214 | """L2XC single-loop test |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 215 | |
| 216 | Test scenario: |
| 217 | 1. config |
| 218 | 2 pairs of 2 interfaces, l2xconnected |
| 219 | |
| 220 | 2. sending l2 eth packets between 4 interfaces |
| 221 | 64B, 512B, 1518B, 9018B (ether_size) |
| 222 | burst of 2 packets per interface |
| 223 | """ |
| 224 | |
| 225 | self.run_l2xc_test(self.sl_pkts_per_burst) |
| 226 | |
| 227 | def test_l2xc_dl(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 228 | """L2XC dual-loop test |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 229 | |
| 230 | Test scenario: |
| 231 | 1. config |
| 232 | 2 pairs of 2 interfaces, l2xconnected |
| 233 | |
| 234 | 2. sending l2 eth packets between 4 interfaces |
| 235 | 64B, 512B, 1518B, 9018B (ether_size) |
| 236 | burst of 257 packets per interface |
| 237 | """ |
| 238 | |
| 239 | self.run_l2xc_test(self.dl_pkts_per_burst) |
| 240 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 241 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 242 | if __name__ == "__main__": |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 243 | unittest.main(testRunner=VppTestRunner) |