blob: dc48f539f6e3d2fa1ae337b27215357514779c68 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Damjan Marionf56b77a2016-10-03 19:44:57 +02002
Eyal Baric4aaee12016-12-20 18:36:46 +02003import socket
Paul Vinciguerra2f156312020-05-02 22:34:40 -04004from util import ip4_range, reassemble4
Damjan Marionf56b77a2016-10-03 19:44:57 +02005import unittest
6from framework import VppTestCase, VppTestRunner
Damjan Marionf56b77a2016-10-03 19:44:57 +02007from template_bd import BridgeDomain
8
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
10from scapy.packet import Raw
Damjan Marionf56b77a2016-10-03 19:44:57 +020011from scapy.layers.inet import IP, UDP
Matej Klottondeb69842016-12-09 15:05:46 +010012from scapy.layers.vxlan import VXLAN
Paul Vinciguerra2f156312020-05-02 22:34:40 -040013
14import util
Neale Ranns097fa662018-05-01 05:17:55 -070015from vpp_ip_route import VppIpRoute, VppRoutePath
Jakub Grajciar7c0eb562020-03-02 13:55:31 +010016from vpp_vxlan_tunnel import VppVxlanTunnel
Neale Ranns097fa662018-05-01 05:17:55 -070017from vpp_ip import INVALID_INDEX
Damjan Marionf56b77a2016-10-03 19:44:57 +020018
19
Klement Sekeraf62ae122016-10-11 11:47:09 +020020class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020021 """ VXLAN Test Case """
22
Damjan Marionf56b77a2016-10-03 19:44:57 +020023 def __init__(self, *args):
24 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020025 VppTestCase.__init__(self, *args)
26
Eyal Baric4aaee12016-12-20 18:36:46 +020027 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020028 """
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) /
Eyal Baric4aaee12016-12-20 18:36:46 +020035 VXLAN(vni=vni, flags=self.flags) /
36 pkt)
37
Eyal Baricef1e2a2018-06-18 13:01:59 +030038 def ip_range(self, start, end):
39 """ range of remote ip's """
40 return ip4_range(self.pg0.remote_ip4, start, end)
41
Eyal Baric4aaee12016-12-20 18:36:46 +020042 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 """
Eyal Barid81da8c2017-01-11 13:39:54 +020047 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020048 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) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020051 pkt)
52
Damjan Marionf56b77a2016-10-03 19:44:57 +020053 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020054 """
55 Decapsulate the original payload frame by removing VXLAN header
56 """
Matej Klottondeb69842016-12-09 15:05:46 +010057 # check if is set I flag
58 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020059 return pkt[VXLAN].payload
60
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020062 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020063 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020064 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020065 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020066 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020067 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020068 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020069 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)
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
74 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020075 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020076 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)
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020081 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020082 self.assertEqual(pkt[UDP].dport, type(self).dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030083 # Verify UDP checksum
84 self.assert_udp_checksum_valid(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +020085 # Verify VNI
86 self.assertEqual(pkt[VXLAN].vni, vni)
87
Eyal Baric4aaee12016-12-20 18:36:46 +020088 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020089 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +020090 # Create 10 ucast vxlan tunnels under bd
91 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +020092 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070093 next_hop_address = cls.pg0.remote_ip4
94 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
95 ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -040096 # add host route so dest_ip4 will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070097 rip = VppIpRoute(cls, dest_ip4, 32,
98 [VppRoutePath(next_hop_address,
99 INVALID_INDEX)],
100 register=False)
101 rip.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700102
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100103 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
104 dst=dest_ip4, vni=vni)
105 r.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700106 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +0200107
108 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200109 def add_del_shared_mcast_dst_load(cls, is_add):
110 """
111 add or del tunnels sharing the same mcast dst
112 to test vxlan ref_count mechanism
113 """
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100114 n_shared_dst_tunnels = 20
Eyal Bari4bce2902017-01-16 12:02:46 +0200115 vni_start = 10000
116 vni_end = vni_start + n_shared_dst_tunnels
117 for vni in range(vni_start, vni_end):
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100118 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
119 dst=cls.mcast_ip4, mcast_sw_if_index=1, vni=vni)
120 if is_add:
121 r.add_vpp_config()
122 if r.sw_if_index == 0xffffffff:
123 raise ValueError("bad sw_if_index: ~0")
124 else:
125 r.remove_vpp_config()
Eyal Bari4bce2902017-01-16 12:02:46 +0200126
127 @classmethod
128 def add_shared_mcast_dst_load(cls):
129 cls.add_del_shared_mcast_dst_load(is_add=1)
130
131 @classmethod
132 def del_shared_mcast_dst_load(cls):
133 cls.add_del_shared_mcast_dst_load(is_add=0)
134
135 @classmethod
136 def add_del_mcast_tunnels_load(cls, is_add):
137 """
138 add or del tunnels to test vxlan stability
139 """
140 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200141 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200142 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100143 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
144 ip_range_end):
145 vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3]
146 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
147 dst=dest_ip4, mcast_sw_if_index=1, vni=vni)
148 if is_add:
149 r.add_vpp_config()
150 else:
151 r.remove_vpp_config()
Eyal Baric4aaee12016-12-20 18:36:46 +0200152
153 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200154 def add_mcast_tunnels_load(cls):
155 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200156
157 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200158 def del_mcast_tunnels_load(cls):
159 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200160
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200162 # Overrides setUpClass method in VppTestCase class.
163 # Python try..except statement is used to ensure that the tear down of
164 # the class will be executed even if exception is raised.
165 # @param cls The class pointer.
166 @classmethod
167 def setUpClass(cls):
168 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200169
Klement Sekeraf62ae122016-10-11 11:47:09 +0200170 try:
171 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100172 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200173
174 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200175 cls.create_pg_interfaces(range(4))
176 for pg in cls.pg_interfaces:
177 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200178
179 # Configure IPv4 addresses on VPP pg0.
180 cls.pg0.config_ip4()
181
182 # Resolve MAC address for VPP's IP address on pg0.
183 cls.pg0.resolve_arp()
184
Eyal Baric4aaee12016-12-20 18:36:46 +0200185 # Our Multicast address
186 cls.mcast_ip4 = '239.1.1.1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400187 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200188 except Exception:
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400189 cls.tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200190 raise
191
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700192 @classmethod
193 def tearDownClass(cls):
194 super(TestVxlan, cls).tearDownClass()
195
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100196 def setUp(self):
197 super(TestVxlan, self).setUp()
198 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
199 # into BD.
Neale Ranns91fd9102020-04-03 07:46:28 +0000200 self.single_tunnel_vni = 0x12345
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100201 self.single_tunnel_bd = 1
202 r = VppVxlanTunnel(self, src=self.pg0.local_ip4,
Neale Ranns91fd9102020-04-03 07:46:28 +0000203 dst=self.pg0.remote_ip4,
204 vni=self.single_tunnel_vni)
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100205 r.add_vpp_config()
206 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
207 bd_id=self.single_tunnel_bd)
208 self.vapi.sw_interface_set_l2_bridge(
209 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
210
211 # Setup vni 2 to test multicast flooding
212 self.n_ucast_tunnels = 10
213 self.mcast_flood_bd = 2
214 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
215 self.n_ucast_tunnels)
216 r = VppVxlanTunnel(self, src=self.pg0.local_ip4, dst=self.mcast_ip4,
217 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
218 r.add_vpp_config()
219 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
220 bd_id=self.mcast_flood_bd)
221 self.vapi.sw_interface_set_l2_bridge(
222 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
223
224 # Add and delete mcast tunnels to check stability
225 self.add_shared_mcast_dst_load()
226 self.add_mcast_tunnels_load()
227 self.del_shared_mcast_dst_load()
228 self.del_mcast_tunnels_load()
229
230 # Setup vni 3 to test unicast flooding
231 self.ucast_flood_bd = 3
232 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
233 self.n_ucast_tunnels)
234 self.vapi.sw_interface_set_l2_bridge(
235 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
236
237 def test_decap(self):
238 """ Decapsulation test
239 from BridgeDoman
240 """
241 super(TestVxlan, self).test_decap()
242
Ole Troanb3655e52018-08-16 22:08:49 +0200243 def test_encap_big_packet(self):
244 """ Encapsulation test send big frame from pg1
245 Verify receipt of encapsulated frames on pg0
246 """
247
248 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
249
250 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
251 IP(src='4.3.2.1', dst='1.2.3.4') /
252 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100253 Raw(b'\xa5' * 1450))
Ole Troanb3655e52018-08-16 22:08:49 +0200254
255 self.pg1.add_stream([frame])
256
257 self.pg0.enable_capture()
258
259 self.pg_start()
260
261 # Pick first received frame and check if it's correctly encapsulated.
262 out = self.pg0.get_capture(2)
263 ether = out[0]
Ole Troan7f991832018-12-06 17:35:12 +0100264 pkt = reassemble4(out)
Ole Troanb3655e52018-08-16 22:08:49 +0200265 pkt = ether / pkt
Neale Ranns91fd9102020-04-03 07:46:28 +0000266 self.check_encapsulation(pkt, self.single_tunnel_vni)
Ole Troanb3655e52018-08-16 22:08:49 +0200267
268 payload = self.decapsulate(pkt)
269 # TODO: Scapy bug?
270 # self.assert_eq_pkts(payload, frame)
271
Klement Sekeraf62ae122016-10-11 11:47:09 +0200272 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200273 # Overrides tearDown method in VppTestCase class.
274 # @param self The object pointer.
275 def tearDown(self):
276 super(TestVxlan, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700277
278 def show_commands_at_teardown(self):
279 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
280 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
281 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
282 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200283
Matej Klottondeb69842016-12-09 15:05:46 +0100284
Neale Ranns1b5ca982020-12-16 13:06:58 +0000285class TestVxlan2(VppTestCase):
286 """ VXLAN Test Case """
287 def setUp(self):
288 super(TestVxlan2, self).setUp()
289
290 # Create 2 pg interfaces.
291 self.create_pg_interfaces(range(4))
292 for pg in self.pg_interfaces:
293 pg.admin_up()
294
295 # Configure IPv4 addresses on VPP pg0.
296 self.pg0.config_ip4()
297 self.pg0.resolve_arp()
298
299 def tearDown(self):
300 super(TestVxlan2, self).tearDown()
301
302 def test_xconnect(self):
303 """ VXLAN source address not local """
304
305 #
306 # test the broken configuration of a VXLAN tunnel whose
307 # source address is not local ot the box. packets sent
308 # through the tunnel should be dropped
309 #
310 t = VppVxlanTunnel(self,
311 src="10.0.0.5",
312 dst=self.pg0.local_ip4,
313 vni=1000)
314 t.add_vpp_config()
315 t.admin_up()
316
317 self.vapi.sw_interface_set_l2_xconnect(t.sw_if_index,
318 self.pg1.sw_if_index,
319 enable=1)
320 self.vapi.sw_interface_set_l2_xconnect(self.pg1.sw_if_index,
321 t.sw_if_index,
322 enable=1)
323
324 p = (Ether(src="00:11:22:33:44:55",
325 dst="00:00:00:11:22:33") /
326 IP(src="4.3.2.1", dst="1.2.3.4") /
327 UDP(sport=20000, dport=10000) /
328 Raw(b'\xa5' * 1450))
329
330 rx = self.send_and_assert_no_replies(self.pg1, [p])
331
332
Damjan Marionf56b77a2016-10-03 19:44:57 +0200333if __name__ == '__main__':
334 unittest.main(testRunner=VppTestRunner)