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 |
| 6 | from framework import VppTestCase, VppTestRunner, running_extended_tests |
| 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 |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [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 | |
| 14 | import util |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 15 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 16 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 17 | from vpp_ip import INVALID_INDEX |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 18 | |
| 19 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 20 | @unittest.skipUnless(running_extended_tests, "part of extended tests") |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 21 | class TestVxlanGpe(BridgeDomain, VppTestCase): |
| 22 | """ VXLAN-GPE Test Case """ |
| 23 | |
| 24 | def __init__(self, *args): |
| 25 | BridgeDomain.__init__(self) |
| 26 | VppTestCase.__init__(self, *args) |
| 27 | |
| 28 | def encapsulate(self, pkt, vni): |
| 29 | """ |
| 30 | Encapsulate the original payload frame by adding VXLAN-GPE header |
| 31 | with its UDP, IP and Ethernet fields |
| 32 | """ |
| 33 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 34 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 35 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 36 | VXLAN(vni=vni, flags=self.flags) / |
| 37 | pkt) |
| 38 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 39 | def ip_range(self, start, end): |
| 40 | """ range of remote ip's """ |
| 41 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 42 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 43 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 44 | """ |
| 45 | Encapsulate the original payload frame by adding VXLAN-GPE header |
| 46 | with its UDP, IP and Ethernet fields |
| 47 | """ |
| 48 | return (Ether(src=src_mac, dst=self.mcast_mac) / |
| 49 | IP(src=src_ip, dst=self.mcast_ip4) / |
| 50 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 51 | VXLAN(vni=vni, flags=self.flags) / |
| 52 | pkt) |
| 53 | |
| 54 | def decapsulate(self, pkt): |
| 55 | """ |
| 56 | Decapsulate the original payload frame by removing VXLAN-GPE header |
| 57 | """ |
| 58 | # check if is set I and P flag |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 59 | self.assertEqual(pkt[VXLAN].flags, 0x0c) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 60 | return pkt[VXLAN].payload |
| 61 | |
| 62 | # Method for checking VXLAN-GPE encapsulation. |
| 63 | # |
| 64 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 65 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 66 | # by VPP using ARP. |
| 67 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 68 | if not local_only: |
| 69 | if not mcast_pkt: |
| 70 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 71 | else: |
| 72 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 73 | # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is 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-GPE 4790, source UDP port |
| 81 | # could be arbitrary. |
| 82 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
| 83 | # Verify VNI |
Gabriel Ganne | 3904a0c | 2017-11-15 10:55:22 +0100 | [diff] [blame] | 84 | self.assertEqual(pkt[VXLAN].vni, vni) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 85 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 86 | @classmethod |
| 87 | def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 88 | # Create 10 ucast vxlan tunnels under bd |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 89 | ip_range_start = 10 |
| 90 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 91 | next_hop_address = cls.pg0.remote_ip4 |
| 92 | for dest_ip4 in ip4_range(next_hop_address, ip_range_start, |
| 93 | ip_range_end): |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 94 | # add host route so dest_ip4n will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 95 | rip = VppIpRoute(cls, dest_ip4, 32, |
| 96 | [VppRoutePath(next_hop_address, |
| 97 | INVALID_INDEX)], |
| 98 | register=False) |
| 99 | rip.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 100 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 101 | r = cls.vapi.vxlan_gpe_add_del_tunnel( |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 102 | src_addr=cls.pg0.local_ip4, |
| 103 | dst_addr=dest_ip4, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 104 | vni=vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 105 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 106 | bd_id=vni) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 107 | |
| 108 | @classmethod |
| 109 | def add_del_shared_mcast_dst_load(cls, is_add): |
| 110 | """ |
| 111 | add or del tunnels sharing the same mcast dst |
| 112 | to test vxlan_gpe ref_count mechanism |
| 113 | """ |
| 114 | n_shared_dst_tunnels = 20 |
| 115 | vni_start = 1000 |
| 116 | vni_end = vni_start + n_shared_dst_tunnels |
| 117 | for vni in range(vni_start, vni_end): |
| 118 | r = cls.vapi.vxlan_gpe_add_del_tunnel( |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 119 | local=cls.pg0.local_ip4, |
| 120 | remote=cls.mcast_ip4, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 121 | mcast_sw_if_index=1, |
| 122 | vni=vni, |
| 123 | is_add=is_add) |
| 124 | if r.sw_if_index == 0xffffffff: |
Paul Vinciguerra | c599c6f | 2019-03-12 17:41:27 -0700 | [diff] [blame] | 125 | raise ValueError("bad sw_if_index: ~0") |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 126 | |
| 127 | @classmethod |
| 128 | def add_shared_mcast_dst_load(cls): |
| 129 | cls.add_del_shared_mcast_dst_load(is_add=1) |
| 130 | |
| 131 | @classmethod |
| 132 | def del_shared_mcast_dst_load(cls): |
| 133 | cls.add_del_shared_mcast_dst_load(is_add=0) |
| 134 | |
| 135 | @classmethod |
| 136 | def add_del_mcast_tunnels_load(cls, is_add): |
| 137 | """ |
| 138 | add or del tunnels to test vxlan_gpe stability |
| 139 | """ |
| 140 | n_distinct_dst_tunnels = 20 |
| 141 | ip_range_start = 10 |
| 142 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 143 | for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, |
| 144 | ip_range_end): |
| 145 | vni = int(dest_ip4.split(".")[3]) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 146 | cls.vapi.vxlan_gpe_add_del_tunnel( |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 147 | src_addr=cls.pg0.local_ip4, |
| 148 | dst_addr=dest_ip4, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 149 | mcast_sw_if_index=1, |
| 150 | vni=vni, |
| 151 | is_add=is_add) |
| 152 | |
| 153 | @classmethod |
| 154 | def add_mcast_tunnels_load(cls): |
| 155 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 156 | |
| 157 | @classmethod |
| 158 | def del_mcast_tunnels_load(cls): |
| 159 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 160 | |
| 161 | # Class method to start the VXLAN-GPE test case. |
| 162 | # Overrides setUpClass method in VppTestCase class. |
| 163 | # Python try..except statement is used to ensure that the tear down of |
| 164 | # the class will be executed even if exception is raised. |
| 165 | # @param cls The class pointer. |
| 166 | @classmethod |
| 167 | def setUpClass(cls): |
| 168 | super(TestVxlanGpe, cls).setUpClass() |
| 169 | |
| 170 | try: |
| 171 | cls.dport = 4790 |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 172 | cls.flags = 0x0c |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 173 | |
| 174 | # Create 2 pg interfaces. |
| 175 | cls.create_pg_interfaces(range(4)) |
| 176 | for pg in cls.pg_interfaces: |
| 177 | pg.admin_up() |
| 178 | |
| 179 | # Configure IPv4 addresses on VPP pg0. |
| 180 | cls.pg0.config_ip4() |
| 181 | |
| 182 | # Resolve MAC address for VPP's IP address on pg0. |
| 183 | cls.pg0.resolve_arp() |
| 184 | |
| 185 | # Our Multicast address |
| 186 | cls.mcast_ip4 = '239.1.1.1' |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 187 | cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 188 | |
| 189 | # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0 |
| 190 | # and pg1 into BD. |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 191 | cls.single_tunnel_vni = 0xabcde |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 192 | cls.single_tunnel_bd = 11 |
| 193 | r = cls.vapi.vxlan_gpe_add_del_tunnel( |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 194 | src_addr=cls.pg0.local_ip4, |
| 195 | dst_addr=cls.pg0.remote_ip4, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 196 | vni=cls.single_tunnel_vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 197 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 198 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 199 | cls.vapi.sw_interface_set_l2_bridge( |
| 200 | rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 201 | |
| 202 | # Setup vni 2 to test multicast flooding |
| 203 | cls.n_ucast_tunnels = 10 |
| 204 | cls.mcast_flood_bd = 12 |
| 205 | cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd, |
| 206 | cls.n_ucast_tunnels) |
| 207 | r = cls.vapi.vxlan_gpe_add_del_tunnel( |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame^] | 208 | src_addr=cls.pg0.local_ip4, |
| 209 | dst_addr=cls.mcast_ip4, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 210 | mcast_sw_if_index=1, |
| 211 | vni=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 212 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 213 | bd_id=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 214 | cls.vapi.sw_interface_set_l2_bridge( |
| 215 | rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 216 | |
| 217 | # Add and delete mcast tunnels to check stability |
| 218 | cls.add_shared_mcast_dst_load() |
| 219 | cls.add_mcast_tunnels_load() |
| 220 | cls.del_shared_mcast_dst_load() |
| 221 | cls.del_mcast_tunnels_load() |
| 222 | |
| 223 | # Setup vni 3 to test unicast flooding |
| 224 | cls.ucast_flood_bd = 13 |
| 225 | cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd, |
| 226 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 227 | cls.vapi.sw_interface_set_l2_bridge( |
| 228 | rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 229 | except Exception: |
| 230 | super(TestVxlanGpe, cls).tearDownClass() |
| 231 | raise |
| 232 | |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 233 | @classmethod |
| 234 | def tearDownClass(cls): |
| 235 | super(TestVxlanGpe, cls).tearDownClass() |
| 236 | |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 237 | @unittest.skip("test disabled for vxlan-gpe") |
| 238 | def test_mcast_flood(self): |
| 239 | """ inherited from BridgeDomain """ |
| 240 | pass |
| 241 | |
| 242 | @unittest.skip("test disabled for vxlan-gpe") |
| 243 | def test_mcast_rcv(self): |
| 244 | """ inherited from BridgeDomain """ |
| 245 | pass |
| 246 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 247 | # Method to define VPP actions before tear down of the test case. |
| 248 | # Overrides tearDown method in VppTestCase class. |
| 249 | # @param self The object pointer. |
| 250 | def tearDown(self): |
| 251 | super(TestVxlanGpe, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 252 | |
| 253 | def show_commands_at_teardown(self): |
| 254 | self.logger.info(self.vapi.cli("show bridge-domain 11 detail")) |
| 255 | self.logger.info(self.vapi.cli("show bridge-domain 12 detail")) |
| 256 | self.logger.info(self.vapi.cli("show bridge-domain 13 detail")) |
| 257 | self.logger.info(self.vapi.cli("show int")) |
| 258 | self.logger.info(self.vapi.cli("show vxlan-gpe")) |
| 259 | self.logger.info(self.vapi.cli("show trace")) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 260 | |
| 261 | |
| 262 | if __name__ == '__main__': |
| 263 | unittest.main(testRunner=VppTestRunner) |