blob: 791067c06339533fd5def4e01dc10af1c807f09d [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):
24 """ GTPU UDP ports Test Case """
25
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
42 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
43 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
44 UDP(sport=self.dport, dport=self.dport, chksum=0))
45
46 self.pg0.add_stream(pkt)
47 self.pg_start()
48
49 err = self.statistics.get_counter(
Florin Corase759bb52020-04-08 01:55:39 +000050 '/err/ip4-udp-lookup/No listener for dst port')[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020051
52 if enabled:
53 self.assertEqual(err, self.ip4_err)
54 else:
55 self.assertEqual(err, self.ip4_err + 1)
56
57 self.ip4_err = err
58
59 def _check_udp_port_ip6(self, enabled=True):
60
61 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
62 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
63 UDP(sport=self.dport, dport=self.dport, chksum=0))
64
65 self.pg0.add_stream(pkt)
66 self.pg_start()
67
68 err = self.statistics.get_counter(
Florin Corase759bb52020-04-08 01:55:39 +000069 '/err/ip6-udp-lookup/No listener for dst port')[0]
Jakub Grajciarebae4192019-05-23 13:01:41 +020070
71 if enabled:
72 self.assertEqual(err, self.ip6_err)
73 else:
74 self.assertEqual(err, self.ip6_err + 1)
75
76 self.ip6_err = err
77
78 def test_udp_port(self):
79 """ test UDP ports
80 Check if there are no udp listeners before gtpu is enabled
81 """
Jakub Grajciarebae4192019-05-23 13:01:41 +020082 # UDP ports should be disabled unless a tunnel is configured
83 self._check_udp_port_ip4(False)
84 self._check_udp_port_ip6(False)
85
Ole Troan55636cb2019-12-08 14:14:37 +010086 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
87 mcast_sw_if_index=0xFFFFFFFF,
88 decap_next_index=0xFFFFFFFF,
89 src_address=self.pg0.local_ip4,
90 dst_address=self.pg0.remote_ip4)
Jakub Grajciarebae4192019-05-23 13:01:41 +020091
92 # UDP port 2152 enabled for ip4
93 self._check_udp_port_ip4()
94
Ole Troan55636cb2019-12-08 14:14:37 +010095 r = self.vapi.gtpu_add_del_tunnel(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)
Jakub Grajciarebae4192019-05-23 13:01:41 +0200100
101 # UDP port 2152 enabled for ip6
102 self._check_udp_port_ip6()
103
Ole Troan55636cb2019-12-08 14:14:37 +0100104 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
105 mcast_sw_if_index=0xFFFFFFFF,
106 decap_next_index=0xFFFFFFFF,
107 src_address=self.pg0.local_ip4,
108 dst_address=self.pg0.remote_ip4)
Jakub Grajciarebae4192019-05-23 13:01:41 +0200109
Ole Troan55636cb2019-12-08 14:14:37 +0100110 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
111 mcast_sw_if_index=0xFFFFFFFF,
112 decap_next_index=0xFFFFFFFF,
113 src_address=self.pg0.local_ip6,
114 dst_address=self.pg0.remote_ip6)
Jakub Grajciarebae4192019-05-23 13:01:41 +0200115
116
Hongjun Nief486b12017-04-12 19:21:16 +0800117class TestGtpu(BridgeDomain, VppTestCase):
118 """ GTPU Test Case """
119
120 def __init__(self, *args):
121 BridgeDomain.__init__(self)
122 VppTestCase.__init__(self, *args)
123
124 def encapsulate(self, pkt, vni):
125 """
126 Encapsulate the original payload frame by adding GTPU header with its
127 UDP, IP and Ethernet fields
128 """
129 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
130 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
131 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Neale Ranns2bc94022018-02-25 12:27:18 -0800132 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
Hongjun Nief486b12017-04-12 19:21:16 +0800133 pkt)
134
Eyal Baricef1e2a2018-06-18 13:01:59 +0300135 def ip_range(self, start, end):
136 """ range of remote ip's """
137 return ip4_range(self.pg0.remote_ip4, start, end)
138
Hongjun Nief486b12017-04-12 19:21:16 +0800139 def encap_mcast(self, pkt, src_ip, src_mac, vni):
140 """
141 Encapsulate the original payload frame by adding GTPU header with its
142 UDP, IP and Ethernet fields
143 """
144 return (Ether(src=src_mac, dst=self.mcast_mac) /
145 IP(src=src_ip, dst=self.mcast_ip4) /
146 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Neale Ranns2bc94022018-02-25 12:27:18 -0800147 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
Hongjun Nief486b12017-04-12 19:21:16 +0800148 pkt)
149
150 def decapsulate(self, pkt):
151 """
152 Decapsulate the original payload frame by removing GTPU header
153 """
154 return pkt[GTP_U_Header].payload
155
156 # Method for checking GTPU encapsulation.
157 #
158 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
159 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
160 # by VPP using ARP.
161 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
162 if not local_only:
163 if not mcast_pkt:
164 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
165 else:
166 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
167 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
168 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
169 if not local_only:
170 if not mcast_pkt:
171 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
172 else:
173 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
174 # Verify UDP destination port is GTPU 2152, source UDP port could be
175 # arbitrary.
176 self.assertEqual(pkt[UDP].dport, type(self).dport)
Neale Ranns2bc94022018-02-25 12:27:18 -0800177 # Verify teid
178 self.assertEqual(pkt[GTP_U_Header].teid, vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800179
180 def test_encap(self):
181 """ Encapsulation test
182 Send frames from pg1
183 Verify receipt of encapsulated frames on pg0
184 """
185 self.pg1.add_stream([self.frame_reply])
186
187 self.pg0.enable_capture()
188
189 self.pg_start()
190
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700191 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800192 out = self.pg0.get_capture(1)
193 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000194 self.check_encapsulation(pkt, self.single_tunnel_vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800195
196 # payload = self.decapsulate(pkt)
197 # self.assert_eq_pkts(payload, self.frame_reply)
198
199 def test_ucast_flood(self):
200 """ Unicast flood test
201 Send frames from pg3
202 Verify receipt of encapsulated frames on pg0
203 """
204 self.pg3.add_stream([self.frame_reply])
205
206 self.pg0.enable_capture()
207
208 self.pg_start()
209
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700210 # Get packet from each tunnel and assert it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800211 out = self.pg0.get_capture(self.n_ucast_tunnels)
212 for pkt in out:
213 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
214 # payload = self.decapsulate(pkt)
215 # self.assert_eq_pkts(payload, self.frame_reply)
216
217 def test_mcast_flood(self):
218 """ Multicast flood test
219 Send frames from pg2
220 Verify receipt of encapsulated frames on pg0
221 """
222 self.pg2.add_stream([self.frame_reply])
223
224 self.pg0.enable_capture()
225
226 self.pg_start()
227
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700228 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800229 out = self.pg0.get_capture(1)
230 pkt = out[0]
231 self.check_encapsulation(pkt, self.mcast_flood_bd,
232 local_only=False, mcast_pkt=True)
233
234 # payload = self.decapsulate(pkt)
235 # self.assert_eq_pkts(payload, self.frame_reply)
236
237 @classmethod
238 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
239 # Create 10 ucast gtpu tunnels under bd
240 ip_range_start = 10
241 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -0700242 next_hop_address = cls.pg0.remote_ip4
243 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
244 ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400245 # add host route so dest_ip4 will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -0700246 rip = VppIpRoute(cls, dest_ip4, 32,
247 [VppRoutePath(next_hop_address,
248 INVALID_INDEX)],
249 register=False)
250 rip.add_vpp_config()
Hongjun Nief486b12017-04-12 19:21:16 +0800251 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100252 is_add=True,
253 mcast_sw_if_index=0xFFFFFFFF,
254 decap_next_index=0xFFFFFFFF,
255 src_address=cls.pg0.local_ip4,
256 dst_address=dest_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800257 teid=teid)
Ole Troana5b2eec2019-03-11 19:23:25 +0100258 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
259 bd_id=teid)
Hongjun Nief486b12017-04-12 19:21:16 +0800260
261 @classmethod
262 def add_del_shared_mcast_dst_load(cls, is_add):
263 """
264 add or del tunnels sharing the same mcast dst
265 to test gtpu ref_count mechanism
266 """
267 n_shared_dst_tunnels = 20
268 teid_start = 1000
269 teid_end = teid_start + n_shared_dst_tunnels
270 for teid in range(teid_start, teid_end):
271 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100272 decap_next_index=0xFFFFFFFF,
273 src_address=cls.pg0.local_ip4,
274 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800275 mcast_sw_if_index=1,
276 teid=teid,
277 is_add=is_add)
278 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700279 raise ValueError("bad sw_if_index: ~0")
Hongjun Nief486b12017-04-12 19:21:16 +0800280
281 @classmethod
282 def add_shared_mcast_dst_load(cls):
283 cls.add_del_shared_mcast_dst_load(is_add=1)
284
285 @classmethod
286 def del_shared_mcast_dst_load(cls):
287 cls.add_del_shared_mcast_dst_load(is_add=0)
288
289 @classmethod
290 def add_del_mcast_tunnels_load(cls, is_add):
291 """
292 add or del tunnels to test gtpu stability
293 """
294 n_distinct_dst_tunnels = 20
295 ip_range_start = 10
296 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Ole Troan55636cb2019-12-08 14:14:37 +0100297 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
298 ip_range_end):
299 teid = int(dest_ip4.split('.')[3])
Hongjun Nief486b12017-04-12 19:21:16 +0800300 cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100301 decap_next_index=0xFFFFFFFF,
302 src_address=cls.pg0.local_ip4,
303 dst_address=dest_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800304 mcast_sw_if_index=1,
305 teid=teid,
306 is_add=is_add)
307
308 @classmethod
309 def add_mcast_tunnels_load(cls):
310 cls.add_del_mcast_tunnels_load(is_add=1)
311
312 @classmethod
313 def del_mcast_tunnels_load(cls):
314 cls.add_del_mcast_tunnels_load(is_add=0)
315
316 # Class method to start the GTPU test case.
317 # Overrides setUpClass method in VppTestCase class.
318 # Python try..except statement is used to ensure that the tear down of
319 # the class will be executed even if exception is raised.
320 # @param cls The class pointer.
321 @classmethod
322 def setUpClass(cls):
323 super(TestGtpu, cls).setUpClass()
324
325 try:
326 cls.dport = 2152
327 cls.gtp_type = 0xff
328
329 # Create 2 pg interfaces.
330 cls.create_pg_interfaces(range(4))
331 for pg in cls.pg_interfaces:
332 pg.admin_up()
333
334 # Configure IPv4 addresses on VPP pg0.
335 cls.pg0.config_ip4()
336
337 # Resolve MAC address for VPP's IP address on pg0.
338 cls.pg0.resolve_arp()
339
340 # Our Multicast address
341 cls.mcast_ip4 = '239.1.1.1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400342 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Nief486b12017-04-12 19:21:16 +0800343
344 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
345 # into BD.
346 cls.single_tunnel_bd = 11
Neale Ranns91fd9102020-04-03 07:46:28 +0000347 cls.single_tunnel_vni = 11
Hongjun Nief486b12017-04-12 19:21:16 +0800348 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100349 is_add=True,
350 mcast_sw_if_index=0xFFFFFFFF,
351 decap_next_index=0xFFFFFFFF,
352 src_address=cls.pg0.local_ip4,
353 dst_address=cls.pg0.remote_ip4,
Neale Ranns91fd9102020-04-03 07:46:28 +0000354 teid=cls.single_tunnel_vni)
Ole Troana5b2eec2019-03-11 19:23:25 +0100355 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Nief486b12017-04-12 19:21:16 +0800356 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100357 cls.vapi.sw_interface_set_l2_bridge(
358 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800359
360 # Setup teid 2 to test multicast flooding
361 cls.n_ucast_tunnels = 10
362 cls.mcast_flood_bd = 12
363 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
364 cls.n_ucast_tunnels)
365 r = cls.vapi.gtpu_add_del_tunnel(
Ole Troan55636cb2019-12-08 14:14:37 +0100366 is_add=True,
367 src_address=cls.pg0.local_ip4,
368 dst_address=cls.mcast_ip4,
Hongjun Nief486b12017-04-12 19:21:16 +0800369 mcast_sw_if_index=1,
Ole Troan55636cb2019-12-08 14:14:37 +0100370 decap_next_index=0xFFFFFFFF,
Hongjun Nief486b12017-04-12 19:21:16 +0800371 teid=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100372 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Nief486b12017-04-12 19:21:16 +0800373 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100374 cls.vapi.sw_interface_set_l2_bridge(
375 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800376
377 # Add and delete mcast tunnels to check stability
378 cls.add_shared_mcast_dst_load()
379 cls.add_mcast_tunnels_load()
380 cls.del_shared_mcast_dst_load()
381 cls.del_mcast_tunnels_load()
382
383 # Setup teid 3 to test unicast flooding
384 cls.ucast_flood_bd = 13
385 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
386 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100387 cls.vapi.sw_interface_set_l2_bridge(
388 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800389 except Exception:
390 super(TestGtpu, cls).tearDownClass()
391 raise
392
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700393 @classmethod
394 def tearDownClass(cls):
395 super(TestGtpu, cls).tearDownClass()
396
Hongjun Nief486b12017-04-12 19:21:16 +0800397 # Method to define VPP actions before tear down of the test case.
398 # Overrides tearDown method in VppTestCase class.
399 # @param self The object pointer.
400 def tearDown(self):
401 super(TestGtpu, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700402
403 def show_commands_at_teardown(self):
404 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
405 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
406 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
407 self.logger.info(self.vapi.cli("show int"))
408 self.logger.info(self.vapi.cli("show gtpu tunnel"))
409 self.logger.info(self.vapi.cli("show trace"))
Hongjun Nief486b12017-04-12 19:21:16 +0800410
411
412if __name__ == '__main__':
413 unittest.main(testRunner=VppTestRunner)