http: fix client parse error handling
[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
7 from asfframework import VppTestRunner
8 from template_bd import BridgeDomain
9
10 from scapy.layers.l2 import Ether
11 from scapy.layers.l2 import ARP
12 from scapy.packet import Raw, bind_layers
13 from scapy.layers.inet import IP, UDP
14 from scapy.layers.inet6 import IPv6
15 from scapy.layers.vxlan import VXLAN
16 from scapy.contrib.mpls import MPLS
17
18 import util
19 from vpp_ip_route import VppIpRoute, VppRoutePath
20 from vpp_vxlan_tunnel import VppVxlanTunnel
21 from vpp_ip import INVALID_INDEX
22 from vpp_neighbor import VppNeighbor
23
24
25 class TestVxlan(BridgeDomain, VppTestCase):
26     """VXLAN Test Case"""
27
28     def __init__(self, *args):
29         BridgeDomain.__init__(self)
30         VppTestCase.__init__(self, *args)
31
32     def encapsulate(self, pkt, vni):
33         """
34         Encapsulate the original payload frame by adding VXLAN header with its
35         UDP, IP and Ethernet fields
36         """
37         return (
38             Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
39             / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
40             / UDP(sport=self.dport, dport=self.dport, chksum=0)
41             / VXLAN(vni=vni, flags=self.flags)
42             / pkt
43         )
44
45     def ip_range(self, start, end):
46         """range of remote ip's"""
47         return ip4_range(self.pg0.remote_ip4, start, end)
48
49     def encap_mcast(self, pkt, src_ip, src_mac, vni):
50         """
51         Encapsulate the original payload frame by adding VXLAN header with its
52         UDP, IP and Ethernet fields
53         """
54         return (
55             Ether(src=src_mac, dst=self.mcast_mac)
56             / IP(src=src_ip, dst=self.mcast_ip4)
57             / UDP(sport=self.dport, dport=self.dport, chksum=0)
58             / VXLAN(vni=vni, flags=self.flags)
59             / pkt
60         )
61
62     def decapsulate(self, pkt):
63         """
64         Decapsulate the original payload frame by removing VXLAN header
65         """
66         # check if is set I flag
67         self.assertEqual(pkt[VXLAN].flags, int("0x8", 16))
68         return pkt[VXLAN].payload
69
70     # Method for checking VXLAN encapsulation.
71     #
72     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
73         # TODO: add error messages
74         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
75         #  by VPP using ARP.
76         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
77         if not local_only:
78             if not mcast_pkt:
79                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
80             else:
81                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
82         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
83         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
84         if not local_only:
85             if not mcast_pkt:
86                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
87             else:
88                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
89         # Verify UDP destination port is VXLAN 4789, source UDP port could be
90         #  arbitrary.
91         self.assertEqual(pkt[UDP].dport, self.dport)
92         # Verify UDP checksum
93         self.assert_udp_checksum_valid(pkt)
94         # Verify VNI
95         self.assertEqual(pkt[VXLAN].vni, vni)
96
97     @classmethod
98     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels, port):
99         # Create 10 ucast vxlan tunnels under bd
100         ip_range_start = 10
101         ip_range_end = ip_range_start + n_ucast_tunnels
102         next_hop_address = cls.pg0.remote_ip4
103         for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
104             # add host route so dest_ip4 will not be resolved
105             rip = VppIpRoute(
106                 cls,
107                 dest_ip4,
108                 32,
109                 [VppRoutePath(next_hop_address, INVALID_INDEX)],
110                 register=False,
111             )
112             rip.add_vpp_config()
113
114             r = VppVxlanTunnel(
115                 cls,
116                 src=cls.pg0.local_ip4,
117                 src_port=port,
118                 dst_port=port,
119                 dst=dest_ip4,
120                 vni=vni,
121             )
122             r.add_vpp_config()
123             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
124
125     @classmethod
126     def add_del_shared_mcast_dst_load(cls, port, is_add):
127         """
128         add or del tunnels sharing the same mcast dst
129         to test vxlan ref_count mechanism
130         """
131         n_shared_dst_tunnels = 20
132         vni_start = 10000
133         vni_end = vni_start + n_shared_dst_tunnels
134         for vni in range(vni_start, vni_end):
135             r = VppVxlanTunnel(
136                 cls,
137                 src=cls.pg0.local_ip4,
138                 src_port=port,
139                 dst_port=port,
140                 dst=cls.mcast_ip4,
141                 mcast_sw_if_index=1,
142                 vni=vni,
143             )
144             if is_add:
145                 r.add_vpp_config()
146                 if r.sw_if_index == 0xFFFFFFFF:
147                     raise ValueError("bad sw_if_index: ~0")
148             else:
149                 r.remove_vpp_config()
150
151     @classmethod
152     def add_shared_mcast_dst_load(cls, port):
153         cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
154
155     @classmethod
156     def del_shared_mcast_dst_load(cls, port):
157         cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
158
159     @classmethod
160     def add_del_mcast_tunnels_load(cls, port, is_add):
161         """
162         add or del tunnels to test vxlan stability
163         """
164         n_distinct_dst_tunnels = 200
165         ip_range_start = 10
166         ip_range_end = ip_range_start + n_distinct_dst_tunnels
167         for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
168             vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3]
169             r = VppVxlanTunnel(
170                 cls,
171                 src=cls.pg0.local_ip4,
172                 src_port=port,
173                 dst_port=port,
174                 dst=dest_ip4,
175                 mcast_sw_if_index=1,
176                 vni=vni,
177             )
178             if is_add:
179                 r.add_vpp_config()
180             else:
181                 r.remove_vpp_config()
182
183     @classmethod
184     def add_mcast_tunnels_load(cls, port):
185         cls.add_del_mcast_tunnels_load(port=port, is_add=1)
186
187     @classmethod
188     def del_mcast_tunnels_load(cls, port):
189         cls.add_del_mcast_tunnels_load(port=port, is_add=0)
190
191     # Class method to start the VXLAN test case.
192     #  Overrides setUpClass method in VppTestCase class.
193     #  Python try..except statement is used to ensure that the tear down of
194     #  the class will be executed even if exception is raised.
195     #  @param cls The class pointer.
196     @classmethod
197     def setUpClass(cls):
198         super(TestVxlan, cls).setUpClass()
199
200         try:
201             cls.flags = 0x8
202
203             # Create 2 pg interfaces.
204             cls.create_pg_interfaces(range(4))
205             for pg in cls.pg_interfaces:
206                 pg.admin_up()
207
208             # Configure IPv4 addresses on VPP pg0.
209             cls.pg0.config_ip4()
210
211             # Resolve MAC address for VPP's IP address on pg0.
212             cls.pg0.resolve_arp()
213
214             # Our Multicast address
215             cls.mcast_ip4 = "239.1.1.1"
216             cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
217         except Exception:
218             cls.tearDownClass()
219             raise
220
221     @classmethod
222     def tearDownClass(cls):
223         super(TestVxlan, cls).tearDownClass()
224
225     def setUp(self):
226         super(TestVxlan, self).setUp()
227
228     def createVxLANInterfaces(self, port=4789):
229         # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
230         #  into BD.
231         self.dport = port
232
233         self.single_tunnel_vni = 0x12345
234         self.single_tunnel_bd = 1
235         r = VppVxlanTunnel(
236             self,
237             src=self.pg0.local_ip4,
238             dst=self.pg0.remote_ip4,
239             src_port=self.dport,
240             dst_port=self.dport,
241             vni=self.single_tunnel_vni,
242         )
243         r.add_vpp_config()
244         self.vapi.sw_interface_set_l2_bridge(
245             rx_sw_if_index=r.sw_if_index, bd_id=self.single_tunnel_bd
246         )
247         self.vapi.sw_interface_set_l2_bridge(
248             rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd
249         )
250
251         # Setup vni 2 to test multicast flooding
252         self.n_ucast_tunnels = 10
253         self.mcast_flood_bd = 2
254         self.create_vxlan_flood_test_bd(
255             self.mcast_flood_bd, self.n_ucast_tunnels, self.dport
256         )
257         r = VppVxlanTunnel(
258             self,
259             src=self.pg0.local_ip4,
260             dst=self.mcast_ip4,
261             src_port=self.dport,
262             dst_port=self.dport,
263             mcast_sw_if_index=1,
264             vni=self.mcast_flood_bd,
265         )
266         r.add_vpp_config()
267         self.vapi.sw_interface_set_l2_bridge(
268             rx_sw_if_index=r.sw_if_index, bd_id=self.mcast_flood_bd
269         )
270         self.vapi.sw_interface_set_l2_bridge(
271             rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd
272         )
273
274         # Add and delete mcast tunnels to check stability
275         self.add_shared_mcast_dst_load(self.dport)
276         self.add_mcast_tunnels_load(self.dport)
277         self.del_shared_mcast_dst_load(self.dport)
278         self.del_mcast_tunnels_load(self.dport)
279
280         # Setup vni 3 to test unicast flooding
281         self.ucast_flood_bd = 3
282         self.create_vxlan_flood_test_bd(
283             self.ucast_flood_bd, self.n_ucast_tunnels, self.dport
284         )
285         self.vapi.sw_interface_set_l2_bridge(
286             rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd
287         )
288
289         # Set scapy listen custom port for VxLAN
290         bind_layers(UDP, VXLAN, dport=self.dport)
291
292     def encap_packets(self):
293         def encap_frames(frame, n=10):
294             frames = []
295
296             # Provide IP flow hash difference.
297             for i in range(n):
298                 p = frame.copy()
299                 p[UDP].dport += i
300                 frames.append(p)
301
302             self.pg1.add_stream(frames)
303
304             self.pg0.enable_capture()
305             self.pg_start()
306
307             # Pick received frames and check if they're correctly encapsulated.
308             out = self.pg0.get_capture(n)
309             sports = set()
310             for i in range(n):
311                 pkt = out[i]
312                 self.check_encapsulation(pkt, self.single_tunnel_vni)
313
314                 payload = self.decapsulate(pkt)
315                 self.assert_eq_pkts(payload, frames[i])
316
317                 sports.add(pkt[UDP].sport)
318
319             # Check src port randomization presence, not concerned with the
320             # src ports split ratio, just as long as there are more then one.
321             self.assertGreaterEqual(len(sports), min(n, 2))
322
323         frame_ip4 = (
324             Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
325             / IP(src="4.3.2.1", dst="1.2.3.4")
326             / UDP(sport=20000, dport=10000)
327             / Raw("\xa5" * 100)
328         )
329         encap_frames(frame_ip4)
330
331         frame_ip6 = (
332             Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
333             / IPv6(src="2001:db8::4321", dst="2001:db8::1234")
334             / UDP(sport=20000, dport=10000)
335             / Raw("\xa5" * 100)
336         )
337         encap_frames(frame_ip6)
338
339         frame_mpls4 = (
340             Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
341             / MPLS(label=44, ttl=64)
342             / IP(src="4.3.2.1", dst="1.2.3.4")
343             / UDP(sport=20000, dport=10000)
344             / Raw("\xa5" * 100)
345         )
346         encap_frames(frame_mpls4)
347
348         frame_mpls6 = (
349             Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
350             / MPLS(label=44, ttl=64)
351             / IPv6(src="2001:db8::4321", dst="2001:db8::1234")
352             / UDP(sport=20000, dport=10000)
353             / Raw("\xa5" * 100)
354         )
355         encap_frames(frame_mpls6)
356
357     def encap_big_packet(self):
358         self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
359
360         frame = (
361             Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
362             / IP(src="4.3.2.1", dst="1.2.3.4")
363             / UDP(sport=20000, dport=10000)
364             / Raw(b"\xa5" * 1450)
365         )
366
367         self.pg1.add_stream([frame])
368
369         self.pg0.enable_capture()
370
371         self.pg_start()
372
373         # Pick first received frame and check if it's correctly encapsulated.
374         out = self.pg0.get_capture(2)
375         ether = out[0]
376         pkt = reassemble4(out)
377         pkt = ether / pkt
378         self.check_encapsulation(pkt, self.single_tunnel_vni)
379
380         payload = self.decapsulate(pkt)
381         # TODO: Scapy bug?
382         # self.assert_eq_pkts(payload, frame)
383
384     """
385     Tests with default port (4789)
386     """
387
388     def test_decap(self):
389         """Decapsulation test
390         from BridgeDoman
391         """
392         self.createVxLANInterfaces()
393         super(TestVxlan, self).test_decap()
394
395     def test_encap(self):
396         """Encapsulation test
397         from BridgeDoman
398         """
399         self.createVxLANInterfaces()
400         self.encap_packets()
401
402     def test_encap_big_packet(self):
403         """Encapsulation test send big frame from pg1
404         Verify receipt of encapsulated frames on pg0
405         """
406         self.createVxLANInterfaces()
407         self.encap_big_packet()
408
409     def test_ucast_flood(self):
410         """Unicast flood test
411         from BridgeDoman
412         """
413         self.createVxLANInterfaces()
414         super(TestVxlan, self).test_ucast_flood()
415
416     def test_mcast_flood(self):
417         """Multicast flood test
418         from BridgeDoman
419         """
420         self.createVxLANInterfaces()
421         super(TestVxlan, self).test_mcast_flood()
422
423     def test_mcast_rcv(self):
424         """Multicast receive test
425         from BridgeDoman
426         """
427         self.createVxLANInterfaces()
428         super(TestVxlan, self).test_mcast_rcv()
429
430     """
431     Tests with custom port
432     """
433
434     def test_decap_custom_port(self):
435         """Decapsulation test custom port
436         from BridgeDoman
437         """
438         self.createVxLANInterfaces(1111)
439         super(TestVxlan, self).test_decap()
440
441     def test_encap_custom_port(self):
442         """Encapsulation test custom port
443         from BridgeDoman
444         """
445         self.createVxLANInterfaces(1111)
446         super(TestVxlan, self).test_encap()
447
448     def test_ucast_flood_custom_port(self):
449         """Unicast flood test custom port
450         from BridgeDoman
451         """
452         self.createVxLANInterfaces(1111)
453         super(TestVxlan, self).test_ucast_flood()
454
455     def test_mcast_flood_custom_port(self):
456         """Multicast flood test custom port
457         from BridgeDoman
458         """
459         self.createVxLANInterfaces(1111)
460         super(TestVxlan, self).test_mcast_flood()
461
462     def test_mcast_rcv_custom_port(self):
463         """Multicast receive test custom port
464         from BridgeDoman
465         """
466         self.createVxLANInterfaces(1111)
467         super(TestVxlan, self).test_mcast_rcv()
468
469     # Method to define VPP actions before tear down of the test case.
470     #  Overrides tearDown method in VppTestCase class.
471     #  @param self The object pointer.
472
473     def tearDown(self):
474         super(TestVxlan, self).tearDown()
475
476     def show_commands_at_teardown(self):
477         self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
478         self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
479         self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
480         self.logger.info(self.vapi.cli("show vxlan tunnel"))
481
482
483 class TestVxlan2(VppTestCase):
484     """VXLAN Test Case"""
485
486     def setUp(self):
487         super(TestVxlan2, self).setUp()
488
489         # Create 2 pg interfaces.
490         self.create_pg_interfaces(range(4))
491         for pg in self.pg_interfaces:
492             pg.admin_up()
493
494         # Configure IPv4 addresses on VPP pg0.
495         self.pg0.config_ip4()
496         self.pg0.resolve_arp()
497
498     def tearDown(self):
499         super(TestVxlan2, self).tearDown()
500
501     def test_xconnect(self):
502         """VXLAN source address not local"""
503
504         #
505         # test the broken configuration of a VXLAN tunnel whose
506         # source address is not local ot the box. packets sent
507         # through the tunnel should be dropped
508         #
509         t = VppVxlanTunnel(self, src="10.0.0.5", dst=self.pg0.local_ip4, vni=1000)
510         t.add_vpp_config()
511         t.admin_up()
512
513         self.vapi.sw_interface_set_l2_xconnect(
514             t.sw_if_index, self.pg1.sw_if_index, enable=1
515         )
516         self.vapi.sw_interface_set_l2_xconnect(
517             self.pg1.sw_if_index, t.sw_if_index, enable=1
518         )
519
520         p = (
521             Ether(src="00:11:22:33:44:55", dst="00:00:00:11:22:33")
522             / IP(src="4.3.2.1", dst="1.2.3.4")
523             / UDP(sport=20000, dport=10000)
524             / Raw(b"\xa5" * 1450)
525         )
526
527         rx = self.send_and_assert_no_replies(self.pg1, [p])
528
529
530 class TestVxlanL2Mode(VppTestCase):
531     """VXLAN Test Case"""
532
533     def setUp(self):
534         super(TestVxlanL2Mode, self).setUp()
535
536         # Create 2 pg interfaces.
537         self.create_pg_interfaces(range(2))
538         for pg in self.pg_interfaces:
539             pg.admin_up()
540
541         # Configure IPv4 addresses on VPP pg0.
542         self.pg0.config_ip4()
543         self.pg0.resolve_arp()
544
545         # Configure IPv4 addresses on VPP pg1.
546         self.pg1.config_ip4()
547
548     def tearDown(self):
549         super(TestVxlanL2Mode, self).tearDown()
550
551     def test_l2_mode(self):
552         """VXLAN L2 mode"""
553         t = VppVxlanTunnel(
554             self, src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, vni=1000, is_l3=False
555         )
556         t.add_vpp_config()
557         t.config_ip4()
558         t.admin_up()
559
560         dstIP = t.local_ip4[:-1] + "2"
561
562         # Create a packet to send
563         p = (
564             Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
565             / IP(src=self.pg1.local_ip4, dst=dstIP)
566             / UDP(sport=555, dport=556)
567             / Raw(b"\x00" * 80)
568         )
569
570         # Expect ARP request
571         rx = self.send_and_expect(self.pg1, [p], self.pg0)
572         for p in rx:
573             self.assertEqual(p[Ether].dst, self.pg0.remote_mac)
574             self.assertEqual(p[Ether].src, self.pg0.local_mac)
575             self.assertEqual(p[ARP].op, 1)
576             self.assertEqual(p[ARP].pdst, dstIP)
577
578         # Resolve ARP
579         VppNeighbor(self, t.sw_if_index, self.pg1.remote_mac, dstIP).add_vpp_config()
580
581         # Send packets
582         NUM_PKTS = 128
583         p.dst = self.pg1.local_mac
584         rx = self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
585         self.assertEqual(NUM_PKTS, len(rx))
586
587
588 if __name__ == "__main__":
589     unittest.main(testRunner=VppTestRunner)