blob: f432becfce30ddd939909647bbc9f9686a414ff8 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08002
3import socket
Paul Vinciguerra2f156312020-05-02 22:34:40 -04004from util import ip4_range
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08005import unittest
Klement Sekerab23ffd72021-05-31 16:08:53 +02006from config import config
7from framework import VppTestCase, VppTestRunner
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08008from template_bd import BridgeDomain
9
snaramre5d4b8912019-12-13 23:39:35 +000010from scapy.layers.l2 import Ether
Artem Glazychevea962922021-05-28 19:09:14 +070011from scapy.packet import Raw, bind_layers
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080012from scapy.layers.inet import IP, UDP
13from scapy.layers.vxlan import VXLAN
Paul Vinciguerra2f156312020-05-02 22:34:40 -040014
15import util
Neale Ranns097fa662018-05-01 05:17:55 -070016from vpp_ip_route import VppIpRoute, VppRoutePath
Artem Glazychevea962922021-05-28 19:09:14 +070017from vpp_vxlan_gpe_tunnel import VppVxlanGpeTunnel
Neale Ranns097fa662018-05-01 05:17:55 -070018from vpp_ip import INVALID_INDEX
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080019
20
Klement Sekerab23ffd72021-05-31 16:08:53 +020021@unittest.skipUnless(config.extended, "part of extended tests")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080022class TestVxlanGpe(BridgeDomain, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020023 """VXLAN-GPE Test Case"""
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080024
25 def __init__(self, *args):
26 BridgeDomain.__init__(self)
27 VppTestCase.__init__(self, *args)
28
29 def encapsulate(self, pkt, vni):
30 """
31 Encapsulate the original payload frame by adding VXLAN-GPE header
32 with its UDP, IP and Ethernet fields
33 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 return (
35 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
36 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
37 / UDP(sport=self.dport, dport=self.dport, chksum=0)
38 / VXLAN(vni=vni, flags=self.flags)
39 / pkt
40 )
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080041
Eyal Baricef1e2a2018-06-18 13:01:59 +030042 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +030044 return ip4_range(self.pg0.remote_ip4, start, end)
45
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080046 def encap_mcast(self, pkt, src_ip, src_mac, vni):
47 """
48 Encapsulate the original payload frame by adding VXLAN-GPE header
49 with its UDP, IP and Ethernet fields
50 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020051 return (
52 Ether(src=src_mac, dst=self.mcast_mac)
53 / IP(src=src_ip, dst=self.mcast_ip4)
54 / UDP(sport=self.dport, dport=self.dport, chksum=0)
55 / VXLAN(vni=vni, flags=self.flags)
56 / pkt
57 )
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080058
59 def decapsulate(self, pkt):
60 """
61 Decapsulate the original payload frame by removing VXLAN-GPE header
62 """
63 # check if is set I and P flag
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020064 self.assertEqual(pkt[VXLAN].flags, 0x0C)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080065 return pkt[VXLAN].payload
66
67 # Method for checking VXLAN-GPE encapsulation.
68 #
69 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
70 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
71 # by VPP using ARP.
72 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
73 if not local_only:
74 if not mcast_pkt:
75 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
76 else:
77 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
78 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
79 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
80 if not local_only:
81 if not mcast_pkt:
82 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
83 else:
84 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
85 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
86 # could be arbitrary.
Artem Glazychevea962922021-05-28 19:09:14 +070087 self.assertEqual(pkt[UDP].dport, self.dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030088 # Verify UDP checksum
89 self.assert_udp_checksum_valid(pkt)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080090 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010091 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080092
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080093 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +070094 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
Neale Ranns097fa662018-05-01 05:17:55 -070095 # Create 10 ucast vxlan tunnels under bd
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080096 ip_range_start = 10
97 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070098 next_hop_address = cls.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800100 # add host route so dest_ip4n will not be resolved
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 rip = VppIpRoute(
102 cls,
103 dest_ip4,
104 32,
105 [VppRoutePath(next_hop_address, INVALID_INDEX)],
106 register=False,
107 )
Neale Ranns097fa662018-05-01 05:17:55 -0700108 rip.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700109
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 r = VppVxlanGpeTunnel(
111 cls,
112 src_addr=cls.pg0.local_ip4,
113 dst_addr=dest_ip4,
114 src_port=port,
115 dst_port=port,
116 vni=vni,
117 )
Artem Glazychevea962922021-05-28 19:09:14 +0700118 r.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800120
121 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700122 def add_del_shared_mcast_dst_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800123 """
124 add or del tunnels sharing the same mcast dst
125 to test vxlan_gpe ref_count mechanism
126 """
127 n_shared_dst_tunnels = 20
128 vni_start = 1000
129 vni_end = vni_start + n_shared_dst_tunnels
130 for vni in range(vni_start, vni_end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200131 r = VppVxlanGpeTunnel(
132 cls,
133 src_addr=cls.pg0.local_ip4,
134 dst_addr=cls.mcast_ip4,
135 src_port=port,
136 dst_port=port,
137 mcast_sw_if_index=1,
138 vni=vni,
139 )
Artem Glazychevea962922021-05-28 19:09:14 +0700140 if is_add:
141 r.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 if r.sw_if_index == 0xFFFFFFFF:
Artem Glazychevea962922021-05-28 19:09:14 +0700143 raise ValueError("bad sw_if_index: ~0")
144 else:
145 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800146
147 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700148 def add_shared_mcast_dst_load(cls, port):
149 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800150
151 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700152 def del_shared_mcast_dst_load(cls, port):
153 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800154
155 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700156 def add_del_mcast_tunnels_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800157 """
158 add or del tunnels to test vxlan_gpe stability
159 """
160 n_distinct_dst_tunnels = 20
161 ip_range_start = 10
162 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400164 vni = int(dest_ip4.split(".")[3])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200165 r = VppVxlanGpeTunnel(
166 cls,
167 src_addr=cls.pg0.local_ip4,
168 dst_addr=dest_ip4,
169 src_port=port,
170 dst_port=port,
171 mcast_sw_if_index=1,
172 vni=vni,
173 )
Artem Glazychevea962922021-05-28 19:09:14 +0700174 if is_add:
175 r.add_vpp_config()
176 else:
177 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800178
179 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700180 def add_mcast_tunnels_load(cls, port):
181 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800182
183 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700184 def del_mcast_tunnels_load(cls, port):
185 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800186
187 # Class method to start the VXLAN-GPE test case.
188 # Overrides setUpClass method in VppTestCase class.
189 # Python try..except statement is used to ensure that the tear down of
190 # the class will be executed even if exception is raised.
191 # @param cls The class pointer.
192 @classmethod
193 def setUpClass(cls):
194 super(TestVxlanGpe, cls).setUpClass()
195
196 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200197 cls.flags = 0x0C
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800198
199 # Create 2 pg interfaces.
200 cls.create_pg_interfaces(range(4))
201 for pg in cls.pg_interfaces:
202 pg.admin_up()
203
204 # Configure IPv4 addresses on VPP pg0.
205 cls.pg0.config_ip4()
206
207 # Resolve MAC address for VPP's IP address on pg0.
208 cls.pg0.resolve_arp()
209
210 # Our Multicast address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200211 cls.mcast_ip4 = "239.1.1.1"
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400212 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800213 except Exception:
Artem Glazychevea962922021-05-28 19:09:14 +0700214 cls.tearDownClass()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800215 raise
216
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800217 @classmethod
218 def tearDownClass(cls):
219 super(TestVxlanGpe, cls).tearDownClass()
220
Artem Glazychevea962922021-05-28 19:09:14 +0700221 def setUp(self):
222 super(TestVxlanGpe, self).setUp()
223
224 def createVxLANInterfaces(self, port=4790):
225 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
226 # and pg1 into BD.
227 self.dport = port
228
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229 self.single_tunnel_vni = 0xABCDE
Artem Glazychevea962922021-05-28 19:09:14 +0700230 self.single_tunnel_bd = 11
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200231 r = VppVxlanGpeTunnel(
232 self,
233 src_addr=self.pg0.local_ip4,
234 dst_addr=self.pg0.remote_ip4,
235 src_port=port,
236 dst_port=port,
237 vni=self.single_tunnel_vni,
238 )
Artem Glazychevea962922021-05-28 19:09:14 +0700239 r.add_vpp_config()
Artem Glazychevea962922021-05-28 19:09:14 +0700240 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200241 rx_sw_if_index=r.sw_if_index, bd_id=self.single_tunnel_bd
242 )
243 self.vapi.sw_interface_set_l2_bridge(
244 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd
245 )
Artem Glazychevea962922021-05-28 19:09:14 +0700246
247 # Setup vni 2 to test multicast flooding
248 self.n_ucast_tunnels = 10
249 self.mcast_flood_bd = 12
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200250 self.create_vxlan_gpe_flood_test_bd(
251 self.mcast_flood_bd, self.n_ucast_tunnels, self.dport
252 )
253 r = VppVxlanGpeTunnel(
254 self,
255 src_addr=self.pg0.local_ip4,
256 dst_addr=self.mcast_ip4,
257 src_port=port,
258 dst_port=port,
259 mcast_sw_if_index=1,
260 vni=self.mcast_flood_bd,
261 )
Artem Glazychevea962922021-05-28 19:09:14 +0700262 r.add_vpp_config()
Artem Glazychevea962922021-05-28 19:09:14 +0700263 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200264 rx_sw_if_index=r.sw_if_index, bd_id=self.mcast_flood_bd
265 )
266 self.vapi.sw_interface_set_l2_bridge(
267 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd
268 )
Artem Glazychevea962922021-05-28 19:09:14 +0700269
270 # Add and delete mcast tunnels to check stability
271 self.add_shared_mcast_dst_load(self.dport)
272 self.add_mcast_tunnels_load(self.dport)
273 self.del_shared_mcast_dst_load(self.dport)
274 self.del_mcast_tunnels_load(self.dport)
275
276 # Setup vni 3 to test unicast flooding
277 self.ucast_flood_bd = 13
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200278 self.create_vxlan_gpe_flood_test_bd(
279 self.ucast_flood_bd, self.n_ucast_tunnels, self.dport
280 )
Artem Glazychevea962922021-05-28 19:09:14 +0700281 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200282 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd
283 )
Artem Glazychevea962922021-05-28 19:09:14 +0700284
285 # Set scapy listen custom port for VxLAN
286 bind_layers(UDP, VXLAN, dport=self.dport)
287
288 """
289 Tests with default port (4790)
290 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200291
Artem Glazychevea962922021-05-28 19:09:14 +0700292 def test_decap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200293 """Decapsulation test
Artem Glazychevea962922021-05-28 19:09:14 +0700294 from BridgeDoman
295 """
296 self.createVxLANInterfaces()
297 super(TestVxlanGpe, self).test_decap()
298
299 def test_encap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200300 """Encapsulation test
Artem Glazychevea962922021-05-28 19:09:14 +0700301 from BridgeDoman
302 """
303 self.createVxLANInterfaces()
304 super(TestVxlanGpe, self).test_encap()
305
306 def test_ucast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200307 """Unicast flood test
Artem Glazychevea962922021-05-28 19:09:14 +0700308 from BridgeDoman
309 """
310 self.createVxLANInterfaces()
311 super(TestVxlanGpe, self).test_ucast_flood()
312
313 """
314 Tests with custom port (1112)
315 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200316
Artem Glazychevea962922021-05-28 19:09:14 +0700317 def test_decap_custom_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200318 """Decapsulation test custom port
Artem Glazychevea962922021-05-28 19:09:14 +0700319 from BridgeDoman
320 """
321 self.createVxLANInterfaces(1112)
322 super(TestVxlanGpe, self).test_decap()
323
324 def test_encap_custom_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200325 """Encapsulation test custom port
Artem Glazychevea962922021-05-28 19:09:14 +0700326 from BridgeDoman
327 """
328 self.createVxLANInterfaces(1112)
329 super(TestVxlanGpe, self).test_encap()
330
331 def test_ucast_flood_custom_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200332 """Unicast flood test custom port
Artem Glazychevea962922021-05-28 19:09:14 +0700333 from BridgeDoman
334 """
335 self.createVxLANInterfaces(1112)
336 super(TestVxlanGpe, self).test_ucast_flood()
337
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100338 @unittest.skip("test disabled for vxlan-gpe")
339 def test_mcast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200340 """inherited from BridgeDomain"""
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100341 pass
342
343 @unittest.skip("test disabled for vxlan-gpe")
344 def test_mcast_rcv(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200345 """inherited from BridgeDomain"""
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100346 pass
347
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800348 # Method to define VPP actions before tear down of the test case.
349 # Overrides tearDown method in VppTestCase class.
350 # @param self The object pointer.
351 def tearDown(self):
352 super(TestVxlanGpe, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700353
354 def show_commands_at_teardown(self):
355 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
356 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
357 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
358 self.logger.info(self.vapi.cli("show int"))
359 self.logger.info(self.vapi.cli("show vxlan-gpe"))
360 self.logger.info(self.vapi.cli("show trace"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800361
362
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200363if __name__ == "__main__":
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800364 unittest.main(testRunner=VppTestRunner)