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