tests: changes for scapy 2.4.3 migration
[vpp.git] / test / test_vxlan_gpe.py
1 #!/usr/bin/env python3
2
3 import socket
4 from util import ip4n_range, ip4_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner, running_extended_tests
7 from template_bd import BridgeDomain
8
9 from scapy.layers.l2 import Ether
10 from scapy.packet import Raw
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.vxlan import VXLAN
13 from scapy.utils import atol
14 from vpp_ip_route import VppIpRoute, VppRoutePath
15 from vpp_ip import INVALID_INDEX
16
17
18 @unittest.skipUnless(running_extended_tests, "part of extended tests")
19 class TestVxlanGpe(BridgeDomain, VppTestCase):
20     """ VXLAN-GPE Test Case """
21
22     def __init__(self, *args):
23         BridgeDomain.__init__(self)
24         VppTestCase.__init__(self, *args)
25
26     def encapsulate(self, pkt, vni):
27         """
28         Encapsulate the original payload frame by adding VXLAN-GPE header
29         with its UDP, IP and Ethernet fields
30         """
31         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
32                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
33                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
34                 VXLAN(vni=vni, flags=self.flags) /
35                 pkt)
36
37     def ip_range(self, start, end):
38         """ range of remote ip's """
39         return ip4_range(self.pg0.remote_ip4, start, end)
40
41     def encap_mcast(self, pkt, src_ip, src_mac, vni):
42         """
43         Encapsulate the original payload frame by adding VXLAN-GPE header
44         with its UDP, IP and Ethernet fields
45         """
46         return (Ether(src=src_mac, dst=self.mcast_mac) /
47                 IP(src=src_ip, dst=self.mcast_ip4) /
48                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
49                 VXLAN(vni=vni, flags=self.flags) /
50                 pkt)
51
52     def decapsulate(self, pkt):
53         """
54         Decapsulate the original payload frame by removing VXLAN-GPE header
55         """
56         # check if is set I and P flag
57         self.assertEqual(pkt[VXLAN].flags, 0x0c)
58         return pkt[VXLAN].payload
59
60     # Method for checking VXLAN-GPE encapsulation.
61     #
62     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
63         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64         #  by VPP using ARP.
65         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66         if not local_only:
67             if not mcast_pkt:
68                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69             else:
70                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
71         # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
72         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
73         if not local_only:
74             if not mcast_pkt:
75                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
76             else:
77                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
78         # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
79         #  could be arbitrary.
80         self.assertEqual(pkt[UDP].dport, type(self).dport)
81         # Verify VNI
82         self.assertEqual(pkt[VXLAN].vni, vni)
83
84     @classmethod
85     def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
86         # Create 10 ucast vxlan tunnels under bd
87         ip_range_start = 10
88         ip_range_end = ip_range_start + n_ucast_tunnels
89         next_hop_address = cls.pg0.remote_ip4
90         for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
91                                   ip_range_end):
92             # add host route so dest_ip4n will not be resolved
93             rip = VppIpRoute(cls, dest_ip4, 32,
94                              [VppRoutePath(next_hop_address,
95                                            INVALID_INDEX)],
96                              register=False)
97             rip.add_vpp_config()
98             dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
99
100             r = cls.vapi.vxlan_gpe_add_del_tunnel(
101                 src_addr=cls.pg0.local_ip4n,
102                 dst_addr=dest_ip4n,
103                 vni=vni)
104             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
105                                                 bd_id=vni)
106
107     @classmethod
108     def add_del_shared_mcast_dst_load(cls, is_add):
109         """
110         add or del tunnels sharing the same mcast dst
111         to test vxlan_gpe ref_count mechanism
112         """
113         n_shared_dst_tunnels = 20
114         vni_start = 1000
115         vni_end = vni_start + n_shared_dst_tunnels
116         for vni in range(vni_start, vni_end):
117             r = cls.vapi.vxlan_gpe_add_del_tunnel(
118                 src_addr=cls.pg0.local_ip4n,
119                 dst_addr=cls.mcast_ip4n,
120                 mcast_sw_if_index=1,
121                 vni=vni,
122                 is_add=is_add)
123             if r.sw_if_index == 0xffffffff:
124                 raise ValueError("bad sw_if_index: ~0")
125
126     @classmethod
127     def add_shared_mcast_dst_load(cls):
128         cls.add_del_shared_mcast_dst_load(is_add=1)
129
130     @classmethod
131     def del_shared_mcast_dst_load(cls):
132         cls.add_del_shared_mcast_dst_load(is_add=0)
133
134     @classmethod
135     def add_del_mcast_tunnels_load(cls, is_add):
136         """
137         add or del tunnels to test vxlan_gpe stability
138         """
139         n_distinct_dst_tunnels = 20
140         ip_range_start = 10
141         ip_range_end = ip_range_start + n_distinct_dst_tunnels
142         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
143                                     ip_range_end):
144             vni = bytearray(dest_ip4n)[3]
145             cls.vapi.vxlan_gpe_add_del_tunnel(
146                 src_addr=cls.pg0.local_ip4n,
147                 dst_addr=dest_ip4n,
148                 mcast_sw_if_index=1,
149                 vni=vni,
150                 is_add=is_add)
151
152     @classmethod
153     def add_mcast_tunnels_load(cls):
154         cls.add_del_mcast_tunnels_load(is_add=1)
155
156     @classmethod
157     def del_mcast_tunnels_load(cls):
158         cls.add_del_mcast_tunnels_load(is_add=0)
159
160     # Class method to start the VXLAN-GPE test case.
161     #  Overrides setUpClass method in VppTestCase class.
162     #  Python try..except statement is used to ensure that the tear down of
163     #  the class will be executed even if exception is raised.
164     #  @param cls The class pointer.
165     @classmethod
166     def setUpClass(cls):
167         super(TestVxlanGpe, cls).setUpClass()
168
169         try:
170             cls.dport = 4790
171             cls.flags = 0x0c
172
173             # Create 2 pg interfaces.
174             cls.create_pg_interfaces(range(4))
175             for pg in cls.pg_interfaces:
176                 pg.admin_up()
177
178             # Configure IPv4 addresses on VPP pg0.
179             cls.pg0.config_ip4()
180
181             # Resolve MAC address for VPP's IP address on pg0.
182             cls.pg0.resolve_arp()
183
184             # Our Multicast address
185             cls.mcast_ip4 = '239.1.1.1'
186             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
187             iplong = atol(cls.mcast_ip4)
188             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
189                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
190
191             # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
192             # and pg1 into BD.
193             cls.single_tunnel_bd = 11
194             r = cls.vapi.vxlan_gpe_add_del_tunnel(
195                 src_addr=cls.pg0.local_ip4n,
196                 dst_addr=cls.pg0.remote_ip4n,
197                 vni=cls.single_tunnel_bd)
198             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
199                                                 bd_id=cls.single_tunnel_bd)
200             cls.vapi.sw_interface_set_l2_bridge(
201                 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
202
203             # Setup vni 2 to test multicast flooding
204             cls.n_ucast_tunnels = 10
205             cls.mcast_flood_bd = 12
206             cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
207                                                cls.n_ucast_tunnels)
208             r = cls.vapi.vxlan_gpe_add_del_tunnel(
209                 src_addr=cls.pg0.local_ip4n,
210                 dst_addr=cls.mcast_ip4n,
211                 mcast_sw_if_index=1,
212                 vni=cls.mcast_flood_bd)
213             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
214                                                 bd_id=cls.mcast_flood_bd)
215             cls.vapi.sw_interface_set_l2_bridge(
216                 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
217
218             # Add and delete mcast tunnels to check stability
219             cls.add_shared_mcast_dst_load()
220             cls.add_mcast_tunnels_load()
221             cls.del_shared_mcast_dst_load()
222             cls.del_mcast_tunnels_load()
223
224             # Setup vni 3 to test unicast flooding
225             cls.ucast_flood_bd = 13
226             cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
227                                                cls.n_ucast_tunnels)
228             cls.vapi.sw_interface_set_l2_bridge(
229                 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
230         except Exception:
231             super(TestVxlanGpe, cls).tearDownClass()
232             raise
233
234     @classmethod
235     def tearDownClass(cls):
236         super(TestVxlanGpe, cls).tearDownClass()
237
238     @unittest.skip("test disabled for vxlan-gpe")
239     def test_mcast_flood(self):
240         """ inherited from BridgeDomain """
241         pass
242
243     @unittest.skip("test disabled for vxlan-gpe")
244     def test_mcast_rcv(self):
245         """ inherited from BridgeDomain """
246         pass
247
248     # Method to define VPP actions before tear down of the test case.
249     #  Overrides tearDown method in VppTestCase class.
250     #  @param self The object pointer.
251     def tearDown(self):
252         super(TestVxlanGpe, self).tearDown()
253
254     def show_commands_at_teardown(self):
255         self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
256         self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
257         self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
258         self.logger.info(self.vapi.cli("show int"))
259         self.logger.info(self.vapi.cli("show vxlan-gpe"))
260         self.logger.info(self.vapi.cli("show trace"))
261
262
263 if __name__ == '__main__':
264     unittest.main(testRunner=VppTestRunner)