tests: make tests less make dependent
[vpp.git] / test / test_vxlan_gpe.py
1 #!/usr/bin/env python3
2
3 import socket
4 from util import ip4_range
5 import unittest
6 from config import config
7 from framework import VppTestCase, VppTestRunner
8 from template_bd import BridgeDomain
9
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
14
15 import util
16 from vpp_ip_route import VppIpRoute, VppRoutePath
17 from vpp_vxlan_gpe_tunnel import VppVxlanGpeTunnel
18 from vpp_ip import INVALID_INDEX
19
20
21 @unittest.skipUnless(config.extended, "part of extended tests")
22 class TestVxlanGpe(BridgeDomain, VppTestCase):
23     """ VXLAN-GPE Test Case """
24
25     def __init__(self, *args):
26         BridgeDomain.__init__(self)
27         VppTestCase.__init__(self, *args)
28
29     def encapsulate(self, pkt, vni):
30         """
31         Encapsulate the original payload frame by adding VXLAN-GPE header
32         with its UDP, IP and Ethernet fields
33         """
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) /
38                 pkt)
39
40     def ip_range(self, start, end):
41         """ range of remote ip's """
42         return ip4_range(self.pg0.remote_ip4, start, end)
43
44     def encap_mcast(self, pkt, src_ip, src_mac, vni):
45         """
46         Encapsulate the original payload frame by adding VXLAN-GPE header
47         with its UDP, IP and Ethernet fields
48         """
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) /
53                 pkt)
54
55     def decapsulate(self, pkt):
56         """
57         Decapsulate the original payload frame by removing VXLAN-GPE header
58         """
59         # check if is set I and P flag
60         self.assertEqual(pkt[VXLAN].flags, 0x0c)
61         return pkt[VXLAN].payload
62
63     # Method for checking VXLAN-GPE encapsulation.
64     #
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
67         #  by VPP using ARP.
68         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
69         if not local_only:
70             if not mcast_pkt:
71                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
72             else:
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)
76         if not local_only:
77             if not mcast_pkt:
78                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
79             else:
80                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
81         # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
82         #  could be arbitrary.
83         self.assertEqual(pkt[UDP].dport, self.dport)
84         # Verify UDP checksum
85         self.assert_udp_checksum_valid(pkt)
86         # Verify VNI
87         self.assertEqual(pkt[VXLAN].vni, vni)
88
89     @classmethod
90     def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
91         # Create 10 ucast vxlan tunnels under bd
92         ip_range_start = 10
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,
96                                   ip_range_end):
97             # add host route so dest_ip4n will not be resolved
98             rip = VppIpRoute(cls, dest_ip4, 32,
99                              [VppRoutePath(next_hop_address,
100                                            INVALID_INDEX)],
101                              register=False)
102             rip.add_vpp_config()
103
104             r = VppVxlanGpeTunnel(cls,
105                                   src_addr=cls.pg0.local_ip4,
106                                   dst_addr=dest_ip4,
107                                   src_port=port,
108                                   dst_port=port,
109                                   vni=vni)
110             r.add_vpp_config()
111             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
112                                                 bd_id=vni)
113
114     @classmethod
115     def add_del_shared_mcast_dst_load(cls, port, is_add):
116         """
117         add or del tunnels sharing the same mcast dst
118         to test vxlan_gpe ref_count mechanism
119         """
120         n_shared_dst_tunnels = 20
121         vni_start = 1000
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,
127                                   src_port=port,
128                                   dst_port=port,
129                                   mcast_sw_if_index=1,
130                                   vni=vni)
131             if is_add:
132                 r.add_vpp_config()
133                 if r.sw_if_index == 0xffffffff:
134                     raise ValueError("bad sw_if_index: ~0")
135             else:
136                 r.remove_vpp_config()
137
138     @classmethod
139     def add_shared_mcast_dst_load(cls, port):
140         cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
141
142     @classmethod
143     def del_shared_mcast_dst_load(cls, port):
144         cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
145
146     @classmethod
147     def add_del_mcast_tunnels_load(cls, port, is_add):
148         """
149         add or del tunnels to test vxlan_gpe stability
150         """
151         n_distinct_dst_tunnels = 20
152         ip_range_start = 10
153         ip_range_end = ip_range_start + n_distinct_dst_tunnels
154         for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
155                                   ip_range_end):
156             vni = int(dest_ip4.split(".")[3])
157             r = VppVxlanGpeTunnel(cls,
158                                   src_addr=cls.pg0.local_ip4,
159                                   dst_addr=dest_ip4,
160                                   src_port=port,
161                                   dst_port=port,
162                                   mcast_sw_if_index=1,
163                                   vni=vni)
164             if is_add:
165                 r.add_vpp_config()
166             else:
167                 r.remove_vpp_config()
168
169     @classmethod
170     def add_mcast_tunnels_load(cls, port):
171         cls.add_del_mcast_tunnels_load(port=port, is_add=1)
172
173     @classmethod
174     def del_mcast_tunnels_load(cls, port):
175         cls.add_del_mcast_tunnels_load(port=port, is_add=0)
176
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.
182     @classmethod
183     def setUpClass(cls):
184         super(TestVxlanGpe, cls).setUpClass()
185
186         try:
187             cls.flags = 0x0c
188
189             # Create 2 pg interfaces.
190             cls.create_pg_interfaces(range(4))
191             for pg in cls.pg_interfaces:
192                 pg.admin_up()
193
194             # Configure IPv4 addresses on VPP pg0.
195             cls.pg0.config_ip4()
196
197             # Resolve MAC address for VPP's IP address on pg0.
198             cls.pg0.resolve_arp()
199
200             # Our Multicast address
201             cls.mcast_ip4 = '239.1.1.1'
202             cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
203         except Exception:
204             cls.tearDownClass()
205             raise
206
207     @classmethod
208     def tearDownClass(cls):
209         super(TestVxlanGpe, cls).tearDownClass()
210
211     def setUp(self):
212         super(TestVxlanGpe, self).setUp()
213
214     def createVxLANInterfaces(self, port=4790):
215         # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
216         # and pg1 into BD.
217         self.dport = port
218
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,
224                               src_port=port,
225                               dst_port=port,
226                               vni=self.single_tunnel_vni)
227         r.add_vpp_config()
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)
232
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,
238                                             self.dport)
239         r = VppVxlanGpeTunnel(self,
240                               src_addr=self.pg0.local_ip4,
241                               dst_addr=self.mcast_ip4,
242                               src_port=port,
243                               dst_port=port,
244                               mcast_sw_if_index=1,
245                               vni=self.mcast_flood_bd)
246         r.add_vpp_config()
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)
251
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)
257
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,
262                                             self.dport)
263         self.vapi.sw_interface_set_l2_bridge(
264             rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
265
266         # Set scapy listen custom port for VxLAN
267         bind_layers(UDP, VXLAN, dport=self.dport)
268
269     """
270     Tests with default port (4790)
271     """
272     def test_decap(self):
273         """ Decapsulation test
274         from BridgeDoman
275         """
276         self.createVxLANInterfaces()
277         super(TestVxlanGpe, self).test_decap()
278
279     def test_encap(self):
280         """ Encapsulation test
281         from BridgeDoman
282         """
283         self.createVxLANInterfaces()
284         super(TestVxlanGpe, self).test_encap()
285
286     def test_ucast_flood(self):
287         """ Unicast flood test
288         from BridgeDoman
289         """
290         self.createVxLANInterfaces()
291         super(TestVxlanGpe, self).test_ucast_flood()
292
293     """
294     Tests with custom port (1112)
295     """
296     def test_decap_custom_port(self):
297         """ Decapsulation test custom port
298         from BridgeDoman
299         """
300         self.createVxLANInterfaces(1112)
301         super(TestVxlanGpe, self).test_decap()
302
303     def test_encap_custom_port(self):
304         """ Encapsulation test custom port
305         from BridgeDoman
306         """
307         self.createVxLANInterfaces(1112)
308         super(TestVxlanGpe, self).test_encap()
309
310     def test_ucast_flood_custom_port(self):
311         """ Unicast flood test custom port
312         from BridgeDoman
313         """
314         self.createVxLANInterfaces(1112)
315         super(TestVxlanGpe, self).test_ucast_flood()
316
317     @unittest.skip("test disabled for vxlan-gpe")
318     def test_mcast_flood(self):
319         """ inherited from BridgeDomain """
320         pass
321
322     @unittest.skip("test disabled for vxlan-gpe")
323     def test_mcast_rcv(self):
324         """ inherited from BridgeDomain """
325         pass
326
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.
330     def tearDown(self):
331         super(TestVxlanGpe, self).tearDown()
332
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"))
340
341
342 if __name__ == '__main__':
343     unittest.main(testRunner=VppTestRunner)