3 from util import ip4_range
5 from config import config
6 from framework import VppTestCase
7 from asfframework import VppTestRunner
8 from template_bd import BridgeDomain
10 from scapy.layers.l2 import Ether
11 from scapy.packet import 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
35 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
36 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
37 / UDP(sport=self.dport, dport=self.dport, chksum=0)
38 / VXLAN(vni=vni, flags=self.flags)
42 def ip_range(self, start, end):
43 """range of remote ip's"""
44 return ip4_range(self.pg0.remote_ip4, start, end)
46 def encap_mcast(self, pkt, src_ip, src_mac, vni):
48 Encapsulate the original payload frame by adding VXLAN-GPE header
49 with its UDP, IP and Ethernet fields
52 Ether(src=src_mac, dst=self.mcast_mac)
53 / IP(src=src_ip, dst=self.mcast_ip4)
54 / UDP(sport=self.dport, dport=self.dport, chksum=0)
55 / VXLAN(vni=vni, flags=self.flags)
59 def decapsulate(self, pkt):
61 Decapsulate the original payload frame by removing VXLAN-GPE header
63 # check if is set I and P flag
64 self.assertEqual(pkt[VXLAN].flags, 0x0C)
65 return pkt[VXLAN].payload
67 # Method for checking VXLAN-GPE encapsulation.
69 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
70 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
72 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
75 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
77 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
78 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
79 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
82 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
84 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
85 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
87 self.assertEqual(pkt[UDP].dport, self.dport)
89 self.assert_udp_checksum_valid(pkt)
91 self.assertEqual(pkt[VXLAN].vni, vni)
94 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
95 # Create 10 ucast vxlan tunnels under bd
97 ip_range_end = ip_range_start + n_ucast_tunnels
98 next_hop_address = cls.pg0.remote_ip4
99 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
100 # add host route so dest_ip4n will not be resolved
105 [VppRoutePath(next_hop_address, INVALID_INDEX)],
110 r = VppVxlanGpeTunnel(
112 src_addr=cls.pg0.local_ip4,
119 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, bd_id=vni)
122 def add_del_shared_mcast_dst_load(cls, port, is_add):
124 add or del tunnels sharing the same mcast dst
125 to test vxlan_gpe ref_count mechanism
127 n_shared_dst_tunnels = 20
129 vni_end = vni_start + n_shared_dst_tunnels
130 for vni in range(vni_start, vni_end):
131 r = VppVxlanGpeTunnel(
133 src_addr=cls.pg0.local_ip4,
134 dst_addr=cls.mcast_ip4,
142 if r.sw_if_index == 0xFFFFFFFF:
143 raise ValueError("bad sw_if_index: ~0")
145 r.remove_vpp_config()
148 def add_shared_mcast_dst_load(cls, port):
149 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
152 def del_shared_mcast_dst_load(cls, port):
153 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
156 def add_del_mcast_tunnels_load(cls, port, is_add):
158 add or del tunnels to test vxlan_gpe stability
160 n_distinct_dst_tunnels = 20
162 ip_range_end = ip_range_start + n_distinct_dst_tunnels
163 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
164 vni = int(dest_ip4.split(".")[3])
165 r = VppVxlanGpeTunnel(
167 src_addr=cls.pg0.local_ip4,
177 r.remove_vpp_config()
180 def add_mcast_tunnels_load(cls, port):
181 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
184 def del_mcast_tunnels_load(cls, port):
185 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
187 # Class method to start the VXLAN-GPE test case.
188 # Overrides setUpClass method in VppTestCase class.
189 # Python try..except statement is used to ensure that the tear down of
190 # the class will be executed even if exception is raised.
191 # @param cls The class pointer.
194 super(TestVxlanGpe, cls).setUpClass()
199 # Create 2 pg interfaces.
200 cls.create_pg_interfaces(range(4))
201 for pg in cls.pg_interfaces:
204 # Configure IPv4 addresses on VPP pg0.
207 # Resolve MAC address for VPP's IP address on pg0.
208 cls.pg0.resolve_arp()
210 # Our Multicast address
211 cls.mcast_ip4 = "239.1.1.1"
212 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
218 def tearDownClass(cls):
219 super(TestVxlanGpe, cls).tearDownClass()
222 super(TestVxlanGpe, self).setUp()
224 def createVxLANInterfaces(self, port=4790):
225 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
229 self.single_tunnel_vni = 0xABCDE
230 self.single_tunnel_bd = 11
231 r = VppVxlanGpeTunnel(
233 src_addr=self.pg0.local_ip4,
234 dst_addr=self.pg0.remote_ip4,
237 vni=self.single_tunnel_vni,
240 self.vapi.sw_interface_set_l2_bridge(
241 rx_sw_if_index=r.sw_if_index, bd_id=self.single_tunnel_bd
243 self.vapi.sw_interface_set_l2_bridge(
244 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd
247 # Setup vni 2 to test multicast flooding
248 self.n_ucast_tunnels = 10
249 self.mcast_flood_bd = 12
250 self.create_vxlan_gpe_flood_test_bd(
251 self.mcast_flood_bd, self.n_ucast_tunnels, self.dport
253 r = VppVxlanGpeTunnel(
255 src_addr=self.pg0.local_ip4,
256 dst_addr=self.mcast_ip4,
260 vni=self.mcast_flood_bd,
263 self.vapi.sw_interface_set_l2_bridge(
264 rx_sw_if_index=r.sw_if_index, bd_id=self.mcast_flood_bd
266 self.vapi.sw_interface_set_l2_bridge(
267 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd
270 # Add and delete mcast tunnels to check stability
271 self.add_shared_mcast_dst_load(self.dport)
272 self.add_mcast_tunnels_load(self.dport)
273 self.del_shared_mcast_dst_load(self.dport)
274 self.del_mcast_tunnels_load(self.dport)
276 # Setup vni 3 to test unicast flooding
277 self.ucast_flood_bd = 13
278 self.create_vxlan_gpe_flood_test_bd(
279 self.ucast_flood_bd, self.n_ucast_tunnels, self.dport
281 self.vapi.sw_interface_set_l2_bridge(
282 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd
285 # Set scapy listen custom port for VxLAN
286 bind_layers(UDP, VXLAN, dport=self.dport)
289 Tests with default port (4790)
292 def test_decap(self):
293 """Decapsulation test
296 self.createVxLANInterfaces()
297 super(TestVxlanGpe, self).test_decap()
299 def test_encap(self):
300 """Encapsulation test
303 self.createVxLANInterfaces()
304 super(TestVxlanGpe, self).test_encap()
306 def test_ucast_flood(self):
307 """Unicast flood test
310 self.createVxLANInterfaces()
311 super(TestVxlanGpe, self).test_ucast_flood()
314 Tests with custom port (1112)
317 def test_decap_custom_port(self):
318 """Decapsulation test custom port
321 self.createVxLANInterfaces(1112)
322 super(TestVxlanGpe, self).test_decap()
324 def test_encap_custom_port(self):
325 """Encapsulation test custom port
328 self.createVxLANInterfaces(1112)
329 super(TestVxlanGpe, self).test_encap()
331 def test_ucast_flood_custom_port(self):
332 """Unicast flood test custom port
335 self.createVxLANInterfaces(1112)
336 super(TestVxlanGpe, self).test_ucast_flood()
338 @unittest.skip("test disabled for vxlan-gpe")
339 def test_mcast_flood(self):
340 """inherited from BridgeDomain"""
343 @unittest.skip("test disabled for vxlan-gpe")
344 def test_mcast_rcv(self):
345 """inherited from BridgeDomain"""
348 # Method to define VPP actions before tear down of the test case.
349 # Overrides tearDown method in VppTestCase class.
350 # @param self The object pointer.
352 super(TestVxlanGpe, self).tearDown()
354 def show_commands_at_teardown(self):
355 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
356 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
357 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
358 self.logger.info(self.vapi.cli("show int"))
359 self.logger.info(self.vapi.cli("show vxlan-gpe"))
360 self.logger.info(self.vapi.cli("show trace"))
363 if __name__ == "__main__":
364 unittest.main(testRunner=VppTestRunner)