tests: tag the tests that do not work with multi-worker configuration
[vpp.git] / src / plugins / gtpu / test / test_gtpu.py
1 #!/usr/bin/env python3
2
3 import socket
4 from util import ip4_range
5 import unittest
6 from framework import tag_fixme_vpp_workers
7 from framework import VppTestCase, VppTestRunner
8 from template_bd import BridgeDomain
9
10 from scapy.layers.l2 import Ether
11 from scapy.packet import Raw
12 from scapy.layers.inet import IP, UDP
13 from scapy.layers.inet6 import IPv6
14 from scapy.contrib.gtp import GTP_U_Header
15 from scapy.utils import atol
16
17 import util
18 from vpp_ip_route import VppIpRoute, VppRoutePath
19 from vpp_ip import INVALID_INDEX
20
21
22 @tag_fixme_vpp_workers
23 class TestGtpuUDP(VppTestCase):
24     """ GTPU UDP ports Test Case """
25
26     def setUp(self):
27         super(TestGtpuUDP, self).setUp()
28
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(
50             '/err/ip4-udp-lookup/No listener for dst port')[0]
51
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(
69             '/err/ip6-udp-lookup/No listener for dst port')[0]
70
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         """
82         # 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
86         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)
91
92         # UDP port 2152 enabled for ip4
93         self._check_udp_port_ip4()
94
95         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)
100
101         # UDP port 2152 enabled for ip6
102         self._check_udp_port_ip6()
103
104         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)
109
110         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)
115
116
117 class 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) /
132                 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
133                 pkt)
134
135     def ip_range(self, start, end):
136         """ range of remote ip's """
137         return ip4_range(self.pg0.remote_ip4, start, end)
138
139     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) /
147                 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
148                 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)
177         # Verify teid
178         self.assertEqual(pkt[GTP_U_Header].teid, vni)
179
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
191         # Pick first received frame and check if it's correctly encapsulated.
192         out = self.pg0.get_capture(1)
193         pkt = out[0]
194         self.check_encapsulation(pkt, self.single_tunnel_vni)
195
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
210         # Get packet from each tunnel and assert it's correctly encapsulated.
211         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
228         # Pick first received frame and check if it's correctly encapsulated.
229         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
242         next_hop_address = cls.pg0.remote_ip4
243         for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
244                                   ip_range_end):
245             # add host route so dest_ip4 will not be resolved
246             rip = VppIpRoute(cls, dest_ip4, 32,
247                              [VppRoutePath(next_hop_address,
248                                            INVALID_INDEX)],
249                              register=False)
250             rip.add_vpp_config()
251             r = cls.vapi.gtpu_add_del_tunnel(
252                 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,
257                 teid=teid)
258             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
259                                                 bd_id=teid)
260
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(
272                 decap_next_index=0xFFFFFFFF,
273                 src_address=cls.pg0.local_ip4,
274                 dst_address=cls.mcast_ip4,
275                 mcast_sw_if_index=1,
276                 teid=teid,
277                 is_add=is_add)
278             if r.sw_if_index == 0xffffffff:
279                 raise ValueError("bad sw_if_index: ~0")
280
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
297         for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
298                                   ip_range_end):
299             teid = int(dest_ip4.split('.')[3])
300             cls.vapi.gtpu_add_del_tunnel(
301                 decap_next_index=0xFFFFFFFF,
302                 src_address=cls.pg0.local_ip4,
303                 dst_address=dest_ip4,
304                 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'
342             cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
343
344             # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
345             #  into BD.
346             cls.single_tunnel_bd = 11
347             cls.single_tunnel_vni = 11
348             r = cls.vapi.gtpu_add_del_tunnel(
349                 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,
354                 teid=cls.single_tunnel_vni)
355             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
356                                                 bd_id=cls.single_tunnel_bd)
357             cls.vapi.sw_interface_set_l2_bridge(
358                 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
359
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(
366                 is_add=True,
367                 src_address=cls.pg0.local_ip4,
368                 dst_address=cls.mcast_ip4,
369                 mcast_sw_if_index=1,
370                 decap_next_index=0xFFFFFFFF,
371                 teid=cls.mcast_flood_bd)
372             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
373                                                 bd_id=cls.mcast_flood_bd)
374             cls.vapi.sw_interface_set_l2_bridge(
375                 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
376
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)
387             cls.vapi.sw_interface_set_l2_bridge(
388                 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
389         except Exception:
390             super(TestGtpu, cls).tearDownClass()
391             raise
392
393     @classmethod
394     def tearDownClass(cls):
395         super(TestGtpu, cls).tearDownClass()
396
397     # 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()
402
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"))
410
411
412 if __name__ == '__main__':
413     unittest.main(testRunner=VppTestRunner)