Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 2 | |
| 3 | import socket |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 4 | from util import ip4_range, reassemble4_ether |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 5 | import unittest |
| 6 | from framework import VppTestCase, VppTestRunner |
| 7 | from template_bd import BridgeDomain |
| 8 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 9 | from scapy.layers.l2 import Ether |
| 10 | from scapy.packet import Raw |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 11 | from scapy.layers.inet import IP, UDP |
| 12 | from scapy.layers.vxlan import VXLAN |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 13 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 14 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 15 | from vpp_ip import INVALID_INDEX |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 16 | |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 17 | |
| 18 | class TestVxlanGbp(VppTestCase): |
| 19 | """ VXLAN GBP Test Case """ |
| 20 | |
| 21 | @property |
| 22 | def frame_request(self): |
| 23 | """ Ethernet frame modeling a generic request """ |
| 24 | return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / |
| 25 | IP(src='1.2.3.4', dst='4.3.2.1') / |
| 26 | UDP(sport=10000, dport=20000) / |
Ole Troan | 770a0de | 2019-11-07 13:52:21 +0100 | [diff] [blame] | 27 | Raw(b'\xa5' * 100)) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 28 | |
| 29 | @property |
| 30 | def frame_reply(self): |
| 31 | """ Ethernet frame modeling a generic reply """ |
| 32 | return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 33 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 34 | UDP(sport=20000, dport=10000) / |
Ole Troan | 770a0de | 2019-11-07 13:52:21 +0100 | [diff] [blame] | 35 | Raw(b'\xa5' * 100)) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 36 | |
| 37 | def encapsulate(self, pkt, vni): |
| 38 | """ |
| 39 | Encapsulate the original payload frame by adding VXLAN GBP header with |
| 40 | its UDP, IP and Ethernet fields |
| 41 | """ |
| 42 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 43 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 44 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 45 | VXLAN(vni=vni, flags=self.flags, gpflags=self.gpflags, |
| 46 | gpid=self.sclass) / pkt) |
| 47 | |
| 48 | def ip_range(self, start, end): |
| 49 | """ range of remote ip's """ |
| 50 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 51 | |
| 52 | def decapsulate(self, pkt): |
| 53 | """ |
| 54 | Decapsulate the original payload frame by removing VXLAN header |
| 55 | """ |
| 56 | # check if is set G and I flag |
| 57 | self.assertEqual(pkt[VXLAN].flags, int('0x88', 16)) |
| 58 | return pkt[VXLAN].payload |
| 59 | |
| 60 | # Method for checking VXLAN GBP encapsulation. |
| 61 | # |
| 62 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 63 | # TODO: add error messages |
| 64 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 65 | # by VPP using ARP. |
| 66 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 67 | if not local_only: |
| 68 | if not mcast_pkt: |
| 69 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 70 | else: |
| 71 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 72 | # Verify VXLAN GBP tunnel source IP is VPP_IP and destination IP is |
| 73 | # MY_IP. |
| 74 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 75 | if not local_only: |
| 76 | if not mcast_pkt: |
| 77 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 78 | else: |
| 79 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 80 | # Verify UDP destination port is VXLAN GBP 48879, source UDP port could |
| 81 | # be arbitrary. |
| 82 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 83 | # Verify UDP checksum |
| 84 | self.assert_udp_checksum_valid(pkt) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 85 | # Verify VNI |
| 86 | # pkt.show() |
| 87 | self.assertEqual(pkt[VXLAN].vni, vni) |
| 88 | # Verify Source Class |
| 89 | self.assertEqual(pkt[VXLAN].gpid, 0) |
| 90 | |
| 91 | @classmethod |
| 92 | def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels): |
| 93 | # Create 2 ucast vxlan tunnels under bd |
| 94 | ip_range_start = 10 |
| 95 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 96 | next_hop_address = cls.pg0.remote_ip4 |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 97 | for dest_ip4 in ip4_range(cls.pg0.remote_ip4, |
| 98 | ip_range_start, |
| 99 | ip_range_end): |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 100 | # add host route so dest_ip4 will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 101 | rip = VppIpRoute(cls, dest_ip4, 32, |
| 102 | [VppRoutePath(next_hop_address, |
| 103 | INVALID_INDEX)], |
| 104 | register=False) |
| 105 | rip.add_vpp_config() |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 106 | r = cls.vapi.vxlan_gbp_tunnel_add_del( |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 107 | tunnel={ |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 108 | 'src': cls.pg0.local_ip4, |
| 109 | 'dst': dest_ip4, |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 110 | 'vni': vni, |
| 111 | 'instance': INVALID_INDEX, |
| 112 | 'mcast_sw_if_index': INVALID_INDEX, |
| 113 | 'mode': 1, |
| 114 | }, |
| 115 | is_add=1 |
| 116 | ) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 117 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 118 | bd_id=vni) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 119 | |
| 120 | # Class method to start the VXLAN GBP test case. |
| 121 | # Overrides setUpClass method in VppTestCase class. |
| 122 | # Python try..except statement is used to ensure that the tear down of |
| 123 | # the class will be executed even if exception is raised. |
| 124 | # @param cls The class pointer. |
| 125 | @classmethod |
| 126 | def setUpClass(cls): |
| 127 | super(TestVxlanGbp, cls).setUpClass() |
| 128 | |
| 129 | try: |
| 130 | cls.dport = 48879 |
| 131 | cls.flags = 0x88 |
| 132 | cls.gpflags = 0x0 |
| 133 | cls.sclass = 0 |
| 134 | |
| 135 | # Create 2 pg interfaces. |
| 136 | cls.create_pg_interfaces(range(4)) |
| 137 | for pg in cls.pg_interfaces: |
| 138 | pg.admin_up() |
| 139 | |
| 140 | # Configure IPv4 addresses on VPP pg0. |
| 141 | cls.pg0.config_ip4() |
| 142 | |
| 143 | # Resolve MAC address for VPP's IP address on pg0. |
| 144 | cls.pg0.resolve_arp() |
| 145 | |
| 146 | # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and |
| 147 | # pg1 into BD. |
| 148 | cls.single_tunnel_bd = 1 |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 149 | cls.single_tunnel_vni = 0xabcde |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 150 | r = cls.vapi.vxlan_gbp_tunnel_add_del( |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 151 | tunnel={ |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 152 | 'src': cls.pg0.local_ip4, |
| 153 | 'dst': cls.pg0.remote_ip4, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 154 | 'vni': cls.single_tunnel_vni, |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 155 | 'instance': INVALID_INDEX, |
| 156 | 'mcast_sw_if_index': INVALID_INDEX, |
| 157 | 'mode': 1, |
| 158 | }, |
| 159 | is_add=1 |
| 160 | ) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 161 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 162 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 163 | cls.vapi.sw_interface_set_l2_bridge( |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 164 | rx_sw_if_index=cls.pg1.sw_if_index, |
| 165 | bd_id=cls.single_tunnel_bd) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 166 | |
| 167 | # Setup vni 2 to test multicast flooding |
| 168 | cls.n_ucast_tunnels = 2 |
| 169 | # Setup vni 3 to test unicast flooding |
| 170 | cls.ucast_flood_bd = 3 |
| 171 | cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd, |
| 172 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 173 | cls.vapi.sw_interface_set_l2_bridge( |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 174 | rx_sw_if_index=cls.pg3.sw_if_index, |
| 175 | bd_id=cls.ucast_flood_bd) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 176 | except Exception: |
| 177 | super(TestVxlanGbp, cls).tearDownClass() |
| 178 | raise |
| 179 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 180 | @classmethod |
| 181 | def tearDownClass(cls): |
| 182 | super(TestVxlanGbp, cls).tearDownClass() |
| 183 | |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 184 | def assert_eq_pkts(self, pkt1, pkt2): |
| 185 | """ Verify the Ether, IP, UDP, payload are equal in both |
| 186 | packets |
| 187 | """ |
| 188 | self.assertEqual(pkt1[Ether].src, pkt2[Ether].src) |
| 189 | self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst) |
| 190 | self.assertEqual(pkt1[IP].src, pkt2[IP].src) |
| 191 | self.assertEqual(pkt1[IP].dst, pkt2[IP].dst) |
| 192 | self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport) |
| 193 | self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport) |
| 194 | self.assertEqual(pkt1[Raw], pkt2[Raw]) |
| 195 | |
| 196 | def test_decap(self): |
| 197 | """ Decapsulation test |
| 198 | Send encapsulated frames from pg0 |
| 199 | Verify receipt of decapsulated frames on pg1 |
| 200 | """ |
| 201 | encapsulated_pkt = self.encapsulate(self.frame_request, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 202 | self.single_tunnel_vni) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 203 | |
| 204 | self.pg0.add_stream([encapsulated_pkt, ]) |
| 205 | |
| 206 | self.pg1.enable_capture() |
| 207 | |
| 208 | self.pg_start() |
| 209 | |
| 210 | # Pick first received frame and check if it's the non-encapsulated |
| 211 | # frame |
| 212 | out = self.pg1.get_capture(1) |
| 213 | pkt = out[0] |
| 214 | self.assert_eq_pkts(pkt, self.frame_request) |
| 215 | |
| 216 | def test_encap(self): |
| 217 | """ Encapsulation test |
| 218 | Send frames from pg1 |
| 219 | Verify receipt of encapsulated frames on pg0 |
| 220 | """ |
| 221 | self.pg1.add_stream([self.frame_reply]) |
| 222 | |
| 223 | self.pg0.enable_capture() |
| 224 | |
| 225 | self.pg_start() |
| 226 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 227 | # Pick first received frame and check if it's correctly encapsulated. |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 228 | out = self.pg0.get_capture(1) |
| 229 | pkt = out[0] |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 230 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 231 | |
| 232 | payload = self.decapsulate(pkt) |
| 233 | self.assert_eq_pkts(payload, self.frame_reply) |
| 234 | |
| 235 | def test_ucast_flood(self): |
| 236 | """ Unicast flood test |
| 237 | Send frames from pg3 |
| 238 | Verify receipt of encapsulated frames on pg0 |
| 239 | """ |
| 240 | self.pg3.add_stream([self.frame_reply]) |
| 241 | |
| 242 | self.pg0.enable_capture() |
| 243 | |
| 244 | self.pg_start() |
| 245 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 246 | # Get packet from each tunnel and assert it's correctly encapsulated. |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 247 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
| 248 | for pkt in out: |
| 249 | self.check_encapsulation(pkt, self.ucast_flood_bd, True) |
| 250 | payload = self.decapsulate(pkt) |
| 251 | self.assert_eq_pkts(payload, self.frame_reply) |
| 252 | |
| 253 | def test_encap_big_packet(self): |
| 254 | """ Encapsulation test send big frame from pg1 |
| 255 | Verify receipt of encapsulated frames on pg0 |
| 256 | """ |
| 257 | |
| 258 | self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0]) |
| 259 | |
| 260 | frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 261 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 262 | UDP(sport=20000, dport=10000) / |
Ole Troan | 770a0de | 2019-11-07 13:52:21 +0100 | [diff] [blame] | 263 | Raw(b'\xa5' * 1450)) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 264 | |
| 265 | self.pg1.add_stream([frame]) |
| 266 | |
| 267 | self.pg0.enable_capture() |
| 268 | |
| 269 | self.pg_start() |
| 270 | |
| 271 | # Pick first received frame and check if it's correctly encapsulated. |
| 272 | out = self.pg0.get_capture(2) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 273 | pkt = reassemble4_ether(out) |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 274 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 275 | |
| 276 | payload = self.decapsulate(pkt) |
| 277 | self.assert_eq_pkts(payload, frame) |
| 278 | |
| 279 | # Method to define VPP actions before tear down of the test case. |
| 280 | # Overrides tearDown method in VppTestCase class. |
| 281 | # @param self The object pointer. |
| 282 | def tearDown(self): |
| 283 | super(TestVxlanGbp, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 284 | |
| 285 | def show_commands_at_teardown(self): |
| 286 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 287 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 288 | self.logger.info(self.vapi.cli("show vxlan-gbp tunnel")) |
| 289 | self.logger.info(self.vapi.cli("show error")) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 290 | |
| 291 | |
| 292 | if __name__ == '__main__': |
| 293 | unittest.main(testRunner=VppTestRunner) |