4 from util import ip4n_range
6 from framework import VppTestCase, VppTestRunner, running_extended_tests
7 from template_bd import BridgeDomain
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
15 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
16 class TestVxlanGpe(BridgeDomain, VppTestCase):
17 """ VXLAN-GPE Test Case """
19 def __init__(self, *args):
20 BridgeDomain.__init__(self)
21 VppTestCase.__init__(self, *args)
23 def encapsulate(self, pkt, vni):
25 Encapsulate the original payload frame by adding VXLAN-GPE header
26 with its UDP, IP and Ethernet fields
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) /
34 def encap_mcast(self, pkt, src_ip, src_mac, vni):
36 Encapsulate the original payload frame by adding VXLAN-GPE header
37 with its UDP, IP and Ethernet fields
39 return (Ether(src=src_mac, dst=self.mcast_mac) /
40 IP(src=src_ip, dst=self.mcast_ip4) /
41 UDP(sport=self.dport, dport=self.dport, chksum=0) /
42 VXLAN(vni=vni, flags=self.flags) /
45 def decapsulate(self, pkt):
47 Decapsulate the original payload frame by removing VXLAN-GPE header
49 # check if is set I and P flag
50 self.assertEqual(pkt[VXLAN].flags, 0x0c)
51 return pkt[VXLAN].payload
53 # Method for checking VXLAN-GPE encapsulation.
55 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
56 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
58 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
61 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
63 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
64 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
65 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
68 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
70 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
71 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
73 self.assertEqual(pkt[UDP].dport, type(self).dport)
75 self.assertEqual(pkt[VXLAN].vni, vni)
78 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
79 # Create 10 ucast vxlan_gpe tunnels under bd
81 ip_range_end = ip_range_start + n_ucast_tunnels
82 next_hop_address = cls.pg0.remote_ip4n
83 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
85 # add host route so dest_ip4n will not be resolved
86 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
87 r = cls.vapi.vxlan_gpe_add_del_tunnel(
88 src_addr=cls.pg0.local_ip4n,
91 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
94 def add_del_shared_mcast_dst_load(cls, is_add):
96 add or del tunnels sharing the same mcast dst
97 to test vxlan_gpe ref_count mechanism
99 n_shared_dst_tunnels = 20
101 vni_end = vni_start + n_shared_dst_tunnels
102 for vni in range(vni_start, vni_end):
103 r = cls.vapi.vxlan_gpe_add_del_tunnel(
104 src_addr=cls.pg0.local_ip4n,
105 dst_addr=cls.mcast_ip4n,
109 if r.sw_if_index == 0xffffffff:
110 raise "bad sw_if_index"
113 def add_shared_mcast_dst_load(cls):
114 cls.add_del_shared_mcast_dst_load(is_add=1)
117 def del_shared_mcast_dst_load(cls):
118 cls.add_del_shared_mcast_dst_load(is_add=0)
121 def add_del_mcast_tunnels_load(cls, is_add):
123 add or del tunnels to test vxlan_gpe stability
125 n_distinct_dst_tunnels = 20
127 ip_range_end = ip_range_start + n_distinct_dst_tunnels
128 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
130 vni = bytearray(dest_ip4n)[3]
131 cls.vapi.vxlan_gpe_add_del_tunnel(
132 src_addr=cls.pg0.local_ip4n,
139 def add_mcast_tunnels_load(cls):
140 cls.add_del_mcast_tunnels_load(is_add=1)
143 def del_mcast_tunnels_load(cls):
144 cls.add_del_mcast_tunnels_load(is_add=0)
146 # Class method to start the VXLAN-GPE test case.
147 # Overrides setUpClass method in VppTestCase class.
148 # Python try..except statement is used to ensure that the tear down of
149 # the class will be executed even if exception is raised.
150 # @param cls The class pointer.
153 super(TestVxlanGpe, cls).setUpClass()
159 # Create 2 pg interfaces.
160 cls.create_pg_interfaces(range(4))
161 for pg in cls.pg_interfaces:
164 # Configure IPv4 addresses on VPP pg0.
167 # Resolve MAC address for VPP's IP address on pg0.
168 cls.pg0.resolve_arp()
170 # Our Multicast address
171 cls.mcast_ip4 = '239.1.1.1'
172 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
173 iplong = atol(cls.mcast_ip4)
174 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
175 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
177 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
179 cls.single_tunnel_bd = 11
180 r = cls.vapi.vxlan_gpe_add_del_tunnel(
181 src_addr=cls.pg0.local_ip4n,
182 dst_addr=cls.pg0.remote_ip4n,
183 vni=cls.single_tunnel_bd)
184 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
185 bd_id=cls.single_tunnel_bd)
186 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
187 bd_id=cls.single_tunnel_bd)
189 # Setup vni 2 to test multicast flooding
190 cls.n_ucast_tunnels = 10
191 cls.mcast_flood_bd = 12
192 cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
194 r = cls.vapi.vxlan_gpe_add_del_tunnel(
195 src_addr=cls.pg0.local_ip4n,
196 dst_addr=cls.mcast_ip4n,
198 vni=cls.mcast_flood_bd)
199 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
200 bd_id=cls.mcast_flood_bd)
201 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
202 bd_id=cls.mcast_flood_bd)
204 # Add and delete mcast tunnels to check stability
205 cls.add_shared_mcast_dst_load()
206 cls.add_mcast_tunnels_load()
207 cls.del_shared_mcast_dst_load()
208 cls.del_mcast_tunnels_load()
210 # Setup vni 3 to test unicast flooding
211 cls.ucast_flood_bd = 13
212 cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
214 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
215 bd_id=cls.ucast_flood_bd)
217 super(TestVxlanGpe, cls).tearDownClass()
220 @unittest.skip("test disabled for vxlan-gpe")
221 def test_mcast_flood(self):
222 """ inherited from BridgeDomain """
225 @unittest.skip("test disabled for vxlan-gpe")
226 def test_mcast_rcv(self):
227 """ inherited from BridgeDomain """
230 # Method to define VPP actions before tear down of the test case.
231 # Overrides tearDown method in VppTestCase class.
232 # @param self The object pointer.
234 super(TestVxlanGpe, self).tearDown()
235 if not self.vpp_dead:
236 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
237 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
238 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
239 self.logger.info(self.vapi.cli("show int"))
240 self.logger.info(self.vapi.cli("show vxlan-gpe"))
241 self.logger.info(self.vapi.cli("show trace"))
244 if __name__ == '__main__':
245 unittest.main(testRunner=VppTestRunner)