blob: 9f3c090726e50550b5000815258d27ede3108e57 [file] [log] [blame]
Hongjun Nief486b12017-04-12 19:21:16 +08001#!/usr/bin/env python
2
3import socket
Eyal Baricef1e2a2018-06-18 13:01:59 +03004from util import ip4n_range, ip4_range
Hongjun Nief486b12017-04-12 19:21:16 +08005import unittest
6from framework import VppTestCase, VppTestRunner
7from template_bd import BridgeDomain
8
9from scapy.layers.l2 import Ether, Raw
10from 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
13from scapy.utils import atol
14
15
Jakub Grajciarebae4192019-05-23 13:01:41 +020016class TestGtpuUDP(VppTestCase):
17 """ GTPU UDP ports Test Case """
18
19 def setUp(self):
20 super(TestGtpuUDP, self).tearDown()
21
22 self.dport = 2152
23
24 self.ip4_err = 0
25 self.ip6_err = 0
26
27 self.create_pg_interfaces(range(1))
28 for pg in self.pg_interfaces:
29 pg.admin_up()
30 self.pg0.config_ip4()
31 self.pg0.config_ip6()
32
33 def _check_udp_port_ip4(self, enabled=True):
34
35 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
36 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
37 UDP(sport=self.dport, dport=self.dport, chksum=0))
38
39 self.pg0.add_stream(pkt)
40 self.pg_start()
41
42 err = self.statistics.get_counter(
43 '/err/ip4-udp-lookup/no listener for dst port')[0]
44
45 if enabled:
46 self.assertEqual(err, self.ip4_err)
47 else:
48 self.assertEqual(err, self.ip4_err + 1)
49
50 self.ip4_err = err
51
52 def _check_udp_port_ip6(self, enabled=True):
53
54 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
55 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
56 UDP(sport=self.dport, dport=self.dport, chksum=0))
57
58 self.pg0.add_stream(pkt)
59 self.pg_start()
60
61 err = self.statistics.get_counter(
62 '/err/ip6-udp-lookup/no listener for dst port')[0]
63
64 if enabled:
65 self.assertEqual(err, self.ip6_err)
66 else:
67 self.assertEqual(err, self.ip6_err + 1)
68
69 self.ip6_err = err
70
71 def test_udp_port(self):
72 """ test UDP ports
73 Check if there are no udp listeners before gtpu is enabled
74 """
75
76 # UDP ports should be disabled unless a tunnel is configured
77 self._check_udp_port_ip4(False)
78 self._check_udp_port_ip6(False)
79
80 r = self.vapi.gtpu_add_del_tunnel(src_addr=self.pg0.local_ip4n,
81 dst_addr=self.pg0.remote_ip4n)
82
83 # UDP port 2152 enabled for ip4
84 self._check_udp_port_ip4()
85
86 r = self.vapi.gtpu_add_del_tunnel(is_ipv6=1,
87 src_addr=self.pg0.local_ip6n,
88 dst_addr=self.pg0.remote_ip6n)
89
90 # UDP port 2152 enabled for ip6
91 self._check_udp_port_ip6()
92
93 r = self.vapi.gtpu_add_del_tunnel(is_add=0,
94 src_addr=self.pg0.local_ip4n,
95 dst_addr=self.pg0.remote_ip4n)
96
97 r = self.vapi.gtpu_add_del_tunnel(is_add=0, is_ipv6=1,
98 src_addr=self.pg0.local_ip6n,
99 dst_addr=self.pg0.remote_ip6n)
100
101
Hongjun Nief486b12017-04-12 19:21:16 +0800102class TestGtpu(BridgeDomain, VppTestCase):
103 """ GTPU Test Case """
104
105 def __init__(self, *args):
106 BridgeDomain.__init__(self)
107 VppTestCase.__init__(self, *args)
108
109 def encapsulate(self, pkt, vni):
110 """
111 Encapsulate the original payload frame by adding GTPU header with its
112 UDP, IP and Ethernet fields
113 """
114 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
115 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
116 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Neale Ranns2bc94022018-02-25 12:27:18 -0800117 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
Hongjun Nief486b12017-04-12 19:21:16 +0800118 pkt)
119
Eyal Baricef1e2a2018-06-18 13:01:59 +0300120 def ip_range(self, start, end):
121 """ range of remote ip's """
122 return ip4_range(self.pg0.remote_ip4, start, end)
123
Hongjun Nief486b12017-04-12 19:21:16 +0800124 def encap_mcast(self, pkt, src_ip, src_mac, 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=src_mac, dst=self.mcast_mac) /
130 IP(src=src_ip, dst=self.mcast_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
135 def decapsulate(self, pkt):
136 """
137 Decapsulate the original payload frame by removing GTPU header
138 """
139 return pkt[GTP_U_Header].payload
140
141 # Method for checking GTPU encapsulation.
142 #
143 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
144 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
145 # by VPP using ARP.
146 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
147 if not local_only:
148 if not mcast_pkt:
149 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
150 else:
151 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
152 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
153 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
154 if not local_only:
155 if not mcast_pkt:
156 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
157 else:
158 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
159 # Verify UDP destination port is GTPU 2152, source UDP port could be
160 # arbitrary.
161 self.assertEqual(pkt[UDP].dport, type(self).dport)
Neale Ranns2bc94022018-02-25 12:27:18 -0800162 # Verify teid
163 self.assertEqual(pkt[GTP_U_Header].teid, vni)
Hongjun Nief486b12017-04-12 19:21:16 +0800164
165 def test_encap(self):
166 """ Encapsulation test
167 Send frames from pg1
168 Verify receipt of encapsulated frames on pg0
169 """
170 self.pg1.add_stream([self.frame_reply])
171
172 self.pg0.enable_capture()
173
174 self.pg_start()
175
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700176 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800177 out = self.pg0.get_capture(1)
178 pkt = out[0]
179 self.check_encapsulation(pkt, self.single_tunnel_bd)
180
181 # payload = self.decapsulate(pkt)
182 # self.assert_eq_pkts(payload, self.frame_reply)
183
184 def test_ucast_flood(self):
185 """ Unicast flood test
186 Send frames from pg3
187 Verify receipt of encapsulated frames on pg0
188 """
189 self.pg3.add_stream([self.frame_reply])
190
191 self.pg0.enable_capture()
192
193 self.pg_start()
194
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700195 # Get packet from each tunnel and assert it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800196 out = self.pg0.get_capture(self.n_ucast_tunnels)
197 for pkt in out:
198 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
199 # payload = self.decapsulate(pkt)
200 # self.assert_eq_pkts(payload, self.frame_reply)
201
202 def test_mcast_flood(self):
203 """ Multicast flood test
204 Send frames from pg2
205 Verify receipt of encapsulated frames on pg0
206 """
207 self.pg2.add_stream([self.frame_reply])
208
209 self.pg0.enable_capture()
210
211 self.pg_start()
212
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700213 # Pick first received frame and check if it's correctly encapsulated.
Hongjun Nief486b12017-04-12 19:21:16 +0800214 out = self.pg0.get_capture(1)
215 pkt = out[0]
216 self.check_encapsulation(pkt, self.mcast_flood_bd,
217 local_only=False, mcast_pkt=True)
218
219 # payload = self.decapsulate(pkt)
220 # self.assert_eq_pkts(payload, self.frame_reply)
221
222 @classmethod
223 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
224 # Create 10 ucast gtpu tunnels under bd
225 ip_range_start = 10
226 ip_range_end = ip_range_start + n_ucast_tunnels
227 next_hop_address = cls.pg0.remote_ip4n
228 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
229 ip_range_end):
230 # add host route so dest_ip4n will not be resolved
Ole Troana5b2eec2019-03-11 19:23:25 +0100231 cls.vapi.ip_add_del_route(dst_address=dest_ip4n,
232 dst_address_length=32,
233 next_hop_address=next_hop_address)
Hongjun Nief486b12017-04-12 19:21:16 +0800234 r = cls.vapi.gtpu_add_del_tunnel(
235 src_addr=cls.pg0.local_ip4n,
236 dst_addr=dest_ip4n,
237 teid=teid)
Ole Troana5b2eec2019-03-11 19:23:25 +0100238 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
239 bd_id=teid)
Hongjun Nief486b12017-04-12 19:21:16 +0800240
241 @classmethod
242 def add_del_shared_mcast_dst_load(cls, is_add):
243 """
244 add or del tunnels sharing the same mcast dst
245 to test gtpu ref_count mechanism
246 """
247 n_shared_dst_tunnels = 20
248 teid_start = 1000
249 teid_end = teid_start + n_shared_dst_tunnels
250 for teid in range(teid_start, teid_end):
251 r = cls.vapi.gtpu_add_del_tunnel(
252 src_addr=cls.pg0.local_ip4n,
253 dst_addr=cls.mcast_ip4n,
254 mcast_sw_if_index=1,
255 teid=teid,
256 is_add=is_add)
257 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700258 raise ValueError("bad sw_if_index: ~0")
Hongjun Nief486b12017-04-12 19:21:16 +0800259
260 @classmethod
261 def add_shared_mcast_dst_load(cls):
262 cls.add_del_shared_mcast_dst_load(is_add=1)
263
264 @classmethod
265 def del_shared_mcast_dst_load(cls):
266 cls.add_del_shared_mcast_dst_load(is_add=0)
267
268 @classmethod
269 def add_del_mcast_tunnels_load(cls, is_add):
270 """
271 add or del tunnels to test gtpu stability
272 """
273 n_distinct_dst_tunnels = 20
274 ip_range_start = 10
275 ip_range_end = ip_range_start + n_distinct_dst_tunnels
276 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
277 ip_range_end):
278 teid = bytearray(dest_ip4n)[3]
279 cls.vapi.gtpu_add_del_tunnel(
280 src_addr=cls.pg0.local_ip4n,
281 dst_addr=dest_ip4n,
282 mcast_sw_if_index=1,
283 teid=teid,
284 is_add=is_add)
285
286 @classmethod
287 def add_mcast_tunnels_load(cls):
288 cls.add_del_mcast_tunnels_load(is_add=1)
289
290 @classmethod
291 def del_mcast_tunnels_load(cls):
292 cls.add_del_mcast_tunnels_load(is_add=0)
293
294 # Class method to start the GTPU test case.
295 # Overrides setUpClass method in VppTestCase class.
296 # Python try..except statement is used to ensure that the tear down of
297 # the class will be executed even if exception is raised.
298 # @param cls The class pointer.
299 @classmethod
300 def setUpClass(cls):
301 super(TestGtpu, cls).setUpClass()
302
303 try:
304 cls.dport = 2152
305 cls.gtp_type = 0xff
306
307 # Create 2 pg interfaces.
308 cls.create_pg_interfaces(range(4))
309 for pg in cls.pg_interfaces:
310 pg.admin_up()
311
312 # Configure IPv4 addresses on VPP pg0.
313 cls.pg0.config_ip4()
314
315 # Resolve MAC address for VPP's IP address on pg0.
316 cls.pg0.resolve_arp()
317
318 # Our Multicast address
319 cls.mcast_ip4 = '239.1.1.1'
320 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
321 iplong = atol(cls.mcast_ip4)
322 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
323 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
324
325 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
326 # into BD.
327 cls.single_tunnel_bd = 11
328 r = cls.vapi.gtpu_add_del_tunnel(
329 src_addr=cls.pg0.local_ip4n,
330 dst_addr=cls.pg0.remote_ip4n,
331 teid=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100332 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Nief486b12017-04-12 19:21:16 +0800333 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100334 cls.vapi.sw_interface_set_l2_bridge(
335 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800336
337 # Setup teid 2 to test multicast flooding
338 cls.n_ucast_tunnels = 10
339 cls.mcast_flood_bd = 12
340 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
341 cls.n_ucast_tunnels)
342 r = cls.vapi.gtpu_add_del_tunnel(
343 src_addr=cls.pg0.local_ip4n,
344 dst_addr=cls.mcast_ip4n,
345 mcast_sw_if_index=1,
346 teid=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100347 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Nief486b12017-04-12 19:21:16 +0800348 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100349 cls.vapi.sw_interface_set_l2_bridge(
350 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800351
352 # Add and delete mcast tunnels to check stability
353 cls.add_shared_mcast_dst_load()
354 cls.add_mcast_tunnels_load()
355 cls.del_shared_mcast_dst_load()
356 cls.del_mcast_tunnels_load()
357
358 # Setup teid 3 to test unicast flooding
359 cls.ucast_flood_bd = 13
360 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
361 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100362 cls.vapi.sw_interface_set_l2_bridge(
363 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Hongjun Nief486b12017-04-12 19:21:16 +0800364 except Exception:
365 super(TestGtpu, cls).tearDownClass()
366 raise
367
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700368 @classmethod
369 def tearDownClass(cls):
370 super(TestGtpu, cls).tearDownClass()
371
Hongjun Nief486b12017-04-12 19:21:16 +0800372 # Method to define VPP actions before tear down of the test case.
373 # Overrides tearDown method in VppTestCase class.
374 # @param self The object pointer.
375 def tearDown(self):
376 super(TestGtpu, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700377
378 def show_commands_at_teardown(self):
379 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
380 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
381 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
382 self.logger.info(self.vapi.cli("show int"))
383 self.logger.info(self.vapi.cli("show gtpu tunnel"))
384 self.logger.info(self.vapi.cli("show trace"))
Hongjun Nief486b12017-04-12 19:21:16 +0800385
386
387if __name__ == '__main__':
388 unittest.main(testRunner=VppTestRunner)