blob: 828b5fc30cdacffe6b4942bddf54fb5ff0daaccc [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):
23 """ VXLAN-GPE Test Case """
24
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 """
34 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
35 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
36 UDP(sport=self.dport, dport=self.dport, chksum=0) /
37 VXLAN(vni=vni, flags=self.flags) /
38 pkt)
39
Eyal Baricef1e2a2018-06-18 13:01:59 +030040 def ip_range(self, start, end):
41 """ range of remote ip's """
42 return ip4_range(self.pg0.remote_ip4, start, end)
43
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080044 def encap_mcast(self, pkt, src_ip, src_mac, vni):
45 """
46 Encapsulate the original payload frame by adding VXLAN-GPE header
47 with its UDP, IP and Ethernet fields
48 """
49 return (Ether(src=src_mac, dst=self.mcast_mac) /
50 IP(src=src_ip, dst=self.mcast_ip4) /
51 UDP(sport=self.dport, dport=self.dport, chksum=0) /
52 VXLAN(vni=vni, flags=self.flags) /
53 pkt)
54
55 def decapsulate(self, pkt):
56 """
57 Decapsulate the original payload frame by removing VXLAN-GPE header
58 """
59 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010060 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080061 return pkt[VXLAN].payload
62
63 # Method for checking VXLAN-GPE encapsulation.
64 #
65 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
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-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
75 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
76 if not local_only:
77 if not mcast_pkt:
78 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
79 else:
80 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
81 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
82 # could be arbitrary.
Artem Glazychevea962922021-05-28 19:09:14 +070083 self.assertEqual(pkt[UDP].dport, self.dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030084 # Verify UDP checksum
85 self.assert_udp_checksum_valid(pkt)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080086 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010087 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080088
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080089 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +070090 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
Neale Ranns097fa662018-05-01 05:17:55 -070091 # Create 10 ucast vxlan tunnels under bd
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080092 ip_range_start = 10
93 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070094 next_hop_address = cls.pg0.remote_ip4
95 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
96 ip_range_end):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080097 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070098 rip = VppIpRoute(cls, dest_ip4, 32,
99 [VppRoutePath(next_hop_address,
100 INVALID_INDEX)],
101 register=False)
102 rip.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700103
Artem Glazychevea962922021-05-28 19:09:14 +0700104 r = VppVxlanGpeTunnel(cls,
105 src_addr=cls.pg0.local_ip4,
106 dst_addr=dest_ip4,
107 src_port=port,
108 dst_port=port,
109 vni=vni)
110 r.add_vpp_config()
Ole Troana5b2eec2019-03-11 19:23:25 +0100111 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
112 bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800113
114 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700115 def add_del_shared_mcast_dst_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800116 """
117 add or del tunnels sharing the same mcast dst
118 to test vxlan_gpe ref_count mechanism
119 """
120 n_shared_dst_tunnels = 20
121 vni_start = 1000
122 vni_end = vni_start + n_shared_dst_tunnels
123 for vni in range(vni_start, vni_end):
Artem Glazychevea962922021-05-28 19:09:14 +0700124 r = VppVxlanGpeTunnel(cls,
125 src_addr=cls.pg0.local_ip4,
126 dst_addr=cls.mcast_ip4,
127 src_port=port,
128 dst_port=port,
129 mcast_sw_if_index=1,
130 vni=vni)
131 if is_add:
132 r.add_vpp_config()
133 if r.sw_if_index == 0xffffffff:
134 raise ValueError("bad sw_if_index: ~0")
135 else:
136 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800137
138 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700139 def add_shared_mcast_dst_load(cls, port):
140 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800141
142 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700143 def del_shared_mcast_dst_load(cls, port):
144 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800145
146 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700147 def add_del_mcast_tunnels_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800148 """
149 add or del tunnels to test vxlan_gpe stability
150 """
151 n_distinct_dst_tunnels = 20
152 ip_range_start = 10
153 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400154 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
155 ip_range_end):
156 vni = int(dest_ip4.split(".")[3])
Artem Glazychevea962922021-05-28 19:09:14 +0700157 r = VppVxlanGpeTunnel(cls,
158 src_addr=cls.pg0.local_ip4,
159 dst_addr=dest_ip4,
160 src_port=port,
161 dst_port=port,
162 mcast_sw_if_index=1,
163 vni=vni)
164 if is_add:
165 r.add_vpp_config()
166 else:
167 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800168
169 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700170 def add_mcast_tunnels_load(cls, port):
171 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800172
173 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700174 def del_mcast_tunnels_load(cls, port):
175 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800176
177 # Class method to start the VXLAN-GPE test case.
178 # Overrides setUpClass method in VppTestCase class.
179 # Python try..except statement is used to ensure that the tear down of
180 # the class will be executed even if exception is raised.
181 # @param cls The class pointer.
182 @classmethod
183 def setUpClass(cls):
184 super(TestVxlanGpe, cls).setUpClass()
185
186 try:
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100187 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800188
189 # Create 2 pg interfaces.
190 cls.create_pg_interfaces(range(4))
191 for pg in cls.pg_interfaces:
192 pg.admin_up()
193
194 # Configure IPv4 addresses on VPP pg0.
195 cls.pg0.config_ip4()
196
197 # Resolve MAC address for VPP's IP address on pg0.
198 cls.pg0.resolve_arp()
199
200 # Our Multicast address
201 cls.mcast_ip4 = '239.1.1.1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400202 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800203 except Exception:
Artem Glazychevea962922021-05-28 19:09:14 +0700204 cls.tearDownClass()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800205 raise
206
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800207 @classmethod
208 def tearDownClass(cls):
209 super(TestVxlanGpe, cls).tearDownClass()
210
Artem Glazychevea962922021-05-28 19:09:14 +0700211 def setUp(self):
212 super(TestVxlanGpe, self).setUp()
213
214 def createVxLANInterfaces(self, port=4790):
215 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
216 # and pg1 into BD.
217 self.dport = port
218
219 self.single_tunnel_vni = 0xabcde
220 self.single_tunnel_bd = 11
221 r = VppVxlanGpeTunnel(self,
222 src_addr=self.pg0.local_ip4,
223 dst_addr=self.pg0.remote_ip4,
224 src_port=port,
225 dst_port=port,
226 vni=self.single_tunnel_vni)
227 r.add_vpp_config()
228 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
229 bd_id=self.single_tunnel_bd)
230 self.vapi.sw_interface_set_l2_bridge(
231 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
232
233 # Setup vni 2 to test multicast flooding
234 self.n_ucast_tunnels = 10
235 self.mcast_flood_bd = 12
236 self.create_vxlan_gpe_flood_test_bd(self.mcast_flood_bd,
237 self.n_ucast_tunnels,
238 self.dport)
239 r = VppVxlanGpeTunnel(self,
240 src_addr=self.pg0.local_ip4,
241 dst_addr=self.mcast_ip4,
242 src_port=port,
243 dst_port=port,
244 mcast_sw_if_index=1,
245 vni=self.mcast_flood_bd)
246 r.add_vpp_config()
247 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
248 bd_id=self.mcast_flood_bd)
249 self.vapi.sw_interface_set_l2_bridge(
250 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
251
252 # Add and delete mcast tunnels to check stability
253 self.add_shared_mcast_dst_load(self.dport)
254 self.add_mcast_tunnels_load(self.dport)
255 self.del_shared_mcast_dst_load(self.dport)
256 self.del_mcast_tunnels_load(self.dport)
257
258 # Setup vni 3 to test unicast flooding
259 self.ucast_flood_bd = 13
260 self.create_vxlan_gpe_flood_test_bd(self.ucast_flood_bd,
261 self.n_ucast_tunnels,
262 self.dport)
263 self.vapi.sw_interface_set_l2_bridge(
264 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
265
266 # Set scapy listen custom port for VxLAN
267 bind_layers(UDP, VXLAN, dport=self.dport)
268
269 """
270 Tests with default port (4790)
271 """
272 def test_decap(self):
273 """ Decapsulation test
274 from BridgeDoman
275 """
276 self.createVxLANInterfaces()
277 super(TestVxlanGpe, self).test_decap()
278
279 def test_encap(self):
280 """ Encapsulation test
281 from BridgeDoman
282 """
283 self.createVxLANInterfaces()
284 super(TestVxlanGpe, self).test_encap()
285
286 def test_ucast_flood(self):
287 """ Unicast flood test
288 from BridgeDoman
289 """
290 self.createVxLANInterfaces()
291 super(TestVxlanGpe, self).test_ucast_flood()
292
293 """
294 Tests with custom port (1112)
295 """
296 def test_decap_custom_port(self):
297 """ Decapsulation test custom port
298 from BridgeDoman
299 """
300 self.createVxLANInterfaces(1112)
301 super(TestVxlanGpe, self).test_decap()
302
303 def test_encap_custom_port(self):
304 """ Encapsulation test custom port
305 from BridgeDoman
306 """
307 self.createVxLANInterfaces(1112)
308 super(TestVxlanGpe, self).test_encap()
309
310 def test_ucast_flood_custom_port(self):
311 """ Unicast flood test custom port
312 from BridgeDoman
313 """
314 self.createVxLANInterfaces(1112)
315 super(TestVxlanGpe, self).test_ucast_flood()
316
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100317 @unittest.skip("test disabled for vxlan-gpe")
318 def test_mcast_flood(self):
319 """ inherited from BridgeDomain """
320 pass
321
322 @unittest.skip("test disabled for vxlan-gpe")
323 def test_mcast_rcv(self):
324 """ inherited from BridgeDomain """
325 pass
326
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800327 # Method to define VPP actions before tear down of the test case.
328 # Overrides tearDown method in VppTestCase class.
329 # @param self The object pointer.
330 def tearDown(self):
331 super(TestVxlanGpe, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700332
333 def show_commands_at_teardown(self):
334 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
335 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
336 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
337 self.logger.info(self.vapi.cli("show int"))
338 self.logger.info(self.vapi.cli("show vxlan-gpe"))
339 self.logger.info(self.vapi.cli("show trace"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800340
341
342if __name__ == '__main__':
343 unittest.main(testRunner=VppTestRunner)