MAP: Convert from DPO to input feature.
[vpp.git] / test / test_gtpu.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range, ip4_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP
11 from scapy.contrib.gtp import GTP_U_Header
12 from scapy.utils import atol
13
14
15 class TestGtpu(BridgeDomain, VppTestCase):
16     """ GTPU Test Case """
17
18     def __init__(self, *args):
19         BridgeDomain.__init__(self)
20         VppTestCase.__init__(self, *args)
21
22     def encapsulate(self, pkt, vni):
23         """
24         Encapsulate the original payload frame by adding GTPU header with its
25         UDP, IP and Ethernet fields
26         """
27         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
30                 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
31                 pkt)
32
33     def ip_range(self, start, end):
34         """ range of remote ip's """
35         return ip4_range(self.pg0.remote_ip4, start, end)
36
37     def encap_mcast(self, pkt, src_ip, src_mac, vni):
38         """
39         Encapsulate the original payload frame by adding GTPU header with its
40         UDP, IP and Ethernet fields
41         """
42         return (Ether(src=src_mac, dst=self.mcast_mac) /
43                 IP(src=src_ip, dst=self.mcast_ip4) /
44                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
45                 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
46                 pkt)
47
48     def decapsulate(self, pkt):
49         """
50         Decapsulate the original payload frame by removing GTPU header
51         """
52         return pkt[GTP_U_Header].payload
53
54     # Method for checking GTPU encapsulation.
55     #
56     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
57         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
58         #  by VPP using ARP.
59         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
60         if not local_only:
61             if not mcast_pkt:
62                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
63             else:
64                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
65         # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
66         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
67         if not local_only:
68             if not mcast_pkt:
69                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
70             else:
71                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
72         # Verify UDP destination port is GTPU 2152, source UDP port could be
73         #  arbitrary.
74         self.assertEqual(pkt[UDP].dport, type(self).dport)
75         # Verify teid
76         self.assertEqual(pkt[GTP_U_Header].teid, vni)
77
78     def test_encap(self):
79         """ Encapsulation test
80         Send frames from pg1
81         Verify receipt of encapsulated frames on pg0
82         """
83         self.pg1.add_stream([self.frame_reply])
84
85         self.pg0.enable_capture()
86
87         self.pg_start()
88
89         # Pick first received frame and check if it's corectly encapsulated.
90         out = self.pg0.get_capture(1)
91         pkt = out[0]
92         self.check_encapsulation(pkt, self.single_tunnel_bd)
93
94         # payload = self.decapsulate(pkt)
95         # self.assert_eq_pkts(payload, self.frame_reply)
96
97     def test_ucast_flood(self):
98         """ Unicast flood test
99         Send frames from pg3
100         Verify receipt of encapsulated frames on pg0
101         """
102         self.pg3.add_stream([self.frame_reply])
103
104         self.pg0.enable_capture()
105
106         self.pg_start()
107
108         # Get packet from each tunnel and assert it's corectly encapsulated.
109         out = self.pg0.get_capture(self.n_ucast_tunnels)
110         for pkt in out:
111             self.check_encapsulation(pkt, self.ucast_flood_bd, True)
112             # payload = self.decapsulate(pkt)
113             # self.assert_eq_pkts(payload, self.frame_reply)
114
115     def test_mcast_flood(self):
116         """ Multicast flood test
117         Send frames from pg2
118         Verify receipt of encapsulated frames on pg0
119         """
120         self.pg2.add_stream([self.frame_reply])
121
122         self.pg0.enable_capture()
123
124         self.pg_start()
125
126         # Pick first received frame and check if it's corectly encapsulated.
127         out = self.pg0.get_capture(1)
128         pkt = out[0]
129         self.check_encapsulation(pkt, self.mcast_flood_bd,
130                                  local_only=False, mcast_pkt=True)
131
132         # payload = self.decapsulate(pkt)
133         # self.assert_eq_pkts(payload, self.frame_reply)
134
135     @classmethod
136     def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
137         # Create 10 ucast gtpu tunnels under bd
138         ip_range_start = 10
139         ip_range_end = ip_range_start + n_ucast_tunnels
140         next_hop_address = cls.pg0.remote_ip4n
141         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
142                                     ip_range_end):
143             # add host route so dest_ip4n will not be resolved
144             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
145             r = cls.vapi.gtpu_add_del_tunnel(
146                 src_addr=cls.pg0.local_ip4n,
147                 dst_addr=dest_ip4n,
148                 teid=teid)
149             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=teid)
150
151     @classmethod
152     def add_del_shared_mcast_dst_load(cls, is_add):
153         """
154         add or del tunnels sharing the same mcast dst
155         to test gtpu ref_count mechanism
156         """
157         n_shared_dst_tunnels = 20
158         teid_start = 1000
159         teid_end = teid_start + n_shared_dst_tunnels
160         for teid in range(teid_start, teid_end):
161             r = cls.vapi.gtpu_add_del_tunnel(
162                 src_addr=cls.pg0.local_ip4n,
163                 dst_addr=cls.mcast_ip4n,
164                 mcast_sw_if_index=1,
165                 teid=teid,
166                 is_add=is_add)
167             if r.sw_if_index == 0xffffffff:
168                 raise "bad sw_if_index"
169
170     @classmethod
171     def add_shared_mcast_dst_load(cls):
172         cls.add_del_shared_mcast_dst_load(is_add=1)
173
174     @classmethod
175     def del_shared_mcast_dst_load(cls):
176         cls.add_del_shared_mcast_dst_load(is_add=0)
177
178     @classmethod
179     def add_del_mcast_tunnels_load(cls, is_add):
180         """
181         add or del tunnels to test gtpu stability
182         """
183         n_distinct_dst_tunnels = 20
184         ip_range_start = 10
185         ip_range_end = ip_range_start + n_distinct_dst_tunnels
186         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
187                                     ip_range_end):
188             teid = bytearray(dest_ip4n)[3]
189             cls.vapi.gtpu_add_del_tunnel(
190                 src_addr=cls.pg0.local_ip4n,
191                 dst_addr=dest_ip4n,
192                 mcast_sw_if_index=1,
193                 teid=teid,
194                 is_add=is_add)
195
196     @classmethod
197     def add_mcast_tunnels_load(cls):
198         cls.add_del_mcast_tunnels_load(is_add=1)
199
200     @classmethod
201     def del_mcast_tunnels_load(cls):
202         cls.add_del_mcast_tunnels_load(is_add=0)
203
204     # Class method to start the GTPU test case.
205     #  Overrides setUpClass method in VppTestCase class.
206     #  Python try..except statement is used to ensure that the tear down of
207     #  the class will be executed even if exception is raised.
208     #  @param cls The class pointer.
209     @classmethod
210     def setUpClass(cls):
211         super(TestGtpu, cls).setUpClass()
212
213         try:
214             cls.dport = 2152
215             cls.gtp_type = 0xff
216
217             # Create 2 pg interfaces.
218             cls.create_pg_interfaces(range(4))
219             for pg in cls.pg_interfaces:
220                 pg.admin_up()
221
222             # Configure IPv4 addresses on VPP pg0.
223             cls.pg0.config_ip4()
224
225             # Resolve MAC address for VPP's IP address on pg0.
226             cls.pg0.resolve_arp()
227
228             # Our Multicast address
229             cls.mcast_ip4 = '239.1.1.1'
230             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
231             iplong = atol(cls.mcast_ip4)
232             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
233                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
234
235             # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
236             #  into BD.
237             cls.single_tunnel_bd = 11
238             r = cls.vapi.gtpu_add_del_tunnel(
239                 src_addr=cls.pg0.local_ip4n,
240                 dst_addr=cls.pg0.remote_ip4n,
241                 teid=cls.single_tunnel_bd)
242             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
243                                                 bd_id=cls.single_tunnel_bd)
244             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
245                                                 bd_id=cls.single_tunnel_bd)
246
247             # Setup teid 2 to test multicast flooding
248             cls.n_ucast_tunnels = 10
249             cls.mcast_flood_bd = 12
250             cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
251                                           cls.n_ucast_tunnels)
252             r = cls.vapi.gtpu_add_del_tunnel(
253                 src_addr=cls.pg0.local_ip4n,
254                 dst_addr=cls.mcast_ip4n,
255                 mcast_sw_if_index=1,
256                 teid=cls.mcast_flood_bd)
257             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
258                                                 bd_id=cls.mcast_flood_bd)
259             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
260                                                 bd_id=cls.mcast_flood_bd)
261
262             # Add and delete mcast tunnels to check stability
263             cls.add_shared_mcast_dst_load()
264             cls.add_mcast_tunnels_load()
265             cls.del_shared_mcast_dst_load()
266             cls.del_mcast_tunnels_load()
267
268             # Setup teid 3 to test unicast flooding
269             cls.ucast_flood_bd = 13
270             cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
271                                           cls.n_ucast_tunnels)
272             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
273                                                 bd_id=cls.ucast_flood_bd)
274         except Exception:
275             super(TestGtpu, cls).tearDownClass()
276             raise
277
278     # Method to define VPP actions before tear down of the test case.
279     #  Overrides tearDown method in VppTestCase class.
280     #  @param self The object pointer.
281     def tearDown(self):
282         super(TestGtpu, self).tearDown()
283         if not self.vpp_dead:
284             self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
285             self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
286             self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
287             self.logger.info(self.vapi.cli("show int"))
288             self.logger.info(self.vapi.cli("show gtpu tunnel"))
289             self.logger.info(self.vapi.cli("show trace"))
290
291
292 if __name__ == '__main__':
293     unittest.main(testRunner=VppTestRunner)