blob: a6d54f53357f99b628786ecb41653dec5ee8b9e2 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Hongjun Nief486b12017-04-12 19:21:16 +08002
3import socket
Paul Vinciguerra2f156312020-05-02 22:34:40 -04004from util import ip4_range
Hongjun Nief486b12017-04-12 19:21:16 +08005import unittest
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00006from framework import tag_fixme_vpp_workers
Hongjun Nief486b12017-04-12 19:21:16 +08007from framework import VppTestCase, VppTestRunner
8from template_bd import BridgeDomain
9
snaramre5d4b8912019-12-13 23:39:35 +000010from scapy.layers.l2 import Ether
11from scapy.packet import Raw
Hongjun Nief486b12017-04-12 19:21:16 +080012from scapy.layers.inet import IP, UDP
Jakub Grajciarebae4192019-05-23 13:01:41 +020013from scapy.layers.inet6 import IPv6
Hongjun Nief486b12017-04-12 19:21:16 +080014from scapy.contrib.gtp import GTP_U_Header
15from scapy.utils import atol
Paul Vinciguerra2f156312020-05-02 22:34:40 -040016
17import util
Neale Ranns097fa662018-05-01 05:17:55 -070018from vpp_ip_route import VppIpRoute, VppRoutePath
19from vpp_ip import INVALID_INDEX
Hongjun Nief486b12017-04-12 19:21:16 +080020
21
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +000022@tag_fixme_vpp_workers
Jakub Grajciarebae4192019-05-23 13:01:41 +020023class TestGtpuUDP(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020024 """GTPU UDP ports Test Case"""
Jakub Grajciarebae4192019-05-23 13:01:41 +020025
26 def setUp(self):
Paul Vinciguerra20344a12019-06-06 18:01:07 -040027 super(TestGtpuUDP, self).setUp()
Jakub Grajciarebae4192019-05-23 13:01:41 +020028
29 self.dport = 2152
30
31 self.ip4_err = 0
32 self.ip6_err = 0
33
34 self.create_pg_interfaces(range(1))
35 for pg in self.pg_interfaces:
36 pg.admin_up()
37 self.pg0.config_ip4()
38 self.pg0.config_ip6()
39
40 def _check_udp_port_ip4(self, enabled=True):
41
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 pkt = (
43 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
44 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
45 / UDP(sport=self.dport, dport=self.dport, chksum=0)
46 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020047
48 self.pg0.add_stream(pkt)
49 self.pg_start()
50
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020051 err = self.statistics.get_counter("/err/ip4-udp-lookup/no_listener")[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020052
53 if enabled:
54 self.assertEqual(err, self.ip4_err)
55 else:
56 self.assertEqual(err, self.ip4_err + 1)
57
58 self.ip4_err = err
59
60 def _check_udp_port_ip6(self, enabled=True):
61
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020062 pkt = (
63 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
64 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
65 / UDP(sport=self.dport, dport=self.dport, chksum=0)
66 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020067
68 self.pg0.add_stream(pkt)
69 self.pg_start()
70
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 err = self.statistics.get_counter("/err/ip6-udp-lookup/no_listener")[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020072
73 if enabled:
74 self.assertEqual(err, self.ip6_err)
75 else:
76 self.assertEqual(err, self.ip6_err + 1)
77
78 self.ip6_err = err
79
80 def test_udp_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 """test UDP ports
Jakub Grajciarebae4192019-05-23 13:01:41 +020082 Check if there are no udp listeners before gtpu is enabled
83 """
Jakub Grajciarebae4192019-05-23 13:01:41 +020084 # UDP ports should be disabled unless a tunnel is configured
85 self._check_udp_port_ip4(False)
86 self._check_udp_port_ip6(False)
87
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 r = self.vapi.gtpu_add_del_tunnel(
89 is_add=True,
90 mcast_sw_if_index=0xFFFFFFFF,
91 decap_next_index=0xFFFFFFFF,
92 src_address=self.pg0.local_ip4,
93 dst_address=self.pg0.remote_ip4,
94 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020095
96 # UDP port 2152 enabled for ip4
97 self._check_udp_port_ip4()
98
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 r = self.vapi.gtpu_add_del_tunnel(
100 is_add=True,
101 mcast_sw_if_index=0xFFFFFFFF,
102 decap_next_index=0xFFFFFFFF,
103 src_address=self.pg0.local_ip6,
104 dst_address=self.pg0.remote_ip6,
105 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200106
107 # UDP port 2152 enabled for ip6
108 self._check_udp_port_ip6()
109
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 r = self.vapi.gtpu_add_del_tunnel(
111 is_add=False,
112 mcast_sw_if_index=0xFFFFFFFF,
113 decap_next_index=0xFFFFFFFF,
114 src_address=self.pg0.local_ip4,
115 dst_address=self.pg0.remote_ip4,
116 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200117
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200118 r = self.vapi.gtpu_add_del_tunnel(
119 is_add=False,
120 mcast_sw_if_index=0xFFFFFFFF,
121 decap_next_index=0xFFFFFFFF,
122 src_address=self.pg0.local_ip6,
123 dst_address=self.pg0.remote_ip6,
124 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200125
126
Hongjun Nief486b12017-04-12 19:21:16 +0800127class TestGtpu(BridgeDomain, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128 """GTPU Test Case"""
Hongjun Nief486b12017-04-12 19:21:16 +0800129
130 def __init__(self, *args):
131 BridgeDomain.__init__(self)
132 VppTestCase.__init__(self, *args)
133
134 def encapsulate(self, pkt, vni):
135 """
136 Encapsulate the original payload frame by adding GTPU header with its
137 UDP, IP and Ethernet fields
138 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200139 return (
140 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
141 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
142 / UDP(sport=self.dport, dport=self.dport, chksum=0)
143 / GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150)
144 / pkt
145 )
Hongjun Nief486b12017-04-12 19:21:16 +0800146
Eyal Baricef1e2a2018-06-18 13:01:59 +0300147 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +0300149 return ip4_range(self.pg0.remote_ip4, start, end)
150
Hongjun Nief486b12017-04-12 19:21:16 +0800151 def encap_mcast(self, pkt, src_ip, src_mac, vni):
152 """
153 Encapsulate the original payload frame by adding GTPU header with its
154 UDP, IP and Ethernet fields
155 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200156 return (
157 Ether(src=src_mac, dst=self.mcast_mac)
158 / IP(src=src_ip, dst=self.mcast_ip4)
159 / UDP(sport=self.dport, dport=self.dport, chksum=0)
160 / GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150)
161 / pkt
162 )
Hongjun Nief486b12017-04-12 19:21:16 +0800163
164 def decapsulate(self, pkt):
165 """
166 Decapsulate the original payload frame by removing GTPU header
167 """
168 return pkt[GTP_U_Header].payload
169
170 # Method for checking GTPU encapsulation.
171 #
172 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
173 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
174 # by VPP using ARP.
175 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
176 if not local_only:
177 if not mcast_pkt:
178 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
179 else:
180 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
181 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
182 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
183 if not local_only:
184 if not mcast_pkt:
185 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
186 else:
187 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
188 # Verify UDP destination port is GTPU 2152, source UDP port could be
189 # arbitrary.
190 self.assertEqual(pkt[UDP].dport, type(self).dport)
Neale Ranns2bc94022018-02-25 12:27:18 -0800191 # Verify teid
192 self.assertEqual(pkt[GTP_U_Header].teid, vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800193
194 def test_encap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 """Encapsulation test
Hongjun Nief486b12017-04-12 19:21:16 +0800196 Send frames from pg1
197 Verify receipt of encapsulated frames on pg0
198 """
199 self.pg1.add_stream([self.frame_reply])
200
201 self.pg0.enable_capture()
202
203 self.pg_start()
204
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700205 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800206 out = self.pg0.get_capture(1)
207 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000208 self.check_encapsulation(pkt, self.single_tunnel_vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800209
210 # payload = self.decapsulate(pkt)
211 # self.assert_eq_pkts(payload, self.frame_reply)
212
213 def test_ucast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200214 """Unicast flood test
Hongjun Nief486b12017-04-12 19:21:16 +0800215 Send frames from pg3
216 Verify receipt of encapsulated frames on pg0
217 """
218 self.pg3.add_stream([self.frame_reply])
219
220 self.pg0.enable_capture()
221
222 self.pg_start()
223
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700224 # Get packet from each tunnel and assert it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800225 out = self.pg0.get_capture(self.n_ucast_tunnels)
226 for pkt in out:
227 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
228 # payload = self.decapsulate(pkt)
229 # self.assert_eq_pkts(payload, self.frame_reply)
230
231 def test_mcast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200232 """Multicast flood test
Hongjun Nief486b12017-04-12 19:21:16 +0800233 Send frames from pg2
234 Verify receipt of encapsulated frames on pg0
235 """
236 self.pg2.add_stream([self.frame_reply])
237
238 self.pg0.enable_capture()
239
240 self.pg_start()
241
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700242 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800243 out = self.pg0.get_capture(1)
244 pkt = out[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200245 self.check_encapsulation(
246 pkt, self.mcast_flood_bd, local_only=False, mcast_pkt=True
247 )
Hongjun Nief486b12017-04-12 19:21:16 +0800248
249 # payload = self.decapsulate(pkt)
250 # self.assert_eq_pkts(payload, self.frame_reply)
251
252 @classmethod
253 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
254 # Create 10 ucast gtpu tunnels under bd
255 ip_range_start = 10
256 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -0700257 next_hop_address = cls.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200258 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400259 # add host route so dest_ip4 will not be resolved
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200260 rip = VppIpRoute(
261 cls,
262 dest_ip4,
263 32,
264 [VppRoutePath(next_hop_address, INVALID_INDEX)],
265 register=False,
266 )
Neale Ranns097fa662018-05-01 05:17:55 -0700267 rip.add_vpp_config()
Hongjun Nief486b12017-04-12 19:21:16 +0800268 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100269 is_add=True,
270 mcast_sw_if_index=0xFFFFFFFF,
271 decap_next_index=0xFFFFFFFF,
272 src_address=cls.pg0.local_ip4,
273 dst_address=dest_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200274 teid=teid,
275 )
276 cls.vapi.sw_interface_set_l2_bridge(
277 rx_sw_if_index=r.sw_if_index, bd_id=teid
278 )
Hongjun Nief486b12017-04-12 19:21:16 +0800279
280 @classmethod
281 def add_del_shared_mcast_dst_load(cls, is_add):
282 """
283 add or del tunnels sharing the same mcast dst
284 to test gtpu ref_count mechanism
285 """
286 n_shared_dst_tunnels = 20
287 teid_start = 1000
288 teid_end = teid_start + n_shared_dst_tunnels
289 for teid in range(teid_start, teid_end):
290 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100291 decap_next_index=0xFFFFFFFF,
292 src_address=cls.pg0.local_ip4,
293 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800294 mcast_sw_if_index=1,
295 teid=teid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200296 is_add=is_add,
297 )
298 if r.sw_if_index == 0xFFFFFFFF:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700299 raise ValueError("bad sw_if_index: ~0")
Hongjun Nief486b12017-04-12 19:21:16 +0800300
301 @classmethod
302 def add_shared_mcast_dst_load(cls):
303 cls.add_del_shared_mcast_dst_load(is_add=1)
304
305 @classmethod
306 def del_shared_mcast_dst_load(cls):
307 cls.add_del_shared_mcast_dst_load(is_add=0)
308
309 @classmethod
310 def add_del_mcast_tunnels_load(cls, is_add):
311 """
312 add or del tunnels to test gtpu stability
313 """
314 n_distinct_dst_tunnels = 20
315 ip_range_start = 10
316 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200317 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
318 teid = int(dest_ip4.split(".")[3])
Hongjun Nief486b12017-04-12 19:21:16 +0800319 cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100320 decap_next_index=0xFFFFFFFF,
321 src_address=cls.pg0.local_ip4,
322 dst_address=dest_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800323 mcast_sw_if_index=1,
324 teid=teid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200325 is_add=is_add,
326 )
Hongjun Nief486b12017-04-12 19:21:16 +0800327
328 @classmethod
329 def add_mcast_tunnels_load(cls):
330 cls.add_del_mcast_tunnels_load(is_add=1)
331
332 @classmethod
333 def del_mcast_tunnels_load(cls):
334 cls.add_del_mcast_tunnels_load(is_add=0)
335
336 # Class method to start the GTPU test case.
337 # Overrides setUpClass method in VppTestCase class.
338 # Python try..except statement is used to ensure that the tear down of
339 # the class will be executed even if exception is raised.
340 # @param cls The class pointer.
341 @classmethod
342 def setUpClass(cls):
343 super(TestGtpu, cls).setUpClass()
344
345 try:
346 cls.dport = 2152
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200347 cls.gtp_type = 0xFF
Hongjun Nief486b12017-04-12 19:21:16 +0800348
349 # Create 2 pg interfaces.
350 cls.create_pg_interfaces(range(4))
351 for pg in cls.pg_interfaces:
352 pg.admin_up()
353
354 # Configure IPv4 addresses on VPP pg0.
355 cls.pg0.config_ip4()
356
357 # Resolve MAC address for VPP's IP address on pg0.
358 cls.pg0.resolve_arp()
359
360 # Our Multicast address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200361 cls.mcast_ip4 = "239.1.1.1"
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400362 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Nief486b12017-04-12 19:21:16 +0800363
364 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
365 # into BD.
366 cls.single_tunnel_bd = 11
Neale Ranns91fd9102020-04-03 07:46:28 +0000367 cls.single_tunnel_vni = 11
Hongjun Nief486b12017-04-12 19:21:16 +0800368 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100369 is_add=True,
370 mcast_sw_if_index=0xFFFFFFFF,
371 decap_next_index=0xFFFFFFFF,
372 src_address=cls.pg0.local_ip4,
373 dst_address=cls.pg0.remote_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200374 teid=cls.single_tunnel_vni,
375 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100376 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200377 rx_sw_if_index=r.sw_if_index, bd_id=cls.single_tunnel_bd
378 )
379 cls.vapi.sw_interface_set_l2_bridge(
380 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd
381 )
Hongjun Nief486b12017-04-12 19:21:16 +0800382
383 # Setup teid 2 to test multicast flooding
384 cls.n_ucast_tunnels = 10
385 cls.mcast_flood_bd = 12
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200386 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd, cls.n_ucast_tunnels)
Hongjun Nief486b12017-04-12 19:21:16 +0800387 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100388 is_add=True,
389 src_address=cls.pg0.local_ip4,
390 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800391 mcast_sw_if_index=1,
Ole Troan55636cb2019-12-08 14:14:37 +0100392 decap_next_index=0xFFFFFFFF,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200393 teid=cls.mcast_flood_bd,
394 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100395 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200396 rx_sw_if_index=r.sw_if_index, bd_id=cls.mcast_flood_bd
397 )
398 cls.vapi.sw_interface_set_l2_bridge(
399 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd
400 )
Hongjun Nief486b12017-04-12 19:21:16 +0800401
402 # Add and delete mcast tunnels to check stability
403 cls.add_shared_mcast_dst_load()
404 cls.add_mcast_tunnels_load()
405 cls.del_shared_mcast_dst_load()
406 cls.del_mcast_tunnels_load()
407
408 # Setup teid 3 to test unicast flooding
409 cls.ucast_flood_bd = 13
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200410 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd, cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100411 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200412 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd
413 )
Hongjun Nief486b12017-04-12 19:21:16 +0800414 except Exception:
415 super(TestGtpu, cls).tearDownClass()
416 raise
417
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700418 @classmethod
419 def tearDownClass(cls):
420 super(TestGtpu, cls).tearDownClass()
421
Hongjun Nief486b12017-04-12 19:21:16 +0800422 # Method to define VPP actions before tear down of the test case.
423 # Overrides tearDown method in VppTestCase class.
424 # @param self The object pointer.
425 def tearDown(self):
426 super(TestGtpu, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700427
428 def show_commands_at_teardown(self):
429 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
430 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
431 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
432 self.logger.info(self.vapi.cli("show int"))
433 self.logger.info(self.vapi.cli("show gtpu tunnel"))
434 self.logger.info(self.vapi.cli("show trace"))
Hongjun Nief486b12017-04-12 19:21:16 +0800435
436
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200437if __name__ == "__main__":
Hongjun Nief486b12017-04-12 19:21:16 +0800438 unittest.main(testRunner=VppTestRunner)