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