blob: 28c31ccd3bb131de831a5bf71dede458c6540b9a [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
6from framework import VppTestCase, VppTestRunner, running_extended_tests
7from template_bd import BridgeDomain
8
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
Artem Glazychevea962922021-05-28 19:09:14 +070010from scapy.packet import Raw, bind_layers
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080011from scapy.layers.inet import IP, UDP
12from 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
Artem Glazychevea962922021-05-28 19:09:14 +070016from vpp_vxlan_gpe_tunnel import VppVxlanGpeTunnel
Neale Ranns097fa662018-05-01 05:17:55 -070017from vpp_ip import INVALID_INDEX
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080018
19
Paul Vinciguerradefde0f2018-12-06 07:46:13 -080020@unittest.skipUnless(running_extended_tests, "part of extended tests")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080021class TestVxlanGpe(BridgeDomain, VppTestCase):
22 """ VXLAN-GPE Test Case """
23
24 def __init__(self, *args):
25 BridgeDomain.__init__(self)
26 VppTestCase.__init__(self, *args)
27
28 def encapsulate(self, pkt, vni):
29 """
30 Encapsulate the original payload frame by adding VXLAN-GPE header
31 with its UDP, IP and Ethernet fields
32 """
33 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
34 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
35 UDP(sport=self.dport, dport=self.dport, chksum=0) /
36 VXLAN(vni=vni, flags=self.flags) /
37 pkt)
38
Eyal Baricef1e2a2018-06-18 13:01:59 +030039 def ip_range(self, start, end):
40 """ range of remote ip's """
41 return ip4_range(self.pg0.remote_ip4, start, end)
42
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080043 def encap_mcast(self, pkt, src_ip, src_mac, vni):
44 """
45 Encapsulate the original payload frame by adding VXLAN-GPE header
46 with its UDP, IP and Ethernet fields
47 """
48 return (Ether(src=src_mac, dst=self.mcast_mac) /
49 IP(src=src_ip, dst=self.mcast_ip4) /
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-GPE header
57 """
58 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010059 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080060 return pkt[VXLAN].payload
61
62 # Method for checking VXLAN-GPE encapsulation.
63 #
64 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
65 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
66 # by VPP using ARP.
67 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
68 if not local_only:
69 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)
73 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
74 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
75 if not local_only:
76 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)
80 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
81 # could be arbitrary.
Artem Glazychevea962922021-05-28 19:09:14 +070082 self.assertEqual(pkt[UDP].dport, self.dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030083 # Verify UDP checksum
84 self.assert_udp_checksum_valid(pkt)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080085 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010086 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080087
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080088 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +070089 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels, port):
Neale Ranns097fa662018-05-01 05:17:55 -070090 # Create 10 ucast vxlan tunnels under bd
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080091 ip_range_start = 10
92 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):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080096 # add host route so dest_ip4n 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
Artem Glazychevea962922021-05-28 19:09:14 +0700103 r = VppVxlanGpeTunnel(cls,
104 src_addr=cls.pg0.local_ip4,
105 dst_addr=dest_ip4,
106 src_port=port,
107 dst_port=port,
108 vni=vni)
109 r.add_vpp_config()
Ole Troana5b2eec2019-03-11 19:23:25 +0100110 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
111 bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800112
113 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700114 def add_del_shared_mcast_dst_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800115 """
116 add or del tunnels sharing the same mcast dst
117 to test vxlan_gpe ref_count mechanism
118 """
119 n_shared_dst_tunnels = 20
120 vni_start = 1000
121 vni_end = vni_start + n_shared_dst_tunnels
122 for vni in range(vni_start, vni_end):
Artem Glazychevea962922021-05-28 19:09:14 +0700123 r = VppVxlanGpeTunnel(cls,
124 src_addr=cls.pg0.local_ip4,
125 dst_addr=cls.mcast_ip4,
126 src_port=port,
127 dst_port=port,
128 mcast_sw_if_index=1,
129 vni=vni)
130 if is_add:
131 r.add_vpp_config()
132 if r.sw_if_index == 0xffffffff:
133 raise ValueError("bad sw_if_index: ~0")
134 else:
135 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800136
137 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700138 def add_shared_mcast_dst_load(cls, port):
139 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800140
141 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700142 def del_shared_mcast_dst_load(cls, port):
143 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800144
145 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700146 def add_del_mcast_tunnels_load(cls, port, is_add):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800147 """
148 add or del tunnels to test vxlan_gpe stability
149 """
150 n_distinct_dst_tunnels = 20
151 ip_range_start = 10
152 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400153 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
154 ip_range_end):
155 vni = int(dest_ip4.split(".")[3])
Artem Glazychevea962922021-05-28 19:09:14 +0700156 r = VppVxlanGpeTunnel(cls,
157 src_addr=cls.pg0.local_ip4,
158 dst_addr=dest_ip4,
159 src_port=port,
160 dst_port=port,
161 mcast_sw_if_index=1,
162 vni=vni)
163 if is_add:
164 r.add_vpp_config()
165 else:
166 r.remove_vpp_config()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800167
168 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700169 def add_mcast_tunnels_load(cls, port):
170 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800171
172 @classmethod
Artem Glazychevea962922021-05-28 19:09:14 +0700173 def del_mcast_tunnels_load(cls, port):
174 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800175
176 # Class method to start the VXLAN-GPE test case.
177 # Overrides setUpClass method in VppTestCase class.
178 # Python try..except statement is used to ensure that the tear down of
179 # the class will be executed even if exception is raised.
180 # @param cls The class pointer.
181 @classmethod
182 def setUpClass(cls):
183 super(TestVxlanGpe, cls).setUpClass()
184
185 try:
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100186 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800187
188 # Create 2 pg interfaces.
189 cls.create_pg_interfaces(range(4))
190 for pg in cls.pg_interfaces:
191 pg.admin_up()
192
193 # Configure IPv4 addresses on VPP pg0.
194 cls.pg0.config_ip4()
195
196 # Resolve MAC address for VPP's IP address on pg0.
197 cls.pg0.resolve_arp()
198
199 # Our Multicast address
200 cls.mcast_ip4 = '239.1.1.1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400201 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800202 except Exception:
Artem Glazychevea962922021-05-28 19:09:14 +0700203 cls.tearDownClass()
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800204 raise
205
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800206 @classmethod
207 def tearDownClass(cls):
208 super(TestVxlanGpe, cls).tearDownClass()
209
Artem Glazychevea962922021-05-28 19:09:14 +0700210 def setUp(self):
211 super(TestVxlanGpe, self).setUp()
212
213 def createVxLANInterfaces(self, port=4790):
214 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
215 # and pg1 into BD.
216 self.dport = port
217
218 self.single_tunnel_vni = 0xabcde
219 self.single_tunnel_bd = 11
220 r = VppVxlanGpeTunnel(self,
221 src_addr=self.pg0.local_ip4,
222 dst_addr=self.pg0.remote_ip4,
223 src_port=port,
224 dst_port=port,
225 vni=self.single_tunnel_vni)
226 r.add_vpp_config()
227 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
228 bd_id=self.single_tunnel_bd)
229 self.vapi.sw_interface_set_l2_bridge(
230 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
231
232 # Setup vni 2 to test multicast flooding
233 self.n_ucast_tunnels = 10
234 self.mcast_flood_bd = 12
235 self.create_vxlan_gpe_flood_test_bd(self.mcast_flood_bd,
236 self.n_ucast_tunnels,
237 self.dport)
238 r = VppVxlanGpeTunnel(self,
239 src_addr=self.pg0.local_ip4,
240 dst_addr=self.mcast_ip4,
241 src_port=port,
242 dst_port=port,
243 mcast_sw_if_index=1,
244 vni=self.mcast_flood_bd)
245 r.add_vpp_config()
246 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
247 bd_id=self.mcast_flood_bd)
248 self.vapi.sw_interface_set_l2_bridge(
249 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
250
251 # Add and delete mcast tunnels to check stability
252 self.add_shared_mcast_dst_load(self.dport)
253 self.add_mcast_tunnels_load(self.dport)
254 self.del_shared_mcast_dst_load(self.dport)
255 self.del_mcast_tunnels_load(self.dport)
256
257 # Setup vni 3 to test unicast flooding
258 self.ucast_flood_bd = 13
259 self.create_vxlan_gpe_flood_test_bd(self.ucast_flood_bd,
260 self.n_ucast_tunnels,
261 self.dport)
262 self.vapi.sw_interface_set_l2_bridge(
263 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
264
265 # Set scapy listen custom port for VxLAN
266 bind_layers(UDP, VXLAN, dport=self.dport)
267
268 """
269 Tests with default port (4790)
270 """
271 def test_decap(self):
272 """ Decapsulation test
273 from BridgeDoman
274 """
275 self.createVxLANInterfaces()
276 super(TestVxlanGpe, self).test_decap()
277
278 def test_encap(self):
279 """ Encapsulation test
280 from BridgeDoman
281 """
282 self.createVxLANInterfaces()
283 super(TestVxlanGpe, self).test_encap()
284
285 def test_ucast_flood(self):
286 """ Unicast flood test
287 from BridgeDoman
288 """
289 self.createVxLANInterfaces()
290 super(TestVxlanGpe, self).test_ucast_flood()
291
292 """
293 Tests with custom port (1112)
294 """
295 def test_decap_custom_port(self):
296 """ Decapsulation test custom port
297 from BridgeDoman
298 """
299 self.createVxLANInterfaces(1112)
300 super(TestVxlanGpe, self).test_decap()
301
302 def test_encap_custom_port(self):
303 """ Encapsulation test custom port
304 from BridgeDoman
305 """
306 self.createVxLANInterfaces(1112)
307 super(TestVxlanGpe, self).test_encap()
308
309 def test_ucast_flood_custom_port(self):
310 """ Unicast flood test custom port
311 from BridgeDoman
312 """
313 self.createVxLANInterfaces(1112)
314 super(TestVxlanGpe, self).test_ucast_flood()
315
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100316 @unittest.skip("test disabled for vxlan-gpe")
317 def test_mcast_flood(self):
318 """ inherited from BridgeDomain """
319 pass
320
321 @unittest.skip("test disabled for vxlan-gpe")
322 def test_mcast_rcv(self):
323 """ inherited from BridgeDomain """
324 pass
325
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800326 # Method to define VPP actions before tear down of the test case.
327 # Overrides tearDown method in VppTestCase class.
328 # @param self The object pointer.
329 def tearDown(self):
330 super(TestVxlanGpe, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700331
332 def show_commands_at_teardown(self):
333 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
334 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
335 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
336 self.logger.info(self.vapi.cli("show int"))
337 self.logger.info(self.vapi.cli("show vxlan-gpe"))
338 self.logger.info(self.vapi.cli("show trace"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800339
340
341if __name__ == '__main__':
342 unittest.main(testRunner=VppTestRunner)