blob: d9e2d8159d14e050f53f81796ab7e5d4028ccb1a [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Eyal Baricef1e2a2018-06-18 13:01:59 +03002
3import socket
4import unittest
5from framework import VppTestCase, VppTestRunner
6from template_bd import BridgeDomain
7
8from scapy.layers.l2 import Ether
Vladimir Isaev698eb872020-05-21 16:34:17 +03009from scapy.packet import Raw
10from scapy.layers.inet6 import IP, IPv6, UDP
Eyal Baricef1e2a2018-06-18 13:01:59 +030011from scapy.layers.vxlan import VXLAN
Paul Vinciguerra2f156312020-05-02 22:34:40 -040012
13import util
Neale Ranns097fa662018-05-01 05:17:55 -070014from vpp_ip_route import VppIpRoute, VppRoutePath
Jakub Grajciar7c0eb562020-03-02 13:55:31 +010015from vpp_vxlan_tunnel import VppVxlanTunnel
Neale Ranns097fa662018-05-01 05:17:55 -070016from vpp_ip import INVALID_INDEX
Eyal Baricef1e2a2018-06-18 13:01:59 +030017
18
Eyal Baridd47eca2018-07-08 08:15:56 +030019class TestVxlan6(BridgeDomain, VppTestCase):
20 """ VXLAN over IPv6 Test Case """
Eyal Baricef1e2a2018-06-18 13:01:59 +030021
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 header with its
29 UDP, IP and Ethernet fields
30 """
31 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
32 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
33 UDP(sport=self.dport, dport=self.dport, chksum=0) /
34 VXLAN(vni=vni, flags=self.flags) /
35 pkt)
36
37 @classmethod
38 def ip_range(cls, s, e):
39 """ range of remote ip's """
40 tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0]
41 return ("%s:%x" % (tmp, i) for i in range(s, e))
42
43 def encap_mcast(self, pkt, src_ip, src_mac, vni):
44 """
45 Encapsulate the original payload frame by adding VXLAN header with its
46 UDP, IP and Ethernet fields
47 """
48 return (Ether(src=src_mac, dst=self.mcast_mac) /
49 IPv6(src=src_ip, dst=self.mcast_ip6) /
50 UDP(sport=self.dport, dport=self.dport, chksum=0) /
51 VXLAN(vni=vni, flags=self.flags) /
52 pkt)
53
54 def decapsulate(self, pkt):
55 """
56 Decapsulate the original payload frame by removing VXLAN header
57 """
58 # check if is set I flag
59 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
60 return pkt[VXLAN].payload
61
62 # Method for checking VXLAN encapsulation.
63 #
64 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
65 # TODO: add error messages
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 tunnel source IP is VPP_IP and destination IP is MY_IP.
75 self.assertEqual(pkt[IPv6].src, self.pg0.local_ip6)
76 if not local_only:
77 if not mcast_pkt:
78 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
79 else:
80 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
81 # Verify UDP destination port is VXLAN 4789, source UDP port could be
82 # arbitrary.
83 self.assertEqual(pkt[UDP].dport, type(self).dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030084 # Verify UDP checksum
85 self.assert_udp_checksum_valid(pkt, ignore_zero_checksum=False)
Eyal Baricef1e2a2018-06-18 13:01:59 +030086 # Verify VNI
87 self.assertEqual(pkt[VXLAN].vni, vni)
88
89 @classmethod
90 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
91 # Create 10 ucast vxlan tunnels under bd
92 start = 10
93 end = start + n_ucast_tunnels
Eyal Baricef1e2a2018-06-18 13:01:59 +030094 for dest_ip6 in cls.ip_range(start, end):
Eyal Baricef1e2a2018-06-18 13:01:59 +030095 # add host route so dest ip will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070096 rip = VppIpRoute(cls, dest_ip6, 128,
97 [VppRoutePath(cls.pg0.remote_ip6, INVALID_INDEX)],
98 register=False)
99 rip.add_vpp_config()
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100100 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip6,
101 dst=dest_ip6, vni=vni)
102 r.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700103 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300104
105 @classmethod
106 def add_mcast_tunnels_load(cls):
107 cls.add_del_mcast_tunnels_load(is_add=1)
108
109 @classmethod
110 def del_mcast_tunnels_load(cls):
111 cls.add_del_mcast_tunnels_load(is_add=0)
112
113 # Class method to start the VXLAN test case.
114 # Overrides setUpClass method in VppTestCase class.
115 # Python try..except statement is used to ensure that the tear down of
116 # the class will be executed even if exception is raised.
117 # @param cls The class pointer.
118 @classmethod
119 def setUpClass(cls):
Eyal Baridd47eca2018-07-08 08:15:56 +0300120 super(TestVxlan6, cls).setUpClass()
Eyal Baricef1e2a2018-06-18 13:01:59 +0300121
122 try:
123 cls.dport = 4789
124 cls.flags = 0x8
125
126 # Create 2 pg interfaces.
127 cls.create_pg_interfaces(range(4))
128 for pg in cls.pg_interfaces:
129 pg.admin_up()
130
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400131 # Configure IPv6 addresses on VPP pg0.
Eyal Baricef1e2a2018-06-18 13:01:59 +0300132 cls.pg0.config_ip6()
133
134 # Resolve MAC address for VPP's IP address on pg0.
135 cls.pg0.resolve_ndp()
136
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400137 # Our Multicast address
Eyal Baricef1e2a2018-06-18 13:01:59 +0300138 cls.mcast_ip6 = 'ff0e::1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400139 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip6)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300140 except Exception:
Eyal Baridd47eca2018-07-08 08:15:56 +0300141 super(TestVxlan6, cls).tearDownClass()
Eyal Baricef1e2a2018-06-18 13:01:59 +0300142 raise
143
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800144 @classmethod
145 def tearDownClass(cls):
146 super(TestVxlan6, cls).tearDownClass()
147
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100148 def setUp(self):
149 super(TestVxlan6, self).setUp()
150 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
151 # into BD.
Neale Ranns91fd9102020-04-03 07:46:28 +0000152 self.single_tunnel_vni = 0x12345
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100153 self.single_tunnel_bd = 1
154 r = VppVxlanTunnel(self, src=self.pg0.local_ip6,
Neale Ranns91fd9102020-04-03 07:46:28 +0000155 dst=self.pg0.remote_ip6,
156 vni=self.single_tunnel_vni)
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100157 r.add_vpp_config()
158 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
159 bd_id=self.single_tunnel_bd)
160 self.vapi.sw_interface_set_l2_bridge(
161 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
162
163 # Setup vni 2 to test multicast flooding
164 self.n_ucast_tunnels = 10
165 self.mcast_flood_bd = 2
166 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
167 self.n_ucast_tunnels)
168 r = VppVxlanTunnel(self, src=self.pg0.local_ip6, dst=self.mcast_ip6,
169 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
170 r.add_vpp_config()
171 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
172 bd_id=self.mcast_flood_bd)
173 self.vapi.sw_interface_set_l2_bridge(
174 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
175
176 # Setup vni 3 to test unicast flooding
177 self.ucast_flood_bd = 3
178 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
179 self.n_ucast_tunnels)
180 self.vapi.sw_interface_set_l2_bridge(
181 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
182
Eyal Baricef1e2a2018-06-18 13:01:59 +0300183 # Method to define VPP actions before tear down of the test case.
184 # Overrides tearDown method in VppTestCase class.
185 # @param self The object pointer.
186 def tearDown(self):
Eyal Baridd47eca2018-07-08 08:15:56 +0300187 super(TestVxlan6, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700188
189 def show_commands_at_teardown(self):
190 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
191 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
192 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
193 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Eyal Baricef1e2a2018-06-18 13:01:59 +0300194
Vladimir Isaev698eb872020-05-21 16:34:17 +0300195 def test_encap_fragmented_packet(self):
196 """ Encapsulation test send fragments from pg1
197 Verify receipt of encapsulated frames on pg0
198 """
199
200 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
201 IP(src='4.3.2.1', dst='1.2.3.4') /
202 UDP(sport=20000, dport=10000) /
203 Raw(b'\xa5' * 1000))
204
205 frags = util.fragment_rfc791(frame, 400)
206
207 self.pg1.add_stream(frags)
208
209 self.pg0.enable_capture()
210
211 self.pg_start()
212
213 out = self.pg0.get_capture(3)
214
215 payload = []
216 for pkt in out:
217 payload.append(self.decapsulate(pkt))
218 self.check_encapsulation(pkt, self.single_tunnel_vni)
219
220 reassembled = util.reassemble4(payload)
221
222 self.assertEqual(Ether(raw(frame))[IP], reassembled[IP])
223
Eyal Baricef1e2a2018-06-18 13:01:59 +0300224
225if __name__ == '__main__':
226 unittest.main(testRunner=VppTestRunner)