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