blob: 5fe4f36ccb340d4ada459c5c863382fc500e5e53 [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
Paul Vinciguerra2f156312020-05-02 22:34:40 -04003from util import ip4_range
Hongjun Nief486b12017-04-12 19:21:16 +08004import unittest
Dave Wallace8800f732023-08-31 00:47:44 -04005from framework import VppTestCase
6from asfframework import VppTestRunner, tag_fixme_vpp_workers
Hongjun Nief486b12017-04-12 19:21:16 +08007from template_bd import BridgeDomain
8
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
Hongjun Nief486b12017-04-12 19:21:16 +080010from scapy.layers.inet import IP, UDP
Jakub Grajciarebae4192019-05-23 13:01:41 +020011from scapy.layers.inet6 import IPv6
Hongjun Nief486b12017-04-12 19:21:16 +080012from scapy.contrib.gtp import GTP_U_Header
Paul Vinciguerra2f156312020-05-02 22:34:40 -040013
14import util
Neale Ranns097fa662018-05-01 05:17:55 -070015from vpp_ip_route import VppIpRoute, VppRoutePath
16from vpp_ip import INVALID_INDEX
Hongjun Nief486b12017-04-12 19:21:16 +080017
18
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +000019@tag_fixme_vpp_workers
Jakub Grajciarebae4192019-05-23 13:01:41 +020020class TestGtpuUDP(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020021 """GTPU UDP ports Test Case"""
Jakub Grajciarebae4192019-05-23 13:01:41 +020022
23 def setUp(self):
Paul Vinciguerra20344a12019-06-06 18:01:07 -040024 super(TestGtpuUDP, self).setUp()
Jakub Grajciarebae4192019-05-23 13:01:41 +020025
26 self.dport = 2152
27
28 self.ip4_err = 0
29 self.ip6_err = 0
30
31 self.create_pg_interfaces(range(1))
32 for pg in self.pg_interfaces:
33 pg.admin_up()
34 self.pg0.config_ip4()
35 self.pg0.config_ip6()
36
37 def _check_udp_port_ip4(self, enabled=True):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 pkt = (
Steven Luonge4238aa2024-04-19 09:49:20 -070039 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
41 / UDP(sport=self.dport, dport=self.dport, chksum=0)
42 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020043
44 self.pg0.add_stream(pkt)
45 self.pg_start()
46
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 err = self.statistics.get_counter("/err/ip4-udp-lookup/no_listener")[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020048
49 if enabled:
50 self.assertEqual(err, self.ip4_err)
51 else:
52 self.assertEqual(err, self.ip4_err + 1)
53
54 self.ip4_err = err
55
56 def _check_udp_port_ip6(self, enabled=True):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 pkt = (
Steven Luonge4238aa2024-04-19 09:49:20 -070058 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
60 / UDP(sport=self.dport, dport=self.dport, chksum=0)
61 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020062
63 self.pg0.add_stream(pkt)
64 self.pg_start()
65
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 err = self.statistics.get_counter("/err/ip6-udp-lookup/no_listener")[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020067
68 if enabled:
69 self.assertEqual(err, self.ip6_err)
70 else:
71 self.assertEqual(err, self.ip6_err + 1)
72
73 self.ip6_err = err
74
75 def test_udp_port(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 """test UDP ports
Jakub Grajciarebae4192019-05-23 13:01:41 +020077 Check if there are no udp listeners before gtpu is enabled
78 """
Jakub Grajciarebae4192019-05-23 13:01:41 +020079 # UDP ports should be disabled unless a tunnel is configured
80 self._check_udp_port_ip4(False)
81 self._check_udp_port_ip6(False)
82
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020083 r = self.vapi.gtpu_add_del_tunnel(
84 is_add=True,
85 mcast_sw_if_index=0xFFFFFFFF,
86 decap_next_index=0xFFFFFFFF,
87 src_address=self.pg0.local_ip4,
88 dst_address=self.pg0.remote_ip4,
89 )
Jakub Grajciarebae4192019-05-23 13:01:41 +020090
91 # UDP port 2152 enabled for ip4
92 self._check_udp_port_ip4()
93
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020094 r = self.vapi.gtpu_add_del_tunnel(
95 is_add=True,
96 mcast_sw_if_index=0xFFFFFFFF,
97 decap_next_index=0xFFFFFFFF,
98 src_address=self.pg0.local_ip6,
99 dst_address=self.pg0.remote_ip6,
100 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200101
102 # UDP port 2152 enabled for ip6
103 self._check_udp_port_ip6()
104
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200105 r = self.vapi.gtpu_add_del_tunnel(
106 is_add=False,
107 mcast_sw_if_index=0xFFFFFFFF,
108 decap_next_index=0xFFFFFFFF,
109 src_address=self.pg0.local_ip4,
110 dst_address=self.pg0.remote_ip4,
111 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200112
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 r = self.vapi.gtpu_add_del_tunnel(
114 is_add=False,
115 mcast_sw_if_index=0xFFFFFFFF,
116 decap_next_index=0xFFFFFFFF,
117 src_address=self.pg0.local_ip6,
118 dst_address=self.pg0.remote_ip6,
119 )
Jakub Grajciarebae4192019-05-23 13:01:41 +0200120
121
Hongjun Nief486b12017-04-12 19:21:16 +0800122class TestGtpu(BridgeDomain, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200123 """GTPU Test Case"""
Hongjun Nief486b12017-04-12 19:21:16 +0800124
125 def __init__(self, *args):
126 BridgeDomain.__init__(self)
127 VppTestCase.__init__(self, *args)
128
129 def encapsulate(self, pkt, vni):
130 """
131 Encapsulate the original payload frame by adding GTPU header with its
132 UDP, IP and Ethernet fields
133 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200134 return (
135 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
136 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
137 / UDP(sport=self.dport, dport=self.dport, chksum=0)
138 / GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150)
139 / pkt
140 )
Hongjun Nief486b12017-04-12 19:21:16 +0800141
Eyal Baricef1e2a2018-06-18 13:01:59 +0300142 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200143 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +0300144 return ip4_range(self.pg0.remote_ip4, start, end)
145
Hongjun Nief486b12017-04-12 19:21:16 +0800146 def encap_mcast(self, pkt, src_ip, src_mac, vni):
147 """
148 Encapsulate the original payload frame by adding GTPU header with its
149 UDP, IP and Ethernet fields
150 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200151 return (
152 Ether(src=src_mac, dst=self.mcast_mac)
153 / IP(src=src_ip, dst=self.mcast_ip4)
154 / UDP(sport=self.dport, dport=self.dport, chksum=0)
155 / GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150)
156 / pkt
157 )
Hongjun Nief486b12017-04-12 19:21:16 +0800158
159 def decapsulate(self, pkt):
160 """
161 Decapsulate the original payload frame by removing GTPU header
162 """
163 return pkt[GTP_U_Header].payload
164
165 # Method for checking GTPU encapsulation.
166 #
167 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
168 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
169 # by VPP using ARP.
170 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
171 if not local_only:
172 if not mcast_pkt:
173 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
174 else:
175 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
176 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
177 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
178 if not local_only:
179 if not mcast_pkt:
180 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
181 else:
182 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
183 # Verify UDP destination port is GTPU 2152, source UDP port could be
184 # arbitrary.
185 self.assertEqual(pkt[UDP].dport, type(self).dport)
Neale Ranns2bc94022018-02-25 12:27:18 -0800186 # Verify teid
187 self.assertEqual(pkt[GTP_U_Header].teid, vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800188
189 def test_encap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200190 """Encapsulation test
Hongjun Nief486b12017-04-12 19:21:16 +0800191 Send frames from pg1
192 Verify receipt of encapsulated frames on pg0
193 """
194 self.pg1.add_stream([self.frame_reply])
195
196 self.pg0.enable_capture()
197
198 self.pg_start()
199
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700200 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800201 out = self.pg0.get_capture(1)
202 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000203 self.check_encapsulation(pkt, self.single_tunnel_vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800204
205 # payload = self.decapsulate(pkt)
206 # self.assert_eq_pkts(payload, self.frame_reply)
207
208 def test_ucast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200209 """Unicast flood test
Hongjun Nief486b12017-04-12 19:21:16 +0800210 Send frames from pg3
211 Verify receipt of encapsulated frames on pg0
212 """
213 self.pg3.add_stream([self.frame_reply])
214
215 self.pg0.enable_capture()
216
217 self.pg_start()
218
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700219 # Get packet from each tunnel and assert it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800220 out = self.pg0.get_capture(self.n_ucast_tunnels)
221 for pkt in out:
222 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
223 # payload = self.decapsulate(pkt)
224 # self.assert_eq_pkts(payload, self.frame_reply)
225
226 def test_mcast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200227 """Multicast flood test
Hongjun Nief486b12017-04-12 19:21:16 +0800228 Send frames from pg2
229 Verify receipt of encapsulated frames on pg0
230 """
231 self.pg2.add_stream([self.frame_reply])
232
233 self.pg0.enable_capture()
234
235 self.pg_start()
236
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700237 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800238 out = self.pg0.get_capture(1)
239 pkt = out[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200240 self.check_encapsulation(
241 pkt, self.mcast_flood_bd, local_only=False, mcast_pkt=True
242 )
Hongjun Nief486b12017-04-12 19:21:16 +0800243
244 # payload = self.decapsulate(pkt)
245 # self.assert_eq_pkts(payload, self.frame_reply)
246
247 @classmethod
248 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
249 # Create 10 ucast gtpu tunnels under bd
250 ip_range_start = 10
251 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -0700252 next_hop_address = cls.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200253 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400254 # add host route so dest_ip4 will not be resolved
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200255 rip = VppIpRoute(
256 cls,
257 dest_ip4,
258 32,
259 [VppRoutePath(next_hop_address, INVALID_INDEX)],
260 register=False,
261 )
Neale Ranns097fa662018-05-01 05:17:55 -0700262 rip.add_vpp_config()
Hongjun Nief486b12017-04-12 19:21:16 +0800263 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100264 is_add=True,
265 mcast_sw_if_index=0xFFFFFFFF,
266 decap_next_index=0xFFFFFFFF,
267 src_address=cls.pg0.local_ip4,
268 dst_address=dest_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 teid=teid,
270 )
271 cls.vapi.sw_interface_set_l2_bridge(
272 rx_sw_if_index=r.sw_if_index, bd_id=teid
273 )
Hongjun Nief486b12017-04-12 19:21:16 +0800274
275 @classmethod
276 def add_del_shared_mcast_dst_load(cls, is_add):
277 """
278 add or del tunnels sharing the same mcast dst
279 to test gtpu ref_count mechanism
280 """
281 n_shared_dst_tunnels = 20
282 teid_start = 1000
283 teid_end = teid_start + n_shared_dst_tunnels
284 for teid in range(teid_start, teid_end):
285 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100286 decap_next_index=0xFFFFFFFF,
287 src_address=cls.pg0.local_ip4,
288 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800289 mcast_sw_if_index=1,
290 teid=teid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200291 is_add=is_add,
292 )
293 if r.sw_if_index == 0xFFFFFFFF:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700294 raise ValueError("bad sw_if_index: ~0")
Hongjun Nief486b12017-04-12 19:21:16 +0800295
296 @classmethod
297 def add_shared_mcast_dst_load(cls):
298 cls.add_del_shared_mcast_dst_load(is_add=1)
299
300 @classmethod
301 def del_shared_mcast_dst_load(cls):
302 cls.add_del_shared_mcast_dst_load(is_add=0)
303
304 @classmethod
305 def add_del_mcast_tunnels_load(cls, is_add):
306 """
307 add or del tunnels to test gtpu stability
308 """
309 n_distinct_dst_tunnels = 20
310 ip_range_start = 10
311 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200312 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
313 teid = int(dest_ip4.split(".")[3])
Hongjun Nief486b12017-04-12 19:21:16 +0800314 cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100315 decap_next_index=0xFFFFFFFF,
316 src_address=cls.pg0.local_ip4,
317 dst_address=dest_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800318 mcast_sw_if_index=1,
319 teid=teid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200320 is_add=is_add,
321 )
Hongjun Nief486b12017-04-12 19:21:16 +0800322
323 @classmethod
324 def add_mcast_tunnels_load(cls):
325 cls.add_del_mcast_tunnels_load(is_add=1)
326
327 @classmethod
328 def del_mcast_tunnels_load(cls):
329 cls.add_del_mcast_tunnels_load(is_add=0)
330
331 # Class method to start the GTPU test case.
332 # Overrides setUpClass method in VppTestCase class.
333 # Python try..except statement is used to ensure that the tear down of
334 # the class will be executed even if exception is raised.
335 # @param cls The class pointer.
336 @classmethod
337 def setUpClass(cls):
338 super(TestGtpu, cls).setUpClass()
339
340 try:
341 cls.dport = 2152
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200342 cls.gtp_type = 0xFF
Hongjun Nief486b12017-04-12 19:21:16 +0800343
344 # Create 2 pg interfaces.
345 cls.create_pg_interfaces(range(4))
346 for pg in cls.pg_interfaces:
347 pg.admin_up()
348
349 # Configure IPv4 addresses on VPP pg0.
350 cls.pg0.config_ip4()
351
352 # Resolve MAC address for VPP's IP address on pg0.
353 cls.pg0.resolve_arp()
354
355 # Our Multicast address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200356 cls.mcast_ip4 = "239.1.1.1"
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400357 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Nief486b12017-04-12 19:21:16 +0800358
359 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
360 # into BD.
361 cls.single_tunnel_bd = 11
Neale Ranns91fd9102020-04-03 07:46:28 +0000362 cls.single_tunnel_vni = 11
Hongjun Nief486b12017-04-12 19:21:16 +0800363 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100364 is_add=True,
365 mcast_sw_if_index=0xFFFFFFFF,
366 decap_next_index=0xFFFFFFFF,
367 src_address=cls.pg0.local_ip4,
368 dst_address=cls.pg0.remote_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200369 teid=cls.single_tunnel_vni,
370 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100371 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200372 rx_sw_if_index=r.sw_if_index, bd_id=cls.single_tunnel_bd
373 )
374 cls.vapi.sw_interface_set_l2_bridge(
375 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd
376 )
Hongjun Nief486b12017-04-12 19:21:16 +0800377
378 # Setup teid 2 to test multicast flooding
379 cls.n_ucast_tunnels = 10
380 cls.mcast_flood_bd = 12
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200381 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd, cls.n_ucast_tunnels)
Hongjun Nief486b12017-04-12 19:21:16 +0800382 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100383 is_add=True,
384 src_address=cls.pg0.local_ip4,
385 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800386 mcast_sw_if_index=1,
Ole Troan55636cb2019-12-08 14:14:37 +0100387 decap_next_index=0xFFFFFFFF,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200388 teid=cls.mcast_flood_bd,
389 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100390 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200391 rx_sw_if_index=r.sw_if_index, bd_id=cls.mcast_flood_bd
392 )
393 cls.vapi.sw_interface_set_l2_bridge(
394 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd
395 )
Hongjun Nief486b12017-04-12 19:21:16 +0800396
397 # Add and delete mcast tunnels to check stability
398 cls.add_shared_mcast_dst_load()
399 cls.add_mcast_tunnels_load()
400 cls.del_shared_mcast_dst_load()
401 cls.del_mcast_tunnels_load()
402
403 # Setup teid 3 to test unicast flooding
404 cls.ucast_flood_bd = 13
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200405 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd, cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100406 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200407 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd
408 )
Hongjun Nief486b12017-04-12 19:21:16 +0800409 except Exception:
410 super(TestGtpu, cls).tearDownClass()
411 raise
412
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700413 @classmethod
414 def tearDownClass(cls):
415 super(TestGtpu, cls).tearDownClass()
416
Hongjun Nief486b12017-04-12 19:21:16 +0800417 # Method to define VPP actions before tear down of the test case.
418 # Overrides tearDown method in VppTestCase class.
419 # @param self The object pointer.
420 def tearDown(self):
421 super(TestGtpu, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700422
423 def show_commands_at_teardown(self):
424 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
425 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
426 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
427 self.logger.info(self.vapi.cli("show int"))
428 self.logger.info(self.vapi.cli("show gtpu tunnel"))
429 self.logger.info(self.vapi.cli("show trace"))
Hongjun Nief486b12017-04-12 19:21:16 +0800430
431
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200432if __name__ == "__main__":
Hongjun Nief486b12017-04-12 19:21:16 +0800433 unittest.main(testRunner=VppTestRunner)