VPP-1392: VXLAN fails with IP fragmentation
[vpp.git] / test / test_vxlan.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range, ip4_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8
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
13
14 import StringIO
15
16
17 def reassemble(listoffragments):
18     buffer = StringIO.StringIO()
19     first = listoffragments[0]
20     buffer.seek(20)
21     for pkt in listoffragments:
22         buffer.seek(pkt[IP].frag*8)
23         buffer.write(pkt[IP].payload)
24     first.len = len(buffer.getvalue()) + 20
25     first.flags = 0
26     del(first.chksum)
27     header = str(first[IP])[:20]
28     return first[IP].__class__(header + buffer.getvalue())
29
30
31 class TestVxlan(BridgeDomain, VppTestCase):
32     """ VXLAN Test Case """
33
34     def __init__(self, *args):
35         BridgeDomain.__init__(self)
36         VppTestCase.__init__(self, *args)
37
38     def encapsulate(self, pkt, vni):
39         """
40         Encapsulate the original payload frame by adding VXLAN header with its
41         UDP, IP and Ethernet fields
42         """
43         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
44                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
45                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
46                 VXLAN(vni=vni, flags=self.flags) /
47                 pkt)
48
49     def ip_range(self, start, end):
50         """ range of remote ip's """
51         return ip4_range(self.pg0.remote_ip4, start, end)
52
53     def encap_mcast(self, pkt, src_ip, src_mac, vni):
54         """
55         Encapsulate the original payload frame by adding VXLAN header with its
56         UDP, IP and Ethernet fields
57         """
58         return (Ether(src=src_mac, dst=self.mcast_mac) /
59                 IP(src=src_ip, dst=self.mcast_ip4) /
60                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
61                 VXLAN(vni=vni, flags=self.flags) /
62                 pkt)
63
64     def decapsulate(self, pkt):
65         """
66         Decapsulate the original payload frame by removing VXLAN header
67         """
68         # check if is set I flag
69         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
70         return pkt[VXLAN].payload
71
72     # Method for checking VXLAN encapsulation.
73     #
74     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
75         # TODO: add error messages
76         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
77         #  by VPP using ARP.
78         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
79         if not local_only:
80             if not mcast_pkt:
81                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
82             else:
83                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
84         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
85         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
86         if not local_only:
87             if not mcast_pkt:
88                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
89             else:
90                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
91         # Verify UDP destination port is VXLAN 4789, source UDP port could be
92         #  arbitrary.
93         self.assertEqual(pkt[UDP].dport, type(self).dport)
94         # TODO: checksum check
95         # Verify VNI
96         self.assertEqual(pkt[VXLAN].vni, vni)
97
98     @classmethod
99     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
100         # Create 10 ucast vxlan tunnels under bd
101         ip_range_start = 10
102         ip_range_end = ip_range_start + n_ucast_tunnels
103         next_hop_address = cls.pg0.remote_ip4n
104         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
105                                     ip_range_end):
106             # add host route so dest_ip4n will not be resolved
107             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
108             r = cls.vapi.vxlan_add_del_tunnel(
109                 src_addr=cls.pg0.local_ip4n,
110                 dst_addr=dest_ip4n,
111                 vni=vni)
112             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
113
114     @classmethod
115     def add_del_shared_mcast_dst_load(cls, is_add):
116         """
117         add or del tunnels sharing the same mcast dst
118         to test vxlan ref_count mechanism
119         """
120         n_shared_dst_tunnels = 20
121         vni_start = 10000
122         vni_end = vni_start + n_shared_dst_tunnels
123         for vni in range(vni_start, vni_end):
124             r = cls.vapi.vxlan_add_del_tunnel(
125                 src_addr=cls.pg0.local_ip4n,
126                 dst_addr=cls.mcast_ip4n,
127                 mcast_sw_if_index=1,
128                 vni=vni,
129                 is_add=is_add)
130             if r.sw_if_index == 0xffffffff:
131                 raise "bad sw_if_index"
132
133     @classmethod
134     def add_shared_mcast_dst_load(cls):
135         cls.add_del_shared_mcast_dst_load(is_add=1)
136
137     @classmethod
138     def del_shared_mcast_dst_load(cls):
139         cls.add_del_shared_mcast_dst_load(is_add=0)
140
141     @classmethod
142     def add_del_mcast_tunnels_load(cls, is_add):
143         """
144         add or del tunnels to test vxlan stability
145         """
146         n_distinct_dst_tunnels = 200
147         ip_range_start = 10
148         ip_range_end = ip_range_start + n_distinct_dst_tunnels
149         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
150                                     ip_range_end):
151             vni = bytearray(dest_ip4n)[3]
152             cls.vapi.vxlan_add_del_tunnel(
153                 src_addr=cls.pg0.local_ip4n,
154                 dst_addr=dest_ip4n,
155                 mcast_sw_if_index=1,
156                 vni=vni,
157                 is_add=is_add)
158
159     @classmethod
160     def add_mcast_tunnels_load(cls):
161         cls.add_del_mcast_tunnels_load(is_add=1)
162
163     @classmethod
164     def del_mcast_tunnels_load(cls):
165         cls.add_del_mcast_tunnels_load(is_add=0)
166
167     # Class method to start the VXLAN test case.
168     #  Overrides setUpClass method in VppTestCase class.
169     #  Python try..except statement is used to ensure that the tear down of
170     #  the class will be executed even if exception is raised.
171     #  @param cls The class pointer.
172     @classmethod
173     def setUpClass(cls):
174         super(TestVxlan, cls).setUpClass()
175
176         try:
177             cls.dport = 4789
178             cls.flags = 0x8
179
180             # Create 2 pg interfaces.
181             cls.create_pg_interfaces(range(4))
182             for pg in cls.pg_interfaces:
183                 pg.admin_up()
184
185             # Configure IPv4 addresses on VPP pg0.
186             cls.pg0.config_ip4()
187
188             # Resolve MAC address for VPP's IP address on pg0.
189             cls.pg0.resolve_arp()
190
191             # Our Multicast address
192             cls.mcast_ip4 = '239.1.1.1'
193             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
194             iplong = atol(cls.mcast_ip4)
195             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
196                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
197
198             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
199             #  into BD.
200             cls.single_tunnel_bd = 1
201             r = cls.vapi.vxlan_add_del_tunnel(
202                 src_addr=cls.pg0.local_ip4n,
203                 dst_addr=cls.pg0.remote_ip4n,
204                 vni=cls.single_tunnel_bd)
205             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
206                                                 bd_id=cls.single_tunnel_bd)
207             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
208                                                 bd_id=cls.single_tunnel_bd)
209
210             # Setup vni 2 to test multicast flooding
211             cls.n_ucast_tunnels = 10
212             cls.mcast_flood_bd = 2
213             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
214                                            cls.n_ucast_tunnels)
215             r = cls.vapi.vxlan_add_del_tunnel(
216                 src_addr=cls.pg0.local_ip4n,
217                 dst_addr=cls.mcast_ip4n,
218                 mcast_sw_if_index=1,
219                 vni=cls.mcast_flood_bd)
220             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
221                                                 bd_id=cls.mcast_flood_bd)
222             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
223                                                 bd_id=cls.mcast_flood_bd)
224
225             # Add and delete mcast tunnels to check stability
226             cls.add_shared_mcast_dst_load()
227             cls.add_mcast_tunnels_load()
228             cls.del_shared_mcast_dst_load()
229             cls.del_mcast_tunnels_load()
230
231             # Setup vni 3 to test unicast flooding
232             cls.ucast_flood_bd = 3
233             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
234                                            cls.n_ucast_tunnels)
235             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
236                                                 bd_id=cls.ucast_flood_bd)
237         except Exception:
238             super(TestVxlan, cls).tearDownClass()
239             raise
240
241     def test_encap_big_packet(self):
242         """ Encapsulation test send big frame from pg1
243         Verify receipt of encapsulated frames on pg0
244         """
245
246         self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
247
248         frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
249                  IP(src='4.3.2.1', dst='1.2.3.4') /
250                  UDP(sport=20000, dport=10000) /
251                  Raw('\xa5' * 1450))
252
253         self.pg1.add_stream([frame])
254
255         self.pg0.enable_capture()
256
257         self.pg_start()
258
259         # Pick first received frame and check if it's correctly encapsulated.
260         out = self.pg0.get_capture(2)
261         ether = out[0]
262         pkt = reassemble(out)
263         pkt = ether / pkt
264         self.check_encapsulation(pkt, self.single_tunnel_bd)
265
266         payload = self.decapsulate(pkt)
267         # TODO: Scapy bug?
268         # self.assert_eq_pkts(payload, frame)
269
270     # Method to define VPP actions before tear down of the test case.
271     #  Overrides tearDown method in VppTestCase class.
272     #  @param self The object pointer.
273     def tearDown(self):
274         super(TestVxlan, self).tearDown()
275         if not self.vpp_dead:
276             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
277             self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
278             self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
279             self.logger.info(self.vapi.cli("show vxlan tunnel"))
280
281
282 if __name__ == '__main__':
283     unittest.main(testRunner=VppTestRunner)