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