4 from util import ip4_range
6 from config import config
7 from framework import VppTestCase, VppTestRunner
8 from template_bd import BridgeDomain
10 from scapy.layers.l2 import Ether
11 from scapy.packet import Raw, bind_layers
12 from scapy.layers.inet import IP, UDP
13 from scapy.layers.vxlan import VXLAN
16 from vpp_ip_route import VppIpRoute, VppRoutePath
17 from vpp_vxlan_gpe_tunnel import VppVxlanGpeTunnel
18 from vpp_ip import INVALID_INDEX
21 @unittest.skipUnless(config.extended, "part of extended tests")
22 class TestVxlanGpe(BridgeDomain, VppTestCase):
23 """ VXLAN-GPE Test Case """
25 def __init__(self, *args):
26 BridgeDomain.__init__(self)
27 VppTestCase.__init__(self, *args)
29 def encapsulate(self, pkt, vni):
31 Encapsulate the original payload frame by adding VXLAN-GPE header
32 with its UDP, IP and Ethernet fields
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) /
40 def ip_range(self, start, end):
41 """ range of remote ip's """
42 return ip4_range(self.pg0.remote_ip4, start, end)
44 def encap_mcast(self, pkt, src_ip, src_mac, vni):
46 Encapsulate the original payload frame by adding VXLAN-GPE header
47 with its UDP, IP and Ethernet fields
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) /
55 def decapsulate(self, pkt):
57 Decapsulate the original payload frame by removing VXLAN-GPE header
59 # check if is set I and P flag
60 self.assertEqual(pkt[VXLAN].flags, 0x0c)
61 return pkt[VXLAN].payload
63 # Method for checking VXLAN-GPE encapsulation.
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
68 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
71 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
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)
78 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
80 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
81 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
83 self.assertEqual(pkt[UDP].dport, self.dport)
85 self.assert_udp_checksum_valid(pkt)
87 self.assertEqual(pkt[VXLAN].vni, vni)
90 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
91 # Create 10 ucast vxlan tunnels under bd
93 ip_range_end = ip_range_start + n_ucast_tunnels
94 next_hop_address = cls.pg0.remote_ip4
95 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
97 # add host route so dest_ip4n will not be resolved
98 rip = VppIpRoute(cls, dest_ip4, 32,
99 [VppRoutePath(next_hop_address,
104 r = VppVxlanGpeTunnel(cls,
105 src_addr=cls.pg0.local_ip4,
111 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
115 def add_del_shared_mcast_dst_load(cls, port, is_add):
117 add or del tunnels sharing the same mcast dst
118 to test vxlan_gpe ref_count mechanism
120 n_shared_dst_tunnels = 20
122 vni_end = vni_start + n_shared_dst_tunnels
123 for vni in range(vni_start, vni_end):
124 r = VppVxlanGpeTunnel(cls,
125 src_addr=cls.pg0.local_ip4,
126 dst_addr=cls.mcast_ip4,
133 if r.sw_if_index == 0xffffffff:
134 raise ValueError("bad sw_if_index: ~0")
136 r.remove_vpp_config()
139 def add_shared_mcast_dst_load(cls, port):
140 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
143 def del_shared_mcast_dst_load(cls, port):
144 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
147 def add_del_mcast_tunnels_load(cls, port, is_add):
149 add or del tunnels to test vxlan_gpe stability
151 n_distinct_dst_tunnels = 20
153 ip_range_end = ip_range_start + n_distinct_dst_tunnels
154 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
156 vni = int(dest_ip4.split(".")[3])
157 r = VppVxlanGpeTunnel(cls,
158 src_addr=cls.pg0.local_ip4,
167 r.remove_vpp_config()
170 def add_mcast_tunnels_load(cls, port):
171 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
174 def del_mcast_tunnels_load(cls, port):
175 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
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.
184 super(TestVxlanGpe, cls).setUpClass()
189 # Create 2 pg interfaces.
190 cls.create_pg_interfaces(range(4))
191 for pg in cls.pg_interfaces:
194 # Configure IPv4 addresses on VPP pg0.
197 # Resolve MAC address for VPP's IP address on pg0.
198 cls.pg0.resolve_arp()
200 # Our Multicast address
201 cls.mcast_ip4 = '239.1.1.1'
202 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
208 def tearDownClass(cls):
209 super(TestVxlanGpe, cls).tearDownClass()
212 super(TestVxlanGpe, self).setUp()
214 def createVxLANInterfaces(self, port=4790):
215 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
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,
226 vni=self.single_tunnel_vni)
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)
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,
239 r = VppVxlanGpeTunnel(self,
240 src_addr=self.pg0.local_ip4,
241 dst_addr=self.mcast_ip4,
245 vni=self.mcast_flood_bd)
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)
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)
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,
263 self.vapi.sw_interface_set_l2_bridge(
264 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
266 # Set scapy listen custom port for VxLAN
267 bind_layers(UDP, VXLAN, dport=self.dport)
270 Tests with default port (4790)
272 def test_decap(self):
273 """ Decapsulation test
276 self.createVxLANInterfaces()
277 super(TestVxlanGpe, self).test_decap()
279 def test_encap(self):
280 """ Encapsulation test
283 self.createVxLANInterfaces()
284 super(TestVxlanGpe, self).test_encap()
286 def test_ucast_flood(self):
287 """ Unicast flood test
290 self.createVxLANInterfaces()
291 super(TestVxlanGpe, self).test_ucast_flood()
294 Tests with custom port (1112)
296 def test_decap_custom_port(self):
297 """ Decapsulation test custom port
300 self.createVxLANInterfaces(1112)
301 super(TestVxlanGpe, self).test_decap()
303 def test_encap_custom_port(self):
304 """ Encapsulation test custom port
307 self.createVxLANInterfaces(1112)
308 super(TestVxlanGpe, self).test_encap()
310 def test_ucast_flood_custom_port(self):
311 """ Unicast flood test custom port
314 self.createVxLANInterfaces(1112)
315 super(TestVxlanGpe, self).test_ucast_flood()
317 @unittest.skip("test disabled for vxlan-gpe")
318 def test_mcast_flood(self):
319 """ inherited from BridgeDomain """
322 @unittest.skip("test disabled for vxlan-gpe")
323 def test_mcast_rcv(self):
324 """ inherited from BridgeDomain """
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.
331 super(TestVxlanGpe, self).tearDown()
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"))
342 if __name__ == '__main__':
343 unittest.main(testRunner=VppTestRunner)