Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 2 | |
| 3 | import socket |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 4 | from util import ip4_range |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 5 | import unittest |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 6 | from config import config |
| 7 | from framework import VppTestCase, VppTestRunner |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 8 | from template_bd import BridgeDomain |
| 9 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 10 | from scapy.layers.l2 import Ether |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 11 | from scapy.packet import Raw, bind_layers |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 12 | from scapy.layers.inet import IP, UDP |
| 13 | from scapy.layers.vxlan import VXLAN |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 14 | |
| 15 | import util |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 16 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 17 | from vpp_vxlan_gpe_tunnel import VppVxlanGpeTunnel |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 18 | from vpp_ip import INVALID_INDEX |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 19 | |
| 20 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 21 | @unittest.skipUnless(config.extended, "part of extended tests") |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 22 | class TestVxlanGpe(BridgeDomain, VppTestCase): |
| 23 | """ VXLAN-GPE Test Case """ |
| 24 | |
| 25 | def __init__(self, *args): |
| 26 | BridgeDomain.__init__(self) |
| 27 | VppTestCase.__init__(self, *args) |
| 28 | |
| 29 | def encapsulate(self, pkt, vni): |
| 30 | """ |
| 31 | Encapsulate the original payload frame by adding VXLAN-GPE header |
| 32 | with its UDP, IP and Ethernet fields |
| 33 | """ |
| 34 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 35 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 36 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 37 | VXLAN(vni=vni, flags=self.flags) / |
| 38 | pkt) |
| 39 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 40 | def ip_range(self, start, end): |
| 41 | """ range of remote ip's """ |
| 42 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 43 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 44 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 45 | """ |
| 46 | Encapsulate the original payload frame by adding VXLAN-GPE header |
| 47 | with its UDP, IP and Ethernet fields |
| 48 | """ |
| 49 | return (Ether(src=src_mac, dst=self.mcast_mac) / |
| 50 | IP(src=src_ip, dst=self.mcast_ip4) / |
| 51 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 52 | VXLAN(vni=vni, flags=self.flags) / |
| 53 | pkt) |
| 54 | |
| 55 | def decapsulate(self, pkt): |
| 56 | """ |
| 57 | Decapsulate the original payload frame by removing VXLAN-GPE header |
| 58 | """ |
| 59 | # check if is set I and P flag |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 60 | self.assertEqual(pkt[VXLAN].flags, 0x0c) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 61 | return pkt[VXLAN].payload |
| 62 | |
| 63 | # Method for checking VXLAN-GPE encapsulation. |
| 64 | # |
| 65 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 66 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 67 | # by VPP using ARP. |
| 68 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 69 | if not local_only: |
| 70 | if not mcast_pkt: |
| 71 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 72 | else: |
| 73 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 74 | # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP. |
| 75 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 76 | if not local_only: |
| 77 | if not mcast_pkt: |
| 78 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 79 | else: |
| 80 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 81 | # Verify UDP destination port is VXLAN-GPE 4790, source UDP port |
| 82 | # could be arbitrary. |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 83 | self.assertEqual(pkt[UDP].dport, self.dport) |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 84 | # Verify UDP checksum |
| 85 | self.assert_udp_checksum_valid(pkt) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 86 | # Verify VNI |
Gabriel Ganne | 3904a0c | 2017-11-15 10:55:22 +0100 | [diff] [blame] | 87 | self.assertEqual(pkt[VXLAN].vni, vni) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 88 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 89 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 90 | def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 91 | # Create 10 ucast vxlan tunnels under bd |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 92 | ip_range_start = 10 |
| 93 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 94 | next_hop_address = cls.pg0.remote_ip4 |
| 95 | for dest_ip4 in ip4_range(next_hop_address, ip_range_start, |
| 96 | ip_range_end): |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 97 | # add host route so dest_ip4n will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 98 | rip = VppIpRoute(cls, dest_ip4, 32, |
| 99 | [VppRoutePath(next_hop_address, |
| 100 | INVALID_INDEX)], |
| 101 | register=False) |
| 102 | rip.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 103 | |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 104 | r = VppVxlanGpeTunnel(cls, |
| 105 | src_addr=cls.pg0.local_ip4, |
| 106 | dst_addr=dest_ip4, |
| 107 | src_port=port, |
| 108 | dst_port=port, |
| 109 | vni=vni) |
| 110 | r.add_vpp_config() |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 111 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 112 | bd_id=vni) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 113 | |
| 114 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 115 | def add_del_shared_mcast_dst_load(cls, port, is_add): |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 116 | """ |
| 117 | add or del tunnels sharing the same mcast dst |
| 118 | to test vxlan_gpe ref_count mechanism |
| 119 | """ |
| 120 | n_shared_dst_tunnels = 20 |
| 121 | vni_start = 1000 |
| 122 | vni_end = vni_start + n_shared_dst_tunnels |
| 123 | for vni in range(vni_start, vni_end): |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 124 | r = VppVxlanGpeTunnel(cls, |
| 125 | src_addr=cls.pg0.local_ip4, |
| 126 | dst_addr=cls.mcast_ip4, |
| 127 | src_port=port, |
| 128 | dst_port=port, |
| 129 | mcast_sw_if_index=1, |
| 130 | vni=vni) |
| 131 | if is_add: |
| 132 | r.add_vpp_config() |
| 133 | if r.sw_if_index == 0xffffffff: |
| 134 | raise ValueError("bad sw_if_index: ~0") |
| 135 | else: |
| 136 | r.remove_vpp_config() |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 137 | |
| 138 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 139 | def add_shared_mcast_dst_load(cls, port): |
| 140 | cls.add_del_shared_mcast_dst_load(port=port, is_add=1) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 141 | |
| 142 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 143 | def del_shared_mcast_dst_load(cls, port): |
| 144 | cls.add_del_shared_mcast_dst_load(port=port, is_add=0) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 145 | |
| 146 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 147 | def add_del_mcast_tunnels_load(cls, port, is_add): |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 148 | """ |
| 149 | add or del tunnels to test vxlan_gpe stability |
| 150 | """ |
| 151 | n_distinct_dst_tunnels = 20 |
| 152 | ip_range_start = 10 |
| 153 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 154 | for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, |
| 155 | ip_range_end): |
| 156 | vni = int(dest_ip4.split(".")[3]) |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 157 | r = VppVxlanGpeTunnel(cls, |
| 158 | src_addr=cls.pg0.local_ip4, |
| 159 | dst_addr=dest_ip4, |
| 160 | src_port=port, |
| 161 | dst_port=port, |
| 162 | mcast_sw_if_index=1, |
| 163 | vni=vni) |
| 164 | if is_add: |
| 165 | r.add_vpp_config() |
| 166 | else: |
| 167 | r.remove_vpp_config() |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 168 | |
| 169 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 170 | def add_mcast_tunnels_load(cls, port): |
| 171 | cls.add_del_mcast_tunnels_load(port=port, is_add=1) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 172 | |
| 173 | @classmethod |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 174 | def del_mcast_tunnels_load(cls, port): |
| 175 | cls.add_del_mcast_tunnels_load(port=port, is_add=0) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 176 | |
| 177 | # Class method to start the VXLAN-GPE test case. |
| 178 | # Overrides setUpClass method in VppTestCase class. |
| 179 | # Python try..except statement is used to ensure that the tear down of |
| 180 | # the class will be executed even if exception is raised. |
| 181 | # @param cls The class pointer. |
| 182 | @classmethod |
| 183 | def setUpClass(cls): |
| 184 | super(TestVxlanGpe, cls).setUpClass() |
| 185 | |
| 186 | try: |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 187 | cls.flags = 0x0c |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 188 | |
| 189 | # Create 2 pg interfaces. |
| 190 | cls.create_pg_interfaces(range(4)) |
| 191 | for pg in cls.pg_interfaces: |
| 192 | pg.admin_up() |
| 193 | |
| 194 | # Configure IPv4 addresses on VPP pg0. |
| 195 | cls.pg0.config_ip4() |
| 196 | |
| 197 | # Resolve MAC address for VPP's IP address on pg0. |
| 198 | cls.pg0.resolve_arp() |
| 199 | |
| 200 | # Our Multicast address |
| 201 | cls.mcast_ip4 = '239.1.1.1' |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 202 | cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 203 | except Exception: |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 204 | cls.tearDownClass() |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 205 | raise |
| 206 | |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 207 | @classmethod |
| 208 | def tearDownClass(cls): |
| 209 | super(TestVxlanGpe, cls).tearDownClass() |
| 210 | |
Artem Glazychev | ea96292 | 2021-05-28 19:09:14 +0700 | [diff] [blame] | 211 | def setUp(self): |
| 212 | super(TestVxlanGpe, self).setUp() |
| 213 | |
| 214 | def createVxLANInterfaces(self, port=4790): |
| 215 | # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0 |
| 216 | # and pg1 into BD. |
| 217 | self.dport = port |
| 218 | |
| 219 | self.single_tunnel_vni = 0xabcde |
| 220 | self.single_tunnel_bd = 11 |
| 221 | r = VppVxlanGpeTunnel(self, |
| 222 | src_addr=self.pg0.local_ip4, |
| 223 | dst_addr=self.pg0.remote_ip4, |
| 224 | src_port=port, |
| 225 | dst_port=port, |
| 226 | vni=self.single_tunnel_vni) |
| 227 | r.add_vpp_config() |
| 228 | self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 229 | bd_id=self.single_tunnel_bd) |
| 230 | self.vapi.sw_interface_set_l2_bridge( |
| 231 | rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd) |
| 232 | |
| 233 | # Setup vni 2 to test multicast flooding |
| 234 | self.n_ucast_tunnels = 10 |
| 235 | self.mcast_flood_bd = 12 |
| 236 | self.create_vxlan_gpe_flood_test_bd(self.mcast_flood_bd, |
| 237 | self.n_ucast_tunnels, |
| 238 | self.dport) |
| 239 | r = VppVxlanGpeTunnel(self, |
| 240 | src_addr=self.pg0.local_ip4, |
| 241 | dst_addr=self.mcast_ip4, |
| 242 | src_port=port, |
| 243 | dst_port=port, |
| 244 | mcast_sw_if_index=1, |
| 245 | vni=self.mcast_flood_bd) |
| 246 | r.add_vpp_config() |
| 247 | self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 248 | bd_id=self.mcast_flood_bd) |
| 249 | self.vapi.sw_interface_set_l2_bridge( |
| 250 | rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd) |
| 251 | |
| 252 | # Add and delete mcast tunnels to check stability |
| 253 | self.add_shared_mcast_dst_load(self.dport) |
| 254 | self.add_mcast_tunnels_load(self.dport) |
| 255 | self.del_shared_mcast_dst_load(self.dport) |
| 256 | self.del_mcast_tunnels_load(self.dport) |
| 257 | |
| 258 | # Setup vni 3 to test unicast flooding |
| 259 | self.ucast_flood_bd = 13 |
| 260 | self.create_vxlan_gpe_flood_test_bd(self.ucast_flood_bd, |
| 261 | self.n_ucast_tunnels, |
| 262 | self.dport) |
| 263 | self.vapi.sw_interface_set_l2_bridge( |
| 264 | rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd) |
| 265 | |
| 266 | # Set scapy listen custom port for VxLAN |
| 267 | bind_layers(UDP, VXLAN, dport=self.dport) |
| 268 | |
| 269 | """ |
| 270 | Tests with default port (4790) |
| 271 | """ |
| 272 | def test_decap(self): |
| 273 | """ Decapsulation test |
| 274 | from BridgeDoman |
| 275 | """ |
| 276 | self.createVxLANInterfaces() |
| 277 | super(TestVxlanGpe, self).test_decap() |
| 278 | |
| 279 | def test_encap(self): |
| 280 | """ Encapsulation test |
| 281 | from BridgeDoman |
| 282 | """ |
| 283 | self.createVxLANInterfaces() |
| 284 | super(TestVxlanGpe, self).test_encap() |
| 285 | |
| 286 | def test_ucast_flood(self): |
| 287 | """ Unicast flood test |
| 288 | from BridgeDoman |
| 289 | """ |
| 290 | self.createVxLANInterfaces() |
| 291 | super(TestVxlanGpe, self).test_ucast_flood() |
| 292 | |
| 293 | """ |
| 294 | Tests with custom port (1112) |
| 295 | """ |
| 296 | def test_decap_custom_port(self): |
| 297 | """ Decapsulation test custom port |
| 298 | from BridgeDoman |
| 299 | """ |
| 300 | self.createVxLANInterfaces(1112) |
| 301 | super(TestVxlanGpe, self).test_decap() |
| 302 | |
| 303 | def test_encap_custom_port(self): |
| 304 | """ Encapsulation test custom port |
| 305 | from BridgeDoman |
| 306 | """ |
| 307 | self.createVxLANInterfaces(1112) |
| 308 | super(TestVxlanGpe, self).test_encap() |
| 309 | |
| 310 | def test_ucast_flood_custom_port(self): |
| 311 | """ Unicast flood test custom port |
| 312 | from BridgeDoman |
| 313 | """ |
| 314 | self.createVxLANInterfaces(1112) |
| 315 | super(TestVxlanGpe, self).test_ucast_flood() |
| 316 | |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 317 | @unittest.skip("test disabled for vxlan-gpe") |
| 318 | def test_mcast_flood(self): |
| 319 | """ inherited from BridgeDomain """ |
| 320 | pass |
| 321 | |
| 322 | @unittest.skip("test disabled for vxlan-gpe") |
| 323 | def test_mcast_rcv(self): |
| 324 | """ inherited from BridgeDomain """ |
| 325 | pass |
| 326 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 327 | # Method to define VPP actions before tear down of the test case. |
| 328 | # Overrides tearDown method in VppTestCase class. |
| 329 | # @param self The object pointer. |
| 330 | def tearDown(self): |
| 331 | super(TestVxlanGpe, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 332 | |
| 333 | def show_commands_at_teardown(self): |
| 334 | self.logger.info(self.vapi.cli("show bridge-domain 11 detail")) |
| 335 | self.logger.info(self.vapi.cli("show bridge-domain 12 detail")) |
| 336 | self.logger.info(self.vapi.cli("show bridge-domain 13 detail")) |
| 337 | self.logger.info(self.vapi.cli("show int")) |
| 338 | self.logger.info(self.vapi.cli("show vxlan-gpe")) |
| 339 | self.logger.info(self.vapi.cli("show trace")) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 340 | |
| 341 | |
| 342 | if __name__ == '__main__': |
| 343 | unittest.main(testRunner=VppTestRunner) |