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