misc: binary api fuzz test fixes
[vpp.git] / test / test_vxlan.py
1 #!/usr/bin/env python3
2
3 import socket
4 from util import ip4_range, reassemble4
5 import unittest
6 from framework import VppTestCase, VppTestRunner
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
14 import util
15 from vpp_ip_route import VppIpRoute, VppRoutePath
16 from vpp_vxlan_tunnel import VppVxlanTunnel
17 from vpp_ip import INVALID_INDEX
18
19
20 class TestVxlan(BridgeDomain, VppTestCase):
21     """ VXLAN Test Case """
22
23     def __init__(self, *args):
24         BridgeDomain.__init__(self)
25         VppTestCase.__init__(self, *args)
26
27     def encapsulate(self, pkt, vni):
28         """
29         Encapsulate the original payload frame by adding VXLAN header with its
30         UDP, IP and Ethernet fields
31         """
32         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
33                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
34                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
35                 VXLAN(vni=vni, flags=self.flags) /
36                 pkt)
37
38     def ip_range(self, start, end):
39         """ range of remote ip's """
40         return ip4_range(self.pg0.remote_ip4, start, end)
41
42     def encap_mcast(self, pkt, src_ip, src_mac, vni):
43         """
44         Encapsulate the original payload frame by adding VXLAN header with its
45         UDP, IP and Ethernet fields
46         """
47         return (Ether(src=src_mac, dst=self.mcast_mac) /
48                 IP(src=src_ip, dst=self.mcast_ip4) /
49                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
50                 VXLAN(vni=vni, flags=self.flags) /
51                 pkt)
52
53     def decapsulate(self, pkt):
54         """
55         Decapsulate the original payload frame by removing VXLAN header
56         """
57         # check if is set I flag
58         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
59         return pkt[VXLAN].payload
60
61     # Method for checking VXLAN encapsulation.
62     #
63     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
64         # TODO: add error messages
65         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
66         #  by VPP using ARP.
67         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
68         if not local_only:
69             if not mcast_pkt:
70                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
71             else:
72                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
73         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
74         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
75         if not local_only:
76             if not mcast_pkt:
77                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
78             else:
79                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
80         # Verify UDP destination port is VXLAN 4789, source UDP port could be
81         #  arbitrary.
82         self.assertEqual(pkt[UDP].dport, type(self).dport)
83         # TODO: checksum check
84         # Verify VNI
85         self.assertEqual(pkt[VXLAN].vni, vni)
86
87     @classmethod
88     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
89         # Create 10 ucast vxlan tunnels under bd
90         ip_range_start = 10
91         ip_range_end = ip_range_start + n_ucast_tunnels
92         next_hop_address = cls.pg0.remote_ip4
93         for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
94                                   ip_range_end):
95             # add host route so dest_ip4 will not be resolved
96             rip = VppIpRoute(cls, dest_ip4, 32,
97                              [VppRoutePath(next_hop_address,
98                                            INVALID_INDEX)],
99                              register=False)
100             rip.add_vpp_config()
101
102             r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
103                                dst=dest_ip4, vni=vni)
104             r.add_vpp_config()
105             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, 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 ref_count mechanism
112         """
113         n_shared_dst_tunnels = 20
114         vni_start = 10000
115         vni_end = vni_start + n_shared_dst_tunnels
116         for vni in range(vni_start, vni_end):
117             r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
118                                dst=cls.mcast_ip4, mcast_sw_if_index=1, vni=vni)
119             if is_add:
120                 r.add_vpp_config()
121                 if r.sw_if_index == 0xffffffff:
122                     raise ValueError("bad sw_if_index: ~0")
123             else:
124                 r.remove_vpp_config()
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 stability
138         """
139         n_distinct_dst_tunnels = 200
140         ip_range_start = 10
141         ip_range_end = ip_range_start + n_distinct_dst_tunnels
142         for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
143                                   ip_range_end):
144             vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3]
145             r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
146                                dst=dest_ip4, mcast_sw_if_index=1, vni=vni)
147             if is_add:
148                 r.add_vpp_config()
149             else:
150                 r.remove_vpp_config()
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 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(TestVxlan, cls).setUpClass()
168
169         try:
170             cls.dport = 4789
171             cls.flags = 0x8
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_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
187         except Exception:
188             cls.tearDownClass()
189             raise
190
191     @classmethod
192     def tearDownClass(cls):
193         super(TestVxlan, cls).tearDownClass()
194
195     def setUp(self):
196         super(TestVxlan, self).setUp()
197         # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
198         #  into BD.
199         self.single_tunnel_vni = 0x12345
200         self.single_tunnel_bd = 1
201         r = VppVxlanTunnel(self, src=self.pg0.local_ip4,
202                            dst=self.pg0.remote_ip4,
203                            vni=self.single_tunnel_vni)
204         r.add_vpp_config()
205         self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
206                                              bd_id=self.single_tunnel_bd)
207         self.vapi.sw_interface_set_l2_bridge(
208             rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
209
210         # Setup vni 2 to test multicast flooding
211         self.n_ucast_tunnels = 10
212         self.mcast_flood_bd = 2
213         self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
214                                         self.n_ucast_tunnels)
215         r = VppVxlanTunnel(self, src=self.pg0.local_ip4, dst=self.mcast_ip4,
216                            mcast_sw_if_index=1, vni=self.mcast_flood_bd)
217         r.add_vpp_config()
218         self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
219                                              bd_id=self.mcast_flood_bd)
220         self.vapi.sw_interface_set_l2_bridge(
221             rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
222
223         # Add and delete mcast tunnels to check stability
224         self.add_shared_mcast_dst_load()
225         self.add_mcast_tunnels_load()
226         self.del_shared_mcast_dst_load()
227         self.del_mcast_tunnels_load()
228
229         # Setup vni 3 to test unicast flooding
230         self.ucast_flood_bd = 3
231         self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
232                                         self.n_ucast_tunnels)
233         self.vapi.sw_interface_set_l2_bridge(
234             rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
235
236     def test_decap(self):
237         """ Decapsulation test
238         from BridgeDoman
239         """
240         super(TestVxlan, self).test_decap()
241
242     def test_encap_big_packet(self):
243         """ Encapsulation test send big frame from pg1
244         Verify receipt of encapsulated frames on pg0
245         """
246
247         self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
248
249         frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
250                  IP(src='4.3.2.1', dst='1.2.3.4') /
251                  UDP(sport=20000, dport=10000) /
252                  Raw(b'\xa5' * 1450))
253
254         self.pg1.add_stream([frame])
255
256         self.pg0.enable_capture()
257
258         self.pg_start()
259
260         # Pick first received frame and check if it's correctly encapsulated.
261         out = self.pg0.get_capture(2)
262         ether = out[0]
263         pkt = reassemble4(out)
264         pkt = ether / pkt
265         self.check_encapsulation(pkt, self.single_tunnel_vni)
266
267         payload = self.decapsulate(pkt)
268         # TODO: Scapy bug?
269         # self.assert_eq_pkts(payload, frame)
270
271     # Method to define VPP actions before tear down of the test case.
272     #  Overrides tearDown method in VppTestCase class.
273     #  @param self The object pointer.
274     def tearDown(self):
275         super(TestVxlan, self).tearDown()
276
277     def show_commands_at_teardown(self):
278         self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
279         self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
280         self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
281         self.logger.info(self.vapi.cli("show vxlan tunnel"))
282
283
284 if __name__ == '__main__':
285     unittest.main(testRunner=VppTestRunner)